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 3326f54e9aSMarkus Armbruster #include "sysemu/block-backend.h" 349c17d615SPaolo Bonzini #include "sysemu/blockdev.h" 350d09e41aSPaolo Bonzini #include "hw/block/block.h" 36737e150eSPaolo Bonzini #include "block/blockjob.h" 3776f4afb4SAlberto Garcia #include "block/throttle-groups.h" 3883c9089eSPaolo Bonzini #include "monitor/monitor.h" 39d49b6836SMarkus Armbruster #include "qemu/error-report.h" 401de7afc9SPaolo Bonzini #include "qemu/option.h" 411de7afc9SPaolo Bonzini #include "qemu/config-file.h" 427b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 43d26c9a15SKevin Wolf #include "qapi-visit.h" 44cc7a8ea7SMarkus Armbruster #include "qapi/qmp/qerror.h" 45d26c9a15SKevin Wolf #include "qapi/qmp-output-visitor.h" 469e7dac7cSPeter Lieven #include "qapi/util.h" 479c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 48737e150eSPaolo Bonzini #include "block/block_int.h" 49a4dea8a9SLuiz Capitulino #include "qmp-commands.h" 5012bd451fSStefan Hajnoczi #include "trace.h" 519c17d615SPaolo Bonzini #include "sysemu/arch_init.h" 52666daa68SMarkus Armbruster 531960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 541960966dSMarkus Armbruster [IF_NONE] = "none", 551960966dSMarkus Armbruster [IF_IDE] = "ide", 561960966dSMarkus Armbruster [IF_SCSI] = "scsi", 571960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 581960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 591960966dSMarkus Armbruster [IF_MTD] = "mtd", 601960966dSMarkus Armbruster [IF_SD] = "sd", 611960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 621960966dSMarkus Armbruster [IF_XEN] = "xen", 631960966dSMarkus Armbruster }; 641960966dSMarkus Armbruster 6521dff8cfSJohn Snow static int if_max_devs[IF_COUNT] = { 6627d6bf40SMarkus Armbruster /* 6727d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 6827d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 6927d6bf40SMarkus Armbruster * 7027d6bf40SMarkus Armbruster * All controllers used to imlement if=T drives need to support 7127d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 7227d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 7327d6bf40SMarkus Armbruster * values. 7427d6bf40SMarkus Armbruster * 7527d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 7627d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 7727d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 7827d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 7927d6bf40SMarkus Armbruster */ 8027d6bf40SMarkus Armbruster [IF_IDE] = 2, 8127d6bf40SMarkus Armbruster [IF_SCSI] = 7, 821960966dSMarkus Armbruster }; 831960966dSMarkus Armbruster 8421dff8cfSJohn Snow /** 8521dff8cfSJohn Snow * Boards may call this to offer board-by-board overrides 8621dff8cfSJohn Snow * of the default, global values. 8721dff8cfSJohn Snow */ 8821dff8cfSJohn Snow void override_max_devs(BlockInterfaceType type, int max_devs) 8921dff8cfSJohn Snow { 9018e46a03SMarkus Armbruster BlockBackend *blk; 9121dff8cfSJohn Snow DriveInfo *dinfo; 9221dff8cfSJohn Snow 9321dff8cfSJohn Snow if (max_devs <= 0) { 9421dff8cfSJohn Snow return; 9521dff8cfSJohn Snow } 9621dff8cfSJohn Snow 9718e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 9818e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 9921dff8cfSJohn Snow if (dinfo->type == type) { 10021dff8cfSJohn Snow fprintf(stderr, "Cannot override units-per-bus property of" 10121dff8cfSJohn Snow " the %s interface, because a drive of that type has" 10221dff8cfSJohn Snow " already been added.\n", if_name[type]); 10321dff8cfSJohn Snow g_assert_not_reached(); 10421dff8cfSJohn Snow } 10521dff8cfSJohn Snow } 10621dff8cfSJohn Snow 10721dff8cfSJohn Snow if_max_devs[type] = max_devs; 10821dff8cfSJohn Snow } 10921dff8cfSJohn Snow 11014bafc54SMarkus Armbruster /* 11114bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 11214bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 11314bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 11414bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 11514bafc54SMarkus Armbruster * when deletion is actually safe. 11614bafc54SMarkus Armbruster */ 1174be74634SMarkus Armbruster void blockdev_mark_auto_del(BlockBackend *blk) 11814bafc54SMarkus Armbruster { 11918e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 1204be74634SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 12191fddb0dSStefan Hajnoczi AioContext *aio_context; 12214bafc54SMarkus Armbruster 12326f8b3a8SMarkus Armbruster if (!dinfo) { 1242d246f01SKevin Wolf return; 1252d246f01SKevin Wolf } 1262d246f01SKevin Wolf 1275433c24fSMax Reitz if (bs) { 12891fddb0dSStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 12991fddb0dSStefan Hajnoczi aio_context_acquire(aio_context); 13091fddb0dSStefan Hajnoczi 13112bde0eeSPaolo Bonzini if (bs->job) { 13212bde0eeSPaolo Bonzini block_job_cancel(bs->job); 13312bde0eeSPaolo Bonzini } 13491fddb0dSStefan Hajnoczi 13591fddb0dSStefan Hajnoczi aio_context_release(aio_context); 1365433c24fSMax Reitz } 13791fddb0dSStefan Hajnoczi 13814bafc54SMarkus Armbruster dinfo->auto_del = 1; 13914bafc54SMarkus Armbruster } 14014bafc54SMarkus Armbruster 1414be74634SMarkus Armbruster void blockdev_auto_del(BlockBackend *blk) 14214bafc54SMarkus Armbruster { 14318e46a03SMarkus Armbruster DriveInfo *dinfo = blk_legacy_dinfo(blk); 14414bafc54SMarkus Armbruster 1450fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 146b9fe8a7aSMarkus Armbruster blk_unref(blk); 14714bafc54SMarkus Armbruster } 14814bafc54SMarkus Armbruster } 14914bafc54SMarkus Armbruster 150d8f94e1bSJohn Snow /** 151d8f94e1bSJohn Snow * Returns the current mapping of how many units per bus 152d8f94e1bSJohn Snow * a particular interface can support. 153d8f94e1bSJohn Snow * 154d8f94e1bSJohn Snow * A positive integer indicates n units per bus. 155d8f94e1bSJohn Snow * 0 implies the mapping has not been established. 156d8f94e1bSJohn Snow * -1 indicates an invalid BlockInterfaceType was given. 157d8f94e1bSJohn Snow */ 158d8f94e1bSJohn Snow int drive_get_max_devs(BlockInterfaceType type) 159d8f94e1bSJohn Snow { 160d8f94e1bSJohn Snow if (type >= IF_IDE && type < IF_COUNT) { 161d8f94e1bSJohn Snow return if_max_devs[type]; 162d8f94e1bSJohn Snow } 163d8f94e1bSJohn Snow 164d8f94e1bSJohn Snow return -1; 165d8f94e1bSJohn Snow } 166d8f94e1bSJohn Snow 167505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 168505a7fb1SMarkus Armbruster { 169505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 170505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 171505a7fb1SMarkus Armbruster } 172505a7fb1SMarkus Armbruster 173505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 174505a7fb1SMarkus Armbruster { 175505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 176505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 177505a7fb1SMarkus Armbruster } 178505a7fb1SMarkus Armbruster 1792292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 1802292ddaeSMarkus Armbruster { 18170b94331SMarkus Armbruster return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 1822292ddaeSMarkus Armbruster } 1832292ddaeSMarkus Armbruster 1842292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1855645b0f4SMarkus Armbruster const char *optstr) 186666daa68SMarkus Armbruster { 187666daa68SMarkus Armbruster QemuOpts *opts; 188666daa68SMarkus Armbruster 1892292ddaeSMarkus Armbruster opts = drive_def(optstr); 190666daa68SMarkus Armbruster if (!opts) { 191666daa68SMarkus Armbruster return NULL; 192666daa68SMarkus Armbruster } 1932292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 194f43e47dbSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type], &error_abort); 1952292ddaeSMarkus Armbruster } 1962292ddaeSMarkus Armbruster if (index >= 0) { 197a8b18f8fSMarkus Armbruster qemu_opt_set_number(opts, "index", index, &error_abort); 1982292ddaeSMarkus Armbruster } 199666daa68SMarkus Armbruster if (file) 200f43e47dbSMarkus Armbruster qemu_opt_set(opts, "file", file, &error_abort); 201666daa68SMarkus Armbruster return opts; 202666daa68SMarkus Armbruster } 203666daa68SMarkus Armbruster 204666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 205666daa68SMarkus Armbruster { 20618e46a03SMarkus Armbruster BlockBackend *blk; 207666daa68SMarkus Armbruster DriveInfo *dinfo; 208666daa68SMarkus Armbruster 20918e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 21018e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 21118e46a03SMarkus Armbruster if (dinfo && dinfo->type == type 21218e46a03SMarkus Armbruster && dinfo->bus == bus && dinfo->unit == unit) { 213666daa68SMarkus Armbruster return dinfo; 214666daa68SMarkus Armbruster } 21518e46a03SMarkus Armbruster } 216666daa68SMarkus Armbruster 217666daa68SMarkus Armbruster return NULL; 218666daa68SMarkus Armbruster } 219666daa68SMarkus Armbruster 220a66c9dc7SJohn Snow bool drive_check_orphaned(void) 221a66c9dc7SJohn Snow { 22218e46a03SMarkus Armbruster BlockBackend *blk; 223a66c9dc7SJohn Snow DriveInfo *dinfo; 224a66c9dc7SJohn Snow bool rs = false; 225a66c9dc7SJohn Snow 22618e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 22718e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 228a66c9dc7SJohn Snow /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 229a66c9dc7SJohn Snow /* Unless this is a default drive, this may be an oversight. */ 230a7f53e26SMarkus Armbruster if (!blk_get_attached_dev(blk) && !dinfo->is_default && 231a66c9dc7SJohn Snow dinfo->type != IF_NONE) { 232a66c9dc7SJohn Snow fprintf(stderr, "Warning: Orphaned drive without device: " 233a66c9dc7SJohn Snow "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 2345433c24fSMax Reitz blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 2355433c24fSMax Reitz if_name[dinfo->type], dinfo->bus, dinfo->unit); 236a66c9dc7SJohn Snow rs = true; 237a66c9dc7SJohn Snow } 238a66c9dc7SJohn Snow } 239a66c9dc7SJohn Snow 240a66c9dc7SJohn Snow return rs; 241a66c9dc7SJohn Snow } 242a66c9dc7SJohn Snow 243f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 244f1bd51acSMarkus Armbruster { 245f1bd51acSMarkus Armbruster return drive_get(type, 246f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 247f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 248f1bd51acSMarkus Armbruster } 249f1bd51acSMarkus Armbruster 250666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 251666daa68SMarkus Armbruster { 252666daa68SMarkus Armbruster int max_bus; 25318e46a03SMarkus Armbruster BlockBackend *blk; 254666daa68SMarkus Armbruster DriveInfo *dinfo; 255666daa68SMarkus Armbruster 256666daa68SMarkus Armbruster max_bus = -1; 25718e46a03SMarkus Armbruster for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 25818e46a03SMarkus Armbruster dinfo = blk_legacy_dinfo(blk); 25918e46a03SMarkus Armbruster if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 260666daa68SMarkus Armbruster max_bus = dinfo->bus; 261666daa68SMarkus Armbruster } 26218e46a03SMarkus Armbruster } 263666daa68SMarkus Armbruster return max_bus; 264666daa68SMarkus Armbruster } 265666daa68SMarkus Armbruster 26613839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 26713839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 26813839974SMarkus Armbruster appropriate bus. */ 26913839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 27013839974SMarkus Armbruster { 27113839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 27213839974SMarkus Armbruster 27313839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 27413839974SMarkus Armbruster } 27513839974SMarkus Armbruster 276666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 277666daa68SMarkus Armbruster { 278807105a7SMarkus Armbruster error_printf(" %s", name); 279666daa68SMarkus Armbruster } 280666daa68SMarkus Armbruster 281aa398a5cSStefan Hajnoczi typedef struct { 282aa398a5cSStefan Hajnoczi QEMUBH *bh; 283fa510ebfSFam Zheng BlockDriverState *bs; 284fa510ebfSFam Zheng } BDRVPutRefBH; 285aa398a5cSStefan Hajnoczi 286b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 287666daa68SMarkus Armbruster { 288666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 28992aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 290666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 29192aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 292666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 29392aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 294666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 29592aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 296666daa68SMarkus Armbruster } else { 297b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 298666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 299666daa68SMarkus Armbruster return -1; 300666daa68SMarkus Armbruster } 301666daa68SMarkus Armbruster } 302666daa68SMarkus Armbruster 30340119effSAlberto Garcia static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 30440119effSAlberto Garcia Error **errp) 30540119effSAlberto Garcia { 30640119effSAlberto Garcia const QListEntry *entry; 30740119effSAlberto Garcia for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 30840119effSAlberto Garcia switch (qobject_type(entry->value)) { 30940119effSAlberto Garcia 31040119effSAlberto Garcia case QTYPE_QSTRING: { 31140119effSAlberto Garcia unsigned long long length; 31240119effSAlberto Garcia const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 31340119effSAlberto Garcia if (parse_uint_full(str, &length, 10) == 0 && 31440119effSAlberto Garcia length > 0 && length <= UINT_MAX) { 31540119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 31640119effSAlberto Garcia } else { 31740119effSAlberto Garcia error_setg(errp, "Invalid interval length: %s", str); 31840119effSAlberto Garcia return false; 31940119effSAlberto Garcia } 32040119effSAlberto Garcia break; 32140119effSAlberto Garcia } 32240119effSAlberto Garcia 32340119effSAlberto Garcia case QTYPE_QINT: { 32440119effSAlberto Garcia int64_t length = qint_get_int(qobject_to_qint(entry->value)); 32540119effSAlberto Garcia if (length > 0 && length <= UINT_MAX) { 32640119effSAlberto Garcia block_acct_add_interval(stats, (unsigned) length); 32740119effSAlberto Garcia } else { 32840119effSAlberto Garcia error_setg(errp, "Invalid interval length: %" PRId64, length); 32940119effSAlberto Garcia return false; 33040119effSAlberto Garcia } 33140119effSAlberto Garcia break; 33240119effSAlberto Garcia } 33340119effSAlberto Garcia 33440119effSAlberto Garcia default: 33540119effSAlberto Garcia error_setg(errp, "The specification of stats-intervals is invalid"); 33640119effSAlberto Garcia return false; 33740119effSAlberto Garcia } 33840119effSAlberto Garcia } 33940119effSAlberto Garcia return true; 34040119effSAlberto Garcia } 34140119effSAlberto Garcia 342cc0681c4SBenoît Canet static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) 3430563e191SZhi Yong Wu { 344cc0681c4SBenoît Canet if (throttle_conflicting(cfg)) { 345cc0681c4SBenoît Canet error_setg(errp, "bps/iops/max total values and read/write values" 346c546194fSStefan Hajnoczi " cannot be used at the same time"); 3470563e191SZhi Yong Wu return false; 3480563e191SZhi Yong Wu } 3490563e191SZhi Yong Wu 350cc0681c4SBenoît Canet if (!throttle_is_valid(cfg)) { 351cc0681c4SBenoît Canet error_setg(errp, "bps/iops/maxs values must be 0 or greater"); 3527d81c141SStefan Hajnoczi return false; 3537d81c141SStefan Hajnoczi } 3547d81c141SStefan Hajnoczi 355ee2bdc33SStefan Hajnoczi if (throttle_max_is_missing_limit(cfg)) { 356ee2bdc33SStefan Hajnoczi error_setg(errp, "bps_max/iops_max require corresponding" 357ee2bdc33SStefan Hajnoczi " bps/iops values"); 358ee2bdc33SStefan Hajnoczi return false; 359ee2bdc33SStefan Hajnoczi } 360ee2bdc33SStefan Hajnoczi 3610563e191SZhi Yong Wu return true; 3620563e191SZhi Yong Wu } 3630563e191SZhi Yong Wu 36433cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 36533cb7dc8SKevin Wolf 366fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 367fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 368fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 369fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 370fbf8175eSMax Reitz { 371fbf8175eSMax Reitz const char *discard; 372fbf8175eSMax Reitz Error *local_error = NULL; 373fbf8175eSMax Reitz const char *aio; 374fbf8175eSMax Reitz 375fbf8175eSMax Reitz if (bdrv_flags) { 376fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 377fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 378fbf8175eSMax Reitz } 379fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 380fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 381fbf8175eSMax Reitz } 382fbf8175eSMax Reitz 383fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 384fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 385fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 386fbf8175eSMax Reitz return; 387fbf8175eSMax Reitz } 388fbf8175eSMax Reitz } 389fbf8175eSMax Reitz 390fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 391fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 392fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 393fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 394fbf8175eSMax Reitz /* this is the default */ 395fbf8175eSMax Reitz } else { 396fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 397fbf8175eSMax Reitz return; 398fbf8175eSMax Reitz } 399fbf8175eSMax Reitz } 400fbf8175eSMax Reitz } 401fbf8175eSMax Reitz 402fbf8175eSMax Reitz /* disk I/O throttling */ 403fbf8175eSMax Reitz if (throttling_group) { 404fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 405fbf8175eSMax Reitz } 406fbf8175eSMax Reitz 407fbf8175eSMax Reitz if (throttle_cfg) { 408fbf8175eSMax Reitz memset(throttle_cfg, 0, sizeof(*throttle_cfg)); 409fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 410fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 411fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 412fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 413fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 414fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 415fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 416fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 417fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 418fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 421fbf8175eSMax Reitz 422fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 423fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 424fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 425fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 426fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 427fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 428fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 429fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 430fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 431fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 432fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 433fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 434fbf8175eSMax Reitz 435fbf8175eSMax Reitz throttle_cfg->op_size = 436fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 437fbf8175eSMax Reitz 438fbf8175eSMax Reitz if (!check_throttle_config(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; 47426f54e9aSMarkus Armbruster BlockBackend *blk; 475a0f1eab1SMarkus Armbruster BlockDriverState *bs; 476cc0681c4SBenoît Canet ThrottleConfig cfg; 477666daa68SMarkus Armbruster int snapshot = 0; 478c546194fSStefan Hajnoczi Error *error = NULL; 4790006383eSKevin Wolf QemuOpts *opts; 48040119effSAlberto Garcia QDict *interval_dict = NULL; 48140119effSAlberto Garcia QList *interval_list = NULL; 4820006383eSKevin Wolf const char *id; 483fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 484fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 485fbf8175eSMax Reitz const char *throttling_group = NULL; 486666daa68SMarkus Armbruster 487f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 488f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 489f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4900006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49184d18f06SMarkus Armbruster if (error) { 492b681072dSKevin Wolf error_propagate(errp, error); 4936376f952SMarkus Armbruster goto err_no_opts; 4940006383eSKevin Wolf } 4950006383eSKevin Wolf 4960006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49784d18f06SMarkus Armbruster if (error) { 498b681072dSKevin Wolf error_propagate(errp, error); 499ec9c10d2SStefan Hajnoczi goto early_err; 5000006383eSKevin Wolf } 5010006383eSKevin Wolf 5020006383eSKevin Wolf if (id) { 5030006383eSKevin Wolf qdict_del(bs_opts, "id"); 5040006383eSKevin Wolf } 5050006383eSKevin Wolf 506666daa68SMarkus Armbruster /* extract parameters */ 507666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 508666daa68SMarkus Armbruster 509362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 510362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 511362e9299SAlberto Garcia 51240119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51340119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51440119effSAlberto Garcia 51540119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51640119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51740119effSAlberto Garcia qdict_first(interval_dict)->key); 51840119effSAlberto Garcia goto early_err; 51940119effSAlberto Garcia } 5202be5506fSAlberto Garcia 521fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 522fbf8175eSMax Reitz &detect_zeroes, &error); 523fbf8175eSMax Reitz if (error) { 524fbf8175eSMax Reitz error_propagate(errp, error); 525ec9c10d2SStefan Hajnoczi goto early_err; 526a9384affSPaolo Bonzini } 527666daa68SMarkus Armbruster 528666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 529c8057f95SPeter Maydell if (is_help_option(buf)) { 530807105a7SMarkus Armbruster error_printf("Supported formats:"); 531666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 532807105a7SMarkus Armbruster error_printf("\n"); 533ec9c10d2SStefan Hajnoczi goto early_err; 534666daa68SMarkus Armbruster } 53574fe54f2SKevin Wolf 536e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 537e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 538ec9c10d2SStefan Hajnoczi goto early_err; 5396db5f5d6SMike Qiu } 540e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 541666daa68SMarkus Armbruster } 542666daa68SMarkus Armbruster 54392aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 544666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 545b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54684d18f06SMarkus Armbruster if (error) { 547b681072dSKevin Wolf error_propagate(errp, error); 548ec9c10d2SStefan Hajnoczi goto early_err; 549666daa68SMarkus Armbruster } 550666daa68SMarkus Armbruster } 551666daa68SMarkus Armbruster 55292aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 553666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 554b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55584d18f06SMarkus Armbruster if (error) { 556b681072dSKevin Wolf error_propagate(errp, error); 557ec9c10d2SStefan Hajnoczi goto early_err; 558666daa68SMarkus Armbruster } 559666daa68SMarkus Armbruster } 560666daa68SMarkus Armbruster 561666daa68SMarkus Armbruster if (snapshot) { 56291a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 563666daa68SMarkus Armbruster } 564666daa68SMarkus Armbruster 5655ec18f8cSMax Reitz /* init */ 56639c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5675ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5685ec18f8cSMax Reitz 5695ec18f8cSMax Reitz blk = blk_new(qemu_opts_id(opts), errp); 5705ec18f8cSMax Reitz if (!blk) { 5715ec18f8cSMax Reitz goto early_err; 5725ec18f8cSMax Reitz } 5735ec18f8cSMax Reitz 5745ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5755ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 576fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5775ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5785ec18f8cSMax Reitz 5795ec18f8cSMax Reitz if (throttle_enabled(&cfg)) { 5805ec18f8cSMax Reitz if (!throttling_group) { 5815ec18f8cSMax Reitz throttling_group = blk_name(blk); 5825ec18f8cSMax Reitz } 5835ec18f8cSMax Reitz blk_rs->throttle_group = g_strdup(throttling_group); 5845ec18f8cSMax Reitz blk_rs->throttle_state = throttle_group_incref(throttling_group); 5855ec18f8cSMax Reitz blk_rs->throttle_state->cfg = cfg; 5865ec18f8cSMax Reitz } 5875ec18f8cSMax Reitz 5885ec18f8cSMax Reitz QDECREF(bs_opts); 5895ec18f8cSMax Reitz } else { 5905ec18f8cSMax Reitz if (file && !*file) { 5915ec18f8cSMax Reitz file = NULL; 5925ec18f8cSMax Reitz } 5935ec18f8cSMax Reitz 59491a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 59591a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 59691a097e7SKevin Wolf * Apply the defaults here instead. */ 59791a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on"); 59891a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 59991a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 60091a097e7SKevin Wolf 60191a097e7SKevin Wolf if (snapshot) { 60291a097e7SKevin Wolf /* always use cache=unsafe with snapshot */ 60391a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on")); 60491a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off")); 60591a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on")); 60691a097e7SKevin Wolf } 60791a097e7SKevin Wolf 608e4342ce5SMax Reitz blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, 609e4342ce5SMax Reitz errp); 610e4342ce5SMax Reitz if (!blk) { 611e4342ce5SMax Reitz goto err_no_bs_opts; 612e4342ce5SMax Reitz } 613e4342ce5SMax Reitz bs = blk_bs(blk); 6140006383eSKevin Wolf 615e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 616e4342ce5SMax Reitz 617e4342ce5SMax Reitz /* disk I/O throttling */ 618e4342ce5SMax Reitz if (throttle_enabled(&cfg)) { 61976f4afb4SAlberto Garcia if (!throttling_group) { 62076f4afb4SAlberto Garcia throttling_group = blk_name(blk); 62176f4afb4SAlberto Garcia } 62276f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, throttling_group); 623e4342ce5SMax Reitz bdrv_set_io_limits(bs, &cfg); 624666daa68SMarkus Armbruster } 625666daa68SMarkus Armbruster 626a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 627666daa68SMarkus Armbruster autostart = 0; 628a0f1eab1SMarkus Armbruster } 629362e9299SAlberto Garcia 630362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6312be5506fSAlberto Garcia 63240119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6332be5506fSAlberto Garcia blk_unref(blk); 6342be5506fSAlberto Garcia blk = NULL; 6352be5506fSAlberto Garcia goto err_no_bs_opts; 6362be5506fSAlberto Garcia } 6372be5506fSAlberto Garcia } 6385ec18f8cSMax Reitz 6395ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6400006383eSKevin Wolf 641e4342ce5SMax Reitz err_no_bs_opts: 6420006383eSKevin Wolf qemu_opts_del(opts); 64340119effSAlberto Garcia QDECREF(interval_dict); 64440119effSAlberto Garcia QDECREF(interval_list); 64518e46a03SMarkus Armbruster return blk; 646a9ae2bffSMarkus Armbruster 647ec9c10d2SStefan Hajnoczi early_err: 648ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 64940119effSAlberto Garcia QDECREF(interval_dict); 65040119effSAlberto Garcia QDECREF(interval_list); 6516376f952SMarkus Armbruster err_no_opts: 6526376f952SMarkus Armbruster QDECREF(bs_opts); 653a9ae2bffSMarkus Armbruster return NULL; 654666daa68SMarkus Armbruster } 655666daa68SMarkus Armbruster 656bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 657bd745e23SMax Reitz 658bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 659bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 660bd745e23SMax Reitz { 661bd745e23SMax Reitz BlockDriverState *bs; 662bd745e23SMax Reitz QemuOpts *opts; 663bd745e23SMax Reitz Error *local_error = NULL; 664bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 665bd745e23SMax Reitz int ret; 666bd745e23SMax Reitz int bdrv_flags = 0; 667bd745e23SMax Reitz 668bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 669bd745e23SMax Reitz if (!opts) { 670bd745e23SMax Reitz goto fail; 671bd745e23SMax Reitz } 672bd745e23SMax Reitz 673bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 674bd745e23SMax Reitz if (local_error) { 675bd745e23SMax Reitz error_propagate(errp, local_error); 676bd745e23SMax Reitz goto fail; 677bd745e23SMax Reitz } 678bd745e23SMax Reitz 679bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 680bd745e23SMax Reitz &detect_zeroes, &local_error); 681bd745e23SMax Reitz if (local_error) { 682bd745e23SMax Reitz error_propagate(errp, local_error); 683bd745e23SMax Reitz goto fail; 684bd745e23SMax Reitz } 685bd745e23SMax Reitz 686bd745e23SMax Reitz bs = NULL; 687bd745e23SMax Reitz ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 688bd745e23SMax Reitz if (ret < 0) { 689bd745e23SMax Reitz goto fail_no_bs_opts; 690bd745e23SMax Reitz } 691bd745e23SMax Reitz 692bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 693bd745e23SMax Reitz 694bd745e23SMax Reitz fail_no_bs_opts: 695bd745e23SMax Reitz qemu_opts_del(opts); 696bd745e23SMax Reitz return bs; 697bd745e23SMax Reitz 698bd745e23SMax Reitz fail: 699bd745e23SMax Reitz qemu_opts_del(opts); 700bd745e23SMax Reitz QDECREF(bs_opts); 701bd745e23SMax Reitz return NULL; 702bd745e23SMax Reitz } 703bd745e23SMax Reitz 7045abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7055abbf0eeSKevin Wolf Error **errp) 70657975222SKevin Wolf { 70757975222SKevin Wolf const char *value; 70857975222SKevin Wolf 70957975222SKevin Wolf value = qemu_opt_get(opts, from); 71057975222SKevin Wolf if (value) { 7115abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7125abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7135abbf0eeSKevin Wolf "same time", to, from); 7145abbf0eeSKevin Wolf return; 7155abbf0eeSKevin Wolf } 71620d6cd47SJun Li } 71720d6cd47SJun Li 71820d6cd47SJun Li /* rename all items in opts */ 71920d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 720f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 72157975222SKevin Wolf qemu_opt_unset(opts, from); 72257975222SKevin Wolf } 72357975222SKevin Wolf } 72457975222SKevin Wolf 72533cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 72633cb7dc8SKevin Wolf .name = "drive", 72733cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 72833cb7dc8SKevin Wolf .desc = { 72933cb7dc8SKevin Wolf { 73087a899c5SKevin Wolf .name = "bus", 73187a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 73287a899c5SKevin Wolf .help = "bus number", 73387a899c5SKevin Wolf },{ 73487a899c5SKevin Wolf .name = "unit", 73587a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 73687a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 73787a899c5SKevin Wolf },{ 73887a899c5SKevin Wolf .name = "index", 73987a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 74087a899c5SKevin Wolf .help = "index number", 74187a899c5SKevin Wolf },{ 74233cb7dc8SKevin Wolf .name = "media", 74333cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 74433cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 745593d464bSKevin Wolf },{ 746593d464bSKevin Wolf .name = "if", 747593d464bSKevin Wolf .type = QEMU_OPT_STRING, 748593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 749b41a7338SKevin Wolf },{ 750b41a7338SKevin Wolf .name = "cyls", 751b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 752b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 753b41a7338SKevin Wolf },{ 754b41a7338SKevin Wolf .name = "heads", 755b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 756b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 757b41a7338SKevin Wolf },{ 758b41a7338SKevin Wolf .name = "secs", 759b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 760b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 761b41a7338SKevin Wolf },{ 762b41a7338SKevin Wolf .name = "trans", 763b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 764b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 76526929298SKevin Wolf },{ 76626929298SKevin Wolf .name = "boot", 76726929298SKevin Wolf .type = QEMU_OPT_BOOL, 76826929298SKevin Wolf .help = "(deprecated, ignored)", 769394c7d4dSKevin Wolf },{ 770394c7d4dSKevin Wolf .name = "addr", 771394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 772394c7d4dSKevin Wolf .help = "pci address (virtio only)", 773d095b465SMax Reitz },{ 774bcf83158SKevin Wolf .name = "serial", 775bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 776bcf83158SKevin Wolf .help = "disk serial number", 777bcf83158SKevin Wolf },{ 778d095b465SMax Reitz .name = "file", 779d095b465SMax Reitz .type = QEMU_OPT_STRING, 780d095b465SMax Reitz .help = "file name", 78133cb7dc8SKevin Wolf }, 7820ebd24e0SKevin Wolf 7830ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7840ebd24e0SKevin Wolf { 7850ebd24e0SKevin Wolf .name = "read-only", 7860ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7870ebd24e0SKevin Wolf .help = "open drive file as read-only", 7880ebd24e0SKevin Wolf },{ 789ee13ed1cSKevin Wolf .name = "rerror", 790ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 791ee13ed1cSKevin Wolf .help = "read error action", 792ee13ed1cSKevin Wolf },{ 793ee13ed1cSKevin Wolf .name = "werror", 794ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 795ee13ed1cSKevin Wolf .help = "write error action", 796ee13ed1cSKevin Wolf },{ 7970ebd24e0SKevin Wolf .name = "copy-on-read", 7980ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7990ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8000ebd24e0SKevin Wolf }, 8010ebd24e0SKevin Wolf 80233cb7dc8SKevin Wolf { /* end of list */ } 80333cb7dc8SKevin Wolf }, 80433cb7dc8SKevin Wolf }; 80533cb7dc8SKevin Wolf 80660e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 80757975222SKevin Wolf { 80829c4e2b5SKevin Wolf const char *value; 80918e46a03SMarkus Armbruster BlockBackend *blk; 81033cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 811f298d071SKevin Wolf QDict *bs_opts; 81233cb7dc8SKevin Wolf QemuOpts *legacy_opts; 81333cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 814593d464bSKevin Wolf BlockInterfaceType type; 815b41a7338SKevin Wolf int cyls, heads, secs, translation; 81687a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 817394c7d4dSKevin Wolf const char *devaddr; 818ee13ed1cSKevin Wolf const char *werror, *rerror; 819a7fdbcf0SFam Zheng bool read_only = false; 820a7fdbcf0SFam Zheng bool copy_on_read; 821bcf83158SKevin Wolf const char *serial; 822d095b465SMax Reitz const char *filename; 82333cb7dc8SKevin Wolf Error *local_err = NULL; 824247147fbSKevin Wolf int i; 82529c4e2b5SKevin Wolf 82657975222SKevin Wolf /* Change legacy command line options into QMP ones */ 827247147fbSKevin Wolf static const struct { 828247147fbSKevin Wolf const char *from; 829247147fbSKevin Wolf const char *to; 830247147fbSKevin Wolf } opt_renames[] = { 831247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 832247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 833247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 83457975222SKevin Wolf 835247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 836247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 837247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 83857975222SKevin Wolf 839247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 840247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 841247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8423e9fab69SBenoît Canet 843247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 844247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 845247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8463e9fab69SBenoît Canet 847247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8482024c1dfSBenoît Canet 84976f4afb4SAlberto Garcia { "group", "throttling.group" }, 85076f4afb4SAlberto Garcia 851247147fbSKevin Wolf { "readonly", "read-only" }, 852247147fbSKevin Wolf }; 853247147fbSKevin Wolf 854247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8555abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8565abbf0eeSKevin Wolf &local_err); 8575abbf0eeSKevin Wolf if (local_err) { 858565f65d2SMarkus Armbruster error_report_err(local_err); 8595abbf0eeSKevin Wolf return NULL; 8605abbf0eeSKevin Wolf } 861247147fbSKevin Wolf } 8620f227a94SKevin Wolf 86329c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 86429c4e2b5SKevin Wolf if (value) { 86529c4e2b5SKevin Wolf int flags = 0; 86629c4e2b5SKevin Wolf 86729c4e2b5SKevin Wolf if (bdrv_parse_cache_flags(value, &flags) != 0) { 86829c4e2b5SKevin Wolf error_report("invalid cache option"); 86929c4e2b5SKevin Wolf return NULL; 87029c4e2b5SKevin Wolf } 87129c4e2b5SKevin Wolf 87229c4e2b5SKevin Wolf /* Specific options take precedence */ 87354861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 87454861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 875cccb7967SMarkus Armbruster !!(flags & BDRV_O_CACHE_WB), &error_abort); 87629c4e2b5SKevin Wolf } 87754861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 87854861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 879cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 88029c4e2b5SKevin Wolf } 88154861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 88254861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 883cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 88429c4e2b5SKevin Wolf } 88529c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 88629c4e2b5SKevin Wolf } 88729c4e2b5SKevin Wolf 888f298d071SKevin Wolf /* Get a QDict for processing the options */ 889f298d071SKevin Wolf bs_opts = qdict_new(); 890f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 891f298d071SKevin Wolf 89287ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 89387ea75d5SPeter Crosthwaite &error_abort); 89433cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 89584d18f06SMarkus Armbruster if (local_err) { 896565f65d2SMarkus Armbruster error_report_err(local_err); 89733cb7dc8SKevin Wolf goto fail; 89833cb7dc8SKevin Wolf } 89933cb7dc8SKevin Wolf 90026929298SKevin Wolf /* Deprecated option boot=[on|off] */ 90126929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 90226929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 90326929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 90426929298SKevin Wolf "update your scripts.\n"); 90526929298SKevin Wolf } 90626929298SKevin Wolf 90733cb7dc8SKevin Wolf /* Media type */ 90833cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 90933cb7dc8SKevin Wolf if (value) { 91033cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 91133cb7dc8SKevin Wolf media = MEDIA_DISK; 91233cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 91333cb7dc8SKevin Wolf media = MEDIA_CDROM; 914a7fdbcf0SFam Zheng read_only = true; 91533cb7dc8SKevin Wolf } else { 91633cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 91733cb7dc8SKevin Wolf goto fail; 91833cb7dc8SKevin Wolf } 91933cb7dc8SKevin Wolf } 92033cb7dc8SKevin Wolf 9210ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 922a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9230ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9240ebd24e0SKevin Wolf 9250ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9260ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9270ebd24e0SKevin Wolf copy_on_read = false; 9280ebd24e0SKevin Wolf } 9290ebd24e0SKevin Wolf 9300ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9310ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9320ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9330ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9340ebd24e0SKevin Wolf 935593d464bSKevin Wolf /* Controller type */ 936593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 937593d464bSKevin Wolf if (value) { 938593d464bSKevin Wolf for (type = 0; 939593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 940593d464bSKevin Wolf type++) { 941593d464bSKevin Wolf } 942593d464bSKevin Wolf if (type == IF_COUNT) { 943593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 944593d464bSKevin Wolf goto fail; 945593d464bSKevin Wolf } 946593d464bSKevin Wolf } else { 947593d464bSKevin Wolf type = block_default_type; 948593d464bSKevin Wolf } 949593d464bSKevin Wolf 950b41a7338SKevin Wolf /* Geometry */ 951b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 952b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 953b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 954b41a7338SKevin Wolf 955b41a7338SKevin Wolf if (cyls || heads || secs) { 956b41a7338SKevin Wolf if (cyls < 1) { 957b41a7338SKevin Wolf error_report("invalid physical cyls number"); 958b41a7338SKevin Wolf goto fail; 959b41a7338SKevin Wolf } 960b41a7338SKevin Wolf if (heads < 1) { 961b41a7338SKevin Wolf error_report("invalid physical heads number"); 962b41a7338SKevin Wolf goto fail; 963b41a7338SKevin Wolf } 964b41a7338SKevin Wolf if (secs < 1) { 965b41a7338SKevin Wolf error_report("invalid physical secs number"); 966b41a7338SKevin Wolf goto fail; 967b41a7338SKevin Wolf } 968b41a7338SKevin Wolf } 969b41a7338SKevin Wolf 970b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 971b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 972b41a7338SKevin Wolf if (value != NULL) { 973b41a7338SKevin Wolf if (!cyls) { 974b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 975b41a7338SKevin Wolf value); 976b41a7338SKevin Wolf goto fail; 977b41a7338SKevin Wolf } 978b41a7338SKevin Wolf if (!strcmp(value, "none")) { 979b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 980b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 981b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 982f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 983f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 984f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 985f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 986b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 987b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 988b41a7338SKevin Wolf } else { 989b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 990b41a7338SKevin Wolf goto fail; 991b41a7338SKevin Wolf } 992b41a7338SKevin Wolf } 993b41a7338SKevin Wolf 994b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 995b41a7338SKevin Wolf if (cyls || secs || heads) { 996b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 997b41a7338SKevin Wolf goto fail; 998b41a7338SKevin Wolf } 999b41a7338SKevin Wolf } 1000b41a7338SKevin Wolf 100187a899c5SKevin Wolf /* Device address specified by bus/unit or index. 100287a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 100387a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 100487a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 100587a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 100687a899c5SKevin Wolf 100787a899c5SKevin Wolf max_devs = if_max_devs[type]; 100887a899c5SKevin Wolf 100987a899c5SKevin Wolf if (index != -1) { 101087a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 101187a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 101287a899c5SKevin Wolf goto fail; 101387a899c5SKevin Wolf } 101487a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 101587a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 101687a899c5SKevin Wolf } 101787a899c5SKevin Wolf 101887a899c5SKevin Wolf if (unit_id == -1) { 101987a899c5SKevin Wolf unit_id = 0; 102087a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 102187a899c5SKevin Wolf unit_id++; 102287a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 102387a899c5SKevin Wolf unit_id -= max_devs; 102487a899c5SKevin Wolf bus_id++; 102587a899c5SKevin Wolf } 102687a899c5SKevin Wolf } 102787a899c5SKevin Wolf } 102887a899c5SKevin Wolf 102987a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 103087a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 103187a899c5SKevin Wolf goto fail; 103287a899c5SKevin Wolf } 103387a899c5SKevin Wolf 103487a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 103587a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 103687a899c5SKevin Wolf bus_id, unit_id, index); 103787a899c5SKevin Wolf goto fail; 103887a899c5SKevin Wolf } 103987a899c5SKevin Wolf 1040bcf83158SKevin Wolf /* Serial number */ 1041bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1042bcf83158SKevin Wolf 104387a899c5SKevin Wolf /* no id supplied -> create one */ 104487a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 104587a899c5SKevin Wolf char *new_id; 104687a899c5SKevin Wolf const char *mediastr = ""; 104787a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 104887a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 104987a899c5SKevin Wolf } 105087a899c5SKevin Wolf if (max_devs) { 105187a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 105287a899c5SKevin Wolf mediastr, unit_id); 105387a899c5SKevin Wolf } else { 105487a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 105587a899c5SKevin Wolf mediastr, unit_id); 105687a899c5SKevin Wolf } 105787a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 105887a899c5SKevin Wolf g_free(new_id); 105987a899c5SKevin Wolf } 106087a899c5SKevin Wolf 1061394c7d4dSKevin Wolf /* Add virtio block device */ 1062394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1063394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1064394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1065394c7d4dSKevin Wolf goto fail; 1066394c7d4dSKevin Wolf } 1067394c7d4dSKevin Wolf 1068394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1069394c7d4dSKevin Wolf QemuOpts *devopts; 107087ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 107187ea75d5SPeter Crosthwaite &error_abort); 1072394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10731f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1074394c7d4dSKevin Wolf } else { 1075f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1076394c7d4dSKevin Wolf } 1077f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1078f43e47dbSMarkus Armbruster &error_abort); 1079394c7d4dSKevin Wolf if (devaddr) { 1080f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1081394c7d4dSKevin Wolf } 1082394c7d4dSKevin Wolf } 1083394c7d4dSKevin Wolf 1084d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1085d095b465SMax Reitz 1086ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1087ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1088ee13ed1cSKevin Wolf if (werror != NULL) { 1089ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1090ee13ed1cSKevin Wolf type != IF_NONE) { 1091ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1092ee13ed1cSKevin Wolf goto fail; 1093ee13ed1cSKevin Wolf } 1094ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1095ee13ed1cSKevin Wolf } 1096ee13ed1cSKevin Wolf 1097ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1098ee13ed1cSKevin Wolf if (rerror != NULL) { 1099ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1100ee13ed1cSKevin Wolf type != IF_NONE) { 1101ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1102ee13ed1cSKevin Wolf goto fail; 1103ee13ed1cSKevin Wolf } 1104ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1105ee13ed1cSKevin Wolf } 1106ee13ed1cSKevin Wolf 11072d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 110818e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11093cb0e25cSMarkus Armbruster bs_opts = NULL; 111018e46a03SMarkus Armbruster if (!blk) { 111184d18f06SMarkus Armbruster if (local_err) { 1112565f65d2SMarkus Armbruster error_report_err(local_err); 1113b681072dSKevin Wolf } 11142d246f01SKevin Wolf goto fail; 1115b681072dSKevin Wolf } else { 111684d18f06SMarkus Armbruster assert(!local_err); 11172d246f01SKevin Wolf } 11182d246f01SKevin Wolf 111926f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 112026f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1121f298d071SKevin Wolf dinfo->opts = all_opts; 11222d246f01SKevin Wolf 1123b41a7338SKevin Wolf dinfo->cyls = cyls; 1124b41a7338SKevin Wolf dinfo->heads = heads; 1125b41a7338SKevin Wolf dinfo->secs = secs; 1126b41a7338SKevin Wolf dinfo->trans = translation; 1127b41a7338SKevin Wolf 1128ee13ed1cSKevin Wolf dinfo->type = type; 112987a899c5SKevin Wolf dinfo->bus = bus_id; 113087a899c5SKevin Wolf dinfo->unit = unit_id; 1131394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1132bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1133bcf83158SKevin Wolf 113426f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 113526f8b3a8SMarkus Armbruster 1136e34ef046SKevin Wolf switch(type) { 1137e34ef046SKevin Wolf case IF_IDE: 1138e34ef046SKevin Wolf case IF_SCSI: 1139e34ef046SKevin Wolf case IF_XEN: 1140e34ef046SKevin Wolf case IF_NONE: 1141e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1142e34ef046SKevin Wolf break; 1143e34ef046SKevin Wolf default: 1144e34ef046SKevin Wolf break; 1145e34ef046SKevin Wolf } 1146e34ef046SKevin Wolf 11472d246f01SKevin Wolf fail: 114833cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11493cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11502d246f01SKevin Wolf return dinfo; 115157975222SKevin Wolf } 115257975222SKevin Wolf 11533e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1154666daa68SMarkus Armbruster { 1155666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1156a0e8544cSFam Zheng BlockBackend *blk; 11572d3735d3SStefan Hajnoczi int ret; 11582d3735d3SStefan Hajnoczi 1159e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1160e8877497SStefan Hajnoczi ret = bdrv_commit_all(); 1161e8877497SStefan Hajnoczi } else { 116284aa0140SStefan Hajnoczi BlockDriverState *bs; 116384aa0140SStefan Hajnoczi AioContext *aio_context; 116484aa0140SStefan Hajnoczi 1165a0e8544cSFam Zheng blk = blk_by_name(device); 1166a0e8544cSFam Zheng if (!blk) { 116758513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1168ac59eb95SMarkus Armbruster return; 11696ab4b5abSMarkus Armbruster } 11705433c24fSMax Reitz if (!blk_is_available(blk)) { 11715433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11725433c24fSMax Reitz return; 11735433c24fSMax Reitz } 117484aa0140SStefan Hajnoczi 117584aa0140SStefan Hajnoczi bs = blk_bs(blk); 117684aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 117784aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 117884aa0140SStefan Hajnoczi 117984aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 118084aa0140SStefan Hajnoczi 118184aa0140SStefan Hajnoczi aio_context_release(aio_context); 11822d3735d3SStefan Hajnoczi } 118358513bdeSJeff Cody if (ret < 0) { 118458513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 118558513bdeSJeff Cody strerror(-ret)); 1186666daa68SMarkus Armbruster } 1187666daa68SMarkus Armbruster } 1188666daa68SMarkus Armbruster 11896a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data, 11906a8f9661SEric Blake Error **errp) 11916cc2a415SPaolo Bonzini { 1192c8a83e85SKevin Wolf TransactionAction action; 1193c8a83e85SKevin Wolf TransactionActionList list; 11946cc2a415SPaolo Bonzini 11956a8f9661SEric Blake action.type = type; 11966a8f9661SEric Blake action.u.data = data; 11976cc2a415SPaolo Bonzini list.value = &action; 11986cc2a415SPaolo Bonzini list.next = NULL; 119994d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12006cc2a415SPaolo Bonzini } 12016cc2a415SPaolo Bonzini 12020901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12030901f67eSBenoît Canet bool has_node_name, const char *node_name, 12040901f67eSBenoît Canet const char *snapshot_file, 12050901f67eSBenoît Canet bool has_snapshot_node_name, 12060901f67eSBenoît Canet const char *snapshot_node_name, 12076106e249SLuiz Capitulino bool has_format, const char *format, 12080901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1209f8882568SJes Sorensen { 1210a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12110901f67eSBenoît Canet .has_device = has_device, 12126cc2a415SPaolo Bonzini .device = (char *) device, 12130901f67eSBenoît Canet .has_node_name = has_node_name, 12140901f67eSBenoît Canet .node_name = (char *) node_name, 12156cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12160901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12170901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12186cc2a415SPaolo Bonzini .has_format = has_format, 12196cc2a415SPaolo Bonzini .format = (char *) format, 12206cc2a415SPaolo Bonzini .has_mode = has_mode, 12216cc2a415SPaolo Bonzini .mode = mode, 12226cc2a415SPaolo Bonzini }; 1223c8a83e85SKevin Wolf blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 1224c8a83e85SKevin Wolf &snapshot, errp); 1225f8882568SJes Sorensen } 1226f8882568SJes Sorensen 122743de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 122843de7e2dSAlberto Garcia Error **errp) 122943de7e2dSAlberto Garcia { 123043de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 123143de7e2dSAlberto Garcia .node = (char *) node, 123243de7e2dSAlberto Garcia .overlay = (char *) overlay 123343de7e2dSAlberto Garcia }; 123443de7e2dSAlberto Garcia 123543de7e2dSAlberto Garcia blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 123643de7e2dSAlberto Garcia &snapshot_data, errp); 123743de7e2dSAlberto Garcia } 123843de7e2dSAlberto Garcia 1239f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1240f323bc9eSWenchao Xia const char *name, 1241f323bc9eSWenchao Xia Error **errp) 1242f323bc9eSWenchao Xia { 1243f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1244f323bc9eSWenchao Xia .device = (char *) device, 1245f323bc9eSWenchao Xia .name = (char *) name 1246f323bc9eSWenchao Xia }; 1247f323bc9eSWenchao Xia 1248f323bc9eSWenchao Xia blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 1249f323bc9eSWenchao Xia &snapshot, errp); 1250f323bc9eSWenchao Xia } 1251f323bc9eSWenchao Xia 125244e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 125344e3e053SWenchao Xia bool has_id, 125444e3e053SWenchao Xia const char *id, 125544e3e053SWenchao Xia bool has_name, 125644e3e053SWenchao Xia const char *name, 125744e3e053SWenchao Xia Error **errp) 125844e3e053SWenchao Xia { 1259a0e8544cSFam Zheng BlockDriverState *bs; 1260a0e8544cSFam Zheng BlockBackend *blk; 12614ef3982aSStefan Hajnoczi AioContext *aio_context; 126244e3e053SWenchao Xia QEMUSnapshotInfo sn; 126344e3e053SWenchao Xia Error *local_err = NULL; 126444e3e053SWenchao Xia SnapshotInfo *info = NULL; 126544e3e053SWenchao Xia int ret; 126644e3e053SWenchao Xia 1267a0e8544cSFam Zheng blk = blk_by_name(device); 1268a0e8544cSFam Zheng if (!blk) { 126975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 127075158ebbSMarkus Armbruster "Device '%s' not found", device); 127144e3e053SWenchao Xia return NULL; 127244e3e053SWenchao Xia } 12735433c24fSMax Reitz 12745433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 12755433c24fSMax Reitz aio_context_acquire(aio_context); 127644e3e053SWenchao Xia 127744e3e053SWenchao Xia if (!has_id) { 127844e3e053SWenchao Xia id = NULL; 127944e3e053SWenchao Xia } 128044e3e053SWenchao Xia 128144e3e053SWenchao Xia if (!has_name) { 128244e3e053SWenchao Xia name = NULL; 128344e3e053SWenchao Xia } 128444e3e053SWenchao Xia 128544e3e053SWenchao Xia if (!id && !name) { 128644e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 12875433c24fSMax Reitz goto out_aio_context; 128844e3e053SWenchao Xia } 128944e3e053SWenchao Xia 12905433c24fSMax Reitz if (!blk_is_available(blk)) { 12915433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 12925433c24fSMax Reitz goto out_aio_context; 12935433c24fSMax Reitz } 12945433c24fSMax Reitz bs = blk_bs(blk); 12954ef3982aSStefan Hajnoczi 12960b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 12970b928854SStefan Hajnoczi goto out_aio_context; 12980b928854SStefan Hajnoczi } 12990b928854SStefan Hajnoczi 130044e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 130184d18f06SMarkus Armbruster if (local_err) { 130244e3e053SWenchao Xia error_propagate(errp, local_err); 13034ef3982aSStefan Hajnoczi goto out_aio_context; 130444e3e053SWenchao Xia } 130544e3e053SWenchao Xia if (!ret) { 130644e3e053SWenchao Xia error_setg(errp, 130744e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 130844e3e053SWenchao Xia "device '%s'", 130944e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13104ef3982aSStefan Hajnoczi goto out_aio_context; 131144e3e053SWenchao Xia } 131244e3e053SWenchao Xia 131344e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 131484d18f06SMarkus Armbruster if (local_err) { 131544e3e053SWenchao Xia error_propagate(errp, local_err); 13164ef3982aSStefan Hajnoczi goto out_aio_context; 131744e3e053SWenchao Xia } 131844e3e053SWenchao Xia 13194ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13204ef3982aSStefan Hajnoczi 13215839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 132244e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 132344e3e053SWenchao Xia info->name = g_strdup(sn.name); 132444e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 132544e3e053SWenchao Xia info->date_sec = sn.date_sec; 132644e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 132744e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 132844e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 132944e3e053SWenchao Xia 133044e3e053SWenchao Xia return info; 13314ef3982aSStefan Hajnoczi 13324ef3982aSStefan Hajnoczi out_aio_context: 13334ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13344ef3982aSStefan Hajnoczi return NULL; 133544e3e053SWenchao Xia } 13368802d1fdSJeff Cody 1337341ebc2fSJohn Snow /** 1338341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1339341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1340341ebc2fSJohn Snow * the node reference and bitmap names. 1341341ebc2fSJohn Snow * 1342341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1343341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1344341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1345341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1346341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1347341ebc2fSJohn Snow * 1348341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1349341ebc2fSJohn Snow */ 1350341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1351341ebc2fSJohn Snow const char *name, 1352341ebc2fSJohn Snow BlockDriverState **pbs, 1353341ebc2fSJohn Snow AioContext **paio, 1354341ebc2fSJohn Snow Error **errp) 1355341ebc2fSJohn Snow { 1356341ebc2fSJohn Snow BlockDriverState *bs; 1357341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1358341ebc2fSJohn Snow AioContext *aio_context; 1359341ebc2fSJohn Snow 1360341ebc2fSJohn Snow if (!node) { 1361341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1362341ebc2fSJohn Snow return NULL; 1363341ebc2fSJohn Snow } 1364341ebc2fSJohn Snow if (!name) { 1365341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1366341ebc2fSJohn Snow return NULL; 1367341ebc2fSJohn Snow } 1368341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1369341ebc2fSJohn Snow if (!bs) { 1370341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1371341ebc2fSJohn Snow return NULL; 1372341ebc2fSJohn Snow } 1373341ebc2fSJohn Snow 1374341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1375341ebc2fSJohn Snow aio_context_acquire(aio_context); 1376341ebc2fSJohn Snow 1377341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1378341ebc2fSJohn Snow if (!bitmap) { 1379341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1380341ebc2fSJohn Snow goto fail; 1381341ebc2fSJohn Snow } 1382341ebc2fSJohn Snow 1383341ebc2fSJohn Snow if (pbs) { 1384341ebc2fSJohn Snow *pbs = bs; 1385341ebc2fSJohn Snow } 1386341ebc2fSJohn Snow if (paio) { 1387341ebc2fSJohn Snow *paio = aio_context; 1388341ebc2fSJohn Snow } else { 1389341ebc2fSJohn Snow aio_context_release(aio_context); 1390341ebc2fSJohn Snow } 1391341ebc2fSJohn Snow 1392341ebc2fSJohn Snow return bitmap; 1393341ebc2fSJohn Snow 1394341ebc2fSJohn Snow fail: 1395341ebc2fSJohn Snow aio_context_release(aio_context); 1396341ebc2fSJohn Snow return NULL; 1397341ebc2fSJohn Snow } 1398341ebc2fSJohn Snow 1399b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1400ba0c86a3SWenchao Xia 140150f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1402ba0c86a3SWenchao Xia 140350f43f0fSJohn Snow /** 140450f43f0fSJohn Snow * BlkActionOps: 140550f43f0fSJohn Snow * Table of operations that define an Action. 140650f43f0fSJohn Snow * 140750f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 140850f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 140950f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 141050f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 141150f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 141250f43f0fSJohn Snow * commit() or abort(). Can be NULL. 141350f43f0fSJohn Snow * 141450f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 141550f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1416ba0c86a3SWenchao Xia */ 141750f43f0fSJohn Snow typedef struct BlkActionOps { 141850f43f0fSJohn Snow size_t instance_size; 141950f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 142050f43f0fSJohn Snow void (*commit)(BlkActionState *common); 142150f43f0fSJohn Snow void (*abort)(BlkActionState *common); 142250f43f0fSJohn Snow void (*clean)(BlkActionState *common); 142350f43f0fSJohn Snow } BlkActionOps; 142450f43f0fSJohn Snow 142550f43f0fSJohn Snow /** 142650f43f0fSJohn Snow * BlkActionState: 142750f43f0fSJohn Snow * Describes one Action's state within a Transaction. 142850f43f0fSJohn Snow * 142950f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 143050f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 143194d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 143250f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 143350f43f0fSJohn Snow * 143450f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 143550f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 143650f43f0fSJohn Snow * base class. 143750f43f0fSJohn Snow */ 143850f43f0fSJohn Snow struct BlkActionState { 1439c8a83e85SKevin Wolf TransactionAction *action; 144050f43f0fSJohn Snow const BlkActionOps *ops; 144194d16a64SJohn Snow BlockJobTxn *block_job_txn; 144294d16a64SJohn Snow TransactionProperties *txn_props; 144350f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1444ba0c86a3SWenchao Xia }; 1445ba0c86a3SWenchao Xia 1446bbe86010SWenchao Xia /* internal snapshot private data */ 1447bbe86010SWenchao Xia typedef struct InternalSnapshotState { 144850f43f0fSJohn Snow BlkActionState common; 1449bbe86010SWenchao Xia BlockDriverState *bs; 14505d6e96efSStefan Hajnoczi AioContext *aio_context; 1451bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1452507306ccSFam Zheng bool created; 1453bbe86010SWenchao Xia } InternalSnapshotState; 1454bbe86010SWenchao Xia 145594d16a64SJohn Snow 145694d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 145794d16a64SJohn Snow { 145894d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 145994d16a64SJohn Snow error_setg(errp, 146094d16a64SJohn Snow "Action '%s' does not support Transaction property " 146194d16a64SJohn Snow "completion-mode = %s", 146294d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 146394d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 146494d16a64SJohn Snow return -1; 146594d16a64SJohn Snow } 146694d16a64SJohn Snow return 0; 146794d16a64SJohn Snow } 146894d16a64SJohn Snow 146950f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1470bbe86010SWenchao Xia Error **errp) 1471bbe86010SWenchao Xia { 1472f70edf99SMarkus Armbruster Error *local_err = NULL; 1473bbe86010SWenchao Xia const char *device; 1474bbe86010SWenchao Xia const char *name; 1475a0e8544cSFam Zheng BlockBackend *blk; 1476bbe86010SWenchao Xia BlockDriverState *bs; 1477bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1478bbe86010SWenchao Xia bool ret; 1479bbe86010SWenchao Xia qemu_timeval tv; 1480bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1481bbe86010SWenchao Xia InternalSnapshotState *state; 1482bbe86010SWenchao Xia int ret1; 1483bbe86010SWenchao Xia 14846a8f9661SEric Blake g_assert(common->action->type == 1485bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 14866a8f9661SEric Blake internal = common->action->u.blockdev_snapshot_internal_sync; 1487bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1488bbe86010SWenchao Xia 1489bbe86010SWenchao Xia /* 1. parse input */ 1490bbe86010SWenchao Xia device = internal->device; 1491bbe86010SWenchao Xia name = internal->name; 1492bbe86010SWenchao Xia 1493bbe86010SWenchao Xia /* 2. check for validation */ 149494d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 149594d16a64SJohn Snow return; 149694d16a64SJohn Snow } 149794d16a64SJohn Snow 1498a0e8544cSFam Zheng blk = blk_by_name(device); 1499a0e8544cSFam Zheng if (!blk) { 150075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 150175158ebbSMarkus Armbruster "Device '%s' not found", device); 1502bbe86010SWenchao Xia return; 1503bbe86010SWenchao Xia } 1504bbe86010SWenchao Xia 15055d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15065433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15075d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15085d6e96efSStefan Hajnoczi 15095433c24fSMax Reitz if (!blk_is_available(blk)) { 1510c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1511bbe86010SWenchao Xia return; 1512bbe86010SWenchao Xia } 15135433c24fSMax Reitz bs = blk_bs(blk); 1514bbe86010SWenchao Xia 1515507306ccSFam Zheng state->bs = bs; 1516507306ccSFam Zheng bdrv_drained_begin(bs); 1517507306ccSFam Zheng 15183dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15193dc7ca3cSStefan Hajnoczi return; 15203dc7ca3cSStefan Hajnoczi } 15213dc7ca3cSStefan Hajnoczi 1522bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 152381e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1524bbe86010SWenchao Xia return; 1525bbe86010SWenchao Xia } 1526bbe86010SWenchao Xia 1527bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 152881e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 152981e5f78aSAlberto Garcia "does not support internal snapshots", 153081e5f78aSAlberto Garcia bs->drv->format_name, device); 1531bbe86010SWenchao Xia return; 1532bbe86010SWenchao Xia } 1533bbe86010SWenchao Xia 1534bbe86010SWenchao Xia if (!strlen(name)) { 1535bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1536bbe86010SWenchao Xia return; 1537bbe86010SWenchao Xia } 1538bbe86010SWenchao Xia 1539bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1540f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1541f70edf99SMarkus Armbruster &local_err); 1542f70edf99SMarkus Armbruster if (local_err) { 1543f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1544bbe86010SWenchao Xia return; 1545bbe86010SWenchao Xia } else if (ret) { 1546bbe86010SWenchao Xia error_setg(errp, 1547bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1548bbe86010SWenchao Xia name, device); 1549bbe86010SWenchao Xia return; 1550bbe86010SWenchao Xia } 1551bbe86010SWenchao Xia 1552bbe86010SWenchao Xia /* 3. take the snapshot */ 1553bbe86010SWenchao Xia sn = &state->sn; 1554bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1555bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1556bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1557bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1558bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1559bbe86010SWenchao Xia 1560bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1561bbe86010SWenchao Xia if (ret1 < 0) { 1562bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1563bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1564bbe86010SWenchao Xia name, device); 1565bbe86010SWenchao Xia return; 1566bbe86010SWenchao Xia } 1567bbe86010SWenchao Xia 1568bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1569507306ccSFam Zheng state->created = true; 1570bbe86010SWenchao Xia } 1571bbe86010SWenchao Xia 157250f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1573bbe86010SWenchao Xia { 1574bbe86010SWenchao Xia InternalSnapshotState *state = 1575bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1576bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1577bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1578bbe86010SWenchao Xia Error *local_error = NULL; 1579bbe86010SWenchao Xia 1580507306ccSFam Zheng if (!state->created) { 1581bbe86010SWenchao Xia return; 1582bbe86010SWenchao Xia } 1583bbe86010SWenchao Xia 1584bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1585*c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1586*c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1587*c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1588*c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1589*c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1590bbe86010SWenchao Xia } 1591bbe86010SWenchao Xia } 1592bbe86010SWenchao Xia 159350f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 15945d6e96efSStefan Hajnoczi { 15955d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15965d6e96efSStefan Hajnoczi common, common); 15975d6e96efSStefan Hajnoczi 15985d6e96efSStefan Hajnoczi if (state->aio_context) { 1599507306ccSFam Zheng if (state->bs) { 1600507306ccSFam Zheng bdrv_drained_end(state->bs); 1601507306ccSFam Zheng } 16025d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16035d6e96efSStefan Hajnoczi } 16045d6e96efSStefan Hajnoczi } 16055d6e96efSStefan Hajnoczi 1606ba0c86a3SWenchao Xia /* external snapshot private data */ 1607ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 160850f43f0fSJohn Snow BlkActionState common; 16098802d1fdSJeff Cody BlockDriverState *old_bs; 16108802d1fdSJeff Cody BlockDriverState *new_bs; 16115d6e96efSStefan Hajnoczi AioContext *aio_context; 1612ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16138802d1fdSJeff Cody 161450f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16159b9877eeSWenchao Xia Error **errp) 16169b9877eeSWenchao Xia { 161743de7e2dSAlberto Garcia int flags = 0, ret; 161843de7e2dSAlberto Garcia QDict *options = NULL; 16199b9877eeSWenchao Xia Error *local_err = NULL; 162043de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1621e2a31e87SWenchao Xia const char *device; 16220901f67eSBenoît Canet const char *node_name; 162343de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 162443de7e2dSAlberto Garcia const char *snapshot_ref; 162543de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1626e2a31e87SWenchao Xia const char *new_image_file; 1627ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1628ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1629c8a83e85SKevin Wolf TransactionAction *action = common->action; 16309b9877eeSWenchao Xia 163143de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 163243de7e2dSAlberto Garcia * purpose but a different set of parameters */ 163343de7e2dSAlberto Garcia switch (action->type) { 163443de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 163543de7e2dSAlberto Garcia { 163643de7e2dSAlberto Garcia BlockdevSnapshot *s = action->u.blockdev_snapshot; 163743de7e2dSAlberto Garcia device = s->node; 163843de7e2dSAlberto Garcia node_name = s->node; 163943de7e2dSAlberto Garcia new_image_file = NULL; 164043de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1641e2a31e87SWenchao Xia } 164243de7e2dSAlberto Garcia break; 164343de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 164443de7e2dSAlberto Garcia { 164543de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 164643de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 164743de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 164843de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 164943de7e2dSAlberto Garcia snapshot_ref = NULL; 165043de7e2dSAlberto Garcia } 165143de7e2dSAlberto Garcia break; 165243de7e2dSAlberto Garcia default: 165343de7e2dSAlberto Garcia g_assert_not_reached(); 1654e2a31e87SWenchao Xia } 1655e2a31e87SWenchao Xia 1656e2a31e87SWenchao Xia /* start processing */ 165794d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 165894d16a64SJohn Snow return; 165994d16a64SJohn Snow } 166094d16a64SJohn Snow 166143de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 166243de7e2dSAlberto Garcia if (!state->old_bs) { 16639b9877eeSWenchao Xia return; 16649b9877eeSWenchao Xia } 16659b9877eeSWenchao Xia 16665d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16675d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16685d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1669da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16705d6e96efSStefan Hajnoczi 1671ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1672c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16739b9877eeSWenchao Xia return; 16749b9877eeSWenchao Xia } 16759b9877eeSWenchao Xia 16763718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16773718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16789b9877eeSWenchao Xia return; 16799b9877eeSWenchao Xia } 16809b9877eeSWenchao Xia 1681ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1682ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1683c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16849b9877eeSWenchao Xia return; 16859b9877eeSWenchao Xia } 16869b9877eeSWenchao Xia } 16879b9877eeSWenchao Xia 1688212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1689c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1690f6186f49SBenoît Canet return; 1691f6186f49SBenoît Canet } 1692f6186f49SBenoît Canet 169343de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 169443de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 169543de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 169643de7e2dSAlberto Garcia enum NewImageMode mode; 169743de7e2dSAlberto Garcia const char *snapshot_node_name = 169843de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 169943de7e2dSAlberto Garcia 170043de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 170143de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 170243de7e2dSAlberto Garcia return; 170343de7e2dSAlberto Garcia } 170443de7e2dSAlberto Garcia 170543de7e2dSAlberto Garcia if (snapshot_node_name && 170643de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 170743de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 170843de7e2dSAlberto Garcia return; 170943de7e2dSAlberto Garcia } 171043de7e2dSAlberto Garcia 1711ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17129b9877eeSWenchao Xia 17139b9877eeSWenchao Xia /* create new image w/backing file */ 171443de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17159b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 17169b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1717ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1718ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17199b9877eeSWenchao Xia NULL, -1, flags, &local_err, false); 172084d18f06SMarkus Armbruster if (local_err) { 17219b9877eeSWenchao Xia error_propagate(errp, local_err); 17229b9877eeSWenchao Xia return; 17239b9877eeSWenchao Xia } 17249b9877eeSWenchao Xia } 17259b9877eeSWenchao Xia 17260901f67eSBenoît Canet options = qdict_new(); 172743de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17280901f67eSBenoît Canet qdict_put(options, "node-name", 17290901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17300901f67eSBenoît Canet } 1731e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17320901f67eSBenoît Canet 173343de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 173443de7e2dSAlberto Garcia } 173543de7e2dSAlberto Garcia 1736f67503e5SMax Reitz assert(state->new_bs == NULL); 173743de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 173843de7e2dSAlberto Garcia flags, errp); 1739f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17409b9877eeSWenchao Xia if (ret != 0) { 174143de7e2dSAlberto Garcia return; 174243de7e2dSAlberto Garcia } 174343de7e2dSAlberto Garcia 174443de7e2dSAlberto Garcia if (state->new_bs->blk != NULL) { 174543de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 174643de7e2dSAlberto Garcia blk_name(state->new_bs->blk)); 174743de7e2dSAlberto Garcia return; 174843de7e2dSAlberto Garcia } 174943de7e2dSAlberto Garcia 175043de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 175143de7e2dSAlberto Garcia errp)) { 175243de7e2dSAlberto Garcia return; 175343de7e2dSAlberto Garcia } 175443de7e2dSAlberto Garcia 175543de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 175643de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 175708b24cfeSAlberto Garcia return; 175808b24cfeSAlberto Garcia } 175908b24cfeSAlberto Garcia 176008b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 176108b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17629b9877eeSWenchao Xia } 17639b9877eeSWenchao Xia } 17649b9877eeSWenchao Xia 176550f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17663b0047e8SWenchao Xia { 1767ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1768ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1769ba0c86a3SWenchao Xia 17705d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 17715d6e96efSStefan Hajnoczi 1772ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1773ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 17743b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17753b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17763b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1777dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17783b0047e8SWenchao Xia NULL); 17793b0047e8SWenchao Xia } 17803b0047e8SWenchao Xia 178150f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 178296b86bf7SWenchao Xia { 1783ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1784ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1785ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 17864f6fd349SFam Zheng bdrv_unref(state->new_bs); 178796b86bf7SWenchao Xia } 1788da763e83SFam Zheng } 1789da763e83SFam Zheng 179050f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1791da763e83SFam Zheng { 1792da763e83SFam Zheng ExternalSnapshotState *state = 1793da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17945d6e96efSStefan Hajnoczi if (state->aio_context) { 1795da763e83SFam Zheng bdrv_drained_end(state->old_bs); 17965d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 17975d6e96efSStefan Hajnoczi } 179896b86bf7SWenchao Xia } 179996b86bf7SWenchao Xia 18003037f364SStefan Hajnoczi typedef struct DriveBackupState { 180150f43f0fSJohn Snow BlkActionState common; 18023037f364SStefan Hajnoczi BlockDriverState *bs; 18035d6e96efSStefan Hajnoczi AioContext *aio_context; 18043037f364SStefan Hajnoczi BlockJob *job; 18053037f364SStefan Hajnoczi } DriveBackupState; 18063037f364SStefan Hajnoczi 180778f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 180878f51fdeSJohn Snow bool has_format, const char *format, 180978f51fdeSJohn Snow enum MirrorSyncMode sync, 181078f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 181178f51fdeSJohn Snow bool has_speed, int64_t speed, 181278f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 181378f51fdeSJohn Snow bool has_on_source_error, 181478f51fdeSJohn Snow BlockdevOnError on_source_error, 181578f51fdeSJohn Snow bool has_on_target_error, 181678f51fdeSJohn Snow BlockdevOnError on_target_error, 181778f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 181878f51fdeSJohn Snow 181950f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18203037f364SStefan Hajnoczi { 18213037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1822a0e8544cSFam Zheng BlockBackend *blk; 18233037f364SStefan Hajnoczi DriveBackup *backup; 18243037f364SStefan Hajnoczi Error *local_err = NULL; 18253037f364SStefan Hajnoczi 18266a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 18276a8f9661SEric Blake backup = common->action->u.drive_backup; 18283037f364SStefan Hajnoczi 1829a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1830a0e8544cSFam Zheng if (!blk) { 183175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 183275158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18335d6e96efSStefan Hajnoczi return; 18345d6e96efSStefan Hajnoczi } 18355d6e96efSStefan Hajnoczi 18361fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18371fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18381fdd4b7bSFam Zheng return; 18391fdd4b7bSFam Zheng } 18401fdd4b7bSFam Zheng 18415d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18425433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18435d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18441fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18451fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18465d6e96efSStefan Hajnoczi 184778f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18483037f364SStefan Hajnoczi backup->has_format, backup->format, 1849b53169eaSStefan Hajnoczi backup->sync, 18503037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18513037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1852d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18533037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18543037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 185594d16a64SJohn Snow common->block_job_txn, &local_err); 185684d18f06SMarkus Armbruster if (local_err) { 18573037f364SStefan Hajnoczi error_propagate(errp, local_err); 18583037f364SStefan Hajnoczi return; 18593037f364SStefan Hajnoczi } 18603037f364SStefan Hajnoczi 18613037f364SStefan Hajnoczi state->job = state->bs->job; 18623037f364SStefan Hajnoczi } 18633037f364SStefan Hajnoczi 186450f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18653037f364SStefan Hajnoczi { 18663037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18673037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 18683037f364SStefan Hajnoczi 18693037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 18703037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 18713037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 18723037f364SStefan Hajnoczi } 18733037f364SStefan Hajnoczi } 18743037f364SStefan Hajnoczi 187550f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18765d6e96efSStefan Hajnoczi { 18775d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18785d6e96efSStefan Hajnoczi 18795d6e96efSStefan Hajnoczi if (state->aio_context) { 18801fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 18815d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18825d6e96efSStefan Hajnoczi } 18835d6e96efSStefan Hajnoczi } 18845d6e96efSStefan Hajnoczi 1885bd8baecdSFam Zheng typedef struct BlockdevBackupState { 188650f43f0fSJohn Snow BlkActionState common; 1887bd8baecdSFam Zheng BlockDriverState *bs; 1888bd8baecdSFam Zheng BlockJob *job; 1889bd8baecdSFam Zheng AioContext *aio_context; 1890bd8baecdSFam Zheng } BlockdevBackupState; 1891bd8baecdSFam Zheng 189278f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 189378f51fdeSJohn Snow enum MirrorSyncMode sync, 189478f51fdeSJohn Snow bool has_speed, int64_t speed, 189578f51fdeSJohn Snow bool has_on_source_error, 189678f51fdeSJohn Snow BlockdevOnError on_source_error, 189778f51fdeSJohn Snow bool has_on_target_error, 189878f51fdeSJohn Snow BlockdevOnError on_target_error, 189978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 190078f51fdeSJohn Snow 190150f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1902bd8baecdSFam Zheng { 1903bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1904bd8baecdSFam Zheng BlockdevBackup *backup; 19055433c24fSMax Reitz BlockBackend *blk, *target; 1906bd8baecdSFam Zheng Error *local_err = NULL; 1907bd8baecdSFam Zheng 19086a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 19096a8f9661SEric Blake backup = common->action->u.blockdev_backup; 1910bd8baecdSFam Zheng 1911a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1912a0e8544cSFam Zheng if (!blk) { 19135b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1914bd8baecdSFam Zheng return; 1915bd8baecdSFam Zheng } 1916bd8baecdSFam Zheng 1917ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1918ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1919ff52bf36SFam Zheng return; 1920ff52bf36SFam Zheng } 1921ff52bf36SFam Zheng 19225433c24fSMax Reitz target = blk_by_name(backup->target); 19235433c24fSMax Reitz if (!target) { 19245b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1925bd8baecdSFam Zheng return; 1926bd8baecdSFam Zheng } 1927bd8baecdSFam Zheng 1928bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19295433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19305433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1931bd8baecdSFam Zheng state->aio_context = NULL; 1932bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1933bd8baecdSFam Zheng return; 1934bd8baecdSFam Zheng } 1935bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1936ff52bf36SFam Zheng state->bs = blk_bs(blk); 1937ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1938bd8baecdSFam Zheng 193978f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1940bd8baecdSFam Zheng backup->sync, 1941bd8baecdSFam Zheng backup->has_speed, backup->speed, 1942bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1943bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 194494d16a64SJohn Snow common->block_job_txn, &local_err); 1945bd8baecdSFam Zheng if (local_err) { 1946bd8baecdSFam Zheng error_propagate(errp, local_err); 1947bd8baecdSFam Zheng return; 1948bd8baecdSFam Zheng } 1949bd8baecdSFam Zheng 1950bd8baecdSFam Zheng state->job = state->bs->job; 1951bd8baecdSFam Zheng } 1952bd8baecdSFam Zheng 195350f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1954bd8baecdSFam Zheng { 1955bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1956bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1957bd8baecdSFam Zheng 1958bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1959bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1960bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1961bd8baecdSFam Zheng } 1962bd8baecdSFam Zheng } 1963bd8baecdSFam Zheng 196450f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1965bd8baecdSFam Zheng { 1966bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1967bd8baecdSFam Zheng 1968bd8baecdSFam Zheng if (state->aio_context) { 1969ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1970bd8baecdSFam Zheng aio_context_release(state->aio_context); 1971bd8baecdSFam Zheng } 1972bd8baecdSFam Zheng } 1973bd8baecdSFam Zheng 1974df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 197550f43f0fSJohn Snow BlkActionState common; 1976df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1977df9a681dSFam Zheng BlockDriverState *bs; 1978df9a681dSFam Zheng AioContext *aio_context; 1979df9a681dSFam Zheng HBitmap *backup; 1980df9a681dSFam Zheng bool prepared; 1981df9a681dSFam Zheng } BlockDirtyBitmapState; 1982df9a681dSFam Zheng 198350f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1984df9a681dSFam Zheng Error **errp) 1985df9a681dSFam Zheng { 1986df9a681dSFam Zheng Error *local_err = NULL; 1987df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1988df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1989df9a681dSFam Zheng common, common); 1990df9a681dSFam Zheng 199194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 199294d16a64SJohn Snow return; 199394d16a64SJohn Snow } 199494d16a64SJohn Snow 199550f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 1996df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1997df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 1998df9a681dSFam Zheng action->has_granularity, action->granularity, 1999df9a681dSFam Zheng &local_err); 2000df9a681dSFam Zheng 2001df9a681dSFam Zheng if (!local_err) { 2002df9a681dSFam Zheng state->prepared = true; 2003df9a681dSFam Zheng } else { 2004df9a681dSFam Zheng error_propagate(errp, local_err); 2005df9a681dSFam Zheng } 2006df9a681dSFam Zheng } 2007df9a681dSFam Zheng 200850f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2009df9a681dSFam Zheng { 2010df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2011df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2012df9a681dSFam Zheng common, common); 2013df9a681dSFam Zheng 201450f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 2015df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2016df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2017df9a681dSFam Zheng */ 2018df9a681dSFam Zheng if (state->prepared) { 2019df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2020df9a681dSFam Zheng } 2021df9a681dSFam Zheng } 2022df9a681dSFam Zheng 202350f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2024df9a681dSFam Zheng Error **errp) 2025df9a681dSFam Zheng { 2026df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2027df9a681dSFam Zheng common, common); 2028df9a681dSFam Zheng BlockDirtyBitmap *action; 2029df9a681dSFam Zheng 203094d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 203194d16a64SJohn Snow return; 203294d16a64SJohn Snow } 203394d16a64SJohn Snow 203450f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_clear; 2035df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2036df9a681dSFam Zheng action->name, 2037df9a681dSFam Zheng &state->bs, 2038df9a681dSFam Zheng &state->aio_context, 2039df9a681dSFam Zheng errp); 2040df9a681dSFam Zheng if (!state->bitmap) { 2041df9a681dSFam Zheng return; 2042df9a681dSFam Zheng } 2043df9a681dSFam Zheng 2044df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2045df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2046df9a681dSFam Zheng return; 2047df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2048df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2049df9a681dSFam Zheng return; 2050df9a681dSFam Zheng } 2051df9a681dSFam Zheng 2052df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2053df9a681dSFam Zheng /* AioContext is released in .clean() */ 2054df9a681dSFam Zheng } 2055df9a681dSFam Zheng 205650f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2057df9a681dSFam Zheng { 2058df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2059df9a681dSFam Zheng common, common); 2060df9a681dSFam Zheng 2061df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2062df9a681dSFam Zheng } 2063df9a681dSFam Zheng 206450f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2065df9a681dSFam Zheng { 2066df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2067df9a681dSFam Zheng common, common); 2068df9a681dSFam Zheng 2069df9a681dSFam Zheng hbitmap_free(state->backup); 2070df9a681dSFam Zheng } 2071df9a681dSFam Zheng 207250f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2073df9a681dSFam Zheng { 2074df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2075df9a681dSFam Zheng common, common); 2076df9a681dSFam Zheng 2077df9a681dSFam Zheng if (state->aio_context) { 2078df9a681dSFam Zheng aio_context_release(state->aio_context); 2079df9a681dSFam Zheng } 2080df9a681dSFam Zheng } 2081df9a681dSFam Zheng 208250f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 208378b18b78SStefan Hajnoczi { 208478b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 208578b18b78SStefan Hajnoczi } 208678b18b78SStefan Hajnoczi 208750f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 208878b18b78SStefan Hajnoczi { 2089dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 209078b18b78SStefan Hajnoczi } 209178b18b78SStefan Hajnoczi 209250f43f0fSJohn Snow static const BlkActionOps actions[] = { 209343de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 209443de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 209543de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 209643de7e2dSAlberto Garcia .commit = external_snapshot_commit, 209743de7e2dSAlberto Garcia .abort = external_snapshot_abort, 20984ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 209943de7e2dSAlberto Garcia }, 2100c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2101ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2102ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2103ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2104ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2105da763e83SFam Zheng .clean = external_snapshot_clean, 2106ba0c86a3SWenchao Xia }, 21073037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21083037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21093037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21103037f364SStefan Hajnoczi .abort = drive_backup_abort, 21115d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21123037f364SStefan Hajnoczi }, 2113bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2114bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2115bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2116bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2117bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2118bd8baecdSFam Zheng }, 211978b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 212050f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 212178b18b78SStefan Hajnoczi .prepare = abort_prepare, 212278b18b78SStefan Hajnoczi .commit = abort_commit, 212378b18b78SStefan Hajnoczi }, 2124bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2125bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2126bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2127bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21285d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2129bbe86010SWenchao Xia }, 2130df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2131df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2132df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2133df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2134df9a681dSFam Zheng }, 2135df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2136df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2137df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2138df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2139df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2140df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2141df9a681dSFam Zheng } 2142ba0c86a3SWenchao Xia }; 2143ba0c86a3SWenchao Xia 214494d16a64SJohn Snow /** 214594d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 214694d16a64SJohn Snow * that structure with desired defaults if they are unset. 214794d16a64SJohn Snow */ 214894d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 214994d16a64SJohn Snow TransactionProperties *props) 215094d16a64SJohn Snow { 215194d16a64SJohn Snow if (!props) { 215294d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 215394d16a64SJohn Snow } 215494d16a64SJohn Snow 215594d16a64SJohn Snow if (!props->has_completion_mode) { 215694d16a64SJohn Snow props->has_completion_mode = true; 215794d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 215894d16a64SJohn Snow } 215994d16a64SJohn Snow 216094d16a64SJohn Snow return props; 216194d16a64SJohn Snow } 216294d16a64SJohn Snow 21638802d1fdSJeff Cody /* 2164b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2165b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21668802d1fdSJeff Cody */ 216794d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 216894d16a64SJohn Snow bool has_props, 216994d16a64SJohn Snow struct TransactionProperties *props, 217094d16a64SJohn Snow Error **errp) 21718802d1fdSJeff Cody { 2172c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 217394d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 217450f43f0fSJohn Snow BlkActionState *state, *next; 217543e17041SLuiz Capitulino Error *local_err = NULL; 21768802d1fdSJeff Cody 217750f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 21788802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 21798802d1fdSJeff Cody 218094d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 218194d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 218294d16a64SJohn Snow */ 218394d16a64SJohn Snow props = get_transaction_properties(props); 218494d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 218594d16a64SJohn Snow block_job_txn = block_job_txn_new(); 218694d16a64SJohn Snow } 218794d16a64SJohn Snow 2188b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 21898802d1fdSJeff Cody bdrv_drain_all(); 21908802d1fdSJeff Cody 2191b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 21928802d1fdSJeff Cody while (NULL != dev_entry) { 2193c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 219450f43f0fSJohn Snow const BlkActionOps *ops; 219552e7c241SPaolo Bonzini 21968802d1fdSJeff Cody dev_info = dev_entry->value; 21978802d1fdSJeff Cody dev_entry = dev_entry->next; 21988802d1fdSJeff Cody 21996a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2200ba0c86a3SWenchao Xia 22016a8f9661SEric Blake ops = &actions[dev_info->type]; 2202aa3fe714SMax Reitz assert(ops->instance_size > 0); 2203aa3fe714SMax Reitz 2204ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2205ba5d6ab6SStefan Hajnoczi state->ops = ops; 2206ba5d6ab6SStefan Hajnoczi state->action = dev_info; 220794d16a64SJohn Snow state->block_job_txn = block_job_txn; 220894d16a64SJohn Snow state->txn_props = props; 2209ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22108802d1fdSJeff Cody 2211ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 221284d18f06SMarkus Armbruster if (local_err) { 22139b9877eeSWenchao Xia error_propagate(errp, local_err); 22149b9877eeSWenchao Xia goto delete_and_fail; 22159b9877eeSWenchao Xia } 221652e7c241SPaolo Bonzini } 22178802d1fdSJeff Cody 2218ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2219f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2220ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22218802d1fdSJeff Cody } 2222f9ea81e8SStefan Hajnoczi } 22238802d1fdSJeff Cody 22248802d1fdSJeff Cody /* success */ 22258802d1fdSJeff Cody goto exit; 22268802d1fdSJeff Cody 22278802d1fdSJeff Cody delete_and_fail: 2228b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2229ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2230ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2231ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2232ba0c86a3SWenchao Xia } 22338802d1fdSJeff Cody } 22348802d1fdSJeff Cody exit: 2235ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2236ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2237ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2238ba0c86a3SWenchao Xia } 2239ba5d6ab6SStefan Hajnoczi g_free(state); 22408802d1fdSJeff Cody } 224194d16a64SJohn Snow if (!has_props) { 224294d16a64SJohn Snow qapi_free_TransactionProperties(props); 224394d16a64SJohn Snow } 224494d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22458802d1fdSJeff Cody } 22468802d1fdSJeff Cody 2247c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2248666daa68SMarkus Armbruster { 224938f54bd1SMax Reitz Error *local_err = NULL; 2250666daa68SMarkus Armbruster 225138f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 225238f54bd1SMax Reitz if (local_err) { 225338f54bd1SMax Reitz error_propagate(errp, local_err); 2254c245b6a3SLuiz Capitulino return; 2255666daa68SMarkus Armbruster } 225692d48558SLuiz Capitulino 22576e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2258666daa68SMarkus Armbruster } 2259666daa68SMarkus Armbruster 226012d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 226112d3ba82SBenoît Canet bool has_node_name, const char *node_name, 226212d3ba82SBenoît Canet const char *password, Error **errp) 2263666daa68SMarkus Armbruster { 226412d3ba82SBenoît Canet Error *local_err = NULL; 2265666daa68SMarkus Armbruster BlockDriverState *bs; 2266e3442099SStefan Hajnoczi AioContext *aio_context; 2267666daa68SMarkus Armbruster 226812d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 226912d3ba82SBenoît Canet has_node_name ? node_name : NULL, 227012d3ba82SBenoît Canet &local_err); 227184d18f06SMarkus Armbruster if (local_err) { 227212d3ba82SBenoît Canet error_propagate(errp, local_err); 2273a4dea8a9SLuiz Capitulino return; 2274666daa68SMarkus Armbruster } 2275666daa68SMarkus Armbruster 2276e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2277e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2278e3442099SStefan Hajnoczi 22794d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2280666daa68SMarkus Armbruster 2281e3442099SStefan Hajnoczi aio_context_release(aio_context); 2282e3442099SStefan Hajnoczi } 2283e3442099SStefan Hajnoczi 22847d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 22857d8a9f71SMax Reitz Error **errp) 22867d8a9f71SMax Reitz { 22877d8a9f71SMax Reitz BlockBackend *blk; 22887d8a9f71SMax Reitz bool locked; 22897d8a9f71SMax Reitz 22907d8a9f71SMax Reitz if (!has_force) { 22917d8a9f71SMax Reitz force = false; 22927d8a9f71SMax Reitz } 22937d8a9f71SMax Reitz 22947d8a9f71SMax Reitz blk = blk_by_name(device); 22957d8a9f71SMax Reitz if (!blk) { 22967d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 22977d8a9f71SMax Reitz "Device '%s' not found", device); 22987d8a9f71SMax Reitz return; 22997d8a9f71SMax Reitz } 23007d8a9f71SMax Reitz 23017d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23027d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23037d8a9f71SMax Reitz return; 23047d8a9f71SMax Reitz } 23057d8a9f71SMax Reitz 23067d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23077d8a9f71SMax Reitz return; 23087d8a9f71SMax Reitz } 23097d8a9f71SMax Reitz 23107d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23117d8a9f71SMax Reitz if (locked) { 23127d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23137d8a9f71SMax Reitz } 23147d8a9f71SMax Reitz 23157d8a9f71SMax Reitz if (!locked || force) { 23167d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23177d8a9f71SMax Reitz } 23187d8a9f71SMax Reitz } 23197d8a9f71SMax Reitz 2320abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2321abaaf59dSMax Reitz { 2322abaaf59dSMax Reitz BlockBackend *blk; 2323abaaf59dSMax Reitz 2324abaaf59dSMax Reitz blk = blk_by_name(device); 2325abaaf59dSMax Reitz if (!blk) { 2326abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2327abaaf59dSMax Reitz "Device '%s' not found", device); 2328abaaf59dSMax Reitz return; 2329abaaf59dSMax Reitz } 2330abaaf59dSMax Reitz 2331abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2332abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2333abaaf59dSMax Reitz return; 2334abaaf59dSMax Reitz } 2335abaaf59dSMax Reitz 2336abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2337abaaf59dSMax Reitz return; 2338abaaf59dSMax Reitz } 2339abaaf59dSMax Reitz 2340abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2341abaaf59dSMax Reitz } 2342abaaf59dSMax Reitz 23436e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23442814f672SMax Reitz { 23452814f672SMax Reitz BlockBackend *blk; 23462814f672SMax Reitz BlockDriverState *bs; 23472814f672SMax Reitz AioContext *aio_context; 23482814f672SMax Reitz bool has_device; 23492814f672SMax Reitz 23502814f672SMax Reitz blk = blk_by_name(device); 23512814f672SMax Reitz if (!blk) { 23522814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23532814f672SMax Reitz "Device '%s' not found", device); 23542814f672SMax Reitz return; 23552814f672SMax Reitz } 23562814f672SMax Reitz 23572814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 23582814f672SMax Reitz has_device = blk_get_attached_dev(blk); 23592814f672SMax Reitz 23602814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 23612814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23622814f672SMax Reitz return; 23632814f672SMax Reitz } 23642814f672SMax Reitz 23652814f672SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 23662814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 23672814f672SMax Reitz return; 23682814f672SMax Reitz } 23692814f672SMax Reitz 23702814f672SMax Reitz bs = blk_bs(blk); 23712814f672SMax Reitz if (!bs) { 23722814f672SMax Reitz return; 23732814f672SMax Reitz } 23742814f672SMax Reitz 23752814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 23762814f672SMax Reitz aio_context_acquire(aio_context); 23772814f672SMax Reitz 23782814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 23792814f672SMax Reitz goto out; 23802814f672SMax Reitz } 23812814f672SMax Reitz 23822814f672SMax Reitz /* This follows the convention established by bdrv_make_anon() */ 23832814f672SMax Reitz if (bs->device_list.tqe_prev) { 23842814f672SMax Reitz QTAILQ_REMOVE(&bdrv_states, bs, device_list); 23852814f672SMax Reitz bs->device_list.tqe_prev = NULL; 23862814f672SMax Reitz } 23872814f672SMax Reitz 23882814f672SMax Reitz blk_remove_bs(blk); 23892814f672SMax Reitz 23902814f672SMax Reitz out: 23912814f672SMax Reitz aio_context_release(aio_context); 23922814f672SMax Reitz } 23932814f672SMax Reitz 2394d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2395d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2396d1299882SMax Reitz { 2397d1299882SMax Reitz BlockBackend *blk; 2398d1299882SMax Reitz bool has_device; 2399d1299882SMax Reitz 2400d1299882SMax Reitz blk = blk_by_name(device); 2401d1299882SMax Reitz if (!blk) { 2402d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2403d1299882SMax Reitz "Device '%s' not found", device); 2404d1299882SMax Reitz return; 2405d1299882SMax Reitz } 2406d1299882SMax Reitz 2407d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2408d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2409d1299882SMax Reitz 2410d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2411d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2412d1299882SMax Reitz return; 2413d1299882SMax Reitz } 2414d1299882SMax Reitz 2415d1299882SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 2416d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2417d1299882SMax Reitz return; 2418d1299882SMax Reitz } 2419d1299882SMax Reitz 2420d1299882SMax Reitz if (blk_bs(blk)) { 2421d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2422d1299882SMax Reitz return; 2423d1299882SMax Reitz } 2424d1299882SMax Reitz 2425d1299882SMax Reitz blk_insert_bs(blk, bs); 2426d1299882SMax Reitz 2427d1299882SMax Reitz QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 2428d1299882SMax Reitz } 2429d1299882SMax Reitz 24306e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2431d1299882SMax Reitz Error **errp) 2432d1299882SMax Reitz { 2433d1299882SMax Reitz BlockDriverState *bs; 2434d1299882SMax Reitz 2435d1299882SMax Reitz bs = bdrv_find_node(node_name); 2436d1299882SMax Reitz if (!bs) { 2437d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2438d1299882SMax Reitz return; 2439d1299882SMax Reitz } 2440d1299882SMax Reitz 2441d1299882SMax Reitz if (bs->blk) { 2442d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2443d1299882SMax Reitz blk_name(bs->blk)); 2444d1299882SMax Reitz return; 2445d1299882SMax Reitz } 2446d1299882SMax Reitz 2447d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2448d1299882SMax Reitz } 2449d1299882SMax Reitz 245024fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 245124fb4133SMax Reitz bool has_format, const char *format, 245239ff43d9SMax Reitz bool has_read_only, 245339ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 245424fb4133SMax Reitz Error **errp) 2455de2c6c05SMax Reitz { 2456de2c6c05SMax Reitz BlockBackend *blk; 2457de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2458de2c6c05SMax Reitz int bdrv_flags, ret; 2459de2c6c05SMax Reitz QDict *options = NULL; 2460de2c6c05SMax Reitz Error *err = NULL; 2461de2c6c05SMax Reitz 2462de2c6c05SMax Reitz blk = blk_by_name(device); 2463de2c6c05SMax Reitz if (!blk) { 2464de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2465de2c6c05SMax Reitz "Device '%s' not found", device); 2466de2c6c05SMax Reitz goto fail; 2467de2c6c05SMax Reitz } 2468de2c6c05SMax Reitz 2469de2c6c05SMax Reitz if (blk_bs(blk)) { 2470de2c6c05SMax Reitz blk_update_root_state(blk); 2471de2c6c05SMax Reitz } 2472de2c6c05SMax Reitz 2473de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2474de2c6c05SMax Reitz 247539ff43d9SMax Reitz if (!has_read_only) { 247639ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 247739ff43d9SMax Reitz } 247839ff43d9SMax Reitz 247939ff43d9SMax Reitz switch (read_only) { 248039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 248139ff43d9SMax Reitz break; 248239ff43d9SMax Reitz 248339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 248439ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 248539ff43d9SMax Reitz break; 248639ff43d9SMax Reitz 248739ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 248839ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 248939ff43d9SMax Reitz break; 249039ff43d9SMax Reitz 249139ff43d9SMax Reitz default: 249239ff43d9SMax Reitz abort(); 249339ff43d9SMax Reitz } 249439ff43d9SMax Reitz 249524fb4133SMax Reitz if (has_format) { 2496de2c6c05SMax Reitz options = qdict_new(); 2497de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2498de2c6c05SMax Reitz } 2499de2c6c05SMax Reitz 2500de2c6c05SMax Reitz assert(!medium_bs); 2501de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2502de2c6c05SMax Reitz if (ret < 0) { 2503de2c6c05SMax Reitz goto fail; 2504de2c6c05SMax Reitz } 2505de2c6c05SMax Reitz 2506de2c6c05SMax Reitz blk_apply_root_state(blk, medium_bs); 2507de2c6c05SMax Reitz 2508de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2509de2c6c05SMax Reitz if (err) { 2510de2c6c05SMax Reitz error_propagate(errp, err); 2511de2c6c05SMax Reitz goto fail; 2512de2c6c05SMax Reitz } 2513de2c6c05SMax Reitz 2514de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2515de2c6c05SMax Reitz if (err) { 2516de2c6c05SMax Reitz error_propagate(errp, err); 2517de2c6c05SMax Reitz goto fail; 2518de2c6c05SMax Reitz } 2519de2c6c05SMax Reitz 25206e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2521de2c6c05SMax Reitz if (err) { 2522de2c6c05SMax Reitz error_propagate(errp, err); 2523de2c6c05SMax Reitz goto fail; 2524de2c6c05SMax Reitz } 2525de2c6c05SMax Reitz 2526de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2527de2c6c05SMax Reitz if (err) { 2528de2c6c05SMax Reitz error_propagate(errp, err); 2529de2c6c05SMax Reitz goto fail; 2530de2c6c05SMax Reitz } 2531de2c6c05SMax Reitz 2532de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2533de2c6c05SMax Reitz 2534de2c6c05SMax Reitz fail: 2535de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2536de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2537de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2538de2c6c05SMax Reitz bdrv_unref(medium_bs); 2539de2c6c05SMax Reitz } 2540de2c6c05SMax Reitz 2541727f005eSZhi Yong Wu /* throttling disk I/O limits */ 254280047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 25433e9fab69SBenoît Canet int64_t bps_wr, 25443e9fab69SBenoît Canet int64_t iops, 25453e9fab69SBenoît Canet int64_t iops_rd, 25463e9fab69SBenoît Canet int64_t iops_wr, 25473e9fab69SBenoît Canet bool has_bps_max, 25483e9fab69SBenoît Canet int64_t bps_max, 25493e9fab69SBenoît Canet bool has_bps_rd_max, 25503e9fab69SBenoît Canet int64_t bps_rd_max, 25513e9fab69SBenoît Canet bool has_bps_wr_max, 25523e9fab69SBenoît Canet int64_t bps_wr_max, 25533e9fab69SBenoît Canet bool has_iops_max, 25543e9fab69SBenoît Canet int64_t iops_max, 25553e9fab69SBenoît Canet bool has_iops_rd_max, 25563e9fab69SBenoît Canet int64_t iops_rd_max, 25573e9fab69SBenoît Canet bool has_iops_wr_max, 25582024c1dfSBenoît Canet int64_t iops_wr_max, 25592024c1dfSBenoît Canet bool has_iops_size, 256076f4afb4SAlberto Garcia int64_t iops_size, 256176f4afb4SAlberto Garcia bool has_group, 256276f4afb4SAlberto Garcia const char *group, Error **errp) 2563727f005eSZhi Yong Wu { 2564cc0681c4SBenoît Canet ThrottleConfig cfg; 2565727f005eSZhi Yong Wu BlockDriverState *bs; 2566a0e8544cSFam Zheng BlockBackend *blk; 2567b15446fdSStefan Hajnoczi AioContext *aio_context; 2568727f005eSZhi Yong Wu 2569a0e8544cSFam Zheng blk = blk_by_name(device); 2570a0e8544cSFam Zheng if (!blk) { 257175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 257275158ebbSMarkus Armbruster "Device '%s' not found", device); 257380047da5SLuiz Capitulino return; 2574727f005eSZhi Yong Wu } 25755433c24fSMax Reitz 25765433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 25775433c24fSMax Reitz aio_context_acquire(aio_context); 25785433c24fSMax Reitz 2579a0e8544cSFam Zheng bs = blk_bs(blk); 25805433c24fSMax Reitz if (!bs) { 25815433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 25825433c24fSMax Reitz goto out; 25835433c24fSMax Reitz } 2584727f005eSZhi Yong Wu 2585cc0681c4SBenoît Canet memset(&cfg, 0, sizeof(cfg)); 2586cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2587cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2588cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2589727f005eSZhi Yong Wu 2590cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2591cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2592cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2593cc0681c4SBenoît Canet 25943e9fab69SBenoît Canet if (has_bps_max) { 25953e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 25963e9fab69SBenoît Canet } 25973e9fab69SBenoît Canet if (has_bps_rd_max) { 25983e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 25993e9fab69SBenoît Canet } 26003e9fab69SBenoît Canet if (has_bps_wr_max) { 26013e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26023e9fab69SBenoît Canet } 26033e9fab69SBenoît Canet if (has_iops_max) { 26043e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26053e9fab69SBenoît Canet } 26063e9fab69SBenoît Canet if (has_iops_rd_max) { 26073e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26083e9fab69SBenoît Canet } 26093e9fab69SBenoît Canet if (has_iops_wr_max) { 26103e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26113e9fab69SBenoît Canet } 2612cc0681c4SBenoît Canet 26132024c1dfSBenoît Canet if (has_iops_size) { 26142024c1dfSBenoît Canet cfg.op_size = iops_size; 26152024c1dfSBenoît Canet } 2616cc0681c4SBenoît Canet 2617cc0681c4SBenoît Canet if (!check_throttle_config(&cfg, errp)) { 26185433c24fSMax Reitz goto out; 2619727f005eSZhi Yong Wu } 2620727f005eSZhi Yong Wu 262176f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 262276f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 262376f4afb4SAlberto Garcia * just update the throttling group. */ 2624a0d64a61SAlberto Garcia if (!bs->throttle_state) { 262576f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 262676f4afb4SAlberto Garcia } else if (has_group) { 262776f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2628727f005eSZhi Yong Wu } 262976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2630cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 2631a0d64a61SAlberto Garcia } else if (bs->throttle_state) { 263276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 263376f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2634727f005eSZhi Yong Wu } 2635b15446fdSStefan Hajnoczi 26365433c24fSMax Reitz out: 2637b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2638727f005eSZhi Yong Wu } 2639727f005eSZhi Yong Wu 2640341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2641341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2642341ebc2fSJohn Snow Error **errp) 2643341ebc2fSJohn Snow { 2644341ebc2fSJohn Snow AioContext *aio_context; 2645341ebc2fSJohn Snow BlockDriverState *bs; 2646341ebc2fSJohn Snow 2647341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2648341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2649341ebc2fSJohn Snow return; 2650341ebc2fSJohn Snow } 2651341ebc2fSJohn Snow 2652341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2653341ebc2fSJohn Snow if (!bs) { 2654341ebc2fSJohn Snow return; 2655341ebc2fSJohn Snow } 2656341ebc2fSJohn Snow 2657341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2658341ebc2fSJohn Snow aio_context_acquire(aio_context); 2659341ebc2fSJohn Snow 2660341ebc2fSJohn Snow if (has_granularity) { 2661341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2662341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2663341ebc2fSJohn Snow "and at least 512"); 2664341ebc2fSJohn Snow goto out; 2665341ebc2fSJohn Snow } 2666341ebc2fSJohn Snow } else { 2667341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2668341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2669341ebc2fSJohn Snow } 2670341ebc2fSJohn Snow 2671341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2672341ebc2fSJohn Snow 2673341ebc2fSJohn Snow out: 2674341ebc2fSJohn Snow aio_context_release(aio_context); 2675341ebc2fSJohn Snow } 2676341ebc2fSJohn Snow 2677341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2678341ebc2fSJohn Snow Error **errp) 2679341ebc2fSJohn Snow { 2680341ebc2fSJohn Snow AioContext *aio_context; 2681341ebc2fSJohn Snow BlockDriverState *bs; 2682341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2683341ebc2fSJohn Snow 2684341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2685341ebc2fSJohn Snow if (!bitmap || !bs) { 2686341ebc2fSJohn Snow return; 2687341ebc2fSJohn Snow } 2688341ebc2fSJohn Snow 26899bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 26909bd2b08fSJohn Snow error_setg(errp, 26919bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 26929bd2b08fSJohn Snow name); 26939bd2b08fSJohn Snow goto out; 26949bd2b08fSJohn Snow } 269520dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2696341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2697341ebc2fSJohn Snow 26989bd2b08fSJohn Snow out: 2699341ebc2fSJohn Snow aio_context_release(aio_context); 2700341ebc2fSJohn Snow } 2701341ebc2fSJohn Snow 2702e74e6b78SJohn Snow /** 2703e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2704e74e6b78SJohn Snow * immediately after a full backup operation. 2705e74e6b78SJohn Snow */ 2706e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2707e74e6b78SJohn Snow Error **errp) 2708e74e6b78SJohn Snow { 2709e74e6b78SJohn Snow AioContext *aio_context; 2710e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2711e74e6b78SJohn Snow BlockDriverState *bs; 2712e74e6b78SJohn Snow 2713e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2714e74e6b78SJohn Snow if (!bitmap || !bs) { 2715e74e6b78SJohn Snow return; 2716e74e6b78SJohn Snow } 2717e74e6b78SJohn Snow 2718e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2719e74e6b78SJohn Snow error_setg(errp, 2720e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2721e74e6b78SJohn Snow name); 2722e74e6b78SJohn Snow goto out; 2723e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2724e74e6b78SJohn Snow error_setg(errp, 2725e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2726e74e6b78SJohn Snow name); 2727e74e6b78SJohn Snow goto out; 2728e74e6b78SJohn Snow } 2729e74e6b78SJohn Snow 2730df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2731e74e6b78SJohn Snow 2732e74e6b78SJohn Snow out: 2733e74e6b78SJohn Snow aio_context_release(aio_context); 2734e74e6b78SJohn Snow } 2735e74e6b78SJohn Snow 2736072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 27379063f814SRyan Harper { 27389063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 27397e7d56d9SMarkus Armbruster BlockBackend *blk; 27409063f814SRyan Harper BlockDriverState *bs; 27418ad4202bSStefan Hajnoczi AioContext *aio_context; 27423718d8abSFam Zheng Error *local_err = NULL; 27439063f814SRyan Harper 27447e7d56d9SMarkus Armbruster blk = blk_by_name(id); 27457e7d56d9SMarkus Armbruster if (!blk) { 2746b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2747072ebe6bSMarkus Armbruster return; 27489063f814SRyan Harper } 27498ad4202bSStefan Hajnoczi 275026f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 275148f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 275248f364ddSMarkus Armbruster " is not supported"); 2753072ebe6bSMarkus Armbruster return; 275448f364ddSMarkus Armbruster } 275548f364ddSMarkus Armbruster 27565433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27578ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 27588ad4202bSStefan Hajnoczi 27595433c24fSMax Reitz bs = blk_bs(blk); 27605433c24fSMax Reitz if (bs) { 27613718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2762565f65d2SMarkus Armbruster error_report_err(local_err); 27638ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2764072ebe6bSMarkus Armbruster return; 27658591675fSMarcelo Tosatti } 27669063f814SRyan Harper 27679063f814SRyan Harper bdrv_close(bs); 27685433c24fSMax Reitz } 27699063f814SRyan Harper 2770fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 2771d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 2772d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 2773d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 2774d22b2f41SRyan Harper */ 2775a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 27763e5a50d6SMarkus Armbruster blk_hide_on_behalf_of_hmp_drive_del(blk); 2777293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2778373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2779293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2780d22b2f41SRyan Harper } else { 2781b9fe8a7aSMarkus Armbruster blk_unref(blk); 2782d22b2f41SRyan Harper } 27839063f814SRyan Harper 27848ad4202bSStefan Hajnoczi aio_context_release(aio_context); 27859063f814SRyan Harper } 27866d4a2b3aSChristoph Hellwig 27873b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 27883b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 27893b1dbd11SBenoît Canet int64_t size, Error **errp) 27906d4a2b3aSChristoph Hellwig { 27913b1dbd11SBenoît Canet Error *local_err = NULL; 27926d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2793927e0e76SStefan Hajnoczi AioContext *aio_context; 27948732901eSKevin Wolf int ret; 27956d4a2b3aSChristoph Hellwig 27963b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 27973b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 27983b1dbd11SBenoît Canet &local_err); 279984d18f06SMarkus Armbruster if (local_err) { 28003b1dbd11SBenoît Canet error_propagate(errp, local_err); 28013b1dbd11SBenoît Canet return; 28023b1dbd11SBenoît Canet } 28033b1dbd11SBenoît Canet 2804927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2805927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2806927e0e76SStefan Hajnoczi 28073b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2808c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2809927e0e76SStefan Hajnoczi goto out; 28106d4a2b3aSChristoph Hellwig } 28116d4a2b3aSChristoph Hellwig 28126d4a2b3aSChristoph Hellwig if (size < 0) { 2813c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2814927e0e76SStefan Hajnoczi goto out; 28156d4a2b3aSChristoph Hellwig } 28166d4a2b3aSChristoph Hellwig 28179c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2818c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2819927e0e76SStefan Hajnoczi goto out; 28209c75e168SJeff Cody } 28219c75e168SJeff Cody 282292b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 282392b7a08dSPeter Lieven bdrv_drain_all(); 282492b7a08dSPeter Lieven 28258732901eSKevin Wolf ret = bdrv_truncate(bs, size); 28268732901eSKevin Wolf switch (ret) { 2827939a1cc3SStefan Hajnoczi case 0: 2828939a1cc3SStefan Hajnoczi break; 2829939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2830c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2831939a1cc3SStefan Hajnoczi break; 2832939a1cc3SStefan Hajnoczi case -ENOTSUP: 2833c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2834939a1cc3SStefan Hajnoczi break; 2835939a1cc3SStefan Hajnoczi case -EACCES: 283681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2837939a1cc3SStefan Hajnoczi break; 2838939a1cc3SStefan Hajnoczi case -EBUSY: 2839c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2840939a1cc3SStefan Hajnoczi break; 2841939a1cc3SStefan Hajnoczi default: 28428732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2843939a1cc3SStefan Hajnoczi break; 28446d4a2b3aSChristoph Hellwig } 2845927e0e76SStefan Hajnoczi 2846927e0e76SStefan Hajnoczi out: 2847927e0e76SStefan Hajnoczi aio_context_release(aio_context); 28486d4a2b3aSChristoph Hellwig } 284912bd451fSStefan Hajnoczi 28509abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 285112bd451fSStefan Hajnoczi { 2852723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2853723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2854723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2855723c5d93SStefan Hajnoczi */ 2856723c5d93SStefan Hajnoczi 285712bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2858bcada37bSWenchao Xia const char *msg = NULL; 285912bd451fSStefan Hajnoczi 28609abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 286112bd451fSStefan Hajnoczi 286212bd451fSStefan Hajnoczi assert(bs->job); 2863bcada37bSWenchao Xia 286412bd451fSStefan Hajnoczi if (ret < 0) { 2865bcada37bSWenchao Xia msg = strerror(-ret); 286612bd451fSStefan Hajnoczi } 286712bd451fSStefan Hajnoczi 2868370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2869bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2870370521a1SStefan Hajnoczi } else { 2871bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2872370521a1SStefan Hajnoczi } 287312bd451fSStefan Hajnoczi } 287412bd451fSStefan Hajnoczi 287513d8cc51SJeff Cody void qmp_block_stream(const char *device, 287613d8cc51SJeff Cody bool has_base, const char *base, 287713d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 287813d8cc51SJeff Cody bool has_speed, int64_t speed, 28791d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 28801d809098SPaolo Bonzini Error **errp) 288112bd451fSStefan Hajnoczi { 2882a0e8544cSFam Zheng BlockBackend *blk; 288312bd451fSStefan Hajnoczi BlockDriverState *bs; 2884c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2885f3e69bebSStefan Hajnoczi AioContext *aio_context; 2886fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 288713d8cc51SJeff Cody const char *base_name = NULL; 288812bd451fSStefan Hajnoczi 28891d809098SPaolo Bonzini if (!has_on_error) { 28901d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 28911d809098SPaolo Bonzini } 28921d809098SPaolo Bonzini 2893a0e8544cSFam Zheng blk = blk_by_name(device); 2894a0e8544cSFam Zheng if (!blk) { 289575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 289675158ebbSMarkus Armbruster "Device '%s' not found", device); 289712bd451fSStefan Hajnoczi return; 289812bd451fSStefan Hajnoczi } 289912bd451fSStefan Hajnoczi 29005433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2901f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2902f3e69bebSStefan Hajnoczi 29035433c24fSMax Reitz if (!blk_is_available(blk)) { 29045433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29055433c24fSMax Reitz goto out; 29065433c24fSMax Reitz } 29075433c24fSMax Reitz bs = blk_bs(blk); 29085433c24fSMax Reitz 2909628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2910f3e69bebSStefan Hajnoczi goto out; 2911628ff683SFam Zheng } 2912628ff683SFam Zheng 291313d8cc51SJeff Cody if (has_base) { 2914c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2915c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2916c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2917f3e69bebSStefan Hajnoczi goto out; 291812bd451fSStefan Hajnoczi } 2919f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 292013d8cc51SJeff Cody base_name = base; 2921c8c3080fSMarcelo Tosatti } 292212bd451fSStefan Hajnoczi 292313d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 292413d8cc51SJeff Cody * file, and specifying one is therefore an error */ 292513d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 292613d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 292713d8cc51SJeff Cody "entire chain"); 2928f3e69bebSStefan Hajnoczi goto out; 292913d8cc51SJeff Cody } 293013d8cc51SJeff Cody 293113d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 293213d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 293313d8cc51SJeff Cody 293413d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 29351d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 293684d18f06SMarkus Armbruster if (local_err) { 2937fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2938f3e69bebSStefan Hajnoczi goto out; 293912bd451fSStefan Hajnoczi } 294012bd451fSStefan Hajnoczi 294112bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 2942f3e69bebSStefan Hajnoczi 2943f3e69bebSStefan Hajnoczi out: 2944f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 294512bd451fSStefan Hajnoczi } 29462d47c6e9SStefan Hajnoczi 2947ed61fc10SJeff Cody void qmp_block_commit(const char *device, 29487676e2c5SJeff Cody bool has_base, const char *base, 29497676e2c5SJeff Cody bool has_top, const char *top, 295054e26900SJeff Cody bool has_backing_file, const char *backing_file, 2951ed61fc10SJeff Cody bool has_speed, int64_t speed, 2952ed61fc10SJeff Cody Error **errp) 2953ed61fc10SJeff Cody { 2954a0e8544cSFam Zheng BlockBackend *blk; 2955ed61fc10SJeff Cody BlockDriverState *bs; 2956ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 29579e85cd5cSStefan Hajnoczi AioContext *aio_context; 2958ed61fc10SJeff Cody Error *local_err = NULL; 2959ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 2960ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 2961ed61fc10SJeff Cody */ 296292aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2963ed61fc10SJeff Cody 296454504663SMax Reitz if (!has_speed) { 296554504663SMax Reitz speed = 0; 296654504663SMax Reitz } 296754504663SMax Reitz 29687676e2c5SJeff Cody /* Important Note: 29697676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 29707676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 29717676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 29727676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 2973a0e8544cSFam Zheng blk = blk_by_name(device); 2974a0e8544cSFam Zheng if (!blk) { 297575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 297675158ebbSMarkus Armbruster "Device '%s' not found", device); 2977ed61fc10SJeff Cody return; 2978ed61fc10SJeff Cody } 2979ed61fc10SJeff Cody 29805433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29819e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 29829e85cd5cSStefan Hajnoczi 29835433c24fSMax Reitz if (!blk_is_available(blk)) { 29845433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29855433c24fSMax Reitz goto out; 29865433c24fSMax Reitz } 29875433c24fSMax Reitz bs = blk_bs(blk); 29885433c24fSMax Reitz 2989bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 29909e85cd5cSStefan Hajnoczi goto out; 2991628ff683SFam Zheng } 2992628ff683SFam Zheng 2993ed61fc10SJeff Cody /* default top_bs is the active layer */ 2994ed61fc10SJeff Cody top_bs = bs; 2995ed61fc10SJeff Cody 29967676e2c5SJeff Cody if (has_top && top) { 2997ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 2998ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 2999ed61fc10SJeff Cody } 3000ed61fc10SJeff Cody } 3001ed61fc10SJeff Cody 3002ed61fc10SJeff Cody if (top_bs == NULL) { 3003ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30049e85cd5cSStefan Hajnoczi goto out; 3005ed61fc10SJeff Cody } 3006ed61fc10SJeff Cody 30079e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30089e85cd5cSStefan Hajnoczi 3009d5208c45SJeff Cody if (has_base && base) { 3010d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3011d5208c45SJeff Cody } else { 3012d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3013d5208c45SJeff Cody } 3014d5208c45SJeff Cody 3015d5208c45SJeff Cody if (base_bs == NULL) { 3016c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 30179e85cd5cSStefan Hajnoczi goto out; 3018d5208c45SJeff Cody } 3019d5208c45SJeff Cody 30209e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 30219e85cd5cSStefan Hajnoczi 3022bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3023bb00021dSFam Zheng goto out; 3024bb00021dSFam Zheng } 3025bb00021dSFam Zheng 30267676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 30277676e2c5SJeff Cody if (top_bs == base_bs) { 30287676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 30299e85cd5cSStefan Hajnoczi goto out; 30307676e2c5SJeff Cody } 30317676e2c5SJeff Cody 303220a63d2cSFam Zheng if (top_bs == bs) { 303354e26900SJeff Cody if (has_backing_file) { 303454e26900SJeff Cody error_setg(errp, "'backing-file' specified," 303554e26900SJeff Cody " but 'top' is the active layer"); 30369e85cd5cSStefan Hajnoczi goto out; 303754e26900SJeff Cody } 303820a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 303920a63d2cSFam Zheng bs, &local_err); 304020a63d2cSFam Zheng } else { 3041ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 304254e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 304320a63d2cSFam Zheng } 3044ed61fc10SJeff Cody if (local_err != NULL) { 3045ed61fc10SJeff Cody error_propagate(errp, local_err); 30469e85cd5cSStefan Hajnoczi goto out; 3047ed61fc10SJeff Cody } 30489e85cd5cSStefan Hajnoczi 30499e85cd5cSStefan Hajnoczi out: 30509e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3051ed61fc10SJeff Cody } 3052ed61fc10SJeff Cody 305378f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 305499a9addfSStefan Hajnoczi bool has_format, const char *format, 3055b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 305699a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 305799a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3058d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 305978f51fdeSJohn Snow bool has_on_source_error, 306078f51fdeSJohn Snow BlockdevOnError on_source_error, 306178f51fdeSJohn Snow bool has_on_target_error, 306278f51fdeSJohn Snow BlockdevOnError on_target_error, 306378f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 306499a9addfSStefan Hajnoczi { 3065a0e8544cSFam Zheng BlockBackend *blk; 306699a9addfSStefan Hajnoczi BlockDriverState *bs; 306799a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3068fc5d3f84SIan Main BlockDriverState *source = NULL; 3069d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3070761731b1SStefan Hajnoczi AioContext *aio_context; 3071e6641719SMax Reitz QDict *options = NULL; 307299a9addfSStefan Hajnoczi Error *local_err = NULL; 307399a9addfSStefan Hajnoczi int flags; 307499a9addfSStefan Hajnoczi int64_t size; 307599a9addfSStefan Hajnoczi int ret; 307699a9addfSStefan Hajnoczi 307799a9addfSStefan Hajnoczi if (!has_speed) { 307899a9addfSStefan Hajnoczi speed = 0; 307999a9addfSStefan Hajnoczi } 308099a9addfSStefan Hajnoczi if (!has_on_source_error) { 308199a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 308299a9addfSStefan Hajnoczi } 308399a9addfSStefan Hajnoczi if (!has_on_target_error) { 308499a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 308599a9addfSStefan Hajnoczi } 308699a9addfSStefan Hajnoczi if (!has_mode) { 308799a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 308899a9addfSStefan Hajnoczi } 308999a9addfSStefan Hajnoczi 3090a0e8544cSFam Zheng blk = blk_by_name(device); 3091a0e8544cSFam Zheng if (!blk) { 309275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 309375158ebbSMarkus Armbruster "Device '%s' not found", device); 309499a9addfSStefan Hajnoczi return; 309599a9addfSStefan Hajnoczi } 309699a9addfSStefan Hajnoczi 30975433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3098761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3099761731b1SStefan Hajnoczi 3100c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3101c29c1dd3SFam Zheng * do an early check redundantly. */ 31025433c24fSMax Reitz if (!blk_is_available(blk)) { 3103c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3104761731b1SStefan Hajnoczi goto out; 310599a9addfSStefan Hajnoczi } 31065433c24fSMax Reitz bs = blk_bs(blk); 310799a9addfSStefan Hajnoczi 310899a9addfSStefan Hajnoczi if (!has_format) { 310999a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 311099a9addfSStefan Hajnoczi } 311199a9addfSStefan Hajnoczi 3112c29c1dd3SFam Zheng /* Early check to avoid creating target */ 31133718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3114761731b1SStefan Hajnoczi goto out; 311599a9addfSStefan Hajnoczi } 311699a9addfSStefan Hajnoczi 311799a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 311899a9addfSStefan Hajnoczi 3119fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3120fc5d3f84SIan Main * on top of. */ 3121fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3122760e0063SKevin Wolf source = backing_bs(bs); 3123fc5d3f84SIan Main if (!source) { 3124fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3125fc5d3f84SIan Main } 3126fc5d3f84SIan Main } 3127fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3128fc5d3f84SIan Main source = bs; 3129fc5d3f84SIan Main } 3130fc5d3f84SIan Main 313199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 313299a9addfSStefan Hajnoczi if (size < 0) { 313399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3134761731b1SStefan Hajnoczi goto out; 313599a9addfSStefan Hajnoczi } 313699a9addfSStefan Hajnoczi 313799a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3138e6641719SMax Reitz assert(format); 3139fc5d3f84SIan Main if (source) { 3140fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3141fc5d3f84SIan Main source->drv->format_name, NULL, 3142fc5d3f84SIan Main size, flags, &local_err, false); 3143fc5d3f84SIan Main } else { 3144fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3145fc5d3f84SIan Main size, flags, &local_err, false); 3146fc5d3f84SIan Main } 314799a9addfSStefan Hajnoczi } 314899a9addfSStefan Hajnoczi 314984d18f06SMarkus Armbruster if (local_err) { 315099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3151761731b1SStefan Hajnoczi goto out; 315299a9addfSStefan Hajnoczi } 315399a9addfSStefan Hajnoczi 3154e6641719SMax Reitz if (format) { 3155e6641719SMax Reitz options = qdict_new(); 3156e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3157e6641719SMax Reitz } 3158e6641719SMax Reitz 3159f67503e5SMax Reitz target_bs = NULL; 31606ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 316199a9addfSStefan Hajnoczi if (ret < 0) { 316234b5d2c6SMax Reitz error_propagate(errp, local_err); 3163761731b1SStefan Hajnoczi goto out; 316499a9addfSStefan Hajnoczi } 316599a9addfSStefan Hajnoczi 3166761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3167761731b1SStefan Hajnoczi 3168d58d8453SJohn Snow if (has_bitmap) { 3169d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3170d58d8453SJohn Snow if (!bmap) { 3171d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 31720702d3d8SMax Reitz bdrv_unref(target_bs); 3173d58d8453SJohn Snow goto out; 3174d58d8453SJohn Snow } 3175d58d8453SJohn Snow } 3176d58d8453SJohn Snow 3177d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3178d58d8453SJohn Snow on_source_error, on_target_error, 317978f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 318099a9addfSStefan Hajnoczi if (local_err != NULL) { 31814f6fd349SFam Zheng bdrv_unref(target_bs); 318299a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3183761731b1SStefan Hajnoczi goto out; 318499a9addfSStefan Hajnoczi } 3185761731b1SStefan Hajnoczi 3186761731b1SStefan Hajnoczi out: 3187761731b1SStefan Hajnoczi aio_context_release(aio_context); 318899a9addfSStefan Hajnoczi } 318999a9addfSStefan Hajnoczi 319078f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 319178f51fdeSJohn Snow bool has_format, const char *format, 319278f51fdeSJohn Snow enum MirrorSyncMode sync, 319378f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 319478f51fdeSJohn Snow bool has_speed, int64_t speed, 319578f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 319678f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 319778f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 319878f51fdeSJohn Snow Error **errp) 319978f51fdeSJohn Snow { 320078f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 320178f51fdeSJohn Snow has_mode, mode, has_speed, speed, 320278f51fdeSJohn Snow has_bitmap, bitmap, 320378f51fdeSJohn Snow has_on_source_error, on_source_error, 320478f51fdeSJohn Snow has_on_target_error, on_target_error, 320578f51fdeSJohn Snow NULL, errp); 320678f51fdeSJohn Snow } 320778f51fdeSJohn Snow 3208c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3209c13163fbSBenoît Canet { 3210d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3211c13163fbSBenoît Canet } 3212c13163fbSBenoît Canet 321378f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3214c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3215c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3216c29c1dd3SFam Zheng bool has_on_source_error, 3217c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3218c29c1dd3SFam Zheng bool has_on_target_error, 3219c29c1dd3SFam Zheng BlockdevOnError on_target_error, 322078f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3221c29c1dd3SFam Zheng { 32225433c24fSMax Reitz BlockBackend *blk, *target_blk; 3223c29c1dd3SFam Zheng BlockDriverState *bs; 3224c29c1dd3SFam Zheng BlockDriverState *target_bs; 3225c29c1dd3SFam Zheng Error *local_err = NULL; 3226c29c1dd3SFam Zheng AioContext *aio_context; 3227c29c1dd3SFam Zheng 3228c29c1dd3SFam Zheng if (!has_speed) { 3229c29c1dd3SFam Zheng speed = 0; 3230c29c1dd3SFam Zheng } 3231c29c1dd3SFam Zheng if (!has_on_source_error) { 3232c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3233c29c1dd3SFam Zheng } 3234c29c1dd3SFam Zheng if (!has_on_target_error) { 3235c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3236c29c1dd3SFam Zheng } 3237c29c1dd3SFam Zheng 3238a0e8544cSFam Zheng blk = blk_by_name(device); 3239a0e8544cSFam Zheng if (!blk) { 32405b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3241c29c1dd3SFam Zheng return; 3242c29c1dd3SFam Zheng } 3243c29c1dd3SFam Zheng 32445433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3245c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3246c29c1dd3SFam Zheng 32475433c24fSMax Reitz if (!blk_is_available(blk)) { 32485433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 32495433c24fSMax Reitz goto out; 32505433c24fSMax Reitz } 32515433c24fSMax Reitz bs = blk_bs(blk); 32525433c24fSMax Reitz 32535433c24fSMax Reitz target_blk = blk_by_name(target); 32545433c24fSMax Reitz if (!target_blk) { 32555b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3256c29c1dd3SFam Zheng goto out; 3257c29c1dd3SFam Zheng } 32585433c24fSMax Reitz 32595433c24fSMax Reitz if (!blk_is_available(target_blk)) { 32605433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 32615433c24fSMax Reitz goto out; 32625433c24fSMax Reitz } 32635433c24fSMax Reitz target_bs = blk_bs(target_blk); 3264c29c1dd3SFam Zheng 3265c29c1dd3SFam Zheng bdrv_ref(target_bs); 3266c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3267d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 326878f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3269c29c1dd3SFam Zheng if (local_err != NULL) { 3270c29c1dd3SFam Zheng bdrv_unref(target_bs); 3271c29c1dd3SFam Zheng error_propagate(errp, local_err); 3272c29c1dd3SFam Zheng } 3273c29c1dd3SFam Zheng out: 3274c29c1dd3SFam Zheng aio_context_release(aio_context); 3275c29c1dd3SFam Zheng } 3276c29c1dd3SFam Zheng 327778f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 327878f51fdeSJohn Snow enum MirrorSyncMode sync, 327978f51fdeSJohn Snow bool has_speed, int64_t speed, 328078f51fdeSJohn Snow bool has_on_source_error, 328178f51fdeSJohn Snow BlockdevOnError on_source_error, 328278f51fdeSJohn Snow bool has_on_target_error, 328378f51fdeSJohn Snow BlockdevOnError on_target_error, 328478f51fdeSJohn Snow Error **errp) 328578f51fdeSJohn Snow { 328678f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 328778f51fdeSJohn Snow has_on_source_error, on_source_error, 328878f51fdeSJohn Snow has_on_target_error, on_target_error, 328978f51fdeSJohn Snow NULL, errp); 329078f51fdeSJohn Snow } 329178f51fdeSJohn Snow 32924193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 32934193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 32944193cdd7SFam Zheng **/ 32954193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 32964193cdd7SFam Zheng BlockDriverState *target, 329709158f00SBenoît Canet bool has_replaces, const char *replaces, 3298d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3299b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3300eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 330108e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 33024193cdd7SFam Zheng bool has_on_source_error, 33034193cdd7SFam Zheng BlockdevOnError on_source_error, 33044193cdd7SFam Zheng bool has_on_target_error, 33054193cdd7SFam Zheng BlockdevOnError on_target_error, 33060fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3307b952b558SPaolo Bonzini Error **errp) 3308d9b902dbSPaolo Bonzini { 3309d9b902dbSPaolo Bonzini 3310d9b902dbSPaolo Bonzini if (!has_speed) { 3311d9b902dbSPaolo Bonzini speed = 0; 3312d9b902dbSPaolo Bonzini } 3313b952b558SPaolo Bonzini if (!has_on_source_error) { 3314b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3315b952b558SPaolo Bonzini } 3316b952b558SPaolo Bonzini if (!has_on_target_error) { 3317b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3318b952b558SPaolo Bonzini } 3319eee13dfeSPaolo Bonzini if (!has_granularity) { 3320eee13dfeSPaolo Bonzini granularity = 0; 3321eee13dfeSPaolo Bonzini } 332208e4ed6cSPaolo Bonzini if (!has_buf_size) { 332348ac0a4dSWen Congyang buf_size = 0; 332408e4ed6cSPaolo Bonzini } 33250fc9f8eaSFam Zheng if (!has_unmap) { 33260fc9f8eaSFam Zheng unmap = true; 33270fc9f8eaSFam Zheng } 332808e4ed6cSPaolo Bonzini 3329eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3330c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 33313cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3332eee13dfeSPaolo Bonzini return; 3333eee13dfeSPaolo Bonzini } 3334eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3335c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3336c6bd8c70SMarkus Armbruster "power of 2"); 3337eee13dfeSPaolo Bonzini return; 3338eee13dfeSPaolo Bonzini } 3339d9b902dbSPaolo Bonzini 33404193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 33414193cdd7SFam Zheng return; 33424193cdd7SFam Zheng } 3343e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3344e40e5027SFam Zheng return; 3345e40e5027SFam Zheng } 3346df92562eSFam Zheng if (target->blk) { 3347df92562eSFam Zheng error_setg(errp, "Cannot mirror to an attached block device"); 3348df92562eSFam Zheng return; 3349df92562eSFam Zheng } 33504193cdd7SFam Zheng 33514193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 33524193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 33534193cdd7SFam Zheng } 33544193cdd7SFam Zheng 33554193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 33564193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 33574193cdd7SFam Zheng */ 33584193cdd7SFam Zheng mirror_start(bs, target, 33594193cdd7SFam Zheng has_replaces ? replaces : NULL, 33604193cdd7SFam Zheng speed, granularity, buf_size, sync, 33614193cdd7SFam Zheng on_source_error, on_target_error, unmap, 33624193cdd7SFam Zheng block_job_cb, bs, errp); 33634193cdd7SFam Zheng } 33644193cdd7SFam Zheng 33654193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 33664193cdd7SFam Zheng bool has_format, const char *format, 33674193cdd7SFam Zheng bool has_node_name, const char *node_name, 33684193cdd7SFam Zheng bool has_replaces, const char *replaces, 33694193cdd7SFam Zheng enum MirrorSyncMode sync, 33704193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 33714193cdd7SFam Zheng bool has_speed, int64_t speed, 33724193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 33734193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 33744193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 33754193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 33764193cdd7SFam Zheng bool has_unmap, bool unmap, 33774193cdd7SFam Zheng Error **errp) 33784193cdd7SFam Zheng { 33794193cdd7SFam Zheng BlockDriverState *bs; 33804193cdd7SFam Zheng BlockBackend *blk; 33814193cdd7SFam Zheng BlockDriverState *source, *target_bs; 33824193cdd7SFam Zheng AioContext *aio_context; 33834193cdd7SFam Zheng Error *local_err = NULL; 33844193cdd7SFam Zheng QDict *options = NULL; 33854193cdd7SFam Zheng int flags; 33864193cdd7SFam Zheng int64_t size; 33874193cdd7SFam Zheng int ret; 33884193cdd7SFam Zheng 3389a0e8544cSFam Zheng blk = blk_by_name(device); 3390a0e8544cSFam Zheng if (!blk) { 339175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 339275158ebbSMarkus Armbruster "Device '%s' not found", device); 3393d9b902dbSPaolo Bonzini return; 3394d9b902dbSPaolo Bonzini } 3395d9b902dbSPaolo Bonzini 33965433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 33975a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 33985a7e7a0bSStefan Hajnoczi 33995433c24fSMax Reitz if (!blk_is_available(blk)) { 3400c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 34015a7e7a0bSStefan Hajnoczi goto out; 3402d9b902dbSPaolo Bonzini } 34035433c24fSMax Reitz bs = blk_bs(blk); 34044193cdd7SFam Zheng if (!has_mode) { 34054193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 34064193cdd7SFam Zheng } 3407d9b902dbSPaolo Bonzini 3408d9b902dbSPaolo Bonzini if (!has_format) { 3409d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3410d9b902dbSPaolo Bonzini } 3411d9b902dbSPaolo Bonzini 3412d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3413760e0063SKevin Wolf source = backing_bs(bs); 3414d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3415d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3416d9b902dbSPaolo Bonzini } 3417117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3418117e0c82SMax Reitz source = bs; 3419117e0c82SMax Reitz } 3420d9b902dbSPaolo Bonzini 3421ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3422ac3c5d83SStefan Hajnoczi if (size < 0) { 3423ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 34245a7e7a0bSStefan Hajnoczi goto out; 3425ac3c5d83SStefan Hajnoczi } 3426ac3c5d83SStefan Hajnoczi 342709158f00SBenoît Canet if (has_replaces) { 342809158f00SBenoît Canet BlockDriverState *to_replace_bs; 34295a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 34305a7e7a0bSStefan Hajnoczi int64_t replace_size; 343109158f00SBenoît Canet 343209158f00SBenoît Canet if (!has_node_name) { 343309158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 343409158f00SBenoît Canet " named node of the graph"); 34355a7e7a0bSStefan Hajnoczi goto out; 343609158f00SBenoît Canet } 343709158f00SBenoît Canet 3438e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 343909158f00SBenoît Canet 344009158f00SBenoît Canet if (!to_replace_bs) { 344109158f00SBenoît Canet error_propagate(errp, local_err); 34425a7e7a0bSStefan Hajnoczi goto out; 344309158f00SBenoît Canet } 344409158f00SBenoît Canet 34455a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 34465a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 34475a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 34485a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 34495a7e7a0bSStefan Hajnoczi 34505a7e7a0bSStefan Hajnoczi if (size != replace_size) { 345109158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 345209158f00SBenoît Canet "different size"); 34535a7e7a0bSStefan Hajnoczi goto out; 345409158f00SBenoît Canet } 345509158f00SBenoît Canet } 345609158f00SBenoît Canet 345714526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 345814526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 345914526864SMax Reitz { 3460d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3461e6641719SMax Reitz assert(format); 3462cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3463f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3464d9b902dbSPaolo Bonzini } else { 3465d9b902dbSPaolo Bonzini switch (mode) { 3466d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3467d9b902dbSPaolo Bonzini break; 3468d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3469d9b902dbSPaolo Bonzini /* create new image with backing file */ 3470cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3471d9b902dbSPaolo Bonzini source->filename, 3472d9b902dbSPaolo Bonzini source->drv->format_name, 3473f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3474d9b902dbSPaolo Bonzini break; 3475d9b902dbSPaolo Bonzini default: 3476d9b902dbSPaolo Bonzini abort(); 3477d9b902dbSPaolo Bonzini } 3478d9b902dbSPaolo Bonzini } 3479d9b902dbSPaolo Bonzini 348084d18f06SMarkus Armbruster if (local_err) { 3481cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 34825a7e7a0bSStefan Hajnoczi goto out; 3483d9b902dbSPaolo Bonzini } 3484d9b902dbSPaolo Bonzini 34854c828dc6SBenoît Canet options = qdict_new(); 3486e6641719SMax Reitz if (has_node_name) { 34874c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 34884c828dc6SBenoît Canet } 3489e6641719SMax Reitz if (format) { 3490e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3491e6641719SMax Reitz } 34924c828dc6SBenoît Canet 3493b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3494b812f671SPaolo Bonzini * file. 3495b812f671SPaolo Bonzini */ 3496f67503e5SMax Reitz target_bs = NULL; 34974c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 34986ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3499d9b902dbSPaolo Bonzini if (ret < 0) { 350034b5d2c6SMax Reitz error_propagate(errp, local_err); 35015a7e7a0bSStefan Hajnoczi goto out; 3502d9b902dbSPaolo Bonzini } 3503d9b902dbSPaolo Bonzini 35045a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 35055a7e7a0bSStefan Hajnoczi 35064193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 35074193cdd7SFam Zheng has_replaces, replaces, sync, 35084193cdd7SFam Zheng has_speed, speed, 35094193cdd7SFam Zheng has_granularity, granularity, 35104193cdd7SFam Zheng has_buf_size, buf_size, 35114193cdd7SFam Zheng has_on_source_error, on_source_error, 35124193cdd7SFam Zheng has_on_target_error, on_target_error, 35134193cdd7SFam Zheng has_unmap, unmap, 35144193cdd7SFam Zheng &local_err); 35154193cdd7SFam Zheng if (local_err) { 3516d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 35174193cdd7SFam Zheng bdrv_unref(target_bs); 3518d9b902dbSPaolo Bonzini } 35195a7e7a0bSStefan Hajnoczi out: 35205a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3521d9b902dbSPaolo Bonzini } 3522d9b902dbSPaolo Bonzini 3523df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3524df92562eSFam Zheng bool has_replaces, const char *replaces, 3525df92562eSFam Zheng MirrorSyncMode sync, 3526df92562eSFam Zheng bool has_speed, int64_t speed, 3527df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3528df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3529df92562eSFam Zheng bool has_on_source_error, 3530df92562eSFam Zheng BlockdevOnError on_source_error, 3531df92562eSFam Zheng bool has_on_target_error, 3532df92562eSFam Zheng BlockdevOnError on_target_error, 3533df92562eSFam Zheng Error **errp) 3534df92562eSFam Zheng { 3535df92562eSFam Zheng BlockDriverState *bs; 3536df92562eSFam Zheng BlockBackend *blk; 3537df92562eSFam Zheng BlockDriverState *target_bs; 3538df92562eSFam Zheng AioContext *aio_context; 3539df92562eSFam Zheng Error *local_err = NULL; 3540df92562eSFam Zheng 3541df92562eSFam Zheng blk = blk_by_name(device); 3542df92562eSFam Zheng if (!blk) { 3543df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3544df92562eSFam Zheng return; 3545df92562eSFam Zheng } 3546df92562eSFam Zheng bs = blk_bs(blk); 3547df92562eSFam Zheng 3548df92562eSFam Zheng if (!bs) { 3549df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3550df92562eSFam Zheng return; 3551df92562eSFam Zheng } 3552df92562eSFam Zheng 3553df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3554df92562eSFam Zheng if (!target_bs) { 3555df92562eSFam Zheng return; 3556df92562eSFam Zheng } 3557df92562eSFam Zheng 3558df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3559df92562eSFam Zheng aio_context_acquire(aio_context); 3560df92562eSFam Zheng 3561df92562eSFam Zheng bdrv_ref(target_bs); 3562df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3563df92562eSFam Zheng 3564df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3565df92562eSFam Zheng has_replaces, replaces, sync, 3566df92562eSFam Zheng has_speed, speed, 3567df92562eSFam Zheng has_granularity, granularity, 3568df92562eSFam Zheng has_buf_size, buf_size, 3569df92562eSFam Zheng has_on_source_error, on_source_error, 3570df92562eSFam Zheng has_on_target_error, on_target_error, 3571df92562eSFam Zheng true, true, 3572df92562eSFam Zheng &local_err); 3573df92562eSFam Zheng if (local_err) { 3574df92562eSFam Zheng error_propagate(errp, local_err); 3575df92562eSFam Zheng bdrv_unref(target_bs); 3576df92562eSFam Zheng } 3577df92562eSFam Zheng 3578df92562eSFam Zheng aio_context_release(aio_context); 3579df92562eSFam Zheng } 3580df92562eSFam Zheng 35813d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 358224d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 358324d6bffeSMarkus Armbruster Error **errp) 35842d47c6e9SStefan Hajnoczi { 3585a0e8544cSFam Zheng BlockBackend *blk; 35862d47c6e9SStefan Hajnoczi BlockDriverState *bs; 35872d47c6e9SStefan Hajnoczi 35885433c24fSMax Reitz *aio_context = NULL; 35895433c24fSMax Reitz 3590a0e8544cSFam Zheng blk = blk_by_name(device); 3591a0e8544cSFam Zheng if (!blk) { 35923d948cdfSStefan Hajnoczi goto notfound; 35932d47c6e9SStefan Hajnoczi } 35943d948cdfSStefan Hajnoczi 35955433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 35963d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 35973d948cdfSStefan Hajnoczi 35985433c24fSMax Reitz if (!blk_is_available(blk)) { 35995433c24fSMax Reitz goto notfound; 36005433c24fSMax Reitz } 36015433c24fSMax Reitz bs = blk_bs(blk); 36025433c24fSMax Reitz 36033d948cdfSStefan Hajnoczi if (!bs->job) { 36043d948cdfSStefan Hajnoczi goto notfound; 36053d948cdfSStefan Hajnoczi } 36063d948cdfSStefan Hajnoczi 36072d47c6e9SStefan Hajnoczi return bs->job; 36083d948cdfSStefan Hajnoczi 36093d948cdfSStefan Hajnoczi notfound: 36102e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 36112e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 36125433c24fSMax Reitz if (*aio_context) { 36135433c24fSMax Reitz aio_context_release(*aio_context); 36143d948cdfSStefan Hajnoczi *aio_context = NULL; 36155433c24fSMax Reitz } 36163d948cdfSStefan Hajnoczi return NULL; 36172d47c6e9SStefan Hajnoczi } 36182d47c6e9SStefan Hajnoczi 3619882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 36202d47c6e9SStefan Hajnoczi { 36213d948cdfSStefan Hajnoczi AioContext *aio_context; 362224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36232d47c6e9SStefan Hajnoczi 36242d47c6e9SStefan Hajnoczi if (!job) { 36252d47c6e9SStefan Hajnoczi return; 36262d47c6e9SStefan Hajnoczi } 36272d47c6e9SStefan Hajnoczi 3628882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 36293d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36302d47c6e9SStefan Hajnoczi } 3631370521a1SStefan Hajnoczi 36326e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 36336e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 36346e37fb81SPaolo Bonzini { 36353d948cdfSStefan Hajnoczi AioContext *aio_context; 363624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36376e37fb81SPaolo Bonzini 36386e37fb81SPaolo Bonzini if (!job) { 36396e37fb81SPaolo Bonzini return; 36406e37fb81SPaolo Bonzini } 36413d948cdfSStefan Hajnoczi 36423d948cdfSStefan Hajnoczi if (!has_force) { 36433d948cdfSStefan Hajnoczi force = false; 36443d948cdfSStefan Hajnoczi } 36453d948cdfSStefan Hajnoczi 3646751ebd76SFam Zheng if (job->user_paused && !force) { 3647f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3648f231b88dSCole Robinson device); 36493d948cdfSStefan Hajnoczi goto out; 36506e37fb81SPaolo Bonzini } 36516e37fb81SPaolo Bonzini 36526e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 36536e37fb81SPaolo Bonzini block_job_cancel(job); 36543d948cdfSStefan Hajnoczi out: 36553d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36566e37fb81SPaolo Bonzini } 36576e37fb81SPaolo Bonzini 36586e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3659370521a1SStefan Hajnoczi { 36603d948cdfSStefan Hajnoczi AioContext *aio_context; 366124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3662370521a1SStefan Hajnoczi 3663751ebd76SFam Zheng if (!job || job->user_paused) { 3664370521a1SStefan Hajnoczi return; 3665370521a1SStefan Hajnoczi } 36666e37fb81SPaolo Bonzini 3667751ebd76SFam Zheng job->user_paused = true; 36686e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 36696e37fb81SPaolo Bonzini block_job_pause(job); 36703d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36716e37fb81SPaolo Bonzini } 36726e37fb81SPaolo Bonzini 36736e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 36746e37fb81SPaolo Bonzini { 36753d948cdfSStefan Hajnoczi AioContext *aio_context; 367624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36776e37fb81SPaolo Bonzini 3678751ebd76SFam Zheng if (!job || !job->user_paused) { 36798acc72a4SPaolo Bonzini return; 36808acc72a4SPaolo Bonzini } 3681370521a1SStefan Hajnoczi 3682751ebd76SFam Zheng job->user_paused = false; 36836e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 36846e37fb81SPaolo Bonzini block_job_resume(job); 36853d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3686370521a1SStefan Hajnoczi } 3687fb5458cdSStefan Hajnoczi 3688aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3689aeae883bSPaolo Bonzini { 36903d948cdfSStefan Hajnoczi AioContext *aio_context; 369124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3692aeae883bSPaolo Bonzini 3693aeae883bSPaolo Bonzini if (!job) { 3694aeae883bSPaolo Bonzini return; 3695aeae883bSPaolo Bonzini } 3696aeae883bSPaolo Bonzini 3697aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3698aeae883bSPaolo Bonzini block_job_complete(job, errp); 36993d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3700aeae883bSPaolo Bonzini } 3701aeae883bSPaolo Bonzini 3702fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3703fa40e656SJeff Cody const char *image_node_name, 3704fa40e656SJeff Cody const char *backing_file, 3705fa40e656SJeff Cody Error **errp) 3706fa40e656SJeff Cody { 3707a0e8544cSFam Zheng BlockBackend *blk; 3708fa40e656SJeff Cody BlockDriverState *bs = NULL; 3709729962f6SStefan Hajnoczi AioContext *aio_context; 3710fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3711fa40e656SJeff Cody Error *local_err = NULL; 3712fa40e656SJeff Cody bool ro; 3713fa40e656SJeff Cody int open_flags; 3714fa40e656SJeff Cody int ret; 3715fa40e656SJeff Cody 3716a0e8544cSFam Zheng blk = blk_by_name(device); 3717a0e8544cSFam Zheng if (!blk) { 371875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 371975158ebbSMarkus Armbruster "Device '%s' not found", device); 3720fa40e656SJeff Cody return; 3721fa40e656SJeff Cody } 3722fa40e656SJeff Cody 37235433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3724729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3725729962f6SStefan Hajnoczi 37265433c24fSMax Reitz if (!blk_is_available(blk)) { 37275433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 37285433c24fSMax Reitz goto out; 37295433c24fSMax Reitz } 37305433c24fSMax Reitz bs = blk_bs(blk); 37315433c24fSMax Reitz 3732fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3733fa40e656SJeff Cody if (local_err) { 3734fa40e656SJeff Cody error_propagate(errp, local_err); 3735729962f6SStefan Hajnoczi goto out; 3736fa40e656SJeff Cody } 3737fa40e656SJeff Cody 3738fa40e656SJeff Cody if (!image_bs) { 3739fa40e656SJeff Cody error_setg(errp, "image file not found"); 3740729962f6SStefan Hajnoczi goto out; 3741fa40e656SJeff Cody } 3742fa40e656SJeff Cody 3743fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3744fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3745fa40e656SJeff Cody "without a backing file"); 3746729962f6SStefan Hajnoczi goto out; 3747fa40e656SJeff Cody } 3748fa40e656SJeff Cody 3749fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3750fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3751fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3752729962f6SStefan Hajnoczi goto out; 3753fa40e656SJeff Cody } 3754fa40e656SJeff Cody 3755fa40e656SJeff Cody /* final sanity check */ 3756fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3757fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3758fa40e656SJeff Cody device); 3759729962f6SStefan Hajnoczi goto out; 3760fa40e656SJeff Cody } 3761fa40e656SJeff Cody 3762fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3763fa40e656SJeff Cody open_flags = image_bs->open_flags; 3764fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3765fa40e656SJeff Cody 3766fa40e656SJeff Cody if (ro) { 3767fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3768fa40e656SJeff Cody if (local_err) { 3769fa40e656SJeff Cody error_propagate(errp, local_err); 3770729962f6SStefan Hajnoczi goto out; 3771fa40e656SJeff Cody } 3772fa40e656SJeff Cody } 3773fa40e656SJeff Cody 3774fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3775fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3776fa40e656SJeff Cody 3777fa40e656SJeff Cody if (ret < 0) { 3778fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3779fa40e656SJeff Cody backing_file); 3780fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3781fa40e656SJeff Cody * appropriate */ 3782fa40e656SJeff Cody } 3783fa40e656SJeff Cody 3784fa40e656SJeff Cody if (ro) { 3785fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3786fa40e656SJeff Cody if (local_err) { 3787fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3788fa40e656SJeff Cody } 3789fa40e656SJeff Cody } 3790729962f6SStefan Hajnoczi 3791729962f6SStefan Hajnoczi out: 3792729962f6SStefan Hajnoczi aio_context_release(aio_context); 3793fa40e656SJeff Cody } 3794fa40e656SJeff Cody 3795d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3796d26c9a15SKevin Wolf { 3797d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3798be4b67bcSMax Reitz BlockDriverState *bs; 3799be4b67bcSMax Reitz BlockBackend *blk = NULL; 3800d26c9a15SKevin Wolf QObject *obj; 3801d26c9a15SKevin Wolf QDict *qdict; 3802d26c9a15SKevin Wolf Error *local_err = NULL; 3803d26c9a15SKevin Wolf 380460e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3805d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 380660e19e06SMarkus Armbruster * when called from drive_new(). 3807d26c9a15SKevin Wolf * 3808d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3809d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3810c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3811c6e0bd9bSKevin Wolf options->cache->has_direct && 3812c6e0bd9bSKevin Wolf options->cache->direct; 3813c6e0bd9bSKevin Wolf if (!direct) { 3814d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3815d26c9a15SKevin Wolf goto fail; 3816d26c9a15SKevin Wolf } 3817d26c9a15SKevin Wolf } 3818d26c9a15SKevin Wolf 3819d26c9a15SKevin Wolf visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3820d26c9a15SKevin Wolf &options, NULL, &local_err); 382184d18f06SMarkus Armbruster if (local_err) { 3822d26c9a15SKevin Wolf error_propagate(errp, local_err); 3823d26c9a15SKevin Wolf goto fail; 3824d26c9a15SKevin Wolf } 3825d26c9a15SKevin Wolf 3826d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3827d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3828d26c9a15SKevin Wolf 3829d26c9a15SKevin Wolf qdict_flatten(qdict); 3830d26c9a15SKevin Wolf 3831be4b67bcSMax Reitz if (options->has_id) { 383218e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 383384d18f06SMarkus Armbruster if (local_err) { 3834b681072dSKevin Wolf error_propagate(errp, local_err); 3835d26c9a15SKevin Wolf goto fail; 3836d26c9a15SKevin Wolf } 3837d26c9a15SKevin Wolf 3838be4b67bcSMax Reitz bs = blk_bs(blk); 3839be4b67bcSMax Reitz } else { 3840be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3841be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3842be4b67bcSMax Reitz "the root node"); 3843be4b67bcSMax Reitz goto fail; 3844be4b67bcSMax Reitz } 3845be4b67bcSMax Reitz 3846bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3847bd745e23SMax Reitz if (!bs) { 3848be4b67bcSMax Reitz goto fail; 3849be4b67bcSMax Reitz } 3850be4b67bcSMax Reitz } 3851be4b67bcSMax Reitz 3852be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3853be4b67bcSMax Reitz if (blk) { 385418e46a03SMarkus Armbruster blk_unref(blk); 3855be4b67bcSMax Reitz } else { 3856be4b67bcSMax Reitz bdrv_unref(bs); 3857be4b67bcSMax Reitz } 38588ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 38598ae8e904SKevin Wolf goto fail; 38608ae8e904SKevin Wolf } 38618ae8e904SKevin Wolf 3862d26c9a15SKevin Wolf fail: 3863d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3864d26c9a15SKevin Wolf } 3865d26c9a15SKevin Wolf 386681b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 386781b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 386881b936aeSAlberto Garcia { 386981b936aeSAlberto Garcia AioContext *aio_context; 387081b936aeSAlberto Garcia BlockBackend *blk; 387181b936aeSAlberto Garcia BlockDriverState *bs; 387281b936aeSAlberto Garcia 387381b936aeSAlberto Garcia if (has_id && has_node_name) { 387481b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 387581b936aeSAlberto Garcia return; 387681b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 387781b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 387881b936aeSAlberto Garcia return; 387981b936aeSAlberto Garcia } 388081b936aeSAlberto Garcia 388181b936aeSAlberto Garcia if (has_id) { 388281b936aeSAlberto Garcia blk = blk_by_name(id); 388381b936aeSAlberto Garcia if (!blk) { 388481b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 388581b936aeSAlberto Garcia return; 388681b936aeSAlberto Garcia } 388781b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 388881b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 388981b936aeSAlberto Garcia return; 389081b936aeSAlberto Garcia } 389181b936aeSAlberto Garcia bs = blk_bs(blk); 389281b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 389381b936aeSAlberto Garcia } else { 389481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 389581b936aeSAlberto Garcia if (!bs) { 389681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 389781b936aeSAlberto Garcia return; 389881b936aeSAlberto Garcia } 389981b936aeSAlberto Garcia blk = bs->blk; 390081b936aeSAlberto Garcia if (blk) { 390181b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 390281b936aeSAlberto Garcia node_name, blk_name(blk)); 390381b936aeSAlberto Garcia return; 390481b936aeSAlberto Garcia } 390581b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 390681b936aeSAlberto Garcia } 390781b936aeSAlberto Garcia 390881b936aeSAlberto Garcia aio_context_acquire(aio_context); 390981b936aeSAlberto Garcia 391081b936aeSAlberto Garcia if (bs) { 391181b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 391281b936aeSAlberto Garcia goto out; 391381b936aeSAlberto Garcia } 391481b936aeSAlberto Garcia 391581b936aeSAlberto Garcia if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { 391681b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 391781b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 391881b936aeSAlberto Garcia goto out; 391981b936aeSAlberto Garcia } 392081b936aeSAlberto Garcia } 392181b936aeSAlberto Garcia 392281b936aeSAlberto Garcia if (blk) { 392381b936aeSAlberto Garcia blk_unref(blk); 392481b936aeSAlberto Garcia } else { 392581b936aeSAlberto Garcia bdrv_unref(bs); 392681b936aeSAlberto Garcia } 392781b936aeSAlberto Garcia 392881b936aeSAlberto Garcia out: 392981b936aeSAlberto Garcia aio_context_release(aio_context); 393081b936aeSAlberto Garcia } 393181b936aeSAlberto Garcia 3932fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3933fb5458cdSStefan Hajnoczi { 3934fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3935fea68bb6SMarkus Armbruster BlockDriverState *bs; 3936fea68bb6SMarkus Armbruster 3937fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 393869691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 393969691e72SStefan Hajnoczi 394069691e72SStefan Hajnoczi aio_context_acquire(aio_context); 394169691e72SStefan Hajnoczi 3942fea68bb6SMarkus Armbruster if (bs->job) { 3943fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3944fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 3945fea68bb6SMarkus Armbruster *p_next = elem; 3946fea68bb6SMarkus Armbruster p_next = &elem->next; 3947fea68bb6SMarkus Armbruster } 394869691e72SStefan Hajnoczi 394969691e72SStefan Hajnoczi aio_context_release(aio_context); 3950fea68bb6SMarkus Armbruster } 3951fea68bb6SMarkus Armbruster 3952fea68bb6SMarkus Armbruster return head; 3953fb5458cdSStefan Hajnoczi } 39544d454574SPaolo Bonzini 39550006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 39564d454574SPaolo Bonzini .name = "drive", 39570006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 39584d454574SPaolo Bonzini .desc = { 39594d454574SPaolo Bonzini { 39604d454574SPaolo Bonzini .name = "snapshot", 39614d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39624d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 39634d454574SPaolo Bonzini },{ 3964a9384affSPaolo Bonzini .name = "discard", 3965a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 3966a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 3967a9384affSPaolo Bonzini },{ 39684d454574SPaolo Bonzini .name = "aio", 39694d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39704d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 39714d454574SPaolo Bonzini },{ 39724d454574SPaolo Bonzini .name = "format", 39734d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39744d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 39754d454574SPaolo Bonzini },{ 39764d454574SPaolo Bonzini .name = "rerror", 39774d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39784d454574SPaolo Bonzini .help = "read error action", 39794d454574SPaolo Bonzini },{ 39804d454574SPaolo Bonzini .name = "werror", 39814d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39824d454574SPaolo Bonzini .help = "write error action", 39834d454574SPaolo Bonzini },{ 39840f227a94SKevin Wolf .name = "read-only", 39854d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39864d454574SPaolo Bonzini .help = "open drive file as read-only", 39874d454574SPaolo Bonzini },{ 398857975222SKevin Wolf .name = "throttling.iops-total", 39894d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39904d454574SPaolo Bonzini .help = "limit total I/O operations per second", 39914d454574SPaolo Bonzini },{ 399257975222SKevin Wolf .name = "throttling.iops-read", 39934d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39944d454574SPaolo Bonzini .help = "limit read operations per second", 39954d454574SPaolo Bonzini },{ 399657975222SKevin Wolf .name = "throttling.iops-write", 39974d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39984d454574SPaolo Bonzini .help = "limit write operations per second", 39994d454574SPaolo Bonzini },{ 400057975222SKevin Wolf .name = "throttling.bps-total", 40014d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40024d454574SPaolo Bonzini .help = "limit total bytes per second", 40034d454574SPaolo Bonzini },{ 400457975222SKevin Wolf .name = "throttling.bps-read", 40054d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40064d454574SPaolo Bonzini .help = "limit read bytes per second", 40074d454574SPaolo Bonzini },{ 400857975222SKevin Wolf .name = "throttling.bps-write", 40094d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40104d454574SPaolo Bonzini .help = "limit write bytes per second", 40114d454574SPaolo Bonzini },{ 40123e9fab69SBenoît Canet .name = "throttling.iops-total-max", 40133e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40143e9fab69SBenoît Canet .help = "I/O operations burst", 40153e9fab69SBenoît Canet },{ 40163e9fab69SBenoît Canet .name = "throttling.iops-read-max", 40173e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40183e9fab69SBenoît Canet .help = "I/O operations read burst", 40193e9fab69SBenoît Canet },{ 40203e9fab69SBenoît Canet .name = "throttling.iops-write-max", 40213e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40223e9fab69SBenoît Canet .help = "I/O operations write burst", 40233e9fab69SBenoît Canet },{ 40243e9fab69SBenoît Canet .name = "throttling.bps-total-max", 40253e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40263e9fab69SBenoît Canet .help = "total bytes burst", 40273e9fab69SBenoît Canet },{ 40283e9fab69SBenoît Canet .name = "throttling.bps-read-max", 40293e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40303e9fab69SBenoît Canet .help = "total bytes read burst", 40313e9fab69SBenoît Canet },{ 40323e9fab69SBenoît Canet .name = "throttling.bps-write-max", 40333e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40343e9fab69SBenoît Canet .help = "total bytes write burst", 40353e9fab69SBenoît Canet },{ 40362024c1dfSBenoît Canet .name = "throttling.iops-size", 40372024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 40382024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 40392024c1dfSBenoît Canet },{ 404076f4afb4SAlberto Garcia .name = "throttling.group", 404176f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 404276f4afb4SAlberto Garcia .help = "name of the block throttling group", 404376f4afb4SAlberto Garcia },{ 40444d454574SPaolo Bonzini .name = "copy-on-read", 40454d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 40464d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4047465bee1dSPeter Lieven },{ 4048465bee1dSPeter Lieven .name = "detect-zeroes", 4049465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4050465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4051362e9299SAlberto Garcia },{ 4052362e9299SAlberto Garcia .name = "stats-account-invalid", 4053362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4054362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4055362e9299SAlberto Garcia "in the statistics", 4056362e9299SAlberto Garcia },{ 4057362e9299SAlberto Garcia .name = "stats-account-failed", 4058362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4059362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4060362e9299SAlberto Garcia "in the statistics", 40614d454574SPaolo Bonzini }, 40624d454574SPaolo Bonzini { /* end of list */ } 40634d454574SPaolo Bonzini }, 40644d454574SPaolo Bonzini }; 40650006383eSKevin Wolf 4066bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4067bd745e23SMax Reitz .name = "root-bds", 4068bd745e23SMax Reitz .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 4069bd745e23SMax Reitz .desc = { 4070bd745e23SMax Reitz { 4071bd745e23SMax Reitz .name = "discard", 4072bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4073bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4074bd745e23SMax Reitz },{ 4075bd745e23SMax Reitz .name = "aio", 4076bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4077bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4078bd745e23SMax Reitz },{ 4079bd745e23SMax Reitz .name = "read-only", 4080bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4081bd745e23SMax Reitz .help = "open drive file as read-only", 4082bd745e23SMax Reitz },{ 4083bd745e23SMax Reitz .name = "copy-on-read", 4084bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4085bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4086bd745e23SMax Reitz },{ 4087bd745e23SMax Reitz .name = "detect-zeroes", 4088bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4089bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4090bd745e23SMax Reitz }, 4091bd745e23SMax Reitz { /* end of list */ } 4092bd745e23SMax Reitz }, 4093bd745e23SMax Reitz }; 4094bd745e23SMax Reitz 40950006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 40960006383eSKevin Wolf .name = "drive", 40970006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 40980006383eSKevin Wolf .desc = { 4099492fdc6fSKevin Wolf /* 4100492fdc6fSKevin Wolf * no elements => accept any params 4101492fdc6fSKevin Wolf * validation will happen later 4102492fdc6fSKevin Wolf */ 41030006383eSKevin Wolf { /* end of list */ } 41040006383eSKevin Wolf }, 41050006383eSKevin Wolf }; 4106