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 (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) { 391fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_CACHE_WB; 392fbf8175eSMax Reitz } 393fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { 394fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NOCACHE; 395fbf8175eSMax Reitz } 396fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { 397fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NO_FLUSH; 398fbf8175eSMax Reitz } 399fbf8175eSMax Reitz 400fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 401fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 402fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 403fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 404fbf8175eSMax Reitz /* this is the default */ 405fbf8175eSMax Reitz } else { 406fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 407fbf8175eSMax Reitz return; 408fbf8175eSMax Reitz } 409fbf8175eSMax Reitz } 410fbf8175eSMax Reitz } 411fbf8175eSMax Reitz 412fbf8175eSMax Reitz /* disk I/O throttling */ 413fbf8175eSMax Reitz if (throttling_group) { 414fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 415fbf8175eSMax Reitz } 416fbf8175eSMax Reitz 417fbf8175eSMax Reitz if (throttle_cfg) { 418fbf8175eSMax Reitz memset(throttle_cfg, 0, sizeof(*throttle_cfg)); 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 421fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 422fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 429fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 431fbf8175eSMax Reitz 432fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 433fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 434fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 435fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 436fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 437fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 438fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 439fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 440fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 441fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 442fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 443fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 444fbf8175eSMax Reitz 445fbf8175eSMax Reitz throttle_cfg->op_size = 446fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 447fbf8175eSMax Reitz 448fbf8175eSMax Reitz if (!check_throttle_config(throttle_cfg, errp)) { 449fbf8175eSMax Reitz return; 450fbf8175eSMax Reitz } 451fbf8175eSMax Reitz } 452fbf8175eSMax Reitz 453fbf8175eSMax Reitz if (detect_zeroes) { 454fbf8175eSMax Reitz *detect_zeroes = 455fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 456fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 457fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, 458fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 459fbf8175eSMax Reitz &local_error); 460fbf8175eSMax Reitz if (local_error) { 461fbf8175eSMax Reitz error_propagate(errp, local_error); 462fbf8175eSMax Reitz return; 463fbf8175eSMax Reitz } 464fbf8175eSMax Reitz 465fbf8175eSMax Reitz if (bdrv_flags && 466fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 467fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 468fbf8175eSMax Reitz { 469fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 470fbf8175eSMax Reitz "without setting discard operation to unmap"); 471fbf8175eSMax Reitz return; 472fbf8175eSMax Reitz } 473fbf8175eSMax Reitz } 474fbf8175eSMax Reitz } 475fbf8175eSMax Reitz 476f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 47718e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 478b681072dSKevin Wolf Error **errp) 479666daa68SMarkus Armbruster { 480666daa68SMarkus Armbruster const char *buf; 481666daa68SMarkus Armbruster int bdrv_flags = 0; 482666daa68SMarkus Armbruster int on_read_error, on_write_error; 483362e9299SAlberto Garcia bool account_invalid, account_failed; 48426f54e9aSMarkus Armbruster BlockBackend *blk; 485a0f1eab1SMarkus Armbruster BlockDriverState *bs; 486cc0681c4SBenoît Canet ThrottleConfig cfg; 487666daa68SMarkus Armbruster int snapshot = 0; 488c546194fSStefan Hajnoczi Error *error = NULL; 4890006383eSKevin Wolf QemuOpts *opts; 49040119effSAlberto Garcia QDict *interval_dict = NULL; 49140119effSAlberto Garcia QList *interval_list = NULL; 4920006383eSKevin Wolf const char *id; 49374fe54f2SKevin Wolf bool has_driver_specific_opts; 494fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 495fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 496fbf8175eSMax Reitz const char *throttling_group = NULL; 497666daa68SMarkus Armbruster 498f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 499f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 500f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 5010006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 50284d18f06SMarkus Armbruster if (error) { 503b681072dSKevin Wolf error_propagate(errp, error); 5046376f952SMarkus Armbruster goto err_no_opts; 5050006383eSKevin Wolf } 5060006383eSKevin Wolf 5070006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 50884d18f06SMarkus Armbruster if (error) { 509b681072dSKevin Wolf error_propagate(errp, error); 510ec9c10d2SStefan Hajnoczi goto early_err; 5110006383eSKevin Wolf } 5120006383eSKevin Wolf 5130006383eSKevin Wolf if (id) { 5140006383eSKevin Wolf qdict_del(bs_opts, "id"); 5150006383eSKevin Wolf } 5160006383eSKevin Wolf 51774fe54f2SKevin Wolf has_driver_specific_opts = !!qdict_size(bs_opts); 51874fe54f2SKevin Wolf 519666daa68SMarkus Armbruster /* extract parameters */ 520666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 521666daa68SMarkus Armbruster 522362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 523362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 524362e9299SAlberto Garcia 52540119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 52640119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 52740119effSAlberto Garcia 52840119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 52940119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 53040119effSAlberto Garcia qdict_first(interval_dict)->key); 53140119effSAlberto Garcia goto early_err; 53240119effSAlberto Garcia } 5332be5506fSAlberto Garcia 534fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 535fbf8175eSMax Reitz &detect_zeroes, &error); 536fbf8175eSMax Reitz if (error) { 537fbf8175eSMax Reitz error_propagate(errp, error); 538ec9c10d2SStefan Hajnoczi goto early_err; 539a9384affSPaolo Bonzini } 540666daa68SMarkus Armbruster 541666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 542c8057f95SPeter Maydell if (is_help_option(buf)) { 543807105a7SMarkus Armbruster error_printf("Supported formats:"); 544666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 545807105a7SMarkus Armbruster error_printf("\n"); 546ec9c10d2SStefan Hajnoczi goto early_err; 547666daa68SMarkus Armbruster } 54874fe54f2SKevin Wolf 549e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 550e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 551ec9c10d2SStefan Hajnoczi goto early_err; 5526db5f5d6SMike Qiu } 553e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 554666daa68SMarkus Armbruster } 555666daa68SMarkus Armbruster 55692aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 557666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 558b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 55984d18f06SMarkus Armbruster if (error) { 560b681072dSKevin Wolf error_propagate(errp, error); 561ec9c10d2SStefan Hajnoczi goto early_err; 562666daa68SMarkus Armbruster } 563666daa68SMarkus Armbruster } 564666daa68SMarkus Armbruster 56592aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 566666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 567b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 56884d18f06SMarkus Armbruster if (error) { 569b681072dSKevin Wolf error_propagate(errp, error); 570ec9c10d2SStefan Hajnoczi goto early_err; 571666daa68SMarkus Armbruster } 572666daa68SMarkus Armbruster } 573666daa68SMarkus Armbruster 574666daa68SMarkus Armbruster if (snapshot) { 575666daa68SMarkus Armbruster /* always use cache=unsafe with snapshot */ 576666daa68SMarkus Armbruster bdrv_flags &= ~BDRV_O_CACHE_MASK; 577666daa68SMarkus Armbruster bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); 578666daa68SMarkus Armbruster } 579666daa68SMarkus Armbruster 5805ec18f8cSMax Reitz /* init */ 5815ec18f8cSMax Reitz if ((!file || !*file) && !has_driver_specific_opts) { 5825ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5835ec18f8cSMax Reitz 5845ec18f8cSMax Reitz blk = blk_new(qemu_opts_id(opts), errp); 5855ec18f8cSMax Reitz if (!blk) { 5865ec18f8cSMax Reitz goto early_err; 5875ec18f8cSMax Reitz } 5885ec18f8cSMax Reitz 5895ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5905ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 591fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5925ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5935ec18f8cSMax Reitz 5945ec18f8cSMax Reitz if (throttle_enabled(&cfg)) { 5955ec18f8cSMax Reitz if (!throttling_group) { 5965ec18f8cSMax Reitz throttling_group = blk_name(blk); 5975ec18f8cSMax Reitz } 5985ec18f8cSMax Reitz blk_rs->throttle_group = g_strdup(throttling_group); 5995ec18f8cSMax Reitz blk_rs->throttle_state = throttle_group_incref(throttling_group); 6005ec18f8cSMax Reitz blk_rs->throttle_state->cfg = cfg; 6015ec18f8cSMax Reitz } 6025ec18f8cSMax Reitz 6035ec18f8cSMax Reitz QDECREF(bs_opts); 6045ec18f8cSMax Reitz } else { 6055ec18f8cSMax Reitz if (file && !*file) { 6065ec18f8cSMax Reitz file = NULL; 6075ec18f8cSMax Reitz } 6085ec18f8cSMax Reitz 609e4342ce5SMax Reitz blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, 610e4342ce5SMax Reitz errp); 611e4342ce5SMax Reitz if (!blk) { 612e4342ce5SMax Reitz goto err_no_bs_opts; 613e4342ce5SMax Reitz } 614e4342ce5SMax Reitz bs = blk_bs(blk); 6150006383eSKevin Wolf 616e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 617e4342ce5SMax Reitz 618e4342ce5SMax Reitz /* disk I/O throttling */ 619e4342ce5SMax Reitz if (throttle_enabled(&cfg)) { 62076f4afb4SAlberto Garcia if (!throttling_group) { 62176f4afb4SAlberto Garcia throttling_group = blk_name(blk); 62276f4afb4SAlberto Garcia } 62376f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, throttling_group); 624e4342ce5SMax Reitz bdrv_set_io_limits(bs, &cfg); 625666daa68SMarkus Armbruster } 626666daa68SMarkus Armbruster 627a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 628666daa68SMarkus Armbruster autostart = 0; 629a0f1eab1SMarkus Armbruster } 630362e9299SAlberto Garcia 631362e9299SAlberto Garcia block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 6322be5506fSAlberto Garcia 63340119effSAlberto Garcia if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 6342be5506fSAlberto Garcia blk_unref(blk); 6352be5506fSAlberto Garcia blk = NULL; 6362be5506fSAlberto Garcia goto err_no_bs_opts; 6372be5506fSAlberto Garcia } 6382be5506fSAlberto Garcia } 6395ec18f8cSMax Reitz 6405ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6410006383eSKevin Wolf 642e4342ce5SMax Reitz err_no_bs_opts: 6430006383eSKevin Wolf qemu_opts_del(opts); 64440119effSAlberto Garcia QDECREF(interval_dict); 64540119effSAlberto Garcia QDECREF(interval_list); 64618e46a03SMarkus Armbruster return blk; 647a9ae2bffSMarkus Armbruster 648ec9c10d2SStefan Hajnoczi early_err: 649ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 65040119effSAlberto Garcia QDECREF(interval_dict); 65140119effSAlberto Garcia QDECREF(interval_list); 6526376f952SMarkus Armbruster err_no_opts: 6536376f952SMarkus Armbruster QDECREF(bs_opts); 654a9ae2bffSMarkus Armbruster return NULL; 655666daa68SMarkus Armbruster } 656666daa68SMarkus Armbruster 657bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 658bd745e23SMax Reitz 659bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 660bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 661bd745e23SMax Reitz { 662bd745e23SMax Reitz BlockDriverState *bs; 663bd745e23SMax Reitz QemuOpts *opts; 664bd745e23SMax Reitz Error *local_error = NULL; 665bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 666bd745e23SMax Reitz int ret; 667bd745e23SMax Reitz int bdrv_flags = 0; 668bd745e23SMax Reitz 669bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 670bd745e23SMax Reitz if (!opts) { 671bd745e23SMax Reitz goto fail; 672bd745e23SMax Reitz } 673bd745e23SMax Reitz 674bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 675bd745e23SMax Reitz if (local_error) { 676bd745e23SMax Reitz error_propagate(errp, local_error); 677bd745e23SMax Reitz goto fail; 678bd745e23SMax Reitz } 679bd745e23SMax Reitz 680bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 681bd745e23SMax Reitz &detect_zeroes, &local_error); 682bd745e23SMax Reitz if (local_error) { 683bd745e23SMax Reitz error_propagate(errp, local_error); 684bd745e23SMax Reitz goto fail; 685bd745e23SMax Reitz } 686bd745e23SMax Reitz 687bd745e23SMax Reitz bs = NULL; 688bd745e23SMax Reitz ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 689bd745e23SMax Reitz if (ret < 0) { 690bd745e23SMax Reitz goto fail_no_bs_opts; 691bd745e23SMax Reitz } 692bd745e23SMax Reitz 693bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 694bd745e23SMax Reitz 695bd745e23SMax Reitz fail_no_bs_opts: 696bd745e23SMax Reitz qemu_opts_del(opts); 697bd745e23SMax Reitz return bs; 698bd745e23SMax Reitz 699bd745e23SMax Reitz fail: 700bd745e23SMax Reitz qemu_opts_del(opts); 701bd745e23SMax Reitz QDECREF(bs_opts); 702bd745e23SMax Reitz return NULL; 703bd745e23SMax Reitz } 704bd745e23SMax Reitz 7055abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 7065abbf0eeSKevin Wolf Error **errp) 70757975222SKevin Wolf { 70857975222SKevin Wolf const char *value; 70957975222SKevin Wolf 71057975222SKevin Wolf value = qemu_opt_get(opts, from); 71157975222SKevin Wolf if (value) { 7125abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 7135abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 7145abbf0eeSKevin Wolf "same time", to, from); 7155abbf0eeSKevin Wolf return; 7165abbf0eeSKevin Wolf } 71720d6cd47SJun Li } 71820d6cd47SJun Li 71920d6cd47SJun Li /* rename all items in opts */ 72020d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 721f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 72257975222SKevin Wolf qemu_opt_unset(opts, from); 72357975222SKevin Wolf } 72457975222SKevin Wolf } 72557975222SKevin Wolf 72633cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 72733cb7dc8SKevin Wolf .name = "drive", 72833cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 72933cb7dc8SKevin Wolf .desc = { 73033cb7dc8SKevin Wolf { 73187a899c5SKevin Wolf .name = "bus", 73287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 73387a899c5SKevin Wolf .help = "bus number", 73487a899c5SKevin Wolf },{ 73587a899c5SKevin Wolf .name = "unit", 73687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 73787a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 73887a899c5SKevin Wolf },{ 73987a899c5SKevin Wolf .name = "index", 74087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 74187a899c5SKevin Wolf .help = "index number", 74287a899c5SKevin Wolf },{ 74333cb7dc8SKevin Wolf .name = "media", 74433cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 74533cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 746593d464bSKevin Wolf },{ 747593d464bSKevin Wolf .name = "if", 748593d464bSKevin Wolf .type = QEMU_OPT_STRING, 749593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 750b41a7338SKevin Wolf },{ 751b41a7338SKevin Wolf .name = "cyls", 752b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 753b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 754b41a7338SKevin Wolf },{ 755b41a7338SKevin Wolf .name = "heads", 756b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 757b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 758b41a7338SKevin Wolf },{ 759b41a7338SKevin Wolf .name = "secs", 760b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 761b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 762b41a7338SKevin Wolf },{ 763b41a7338SKevin Wolf .name = "trans", 764b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 765b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 76626929298SKevin Wolf },{ 76726929298SKevin Wolf .name = "boot", 76826929298SKevin Wolf .type = QEMU_OPT_BOOL, 76926929298SKevin Wolf .help = "(deprecated, ignored)", 770394c7d4dSKevin Wolf },{ 771394c7d4dSKevin Wolf .name = "addr", 772394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 773394c7d4dSKevin Wolf .help = "pci address (virtio only)", 774d095b465SMax Reitz },{ 775bcf83158SKevin Wolf .name = "serial", 776bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 777bcf83158SKevin Wolf .help = "disk serial number", 778bcf83158SKevin Wolf },{ 779d095b465SMax Reitz .name = "file", 780d095b465SMax Reitz .type = QEMU_OPT_STRING, 781d095b465SMax Reitz .help = "file name", 78233cb7dc8SKevin Wolf }, 7830ebd24e0SKevin Wolf 7840ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7850ebd24e0SKevin Wolf { 7860ebd24e0SKevin Wolf .name = "read-only", 7870ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7880ebd24e0SKevin Wolf .help = "open drive file as read-only", 7890ebd24e0SKevin Wolf },{ 790ee13ed1cSKevin Wolf .name = "rerror", 791ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 792ee13ed1cSKevin Wolf .help = "read error action", 793ee13ed1cSKevin Wolf },{ 794ee13ed1cSKevin Wolf .name = "werror", 795ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 796ee13ed1cSKevin Wolf .help = "write error action", 797ee13ed1cSKevin Wolf },{ 7980ebd24e0SKevin Wolf .name = "copy-on-read", 7990ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 8000ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 8010ebd24e0SKevin Wolf }, 8020ebd24e0SKevin Wolf 80333cb7dc8SKevin Wolf { /* end of list */ } 80433cb7dc8SKevin Wolf }, 80533cb7dc8SKevin Wolf }; 80633cb7dc8SKevin Wolf 80760e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 80857975222SKevin Wolf { 80929c4e2b5SKevin Wolf const char *value; 81018e46a03SMarkus Armbruster BlockBackend *blk; 81133cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 812f298d071SKevin Wolf QDict *bs_opts; 81333cb7dc8SKevin Wolf QemuOpts *legacy_opts; 81433cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 815593d464bSKevin Wolf BlockInterfaceType type; 816b41a7338SKevin Wolf int cyls, heads, secs, translation; 81787a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 818394c7d4dSKevin Wolf const char *devaddr; 819ee13ed1cSKevin Wolf const char *werror, *rerror; 820a7fdbcf0SFam Zheng bool read_only = false; 821a7fdbcf0SFam Zheng bool copy_on_read; 822bcf83158SKevin Wolf const char *serial; 823d095b465SMax Reitz const char *filename; 82433cb7dc8SKevin Wolf Error *local_err = NULL; 825247147fbSKevin Wolf int i; 82629c4e2b5SKevin Wolf 82757975222SKevin Wolf /* Change legacy command line options into QMP ones */ 828247147fbSKevin Wolf static const struct { 829247147fbSKevin Wolf const char *from; 830247147fbSKevin Wolf const char *to; 831247147fbSKevin Wolf } opt_renames[] = { 832247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 833247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 834247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 83557975222SKevin Wolf 836247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 837247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 838247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 83957975222SKevin Wolf 840247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 841247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 842247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8433e9fab69SBenoît Canet 844247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 845247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 846247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8473e9fab69SBenoît Canet 848247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8492024c1dfSBenoît Canet 85076f4afb4SAlberto Garcia { "group", "throttling.group" }, 85176f4afb4SAlberto Garcia 852247147fbSKevin Wolf { "readonly", "read-only" }, 853247147fbSKevin Wolf }; 854247147fbSKevin Wolf 855247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8565abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8575abbf0eeSKevin Wolf &local_err); 8585abbf0eeSKevin Wolf if (local_err) { 859565f65d2SMarkus Armbruster error_report_err(local_err); 8605abbf0eeSKevin Wolf return NULL; 8615abbf0eeSKevin Wolf } 862247147fbSKevin Wolf } 8630f227a94SKevin Wolf 86429c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 86529c4e2b5SKevin Wolf if (value) { 86629c4e2b5SKevin Wolf int flags = 0; 86729c4e2b5SKevin Wolf 86829c4e2b5SKevin Wolf if (bdrv_parse_cache_flags(value, &flags) != 0) { 86929c4e2b5SKevin Wolf error_report("invalid cache option"); 87029c4e2b5SKevin Wolf return NULL; 87129c4e2b5SKevin Wolf } 87229c4e2b5SKevin Wolf 87329c4e2b5SKevin Wolf /* Specific options take precedence */ 87454861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 87554861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 876cccb7967SMarkus Armbruster !!(flags & BDRV_O_CACHE_WB), &error_abort); 87729c4e2b5SKevin Wolf } 87854861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 87954861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 880cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 88129c4e2b5SKevin Wolf } 88254861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 88354861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 884cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 88529c4e2b5SKevin Wolf } 88629c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 88729c4e2b5SKevin Wolf } 88829c4e2b5SKevin Wolf 889f298d071SKevin Wolf /* Get a QDict for processing the options */ 890f298d071SKevin Wolf bs_opts = qdict_new(); 891f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 892f298d071SKevin Wolf 89387ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 89487ea75d5SPeter Crosthwaite &error_abort); 89533cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 89684d18f06SMarkus Armbruster if (local_err) { 897565f65d2SMarkus Armbruster error_report_err(local_err); 89833cb7dc8SKevin Wolf goto fail; 89933cb7dc8SKevin Wolf } 90033cb7dc8SKevin Wolf 90126929298SKevin Wolf /* Deprecated option boot=[on|off] */ 90226929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 90326929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 90426929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 90526929298SKevin Wolf "update your scripts.\n"); 90626929298SKevin Wolf } 90726929298SKevin Wolf 90833cb7dc8SKevin Wolf /* Media type */ 90933cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 91033cb7dc8SKevin Wolf if (value) { 91133cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 91233cb7dc8SKevin Wolf media = MEDIA_DISK; 91333cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 91433cb7dc8SKevin Wolf media = MEDIA_CDROM; 915a7fdbcf0SFam Zheng read_only = true; 91633cb7dc8SKevin Wolf } else { 91733cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 91833cb7dc8SKevin Wolf goto fail; 91933cb7dc8SKevin Wolf } 92033cb7dc8SKevin Wolf } 92133cb7dc8SKevin Wolf 9220ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 923a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 9240ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 9250ebd24e0SKevin Wolf 9260ebd24e0SKevin Wolf if (read_only && copy_on_read) { 9270ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 9280ebd24e0SKevin Wolf copy_on_read = false; 9290ebd24e0SKevin Wolf } 9300ebd24e0SKevin Wolf 9310ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 9320ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 9330ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 9340ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 9350ebd24e0SKevin Wolf 936593d464bSKevin Wolf /* Controller type */ 937593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 938593d464bSKevin Wolf if (value) { 939593d464bSKevin Wolf for (type = 0; 940593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 941593d464bSKevin Wolf type++) { 942593d464bSKevin Wolf } 943593d464bSKevin Wolf if (type == IF_COUNT) { 944593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 945593d464bSKevin Wolf goto fail; 946593d464bSKevin Wolf } 947593d464bSKevin Wolf } else { 948593d464bSKevin Wolf type = block_default_type; 949593d464bSKevin Wolf } 950593d464bSKevin Wolf 951b41a7338SKevin Wolf /* Geometry */ 952b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 953b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 954b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 955b41a7338SKevin Wolf 956b41a7338SKevin Wolf if (cyls || heads || secs) { 957b41a7338SKevin Wolf if (cyls < 1) { 958b41a7338SKevin Wolf error_report("invalid physical cyls number"); 959b41a7338SKevin Wolf goto fail; 960b41a7338SKevin Wolf } 961b41a7338SKevin Wolf if (heads < 1) { 962b41a7338SKevin Wolf error_report("invalid physical heads number"); 963b41a7338SKevin Wolf goto fail; 964b41a7338SKevin Wolf } 965b41a7338SKevin Wolf if (secs < 1) { 966b41a7338SKevin Wolf error_report("invalid physical secs number"); 967b41a7338SKevin Wolf goto fail; 968b41a7338SKevin Wolf } 969b41a7338SKevin Wolf } 970b41a7338SKevin Wolf 971b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 972b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 973b41a7338SKevin Wolf if (value != NULL) { 974b41a7338SKevin Wolf if (!cyls) { 975b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 976b41a7338SKevin Wolf value); 977b41a7338SKevin Wolf goto fail; 978b41a7338SKevin Wolf } 979b41a7338SKevin Wolf if (!strcmp(value, "none")) { 980b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 981b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 982b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 983f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 984f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 985f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 986f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 987b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 988b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 989b41a7338SKevin Wolf } else { 990b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 991b41a7338SKevin Wolf goto fail; 992b41a7338SKevin Wolf } 993b41a7338SKevin Wolf } 994b41a7338SKevin Wolf 995b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 996b41a7338SKevin Wolf if (cyls || secs || heads) { 997b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 998b41a7338SKevin Wolf goto fail; 999b41a7338SKevin Wolf } 1000b41a7338SKevin Wolf } 1001b41a7338SKevin Wolf 100287a899c5SKevin Wolf /* Device address specified by bus/unit or index. 100387a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 100487a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 100587a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 100687a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 100787a899c5SKevin Wolf 100887a899c5SKevin Wolf max_devs = if_max_devs[type]; 100987a899c5SKevin Wolf 101087a899c5SKevin Wolf if (index != -1) { 101187a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 101287a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 101387a899c5SKevin Wolf goto fail; 101487a899c5SKevin Wolf } 101587a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 101687a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 101787a899c5SKevin Wolf } 101887a899c5SKevin Wolf 101987a899c5SKevin Wolf if (unit_id == -1) { 102087a899c5SKevin Wolf unit_id = 0; 102187a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 102287a899c5SKevin Wolf unit_id++; 102387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 102487a899c5SKevin Wolf unit_id -= max_devs; 102587a899c5SKevin Wolf bus_id++; 102687a899c5SKevin Wolf } 102787a899c5SKevin Wolf } 102887a899c5SKevin Wolf } 102987a899c5SKevin Wolf 103087a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 103187a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 103287a899c5SKevin Wolf goto fail; 103387a899c5SKevin Wolf } 103487a899c5SKevin Wolf 103587a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 103687a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 103787a899c5SKevin Wolf bus_id, unit_id, index); 103887a899c5SKevin Wolf goto fail; 103987a899c5SKevin Wolf } 104087a899c5SKevin Wolf 1041bcf83158SKevin Wolf /* Serial number */ 1042bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1043bcf83158SKevin Wolf 104487a899c5SKevin Wolf /* no id supplied -> create one */ 104587a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 104687a899c5SKevin Wolf char *new_id; 104787a899c5SKevin Wolf const char *mediastr = ""; 104887a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 104987a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 105087a899c5SKevin Wolf } 105187a899c5SKevin Wolf if (max_devs) { 105287a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 105387a899c5SKevin Wolf mediastr, unit_id); 105487a899c5SKevin Wolf } else { 105587a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 105687a899c5SKevin Wolf mediastr, unit_id); 105787a899c5SKevin Wolf } 105887a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 105987a899c5SKevin Wolf g_free(new_id); 106087a899c5SKevin Wolf } 106187a899c5SKevin Wolf 1062394c7d4dSKevin Wolf /* Add virtio block device */ 1063394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1064394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1065394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1066394c7d4dSKevin Wolf goto fail; 1067394c7d4dSKevin Wolf } 1068394c7d4dSKevin Wolf 1069394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1070394c7d4dSKevin Wolf QemuOpts *devopts; 107187ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 107287ea75d5SPeter Crosthwaite &error_abort); 1073394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10741f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1075394c7d4dSKevin Wolf } else { 1076f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1077394c7d4dSKevin Wolf } 1078f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1079f43e47dbSMarkus Armbruster &error_abort); 1080394c7d4dSKevin Wolf if (devaddr) { 1081f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1082394c7d4dSKevin Wolf } 1083394c7d4dSKevin Wolf } 1084394c7d4dSKevin Wolf 1085d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1086d095b465SMax Reitz 1087ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1088ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1089ee13ed1cSKevin Wolf if (werror != NULL) { 1090ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1091ee13ed1cSKevin Wolf type != IF_NONE) { 1092ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1093ee13ed1cSKevin Wolf goto fail; 1094ee13ed1cSKevin Wolf } 1095ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1096ee13ed1cSKevin Wolf } 1097ee13ed1cSKevin Wolf 1098ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1099ee13ed1cSKevin Wolf if (rerror != NULL) { 1100ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1101ee13ed1cSKevin Wolf type != IF_NONE) { 1102ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1103ee13ed1cSKevin Wolf goto fail; 1104ee13ed1cSKevin Wolf } 1105ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1106ee13ed1cSKevin Wolf } 1107ee13ed1cSKevin Wolf 11082d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 110918e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 11103cb0e25cSMarkus Armbruster bs_opts = NULL; 111118e46a03SMarkus Armbruster if (!blk) { 111284d18f06SMarkus Armbruster if (local_err) { 1113565f65d2SMarkus Armbruster error_report_err(local_err); 1114b681072dSKevin Wolf } 11152d246f01SKevin Wolf goto fail; 1116b681072dSKevin Wolf } else { 111784d18f06SMarkus Armbruster assert(!local_err); 11182d246f01SKevin Wolf } 11192d246f01SKevin Wolf 112026f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 112126f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1122f298d071SKevin Wolf dinfo->opts = all_opts; 11232d246f01SKevin Wolf 1124b41a7338SKevin Wolf dinfo->cyls = cyls; 1125b41a7338SKevin Wolf dinfo->heads = heads; 1126b41a7338SKevin Wolf dinfo->secs = secs; 1127b41a7338SKevin Wolf dinfo->trans = translation; 1128b41a7338SKevin Wolf 1129ee13ed1cSKevin Wolf dinfo->type = type; 113087a899c5SKevin Wolf dinfo->bus = bus_id; 113187a899c5SKevin Wolf dinfo->unit = unit_id; 1132394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1133bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1134bcf83158SKevin Wolf 113526f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 113626f8b3a8SMarkus Armbruster 1137e34ef046SKevin Wolf switch(type) { 1138e34ef046SKevin Wolf case IF_IDE: 1139e34ef046SKevin Wolf case IF_SCSI: 1140e34ef046SKevin Wolf case IF_XEN: 1141e34ef046SKevin Wolf case IF_NONE: 1142e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1143e34ef046SKevin Wolf break; 1144e34ef046SKevin Wolf default: 1145e34ef046SKevin Wolf break; 1146e34ef046SKevin Wolf } 1147e34ef046SKevin Wolf 11482d246f01SKevin Wolf fail: 114933cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11503cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11512d246f01SKevin Wolf return dinfo; 115257975222SKevin Wolf } 115357975222SKevin Wolf 11543e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1155666daa68SMarkus Armbruster { 1156666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1157a0e8544cSFam Zheng BlockBackend *blk; 11582d3735d3SStefan Hajnoczi int ret; 11592d3735d3SStefan Hajnoczi 1160e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1161e8877497SStefan Hajnoczi ret = bdrv_commit_all(); 1162e8877497SStefan Hajnoczi } else { 116384aa0140SStefan Hajnoczi BlockDriverState *bs; 116484aa0140SStefan Hajnoczi AioContext *aio_context; 116584aa0140SStefan Hajnoczi 1166a0e8544cSFam Zheng blk = blk_by_name(device); 1167a0e8544cSFam Zheng if (!blk) { 116858513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1169ac59eb95SMarkus Armbruster return; 11706ab4b5abSMarkus Armbruster } 11715433c24fSMax Reitz if (!blk_is_available(blk)) { 11725433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11735433c24fSMax Reitz return; 11745433c24fSMax Reitz } 117584aa0140SStefan Hajnoczi 117684aa0140SStefan Hajnoczi bs = blk_bs(blk); 117784aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 117884aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 117984aa0140SStefan Hajnoczi 118084aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 118184aa0140SStefan Hajnoczi 118284aa0140SStefan Hajnoczi aio_context_release(aio_context); 11832d3735d3SStefan Hajnoczi } 118458513bdeSJeff Cody if (ret < 0) { 118558513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 118658513bdeSJeff Cody strerror(-ret)); 1187666daa68SMarkus Armbruster } 1188666daa68SMarkus Armbruster } 1189666daa68SMarkus Armbruster 11906a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data, 11916a8f9661SEric Blake Error **errp) 11926cc2a415SPaolo Bonzini { 1193c8a83e85SKevin Wolf TransactionAction action; 1194c8a83e85SKevin Wolf TransactionActionList list; 11956cc2a415SPaolo Bonzini 11966a8f9661SEric Blake action.type = type; 11976a8f9661SEric Blake action.u.data = data; 11986cc2a415SPaolo Bonzini list.value = &action; 11996cc2a415SPaolo Bonzini list.next = NULL; 120094d16a64SJohn Snow qmp_transaction(&list, false, NULL, errp); 12016cc2a415SPaolo Bonzini } 12026cc2a415SPaolo Bonzini 12030901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 12040901f67eSBenoît Canet bool has_node_name, const char *node_name, 12050901f67eSBenoît Canet const char *snapshot_file, 12060901f67eSBenoît Canet bool has_snapshot_node_name, 12070901f67eSBenoît Canet const char *snapshot_node_name, 12086106e249SLuiz Capitulino bool has_format, const char *format, 12090901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1210f8882568SJes Sorensen { 1211a911e6aeSAlberto Garcia BlockdevSnapshotSync snapshot = { 12120901f67eSBenoît Canet .has_device = has_device, 12136cc2a415SPaolo Bonzini .device = (char *) device, 12140901f67eSBenoît Canet .has_node_name = has_node_name, 12150901f67eSBenoît Canet .node_name = (char *) node_name, 12166cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 12170901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 12180901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 12196cc2a415SPaolo Bonzini .has_format = has_format, 12206cc2a415SPaolo Bonzini .format = (char *) format, 12216cc2a415SPaolo Bonzini .has_mode = has_mode, 12226cc2a415SPaolo Bonzini .mode = mode, 12236cc2a415SPaolo Bonzini }; 1224c8a83e85SKevin Wolf blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 1225c8a83e85SKevin Wolf &snapshot, errp); 1226f8882568SJes Sorensen } 1227f8882568SJes Sorensen 122843de7e2dSAlberto Garcia void qmp_blockdev_snapshot(const char *node, const char *overlay, 122943de7e2dSAlberto Garcia Error **errp) 123043de7e2dSAlberto Garcia { 123143de7e2dSAlberto Garcia BlockdevSnapshot snapshot_data = { 123243de7e2dSAlberto Garcia .node = (char *) node, 123343de7e2dSAlberto Garcia .overlay = (char *) overlay 123443de7e2dSAlberto Garcia }; 123543de7e2dSAlberto Garcia 123643de7e2dSAlberto Garcia blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 123743de7e2dSAlberto Garcia &snapshot_data, errp); 123843de7e2dSAlberto Garcia } 123943de7e2dSAlberto Garcia 1240f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1241f323bc9eSWenchao Xia const char *name, 1242f323bc9eSWenchao Xia Error **errp) 1243f323bc9eSWenchao Xia { 1244f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1245f323bc9eSWenchao Xia .device = (char *) device, 1246f323bc9eSWenchao Xia .name = (char *) name 1247f323bc9eSWenchao Xia }; 1248f323bc9eSWenchao Xia 1249f323bc9eSWenchao Xia blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 1250f323bc9eSWenchao Xia &snapshot, errp); 1251f323bc9eSWenchao Xia } 1252f323bc9eSWenchao Xia 125344e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 125444e3e053SWenchao Xia bool has_id, 125544e3e053SWenchao Xia const char *id, 125644e3e053SWenchao Xia bool has_name, 125744e3e053SWenchao Xia const char *name, 125844e3e053SWenchao Xia Error **errp) 125944e3e053SWenchao Xia { 1260a0e8544cSFam Zheng BlockDriverState *bs; 1261a0e8544cSFam Zheng BlockBackend *blk; 12624ef3982aSStefan Hajnoczi AioContext *aio_context; 126344e3e053SWenchao Xia QEMUSnapshotInfo sn; 126444e3e053SWenchao Xia Error *local_err = NULL; 126544e3e053SWenchao Xia SnapshotInfo *info = NULL; 126644e3e053SWenchao Xia int ret; 126744e3e053SWenchao Xia 1268a0e8544cSFam Zheng blk = blk_by_name(device); 1269a0e8544cSFam Zheng if (!blk) { 127075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 127175158ebbSMarkus Armbruster "Device '%s' not found", device); 127244e3e053SWenchao Xia return NULL; 127344e3e053SWenchao Xia } 12745433c24fSMax Reitz 12755433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 12765433c24fSMax Reitz aio_context_acquire(aio_context); 127744e3e053SWenchao Xia 127844e3e053SWenchao Xia if (!has_id) { 127944e3e053SWenchao Xia id = NULL; 128044e3e053SWenchao Xia } 128144e3e053SWenchao Xia 128244e3e053SWenchao Xia if (!has_name) { 128344e3e053SWenchao Xia name = NULL; 128444e3e053SWenchao Xia } 128544e3e053SWenchao Xia 128644e3e053SWenchao Xia if (!id && !name) { 128744e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 12885433c24fSMax Reitz goto out_aio_context; 128944e3e053SWenchao Xia } 129044e3e053SWenchao Xia 12915433c24fSMax Reitz if (!blk_is_available(blk)) { 12925433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 12935433c24fSMax Reitz goto out_aio_context; 12945433c24fSMax Reitz } 12955433c24fSMax Reitz bs = blk_bs(blk); 12964ef3982aSStefan Hajnoczi 12970b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 12980b928854SStefan Hajnoczi goto out_aio_context; 12990b928854SStefan Hajnoczi } 13000b928854SStefan Hajnoczi 130144e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 130284d18f06SMarkus Armbruster if (local_err) { 130344e3e053SWenchao Xia error_propagate(errp, local_err); 13044ef3982aSStefan Hajnoczi goto out_aio_context; 130544e3e053SWenchao Xia } 130644e3e053SWenchao Xia if (!ret) { 130744e3e053SWenchao Xia error_setg(errp, 130844e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 130944e3e053SWenchao Xia "device '%s'", 131044e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 13114ef3982aSStefan Hajnoczi goto out_aio_context; 131244e3e053SWenchao Xia } 131344e3e053SWenchao Xia 131444e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 131584d18f06SMarkus Armbruster if (local_err) { 131644e3e053SWenchao Xia error_propagate(errp, local_err); 13174ef3982aSStefan Hajnoczi goto out_aio_context; 131844e3e053SWenchao Xia } 131944e3e053SWenchao Xia 13204ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13214ef3982aSStefan Hajnoczi 13225839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 132344e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 132444e3e053SWenchao Xia info->name = g_strdup(sn.name); 132544e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 132644e3e053SWenchao Xia info->date_sec = sn.date_sec; 132744e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 132844e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 132944e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 133044e3e053SWenchao Xia 133144e3e053SWenchao Xia return info; 13324ef3982aSStefan Hajnoczi 13334ef3982aSStefan Hajnoczi out_aio_context: 13344ef3982aSStefan Hajnoczi aio_context_release(aio_context); 13354ef3982aSStefan Hajnoczi return NULL; 133644e3e053SWenchao Xia } 13378802d1fdSJeff Cody 1338341ebc2fSJohn Snow /** 1339341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1340341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1341341ebc2fSJohn Snow * the node reference and bitmap names. 1342341ebc2fSJohn Snow * 1343341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1344341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1345341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1346341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1347341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1348341ebc2fSJohn Snow * 1349341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1350341ebc2fSJohn Snow */ 1351341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1352341ebc2fSJohn Snow const char *name, 1353341ebc2fSJohn Snow BlockDriverState **pbs, 1354341ebc2fSJohn Snow AioContext **paio, 1355341ebc2fSJohn Snow Error **errp) 1356341ebc2fSJohn Snow { 1357341ebc2fSJohn Snow BlockDriverState *bs; 1358341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1359341ebc2fSJohn Snow AioContext *aio_context; 1360341ebc2fSJohn Snow 1361341ebc2fSJohn Snow if (!node) { 1362341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1363341ebc2fSJohn Snow return NULL; 1364341ebc2fSJohn Snow } 1365341ebc2fSJohn Snow if (!name) { 1366341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1367341ebc2fSJohn Snow return NULL; 1368341ebc2fSJohn Snow } 1369341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1370341ebc2fSJohn Snow if (!bs) { 1371341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1372341ebc2fSJohn Snow return NULL; 1373341ebc2fSJohn Snow } 1374341ebc2fSJohn Snow 1375341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1376341ebc2fSJohn Snow aio_context_acquire(aio_context); 1377341ebc2fSJohn Snow 1378341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1379341ebc2fSJohn Snow if (!bitmap) { 1380341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1381341ebc2fSJohn Snow goto fail; 1382341ebc2fSJohn Snow } 1383341ebc2fSJohn Snow 1384341ebc2fSJohn Snow if (pbs) { 1385341ebc2fSJohn Snow *pbs = bs; 1386341ebc2fSJohn Snow } 1387341ebc2fSJohn Snow if (paio) { 1388341ebc2fSJohn Snow *paio = aio_context; 1389341ebc2fSJohn Snow } else { 1390341ebc2fSJohn Snow aio_context_release(aio_context); 1391341ebc2fSJohn Snow } 1392341ebc2fSJohn Snow 1393341ebc2fSJohn Snow return bitmap; 1394341ebc2fSJohn Snow 1395341ebc2fSJohn Snow fail: 1396341ebc2fSJohn Snow aio_context_release(aio_context); 1397341ebc2fSJohn Snow return NULL; 1398341ebc2fSJohn Snow } 1399341ebc2fSJohn Snow 1400b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1401ba0c86a3SWenchao Xia 140250f43f0fSJohn Snow typedef struct BlkActionState BlkActionState; 1403ba0c86a3SWenchao Xia 140450f43f0fSJohn Snow /** 140550f43f0fSJohn Snow * BlkActionOps: 140650f43f0fSJohn Snow * Table of operations that define an Action. 140750f43f0fSJohn Snow * 140850f43f0fSJohn Snow * @instance_size: Size of state struct, in bytes. 140950f43f0fSJohn Snow * @prepare: Prepare the work, must NOT be NULL. 141050f43f0fSJohn Snow * @commit: Commit the changes, can be NULL. 141150f43f0fSJohn Snow * @abort: Abort the changes on fail, can be NULL. 141250f43f0fSJohn Snow * @clean: Clean up resources after all transaction actions have called 141350f43f0fSJohn Snow * commit() or abort(). Can be NULL. 141450f43f0fSJohn Snow * 141550f43f0fSJohn Snow * Only prepare() may fail. In a single transaction, only one of commit() or 141650f43f0fSJohn Snow * abort() will be called. clean() will always be called if it is present. 1417ba0c86a3SWenchao Xia */ 141850f43f0fSJohn Snow typedef struct BlkActionOps { 141950f43f0fSJohn Snow size_t instance_size; 142050f43f0fSJohn Snow void (*prepare)(BlkActionState *common, Error **errp); 142150f43f0fSJohn Snow void (*commit)(BlkActionState *common); 142250f43f0fSJohn Snow void (*abort)(BlkActionState *common); 142350f43f0fSJohn Snow void (*clean)(BlkActionState *common); 142450f43f0fSJohn Snow } BlkActionOps; 142550f43f0fSJohn Snow 142650f43f0fSJohn Snow /** 142750f43f0fSJohn Snow * BlkActionState: 142850f43f0fSJohn Snow * Describes one Action's state within a Transaction. 142950f43f0fSJohn Snow * 143050f43f0fSJohn Snow * @action: QAPI-defined enum identifying which Action to perform. 143150f43f0fSJohn Snow * @ops: Table of ActionOps this Action can perform. 143294d16a64SJohn Snow * @block_job_txn: Transaction which this action belongs to. 143350f43f0fSJohn Snow * @entry: List membership for all Actions in this Transaction. 143450f43f0fSJohn Snow * 143550f43f0fSJohn Snow * This structure must be arranged as first member in a subclassed type, 143650f43f0fSJohn Snow * assuming that the compiler will also arrange it to the same offsets as the 143750f43f0fSJohn Snow * base class. 143850f43f0fSJohn Snow */ 143950f43f0fSJohn Snow struct BlkActionState { 1440c8a83e85SKevin Wolf TransactionAction *action; 144150f43f0fSJohn Snow const BlkActionOps *ops; 144294d16a64SJohn Snow BlockJobTxn *block_job_txn; 144394d16a64SJohn Snow TransactionProperties *txn_props; 144450f43f0fSJohn Snow QSIMPLEQ_ENTRY(BlkActionState) entry; 1445ba0c86a3SWenchao Xia }; 1446ba0c86a3SWenchao Xia 1447bbe86010SWenchao Xia /* internal snapshot private data */ 1448bbe86010SWenchao Xia typedef struct InternalSnapshotState { 144950f43f0fSJohn Snow BlkActionState common; 1450bbe86010SWenchao Xia BlockDriverState *bs; 14515d6e96efSStefan Hajnoczi AioContext *aio_context; 1452bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1453507306ccSFam Zheng bool created; 1454bbe86010SWenchao Xia } InternalSnapshotState; 1455bbe86010SWenchao Xia 145694d16a64SJohn Snow 145794d16a64SJohn Snow static int action_check_completion_mode(BlkActionState *s, Error **errp) 145894d16a64SJohn Snow { 145994d16a64SJohn Snow if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 146094d16a64SJohn Snow error_setg(errp, 146194d16a64SJohn Snow "Action '%s' does not support Transaction property " 146294d16a64SJohn Snow "completion-mode = %s", 146394d16a64SJohn Snow TransactionActionKind_lookup[s->action->type], 146494d16a64SJohn Snow ActionCompletionMode_lookup[s->txn_props->completion_mode]); 146594d16a64SJohn Snow return -1; 146694d16a64SJohn Snow } 146794d16a64SJohn Snow return 0; 146894d16a64SJohn Snow } 146994d16a64SJohn Snow 147050f43f0fSJohn Snow static void internal_snapshot_prepare(BlkActionState *common, 1471bbe86010SWenchao Xia Error **errp) 1472bbe86010SWenchao Xia { 1473f70edf99SMarkus Armbruster Error *local_err = NULL; 1474bbe86010SWenchao Xia const char *device; 1475bbe86010SWenchao Xia const char *name; 1476a0e8544cSFam Zheng BlockBackend *blk; 1477bbe86010SWenchao Xia BlockDriverState *bs; 1478bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1479bbe86010SWenchao Xia bool ret; 1480bbe86010SWenchao Xia qemu_timeval tv; 1481bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1482bbe86010SWenchao Xia InternalSnapshotState *state; 1483bbe86010SWenchao Xia int ret1; 1484bbe86010SWenchao Xia 14856a8f9661SEric Blake g_assert(common->action->type == 1486bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 14876a8f9661SEric Blake internal = common->action->u.blockdev_snapshot_internal_sync; 1488bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1489bbe86010SWenchao Xia 1490bbe86010SWenchao Xia /* 1. parse input */ 1491bbe86010SWenchao Xia device = internal->device; 1492bbe86010SWenchao Xia name = internal->name; 1493bbe86010SWenchao Xia 1494bbe86010SWenchao Xia /* 2. check for validation */ 149594d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 149694d16a64SJohn Snow return; 149794d16a64SJohn Snow } 149894d16a64SJohn Snow 1499a0e8544cSFam Zheng blk = blk_by_name(device); 1500a0e8544cSFam Zheng if (!blk) { 150175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 150275158ebbSMarkus Armbruster "Device '%s' not found", device); 1503bbe86010SWenchao Xia return; 1504bbe86010SWenchao Xia } 1505bbe86010SWenchao Xia 15065d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 15075433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 15085d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 15095d6e96efSStefan Hajnoczi 15105433c24fSMax Reitz if (!blk_is_available(blk)) { 1511c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1512bbe86010SWenchao Xia return; 1513bbe86010SWenchao Xia } 15145433c24fSMax Reitz bs = blk_bs(blk); 1515bbe86010SWenchao Xia 1516507306ccSFam Zheng state->bs = bs; 1517507306ccSFam Zheng bdrv_drained_begin(bs); 1518507306ccSFam Zheng 15193dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 15203dc7ca3cSStefan Hajnoczi return; 15213dc7ca3cSStefan Hajnoczi } 15223dc7ca3cSStefan Hajnoczi 1523bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 152481e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1525bbe86010SWenchao Xia return; 1526bbe86010SWenchao Xia } 1527bbe86010SWenchao Xia 1528bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 152981e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 153081e5f78aSAlberto Garcia "does not support internal snapshots", 153181e5f78aSAlberto Garcia bs->drv->format_name, device); 1532bbe86010SWenchao Xia return; 1533bbe86010SWenchao Xia } 1534bbe86010SWenchao Xia 1535bbe86010SWenchao Xia if (!strlen(name)) { 1536bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1537bbe86010SWenchao Xia return; 1538bbe86010SWenchao Xia } 1539bbe86010SWenchao Xia 1540bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1541f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1542f70edf99SMarkus Armbruster &local_err); 1543f70edf99SMarkus Armbruster if (local_err) { 1544f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1545bbe86010SWenchao Xia return; 1546bbe86010SWenchao Xia } else if (ret) { 1547bbe86010SWenchao Xia error_setg(errp, 1548bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1549bbe86010SWenchao Xia name, device); 1550bbe86010SWenchao Xia return; 1551bbe86010SWenchao Xia } 1552bbe86010SWenchao Xia 1553bbe86010SWenchao Xia /* 3. take the snapshot */ 1554bbe86010SWenchao Xia sn = &state->sn; 1555bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1556bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1557bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1558bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1559bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1560bbe86010SWenchao Xia 1561bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1562bbe86010SWenchao Xia if (ret1 < 0) { 1563bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1564bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1565bbe86010SWenchao Xia name, device); 1566bbe86010SWenchao Xia return; 1567bbe86010SWenchao Xia } 1568bbe86010SWenchao Xia 1569bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1570507306ccSFam Zheng state->created = true; 1571bbe86010SWenchao Xia } 1572bbe86010SWenchao Xia 157350f43f0fSJohn Snow static void internal_snapshot_abort(BlkActionState *common) 1574bbe86010SWenchao Xia { 1575bbe86010SWenchao Xia InternalSnapshotState *state = 1576bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1577bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1578bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1579bbe86010SWenchao Xia Error *local_error = NULL; 1580bbe86010SWenchao Xia 1581507306ccSFam Zheng if (!state->created) { 1582bbe86010SWenchao Xia return; 1583bbe86010SWenchao Xia } 1584bbe86010SWenchao Xia 1585bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1586bbe86010SWenchao Xia error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1587bbe86010SWenchao Xia "device '%s' in abort: %s", 1588bbe86010SWenchao Xia sn->id_str, 1589bbe86010SWenchao Xia sn->name, 1590bbe86010SWenchao Xia bdrv_get_device_name(bs), 1591bbe86010SWenchao Xia error_get_pretty(local_error)); 1592bbe86010SWenchao Xia error_free(local_error); 1593bbe86010SWenchao Xia } 1594bbe86010SWenchao Xia } 1595bbe86010SWenchao Xia 159650f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 15975d6e96efSStefan Hajnoczi { 15985d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15995d6e96efSStefan Hajnoczi common, common); 16005d6e96efSStefan Hajnoczi 16015d6e96efSStefan Hajnoczi if (state->aio_context) { 1602507306ccSFam Zheng if (state->bs) { 1603507306ccSFam Zheng bdrv_drained_end(state->bs); 1604507306ccSFam Zheng } 16055d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16065d6e96efSStefan Hajnoczi } 16075d6e96efSStefan Hajnoczi } 16085d6e96efSStefan Hajnoczi 1609ba0c86a3SWenchao Xia /* external snapshot private data */ 1610ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 161150f43f0fSJohn Snow BlkActionState common; 16128802d1fdSJeff Cody BlockDriverState *old_bs; 16138802d1fdSJeff Cody BlockDriverState *new_bs; 16145d6e96efSStefan Hajnoczi AioContext *aio_context; 1615ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16168802d1fdSJeff Cody 161750f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16189b9877eeSWenchao Xia Error **errp) 16199b9877eeSWenchao Xia { 162043de7e2dSAlberto Garcia int flags = 0, ret; 162143de7e2dSAlberto Garcia QDict *options = NULL; 16229b9877eeSWenchao Xia Error *local_err = NULL; 162343de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1624e2a31e87SWenchao Xia const char *device; 16250901f67eSBenoît Canet const char *node_name; 162643de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 162743de7e2dSAlberto Garcia const char *snapshot_ref; 162843de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1629e2a31e87SWenchao Xia const char *new_image_file; 1630ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1631ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1632c8a83e85SKevin Wolf TransactionAction *action = common->action; 16339b9877eeSWenchao Xia 163443de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 163543de7e2dSAlberto Garcia * purpose but a different set of parameters */ 163643de7e2dSAlberto Garcia switch (action->type) { 163743de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 163843de7e2dSAlberto Garcia { 163943de7e2dSAlberto Garcia BlockdevSnapshot *s = action->u.blockdev_snapshot; 164043de7e2dSAlberto Garcia device = s->node; 164143de7e2dSAlberto Garcia node_name = s->node; 164243de7e2dSAlberto Garcia new_image_file = NULL; 164343de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1644e2a31e87SWenchao Xia } 164543de7e2dSAlberto Garcia break; 164643de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 164743de7e2dSAlberto Garcia { 164843de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 164943de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 165043de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 165143de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 165243de7e2dSAlberto Garcia snapshot_ref = NULL; 165343de7e2dSAlberto Garcia } 165443de7e2dSAlberto Garcia break; 165543de7e2dSAlberto Garcia default: 165643de7e2dSAlberto Garcia g_assert_not_reached(); 1657e2a31e87SWenchao Xia } 1658e2a31e87SWenchao Xia 1659e2a31e87SWenchao Xia /* start processing */ 166094d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 166194d16a64SJohn Snow return; 166294d16a64SJohn Snow } 166394d16a64SJohn Snow 166443de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 166543de7e2dSAlberto Garcia if (!state->old_bs) { 16669b9877eeSWenchao Xia return; 16679b9877eeSWenchao Xia } 16689b9877eeSWenchao Xia 16695d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16705d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16715d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1672da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16735d6e96efSStefan Hajnoczi 1674ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1675c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16769b9877eeSWenchao Xia return; 16779b9877eeSWenchao Xia } 16789b9877eeSWenchao Xia 16793718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16803718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16819b9877eeSWenchao Xia return; 16829b9877eeSWenchao Xia } 16839b9877eeSWenchao Xia 1684ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1685ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1686c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16879b9877eeSWenchao Xia return; 16889b9877eeSWenchao Xia } 16899b9877eeSWenchao Xia } 16909b9877eeSWenchao Xia 1691212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1692c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1693f6186f49SBenoît Canet return; 1694f6186f49SBenoît Canet } 1695f6186f49SBenoît Canet 169643de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 169743de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 169843de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 169943de7e2dSAlberto Garcia enum NewImageMode mode; 170043de7e2dSAlberto Garcia const char *snapshot_node_name = 170143de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 170243de7e2dSAlberto Garcia 170343de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 170443de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 170543de7e2dSAlberto Garcia return; 170643de7e2dSAlberto Garcia } 170743de7e2dSAlberto Garcia 170843de7e2dSAlberto Garcia if (snapshot_node_name && 170943de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 171043de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 171143de7e2dSAlberto Garcia return; 171243de7e2dSAlberto Garcia } 171343de7e2dSAlberto Garcia 1714ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17159b9877eeSWenchao Xia 17169b9877eeSWenchao Xia /* create new image w/backing file */ 171743de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17189b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 17199b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1720ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1721ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17229b9877eeSWenchao Xia NULL, -1, flags, &local_err, false); 172384d18f06SMarkus Armbruster if (local_err) { 17249b9877eeSWenchao Xia error_propagate(errp, local_err); 17259b9877eeSWenchao Xia return; 17269b9877eeSWenchao Xia } 17279b9877eeSWenchao Xia } 17289b9877eeSWenchao Xia 17290901f67eSBenoît Canet options = qdict_new(); 173043de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17310901f67eSBenoît Canet qdict_put(options, "node-name", 17320901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17330901f67eSBenoît Canet } 1734e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17350901f67eSBenoît Canet 173643de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 173743de7e2dSAlberto Garcia } 173843de7e2dSAlberto Garcia 1739f67503e5SMax Reitz assert(state->new_bs == NULL); 174043de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 174143de7e2dSAlberto Garcia flags, errp); 1742f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17439b9877eeSWenchao Xia if (ret != 0) { 174443de7e2dSAlberto Garcia return; 174543de7e2dSAlberto Garcia } 174643de7e2dSAlberto Garcia 174743de7e2dSAlberto Garcia if (state->new_bs->blk != NULL) { 174843de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 174943de7e2dSAlberto Garcia blk_name(state->new_bs->blk)); 175043de7e2dSAlberto Garcia return; 175143de7e2dSAlberto Garcia } 175243de7e2dSAlberto Garcia 175343de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 175443de7e2dSAlberto Garcia errp)) { 175543de7e2dSAlberto Garcia return; 175643de7e2dSAlberto Garcia } 175743de7e2dSAlberto Garcia 175843de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 175943de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 176008b24cfeSAlberto Garcia return; 176108b24cfeSAlberto Garcia } 176208b24cfeSAlberto Garcia 176308b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 176408b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17659b9877eeSWenchao Xia } 17669b9877eeSWenchao Xia } 17679b9877eeSWenchao Xia 176850f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17693b0047e8SWenchao Xia { 1770ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1771ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1772ba0c86a3SWenchao Xia 17735d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 17745d6e96efSStefan Hajnoczi 1775ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1776ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 17773b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17783b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17793b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1780dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17813b0047e8SWenchao Xia NULL); 17823b0047e8SWenchao Xia } 17833b0047e8SWenchao Xia 178450f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 178596b86bf7SWenchao Xia { 1786ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1787ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1788ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 17894f6fd349SFam Zheng bdrv_unref(state->new_bs); 179096b86bf7SWenchao Xia } 1791da763e83SFam Zheng } 1792da763e83SFam Zheng 179350f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1794da763e83SFam Zheng { 1795da763e83SFam Zheng ExternalSnapshotState *state = 1796da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17975d6e96efSStefan Hajnoczi if (state->aio_context) { 1798da763e83SFam Zheng bdrv_drained_end(state->old_bs); 17995d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18005d6e96efSStefan Hajnoczi } 180196b86bf7SWenchao Xia } 180296b86bf7SWenchao Xia 18033037f364SStefan Hajnoczi typedef struct DriveBackupState { 180450f43f0fSJohn Snow BlkActionState common; 18053037f364SStefan Hajnoczi BlockDriverState *bs; 18065d6e96efSStefan Hajnoczi AioContext *aio_context; 18073037f364SStefan Hajnoczi BlockJob *job; 18083037f364SStefan Hajnoczi } DriveBackupState; 18093037f364SStefan Hajnoczi 181078f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 181178f51fdeSJohn Snow bool has_format, const char *format, 181278f51fdeSJohn Snow enum MirrorSyncMode sync, 181378f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 181478f51fdeSJohn Snow bool has_speed, int64_t speed, 181578f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 181678f51fdeSJohn Snow bool has_on_source_error, 181778f51fdeSJohn Snow BlockdevOnError on_source_error, 181878f51fdeSJohn Snow bool has_on_target_error, 181978f51fdeSJohn Snow BlockdevOnError on_target_error, 182078f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 182178f51fdeSJohn Snow 182250f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18233037f364SStefan Hajnoczi { 18243037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1825a0e8544cSFam Zheng BlockBackend *blk; 18263037f364SStefan Hajnoczi DriveBackup *backup; 18273037f364SStefan Hajnoczi Error *local_err = NULL; 18283037f364SStefan Hajnoczi 18296a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 18306a8f9661SEric Blake backup = common->action->u.drive_backup; 18313037f364SStefan Hajnoczi 1832a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1833a0e8544cSFam Zheng if (!blk) { 183475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 183575158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18365d6e96efSStefan Hajnoczi return; 18375d6e96efSStefan Hajnoczi } 18385d6e96efSStefan Hajnoczi 18391fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18401fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18411fdd4b7bSFam Zheng return; 18421fdd4b7bSFam Zheng } 18431fdd4b7bSFam Zheng 18445d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18455433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18465d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18471fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18481fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18495d6e96efSStefan Hajnoczi 185078f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18513037f364SStefan Hajnoczi backup->has_format, backup->format, 1852b53169eaSStefan Hajnoczi backup->sync, 18533037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18543037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1855d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18563037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18573037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 185894d16a64SJohn Snow common->block_job_txn, &local_err); 185984d18f06SMarkus Armbruster if (local_err) { 18603037f364SStefan Hajnoczi error_propagate(errp, local_err); 18613037f364SStefan Hajnoczi return; 18623037f364SStefan Hajnoczi } 18633037f364SStefan Hajnoczi 18643037f364SStefan Hajnoczi state->job = state->bs->job; 18653037f364SStefan Hajnoczi } 18663037f364SStefan Hajnoczi 186750f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18683037f364SStefan Hajnoczi { 18693037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18703037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 18713037f364SStefan Hajnoczi 18723037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 18733037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 18743037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 18753037f364SStefan Hajnoczi } 18763037f364SStefan Hajnoczi } 18773037f364SStefan Hajnoczi 187850f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18795d6e96efSStefan Hajnoczi { 18805d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18815d6e96efSStefan Hajnoczi 18825d6e96efSStefan Hajnoczi if (state->aio_context) { 18831fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 18845d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18855d6e96efSStefan Hajnoczi } 18865d6e96efSStefan Hajnoczi } 18875d6e96efSStefan Hajnoczi 1888bd8baecdSFam Zheng typedef struct BlockdevBackupState { 188950f43f0fSJohn Snow BlkActionState common; 1890bd8baecdSFam Zheng BlockDriverState *bs; 1891bd8baecdSFam Zheng BlockJob *job; 1892bd8baecdSFam Zheng AioContext *aio_context; 1893bd8baecdSFam Zheng } BlockdevBackupState; 1894bd8baecdSFam Zheng 189578f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 189678f51fdeSJohn Snow enum MirrorSyncMode sync, 189778f51fdeSJohn Snow bool has_speed, int64_t speed, 189878f51fdeSJohn Snow bool has_on_source_error, 189978f51fdeSJohn Snow BlockdevOnError on_source_error, 190078f51fdeSJohn Snow bool has_on_target_error, 190178f51fdeSJohn Snow BlockdevOnError on_target_error, 190278f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 190378f51fdeSJohn Snow 190450f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1905bd8baecdSFam Zheng { 1906bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1907bd8baecdSFam Zheng BlockdevBackup *backup; 19085433c24fSMax Reitz BlockBackend *blk, *target; 1909bd8baecdSFam Zheng Error *local_err = NULL; 1910bd8baecdSFam Zheng 19116a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 19126a8f9661SEric Blake backup = common->action->u.blockdev_backup; 1913bd8baecdSFam Zheng 1914a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1915a0e8544cSFam Zheng if (!blk) { 19165b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1917bd8baecdSFam Zheng return; 1918bd8baecdSFam Zheng } 1919bd8baecdSFam Zheng 1920ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1921ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1922ff52bf36SFam Zheng return; 1923ff52bf36SFam Zheng } 1924ff52bf36SFam Zheng 19255433c24fSMax Reitz target = blk_by_name(backup->target); 19265433c24fSMax Reitz if (!target) { 19275b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1928bd8baecdSFam Zheng return; 1929bd8baecdSFam Zheng } 1930bd8baecdSFam Zheng 1931bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19325433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19335433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1934bd8baecdSFam Zheng state->aio_context = NULL; 1935bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1936bd8baecdSFam Zheng return; 1937bd8baecdSFam Zheng } 1938bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1939ff52bf36SFam Zheng state->bs = blk_bs(blk); 1940ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1941bd8baecdSFam Zheng 194278f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1943bd8baecdSFam Zheng backup->sync, 1944bd8baecdSFam Zheng backup->has_speed, backup->speed, 1945bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1946bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 194794d16a64SJohn Snow common->block_job_txn, &local_err); 1948bd8baecdSFam Zheng if (local_err) { 1949bd8baecdSFam Zheng error_propagate(errp, local_err); 1950bd8baecdSFam Zheng return; 1951bd8baecdSFam Zheng } 1952bd8baecdSFam Zheng 1953bd8baecdSFam Zheng state->job = state->bs->job; 1954bd8baecdSFam Zheng } 1955bd8baecdSFam Zheng 195650f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1957bd8baecdSFam Zheng { 1958bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1959bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1960bd8baecdSFam Zheng 1961bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1962bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1963bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1964bd8baecdSFam Zheng } 1965bd8baecdSFam Zheng } 1966bd8baecdSFam Zheng 196750f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1968bd8baecdSFam Zheng { 1969bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1970bd8baecdSFam Zheng 1971bd8baecdSFam Zheng if (state->aio_context) { 1972ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1973bd8baecdSFam Zheng aio_context_release(state->aio_context); 1974bd8baecdSFam Zheng } 1975bd8baecdSFam Zheng } 1976bd8baecdSFam Zheng 1977df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 197850f43f0fSJohn Snow BlkActionState common; 1979df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1980df9a681dSFam Zheng BlockDriverState *bs; 1981df9a681dSFam Zheng AioContext *aio_context; 1982df9a681dSFam Zheng HBitmap *backup; 1983df9a681dSFam Zheng bool prepared; 1984df9a681dSFam Zheng } BlockDirtyBitmapState; 1985df9a681dSFam Zheng 198650f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1987df9a681dSFam Zheng Error **errp) 1988df9a681dSFam Zheng { 1989df9a681dSFam Zheng Error *local_err = NULL; 1990df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1991df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1992df9a681dSFam Zheng common, common); 1993df9a681dSFam Zheng 199494d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 199594d16a64SJohn Snow return; 199694d16a64SJohn Snow } 199794d16a64SJohn Snow 199850f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 1999df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 2000df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 2001df9a681dSFam Zheng action->has_granularity, action->granularity, 2002df9a681dSFam Zheng &local_err); 2003df9a681dSFam Zheng 2004df9a681dSFam Zheng if (!local_err) { 2005df9a681dSFam Zheng state->prepared = true; 2006df9a681dSFam Zheng } else { 2007df9a681dSFam Zheng error_propagate(errp, local_err); 2008df9a681dSFam Zheng } 2009df9a681dSFam Zheng } 2010df9a681dSFam Zheng 201150f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2012df9a681dSFam Zheng { 2013df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2014df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2015df9a681dSFam Zheng common, common); 2016df9a681dSFam Zheng 201750f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 2018df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2019df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2020df9a681dSFam Zheng */ 2021df9a681dSFam Zheng if (state->prepared) { 2022df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2023df9a681dSFam Zheng } 2024df9a681dSFam Zheng } 2025df9a681dSFam Zheng 202650f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2027df9a681dSFam Zheng Error **errp) 2028df9a681dSFam Zheng { 2029df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2030df9a681dSFam Zheng common, common); 2031df9a681dSFam Zheng BlockDirtyBitmap *action; 2032df9a681dSFam Zheng 203394d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 203494d16a64SJohn Snow return; 203594d16a64SJohn Snow } 203694d16a64SJohn Snow 203750f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_clear; 2038df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2039df9a681dSFam Zheng action->name, 2040df9a681dSFam Zheng &state->bs, 2041df9a681dSFam Zheng &state->aio_context, 2042df9a681dSFam Zheng errp); 2043df9a681dSFam Zheng if (!state->bitmap) { 2044df9a681dSFam Zheng return; 2045df9a681dSFam Zheng } 2046df9a681dSFam Zheng 2047df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2048df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2049df9a681dSFam Zheng return; 2050df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2051df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2052df9a681dSFam Zheng return; 2053df9a681dSFam Zheng } 2054df9a681dSFam Zheng 2055df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2056df9a681dSFam Zheng /* AioContext is released in .clean() */ 2057df9a681dSFam Zheng } 2058df9a681dSFam Zheng 205950f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2060df9a681dSFam Zheng { 2061df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2062df9a681dSFam Zheng common, common); 2063df9a681dSFam Zheng 2064df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2065df9a681dSFam Zheng } 2066df9a681dSFam Zheng 206750f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2068df9a681dSFam Zheng { 2069df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2070df9a681dSFam Zheng common, common); 2071df9a681dSFam Zheng 2072df9a681dSFam Zheng hbitmap_free(state->backup); 2073df9a681dSFam Zheng } 2074df9a681dSFam Zheng 207550f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2076df9a681dSFam Zheng { 2077df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2078df9a681dSFam Zheng common, common); 2079df9a681dSFam Zheng 2080df9a681dSFam Zheng if (state->aio_context) { 2081df9a681dSFam Zheng aio_context_release(state->aio_context); 2082df9a681dSFam Zheng } 2083df9a681dSFam Zheng } 2084df9a681dSFam Zheng 208550f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 208678b18b78SStefan Hajnoczi { 208778b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 208878b18b78SStefan Hajnoczi } 208978b18b78SStefan Hajnoczi 209050f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 209178b18b78SStefan Hajnoczi { 2092dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 209378b18b78SStefan Hajnoczi } 209478b18b78SStefan Hajnoczi 209550f43f0fSJohn Snow static const BlkActionOps actions[] = { 209643de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 209743de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 209843de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 209943de7e2dSAlberto Garcia .commit = external_snapshot_commit, 210043de7e2dSAlberto Garcia .abort = external_snapshot_abort, 21014ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 210243de7e2dSAlberto Garcia }, 2103c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2104ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2105ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2106ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2107ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2108da763e83SFam Zheng .clean = external_snapshot_clean, 2109ba0c86a3SWenchao Xia }, 21103037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21113037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21123037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21133037f364SStefan Hajnoczi .abort = drive_backup_abort, 21145d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21153037f364SStefan Hajnoczi }, 2116bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2117bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2118bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2119bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2120bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2121bd8baecdSFam Zheng }, 212278b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 212350f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 212478b18b78SStefan Hajnoczi .prepare = abort_prepare, 212578b18b78SStefan Hajnoczi .commit = abort_commit, 212678b18b78SStefan Hajnoczi }, 2127bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2128bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2129bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2130bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21315d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2132bbe86010SWenchao Xia }, 2133df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2134df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2135df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2136df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2137df9a681dSFam Zheng }, 2138df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2139df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2140df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2141df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2142df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2143df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2144df9a681dSFam Zheng } 2145ba0c86a3SWenchao Xia }; 2146ba0c86a3SWenchao Xia 214794d16a64SJohn Snow /** 214894d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 214994d16a64SJohn Snow * that structure with desired defaults if they are unset. 215094d16a64SJohn Snow */ 215194d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 215294d16a64SJohn Snow TransactionProperties *props) 215394d16a64SJohn Snow { 215494d16a64SJohn Snow if (!props) { 215594d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 215694d16a64SJohn Snow } 215794d16a64SJohn Snow 215894d16a64SJohn Snow if (!props->has_completion_mode) { 215994d16a64SJohn Snow props->has_completion_mode = true; 216094d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 216194d16a64SJohn Snow } 216294d16a64SJohn Snow 216394d16a64SJohn Snow return props; 216494d16a64SJohn Snow } 216594d16a64SJohn Snow 21668802d1fdSJeff Cody /* 2167b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2168b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21698802d1fdSJeff Cody */ 217094d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 217194d16a64SJohn Snow bool has_props, 217294d16a64SJohn Snow struct TransactionProperties *props, 217394d16a64SJohn Snow Error **errp) 21748802d1fdSJeff Cody { 2175c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 217694d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 217750f43f0fSJohn Snow BlkActionState *state, *next; 217843e17041SLuiz Capitulino Error *local_err = NULL; 21798802d1fdSJeff Cody 218050f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 21818802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 21828802d1fdSJeff Cody 218394d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 218494d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 218594d16a64SJohn Snow */ 218694d16a64SJohn Snow props = get_transaction_properties(props); 218794d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 218894d16a64SJohn Snow block_job_txn = block_job_txn_new(); 218994d16a64SJohn Snow } 219094d16a64SJohn Snow 2191b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 21928802d1fdSJeff Cody bdrv_drain_all(); 21938802d1fdSJeff Cody 2194b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 21958802d1fdSJeff Cody while (NULL != dev_entry) { 2196c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 219750f43f0fSJohn Snow const BlkActionOps *ops; 219852e7c241SPaolo Bonzini 21998802d1fdSJeff Cody dev_info = dev_entry->value; 22008802d1fdSJeff Cody dev_entry = dev_entry->next; 22018802d1fdSJeff Cody 22026a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2203ba0c86a3SWenchao Xia 22046a8f9661SEric Blake ops = &actions[dev_info->type]; 2205aa3fe714SMax Reitz assert(ops->instance_size > 0); 2206aa3fe714SMax Reitz 2207ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2208ba5d6ab6SStefan Hajnoczi state->ops = ops; 2209ba5d6ab6SStefan Hajnoczi state->action = dev_info; 221094d16a64SJohn Snow state->block_job_txn = block_job_txn; 221194d16a64SJohn Snow state->txn_props = props; 2212ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22138802d1fdSJeff Cody 2214ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 221584d18f06SMarkus Armbruster if (local_err) { 22169b9877eeSWenchao Xia error_propagate(errp, local_err); 22179b9877eeSWenchao Xia goto delete_and_fail; 22189b9877eeSWenchao Xia } 221952e7c241SPaolo Bonzini } 22208802d1fdSJeff Cody 2221ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2222f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2223ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22248802d1fdSJeff Cody } 2225f9ea81e8SStefan Hajnoczi } 22268802d1fdSJeff Cody 22278802d1fdSJeff Cody /* success */ 22288802d1fdSJeff Cody goto exit; 22298802d1fdSJeff Cody 22308802d1fdSJeff Cody delete_and_fail: 2231b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2232ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2233ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2234ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2235ba0c86a3SWenchao Xia } 22368802d1fdSJeff Cody } 22378802d1fdSJeff Cody exit: 2238ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2239ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2240ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2241ba0c86a3SWenchao Xia } 2242ba5d6ab6SStefan Hajnoczi g_free(state); 22438802d1fdSJeff Cody } 224494d16a64SJohn Snow if (!has_props) { 224594d16a64SJohn Snow qapi_free_TransactionProperties(props); 224694d16a64SJohn Snow } 224794d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22488802d1fdSJeff Cody } 22498802d1fdSJeff Cody 2250c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2251666daa68SMarkus Armbruster { 225238f54bd1SMax Reitz Error *local_err = NULL; 2253666daa68SMarkus Armbruster 225438f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 225538f54bd1SMax Reitz if (local_err) { 225638f54bd1SMax Reitz error_propagate(errp, local_err); 2257c245b6a3SLuiz Capitulino return; 2258666daa68SMarkus Armbruster } 225992d48558SLuiz Capitulino 2260*6e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2261666daa68SMarkus Armbruster } 2262666daa68SMarkus Armbruster 226312d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 226412d3ba82SBenoît Canet bool has_node_name, const char *node_name, 226512d3ba82SBenoît Canet const char *password, Error **errp) 2266666daa68SMarkus Armbruster { 226712d3ba82SBenoît Canet Error *local_err = NULL; 2268666daa68SMarkus Armbruster BlockDriverState *bs; 2269e3442099SStefan Hajnoczi AioContext *aio_context; 2270666daa68SMarkus Armbruster 227112d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 227212d3ba82SBenoît Canet has_node_name ? node_name : NULL, 227312d3ba82SBenoît Canet &local_err); 227484d18f06SMarkus Armbruster if (local_err) { 227512d3ba82SBenoît Canet error_propagate(errp, local_err); 2276a4dea8a9SLuiz Capitulino return; 2277666daa68SMarkus Armbruster } 2278666daa68SMarkus Armbruster 2279e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2280e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2281e3442099SStefan Hajnoczi 22824d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2283666daa68SMarkus Armbruster 2284e3442099SStefan Hajnoczi aio_context_release(aio_context); 2285e3442099SStefan Hajnoczi } 2286e3442099SStefan Hajnoczi 22877d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 22887d8a9f71SMax Reitz Error **errp) 22897d8a9f71SMax Reitz { 22907d8a9f71SMax Reitz BlockBackend *blk; 22917d8a9f71SMax Reitz bool locked; 22927d8a9f71SMax Reitz 22937d8a9f71SMax Reitz if (!has_force) { 22947d8a9f71SMax Reitz force = false; 22957d8a9f71SMax Reitz } 22967d8a9f71SMax Reitz 22977d8a9f71SMax Reitz blk = blk_by_name(device); 22987d8a9f71SMax Reitz if (!blk) { 22997d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23007d8a9f71SMax Reitz "Device '%s' not found", device); 23017d8a9f71SMax Reitz return; 23027d8a9f71SMax Reitz } 23037d8a9f71SMax Reitz 23047d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23057d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23067d8a9f71SMax Reitz return; 23077d8a9f71SMax Reitz } 23087d8a9f71SMax Reitz 23097d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23107d8a9f71SMax Reitz return; 23117d8a9f71SMax Reitz } 23127d8a9f71SMax Reitz 23137d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23147d8a9f71SMax Reitz if (locked) { 23157d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23167d8a9f71SMax Reitz } 23177d8a9f71SMax Reitz 23187d8a9f71SMax Reitz if (!locked || force) { 23197d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23207d8a9f71SMax Reitz } 23217d8a9f71SMax Reitz } 23227d8a9f71SMax Reitz 2323abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2324abaaf59dSMax Reitz { 2325abaaf59dSMax Reitz BlockBackend *blk; 2326abaaf59dSMax Reitz 2327abaaf59dSMax Reitz blk = blk_by_name(device); 2328abaaf59dSMax Reitz if (!blk) { 2329abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2330abaaf59dSMax Reitz "Device '%s' not found", device); 2331abaaf59dSMax Reitz return; 2332abaaf59dSMax Reitz } 2333abaaf59dSMax Reitz 2334abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2335abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2336abaaf59dSMax Reitz return; 2337abaaf59dSMax Reitz } 2338abaaf59dSMax Reitz 2339abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2340abaaf59dSMax Reitz return; 2341abaaf59dSMax Reitz } 2342abaaf59dSMax Reitz 2343abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2344abaaf59dSMax Reitz } 2345abaaf59dSMax Reitz 2346*6e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23472814f672SMax Reitz { 23482814f672SMax Reitz BlockBackend *blk; 23492814f672SMax Reitz BlockDriverState *bs; 23502814f672SMax Reitz AioContext *aio_context; 23512814f672SMax Reitz bool has_device; 23522814f672SMax Reitz 23532814f672SMax Reitz blk = blk_by_name(device); 23542814f672SMax Reitz if (!blk) { 23552814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23562814f672SMax Reitz "Device '%s' not found", device); 23572814f672SMax Reitz return; 23582814f672SMax Reitz } 23592814f672SMax Reitz 23602814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 23612814f672SMax Reitz has_device = blk_get_attached_dev(blk); 23622814f672SMax Reitz 23632814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 23642814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23652814f672SMax Reitz return; 23662814f672SMax Reitz } 23672814f672SMax Reitz 23682814f672SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 23692814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 23702814f672SMax Reitz return; 23712814f672SMax Reitz } 23722814f672SMax Reitz 23732814f672SMax Reitz bs = blk_bs(blk); 23742814f672SMax Reitz if (!bs) { 23752814f672SMax Reitz return; 23762814f672SMax Reitz } 23772814f672SMax Reitz 23782814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 23792814f672SMax Reitz aio_context_acquire(aio_context); 23802814f672SMax Reitz 23812814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 23822814f672SMax Reitz goto out; 23832814f672SMax Reitz } 23842814f672SMax Reitz 23852814f672SMax Reitz /* This follows the convention established by bdrv_make_anon() */ 23862814f672SMax Reitz if (bs->device_list.tqe_prev) { 23872814f672SMax Reitz QTAILQ_REMOVE(&bdrv_states, bs, device_list); 23882814f672SMax Reitz bs->device_list.tqe_prev = NULL; 23892814f672SMax Reitz } 23902814f672SMax Reitz 23912814f672SMax Reitz blk_remove_bs(blk); 23922814f672SMax Reitz 23932814f672SMax Reitz out: 23942814f672SMax Reitz aio_context_release(aio_context); 23952814f672SMax Reitz } 23962814f672SMax Reitz 2397d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2398d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2399d1299882SMax Reitz { 2400d1299882SMax Reitz BlockBackend *blk; 2401d1299882SMax Reitz bool has_device; 2402d1299882SMax Reitz 2403d1299882SMax Reitz blk = blk_by_name(device); 2404d1299882SMax Reitz if (!blk) { 2405d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2406d1299882SMax Reitz "Device '%s' not found", device); 2407d1299882SMax Reitz return; 2408d1299882SMax Reitz } 2409d1299882SMax Reitz 2410d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2411d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2412d1299882SMax Reitz 2413d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2414d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2415d1299882SMax Reitz return; 2416d1299882SMax Reitz } 2417d1299882SMax Reitz 2418d1299882SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 2419d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2420d1299882SMax Reitz return; 2421d1299882SMax Reitz } 2422d1299882SMax Reitz 2423d1299882SMax Reitz if (blk_bs(blk)) { 2424d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2425d1299882SMax Reitz return; 2426d1299882SMax Reitz } 2427d1299882SMax Reitz 2428d1299882SMax Reitz blk_insert_bs(blk, bs); 2429d1299882SMax Reitz 2430d1299882SMax Reitz QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 2431d1299882SMax Reitz } 2432d1299882SMax Reitz 2433*6e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2434d1299882SMax Reitz Error **errp) 2435d1299882SMax Reitz { 2436d1299882SMax Reitz BlockDriverState *bs; 2437d1299882SMax Reitz 2438d1299882SMax Reitz bs = bdrv_find_node(node_name); 2439d1299882SMax Reitz if (!bs) { 2440d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2441d1299882SMax Reitz return; 2442d1299882SMax Reitz } 2443d1299882SMax Reitz 2444d1299882SMax Reitz if (bs->blk) { 2445d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2446d1299882SMax Reitz blk_name(bs->blk)); 2447d1299882SMax Reitz return; 2448d1299882SMax Reitz } 2449d1299882SMax Reitz 2450d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2451d1299882SMax Reitz } 2452d1299882SMax Reitz 245324fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 245424fb4133SMax Reitz bool has_format, const char *format, 245539ff43d9SMax Reitz bool has_read_only, 245639ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 245724fb4133SMax Reitz Error **errp) 2458de2c6c05SMax Reitz { 2459de2c6c05SMax Reitz BlockBackend *blk; 2460de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2461de2c6c05SMax Reitz int bdrv_flags, ret; 2462de2c6c05SMax Reitz QDict *options = NULL; 2463de2c6c05SMax Reitz Error *err = NULL; 2464de2c6c05SMax Reitz 2465de2c6c05SMax Reitz blk = blk_by_name(device); 2466de2c6c05SMax Reitz if (!blk) { 2467de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2468de2c6c05SMax Reitz "Device '%s' not found", device); 2469de2c6c05SMax Reitz goto fail; 2470de2c6c05SMax Reitz } 2471de2c6c05SMax Reitz 2472de2c6c05SMax Reitz if (blk_bs(blk)) { 2473de2c6c05SMax Reitz blk_update_root_state(blk); 2474de2c6c05SMax Reitz } 2475de2c6c05SMax Reitz 2476de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2477de2c6c05SMax Reitz 247839ff43d9SMax Reitz if (!has_read_only) { 247939ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 248039ff43d9SMax Reitz } 248139ff43d9SMax Reitz 248239ff43d9SMax Reitz switch (read_only) { 248339ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 248439ff43d9SMax Reitz break; 248539ff43d9SMax Reitz 248639ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 248739ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 248839ff43d9SMax Reitz break; 248939ff43d9SMax Reitz 249039ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 249139ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 249239ff43d9SMax Reitz break; 249339ff43d9SMax Reitz 249439ff43d9SMax Reitz default: 249539ff43d9SMax Reitz abort(); 249639ff43d9SMax Reitz } 249739ff43d9SMax Reitz 249824fb4133SMax Reitz if (has_format) { 2499de2c6c05SMax Reitz options = qdict_new(); 2500de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2501de2c6c05SMax Reitz } 2502de2c6c05SMax Reitz 2503de2c6c05SMax Reitz assert(!medium_bs); 2504de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2505de2c6c05SMax Reitz if (ret < 0) { 2506de2c6c05SMax Reitz goto fail; 2507de2c6c05SMax Reitz } 2508de2c6c05SMax Reitz 2509de2c6c05SMax Reitz blk_apply_root_state(blk, medium_bs); 2510de2c6c05SMax Reitz 2511de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2512de2c6c05SMax Reitz if (err) { 2513de2c6c05SMax Reitz error_propagate(errp, err); 2514de2c6c05SMax Reitz goto fail; 2515de2c6c05SMax Reitz } 2516de2c6c05SMax Reitz 2517de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2518de2c6c05SMax Reitz if (err) { 2519de2c6c05SMax Reitz error_propagate(errp, err); 2520de2c6c05SMax Reitz goto fail; 2521de2c6c05SMax Reitz } 2522de2c6c05SMax Reitz 2523*6e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2524de2c6c05SMax Reitz if (err) { 2525de2c6c05SMax Reitz error_propagate(errp, err); 2526de2c6c05SMax Reitz goto fail; 2527de2c6c05SMax Reitz } 2528de2c6c05SMax Reitz 2529de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2530de2c6c05SMax Reitz if (err) { 2531de2c6c05SMax Reitz error_propagate(errp, err); 2532de2c6c05SMax Reitz goto fail; 2533de2c6c05SMax Reitz } 2534de2c6c05SMax Reitz 2535de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2536de2c6c05SMax Reitz 2537de2c6c05SMax Reitz fail: 2538de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2539de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2540de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2541de2c6c05SMax Reitz bdrv_unref(medium_bs); 2542de2c6c05SMax Reitz } 2543de2c6c05SMax Reitz 2544727f005eSZhi Yong Wu /* throttling disk I/O limits */ 254580047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 25463e9fab69SBenoît Canet int64_t bps_wr, 25473e9fab69SBenoît Canet int64_t iops, 25483e9fab69SBenoît Canet int64_t iops_rd, 25493e9fab69SBenoît Canet int64_t iops_wr, 25503e9fab69SBenoît Canet bool has_bps_max, 25513e9fab69SBenoît Canet int64_t bps_max, 25523e9fab69SBenoît Canet bool has_bps_rd_max, 25533e9fab69SBenoît Canet int64_t bps_rd_max, 25543e9fab69SBenoît Canet bool has_bps_wr_max, 25553e9fab69SBenoît Canet int64_t bps_wr_max, 25563e9fab69SBenoît Canet bool has_iops_max, 25573e9fab69SBenoît Canet int64_t iops_max, 25583e9fab69SBenoît Canet bool has_iops_rd_max, 25593e9fab69SBenoît Canet int64_t iops_rd_max, 25603e9fab69SBenoît Canet bool has_iops_wr_max, 25612024c1dfSBenoît Canet int64_t iops_wr_max, 25622024c1dfSBenoît Canet bool has_iops_size, 256376f4afb4SAlberto Garcia int64_t iops_size, 256476f4afb4SAlberto Garcia bool has_group, 256576f4afb4SAlberto Garcia const char *group, Error **errp) 2566727f005eSZhi Yong Wu { 2567cc0681c4SBenoît Canet ThrottleConfig cfg; 2568727f005eSZhi Yong Wu BlockDriverState *bs; 2569a0e8544cSFam Zheng BlockBackend *blk; 2570b15446fdSStefan Hajnoczi AioContext *aio_context; 2571727f005eSZhi Yong Wu 2572a0e8544cSFam Zheng blk = blk_by_name(device); 2573a0e8544cSFam Zheng if (!blk) { 257475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 257575158ebbSMarkus Armbruster "Device '%s' not found", device); 257680047da5SLuiz Capitulino return; 2577727f005eSZhi Yong Wu } 25785433c24fSMax Reitz 25795433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 25805433c24fSMax Reitz aio_context_acquire(aio_context); 25815433c24fSMax Reitz 2582a0e8544cSFam Zheng bs = blk_bs(blk); 25835433c24fSMax Reitz if (!bs) { 25845433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 25855433c24fSMax Reitz goto out; 25865433c24fSMax Reitz } 2587727f005eSZhi Yong Wu 2588cc0681c4SBenoît Canet memset(&cfg, 0, sizeof(cfg)); 2589cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2590cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2591cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2592727f005eSZhi Yong Wu 2593cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2594cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2595cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2596cc0681c4SBenoît Canet 25973e9fab69SBenoît Canet if (has_bps_max) { 25983e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 25993e9fab69SBenoît Canet } 26003e9fab69SBenoît Canet if (has_bps_rd_max) { 26013e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26023e9fab69SBenoît Canet } 26033e9fab69SBenoît Canet if (has_bps_wr_max) { 26043e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26053e9fab69SBenoît Canet } 26063e9fab69SBenoît Canet if (has_iops_max) { 26073e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26083e9fab69SBenoît Canet } 26093e9fab69SBenoît Canet if (has_iops_rd_max) { 26103e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26113e9fab69SBenoît Canet } 26123e9fab69SBenoît Canet if (has_iops_wr_max) { 26133e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26143e9fab69SBenoît Canet } 2615cc0681c4SBenoît Canet 26162024c1dfSBenoît Canet if (has_iops_size) { 26172024c1dfSBenoît Canet cfg.op_size = iops_size; 26182024c1dfSBenoît Canet } 2619cc0681c4SBenoît Canet 2620cc0681c4SBenoît Canet if (!check_throttle_config(&cfg, errp)) { 26215433c24fSMax Reitz goto out; 2622727f005eSZhi Yong Wu } 2623727f005eSZhi Yong Wu 262476f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 262576f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 262676f4afb4SAlberto Garcia * just update the throttling group. */ 2627a0d64a61SAlberto Garcia if (!bs->throttle_state) { 262876f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 262976f4afb4SAlberto Garcia } else if (has_group) { 263076f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2631727f005eSZhi Yong Wu } 263276f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2633cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 2634a0d64a61SAlberto Garcia } else if (bs->throttle_state) { 263576f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 263676f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2637727f005eSZhi Yong Wu } 2638b15446fdSStefan Hajnoczi 26395433c24fSMax Reitz out: 2640b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2641727f005eSZhi Yong Wu } 2642727f005eSZhi Yong Wu 2643341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2644341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2645341ebc2fSJohn Snow Error **errp) 2646341ebc2fSJohn Snow { 2647341ebc2fSJohn Snow AioContext *aio_context; 2648341ebc2fSJohn Snow BlockDriverState *bs; 2649341ebc2fSJohn Snow 2650341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2651341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2652341ebc2fSJohn Snow return; 2653341ebc2fSJohn Snow } 2654341ebc2fSJohn Snow 2655341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2656341ebc2fSJohn Snow if (!bs) { 2657341ebc2fSJohn Snow return; 2658341ebc2fSJohn Snow } 2659341ebc2fSJohn Snow 2660341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2661341ebc2fSJohn Snow aio_context_acquire(aio_context); 2662341ebc2fSJohn Snow 2663341ebc2fSJohn Snow if (has_granularity) { 2664341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2665341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2666341ebc2fSJohn Snow "and at least 512"); 2667341ebc2fSJohn Snow goto out; 2668341ebc2fSJohn Snow } 2669341ebc2fSJohn Snow } else { 2670341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2671341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2672341ebc2fSJohn Snow } 2673341ebc2fSJohn Snow 2674341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2675341ebc2fSJohn Snow 2676341ebc2fSJohn Snow out: 2677341ebc2fSJohn Snow aio_context_release(aio_context); 2678341ebc2fSJohn Snow } 2679341ebc2fSJohn Snow 2680341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2681341ebc2fSJohn Snow Error **errp) 2682341ebc2fSJohn Snow { 2683341ebc2fSJohn Snow AioContext *aio_context; 2684341ebc2fSJohn Snow BlockDriverState *bs; 2685341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2686341ebc2fSJohn Snow 2687341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2688341ebc2fSJohn Snow if (!bitmap || !bs) { 2689341ebc2fSJohn Snow return; 2690341ebc2fSJohn Snow } 2691341ebc2fSJohn Snow 26929bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 26939bd2b08fSJohn Snow error_setg(errp, 26949bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 26959bd2b08fSJohn Snow name); 26969bd2b08fSJohn Snow goto out; 26979bd2b08fSJohn Snow } 269820dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2699341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2700341ebc2fSJohn Snow 27019bd2b08fSJohn Snow out: 2702341ebc2fSJohn Snow aio_context_release(aio_context); 2703341ebc2fSJohn Snow } 2704341ebc2fSJohn Snow 2705e74e6b78SJohn Snow /** 2706e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2707e74e6b78SJohn Snow * immediately after a full backup operation. 2708e74e6b78SJohn Snow */ 2709e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2710e74e6b78SJohn Snow Error **errp) 2711e74e6b78SJohn Snow { 2712e74e6b78SJohn Snow AioContext *aio_context; 2713e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2714e74e6b78SJohn Snow BlockDriverState *bs; 2715e74e6b78SJohn Snow 2716e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2717e74e6b78SJohn Snow if (!bitmap || !bs) { 2718e74e6b78SJohn Snow return; 2719e74e6b78SJohn Snow } 2720e74e6b78SJohn Snow 2721e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2722e74e6b78SJohn Snow error_setg(errp, 2723e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2724e74e6b78SJohn Snow name); 2725e74e6b78SJohn Snow goto out; 2726e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2727e74e6b78SJohn Snow error_setg(errp, 2728e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2729e74e6b78SJohn Snow name); 2730e74e6b78SJohn Snow goto out; 2731e74e6b78SJohn Snow } 2732e74e6b78SJohn Snow 2733df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2734e74e6b78SJohn Snow 2735e74e6b78SJohn Snow out: 2736e74e6b78SJohn Snow aio_context_release(aio_context); 2737e74e6b78SJohn Snow } 2738e74e6b78SJohn Snow 2739072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 27409063f814SRyan Harper { 27419063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 27427e7d56d9SMarkus Armbruster BlockBackend *blk; 27439063f814SRyan Harper BlockDriverState *bs; 27448ad4202bSStefan Hajnoczi AioContext *aio_context; 27453718d8abSFam Zheng Error *local_err = NULL; 27469063f814SRyan Harper 27477e7d56d9SMarkus Armbruster blk = blk_by_name(id); 27487e7d56d9SMarkus Armbruster if (!blk) { 2749b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2750072ebe6bSMarkus Armbruster return; 27519063f814SRyan Harper } 27528ad4202bSStefan Hajnoczi 275326f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 275448f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 275548f364ddSMarkus Armbruster " is not supported"); 2756072ebe6bSMarkus Armbruster return; 275748f364ddSMarkus Armbruster } 275848f364ddSMarkus Armbruster 27595433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27608ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 27618ad4202bSStefan Hajnoczi 27625433c24fSMax Reitz bs = blk_bs(blk); 27635433c24fSMax Reitz if (bs) { 27643718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2765565f65d2SMarkus Armbruster error_report_err(local_err); 27668ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2767072ebe6bSMarkus Armbruster return; 27688591675fSMarcelo Tosatti } 27699063f814SRyan Harper 27709063f814SRyan Harper bdrv_close(bs); 27715433c24fSMax Reitz } 27729063f814SRyan Harper 2773fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 2774d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 2775d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 2776d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 2777d22b2f41SRyan Harper */ 2778a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 27793e5a50d6SMarkus Armbruster blk_hide_on_behalf_of_hmp_drive_del(blk); 2780293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2781373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2782293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2783d22b2f41SRyan Harper } else { 2784b9fe8a7aSMarkus Armbruster blk_unref(blk); 2785d22b2f41SRyan Harper } 27869063f814SRyan Harper 27878ad4202bSStefan Hajnoczi aio_context_release(aio_context); 27889063f814SRyan Harper } 27896d4a2b3aSChristoph Hellwig 27903b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 27913b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 27923b1dbd11SBenoît Canet int64_t size, Error **errp) 27936d4a2b3aSChristoph Hellwig { 27943b1dbd11SBenoît Canet Error *local_err = NULL; 27956d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2796927e0e76SStefan Hajnoczi AioContext *aio_context; 27978732901eSKevin Wolf int ret; 27986d4a2b3aSChristoph Hellwig 27993b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 28003b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 28013b1dbd11SBenoît Canet &local_err); 280284d18f06SMarkus Armbruster if (local_err) { 28033b1dbd11SBenoît Canet error_propagate(errp, local_err); 28043b1dbd11SBenoît Canet return; 28053b1dbd11SBenoît Canet } 28063b1dbd11SBenoît Canet 2807927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2808927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2809927e0e76SStefan Hajnoczi 28103b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2811c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2812927e0e76SStefan Hajnoczi goto out; 28136d4a2b3aSChristoph Hellwig } 28146d4a2b3aSChristoph Hellwig 28156d4a2b3aSChristoph Hellwig if (size < 0) { 2816c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2817927e0e76SStefan Hajnoczi goto out; 28186d4a2b3aSChristoph Hellwig } 28196d4a2b3aSChristoph Hellwig 28209c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2821c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2822927e0e76SStefan Hajnoczi goto out; 28239c75e168SJeff Cody } 28249c75e168SJeff Cody 282592b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 282692b7a08dSPeter Lieven bdrv_drain_all(); 282792b7a08dSPeter Lieven 28288732901eSKevin Wolf ret = bdrv_truncate(bs, size); 28298732901eSKevin Wolf switch (ret) { 2830939a1cc3SStefan Hajnoczi case 0: 2831939a1cc3SStefan Hajnoczi break; 2832939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2833c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2834939a1cc3SStefan Hajnoczi break; 2835939a1cc3SStefan Hajnoczi case -ENOTSUP: 2836c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2837939a1cc3SStefan Hajnoczi break; 2838939a1cc3SStefan Hajnoczi case -EACCES: 283981e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2840939a1cc3SStefan Hajnoczi break; 2841939a1cc3SStefan Hajnoczi case -EBUSY: 2842c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2843939a1cc3SStefan Hajnoczi break; 2844939a1cc3SStefan Hajnoczi default: 28458732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2846939a1cc3SStefan Hajnoczi break; 28476d4a2b3aSChristoph Hellwig } 2848927e0e76SStefan Hajnoczi 2849927e0e76SStefan Hajnoczi out: 2850927e0e76SStefan Hajnoczi aio_context_release(aio_context); 28516d4a2b3aSChristoph Hellwig } 285212bd451fSStefan Hajnoczi 28539abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 285412bd451fSStefan Hajnoczi { 2855723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2856723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2857723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2858723c5d93SStefan Hajnoczi */ 2859723c5d93SStefan Hajnoczi 286012bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2861bcada37bSWenchao Xia const char *msg = NULL; 286212bd451fSStefan Hajnoczi 28639abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 286412bd451fSStefan Hajnoczi 286512bd451fSStefan Hajnoczi assert(bs->job); 2866bcada37bSWenchao Xia 286712bd451fSStefan Hajnoczi if (ret < 0) { 2868bcada37bSWenchao Xia msg = strerror(-ret); 286912bd451fSStefan Hajnoczi } 287012bd451fSStefan Hajnoczi 2871370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2872bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2873370521a1SStefan Hajnoczi } else { 2874bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2875370521a1SStefan Hajnoczi } 287612bd451fSStefan Hajnoczi } 287712bd451fSStefan Hajnoczi 287813d8cc51SJeff Cody void qmp_block_stream(const char *device, 287913d8cc51SJeff Cody bool has_base, const char *base, 288013d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 288113d8cc51SJeff Cody bool has_speed, int64_t speed, 28821d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 28831d809098SPaolo Bonzini Error **errp) 288412bd451fSStefan Hajnoczi { 2885a0e8544cSFam Zheng BlockBackend *blk; 288612bd451fSStefan Hajnoczi BlockDriverState *bs; 2887c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2888f3e69bebSStefan Hajnoczi AioContext *aio_context; 2889fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 289013d8cc51SJeff Cody const char *base_name = NULL; 289112bd451fSStefan Hajnoczi 28921d809098SPaolo Bonzini if (!has_on_error) { 28931d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 28941d809098SPaolo Bonzini } 28951d809098SPaolo Bonzini 2896a0e8544cSFam Zheng blk = blk_by_name(device); 2897a0e8544cSFam Zheng if (!blk) { 289875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 289975158ebbSMarkus Armbruster "Device '%s' not found", device); 290012bd451fSStefan Hajnoczi return; 290112bd451fSStefan Hajnoczi } 290212bd451fSStefan Hajnoczi 29035433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2904f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2905f3e69bebSStefan Hajnoczi 29065433c24fSMax Reitz if (!blk_is_available(blk)) { 29075433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29085433c24fSMax Reitz goto out; 29095433c24fSMax Reitz } 29105433c24fSMax Reitz bs = blk_bs(blk); 29115433c24fSMax Reitz 2912628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2913f3e69bebSStefan Hajnoczi goto out; 2914628ff683SFam Zheng } 2915628ff683SFam Zheng 291613d8cc51SJeff Cody if (has_base) { 2917c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2918c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2919c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2920f3e69bebSStefan Hajnoczi goto out; 292112bd451fSStefan Hajnoczi } 2922f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 292313d8cc51SJeff Cody base_name = base; 2924c8c3080fSMarcelo Tosatti } 292512bd451fSStefan Hajnoczi 292613d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 292713d8cc51SJeff Cody * file, and specifying one is therefore an error */ 292813d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 292913d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 293013d8cc51SJeff Cody "entire chain"); 2931f3e69bebSStefan Hajnoczi goto out; 293213d8cc51SJeff Cody } 293313d8cc51SJeff Cody 293413d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 293513d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 293613d8cc51SJeff Cody 293713d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 29381d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 293984d18f06SMarkus Armbruster if (local_err) { 2940fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2941f3e69bebSStefan Hajnoczi goto out; 294212bd451fSStefan Hajnoczi } 294312bd451fSStefan Hajnoczi 294412bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 2945f3e69bebSStefan Hajnoczi 2946f3e69bebSStefan Hajnoczi out: 2947f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 294812bd451fSStefan Hajnoczi } 29492d47c6e9SStefan Hajnoczi 2950ed61fc10SJeff Cody void qmp_block_commit(const char *device, 29517676e2c5SJeff Cody bool has_base, const char *base, 29527676e2c5SJeff Cody bool has_top, const char *top, 295354e26900SJeff Cody bool has_backing_file, const char *backing_file, 2954ed61fc10SJeff Cody bool has_speed, int64_t speed, 2955ed61fc10SJeff Cody Error **errp) 2956ed61fc10SJeff Cody { 2957a0e8544cSFam Zheng BlockBackend *blk; 2958ed61fc10SJeff Cody BlockDriverState *bs; 2959ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 29609e85cd5cSStefan Hajnoczi AioContext *aio_context; 2961ed61fc10SJeff Cody Error *local_err = NULL; 2962ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 2963ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 2964ed61fc10SJeff Cody */ 296592aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2966ed61fc10SJeff Cody 296754504663SMax Reitz if (!has_speed) { 296854504663SMax Reitz speed = 0; 296954504663SMax Reitz } 297054504663SMax Reitz 29717676e2c5SJeff Cody /* Important Note: 29727676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 29737676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 29747676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 29757676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 2976a0e8544cSFam Zheng blk = blk_by_name(device); 2977a0e8544cSFam Zheng if (!blk) { 297875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 297975158ebbSMarkus Armbruster "Device '%s' not found", device); 2980ed61fc10SJeff Cody return; 2981ed61fc10SJeff Cody } 2982ed61fc10SJeff Cody 29835433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29849e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 29859e85cd5cSStefan Hajnoczi 29865433c24fSMax Reitz if (!blk_is_available(blk)) { 29875433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29885433c24fSMax Reitz goto out; 29895433c24fSMax Reitz } 29905433c24fSMax Reitz bs = blk_bs(blk); 29915433c24fSMax Reitz 2992bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 29939e85cd5cSStefan Hajnoczi goto out; 2994628ff683SFam Zheng } 2995628ff683SFam Zheng 2996ed61fc10SJeff Cody /* default top_bs is the active layer */ 2997ed61fc10SJeff Cody top_bs = bs; 2998ed61fc10SJeff Cody 29997676e2c5SJeff Cody if (has_top && top) { 3000ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 3001ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3002ed61fc10SJeff Cody } 3003ed61fc10SJeff Cody } 3004ed61fc10SJeff Cody 3005ed61fc10SJeff Cody if (top_bs == NULL) { 3006ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30079e85cd5cSStefan Hajnoczi goto out; 3008ed61fc10SJeff Cody } 3009ed61fc10SJeff Cody 30109e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30119e85cd5cSStefan Hajnoczi 3012d5208c45SJeff Cody if (has_base && base) { 3013d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3014d5208c45SJeff Cody } else { 3015d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3016d5208c45SJeff Cody } 3017d5208c45SJeff Cody 3018d5208c45SJeff Cody if (base_bs == NULL) { 3019c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 30209e85cd5cSStefan Hajnoczi goto out; 3021d5208c45SJeff Cody } 3022d5208c45SJeff Cody 30239e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 30249e85cd5cSStefan Hajnoczi 3025bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3026bb00021dSFam Zheng goto out; 3027bb00021dSFam Zheng } 3028bb00021dSFam Zheng 30297676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 30307676e2c5SJeff Cody if (top_bs == base_bs) { 30317676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 30329e85cd5cSStefan Hajnoczi goto out; 30337676e2c5SJeff Cody } 30347676e2c5SJeff Cody 303520a63d2cSFam Zheng if (top_bs == bs) { 303654e26900SJeff Cody if (has_backing_file) { 303754e26900SJeff Cody error_setg(errp, "'backing-file' specified," 303854e26900SJeff Cody " but 'top' is the active layer"); 30399e85cd5cSStefan Hajnoczi goto out; 304054e26900SJeff Cody } 304120a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 304220a63d2cSFam Zheng bs, &local_err); 304320a63d2cSFam Zheng } else { 3044ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 304554e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 304620a63d2cSFam Zheng } 3047ed61fc10SJeff Cody if (local_err != NULL) { 3048ed61fc10SJeff Cody error_propagate(errp, local_err); 30499e85cd5cSStefan Hajnoczi goto out; 3050ed61fc10SJeff Cody } 30519e85cd5cSStefan Hajnoczi 30529e85cd5cSStefan Hajnoczi out: 30539e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3054ed61fc10SJeff Cody } 3055ed61fc10SJeff Cody 305678f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 305799a9addfSStefan Hajnoczi bool has_format, const char *format, 3058b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 305999a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 306099a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3061d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 306278f51fdeSJohn Snow bool has_on_source_error, 306378f51fdeSJohn Snow BlockdevOnError on_source_error, 306478f51fdeSJohn Snow bool has_on_target_error, 306578f51fdeSJohn Snow BlockdevOnError on_target_error, 306678f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 306799a9addfSStefan Hajnoczi { 3068a0e8544cSFam Zheng BlockBackend *blk; 306999a9addfSStefan Hajnoczi BlockDriverState *bs; 307099a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3071fc5d3f84SIan Main BlockDriverState *source = NULL; 3072d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3073761731b1SStefan Hajnoczi AioContext *aio_context; 3074e6641719SMax Reitz QDict *options = NULL; 307599a9addfSStefan Hajnoczi Error *local_err = NULL; 307699a9addfSStefan Hajnoczi int flags; 307799a9addfSStefan Hajnoczi int64_t size; 307899a9addfSStefan Hajnoczi int ret; 307999a9addfSStefan Hajnoczi 308099a9addfSStefan Hajnoczi if (!has_speed) { 308199a9addfSStefan Hajnoczi speed = 0; 308299a9addfSStefan Hajnoczi } 308399a9addfSStefan Hajnoczi if (!has_on_source_error) { 308499a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 308599a9addfSStefan Hajnoczi } 308699a9addfSStefan Hajnoczi if (!has_on_target_error) { 308799a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 308899a9addfSStefan Hajnoczi } 308999a9addfSStefan Hajnoczi if (!has_mode) { 309099a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 309199a9addfSStefan Hajnoczi } 309299a9addfSStefan Hajnoczi 3093a0e8544cSFam Zheng blk = blk_by_name(device); 3094a0e8544cSFam Zheng if (!blk) { 309575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 309675158ebbSMarkus Armbruster "Device '%s' not found", device); 309799a9addfSStefan Hajnoczi return; 309899a9addfSStefan Hajnoczi } 309999a9addfSStefan Hajnoczi 31005433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3101761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3102761731b1SStefan Hajnoczi 3103c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3104c29c1dd3SFam Zheng * do an early check redundantly. */ 31055433c24fSMax Reitz if (!blk_is_available(blk)) { 3106c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3107761731b1SStefan Hajnoczi goto out; 310899a9addfSStefan Hajnoczi } 31095433c24fSMax Reitz bs = blk_bs(blk); 311099a9addfSStefan Hajnoczi 311199a9addfSStefan Hajnoczi if (!has_format) { 311299a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 311399a9addfSStefan Hajnoczi } 311499a9addfSStefan Hajnoczi 3115c29c1dd3SFam Zheng /* Early check to avoid creating target */ 31163718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3117761731b1SStefan Hajnoczi goto out; 311899a9addfSStefan Hajnoczi } 311999a9addfSStefan Hajnoczi 312099a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 312199a9addfSStefan Hajnoczi 3122fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3123fc5d3f84SIan Main * on top of. */ 3124fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3125760e0063SKevin Wolf source = backing_bs(bs); 3126fc5d3f84SIan Main if (!source) { 3127fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3128fc5d3f84SIan Main } 3129fc5d3f84SIan Main } 3130fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3131fc5d3f84SIan Main source = bs; 3132fc5d3f84SIan Main } 3133fc5d3f84SIan Main 313499a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 313599a9addfSStefan Hajnoczi if (size < 0) { 313699a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3137761731b1SStefan Hajnoczi goto out; 313899a9addfSStefan Hajnoczi } 313999a9addfSStefan Hajnoczi 314099a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3141e6641719SMax Reitz assert(format); 3142fc5d3f84SIan Main if (source) { 3143fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3144fc5d3f84SIan Main source->drv->format_name, NULL, 3145fc5d3f84SIan Main size, flags, &local_err, false); 3146fc5d3f84SIan Main } else { 3147fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3148fc5d3f84SIan Main size, flags, &local_err, false); 3149fc5d3f84SIan Main } 315099a9addfSStefan Hajnoczi } 315199a9addfSStefan Hajnoczi 315284d18f06SMarkus Armbruster if (local_err) { 315399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3154761731b1SStefan Hajnoczi goto out; 315599a9addfSStefan Hajnoczi } 315699a9addfSStefan Hajnoczi 3157e6641719SMax Reitz if (format) { 3158e6641719SMax Reitz options = qdict_new(); 3159e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3160e6641719SMax Reitz } 3161e6641719SMax Reitz 3162f67503e5SMax Reitz target_bs = NULL; 31636ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 316499a9addfSStefan Hajnoczi if (ret < 0) { 316534b5d2c6SMax Reitz error_propagate(errp, local_err); 3166761731b1SStefan Hajnoczi goto out; 316799a9addfSStefan Hajnoczi } 316899a9addfSStefan Hajnoczi 3169761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3170761731b1SStefan Hajnoczi 3171d58d8453SJohn Snow if (has_bitmap) { 3172d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3173d58d8453SJohn Snow if (!bmap) { 3174d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 31750702d3d8SMax Reitz bdrv_unref(target_bs); 3176d58d8453SJohn Snow goto out; 3177d58d8453SJohn Snow } 3178d58d8453SJohn Snow } 3179d58d8453SJohn Snow 3180d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3181d58d8453SJohn Snow on_source_error, on_target_error, 318278f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 318399a9addfSStefan Hajnoczi if (local_err != NULL) { 31844f6fd349SFam Zheng bdrv_unref(target_bs); 318599a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3186761731b1SStefan Hajnoczi goto out; 318799a9addfSStefan Hajnoczi } 3188761731b1SStefan Hajnoczi 3189761731b1SStefan Hajnoczi out: 3190761731b1SStefan Hajnoczi aio_context_release(aio_context); 319199a9addfSStefan Hajnoczi } 319299a9addfSStefan Hajnoczi 319378f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 319478f51fdeSJohn Snow bool has_format, const char *format, 319578f51fdeSJohn Snow enum MirrorSyncMode sync, 319678f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 319778f51fdeSJohn Snow bool has_speed, int64_t speed, 319878f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 319978f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 320078f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 320178f51fdeSJohn Snow Error **errp) 320278f51fdeSJohn Snow { 320378f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 320478f51fdeSJohn Snow has_mode, mode, has_speed, speed, 320578f51fdeSJohn Snow has_bitmap, bitmap, 320678f51fdeSJohn Snow has_on_source_error, on_source_error, 320778f51fdeSJohn Snow has_on_target_error, on_target_error, 320878f51fdeSJohn Snow NULL, errp); 320978f51fdeSJohn Snow } 321078f51fdeSJohn Snow 3211c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3212c13163fbSBenoît Canet { 3213d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3214c13163fbSBenoît Canet } 3215c13163fbSBenoît Canet 321678f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3217c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3218c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3219c29c1dd3SFam Zheng bool has_on_source_error, 3220c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3221c29c1dd3SFam Zheng bool has_on_target_error, 3222c29c1dd3SFam Zheng BlockdevOnError on_target_error, 322378f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3224c29c1dd3SFam Zheng { 32255433c24fSMax Reitz BlockBackend *blk, *target_blk; 3226c29c1dd3SFam Zheng BlockDriverState *bs; 3227c29c1dd3SFam Zheng BlockDriverState *target_bs; 3228c29c1dd3SFam Zheng Error *local_err = NULL; 3229c29c1dd3SFam Zheng AioContext *aio_context; 3230c29c1dd3SFam Zheng 3231c29c1dd3SFam Zheng if (!has_speed) { 3232c29c1dd3SFam Zheng speed = 0; 3233c29c1dd3SFam Zheng } 3234c29c1dd3SFam Zheng if (!has_on_source_error) { 3235c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3236c29c1dd3SFam Zheng } 3237c29c1dd3SFam Zheng if (!has_on_target_error) { 3238c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3239c29c1dd3SFam Zheng } 3240c29c1dd3SFam Zheng 3241a0e8544cSFam Zheng blk = blk_by_name(device); 3242a0e8544cSFam Zheng if (!blk) { 32435b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3244c29c1dd3SFam Zheng return; 3245c29c1dd3SFam Zheng } 3246c29c1dd3SFam Zheng 32475433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3248c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3249c29c1dd3SFam Zheng 32505433c24fSMax Reitz if (!blk_is_available(blk)) { 32515433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 32525433c24fSMax Reitz goto out; 32535433c24fSMax Reitz } 32545433c24fSMax Reitz bs = blk_bs(blk); 32555433c24fSMax Reitz 32565433c24fSMax Reitz target_blk = blk_by_name(target); 32575433c24fSMax Reitz if (!target_blk) { 32585b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3259c29c1dd3SFam Zheng goto out; 3260c29c1dd3SFam Zheng } 32615433c24fSMax Reitz 32625433c24fSMax Reitz if (!blk_is_available(target_blk)) { 32635433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 32645433c24fSMax Reitz goto out; 32655433c24fSMax Reitz } 32665433c24fSMax Reitz target_bs = blk_bs(target_blk); 3267c29c1dd3SFam Zheng 3268c29c1dd3SFam Zheng bdrv_ref(target_bs); 3269c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3270d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 327178f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3272c29c1dd3SFam Zheng if (local_err != NULL) { 3273c29c1dd3SFam Zheng bdrv_unref(target_bs); 3274c29c1dd3SFam Zheng error_propagate(errp, local_err); 3275c29c1dd3SFam Zheng } 3276c29c1dd3SFam Zheng out: 3277c29c1dd3SFam Zheng aio_context_release(aio_context); 3278c29c1dd3SFam Zheng } 3279c29c1dd3SFam Zheng 328078f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 328178f51fdeSJohn Snow enum MirrorSyncMode sync, 328278f51fdeSJohn Snow bool has_speed, int64_t speed, 328378f51fdeSJohn Snow bool has_on_source_error, 328478f51fdeSJohn Snow BlockdevOnError on_source_error, 328578f51fdeSJohn Snow bool has_on_target_error, 328678f51fdeSJohn Snow BlockdevOnError on_target_error, 328778f51fdeSJohn Snow Error **errp) 328878f51fdeSJohn Snow { 328978f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 329078f51fdeSJohn Snow has_on_source_error, on_source_error, 329178f51fdeSJohn Snow has_on_target_error, on_target_error, 329278f51fdeSJohn Snow NULL, errp); 329378f51fdeSJohn Snow } 329478f51fdeSJohn Snow 3295d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target, 3296d9b902dbSPaolo Bonzini bool has_format, const char *format, 32974c828dc6SBenoît Canet bool has_node_name, const char *node_name, 329809158f00SBenoît Canet bool has_replaces, const char *replaces, 3299d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3300d9b902dbSPaolo Bonzini bool has_mode, enum NewImageMode mode, 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, 3304b952b558SPaolo Bonzini bool has_on_source_error, BlockdevOnError on_source_error, 3305b952b558SPaolo Bonzini bool has_on_target_error, BlockdevOnError on_target_error, 33060fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3307b952b558SPaolo Bonzini Error **errp) 3308d9b902dbSPaolo Bonzini { 3309a0e8544cSFam Zheng BlockBackend *blk; 3310d9b902dbSPaolo Bonzini BlockDriverState *bs; 3311d9b902dbSPaolo Bonzini BlockDriverState *source, *target_bs; 33125a7e7a0bSStefan Hajnoczi AioContext *aio_context; 3313d9b902dbSPaolo Bonzini Error *local_err = NULL; 3314e6641719SMax Reitz QDict *options; 3315d9b902dbSPaolo Bonzini int flags; 3316ac3c5d83SStefan Hajnoczi int64_t size; 3317d9b902dbSPaolo Bonzini int ret; 3318d9b902dbSPaolo Bonzini 3319d9b902dbSPaolo Bonzini if (!has_speed) { 3320d9b902dbSPaolo Bonzini speed = 0; 3321d9b902dbSPaolo Bonzini } 3322b952b558SPaolo Bonzini if (!has_on_source_error) { 3323b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3324b952b558SPaolo Bonzini } 3325b952b558SPaolo Bonzini if (!has_on_target_error) { 3326b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3327b952b558SPaolo Bonzini } 3328d9b902dbSPaolo Bonzini if (!has_mode) { 3329d9b902dbSPaolo Bonzini mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 3330d9b902dbSPaolo Bonzini } 3331eee13dfeSPaolo Bonzini if (!has_granularity) { 3332eee13dfeSPaolo Bonzini granularity = 0; 3333eee13dfeSPaolo Bonzini } 333408e4ed6cSPaolo Bonzini if (!has_buf_size) { 333548ac0a4dSWen Congyang buf_size = 0; 333608e4ed6cSPaolo Bonzini } 33370fc9f8eaSFam Zheng if (!has_unmap) { 33380fc9f8eaSFam Zheng unmap = true; 33390fc9f8eaSFam Zheng } 334008e4ed6cSPaolo Bonzini 3341eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3342c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 33433cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3344eee13dfeSPaolo Bonzini return; 3345eee13dfeSPaolo Bonzini } 3346eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3347c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3348c6bd8c70SMarkus Armbruster "power of 2"); 3349eee13dfeSPaolo Bonzini return; 3350eee13dfeSPaolo Bonzini } 3351d9b902dbSPaolo Bonzini 3352a0e8544cSFam Zheng blk = blk_by_name(device); 3353a0e8544cSFam Zheng if (!blk) { 335475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 335575158ebbSMarkus Armbruster "Device '%s' not found", device); 3356d9b902dbSPaolo Bonzini return; 3357d9b902dbSPaolo Bonzini } 3358d9b902dbSPaolo Bonzini 33595433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 33605a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 33615a7e7a0bSStefan Hajnoczi 33625433c24fSMax Reitz if (!blk_is_available(blk)) { 3363c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 33645a7e7a0bSStefan Hajnoczi goto out; 3365d9b902dbSPaolo Bonzini } 33665433c24fSMax Reitz bs = blk_bs(blk); 3367d9b902dbSPaolo Bonzini 3368d9b902dbSPaolo Bonzini if (!has_format) { 3369d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3370d9b902dbSPaolo Bonzini } 3371d9b902dbSPaolo Bonzini 33723718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { 33735a7e7a0bSStefan Hajnoczi goto out; 3374d9b902dbSPaolo Bonzini } 3375d9b902dbSPaolo Bonzini 3376d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3377760e0063SKevin Wolf source = backing_bs(bs); 3378d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3379d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3380d9b902dbSPaolo Bonzini } 3381117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3382117e0c82SMax Reitz source = bs; 3383117e0c82SMax Reitz } 3384d9b902dbSPaolo Bonzini 3385ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3386ac3c5d83SStefan Hajnoczi if (size < 0) { 3387ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 33885a7e7a0bSStefan Hajnoczi goto out; 3389ac3c5d83SStefan Hajnoczi } 3390ac3c5d83SStefan Hajnoczi 339109158f00SBenoît Canet if (has_replaces) { 339209158f00SBenoît Canet BlockDriverState *to_replace_bs; 33935a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 33945a7e7a0bSStefan Hajnoczi int64_t replace_size; 339509158f00SBenoît Canet 339609158f00SBenoît Canet if (!has_node_name) { 339709158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 339809158f00SBenoît Canet " named node of the graph"); 33995a7e7a0bSStefan Hajnoczi goto out; 340009158f00SBenoît Canet } 340109158f00SBenoît Canet 3402e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 340309158f00SBenoît Canet 340409158f00SBenoît Canet if (!to_replace_bs) { 340509158f00SBenoît Canet error_propagate(errp, local_err); 34065a7e7a0bSStefan Hajnoczi goto out; 340709158f00SBenoît Canet } 340809158f00SBenoît Canet 34095a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 34105a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 34115a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 34125a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 34135a7e7a0bSStefan Hajnoczi 34145a7e7a0bSStefan Hajnoczi if (size != replace_size) { 341509158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 341609158f00SBenoît Canet "different size"); 34175a7e7a0bSStefan Hajnoczi goto out; 341809158f00SBenoît Canet } 341909158f00SBenoît Canet } 342009158f00SBenoît Canet 342114526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 342214526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 342314526864SMax Reitz { 3424d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3425e6641719SMax Reitz assert(format); 3426cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3427f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3428d9b902dbSPaolo Bonzini } else { 3429d9b902dbSPaolo Bonzini switch (mode) { 3430d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3431d9b902dbSPaolo Bonzini break; 3432d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3433d9b902dbSPaolo Bonzini /* create new image with backing file */ 3434cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3435d9b902dbSPaolo Bonzini source->filename, 3436d9b902dbSPaolo Bonzini source->drv->format_name, 3437f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3438d9b902dbSPaolo Bonzini break; 3439d9b902dbSPaolo Bonzini default: 3440d9b902dbSPaolo Bonzini abort(); 3441d9b902dbSPaolo Bonzini } 3442d9b902dbSPaolo Bonzini } 3443d9b902dbSPaolo Bonzini 344484d18f06SMarkus Armbruster if (local_err) { 3445cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 34465a7e7a0bSStefan Hajnoczi goto out; 3447d9b902dbSPaolo Bonzini } 3448d9b902dbSPaolo Bonzini 34494c828dc6SBenoît Canet options = qdict_new(); 3450e6641719SMax Reitz if (has_node_name) { 34514c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 34524c828dc6SBenoît Canet } 3453e6641719SMax Reitz if (format) { 3454e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3455e6641719SMax Reitz } 34564c828dc6SBenoît Canet 3457b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3458b812f671SPaolo Bonzini * file. 3459b812f671SPaolo Bonzini */ 3460f67503e5SMax Reitz target_bs = NULL; 34614c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 34626ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3463d9b902dbSPaolo Bonzini if (ret < 0) { 346434b5d2c6SMax Reitz error_propagate(errp, local_err); 34655a7e7a0bSStefan Hajnoczi goto out; 3466d9b902dbSPaolo Bonzini } 3467d9b902dbSPaolo Bonzini 34685a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 34695a7e7a0bSStefan Hajnoczi 347009158f00SBenoît Canet /* pass the node name to replace to mirror start since it's loose coupling 347109158f00SBenoît Canet * and will allow to check whether the node still exist at mirror completion 347209158f00SBenoît Canet */ 347309158f00SBenoît Canet mirror_start(bs, target_bs, 347409158f00SBenoît Canet has_replaces ? replaces : NULL, 347509158f00SBenoît Canet speed, granularity, buf_size, sync, 3476eee13dfeSPaolo Bonzini on_source_error, on_target_error, 34770fc9f8eaSFam Zheng unmap, 3478b952b558SPaolo Bonzini block_job_cb, bs, &local_err); 3479d9b902dbSPaolo Bonzini if (local_err != NULL) { 34804f6fd349SFam Zheng bdrv_unref(target_bs); 3481d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 34825a7e7a0bSStefan Hajnoczi goto out; 3483d9b902dbSPaolo Bonzini } 34845a7e7a0bSStefan Hajnoczi 34855a7e7a0bSStefan Hajnoczi out: 34865a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3487d9b902dbSPaolo Bonzini } 3488d9b902dbSPaolo Bonzini 34893d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 349024d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 349124d6bffeSMarkus Armbruster Error **errp) 34922d47c6e9SStefan Hajnoczi { 3493a0e8544cSFam Zheng BlockBackend *blk; 34942d47c6e9SStefan Hajnoczi BlockDriverState *bs; 34952d47c6e9SStefan Hajnoczi 34965433c24fSMax Reitz *aio_context = NULL; 34975433c24fSMax Reitz 3498a0e8544cSFam Zheng blk = blk_by_name(device); 3499a0e8544cSFam Zheng if (!blk) { 35003d948cdfSStefan Hajnoczi goto notfound; 35012d47c6e9SStefan Hajnoczi } 35023d948cdfSStefan Hajnoczi 35035433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 35043d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 35053d948cdfSStefan Hajnoczi 35065433c24fSMax Reitz if (!blk_is_available(blk)) { 35075433c24fSMax Reitz goto notfound; 35085433c24fSMax Reitz } 35095433c24fSMax Reitz bs = blk_bs(blk); 35105433c24fSMax Reitz 35113d948cdfSStefan Hajnoczi if (!bs->job) { 35123d948cdfSStefan Hajnoczi goto notfound; 35133d948cdfSStefan Hajnoczi } 35143d948cdfSStefan Hajnoczi 35152d47c6e9SStefan Hajnoczi return bs->job; 35163d948cdfSStefan Hajnoczi 35173d948cdfSStefan Hajnoczi notfound: 35182e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 35192e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 35205433c24fSMax Reitz if (*aio_context) { 35215433c24fSMax Reitz aio_context_release(*aio_context); 35223d948cdfSStefan Hajnoczi *aio_context = NULL; 35235433c24fSMax Reitz } 35243d948cdfSStefan Hajnoczi return NULL; 35252d47c6e9SStefan Hajnoczi } 35262d47c6e9SStefan Hajnoczi 3527882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 35282d47c6e9SStefan Hajnoczi { 35293d948cdfSStefan Hajnoczi AioContext *aio_context; 353024d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 35312d47c6e9SStefan Hajnoczi 35322d47c6e9SStefan Hajnoczi if (!job) { 35332d47c6e9SStefan Hajnoczi return; 35342d47c6e9SStefan Hajnoczi } 35352d47c6e9SStefan Hajnoczi 3536882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 35373d948cdfSStefan Hajnoczi aio_context_release(aio_context); 35382d47c6e9SStefan Hajnoczi } 3539370521a1SStefan Hajnoczi 35406e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 35416e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 35426e37fb81SPaolo Bonzini { 35433d948cdfSStefan Hajnoczi AioContext *aio_context; 354424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 35456e37fb81SPaolo Bonzini 35466e37fb81SPaolo Bonzini if (!job) { 35476e37fb81SPaolo Bonzini return; 35486e37fb81SPaolo Bonzini } 35493d948cdfSStefan Hajnoczi 35503d948cdfSStefan Hajnoczi if (!has_force) { 35513d948cdfSStefan Hajnoczi force = false; 35523d948cdfSStefan Hajnoczi } 35533d948cdfSStefan Hajnoczi 3554751ebd76SFam Zheng if (job->user_paused && !force) { 3555f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3556f231b88dSCole Robinson device); 35573d948cdfSStefan Hajnoczi goto out; 35586e37fb81SPaolo Bonzini } 35596e37fb81SPaolo Bonzini 35606e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 35616e37fb81SPaolo Bonzini block_job_cancel(job); 35623d948cdfSStefan Hajnoczi out: 35633d948cdfSStefan Hajnoczi aio_context_release(aio_context); 35646e37fb81SPaolo Bonzini } 35656e37fb81SPaolo Bonzini 35666e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3567370521a1SStefan Hajnoczi { 35683d948cdfSStefan Hajnoczi AioContext *aio_context; 356924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3570370521a1SStefan Hajnoczi 3571751ebd76SFam Zheng if (!job || job->user_paused) { 3572370521a1SStefan Hajnoczi return; 3573370521a1SStefan Hajnoczi } 35746e37fb81SPaolo Bonzini 3575751ebd76SFam Zheng job->user_paused = true; 35766e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 35776e37fb81SPaolo Bonzini block_job_pause(job); 35783d948cdfSStefan Hajnoczi aio_context_release(aio_context); 35796e37fb81SPaolo Bonzini } 35806e37fb81SPaolo Bonzini 35816e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 35826e37fb81SPaolo Bonzini { 35833d948cdfSStefan Hajnoczi AioContext *aio_context; 358424d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 35856e37fb81SPaolo Bonzini 3586751ebd76SFam Zheng if (!job || !job->user_paused) { 35878acc72a4SPaolo Bonzini return; 35888acc72a4SPaolo Bonzini } 3589370521a1SStefan Hajnoczi 3590751ebd76SFam Zheng job->user_paused = false; 35916e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 35926e37fb81SPaolo Bonzini block_job_resume(job); 35933d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3594370521a1SStefan Hajnoczi } 3595fb5458cdSStefan Hajnoczi 3596aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3597aeae883bSPaolo Bonzini { 35983d948cdfSStefan Hajnoczi AioContext *aio_context; 359924d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3600aeae883bSPaolo Bonzini 3601aeae883bSPaolo Bonzini if (!job) { 3602aeae883bSPaolo Bonzini return; 3603aeae883bSPaolo Bonzini } 3604aeae883bSPaolo Bonzini 3605aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3606aeae883bSPaolo Bonzini block_job_complete(job, errp); 36073d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3608aeae883bSPaolo Bonzini } 3609aeae883bSPaolo Bonzini 3610fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3611fa40e656SJeff Cody const char *image_node_name, 3612fa40e656SJeff Cody const char *backing_file, 3613fa40e656SJeff Cody Error **errp) 3614fa40e656SJeff Cody { 3615a0e8544cSFam Zheng BlockBackend *blk; 3616fa40e656SJeff Cody BlockDriverState *bs = NULL; 3617729962f6SStefan Hajnoczi AioContext *aio_context; 3618fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3619fa40e656SJeff Cody Error *local_err = NULL; 3620fa40e656SJeff Cody bool ro; 3621fa40e656SJeff Cody int open_flags; 3622fa40e656SJeff Cody int ret; 3623fa40e656SJeff Cody 3624a0e8544cSFam Zheng blk = blk_by_name(device); 3625a0e8544cSFam Zheng if (!blk) { 362675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 362775158ebbSMarkus Armbruster "Device '%s' not found", device); 3628fa40e656SJeff Cody return; 3629fa40e656SJeff Cody } 3630fa40e656SJeff Cody 36315433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3632729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3633729962f6SStefan Hajnoczi 36345433c24fSMax Reitz if (!blk_is_available(blk)) { 36355433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 36365433c24fSMax Reitz goto out; 36375433c24fSMax Reitz } 36385433c24fSMax Reitz bs = blk_bs(blk); 36395433c24fSMax Reitz 3640fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3641fa40e656SJeff Cody if (local_err) { 3642fa40e656SJeff Cody error_propagate(errp, local_err); 3643729962f6SStefan Hajnoczi goto out; 3644fa40e656SJeff Cody } 3645fa40e656SJeff Cody 3646fa40e656SJeff Cody if (!image_bs) { 3647fa40e656SJeff Cody error_setg(errp, "image file not found"); 3648729962f6SStefan Hajnoczi goto out; 3649fa40e656SJeff Cody } 3650fa40e656SJeff Cody 3651fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3652fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3653fa40e656SJeff Cody "without a backing file"); 3654729962f6SStefan Hajnoczi goto out; 3655fa40e656SJeff Cody } 3656fa40e656SJeff Cody 3657fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3658fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3659fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3660729962f6SStefan Hajnoczi goto out; 3661fa40e656SJeff Cody } 3662fa40e656SJeff Cody 3663fa40e656SJeff Cody /* final sanity check */ 3664fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3665fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3666fa40e656SJeff Cody device); 3667729962f6SStefan Hajnoczi goto out; 3668fa40e656SJeff Cody } 3669fa40e656SJeff Cody 3670fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3671fa40e656SJeff Cody open_flags = image_bs->open_flags; 3672fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3673fa40e656SJeff Cody 3674fa40e656SJeff Cody if (ro) { 3675fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3676fa40e656SJeff Cody if (local_err) { 3677fa40e656SJeff Cody error_propagate(errp, local_err); 3678729962f6SStefan Hajnoczi goto out; 3679fa40e656SJeff Cody } 3680fa40e656SJeff Cody } 3681fa40e656SJeff Cody 3682fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3683fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3684fa40e656SJeff Cody 3685fa40e656SJeff Cody if (ret < 0) { 3686fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3687fa40e656SJeff Cody backing_file); 3688fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3689fa40e656SJeff Cody * appropriate */ 3690fa40e656SJeff Cody } 3691fa40e656SJeff Cody 3692fa40e656SJeff Cody if (ro) { 3693fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3694fa40e656SJeff Cody if (local_err) { 3695fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3696fa40e656SJeff Cody } 3697fa40e656SJeff Cody } 3698729962f6SStefan Hajnoczi 3699729962f6SStefan Hajnoczi out: 3700729962f6SStefan Hajnoczi aio_context_release(aio_context); 3701fa40e656SJeff Cody } 3702fa40e656SJeff Cody 3703d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3704d26c9a15SKevin Wolf { 3705d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3706be4b67bcSMax Reitz BlockDriverState *bs; 3707be4b67bcSMax Reitz BlockBackend *blk = NULL; 3708d26c9a15SKevin Wolf QObject *obj; 3709d26c9a15SKevin Wolf QDict *qdict; 3710d26c9a15SKevin Wolf Error *local_err = NULL; 3711d26c9a15SKevin Wolf 371260e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3713d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 371460e19e06SMarkus Armbruster * when called from drive_new(). 3715d26c9a15SKevin Wolf * 3716d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3717d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3718c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3719c6e0bd9bSKevin Wolf options->cache->has_direct && 3720c6e0bd9bSKevin Wolf options->cache->direct; 3721c6e0bd9bSKevin Wolf if (!direct) { 3722d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3723d26c9a15SKevin Wolf goto fail; 3724d26c9a15SKevin Wolf } 3725d26c9a15SKevin Wolf } 3726d26c9a15SKevin Wolf 3727d26c9a15SKevin Wolf visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3728d26c9a15SKevin Wolf &options, NULL, &local_err); 372984d18f06SMarkus Armbruster if (local_err) { 3730d26c9a15SKevin Wolf error_propagate(errp, local_err); 3731d26c9a15SKevin Wolf goto fail; 3732d26c9a15SKevin Wolf } 3733d26c9a15SKevin Wolf 3734d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3735d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3736d26c9a15SKevin Wolf 3737d26c9a15SKevin Wolf qdict_flatten(qdict); 3738d26c9a15SKevin Wolf 3739be4b67bcSMax Reitz if (options->has_id) { 374018e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 374184d18f06SMarkus Armbruster if (local_err) { 3742b681072dSKevin Wolf error_propagate(errp, local_err); 3743d26c9a15SKevin Wolf goto fail; 3744d26c9a15SKevin Wolf } 3745d26c9a15SKevin Wolf 3746be4b67bcSMax Reitz bs = blk_bs(blk); 3747be4b67bcSMax Reitz } else { 3748be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3749be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3750be4b67bcSMax Reitz "the root node"); 3751be4b67bcSMax Reitz goto fail; 3752be4b67bcSMax Reitz } 3753be4b67bcSMax Reitz 3754bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3755bd745e23SMax Reitz if (!bs) { 3756be4b67bcSMax Reitz goto fail; 3757be4b67bcSMax Reitz } 3758be4b67bcSMax Reitz } 3759be4b67bcSMax Reitz 3760be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3761be4b67bcSMax Reitz if (blk) { 376218e46a03SMarkus Armbruster blk_unref(blk); 3763be4b67bcSMax Reitz } else { 3764be4b67bcSMax Reitz bdrv_unref(bs); 3765be4b67bcSMax Reitz } 37668ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 37678ae8e904SKevin Wolf goto fail; 37688ae8e904SKevin Wolf } 37698ae8e904SKevin Wolf 3770d26c9a15SKevin Wolf fail: 3771d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3772d26c9a15SKevin Wolf } 3773d26c9a15SKevin Wolf 377481b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 377581b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 377681b936aeSAlberto Garcia { 377781b936aeSAlberto Garcia AioContext *aio_context; 377881b936aeSAlberto Garcia BlockBackend *blk; 377981b936aeSAlberto Garcia BlockDriverState *bs; 378081b936aeSAlberto Garcia 378181b936aeSAlberto Garcia if (has_id && has_node_name) { 378281b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 378381b936aeSAlberto Garcia return; 378481b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 378581b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 378681b936aeSAlberto Garcia return; 378781b936aeSAlberto Garcia } 378881b936aeSAlberto Garcia 378981b936aeSAlberto Garcia if (has_id) { 379081b936aeSAlberto Garcia blk = blk_by_name(id); 379181b936aeSAlberto Garcia if (!blk) { 379281b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 379381b936aeSAlberto Garcia return; 379481b936aeSAlberto Garcia } 379581b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 379681b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 379781b936aeSAlberto Garcia return; 379881b936aeSAlberto Garcia } 379981b936aeSAlberto Garcia bs = blk_bs(blk); 380081b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 380181b936aeSAlberto Garcia } else { 380281b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 380381b936aeSAlberto Garcia if (!bs) { 380481b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 380581b936aeSAlberto Garcia return; 380681b936aeSAlberto Garcia } 380781b936aeSAlberto Garcia blk = bs->blk; 380881b936aeSAlberto Garcia if (blk) { 380981b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 381081b936aeSAlberto Garcia node_name, blk_name(blk)); 381181b936aeSAlberto Garcia return; 381281b936aeSAlberto Garcia } 381381b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 381481b936aeSAlberto Garcia } 381581b936aeSAlberto Garcia 381681b936aeSAlberto Garcia aio_context_acquire(aio_context); 381781b936aeSAlberto Garcia 381881b936aeSAlberto Garcia if (bs) { 381981b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 382081b936aeSAlberto Garcia goto out; 382181b936aeSAlberto Garcia } 382281b936aeSAlberto Garcia 382381b936aeSAlberto Garcia if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { 382481b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 382581b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 382681b936aeSAlberto Garcia goto out; 382781b936aeSAlberto Garcia } 382881b936aeSAlberto Garcia } 382981b936aeSAlberto Garcia 383081b936aeSAlberto Garcia if (blk) { 383181b936aeSAlberto Garcia blk_unref(blk); 383281b936aeSAlberto Garcia } else { 383381b936aeSAlberto Garcia bdrv_unref(bs); 383481b936aeSAlberto Garcia } 383581b936aeSAlberto Garcia 383681b936aeSAlberto Garcia out: 383781b936aeSAlberto Garcia aio_context_release(aio_context); 383881b936aeSAlberto Garcia } 383981b936aeSAlberto Garcia 3840fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3841fb5458cdSStefan Hajnoczi { 3842fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3843fea68bb6SMarkus Armbruster BlockDriverState *bs; 3844fea68bb6SMarkus Armbruster 3845fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 384669691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 384769691e72SStefan Hajnoczi 384869691e72SStefan Hajnoczi aio_context_acquire(aio_context); 384969691e72SStefan Hajnoczi 3850fea68bb6SMarkus Armbruster if (bs->job) { 3851fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3852fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 3853fea68bb6SMarkus Armbruster *p_next = elem; 3854fea68bb6SMarkus Armbruster p_next = &elem->next; 3855fea68bb6SMarkus Armbruster } 385669691e72SStefan Hajnoczi 385769691e72SStefan Hajnoczi aio_context_release(aio_context); 3858fea68bb6SMarkus Armbruster } 3859fea68bb6SMarkus Armbruster 3860fea68bb6SMarkus Armbruster return head; 3861fb5458cdSStefan Hajnoczi } 38624d454574SPaolo Bonzini 38630006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 38644d454574SPaolo Bonzini .name = "drive", 38650006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 38664d454574SPaolo Bonzini .desc = { 38674d454574SPaolo Bonzini { 38684d454574SPaolo Bonzini .name = "snapshot", 38694d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 38704d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 38714d454574SPaolo Bonzini },{ 3872a9384affSPaolo Bonzini .name = "discard", 3873a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 3874a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 3875a9384affSPaolo Bonzini },{ 387654861b92SKevin Wolf .name = BDRV_OPT_CACHE_WB, 387729c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 387829c4e2b5SKevin Wolf .help = "enables writeback mode for any caches", 387929c4e2b5SKevin Wolf },{ 388054861b92SKevin Wolf .name = BDRV_OPT_CACHE_DIRECT, 388129c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 388229c4e2b5SKevin Wolf .help = "enables use of O_DIRECT (bypass the host page cache)", 388329c4e2b5SKevin Wolf },{ 388454861b92SKevin Wolf .name = BDRV_OPT_CACHE_NO_FLUSH, 388529c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 388629c4e2b5SKevin Wolf .help = "ignore any flush requests for the device", 38874d454574SPaolo Bonzini },{ 38884d454574SPaolo Bonzini .name = "aio", 38894d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38904d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 38914d454574SPaolo Bonzini },{ 38924d454574SPaolo Bonzini .name = "format", 38934d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38944d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 38954d454574SPaolo Bonzini },{ 38964d454574SPaolo Bonzini .name = "rerror", 38974d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 38984d454574SPaolo Bonzini .help = "read error action", 38994d454574SPaolo Bonzini },{ 39004d454574SPaolo Bonzini .name = "werror", 39014d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39024d454574SPaolo Bonzini .help = "write error action", 39034d454574SPaolo Bonzini },{ 39040f227a94SKevin Wolf .name = "read-only", 39054d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39064d454574SPaolo Bonzini .help = "open drive file as read-only", 39074d454574SPaolo Bonzini },{ 390857975222SKevin Wolf .name = "throttling.iops-total", 39094d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39104d454574SPaolo Bonzini .help = "limit total I/O operations per second", 39114d454574SPaolo Bonzini },{ 391257975222SKevin Wolf .name = "throttling.iops-read", 39134d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39144d454574SPaolo Bonzini .help = "limit read operations per second", 39154d454574SPaolo Bonzini },{ 391657975222SKevin Wolf .name = "throttling.iops-write", 39174d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39184d454574SPaolo Bonzini .help = "limit write operations per second", 39194d454574SPaolo Bonzini },{ 392057975222SKevin Wolf .name = "throttling.bps-total", 39214d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39224d454574SPaolo Bonzini .help = "limit total bytes per second", 39234d454574SPaolo Bonzini },{ 392457975222SKevin Wolf .name = "throttling.bps-read", 39254d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39264d454574SPaolo Bonzini .help = "limit read bytes per second", 39274d454574SPaolo Bonzini },{ 392857975222SKevin Wolf .name = "throttling.bps-write", 39294d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39304d454574SPaolo Bonzini .help = "limit write bytes per second", 39314d454574SPaolo Bonzini },{ 39323e9fab69SBenoît Canet .name = "throttling.iops-total-max", 39333e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39343e9fab69SBenoît Canet .help = "I/O operations burst", 39353e9fab69SBenoît Canet },{ 39363e9fab69SBenoît Canet .name = "throttling.iops-read-max", 39373e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39383e9fab69SBenoît Canet .help = "I/O operations read burst", 39393e9fab69SBenoît Canet },{ 39403e9fab69SBenoît Canet .name = "throttling.iops-write-max", 39413e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39423e9fab69SBenoît Canet .help = "I/O operations write burst", 39433e9fab69SBenoît Canet },{ 39443e9fab69SBenoît Canet .name = "throttling.bps-total-max", 39453e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39463e9fab69SBenoît Canet .help = "total bytes burst", 39473e9fab69SBenoît Canet },{ 39483e9fab69SBenoît Canet .name = "throttling.bps-read-max", 39493e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39503e9fab69SBenoît Canet .help = "total bytes read burst", 39513e9fab69SBenoît Canet },{ 39523e9fab69SBenoît Canet .name = "throttling.bps-write-max", 39533e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 39543e9fab69SBenoît Canet .help = "total bytes write burst", 39553e9fab69SBenoît Canet },{ 39562024c1dfSBenoît Canet .name = "throttling.iops-size", 39572024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 39582024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 39592024c1dfSBenoît Canet },{ 396076f4afb4SAlberto Garcia .name = "throttling.group", 396176f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 396276f4afb4SAlberto Garcia .help = "name of the block throttling group", 396376f4afb4SAlberto Garcia },{ 39644d454574SPaolo Bonzini .name = "copy-on-read", 39654d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39664d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 3967465bee1dSPeter Lieven },{ 3968465bee1dSPeter Lieven .name = "detect-zeroes", 3969465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 3970465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 3971362e9299SAlberto Garcia },{ 3972362e9299SAlberto Garcia .name = "stats-account-invalid", 3973362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3974362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 3975362e9299SAlberto Garcia "in the statistics", 3976362e9299SAlberto Garcia },{ 3977362e9299SAlberto Garcia .name = "stats-account-failed", 3978362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 3979362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 3980362e9299SAlberto Garcia "in the statistics", 39814d454574SPaolo Bonzini }, 39824d454574SPaolo Bonzini { /* end of list */ } 39834d454574SPaolo Bonzini }, 39844d454574SPaolo Bonzini }; 39850006383eSKevin Wolf 3986bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 3987bd745e23SMax Reitz .name = "root-bds", 3988bd745e23SMax Reitz .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 3989bd745e23SMax Reitz .desc = { 3990bd745e23SMax Reitz { 3991bd745e23SMax Reitz .name = "discard", 3992bd745e23SMax Reitz .type = QEMU_OPT_STRING, 3993bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 3994bd745e23SMax Reitz },{ 3995bd745e23SMax Reitz .name = "cache.writeback", 3996bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3997bd745e23SMax Reitz .help = "enables writeback mode for any caches", 3998bd745e23SMax Reitz },{ 3999bd745e23SMax Reitz .name = "cache.direct", 4000bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4001bd745e23SMax Reitz .help = "enables use of O_DIRECT (bypass the host page cache)", 4002bd745e23SMax Reitz },{ 4003bd745e23SMax Reitz .name = "cache.no-flush", 4004bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4005bd745e23SMax Reitz .help = "ignore any flush requests for the device", 4006bd745e23SMax Reitz },{ 4007bd745e23SMax Reitz .name = "aio", 4008bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4009bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4010bd745e23SMax Reitz },{ 4011bd745e23SMax Reitz .name = "read-only", 4012bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4013bd745e23SMax Reitz .help = "open drive file as read-only", 4014bd745e23SMax Reitz },{ 4015bd745e23SMax Reitz .name = "copy-on-read", 4016bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4017bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4018bd745e23SMax Reitz },{ 4019bd745e23SMax Reitz .name = "detect-zeroes", 4020bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4021bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4022bd745e23SMax Reitz }, 4023bd745e23SMax Reitz { /* end of list */ } 4024bd745e23SMax Reitz }, 4025bd745e23SMax Reitz }; 4026bd745e23SMax Reitz 40270006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 40280006383eSKevin Wolf .name = "drive", 40290006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 40300006383eSKevin Wolf .desc = { 4031492fdc6fSKevin Wolf /* 4032492fdc6fSKevin Wolf * no elements => accept any params 4033492fdc6fSKevin Wolf * validation will happen later 4034492fdc6fSKevin Wolf */ 40350006383eSKevin Wolf { /* end of list */ } 40360006383eSKevin Wolf }, 40370006383eSKevin Wolf }; 4038