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) { 1585bbe86010SWenchao Xia error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1586bbe86010SWenchao Xia "device '%s' in abort: %s", 1587bbe86010SWenchao Xia sn->id_str, 1588bbe86010SWenchao Xia sn->name, 1589bbe86010SWenchao Xia bdrv_get_device_name(bs), 1590bbe86010SWenchao Xia error_get_pretty(local_error)); 1591bbe86010SWenchao Xia error_free(local_error); 1592bbe86010SWenchao Xia } 1593bbe86010SWenchao Xia } 1594bbe86010SWenchao Xia 159550f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 15965d6e96efSStefan Hajnoczi { 15975d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15985d6e96efSStefan Hajnoczi common, common); 15995d6e96efSStefan Hajnoczi 16005d6e96efSStefan Hajnoczi if (state->aio_context) { 1601507306ccSFam Zheng if (state->bs) { 1602507306ccSFam Zheng bdrv_drained_end(state->bs); 1603507306ccSFam Zheng } 16045d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16055d6e96efSStefan Hajnoczi } 16065d6e96efSStefan Hajnoczi } 16075d6e96efSStefan Hajnoczi 1608ba0c86a3SWenchao Xia /* external snapshot private data */ 1609ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 161050f43f0fSJohn Snow BlkActionState common; 16118802d1fdSJeff Cody BlockDriverState *old_bs; 16128802d1fdSJeff Cody BlockDriverState *new_bs; 16135d6e96efSStefan Hajnoczi AioContext *aio_context; 1614ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16158802d1fdSJeff Cody 161650f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16179b9877eeSWenchao Xia Error **errp) 16189b9877eeSWenchao Xia { 161943de7e2dSAlberto Garcia int flags = 0, ret; 162043de7e2dSAlberto Garcia QDict *options = NULL; 16219b9877eeSWenchao Xia Error *local_err = NULL; 162243de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1623e2a31e87SWenchao Xia const char *device; 16240901f67eSBenoît Canet const char *node_name; 162543de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 162643de7e2dSAlberto Garcia const char *snapshot_ref; 162743de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1628e2a31e87SWenchao Xia const char *new_image_file; 1629ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1630ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1631c8a83e85SKevin Wolf TransactionAction *action = common->action; 16329b9877eeSWenchao Xia 163343de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 163443de7e2dSAlberto Garcia * purpose but a different set of parameters */ 163543de7e2dSAlberto Garcia switch (action->type) { 163643de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 163743de7e2dSAlberto Garcia { 163843de7e2dSAlberto Garcia BlockdevSnapshot *s = action->u.blockdev_snapshot; 163943de7e2dSAlberto Garcia device = s->node; 164043de7e2dSAlberto Garcia node_name = s->node; 164143de7e2dSAlberto Garcia new_image_file = NULL; 164243de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1643e2a31e87SWenchao Xia } 164443de7e2dSAlberto Garcia break; 164543de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 164643de7e2dSAlberto Garcia { 164743de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 164843de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 164943de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 165043de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 165143de7e2dSAlberto Garcia snapshot_ref = NULL; 165243de7e2dSAlberto Garcia } 165343de7e2dSAlberto Garcia break; 165443de7e2dSAlberto Garcia default: 165543de7e2dSAlberto Garcia g_assert_not_reached(); 1656e2a31e87SWenchao Xia } 1657e2a31e87SWenchao Xia 1658e2a31e87SWenchao Xia /* start processing */ 165994d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 166094d16a64SJohn Snow return; 166194d16a64SJohn Snow } 166294d16a64SJohn Snow 166343de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 166443de7e2dSAlberto Garcia if (!state->old_bs) { 16659b9877eeSWenchao Xia return; 16669b9877eeSWenchao Xia } 16679b9877eeSWenchao Xia 16685d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16695d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16705d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1671da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16725d6e96efSStefan Hajnoczi 1673ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1674c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16759b9877eeSWenchao Xia return; 16769b9877eeSWenchao Xia } 16779b9877eeSWenchao Xia 16783718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16793718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16809b9877eeSWenchao Xia return; 16819b9877eeSWenchao Xia } 16829b9877eeSWenchao Xia 1683ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1684ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1685c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16869b9877eeSWenchao Xia return; 16879b9877eeSWenchao Xia } 16889b9877eeSWenchao Xia } 16899b9877eeSWenchao Xia 1690212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1691c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1692f6186f49SBenoît Canet return; 1693f6186f49SBenoît Canet } 1694f6186f49SBenoît Canet 169543de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 169643de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 169743de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 169843de7e2dSAlberto Garcia enum NewImageMode mode; 169943de7e2dSAlberto Garcia const char *snapshot_node_name = 170043de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 170143de7e2dSAlberto Garcia 170243de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 170343de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 170443de7e2dSAlberto Garcia return; 170543de7e2dSAlberto Garcia } 170643de7e2dSAlberto Garcia 170743de7e2dSAlberto Garcia if (snapshot_node_name && 170843de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 170943de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 171043de7e2dSAlberto Garcia return; 171143de7e2dSAlberto Garcia } 171243de7e2dSAlberto Garcia 1713ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17149b9877eeSWenchao Xia 17159b9877eeSWenchao Xia /* create new image w/backing file */ 171643de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17179b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 17189b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1719ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1720ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17219b9877eeSWenchao Xia NULL, -1, flags, &local_err, false); 172284d18f06SMarkus Armbruster if (local_err) { 17239b9877eeSWenchao Xia error_propagate(errp, local_err); 17249b9877eeSWenchao Xia return; 17259b9877eeSWenchao Xia } 17269b9877eeSWenchao Xia } 17279b9877eeSWenchao Xia 17280901f67eSBenoît Canet options = qdict_new(); 172943de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17300901f67eSBenoît Canet qdict_put(options, "node-name", 17310901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17320901f67eSBenoît Canet } 1733e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17340901f67eSBenoît Canet 173543de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 173643de7e2dSAlberto Garcia } 173743de7e2dSAlberto Garcia 1738f67503e5SMax Reitz assert(state->new_bs == NULL); 173943de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 174043de7e2dSAlberto Garcia flags, errp); 1741f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17429b9877eeSWenchao Xia if (ret != 0) { 174343de7e2dSAlberto Garcia return; 174443de7e2dSAlberto Garcia } 174543de7e2dSAlberto Garcia 174643de7e2dSAlberto Garcia if (state->new_bs->blk != NULL) { 174743de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 174843de7e2dSAlberto Garcia blk_name(state->new_bs->blk)); 174943de7e2dSAlberto Garcia return; 175043de7e2dSAlberto Garcia } 175143de7e2dSAlberto Garcia 175243de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 175343de7e2dSAlberto Garcia errp)) { 175443de7e2dSAlberto Garcia return; 175543de7e2dSAlberto Garcia } 175643de7e2dSAlberto Garcia 175743de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 175843de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 175908b24cfeSAlberto Garcia return; 176008b24cfeSAlberto Garcia } 176108b24cfeSAlberto Garcia 176208b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 176308b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17649b9877eeSWenchao Xia } 17659b9877eeSWenchao Xia } 17669b9877eeSWenchao Xia 176750f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17683b0047e8SWenchao Xia { 1769ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1770ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1771ba0c86a3SWenchao Xia 17725d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 17735d6e96efSStefan Hajnoczi 1774ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1775ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 17763b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17773b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17783b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1779dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17803b0047e8SWenchao Xia NULL); 17813b0047e8SWenchao Xia } 17823b0047e8SWenchao Xia 178350f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 178496b86bf7SWenchao Xia { 1785ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1786ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1787ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 17884f6fd349SFam Zheng bdrv_unref(state->new_bs); 178996b86bf7SWenchao Xia } 1790da763e83SFam Zheng } 1791da763e83SFam Zheng 179250f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1793da763e83SFam Zheng { 1794da763e83SFam Zheng ExternalSnapshotState *state = 1795da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17965d6e96efSStefan Hajnoczi if (state->aio_context) { 1797da763e83SFam Zheng bdrv_drained_end(state->old_bs); 17985d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 17995d6e96efSStefan Hajnoczi } 180096b86bf7SWenchao Xia } 180196b86bf7SWenchao Xia 18023037f364SStefan Hajnoczi typedef struct DriveBackupState { 180350f43f0fSJohn Snow BlkActionState common; 18043037f364SStefan Hajnoczi BlockDriverState *bs; 18055d6e96efSStefan Hajnoczi AioContext *aio_context; 18063037f364SStefan Hajnoczi BlockJob *job; 18073037f364SStefan Hajnoczi } DriveBackupState; 18083037f364SStefan Hajnoczi 180978f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 181078f51fdeSJohn Snow bool has_format, const char *format, 181178f51fdeSJohn Snow enum MirrorSyncMode sync, 181278f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 181378f51fdeSJohn Snow bool has_speed, int64_t speed, 181478f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 181578f51fdeSJohn Snow bool has_on_source_error, 181678f51fdeSJohn Snow BlockdevOnError on_source_error, 181778f51fdeSJohn Snow bool has_on_target_error, 181878f51fdeSJohn Snow BlockdevOnError on_target_error, 181978f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 182078f51fdeSJohn Snow 182150f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18223037f364SStefan Hajnoczi { 18233037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1824a0e8544cSFam Zheng BlockBackend *blk; 18253037f364SStefan Hajnoczi DriveBackup *backup; 18263037f364SStefan Hajnoczi Error *local_err = NULL; 18273037f364SStefan Hajnoczi 18286a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 18296a8f9661SEric Blake backup = common->action->u.drive_backup; 18303037f364SStefan Hajnoczi 1831a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1832a0e8544cSFam Zheng if (!blk) { 183375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 183475158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18355d6e96efSStefan Hajnoczi return; 18365d6e96efSStefan Hajnoczi } 18375d6e96efSStefan Hajnoczi 18381fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18391fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18401fdd4b7bSFam Zheng return; 18411fdd4b7bSFam Zheng } 18421fdd4b7bSFam Zheng 18435d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18445433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18455d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18461fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18471fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18485d6e96efSStefan Hajnoczi 184978f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18503037f364SStefan Hajnoczi backup->has_format, backup->format, 1851b53169eaSStefan Hajnoczi backup->sync, 18523037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18533037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1854d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18553037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18563037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 185794d16a64SJohn Snow common->block_job_txn, &local_err); 185884d18f06SMarkus Armbruster if (local_err) { 18593037f364SStefan Hajnoczi error_propagate(errp, local_err); 18603037f364SStefan Hajnoczi return; 18613037f364SStefan Hajnoczi } 18623037f364SStefan Hajnoczi 18633037f364SStefan Hajnoczi state->job = state->bs->job; 18643037f364SStefan Hajnoczi } 18653037f364SStefan Hajnoczi 186650f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18673037f364SStefan Hajnoczi { 18683037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18693037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 18703037f364SStefan Hajnoczi 18713037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 18723037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 18733037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 18743037f364SStefan Hajnoczi } 18753037f364SStefan Hajnoczi } 18763037f364SStefan Hajnoczi 187750f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18785d6e96efSStefan Hajnoczi { 18795d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18805d6e96efSStefan Hajnoczi 18815d6e96efSStefan Hajnoczi if (state->aio_context) { 18821fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 18835d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18845d6e96efSStefan Hajnoczi } 18855d6e96efSStefan Hajnoczi } 18865d6e96efSStefan Hajnoczi 1887bd8baecdSFam Zheng typedef struct BlockdevBackupState { 188850f43f0fSJohn Snow BlkActionState common; 1889bd8baecdSFam Zheng BlockDriverState *bs; 1890bd8baecdSFam Zheng BlockJob *job; 1891bd8baecdSFam Zheng AioContext *aio_context; 1892bd8baecdSFam Zheng } BlockdevBackupState; 1893bd8baecdSFam Zheng 189478f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 189578f51fdeSJohn Snow enum MirrorSyncMode sync, 189678f51fdeSJohn Snow bool has_speed, int64_t speed, 189778f51fdeSJohn Snow bool has_on_source_error, 189878f51fdeSJohn Snow BlockdevOnError on_source_error, 189978f51fdeSJohn Snow bool has_on_target_error, 190078f51fdeSJohn Snow BlockdevOnError on_target_error, 190178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 190278f51fdeSJohn Snow 190350f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1904bd8baecdSFam Zheng { 1905bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1906bd8baecdSFam Zheng BlockdevBackup *backup; 19075433c24fSMax Reitz BlockBackend *blk, *target; 1908bd8baecdSFam Zheng Error *local_err = NULL; 1909bd8baecdSFam Zheng 19106a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 19116a8f9661SEric Blake backup = common->action->u.blockdev_backup; 1912bd8baecdSFam Zheng 1913a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1914a0e8544cSFam Zheng if (!blk) { 19155b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1916bd8baecdSFam Zheng return; 1917bd8baecdSFam Zheng } 1918bd8baecdSFam Zheng 1919ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1920ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1921ff52bf36SFam Zheng return; 1922ff52bf36SFam Zheng } 1923ff52bf36SFam Zheng 19245433c24fSMax Reitz target = blk_by_name(backup->target); 19255433c24fSMax Reitz if (!target) { 19265b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1927bd8baecdSFam Zheng return; 1928bd8baecdSFam Zheng } 1929bd8baecdSFam Zheng 1930bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19315433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19325433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1933bd8baecdSFam Zheng state->aio_context = NULL; 1934bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1935bd8baecdSFam Zheng return; 1936bd8baecdSFam Zheng } 1937bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1938ff52bf36SFam Zheng state->bs = blk_bs(blk); 1939ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1940bd8baecdSFam Zheng 194178f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1942bd8baecdSFam Zheng backup->sync, 1943bd8baecdSFam Zheng backup->has_speed, backup->speed, 1944bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1945bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 194694d16a64SJohn Snow common->block_job_txn, &local_err); 1947bd8baecdSFam Zheng if (local_err) { 1948bd8baecdSFam Zheng error_propagate(errp, local_err); 1949bd8baecdSFam Zheng return; 1950bd8baecdSFam Zheng } 1951bd8baecdSFam Zheng 1952bd8baecdSFam Zheng state->job = state->bs->job; 1953bd8baecdSFam Zheng } 1954bd8baecdSFam Zheng 195550f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1956bd8baecdSFam Zheng { 1957bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1958bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1959bd8baecdSFam Zheng 1960bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1961bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1962bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1963bd8baecdSFam Zheng } 1964bd8baecdSFam Zheng } 1965bd8baecdSFam Zheng 196650f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1967bd8baecdSFam Zheng { 1968bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1969bd8baecdSFam Zheng 1970bd8baecdSFam Zheng if (state->aio_context) { 1971ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1972bd8baecdSFam Zheng aio_context_release(state->aio_context); 1973bd8baecdSFam Zheng } 1974bd8baecdSFam Zheng } 1975bd8baecdSFam Zheng 1976df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 197750f43f0fSJohn Snow BlkActionState common; 1978df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1979df9a681dSFam Zheng BlockDriverState *bs; 1980df9a681dSFam Zheng AioContext *aio_context; 1981df9a681dSFam Zheng HBitmap *backup; 1982df9a681dSFam Zheng bool prepared; 1983df9a681dSFam Zheng } BlockDirtyBitmapState; 1984df9a681dSFam Zheng 198550f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1986df9a681dSFam Zheng Error **errp) 1987df9a681dSFam Zheng { 1988df9a681dSFam Zheng Error *local_err = NULL; 1989df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1990df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1991df9a681dSFam Zheng common, common); 1992df9a681dSFam Zheng 199394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 199494d16a64SJohn Snow return; 199594d16a64SJohn Snow } 199694d16a64SJohn Snow 199750f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 1998df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1999df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2000df9a681dSFam Zheng action->has_granularity, action->granularity, 2001df9a681dSFam Zheng &local_err); 2002df9a681dSFam Zheng 2003df9a681dSFam Zheng if (!local_err) { 2004df9a681dSFam Zheng state->prepared = true; 2005df9a681dSFam Zheng } else { 2006df9a681dSFam Zheng error_propagate(errp, local_err); 2007df9a681dSFam Zheng } 2008df9a681dSFam Zheng } 2009df9a681dSFam Zheng 201050f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2011df9a681dSFam Zheng { 2012df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2013df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2014df9a681dSFam Zheng common, common); 2015df9a681dSFam Zheng 201650f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 2017df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2018df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2019df9a681dSFam Zheng */ 2020df9a681dSFam Zheng if (state->prepared) { 2021df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2022df9a681dSFam Zheng } 2023df9a681dSFam Zheng } 2024df9a681dSFam Zheng 202550f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2026df9a681dSFam Zheng Error **errp) 2027df9a681dSFam Zheng { 2028df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2029df9a681dSFam Zheng common, common); 2030df9a681dSFam Zheng BlockDirtyBitmap *action; 2031df9a681dSFam Zheng 203294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 203394d16a64SJohn Snow return; 203494d16a64SJohn Snow } 203594d16a64SJohn Snow 203650f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_clear; 2037df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2038df9a681dSFam Zheng action->name, 2039df9a681dSFam Zheng &state->bs, 2040df9a681dSFam Zheng &state->aio_context, 2041df9a681dSFam Zheng errp); 2042df9a681dSFam Zheng if (!state->bitmap) { 2043df9a681dSFam Zheng return; 2044df9a681dSFam Zheng } 2045df9a681dSFam Zheng 2046df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2047df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2048df9a681dSFam Zheng return; 2049df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2050df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2051df9a681dSFam Zheng return; 2052df9a681dSFam Zheng } 2053df9a681dSFam Zheng 2054df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2055df9a681dSFam Zheng /* AioContext is released in .clean() */ 2056df9a681dSFam Zheng } 2057df9a681dSFam Zheng 205850f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2059df9a681dSFam Zheng { 2060df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2061df9a681dSFam Zheng common, common); 2062df9a681dSFam Zheng 2063df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2064df9a681dSFam Zheng } 2065df9a681dSFam Zheng 206650f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2067df9a681dSFam Zheng { 2068df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2069df9a681dSFam Zheng common, common); 2070df9a681dSFam Zheng 2071df9a681dSFam Zheng hbitmap_free(state->backup); 2072df9a681dSFam Zheng } 2073df9a681dSFam Zheng 207450f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2075df9a681dSFam Zheng { 2076df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2077df9a681dSFam Zheng common, common); 2078df9a681dSFam Zheng 2079df9a681dSFam Zheng if (state->aio_context) { 2080df9a681dSFam Zheng aio_context_release(state->aio_context); 2081df9a681dSFam Zheng } 2082df9a681dSFam Zheng } 2083df9a681dSFam Zheng 208450f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 208578b18b78SStefan Hajnoczi { 208678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 208778b18b78SStefan Hajnoczi } 208878b18b78SStefan Hajnoczi 208950f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 209078b18b78SStefan Hajnoczi { 2091dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 209278b18b78SStefan Hajnoczi } 209378b18b78SStefan Hajnoczi 209450f43f0fSJohn Snow static const BlkActionOps actions[] = { 209543de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 209643de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 209743de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 209843de7e2dSAlberto Garcia .commit = external_snapshot_commit, 209943de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21004ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 210143de7e2dSAlberto Garcia }, 2102c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2103ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2104ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2105ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2106ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2107da763e83SFam Zheng .clean = external_snapshot_clean, 2108ba0c86a3SWenchao Xia }, 21093037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21103037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21113037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21123037f364SStefan Hajnoczi .abort = drive_backup_abort, 21135d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21143037f364SStefan Hajnoczi }, 2115bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2116bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2117bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2118bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2119bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2120bd8baecdSFam Zheng }, 212178b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 212250f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 212378b18b78SStefan Hajnoczi .prepare = abort_prepare, 212478b18b78SStefan Hajnoczi .commit = abort_commit, 212578b18b78SStefan Hajnoczi }, 2126bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2127bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2128bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2129bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21305d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2131bbe86010SWenchao Xia }, 2132df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2133df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2134df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2135df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2136df9a681dSFam Zheng }, 2137df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2138df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2139df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2140df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2141df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2142df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2143df9a681dSFam Zheng } 2144ba0c86a3SWenchao Xia }; 2145ba0c86a3SWenchao Xia 214694d16a64SJohn Snow /** 214794d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 214894d16a64SJohn Snow * that structure with desired defaults if they are unset. 214994d16a64SJohn Snow */ 215094d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 215194d16a64SJohn Snow TransactionProperties *props) 215294d16a64SJohn Snow { 215394d16a64SJohn Snow if (!props) { 215494d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 215594d16a64SJohn Snow } 215694d16a64SJohn Snow 215794d16a64SJohn Snow if (!props->has_completion_mode) { 215894d16a64SJohn Snow props->has_completion_mode = true; 215994d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 216094d16a64SJohn Snow } 216194d16a64SJohn Snow 216294d16a64SJohn Snow return props; 216394d16a64SJohn Snow } 216494d16a64SJohn Snow 21658802d1fdSJeff Cody /* 2166b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2167b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21688802d1fdSJeff Cody */ 216994d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 217094d16a64SJohn Snow bool has_props, 217194d16a64SJohn Snow struct TransactionProperties *props, 217294d16a64SJohn Snow Error **errp) 21738802d1fdSJeff Cody { 2174c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 217594d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 217650f43f0fSJohn Snow BlkActionState *state, *next; 217743e17041SLuiz Capitulino Error *local_err = NULL; 21788802d1fdSJeff Cody 217950f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 21808802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 21818802d1fdSJeff Cody 218294d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 218394d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 218494d16a64SJohn Snow */ 218594d16a64SJohn Snow props = get_transaction_properties(props); 218694d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 218794d16a64SJohn Snow block_job_txn = block_job_txn_new(); 218894d16a64SJohn Snow } 218994d16a64SJohn Snow 2190b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 21918802d1fdSJeff Cody bdrv_drain_all(); 21928802d1fdSJeff Cody 2193b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 21948802d1fdSJeff Cody while (NULL != dev_entry) { 2195c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 219650f43f0fSJohn Snow const BlkActionOps *ops; 219752e7c241SPaolo Bonzini 21988802d1fdSJeff Cody dev_info = dev_entry->value; 21998802d1fdSJeff Cody dev_entry = dev_entry->next; 22008802d1fdSJeff Cody 22016a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2202ba0c86a3SWenchao Xia 22036a8f9661SEric Blake ops = &actions[dev_info->type]; 2204aa3fe714SMax Reitz assert(ops->instance_size > 0); 2205aa3fe714SMax Reitz 2206ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2207ba5d6ab6SStefan Hajnoczi state->ops = ops; 2208ba5d6ab6SStefan Hajnoczi state->action = dev_info; 220994d16a64SJohn Snow state->block_job_txn = block_job_txn; 221094d16a64SJohn Snow state->txn_props = props; 2211ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22128802d1fdSJeff Cody 2213ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 221484d18f06SMarkus Armbruster if (local_err) { 22159b9877eeSWenchao Xia error_propagate(errp, local_err); 22169b9877eeSWenchao Xia goto delete_and_fail; 22179b9877eeSWenchao Xia } 221852e7c241SPaolo Bonzini } 22198802d1fdSJeff Cody 2220ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2221f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2222ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22238802d1fdSJeff Cody } 2224f9ea81e8SStefan Hajnoczi } 22258802d1fdSJeff Cody 22268802d1fdSJeff Cody /* success */ 22278802d1fdSJeff Cody goto exit; 22288802d1fdSJeff Cody 22298802d1fdSJeff Cody delete_and_fail: 2230b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2231ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2232ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2233ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2234ba0c86a3SWenchao Xia } 22358802d1fdSJeff Cody } 22368802d1fdSJeff Cody exit: 2237ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2238ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2239ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2240ba0c86a3SWenchao Xia } 2241ba5d6ab6SStefan Hajnoczi g_free(state); 22428802d1fdSJeff Cody } 224394d16a64SJohn Snow if (!has_props) { 224494d16a64SJohn Snow qapi_free_TransactionProperties(props); 224594d16a64SJohn Snow } 224694d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22478802d1fdSJeff Cody } 22488802d1fdSJeff Cody 2249c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2250666daa68SMarkus Armbruster { 225138f54bd1SMax Reitz Error *local_err = NULL; 2252666daa68SMarkus Armbruster 225338f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 225438f54bd1SMax Reitz if (local_err) { 225538f54bd1SMax Reitz error_propagate(errp, local_err); 2256c245b6a3SLuiz Capitulino return; 2257666daa68SMarkus Armbruster } 225892d48558SLuiz Capitulino 22596e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2260666daa68SMarkus Armbruster } 2261666daa68SMarkus Armbruster 226212d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 226312d3ba82SBenoît Canet bool has_node_name, const char *node_name, 226412d3ba82SBenoît Canet const char *password, Error **errp) 2265666daa68SMarkus Armbruster { 226612d3ba82SBenoît Canet Error *local_err = NULL; 2267666daa68SMarkus Armbruster BlockDriverState *bs; 2268e3442099SStefan Hajnoczi AioContext *aio_context; 2269666daa68SMarkus Armbruster 227012d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 227112d3ba82SBenoît Canet has_node_name ? node_name : NULL, 227212d3ba82SBenoît Canet &local_err); 227384d18f06SMarkus Armbruster if (local_err) { 227412d3ba82SBenoît Canet error_propagate(errp, local_err); 2275a4dea8a9SLuiz Capitulino return; 2276666daa68SMarkus Armbruster } 2277666daa68SMarkus Armbruster 2278e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2279e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2280e3442099SStefan Hajnoczi 22814d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2282666daa68SMarkus Armbruster 2283e3442099SStefan Hajnoczi aio_context_release(aio_context); 2284e3442099SStefan Hajnoczi } 2285e3442099SStefan Hajnoczi 22867d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 22877d8a9f71SMax Reitz Error **errp) 22887d8a9f71SMax Reitz { 22897d8a9f71SMax Reitz BlockBackend *blk; 22907d8a9f71SMax Reitz bool locked; 22917d8a9f71SMax Reitz 22927d8a9f71SMax Reitz if (!has_force) { 22937d8a9f71SMax Reitz force = false; 22947d8a9f71SMax Reitz } 22957d8a9f71SMax Reitz 22967d8a9f71SMax Reitz blk = blk_by_name(device); 22977d8a9f71SMax Reitz if (!blk) { 22987d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 22997d8a9f71SMax Reitz "Device '%s' not found", device); 23007d8a9f71SMax Reitz return; 23017d8a9f71SMax Reitz } 23027d8a9f71SMax Reitz 23037d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23047d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23057d8a9f71SMax Reitz return; 23067d8a9f71SMax Reitz } 23077d8a9f71SMax Reitz 23087d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23097d8a9f71SMax Reitz return; 23107d8a9f71SMax Reitz } 23117d8a9f71SMax Reitz 23127d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23137d8a9f71SMax Reitz if (locked) { 23147d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23157d8a9f71SMax Reitz } 23167d8a9f71SMax Reitz 23177d8a9f71SMax Reitz if (!locked || force) { 23187d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23197d8a9f71SMax Reitz } 23207d8a9f71SMax Reitz } 23217d8a9f71SMax Reitz 2322abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2323abaaf59dSMax Reitz { 2324abaaf59dSMax Reitz BlockBackend *blk; 2325abaaf59dSMax Reitz 2326abaaf59dSMax Reitz blk = blk_by_name(device); 2327abaaf59dSMax Reitz if (!blk) { 2328abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2329abaaf59dSMax Reitz "Device '%s' not found", device); 2330abaaf59dSMax Reitz return; 2331abaaf59dSMax Reitz } 2332abaaf59dSMax Reitz 2333abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2334abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2335abaaf59dSMax Reitz return; 2336abaaf59dSMax Reitz } 2337abaaf59dSMax Reitz 2338abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2339abaaf59dSMax Reitz return; 2340abaaf59dSMax Reitz } 2341abaaf59dSMax Reitz 2342abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2343abaaf59dSMax Reitz } 2344abaaf59dSMax Reitz 23456e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23462814f672SMax Reitz { 23472814f672SMax Reitz BlockBackend *blk; 23482814f672SMax Reitz BlockDriverState *bs; 23492814f672SMax Reitz AioContext *aio_context; 23502814f672SMax Reitz bool has_device; 23512814f672SMax Reitz 23522814f672SMax Reitz blk = blk_by_name(device); 23532814f672SMax Reitz if (!blk) { 23542814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23552814f672SMax Reitz "Device '%s' not found", device); 23562814f672SMax Reitz return; 23572814f672SMax Reitz } 23582814f672SMax Reitz 23592814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 23602814f672SMax Reitz has_device = blk_get_attached_dev(blk); 23612814f672SMax Reitz 23622814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 23632814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23642814f672SMax Reitz return; 23652814f672SMax Reitz } 23662814f672SMax Reitz 23672814f672SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 23682814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 23692814f672SMax Reitz return; 23702814f672SMax Reitz } 23712814f672SMax Reitz 23722814f672SMax Reitz bs = blk_bs(blk); 23732814f672SMax Reitz if (!bs) { 23742814f672SMax Reitz return; 23752814f672SMax Reitz } 23762814f672SMax Reitz 23772814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 23782814f672SMax Reitz aio_context_acquire(aio_context); 23792814f672SMax Reitz 23802814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 23812814f672SMax Reitz goto out; 23822814f672SMax Reitz } 23832814f672SMax Reitz 23842814f672SMax Reitz /* This follows the convention established by bdrv_make_anon() */ 23852814f672SMax Reitz if (bs->device_list.tqe_prev) { 23862814f672SMax Reitz QTAILQ_REMOVE(&bdrv_states, bs, device_list); 23872814f672SMax Reitz bs->device_list.tqe_prev = NULL; 23882814f672SMax Reitz } 23892814f672SMax Reitz 23902814f672SMax Reitz blk_remove_bs(blk); 23912814f672SMax Reitz 23922814f672SMax Reitz out: 23932814f672SMax Reitz aio_context_release(aio_context); 23942814f672SMax Reitz } 23952814f672SMax Reitz 2396d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2397d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2398d1299882SMax Reitz { 2399d1299882SMax Reitz BlockBackend *blk; 2400d1299882SMax Reitz bool has_device; 2401d1299882SMax Reitz 2402d1299882SMax Reitz blk = blk_by_name(device); 2403d1299882SMax Reitz if (!blk) { 2404d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2405d1299882SMax Reitz "Device '%s' not found", device); 2406d1299882SMax Reitz return; 2407d1299882SMax Reitz } 2408d1299882SMax Reitz 2409d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2410d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2411d1299882SMax Reitz 2412d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2413d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2414d1299882SMax Reitz return; 2415d1299882SMax Reitz } 2416d1299882SMax Reitz 2417d1299882SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 2418d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2419d1299882SMax Reitz return; 2420d1299882SMax Reitz } 2421d1299882SMax Reitz 2422d1299882SMax Reitz if (blk_bs(blk)) { 2423d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2424d1299882SMax Reitz return; 2425d1299882SMax Reitz } 2426d1299882SMax Reitz 2427d1299882SMax Reitz blk_insert_bs(blk, bs); 2428d1299882SMax Reitz 2429d1299882SMax Reitz QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 2430d1299882SMax Reitz } 2431d1299882SMax Reitz 24326e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2433d1299882SMax Reitz Error **errp) 2434d1299882SMax Reitz { 2435d1299882SMax Reitz BlockDriverState *bs; 2436d1299882SMax Reitz 2437d1299882SMax Reitz bs = bdrv_find_node(node_name); 2438d1299882SMax Reitz if (!bs) { 2439d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2440d1299882SMax Reitz return; 2441d1299882SMax Reitz } 2442d1299882SMax Reitz 2443d1299882SMax Reitz if (bs->blk) { 2444d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2445d1299882SMax Reitz blk_name(bs->blk)); 2446d1299882SMax Reitz return; 2447d1299882SMax Reitz } 2448d1299882SMax Reitz 2449d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2450d1299882SMax Reitz } 2451d1299882SMax Reitz 245224fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 245324fb4133SMax Reitz bool has_format, const char *format, 245439ff43d9SMax Reitz bool has_read_only, 245539ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 245624fb4133SMax Reitz Error **errp) 2457de2c6c05SMax Reitz { 2458de2c6c05SMax Reitz BlockBackend *blk; 2459de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2460de2c6c05SMax Reitz int bdrv_flags, ret; 2461de2c6c05SMax Reitz QDict *options = NULL; 2462de2c6c05SMax Reitz Error *err = NULL; 2463de2c6c05SMax Reitz 2464de2c6c05SMax Reitz blk = blk_by_name(device); 2465de2c6c05SMax Reitz if (!blk) { 2466de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2467de2c6c05SMax Reitz "Device '%s' not found", device); 2468de2c6c05SMax Reitz goto fail; 2469de2c6c05SMax Reitz } 2470de2c6c05SMax Reitz 2471de2c6c05SMax Reitz if (blk_bs(blk)) { 2472de2c6c05SMax Reitz blk_update_root_state(blk); 2473de2c6c05SMax Reitz } 2474de2c6c05SMax Reitz 2475de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2476de2c6c05SMax Reitz 247739ff43d9SMax Reitz if (!has_read_only) { 247839ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 247939ff43d9SMax Reitz } 248039ff43d9SMax Reitz 248139ff43d9SMax Reitz switch (read_only) { 248239ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 248339ff43d9SMax Reitz break; 248439ff43d9SMax Reitz 248539ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 248639ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 248739ff43d9SMax Reitz break; 248839ff43d9SMax Reitz 248939ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 249039ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 249139ff43d9SMax Reitz break; 249239ff43d9SMax Reitz 249339ff43d9SMax Reitz default: 249439ff43d9SMax Reitz abort(); 249539ff43d9SMax Reitz } 249639ff43d9SMax Reitz 249724fb4133SMax Reitz if (has_format) { 2498de2c6c05SMax Reitz options = qdict_new(); 2499de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2500de2c6c05SMax Reitz } 2501de2c6c05SMax Reitz 2502de2c6c05SMax Reitz assert(!medium_bs); 2503de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2504de2c6c05SMax Reitz if (ret < 0) { 2505de2c6c05SMax Reitz goto fail; 2506de2c6c05SMax Reitz } 2507de2c6c05SMax Reitz 2508de2c6c05SMax Reitz blk_apply_root_state(blk, medium_bs); 2509de2c6c05SMax Reitz 2510de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2511de2c6c05SMax Reitz if (err) { 2512de2c6c05SMax Reitz error_propagate(errp, err); 2513de2c6c05SMax Reitz goto fail; 2514de2c6c05SMax Reitz } 2515de2c6c05SMax Reitz 2516de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2517de2c6c05SMax Reitz if (err) { 2518de2c6c05SMax Reitz error_propagate(errp, err); 2519de2c6c05SMax Reitz goto fail; 2520de2c6c05SMax Reitz } 2521de2c6c05SMax Reitz 25226e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2523de2c6c05SMax Reitz if (err) { 2524de2c6c05SMax Reitz error_propagate(errp, err); 2525de2c6c05SMax Reitz goto fail; 2526de2c6c05SMax Reitz } 2527de2c6c05SMax Reitz 2528de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2529de2c6c05SMax Reitz if (err) { 2530de2c6c05SMax Reitz error_propagate(errp, err); 2531de2c6c05SMax Reitz goto fail; 2532de2c6c05SMax Reitz } 2533de2c6c05SMax Reitz 2534de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2535de2c6c05SMax Reitz 2536de2c6c05SMax Reitz fail: 2537de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2538de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2539de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2540de2c6c05SMax Reitz bdrv_unref(medium_bs); 2541de2c6c05SMax Reitz } 2542de2c6c05SMax Reitz 2543727f005eSZhi Yong Wu /* throttling disk I/O limits */ 254480047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 25453e9fab69SBenoît Canet int64_t bps_wr, 25463e9fab69SBenoît Canet int64_t iops, 25473e9fab69SBenoît Canet int64_t iops_rd, 25483e9fab69SBenoît Canet int64_t iops_wr, 25493e9fab69SBenoît Canet bool has_bps_max, 25503e9fab69SBenoît Canet int64_t bps_max, 25513e9fab69SBenoît Canet bool has_bps_rd_max, 25523e9fab69SBenoît Canet int64_t bps_rd_max, 25533e9fab69SBenoît Canet bool has_bps_wr_max, 25543e9fab69SBenoît Canet int64_t bps_wr_max, 25553e9fab69SBenoît Canet bool has_iops_max, 25563e9fab69SBenoît Canet int64_t iops_max, 25573e9fab69SBenoît Canet bool has_iops_rd_max, 25583e9fab69SBenoît Canet int64_t iops_rd_max, 25593e9fab69SBenoît Canet bool has_iops_wr_max, 25602024c1dfSBenoît Canet int64_t iops_wr_max, 25612024c1dfSBenoît Canet bool has_iops_size, 256276f4afb4SAlberto Garcia int64_t iops_size, 256376f4afb4SAlberto Garcia bool has_group, 256476f4afb4SAlberto Garcia const char *group, Error **errp) 2565727f005eSZhi Yong Wu { 2566cc0681c4SBenoît Canet ThrottleConfig cfg; 2567727f005eSZhi Yong Wu BlockDriverState *bs; 2568a0e8544cSFam Zheng BlockBackend *blk; 2569b15446fdSStefan Hajnoczi AioContext *aio_context; 2570727f005eSZhi Yong Wu 2571a0e8544cSFam Zheng blk = blk_by_name(device); 2572a0e8544cSFam Zheng if (!blk) { 257375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 257475158ebbSMarkus Armbruster "Device '%s' not found", device); 257580047da5SLuiz Capitulino return; 2576727f005eSZhi Yong Wu } 25775433c24fSMax Reitz 25785433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 25795433c24fSMax Reitz aio_context_acquire(aio_context); 25805433c24fSMax Reitz 2581a0e8544cSFam Zheng bs = blk_bs(blk); 25825433c24fSMax Reitz if (!bs) { 25835433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 25845433c24fSMax Reitz goto out; 25855433c24fSMax Reitz } 2586727f005eSZhi Yong Wu 2587cc0681c4SBenoît Canet memset(&cfg, 0, sizeof(cfg)); 2588cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2589cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2590cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2591727f005eSZhi Yong Wu 2592cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2593cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2594cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2595cc0681c4SBenoît Canet 25963e9fab69SBenoît Canet if (has_bps_max) { 25973e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 25983e9fab69SBenoît Canet } 25993e9fab69SBenoît Canet if (has_bps_rd_max) { 26003e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26013e9fab69SBenoît Canet } 26023e9fab69SBenoît Canet if (has_bps_wr_max) { 26033e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26043e9fab69SBenoît Canet } 26053e9fab69SBenoît Canet if (has_iops_max) { 26063e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26073e9fab69SBenoît Canet } 26083e9fab69SBenoît Canet if (has_iops_rd_max) { 26093e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26103e9fab69SBenoît Canet } 26113e9fab69SBenoît Canet if (has_iops_wr_max) { 26123e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26133e9fab69SBenoît Canet } 2614cc0681c4SBenoît Canet 26152024c1dfSBenoît Canet if (has_iops_size) { 26162024c1dfSBenoît Canet cfg.op_size = iops_size; 26172024c1dfSBenoît Canet } 2618cc0681c4SBenoît Canet 2619cc0681c4SBenoît Canet if (!check_throttle_config(&cfg, errp)) { 26205433c24fSMax Reitz goto out; 2621727f005eSZhi Yong Wu } 2622727f005eSZhi Yong Wu 262376f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 262476f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 262576f4afb4SAlberto Garcia * just update the throttling group. */ 2626a0d64a61SAlberto Garcia if (!bs->throttle_state) { 262776f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 262876f4afb4SAlberto Garcia } else if (has_group) { 262976f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2630727f005eSZhi Yong Wu } 263176f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2632cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 2633a0d64a61SAlberto Garcia } else if (bs->throttle_state) { 263476f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 263576f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2636727f005eSZhi Yong Wu } 2637b15446fdSStefan Hajnoczi 26385433c24fSMax Reitz out: 2639b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2640727f005eSZhi Yong Wu } 2641727f005eSZhi Yong Wu 2642341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2643341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2644341ebc2fSJohn Snow Error **errp) 2645341ebc2fSJohn Snow { 2646341ebc2fSJohn Snow AioContext *aio_context; 2647341ebc2fSJohn Snow BlockDriverState *bs; 2648341ebc2fSJohn Snow 2649341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2650341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2651341ebc2fSJohn Snow return; 2652341ebc2fSJohn Snow } 2653341ebc2fSJohn Snow 2654341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2655341ebc2fSJohn Snow if (!bs) { 2656341ebc2fSJohn Snow return; 2657341ebc2fSJohn Snow } 2658341ebc2fSJohn Snow 2659341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2660341ebc2fSJohn Snow aio_context_acquire(aio_context); 2661341ebc2fSJohn Snow 2662341ebc2fSJohn Snow if (has_granularity) { 2663341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2664341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2665341ebc2fSJohn Snow "and at least 512"); 2666341ebc2fSJohn Snow goto out; 2667341ebc2fSJohn Snow } 2668341ebc2fSJohn Snow } else { 2669341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2670341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2671341ebc2fSJohn Snow } 2672341ebc2fSJohn Snow 2673341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2674341ebc2fSJohn Snow 2675341ebc2fSJohn Snow out: 2676341ebc2fSJohn Snow aio_context_release(aio_context); 2677341ebc2fSJohn Snow } 2678341ebc2fSJohn Snow 2679341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2680341ebc2fSJohn Snow Error **errp) 2681341ebc2fSJohn Snow { 2682341ebc2fSJohn Snow AioContext *aio_context; 2683341ebc2fSJohn Snow BlockDriverState *bs; 2684341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2685341ebc2fSJohn Snow 2686341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2687341ebc2fSJohn Snow if (!bitmap || !bs) { 2688341ebc2fSJohn Snow return; 2689341ebc2fSJohn Snow } 2690341ebc2fSJohn Snow 26919bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 26929bd2b08fSJohn Snow error_setg(errp, 26939bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 26949bd2b08fSJohn Snow name); 26959bd2b08fSJohn Snow goto out; 26969bd2b08fSJohn Snow } 269720dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2698341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2699341ebc2fSJohn Snow 27009bd2b08fSJohn Snow out: 2701341ebc2fSJohn Snow aio_context_release(aio_context); 2702341ebc2fSJohn Snow } 2703341ebc2fSJohn Snow 2704e74e6b78SJohn Snow /** 2705e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2706e74e6b78SJohn Snow * immediately after a full backup operation. 2707e74e6b78SJohn Snow */ 2708e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2709e74e6b78SJohn Snow Error **errp) 2710e74e6b78SJohn Snow { 2711e74e6b78SJohn Snow AioContext *aio_context; 2712e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2713e74e6b78SJohn Snow BlockDriverState *bs; 2714e74e6b78SJohn Snow 2715e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2716e74e6b78SJohn Snow if (!bitmap || !bs) { 2717e74e6b78SJohn Snow return; 2718e74e6b78SJohn Snow } 2719e74e6b78SJohn Snow 2720e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2721e74e6b78SJohn Snow error_setg(errp, 2722e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2723e74e6b78SJohn Snow name); 2724e74e6b78SJohn Snow goto out; 2725e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2726e74e6b78SJohn Snow error_setg(errp, 2727e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2728e74e6b78SJohn Snow name); 2729e74e6b78SJohn Snow goto out; 2730e74e6b78SJohn Snow } 2731e74e6b78SJohn Snow 2732df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2733e74e6b78SJohn Snow 2734e74e6b78SJohn Snow out: 2735e74e6b78SJohn Snow aio_context_release(aio_context); 2736e74e6b78SJohn Snow } 2737e74e6b78SJohn Snow 2738072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 27399063f814SRyan Harper { 27409063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 27417e7d56d9SMarkus Armbruster BlockBackend *blk; 27429063f814SRyan Harper BlockDriverState *bs; 27438ad4202bSStefan Hajnoczi AioContext *aio_context; 27443718d8abSFam Zheng Error *local_err = NULL; 27459063f814SRyan Harper 27467e7d56d9SMarkus Armbruster blk = blk_by_name(id); 27477e7d56d9SMarkus Armbruster if (!blk) { 2748b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2749072ebe6bSMarkus Armbruster return; 27509063f814SRyan Harper } 27518ad4202bSStefan Hajnoczi 275226f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 275348f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 275448f364ddSMarkus Armbruster " is not supported"); 2755072ebe6bSMarkus Armbruster return; 275648f364ddSMarkus Armbruster } 275748f364ddSMarkus Armbruster 27585433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27598ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 27608ad4202bSStefan Hajnoczi 27615433c24fSMax Reitz bs = blk_bs(blk); 27625433c24fSMax Reitz if (bs) { 27633718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2764565f65d2SMarkus Armbruster error_report_err(local_err); 27658ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2766072ebe6bSMarkus Armbruster return; 27678591675fSMarcelo Tosatti } 27689063f814SRyan Harper 27699063f814SRyan Harper bdrv_close(bs); 27705433c24fSMax Reitz } 27719063f814SRyan Harper 2772fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 2773d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 2774d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 2775d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 2776d22b2f41SRyan Harper */ 2777a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 27783e5a50d6SMarkus Armbruster blk_hide_on_behalf_of_hmp_drive_del(blk); 2779293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2780373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2781293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2782d22b2f41SRyan Harper } else { 2783b9fe8a7aSMarkus Armbruster blk_unref(blk); 2784d22b2f41SRyan Harper } 27859063f814SRyan Harper 27868ad4202bSStefan Hajnoczi aio_context_release(aio_context); 27879063f814SRyan Harper } 27886d4a2b3aSChristoph Hellwig 27893b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 27903b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 27913b1dbd11SBenoît Canet int64_t size, Error **errp) 27926d4a2b3aSChristoph Hellwig { 27933b1dbd11SBenoît Canet Error *local_err = NULL; 27946d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2795927e0e76SStefan Hajnoczi AioContext *aio_context; 27968732901eSKevin Wolf int ret; 27976d4a2b3aSChristoph Hellwig 27983b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 27993b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 28003b1dbd11SBenoît Canet &local_err); 280184d18f06SMarkus Armbruster if (local_err) { 28023b1dbd11SBenoît Canet error_propagate(errp, local_err); 28033b1dbd11SBenoît Canet return; 28043b1dbd11SBenoît Canet } 28053b1dbd11SBenoît Canet 2806927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2807927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2808927e0e76SStefan Hajnoczi 28093b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2810c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2811927e0e76SStefan Hajnoczi goto out; 28126d4a2b3aSChristoph Hellwig } 28136d4a2b3aSChristoph Hellwig 28146d4a2b3aSChristoph Hellwig if (size < 0) { 2815c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2816927e0e76SStefan Hajnoczi goto out; 28176d4a2b3aSChristoph Hellwig } 28186d4a2b3aSChristoph Hellwig 28199c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2820c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2821927e0e76SStefan Hajnoczi goto out; 28229c75e168SJeff Cody } 28239c75e168SJeff Cody 282492b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 282592b7a08dSPeter Lieven bdrv_drain_all(); 282692b7a08dSPeter Lieven 28278732901eSKevin Wolf ret = bdrv_truncate(bs, size); 28288732901eSKevin Wolf switch (ret) { 2829939a1cc3SStefan Hajnoczi case 0: 2830939a1cc3SStefan Hajnoczi break; 2831939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2832c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2833939a1cc3SStefan Hajnoczi break; 2834939a1cc3SStefan Hajnoczi case -ENOTSUP: 2835c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2836939a1cc3SStefan Hajnoczi break; 2837939a1cc3SStefan Hajnoczi case -EACCES: 283881e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2839939a1cc3SStefan Hajnoczi break; 2840939a1cc3SStefan Hajnoczi case -EBUSY: 2841c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2842939a1cc3SStefan Hajnoczi break; 2843939a1cc3SStefan Hajnoczi default: 28448732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2845939a1cc3SStefan Hajnoczi break; 28466d4a2b3aSChristoph Hellwig } 2847927e0e76SStefan Hajnoczi 2848927e0e76SStefan Hajnoczi out: 2849927e0e76SStefan Hajnoczi aio_context_release(aio_context); 28506d4a2b3aSChristoph Hellwig } 285112bd451fSStefan Hajnoczi 28529abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 285312bd451fSStefan Hajnoczi { 2854723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2855723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2856723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2857723c5d93SStefan Hajnoczi */ 2858723c5d93SStefan Hajnoczi 285912bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2860bcada37bSWenchao Xia const char *msg = NULL; 286112bd451fSStefan Hajnoczi 28629abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 286312bd451fSStefan Hajnoczi 286412bd451fSStefan Hajnoczi assert(bs->job); 2865bcada37bSWenchao Xia 286612bd451fSStefan Hajnoczi if (ret < 0) { 2867bcada37bSWenchao Xia msg = strerror(-ret); 286812bd451fSStefan Hajnoczi } 286912bd451fSStefan Hajnoczi 2870370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2871bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2872370521a1SStefan Hajnoczi } else { 2873bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2874370521a1SStefan Hajnoczi } 287512bd451fSStefan Hajnoczi } 287612bd451fSStefan Hajnoczi 287713d8cc51SJeff Cody void qmp_block_stream(const char *device, 287813d8cc51SJeff Cody bool has_base, const char *base, 287913d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 288013d8cc51SJeff Cody bool has_speed, int64_t speed, 28811d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 28821d809098SPaolo Bonzini Error **errp) 288312bd451fSStefan Hajnoczi { 2884a0e8544cSFam Zheng BlockBackend *blk; 288512bd451fSStefan Hajnoczi BlockDriverState *bs; 2886c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2887f3e69bebSStefan Hajnoczi AioContext *aio_context; 2888fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 288913d8cc51SJeff Cody const char *base_name = NULL; 289012bd451fSStefan Hajnoczi 28911d809098SPaolo Bonzini if (!has_on_error) { 28921d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 28931d809098SPaolo Bonzini } 28941d809098SPaolo Bonzini 2895a0e8544cSFam Zheng blk = blk_by_name(device); 2896a0e8544cSFam Zheng if (!blk) { 289775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 289875158ebbSMarkus Armbruster "Device '%s' not found", device); 289912bd451fSStefan Hajnoczi return; 290012bd451fSStefan Hajnoczi } 290112bd451fSStefan Hajnoczi 29025433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2903f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2904f3e69bebSStefan Hajnoczi 29055433c24fSMax Reitz if (!blk_is_available(blk)) { 29065433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29075433c24fSMax Reitz goto out; 29085433c24fSMax Reitz } 29095433c24fSMax Reitz bs = blk_bs(blk); 29105433c24fSMax Reitz 2911628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2912f3e69bebSStefan Hajnoczi goto out; 2913628ff683SFam Zheng } 2914628ff683SFam Zheng 291513d8cc51SJeff Cody if (has_base) { 2916c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2917c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2918c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2919f3e69bebSStefan Hajnoczi goto out; 292012bd451fSStefan Hajnoczi } 2921f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 292213d8cc51SJeff Cody base_name = base; 2923c8c3080fSMarcelo Tosatti } 292412bd451fSStefan Hajnoczi 292513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 292613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 292713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 292813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 292913d8cc51SJeff Cody "entire chain"); 2930f3e69bebSStefan Hajnoczi goto out; 293113d8cc51SJeff Cody } 293213d8cc51SJeff Cody 293313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 293413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 293513d8cc51SJeff Cody 293613d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 29371d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 293884d18f06SMarkus Armbruster if (local_err) { 2939fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2940f3e69bebSStefan Hajnoczi goto out; 294112bd451fSStefan Hajnoczi } 294212bd451fSStefan Hajnoczi 294312bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 2944f3e69bebSStefan Hajnoczi 2945f3e69bebSStefan Hajnoczi out: 2946f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 294712bd451fSStefan Hajnoczi } 29482d47c6e9SStefan Hajnoczi 2949ed61fc10SJeff Cody void qmp_block_commit(const char *device, 29507676e2c5SJeff Cody bool has_base, const char *base, 29517676e2c5SJeff Cody bool has_top, const char *top, 295254e26900SJeff Cody bool has_backing_file, const char *backing_file, 2953ed61fc10SJeff Cody bool has_speed, int64_t speed, 2954ed61fc10SJeff Cody Error **errp) 2955ed61fc10SJeff Cody { 2956a0e8544cSFam Zheng BlockBackend *blk; 2957ed61fc10SJeff Cody BlockDriverState *bs; 2958ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 29599e85cd5cSStefan Hajnoczi AioContext *aio_context; 2960ed61fc10SJeff Cody Error *local_err = NULL; 2961ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 2962ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 2963ed61fc10SJeff Cody */ 296492aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2965ed61fc10SJeff Cody 296654504663SMax Reitz if (!has_speed) { 296754504663SMax Reitz speed = 0; 296854504663SMax Reitz } 296954504663SMax Reitz 29707676e2c5SJeff Cody /* Important Note: 29717676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 29727676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 29737676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 29747676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 2975a0e8544cSFam Zheng blk = blk_by_name(device); 2976a0e8544cSFam Zheng if (!blk) { 297775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 297875158ebbSMarkus Armbruster "Device '%s' not found", device); 2979ed61fc10SJeff Cody return; 2980ed61fc10SJeff Cody } 2981ed61fc10SJeff Cody 29825433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29839e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 29849e85cd5cSStefan Hajnoczi 29855433c24fSMax Reitz if (!blk_is_available(blk)) { 29865433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29875433c24fSMax Reitz goto out; 29885433c24fSMax Reitz } 29895433c24fSMax Reitz bs = blk_bs(blk); 29905433c24fSMax Reitz 2991bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 29929e85cd5cSStefan Hajnoczi goto out; 2993628ff683SFam Zheng } 2994628ff683SFam Zheng 2995ed61fc10SJeff Cody /* default top_bs is the active layer */ 2996ed61fc10SJeff Cody top_bs = bs; 2997ed61fc10SJeff Cody 29987676e2c5SJeff Cody if (has_top && top) { 2999ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3000ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3001ed61fc10SJeff Cody } 3002ed61fc10SJeff Cody } 3003ed61fc10SJeff Cody 3004ed61fc10SJeff Cody if (top_bs == NULL) { 3005ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30069e85cd5cSStefan Hajnoczi goto out; 3007ed61fc10SJeff Cody } 3008ed61fc10SJeff Cody 30099e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30109e85cd5cSStefan Hajnoczi 3011d5208c45SJeff Cody if (has_base && base) { 3012d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3013d5208c45SJeff Cody } else { 3014d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3015d5208c45SJeff Cody } 3016d5208c45SJeff Cody 3017d5208c45SJeff Cody if (base_bs == NULL) { 3018c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 30199e85cd5cSStefan Hajnoczi goto out; 3020d5208c45SJeff Cody } 3021d5208c45SJeff Cody 30229e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 30239e85cd5cSStefan Hajnoczi 3024bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3025bb00021dSFam Zheng goto out; 3026bb00021dSFam Zheng } 3027bb00021dSFam Zheng 30287676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 30297676e2c5SJeff Cody if (top_bs == base_bs) { 30307676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 30319e85cd5cSStefan Hajnoczi goto out; 30327676e2c5SJeff Cody } 30337676e2c5SJeff Cody 303420a63d2cSFam Zheng if (top_bs == bs) { 303554e26900SJeff Cody if (has_backing_file) { 303654e26900SJeff Cody error_setg(errp, "'backing-file' specified," 303754e26900SJeff Cody " but 'top' is the active layer"); 30389e85cd5cSStefan Hajnoczi goto out; 303954e26900SJeff Cody } 304020a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 304120a63d2cSFam Zheng bs, &local_err); 304220a63d2cSFam Zheng } else { 3043ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 304454e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 304520a63d2cSFam Zheng } 3046ed61fc10SJeff Cody if (local_err != NULL) { 3047ed61fc10SJeff Cody error_propagate(errp, local_err); 30489e85cd5cSStefan Hajnoczi goto out; 3049ed61fc10SJeff Cody } 30509e85cd5cSStefan Hajnoczi 30519e85cd5cSStefan Hajnoczi out: 30529e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3053ed61fc10SJeff Cody } 3054ed61fc10SJeff Cody 305578f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 305699a9addfSStefan Hajnoczi bool has_format, const char *format, 3057b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 305899a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 305999a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3060d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 306178f51fdeSJohn Snow bool has_on_source_error, 306278f51fdeSJohn Snow BlockdevOnError on_source_error, 306378f51fdeSJohn Snow bool has_on_target_error, 306478f51fdeSJohn Snow BlockdevOnError on_target_error, 306578f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 306699a9addfSStefan Hajnoczi { 3067a0e8544cSFam Zheng BlockBackend *blk; 306899a9addfSStefan Hajnoczi BlockDriverState *bs; 306999a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3070fc5d3f84SIan Main BlockDriverState *source = NULL; 3071d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3072761731b1SStefan Hajnoczi AioContext *aio_context; 3073e6641719SMax Reitz QDict *options = NULL; 307499a9addfSStefan Hajnoczi Error *local_err = NULL; 307599a9addfSStefan Hajnoczi int flags; 307699a9addfSStefan Hajnoczi int64_t size; 307799a9addfSStefan Hajnoczi int ret; 307899a9addfSStefan Hajnoczi 307999a9addfSStefan Hajnoczi if (!has_speed) { 308099a9addfSStefan Hajnoczi speed = 0; 308199a9addfSStefan Hajnoczi } 308299a9addfSStefan Hajnoczi if (!has_on_source_error) { 308399a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 308499a9addfSStefan Hajnoczi } 308599a9addfSStefan Hajnoczi if (!has_on_target_error) { 308699a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 308799a9addfSStefan Hajnoczi } 308899a9addfSStefan Hajnoczi if (!has_mode) { 308999a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 309099a9addfSStefan Hajnoczi } 309199a9addfSStefan Hajnoczi 3092a0e8544cSFam Zheng blk = blk_by_name(device); 3093a0e8544cSFam Zheng if (!blk) { 309475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 309575158ebbSMarkus Armbruster "Device '%s' not found", device); 309699a9addfSStefan Hajnoczi return; 309799a9addfSStefan Hajnoczi } 309899a9addfSStefan Hajnoczi 30995433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3100761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3101761731b1SStefan Hajnoczi 3102c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3103c29c1dd3SFam Zheng * do an early check redundantly. */ 31045433c24fSMax Reitz if (!blk_is_available(blk)) { 3105c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3106761731b1SStefan Hajnoczi goto out; 310799a9addfSStefan Hajnoczi } 31085433c24fSMax Reitz bs = blk_bs(blk); 310999a9addfSStefan Hajnoczi 311099a9addfSStefan Hajnoczi if (!has_format) { 311199a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 311299a9addfSStefan Hajnoczi } 311399a9addfSStefan Hajnoczi 3114c29c1dd3SFam Zheng /* Early check to avoid creating target */ 31153718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3116761731b1SStefan Hajnoczi goto out; 311799a9addfSStefan Hajnoczi } 311899a9addfSStefan Hajnoczi 311999a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 312099a9addfSStefan Hajnoczi 3121fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3122fc5d3f84SIan Main * on top of. */ 3123fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3124760e0063SKevin Wolf source = backing_bs(bs); 3125fc5d3f84SIan Main if (!source) { 3126fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3127fc5d3f84SIan Main } 3128fc5d3f84SIan Main } 3129fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3130fc5d3f84SIan Main source = bs; 3131fc5d3f84SIan Main } 3132fc5d3f84SIan Main 313399a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 313499a9addfSStefan Hajnoczi if (size < 0) { 313599a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3136761731b1SStefan Hajnoczi goto out; 313799a9addfSStefan Hajnoczi } 313899a9addfSStefan Hajnoczi 313999a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3140e6641719SMax Reitz assert(format); 3141fc5d3f84SIan Main if (source) { 3142fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3143fc5d3f84SIan Main source->drv->format_name, NULL, 3144fc5d3f84SIan Main size, flags, &local_err, false); 3145fc5d3f84SIan Main } else { 3146fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3147fc5d3f84SIan Main size, flags, &local_err, false); 3148fc5d3f84SIan Main } 314999a9addfSStefan Hajnoczi } 315099a9addfSStefan Hajnoczi 315184d18f06SMarkus Armbruster if (local_err) { 315299a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3153761731b1SStefan Hajnoczi goto out; 315499a9addfSStefan Hajnoczi } 315599a9addfSStefan Hajnoczi 3156e6641719SMax Reitz if (format) { 3157e6641719SMax Reitz options = qdict_new(); 3158e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3159e6641719SMax Reitz } 3160e6641719SMax Reitz 3161f67503e5SMax Reitz target_bs = NULL; 31626ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 316399a9addfSStefan Hajnoczi if (ret < 0) { 316434b5d2c6SMax Reitz error_propagate(errp, local_err); 3165761731b1SStefan Hajnoczi goto out; 316699a9addfSStefan Hajnoczi } 316799a9addfSStefan Hajnoczi 3168761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3169761731b1SStefan Hajnoczi 3170d58d8453SJohn Snow if (has_bitmap) { 3171d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3172d58d8453SJohn Snow if (!bmap) { 3173d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 31740702d3d8SMax Reitz bdrv_unref(target_bs); 3175d58d8453SJohn Snow goto out; 3176d58d8453SJohn Snow } 3177d58d8453SJohn Snow } 3178d58d8453SJohn Snow 3179d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3180d58d8453SJohn Snow on_source_error, on_target_error, 318178f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 318299a9addfSStefan Hajnoczi if (local_err != NULL) { 31834f6fd349SFam Zheng bdrv_unref(target_bs); 318499a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3185761731b1SStefan Hajnoczi goto out; 318699a9addfSStefan Hajnoczi } 3187761731b1SStefan Hajnoczi 3188761731b1SStefan Hajnoczi out: 3189761731b1SStefan Hajnoczi aio_context_release(aio_context); 319099a9addfSStefan Hajnoczi } 319199a9addfSStefan Hajnoczi 319278f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 319378f51fdeSJohn Snow bool has_format, const char *format, 319478f51fdeSJohn Snow enum MirrorSyncMode sync, 319578f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 319678f51fdeSJohn Snow bool has_speed, int64_t speed, 319778f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 319878f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 319978f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 320078f51fdeSJohn Snow Error **errp) 320178f51fdeSJohn Snow { 320278f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 320378f51fdeSJohn Snow has_mode, mode, has_speed, speed, 320478f51fdeSJohn Snow has_bitmap, bitmap, 320578f51fdeSJohn Snow has_on_source_error, on_source_error, 320678f51fdeSJohn Snow has_on_target_error, on_target_error, 320778f51fdeSJohn Snow NULL, errp); 320878f51fdeSJohn Snow } 320978f51fdeSJohn Snow 3210c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3211c13163fbSBenoît Canet { 3212d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3213c13163fbSBenoît Canet } 3214c13163fbSBenoît Canet 321578f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3216c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3217c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3218c29c1dd3SFam Zheng bool has_on_source_error, 3219c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3220c29c1dd3SFam Zheng bool has_on_target_error, 3221c29c1dd3SFam Zheng BlockdevOnError on_target_error, 322278f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3223c29c1dd3SFam Zheng { 32245433c24fSMax Reitz BlockBackend *blk, *target_blk; 3225c29c1dd3SFam Zheng BlockDriverState *bs; 3226c29c1dd3SFam Zheng BlockDriverState *target_bs; 3227c29c1dd3SFam Zheng Error *local_err = NULL; 3228c29c1dd3SFam Zheng AioContext *aio_context; 3229c29c1dd3SFam Zheng 3230c29c1dd3SFam Zheng if (!has_speed) { 3231c29c1dd3SFam Zheng speed = 0; 3232c29c1dd3SFam Zheng } 3233c29c1dd3SFam Zheng if (!has_on_source_error) { 3234c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3235c29c1dd3SFam Zheng } 3236c29c1dd3SFam Zheng if (!has_on_target_error) { 3237c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3238c29c1dd3SFam Zheng } 3239c29c1dd3SFam Zheng 3240a0e8544cSFam Zheng blk = blk_by_name(device); 3241a0e8544cSFam Zheng if (!blk) { 32425b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3243c29c1dd3SFam Zheng return; 3244c29c1dd3SFam Zheng } 3245c29c1dd3SFam Zheng 32465433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3247c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3248c29c1dd3SFam Zheng 32495433c24fSMax Reitz if (!blk_is_available(blk)) { 32505433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 32515433c24fSMax Reitz goto out; 32525433c24fSMax Reitz } 32535433c24fSMax Reitz bs = blk_bs(blk); 32545433c24fSMax Reitz 32555433c24fSMax Reitz target_blk = blk_by_name(target); 32565433c24fSMax Reitz if (!target_blk) { 32575b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3258c29c1dd3SFam Zheng goto out; 3259c29c1dd3SFam Zheng } 32605433c24fSMax Reitz 32615433c24fSMax Reitz if (!blk_is_available(target_blk)) { 32625433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 32635433c24fSMax Reitz goto out; 32645433c24fSMax Reitz } 32655433c24fSMax Reitz target_bs = blk_bs(target_blk); 3266c29c1dd3SFam Zheng 3267c29c1dd3SFam Zheng bdrv_ref(target_bs); 3268c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3269d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 327078f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3271c29c1dd3SFam Zheng if (local_err != NULL) { 3272c29c1dd3SFam Zheng bdrv_unref(target_bs); 3273c29c1dd3SFam Zheng error_propagate(errp, local_err); 3274c29c1dd3SFam Zheng } 3275c29c1dd3SFam Zheng out: 3276c29c1dd3SFam Zheng aio_context_release(aio_context); 3277c29c1dd3SFam Zheng } 3278c29c1dd3SFam Zheng 327978f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 328078f51fdeSJohn Snow enum MirrorSyncMode sync, 328178f51fdeSJohn Snow bool has_speed, int64_t speed, 328278f51fdeSJohn Snow bool has_on_source_error, 328378f51fdeSJohn Snow BlockdevOnError on_source_error, 328478f51fdeSJohn Snow bool has_on_target_error, 328578f51fdeSJohn Snow BlockdevOnError on_target_error, 328678f51fdeSJohn Snow Error **errp) 328778f51fdeSJohn Snow { 328878f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 328978f51fdeSJohn Snow has_on_source_error, on_source_error, 329078f51fdeSJohn Snow has_on_target_error, on_target_error, 329178f51fdeSJohn Snow NULL, errp); 329278f51fdeSJohn Snow } 329378f51fdeSJohn Snow 32944193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 32954193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 32964193cdd7SFam Zheng **/ 32974193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 32984193cdd7SFam Zheng BlockDriverState *target, 329909158f00SBenoît Canet bool has_replaces, const char *replaces, 3300d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3301b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3302eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 330308e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 33044193cdd7SFam Zheng bool has_on_source_error, 33054193cdd7SFam Zheng BlockdevOnError on_source_error, 33064193cdd7SFam Zheng bool has_on_target_error, 33074193cdd7SFam Zheng BlockdevOnError on_target_error, 33080fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3309b952b558SPaolo Bonzini Error **errp) 3310d9b902dbSPaolo Bonzini { 3311d9b902dbSPaolo Bonzini 3312d9b902dbSPaolo Bonzini if (!has_speed) { 3313d9b902dbSPaolo Bonzini speed = 0; 3314d9b902dbSPaolo Bonzini } 3315b952b558SPaolo Bonzini if (!has_on_source_error) { 3316b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3317b952b558SPaolo Bonzini } 3318b952b558SPaolo Bonzini if (!has_on_target_error) { 3319b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3320b952b558SPaolo Bonzini } 3321eee13dfeSPaolo Bonzini if (!has_granularity) { 3322eee13dfeSPaolo Bonzini granularity = 0; 3323eee13dfeSPaolo Bonzini } 332408e4ed6cSPaolo Bonzini if (!has_buf_size) { 332548ac0a4dSWen Congyang buf_size = 0; 332608e4ed6cSPaolo Bonzini } 33270fc9f8eaSFam Zheng if (!has_unmap) { 33280fc9f8eaSFam Zheng unmap = true; 33290fc9f8eaSFam Zheng } 333008e4ed6cSPaolo Bonzini 3331eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3332c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 33333cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3334eee13dfeSPaolo Bonzini return; 3335eee13dfeSPaolo Bonzini } 3336eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3337c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3338c6bd8c70SMarkus Armbruster "power of 2"); 3339eee13dfeSPaolo Bonzini return; 3340eee13dfeSPaolo Bonzini } 3341d9b902dbSPaolo Bonzini 33424193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 33434193cdd7SFam Zheng return; 33444193cdd7SFam Zheng } 3345*e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3346*e40e5027SFam Zheng return; 3347*e40e5027SFam Zheng } 33484193cdd7SFam Zheng 33494193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 33504193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 33514193cdd7SFam Zheng } 33524193cdd7SFam Zheng 33534193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 33544193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 33554193cdd7SFam Zheng */ 33564193cdd7SFam Zheng mirror_start(bs, target, 33574193cdd7SFam Zheng has_replaces ? replaces : NULL, 33584193cdd7SFam Zheng speed, granularity, buf_size, sync, 33594193cdd7SFam Zheng on_source_error, on_target_error, unmap, 33604193cdd7SFam Zheng block_job_cb, bs, errp); 33614193cdd7SFam Zheng } 33624193cdd7SFam Zheng 33634193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 33644193cdd7SFam Zheng bool has_format, const char *format, 33654193cdd7SFam Zheng bool has_node_name, const char *node_name, 33664193cdd7SFam Zheng bool has_replaces, const char *replaces, 33674193cdd7SFam Zheng enum MirrorSyncMode sync, 33684193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 33694193cdd7SFam Zheng bool has_speed, int64_t speed, 33704193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 33714193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 33724193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 33734193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 33744193cdd7SFam Zheng bool has_unmap, bool unmap, 33754193cdd7SFam Zheng Error **errp) 33764193cdd7SFam Zheng { 33774193cdd7SFam Zheng BlockDriverState *bs; 33784193cdd7SFam Zheng BlockBackend *blk; 33794193cdd7SFam Zheng BlockDriverState *source, *target_bs; 33804193cdd7SFam Zheng AioContext *aio_context; 33814193cdd7SFam Zheng Error *local_err = NULL; 33824193cdd7SFam Zheng QDict *options = NULL; 33834193cdd7SFam Zheng int flags; 33844193cdd7SFam Zheng int64_t size; 33854193cdd7SFam Zheng int ret; 33864193cdd7SFam Zheng 3387a0e8544cSFam Zheng blk = blk_by_name(device); 3388a0e8544cSFam Zheng if (!blk) { 338975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 339075158ebbSMarkus Armbruster "Device '%s' not found", device); 3391d9b902dbSPaolo Bonzini return; 3392d9b902dbSPaolo Bonzini } 3393d9b902dbSPaolo Bonzini 33945433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 33955a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 33965a7e7a0bSStefan Hajnoczi 33975433c24fSMax Reitz if (!blk_is_available(blk)) { 3398c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 33995a7e7a0bSStefan Hajnoczi goto out; 3400d9b902dbSPaolo Bonzini } 34015433c24fSMax Reitz bs = blk_bs(blk); 34024193cdd7SFam Zheng if (!has_mode) { 34034193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 34044193cdd7SFam Zheng } 3405d9b902dbSPaolo Bonzini 3406d9b902dbSPaolo Bonzini if (!has_format) { 3407d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3408d9b902dbSPaolo Bonzini } 3409d9b902dbSPaolo Bonzini 3410d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3411760e0063SKevin Wolf source = backing_bs(bs); 3412d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3413d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3414d9b902dbSPaolo Bonzini } 3415117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3416117e0c82SMax Reitz source = bs; 3417117e0c82SMax Reitz } 3418d9b902dbSPaolo Bonzini 3419ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3420ac3c5d83SStefan Hajnoczi if (size < 0) { 3421ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 34225a7e7a0bSStefan Hajnoczi goto out; 3423ac3c5d83SStefan Hajnoczi } 3424ac3c5d83SStefan Hajnoczi 342509158f00SBenoît Canet if (has_replaces) { 342609158f00SBenoît Canet BlockDriverState *to_replace_bs; 34275a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 34285a7e7a0bSStefan Hajnoczi int64_t replace_size; 342909158f00SBenoît Canet 343009158f00SBenoît Canet if (!has_node_name) { 343109158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 343209158f00SBenoît Canet " named node of the graph"); 34335a7e7a0bSStefan Hajnoczi goto out; 343409158f00SBenoît Canet } 343509158f00SBenoît Canet 3436e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 343709158f00SBenoît Canet 343809158f00SBenoît Canet if (!to_replace_bs) { 343909158f00SBenoît Canet error_propagate(errp, local_err); 34405a7e7a0bSStefan Hajnoczi goto out; 344109158f00SBenoît Canet } 344209158f00SBenoît Canet 34435a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 34445a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 34455a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 34465a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 34475a7e7a0bSStefan Hajnoczi 34485a7e7a0bSStefan Hajnoczi if (size != replace_size) { 344909158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 345009158f00SBenoît Canet "different size"); 34515a7e7a0bSStefan Hajnoczi goto out; 345209158f00SBenoît Canet } 345309158f00SBenoît Canet } 345409158f00SBenoît Canet 345514526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 345614526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 345714526864SMax Reitz { 3458d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3459e6641719SMax Reitz assert(format); 3460cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3461f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3462d9b902dbSPaolo Bonzini } else { 3463d9b902dbSPaolo Bonzini switch (mode) { 3464d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3465d9b902dbSPaolo Bonzini break; 3466d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3467d9b902dbSPaolo Bonzini /* create new image with backing file */ 3468cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3469d9b902dbSPaolo Bonzini source->filename, 3470d9b902dbSPaolo Bonzini source->drv->format_name, 3471f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3472d9b902dbSPaolo Bonzini break; 3473d9b902dbSPaolo Bonzini default: 3474d9b902dbSPaolo Bonzini abort(); 3475d9b902dbSPaolo Bonzini } 3476d9b902dbSPaolo Bonzini } 3477d9b902dbSPaolo Bonzini 347884d18f06SMarkus Armbruster if (local_err) { 3479cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 34805a7e7a0bSStefan Hajnoczi goto out; 3481d9b902dbSPaolo Bonzini } 3482d9b902dbSPaolo Bonzini 34834c828dc6SBenoît Canet options = qdict_new(); 3484e6641719SMax Reitz if (has_node_name) { 34854c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 34864c828dc6SBenoît Canet } 3487e6641719SMax Reitz if (format) { 3488e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3489e6641719SMax Reitz } 34904c828dc6SBenoît Canet 3491b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3492b812f671SPaolo Bonzini * file. 3493b812f671SPaolo Bonzini */ 3494f67503e5SMax Reitz target_bs = NULL; 34954c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 34966ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3497d9b902dbSPaolo Bonzini if (ret < 0) { 349834b5d2c6SMax Reitz error_propagate(errp, local_err); 34995a7e7a0bSStefan Hajnoczi goto out; 3500d9b902dbSPaolo Bonzini } 3501d9b902dbSPaolo Bonzini 35025a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 35035a7e7a0bSStefan Hajnoczi 35044193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 35054193cdd7SFam Zheng has_replaces, replaces, sync, 35064193cdd7SFam Zheng has_speed, speed, 35074193cdd7SFam Zheng has_granularity, granularity, 35084193cdd7SFam Zheng has_buf_size, buf_size, 35094193cdd7SFam Zheng has_on_source_error, on_source_error, 35104193cdd7SFam Zheng has_on_target_error, on_target_error, 35114193cdd7SFam Zheng has_unmap, unmap, 35124193cdd7SFam Zheng &local_err); 35134193cdd7SFam Zheng if (local_err) { 3514d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 35154193cdd7SFam Zheng bdrv_unref(target_bs); 3516d9b902dbSPaolo Bonzini } 35175a7e7a0bSStefan Hajnoczi out: 35185a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3519d9b902dbSPaolo Bonzini } 3520d9b902dbSPaolo Bonzini 35213d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 352224d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 352324d6bffeSMarkus Armbruster Error **errp) 35242d47c6e9SStefan Hajnoczi { 3525a0e8544cSFam Zheng BlockBackend *blk; 35262d47c6e9SStefan Hajnoczi BlockDriverState *bs; 35272d47c6e9SStefan Hajnoczi 35285433c24fSMax Reitz *aio_context = NULL; 35295433c24fSMax Reitz 3530a0e8544cSFam Zheng blk = blk_by_name(device); 3531a0e8544cSFam Zheng if (!blk) { 35323d948cdfSStefan Hajnoczi goto notfound; 35332d47c6e9SStefan Hajnoczi } 35343d948cdfSStefan Hajnoczi 35355433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 35363d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 35373d948cdfSStefan Hajnoczi 35385433c24fSMax Reitz if (!blk_is_available(blk)) { 35395433c24fSMax Reitz goto notfound; 35405433c24fSMax Reitz } 35415433c24fSMax Reitz bs = blk_bs(blk); 35425433c24fSMax Reitz 35433d948cdfSStefan Hajnoczi if (!bs->job) { 35443d948cdfSStefan Hajnoczi goto notfound; 35453d948cdfSStefan Hajnoczi } 35463d948cdfSStefan Hajnoczi 35472d47c6e9SStefan Hajnoczi return bs->job; 35483d948cdfSStefan Hajnoczi 35493d948cdfSStefan Hajnoczi notfound: 35502e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 35512e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 35525433c24fSMax Reitz if (*aio_context) { 35535433c24fSMax Reitz aio_context_release(*aio_context); 35543d948cdfSStefan Hajnoczi *aio_context = NULL; 35555433c24fSMax Reitz } 35563d948cdfSStefan Hajnoczi return NULL; 35572d47c6e9SStefan Hajnoczi } 35582d47c6e9SStefan Hajnoczi 3559882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 35602d47c6e9SStefan Hajnoczi { 35613d948cdfSStefan Hajnoczi AioContext *aio_context; 356224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 35632d47c6e9SStefan Hajnoczi 35642d47c6e9SStefan Hajnoczi if (!job) { 35652d47c6e9SStefan Hajnoczi return; 35662d47c6e9SStefan Hajnoczi } 35672d47c6e9SStefan Hajnoczi 3568882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 35693d948cdfSStefan Hajnoczi aio_context_release(aio_context); 35702d47c6e9SStefan Hajnoczi } 3571370521a1SStefan Hajnoczi 35726e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 35736e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 35746e37fb81SPaolo Bonzini { 35753d948cdfSStefan Hajnoczi AioContext *aio_context; 357624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 35776e37fb81SPaolo Bonzini 35786e37fb81SPaolo Bonzini if (!job) { 35796e37fb81SPaolo Bonzini return; 35806e37fb81SPaolo Bonzini } 35813d948cdfSStefan Hajnoczi 35823d948cdfSStefan Hajnoczi if (!has_force) { 35833d948cdfSStefan Hajnoczi force = false; 35843d948cdfSStefan Hajnoczi } 35853d948cdfSStefan Hajnoczi 3586751ebd76SFam Zheng if (job->user_paused && !force) { 3587f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3588f231b88dSCole Robinson device); 35893d948cdfSStefan Hajnoczi goto out; 35906e37fb81SPaolo Bonzini } 35916e37fb81SPaolo Bonzini 35926e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 35936e37fb81SPaolo Bonzini block_job_cancel(job); 35943d948cdfSStefan Hajnoczi out: 35953d948cdfSStefan Hajnoczi aio_context_release(aio_context); 35966e37fb81SPaolo Bonzini } 35976e37fb81SPaolo Bonzini 35986e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3599370521a1SStefan Hajnoczi { 36003d948cdfSStefan Hajnoczi AioContext *aio_context; 360124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3602370521a1SStefan Hajnoczi 3603751ebd76SFam Zheng if (!job || job->user_paused) { 3604370521a1SStefan Hajnoczi return; 3605370521a1SStefan Hajnoczi } 36066e37fb81SPaolo Bonzini 3607751ebd76SFam Zheng job->user_paused = true; 36086e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 36096e37fb81SPaolo Bonzini block_job_pause(job); 36103d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36116e37fb81SPaolo Bonzini } 36126e37fb81SPaolo Bonzini 36136e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 36146e37fb81SPaolo Bonzini { 36153d948cdfSStefan Hajnoczi AioContext *aio_context; 361624d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36176e37fb81SPaolo Bonzini 3618751ebd76SFam Zheng if (!job || !job->user_paused) { 36198acc72a4SPaolo Bonzini return; 36208acc72a4SPaolo Bonzini } 3621370521a1SStefan Hajnoczi 3622751ebd76SFam Zheng job->user_paused = false; 36236e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 36246e37fb81SPaolo Bonzini block_job_resume(job); 36253d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3626370521a1SStefan Hajnoczi } 3627fb5458cdSStefan Hajnoczi 3628aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3629aeae883bSPaolo Bonzini { 36303d948cdfSStefan Hajnoczi AioContext *aio_context; 363124d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3632aeae883bSPaolo Bonzini 3633aeae883bSPaolo Bonzini if (!job) { 3634aeae883bSPaolo Bonzini return; 3635aeae883bSPaolo Bonzini } 3636aeae883bSPaolo Bonzini 3637aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3638aeae883bSPaolo Bonzini block_job_complete(job, errp); 36393d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3640aeae883bSPaolo Bonzini } 3641aeae883bSPaolo Bonzini 3642fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3643fa40e656SJeff Cody const char *image_node_name, 3644fa40e656SJeff Cody const char *backing_file, 3645fa40e656SJeff Cody Error **errp) 3646fa40e656SJeff Cody { 3647a0e8544cSFam Zheng BlockBackend *blk; 3648fa40e656SJeff Cody BlockDriverState *bs = NULL; 3649729962f6SStefan Hajnoczi AioContext *aio_context; 3650fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3651fa40e656SJeff Cody Error *local_err = NULL; 3652fa40e656SJeff Cody bool ro; 3653fa40e656SJeff Cody int open_flags; 3654fa40e656SJeff Cody int ret; 3655fa40e656SJeff Cody 3656a0e8544cSFam Zheng blk = blk_by_name(device); 3657a0e8544cSFam Zheng if (!blk) { 365875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 365975158ebbSMarkus Armbruster "Device '%s' not found", device); 3660fa40e656SJeff Cody return; 3661fa40e656SJeff Cody } 3662fa40e656SJeff Cody 36635433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3664729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3665729962f6SStefan Hajnoczi 36665433c24fSMax Reitz if (!blk_is_available(blk)) { 36675433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 36685433c24fSMax Reitz goto out; 36695433c24fSMax Reitz } 36705433c24fSMax Reitz bs = blk_bs(blk); 36715433c24fSMax Reitz 3672fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3673fa40e656SJeff Cody if (local_err) { 3674fa40e656SJeff Cody error_propagate(errp, local_err); 3675729962f6SStefan Hajnoczi goto out; 3676fa40e656SJeff Cody } 3677fa40e656SJeff Cody 3678fa40e656SJeff Cody if (!image_bs) { 3679fa40e656SJeff Cody error_setg(errp, "image file not found"); 3680729962f6SStefan Hajnoczi goto out; 3681fa40e656SJeff Cody } 3682fa40e656SJeff Cody 3683fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3684fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3685fa40e656SJeff Cody "without a backing file"); 3686729962f6SStefan Hajnoczi goto out; 3687fa40e656SJeff Cody } 3688fa40e656SJeff Cody 3689fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3690fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3691fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3692729962f6SStefan Hajnoczi goto out; 3693fa40e656SJeff Cody } 3694fa40e656SJeff Cody 3695fa40e656SJeff Cody /* final sanity check */ 3696fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3697fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3698fa40e656SJeff Cody device); 3699729962f6SStefan Hajnoczi goto out; 3700fa40e656SJeff Cody } 3701fa40e656SJeff Cody 3702fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3703fa40e656SJeff Cody open_flags = image_bs->open_flags; 3704fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3705fa40e656SJeff Cody 3706fa40e656SJeff Cody if (ro) { 3707fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3708fa40e656SJeff Cody if (local_err) { 3709fa40e656SJeff Cody error_propagate(errp, local_err); 3710729962f6SStefan Hajnoczi goto out; 3711fa40e656SJeff Cody } 3712fa40e656SJeff Cody } 3713fa40e656SJeff Cody 3714fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3715fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3716fa40e656SJeff Cody 3717fa40e656SJeff Cody if (ret < 0) { 3718fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3719fa40e656SJeff Cody backing_file); 3720fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3721fa40e656SJeff Cody * appropriate */ 3722fa40e656SJeff Cody } 3723fa40e656SJeff Cody 3724fa40e656SJeff Cody if (ro) { 3725fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3726fa40e656SJeff Cody if (local_err) { 3727fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3728fa40e656SJeff Cody } 3729fa40e656SJeff Cody } 3730729962f6SStefan Hajnoczi 3731729962f6SStefan Hajnoczi out: 3732729962f6SStefan Hajnoczi aio_context_release(aio_context); 3733fa40e656SJeff Cody } 3734fa40e656SJeff Cody 3735d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3736d26c9a15SKevin Wolf { 3737d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3738be4b67bcSMax Reitz BlockDriverState *bs; 3739be4b67bcSMax Reitz BlockBackend *blk = NULL; 3740d26c9a15SKevin Wolf QObject *obj; 3741d26c9a15SKevin Wolf QDict *qdict; 3742d26c9a15SKevin Wolf Error *local_err = NULL; 3743d26c9a15SKevin Wolf 374460e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3745d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 374660e19e06SMarkus Armbruster * when called from drive_new(). 3747d26c9a15SKevin Wolf * 3748d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3749d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3750c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3751c6e0bd9bSKevin Wolf options->cache->has_direct && 3752c6e0bd9bSKevin Wolf options->cache->direct; 3753c6e0bd9bSKevin Wolf if (!direct) { 3754d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3755d26c9a15SKevin Wolf goto fail; 3756d26c9a15SKevin Wolf } 3757d26c9a15SKevin Wolf } 3758d26c9a15SKevin Wolf 3759d26c9a15SKevin Wolf visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3760d26c9a15SKevin Wolf &options, NULL, &local_err); 376184d18f06SMarkus Armbruster if (local_err) { 3762d26c9a15SKevin Wolf error_propagate(errp, local_err); 3763d26c9a15SKevin Wolf goto fail; 3764d26c9a15SKevin Wolf } 3765d26c9a15SKevin Wolf 3766d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3767d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3768d26c9a15SKevin Wolf 3769d26c9a15SKevin Wolf qdict_flatten(qdict); 3770d26c9a15SKevin Wolf 3771be4b67bcSMax Reitz if (options->has_id) { 377218e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 377384d18f06SMarkus Armbruster if (local_err) { 3774b681072dSKevin Wolf error_propagate(errp, local_err); 3775d26c9a15SKevin Wolf goto fail; 3776d26c9a15SKevin Wolf } 3777d26c9a15SKevin Wolf 3778be4b67bcSMax Reitz bs = blk_bs(blk); 3779be4b67bcSMax Reitz } else { 3780be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3781be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3782be4b67bcSMax Reitz "the root node"); 3783be4b67bcSMax Reitz goto fail; 3784be4b67bcSMax Reitz } 3785be4b67bcSMax Reitz 3786bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3787bd745e23SMax Reitz if (!bs) { 3788be4b67bcSMax Reitz goto fail; 3789be4b67bcSMax Reitz } 3790be4b67bcSMax Reitz } 3791be4b67bcSMax Reitz 3792be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3793be4b67bcSMax Reitz if (blk) { 379418e46a03SMarkus Armbruster blk_unref(blk); 3795be4b67bcSMax Reitz } else { 3796be4b67bcSMax Reitz bdrv_unref(bs); 3797be4b67bcSMax Reitz } 37988ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 37998ae8e904SKevin Wolf goto fail; 38008ae8e904SKevin Wolf } 38018ae8e904SKevin Wolf 3802d26c9a15SKevin Wolf fail: 3803d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3804d26c9a15SKevin Wolf } 3805d26c9a15SKevin Wolf 380681b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 380781b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 380881b936aeSAlberto Garcia { 380981b936aeSAlberto Garcia AioContext *aio_context; 381081b936aeSAlberto Garcia BlockBackend *blk; 381181b936aeSAlberto Garcia BlockDriverState *bs; 381281b936aeSAlberto Garcia 381381b936aeSAlberto Garcia if (has_id && has_node_name) { 381481b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 381581b936aeSAlberto Garcia return; 381681b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 381781b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 381881b936aeSAlberto Garcia return; 381981b936aeSAlberto Garcia } 382081b936aeSAlberto Garcia 382181b936aeSAlberto Garcia if (has_id) { 382281b936aeSAlberto Garcia blk = blk_by_name(id); 382381b936aeSAlberto Garcia if (!blk) { 382481b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 382581b936aeSAlberto Garcia return; 382681b936aeSAlberto Garcia } 382781b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 382881b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 382981b936aeSAlberto Garcia return; 383081b936aeSAlberto Garcia } 383181b936aeSAlberto Garcia bs = blk_bs(blk); 383281b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 383381b936aeSAlberto Garcia } else { 383481b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 383581b936aeSAlberto Garcia if (!bs) { 383681b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 383781b936aeSAlberto Garcia return; 383881b936aeSAlberto Garcia } 383981b936aeSAlberto Garcia blk = bs->blk; 384081b936aeSAlberto Garcia if (blk) { 384181b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 384281b936aeSAlberto Garcia node_name, blk_name(blk)); 384381b936aeSAlberto Garcia return; 384481b936aeSAlberto Garcia } 384581b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 384681b936aeSAlberto Garcia } 384781b936aeSAlberto Garcia 384881b936aeSAlberto Garcia aio_context_acquire(aio_context); 384981b936aeSAlberto Garcia 385081b936aeSAlberto Garcia if (bs) { 385181b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 385281b936aeSAlberto Garcia goto out; 385381b936aeSAlberto Garcia } 385481b936aeSAlberto Garcia 385581b936aeSAlberto Garcia if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { 385681b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 385781b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 385881b936aeSAlberto Garcia goto out; 385981b936aeSAlberto Garcia } 386081b936aeSAlberto Garcia } 386181b936aeSAlberto Garcia 386281b936aeSAlberto Garcia if (blk) { 386381b936aeSAlberto Garcia blk_unref(blk); 386481b936aeSAlberto Garcia } else { 386581b936aeSAlberto Garcia bdrv_unref(bs); 386681b936aeSAlberto Garcia } 386781b936aeSAlberto Garcia 386881b936aeSAlberto Garcia out: 386981b936aeSAlberto Garcia aio_context_release(aio_context); 387081b936aeSAlberto Garcia } 387181b936aeSAlberto Garcia 3872fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3873fb5458cdSStefan Hajnoczi { 3874fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3875fea68bb6SMarkus Armbruster BlockDriverState *bs; 3876fea68bb6SMarkus Armbruster 3877fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 387869691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 387969691e72SStefan Hajnoczi 388069691e72SStefan Hajnoczi aio_context_acquire(aio_context); 388169691e72SStefan Hajnoczi 3882fea68bb6SMarkus Armbruster if (bs->job) { 3883fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3884fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 3885fea68bb6SMarkus Armbruster *p_next = elem; 3886fea68bb6SMarkus Armbruster p_next = &elem->next; 3887fea68bb6SMarkus Armbruster } 388869691e72SStefan Hajnoczi 388969691e72SStefan Hajnoczi aio_context_release(aio_context); 3890fea68bb6SMarkus Armbruster } 3891fea68bb6SMarkus Armbruster 3892fea68bb6SMarkus Armbruster return head; 3893fb5458cdSStefan Hajnoczi } 38944d454574SPaolo Bonzini 38950006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 38964d454574SPaolo Bonzini .name = "drive", 38970006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 38984d454574SPaolo Bonzini .desc = { 38994d454574SPaolo Bonzini { 39004d454574SPaolo Bonzini .name = "snapshot", 39014d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39024d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 39034d454574SPaolo Bonzini },{ 3904a9384affSPaolo Bonzini .name = "discard", 3905a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 3906a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 3907a9384affSPaolo Bonzini },{ 39084d454574SPaolo Bonzini .name = "aio", 39094d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39104d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 39114d454574SPaolo Bonzini },{ 39124d454574SPaolo Bonzini .name = "format", 39134d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39144d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 39154d454574SPaolo Bonzini },{ 39164d454574SPaolo Bonzini .name = "rerror", 39174d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39184d454574SPaolo Bonzini .help = "read error action", 39194d454574SPaolo Bonzini },{ 39204d454574SPaolo Bonzini .name = "werror", 39214d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39224d454574SPaolo Bonzini .help = "write error action", 39234d454574SPaolo Bonzini },{ 39240f227a94SKevin Wolf .name = "read-only", 39254d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39264d454574SPaolo Bonzini .help = "open drive file as read-only", 39274d454574SPaolo Bonzini },{ 392857975222SKevin Wolf .name = "throttling.iops-total", 39294d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39304d454574SPaolo Bonzini .help = "limit total I/O operations per second", 39314d454574SPaolo Bonzini },{ 393257975222SKevin Wolf .name = "throttling.iops-read", 39334d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39344d454574SPaolo Bonzini .help = "limit read operations per second", 39354d454574SPaolo Bonzini },{ 393657975222SKevin Wolf .name = "throttling.iops-write", 39374d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39384d454574SPaolo Bonzini .help = "limit write operations per second", 39394d454574SPaolo Bonzini },{ 394057975222SKevin Wolf .name = "throttling.bps-total", 39414d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39424d454574SPaolo Bonzini .help = "limit total bytes per second", 39434d454574SPaolo Bonzini },{ 394457975222SKevin Wolf .name = "throttling.bps-read", 39454d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39464d454574SPaolo Bonzini .help = "limit read bytes per second", 39474d454574SPaolo Bonzini },{ 394857975222SKevin Wolf .name = "throttling.bps-write", 39494d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39504d454574SPaolo Bonzini .help = "limit write bytes per second", 39514d454574SPaolo Bonzini },{ 39523e9fab69SBenoît Canet .name = "throttling.iops-total-max", 39533e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39543e9fab69SBenoît Canet .help = "I/O operations burst", 39553e9fab69SBenoît Canet },{ 39563e9fab69SBenoît Canet .name = "throttling.iops-read-max", 39573e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39583e9fab69SBenoît Canet .help = "I/O operations read burst", 39593e9fab69SBenoît Canet },{ 39603e9fab69SBenoît Canet .name = "throttling.iops-write-max", 39613e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39623e9fab69SBenoît Canet .help = "I/O operations write burst", 39633e9fab69SBenoît Canet },{ 39643e9fab69SBenoît Canet .name = "throttling.bps-total-max", 39653e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39663e9fab69SBenoît Canet .help = "total bytes burst", 39673e9fab69SBenoît Canet },{ 39683e9fab69SBenoît Canet .name = "throttling.bps-read-max", 39693e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39703e9fab69SBenoît Canet .help = "total bytes read burst", 39713e9fab69SBenoît Canet },{ 39723e9fab69SBenoît Canet .name = "throttling.bps-write-max", 39733e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39743e9fab69SBenoît Canet .help = "total bytes write burst", 39753e9fab69SBenoît Canet },{ 39762024c1dfSBenoît Canet .name = "throttling.iops-size", 39772024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 39782024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 39792024c1dfSBenoît Canet },{ 398076f4afb4SAlberto Garcia .name = "throttling.group", 398176f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 398276f4afb4SAlberto Garcia .help = "name of the block throttling group", 398376f4afb4SAlberto Garcia },{ 39844d454574SPaolo Bonzini .name = "copy-on-read", 39854d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39864d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 3987465bee1dSPeter Lieven },{ 3988465bee1dSPeter Lieven .name = "detect-zeroes", 3989465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 3990465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 3991362e9299SAlberto Garcia },{ 3992362e9299SAlberto Garcia .name = "stats-account-invalid", 3993362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3994362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 3995362e9299SAlberto Garcia "in the statistics", 3996362e9299SAlberto Garcia },{ 3997362e9299SAlberto Garcia .name = "stats-account-failed", 3998362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3999362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4000362e9299SAlberto Garcia "in the statistics", 40014d454574SPaolo Bonzini }, 40024d454574SPaolo Bonzini { /* end of list */ } 40034d454574SPaolo Bonzini }, 40044d454574SPaolo Bonzini }; 40050006383eSKevin Wolf 4006bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4007bd745e23SMax Reitz .name = "root-bds", 4008bd745e23SMax Reitz .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 4009bd745e23SMax Reitz .desc = { 4010bd745e23SMax Reitz { 4011bd745e23SMax Reitz .name = "discard", 4012bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4013bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4014bd745e23SMax Reitz },{ 4015bd745e23SMax Reitz .name = "aio", 4016bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4017bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4018bd745e23SMax Reitz },{ 4019bd745e23SMax Reitz .name = "read-only", 4020bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4021bd745e23SMax Reitz .help = "open drive file as read-only", 4022bd745e23SMax Reitz },{ 4023bd745e23SMax Reitz .name = "copy-on-read", 4024bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4025bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4026bd745e23SMax Reitz },{ 4027bd745e23SMax Reitz .name = "detect-zeroes", 4028bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4029bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4030bd745e23SMax Reitz }, 4031bd745e23SMax Reitz { /* end of list */ } 4032bd745e23SMax Reitz }, 4033bd745e23SMax Reitz }; 4034bd745e23SMax Reitz 40350006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 40360006383eSKevin Wolf .name = "drive", 40370006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 40380006383eSKevin Wolf .desc = { 4039492fdc6fSKevin Wolf /* 4040492fdc6fSKevin Wolf * no elements => accept any params 4041492fdc6fSKevin Wolf * validation will happen later 4042492fdc6fSKevin Wolf */ 40430006383eSKevin Wolf { /* end of list */ } 40440006383eSKevin Wolf }, 40450006383eSKevin Wolf }; 4046