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)) { 351*972606c4SFam Zheng error_setg(errp, "bps/iops/max values must be within [0, %lld]", 352*972606c4SFam Zheng THROTTLE_VALUE_MAX); 3537d81c141SStefan Hajnoczi return false; 3547d81c141SStefan Hajnoczi } 3557d81c141SStefan Hajnoczi 356ee2bdc33SStefan Hajnoczi if (throttle_max_is_missing_limit(cfg)) { 357ee2bdc33SStefan Hajnoczi error_setg(errp, "bps_max/iops_max require corresponding" 358ee2bdc33SStefan Hajnoczi " bps/iops values"); 359ee2bdc33SStefan Hajnoczi return false; 360ee2bdc33SStefan Hajnoczi } 361ee2bdc33SStefan Hajnoczi 3620563e191SZhi Yong Wu return true; 3630563e191SZhi Yong Wu } 3640563e191SZhi Yong Wu 36533cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 36633cb7dc8SKevin Wolf 367fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 368fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 369fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 370fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 371fbf8175eSMax Reitz { 372fbf8175eSMax Reitz const char *discard; 373fbf8175eSMax Reitz Error *local_error = NULL; 374fbf8175eSMax Reitz const char *aio; 375fbf8175eSMax Reitz 376fbf8175eSMax Reitz if (bdrv_flags) { 377fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 378fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 379fbf8175eSMax Reitz } 380fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 381fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 382fbf8175eSMax Reitz } 383fbf8175eSMax Reitz 384fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 385fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 386fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 387fbf8175eSMax Reitz return; 388fbf8175eSMax Reitz } 389fbf8175eSMax Reitz } 390fbf8175eSMax Reitz 391fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 392fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 393fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 394fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 395fbf8175eSMax Reitz /* this is the default */ 396fbf8175eSMax Reitz } else { 397fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 398fbf8175eSMax Reitz return; 399fbf8175eSMax Reitz } 400fbf8175eSMax Reitz } 401fbf8175eSMax Reitz } 402fbf8175eSMax Reitz 403fbf8175eSMax Reitz /* disk I/O throttling */ 404fbf8175eSMax Reitz if (throttling_group) { 405fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 406fbf8175eSMax Reitz } 407fbf8175eSMax Reitz 408fbf8175eSMax Reitz if (throttle_cfg) { 409fbf8175eSMax Reitz memset(throttle_cfg, 0, sizeof(*throttle_cfg)); 410fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 411fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 412fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 413fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 414fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 415fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 416fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 417fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 418fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 419fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 420fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 421fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 422fbf8175eSMax Reitz 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 429fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 431fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 432fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 433fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 434fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 435fbf8175eSMax Reitz 436fbf8175eSMax Reitz throttle_cfg->op_size = 437fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 438fbf8175eSMax Reitz 439fbf8175eSMax Reitz if (!check_throttle_config(throttle_cfg, errp)) { 440fbf8175eSMax Reitz return; 441fbf8175eSMax Reitz } 442fbf8175eSMax Reitz } 443fbf8175eSMax Reitz 444fbf8175eSMax Reitz if (detect_zeroes) { 445fbf8175eSMax Reitz *detect_zeroes = 446fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 447fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 4487fb1cf16SEric Blake BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 449fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 450fbf8175eSMax Reitz &local_error); 451fbf8175eSMax Reitz if (local_error) { 452fbf8175eSMax Reitz error_propagate(errp, local_error); 453fbf8175eSMax Reitz return; 454fbf8175eSMax Reitz } 455fbf8175eSMax Reitz 456fbf8175eSMax Reitz if (bdrv_flags && 457fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 458fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 459fbf8175eSMax Reitz { 460fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 461fbf8175eSMax Reitz "without setting discard operation to unmap"); 462fbf8175eSMax Reitz return; 463fbf8175eSMax Reitz } 464fbf8175eSMax Reitz } 465fbf8175eSMax Reitz } 466fbf8175eSMax Reitz 467f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46818e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 469b681072dSKevin Wolf Error **errp) 470666daa68SMarkus Armbruster { 471666daa68SMarkus Armbruster const char *buf; 472666daa68SMarkus Armbruster int bdrv_flags = 0; 473666daa68SMarkus Armbruster int on_read_error, on_write_error; 474362e9299SAlberto Garcia bool account_invalid, account_failed; 47526f54e9aSMarkus Armbruster BlockBackend *blk; 476a0f1eab1SMarkus Armbruster BlockDriverState *bs; 477cc0681c4SBenoît Canet ThrottleConfig cfg; 478666daa68SMarkus Armbruster int snapshot = 0; 479c546194fSStefan Hajnoczi Error *error = NULL; 4800006383eSKevin Wolf QemuOpts *opts; 48140119effSAlberto Garcia QDict *interval_dict = NULL; 48240119effSAlberto Garcia QList *interval_list = NULL; 4830006383eSKevin Wolf const char *id; 484fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 485fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 486fbf8175eSMax Reitz const char *throttling_group = NULL; 487666daa68SMarkus Armbruster 488f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 489f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 490f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4910006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 49284d18f06SMarkus Armbruster if (error) { 493b681072dSKevin Wolf error_propagate(errp, error); 4946376f952SMarkus Armbruster goto err_no_opts; 4950006383eSKevin Wolf } 4960006383eSKevin Wolf 4970006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49884d18f06SMarkus Armbruster if (error) { 499b681072dSKevin Wolf error_propagate(errp, error); 500ec9c10d2SStefan Hajnoczi goto early_err; 5010006383eSKevin Wolf } 5020006383eSKevin Wolf 5030006383eSKevin Wolf if (id) { 5040006383eSKevin Wolf qdict_del(bs_opts, "id"); 5050006383eSKevin Wolf } 5060006383eSKevin Wolf 507666daa68SMarkus Armbruster /* extract parameters */ 508666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 509666daa68SMarkus Armbruster 510362e9299SAlberto Garcia account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 511362e9299SAlberto Garcia account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 512362e9299SAlberto Garcia 51340119effSAlberto Garcia qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 51440119effSAlberto Garcia qdict_array_split(interval_dict, &interval_list); 51540119effSAlberto Garcia 51640119effSAlberto Garcia if (qdict_size(interval_dict) != 0) { 51740119effSAlberto Garcia error_setg(errp, "Invalid option stats-intervals.%s", 51840119effSAlberto Garcia qdict_first(interval_dict)->key); 51940119effSAlberto Garcia goto early_err; 52040119effSAlberto Garcia } 5212be5506fSAlberto Garcia 522fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 523fbf8175eSMax Reitz &detect_zeroes, &error); 524fbf8175eSMax Reitz if (error) { 525fbf8175eSMax Reitz error_propagate(errp, error); 526ec9c10d2SStefan Hajnoczi goto early_err; 527a9384affSPaolo Bonzini } 528666daa68SMarkus Armbruster 529666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 530c8057f95SPeter Maydell if (is_help_option(buf)) { 531807105a7SMarkus Armbruster error_printf("Supported formats:"); 532666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 533807105a7SMarkus Armbruster error_printf("\n"); 534ec9c10d2SStefan Hajnoczi goto early_err; 535666daa68SMarkus Armbruster } 53674fe54f2SKevin Wolf 537e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 538e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 539ec9c10d2SStefan Hajnoczi goto early_err; 5406db5f5d6SMike Qiu } 541e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 542666daa68SMarkus Armbruster } 543666daa68SMarkus Armbruster 54492aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 545666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 546b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 54784d18f06SMarkus Armbruster if (error) { 548b681072dSKevin Wolf error_propagate(errp, error); 549ec9c10d2SStefan Hajnoczi goto early_err; 550666daa68SMarkus Armbruster } 551666daa68SMarkus Armbruster } 552666daa68SMarkus Armbruster 55392aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 554666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 555b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 55684d18f06SMarkus Armbruster if (error) { 557b681072dSKevin Wolf error_propagate(errp, error); 558ec9c10d2SStefan Hajnoczi goto early_err; 559666daa68SMarkus Armbruster } 560666daa68SMarkus Armbruster } 561666daa68SMarkus Armbruster 562666daa68SMarkus Armbruster if (snapshot) { 56391a097e7SKevin Wolf bdrv_flags |= BDRV_O_SNAPSHOT; 564666daa68SMarkus Armbruster } 565666daa68SMarkus Armbruster 5665ec18f8cSMax Reitz /* init */ 56739c4ae94SKevin Wolf if ((!file || !*file) && !qdict_size(bs_opts)) { 5685ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5695ec18f8cSMax Reitz 5705ec18f8cSMax Reitz blk = blk_new(qemu_opts_id(opts), errp); 5715ec18f8cSMax Reitz if (!blk) { 5725ec18f8cSMax Reitz goto early_err; 5735ec18f8cSMax Reitz } 5745ec18f8cSMax Reitz 5755ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5765ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 577fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5785ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5795ec18f8cSMax Reitz 5805ec18f8cSMax Reitz if (throttle_enabled(&cfg)) { 5815ec18f8cSMax Reitz if (!throttling_group) { 5825ec18f8cSMax Reitz throttling_group = blk_name(blk); 5835ec18f8cSMax Reitz } 5845ec18f8cSMax Reitz blk_rs->throttle_group = g_strdup(throttling_group); 5855ec18f8cSMax Reitz blk_rs->throttle_state = throttle_group_incref(throttling_group); 5865ec18f8cSMax Reitz blk_rs->throttle_state->cfg = cfg; 5875ec18f8cSMax Reitz } 5885ec18f8cSMax Reitz 5895ec18f8cSMax Reitz QDECREF(bs_opts); 5905ec18f8cSMax Reitz } else { 5915ec18f8cSMax Reitz if (file && !*file) { 5925ec18f8cSMax Reitz file = NULL; 5935ec18f8cSMax Reitz } 5945ec18f8cSMax Reitz 59591a097e7SKevin Wolf /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 59691a097e7SKevin Wolf * with other callers) rather than what we want as the real defaults. 59791a097e7SKevin Wolf * Apply the defaults here instead. */ 59891a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on"); 59991a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 60091a097e7SKevin Wolf qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 60191a097e7SKevin Wolf 60291a097e7SKevin Wolf if (snapshot) { 60391a097e7SKevin Wolf /* always use cache=unsafe with snapshot */ 60491a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on")); 60591a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off")); 60691a097e7SKevin Wolf qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on")); 60791a097e7SKevin Wolf } 60891a097e7SKevin Wolf 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) { 1586c29b77f9SMarkus Armbruster error_reportf_err(local_error, 1587c29b77f9SMarkus Armbruster "Failed to delete snapshot with id '%s' and " 1588c29b77f9SMarkus Armbruster "name '%s' on device '%s' in abort: ", 1589c29b77f9SMarkus Armbruster sn->id_str, sn->name, 1590c29b77f9SMarkus Armbruster bdrv_get_device_name(bs)); 1591bbe86010SWenchao Xia } 1592bbe86010SWenchao Xia } 1593bbe86010SWenchao Xia 159450f43f0fSJohn Snow static void internal_snapshot_clean(BlkActionState *common) 15955d6e96efSStefan Hajnoczi { 15965d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15975d6e96efSStefan Hajnoczi common, common); 15985d6e96efSStefan Hajnoczi 15995d6e96efSStefan Hajnoczi if (state->aio_context) { 1600507306ccSFam Zheng if (state->bs) { 1601507306ccSFam Zheng bdrv_drained_end(state->bs); 1602507306ccSFam Zheng } 16035d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16045d6e96efSStefan Hajnoczi } 16055d6e96efSStefan Hajnoczi } 16065d6e96efSStefan Hajnoczi 1607ba0c86a3SWenchao Xia /* external snapshot private data */ 1608ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 160950f43f0fSJohn Snow BlkActionState common; 16108802d1fdSJeff Cody BlockDriverState *old_bs; 16118802d1fdSJeff Cody BlockDriverState *new_bs; 16125d6e96efSStefan Hajnoczi AioContext *aio_context; 1613ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 16148802d1fdSJeff Cody 161550f43f0fSJohn Snow static void external_snapshot_prepare(BlkActionState *common, 16169b9877eeSWenchao Xia Error **errp) 16179b9877eeSWenchao Xia { 161843de7e2dSAlberto Garcia int flags = 0, ret; 161943de7e2dSAlberto Garcia QDict *options = NULL; 16209b9877eeSWenchao Xia Error *local_err = NULL; 162143de7e2dSAlberto Garcia /* Device and node name of the image to generate the snapshot from */ 1622e2a31e87SWenchao Xia const char *device; 16230901f67eSBenoît Canet const char *node_name; 162443de7e2dSAlberto Garcia /* Reference to the new image (for 'blockdev-snapshot') */ 162543de7e2dSAlberto Garcia const char *snapshot_ref; 162643de7e2dSAlberto Garcia /* File name of the new image (for 'blockdev-snapshot-sync') */ 1627e2a31e87SWenchao Xia const char *new_image_file; 1628ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1629ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1630c8a83e85SKevin Wolf TransactionAction *action = common->action; 16319b9877eeSWenchao Xia 163243de7e2dSAlberto Garcia /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 163343de7e2dSAlberto Garcia * purpose but a different set of parameters */ 163443de7e2dSAlberto Garcia switch (action->type) { 163543de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 163643de7e2dSAlberto Garcia { 163743de7e2dSAlberto Garcia BlockdevSnapshot *s = action->u.blockdev_snapshot; 163843de7e2dSAlberto Garcia device = s->node; 163943de7e2dSAlberto Garcia node_name = s->node; 164043de7e2dSAlberto Garcia new_image_file = NULL; 164143de7e2dSAlberto Garcia snapshot_ref = s->overlay; 1642e2a31e87SWenchao Xia } 164343de7e2dSAlberto Garcia break; 164443de7e2dSAlberto Garcia case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 164543de7e2dSAlberto Garcia { 164643de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 164743de7e2dSAlberto Garcia device = s->has_device ? s->device : NULL; 164843de7e2dSAlberto Garcia node_name = s->has_node_name ? s->node_name : NULL; 164943de7e2dSAlberto Garcia new_image_file = s->snapshot_file; 165043de7e2dSAlberto Garcia snapshot_ref = NULL; 165143de7e2dSAlberto Garcia } 165243de7e2dSAlberto Garcia break; 165343de7e2dSAlberto Garcia default: 165443de7e2dSAlberto Garcia g_assert_not_reached(); 1655e2a31e87SWenchao Xia } 1656e2a31e87SWenchao Xia 1657e2a31e87SWenchao Xia /* start processing */ 165894d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 165994d16a64SJohn Snow return; 166094d16a64SJohn Snow } 166194d16a64SJohn Snow 166243de7e2dSAlberto Garcia state->old_bs = bdrv_lookup_bs(device, node_name, errp); 166343de7e2dSAlberto Garcia if (!state->old_bs) { 16649b9877eeSWenchao Xia return; 16659b9877eeSWenchao Xia } 16669b9877eeSWenchao Xia 16675d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 16685d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 16695d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1670da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 16715d6e96efSStefan Hajnoczi 1672ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1673c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 16749b9877eeSWenchao Xia return; 16759b9877eeSWenchao Xia } 16769b9877eeSWenchao Xia 16773718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 16783718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 16799b9877eeSWenchao Xia return; 16809b9877eeSWenchao Xia } 16819b9877eeSWenchao Xia 1682ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1683ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1684c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16859b9877eeSWenchao Xia return; 16869b9877eeSWenchao Xia } 16879b9877eeSWenchao Xia } 16889b9877eeSWenchao Xia 1689212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1690c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1691f6186f49SBenoît Canet return; 1692f6186f49SBenoît Canet } 1693f6186f49SBenoît Canet 169443de7e2dSAlberto Garcia if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 169543de7e2dSAlberto Garcia BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 169643de7e2dSAlberto Garcia const char *format = s->has_format ? s->format : "qcow2"; 169743de7e2dSAlberto Garcia enum NewImageMode mode; 169843de7e2dSAlberto Garcia const char *snapshot_node_name = 169943de7e2dSAlberto Garcia s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 170043de7e2dSAlberto Garcia 170143de7e2dSAlberto Garcia if (node_name && !snapshot_node_name) { 170243de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name missing"); 170343de7e2dSAlberto Garcia return; 170443de7e2dSAlberto Garcia } 170543de7e2dSAlberto Garcia 170643de7e2dSAlberto Garcia if (snapshot_node_name && 170743de7e2dSAlberto Garcia bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 170843de7e2dSAlberto Garcia error_setg(errp, "New snapshot node name already in use"); 170943de7e2dSAlberto Garcia return; 171043de7e2dSAlberto Garcia } 171143de7e2dSAlberto Garcia 1712ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 17139b9877eeSWenchao Xia 17149b9877eeSWenchao Xia /* create new image w/backing file */ 171543de7e2dSAlberto Garcia mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 17169b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 17179b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1718ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1719ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 17209b9877eeSWenchao Xia NULL, -1, flags, &local_err, false); 172184d18f06SMarkus Armbruster if (local_err) { 17229b9877eeSWenchao Xia error_propagate(errp, local_err); 17239b9877eeSWenchao Xia return; 17249b9877eeSWenchao Xia } 17259b9877eeSWenchao Xia } 17269b9877eeSWenchao Xia 17270901f67eSBenoît Canet options = qdict_new(); 172843de7e2dSAlberto Garcia if (s->has_snapshot_node_name) { 17290901f67eSBenoît Canet qdict_put(options, "node-name", 17300901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 17310901f67eSBenoît Canet } 1732e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 17330901f67eSBenoît Canet 173443de7e2dSAlberto Garcia flags |= BDRV_O_NO_BACKING; 173543de7e2dSAlberto Garcia } 173643de7e2dSAlberto Garcia 1737f67503e5SMax Reitz assert(state->new_bs == NULL); 173843de7e2dSAlberto Garcia ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 173943de7e2dSAlberto Garcia flags, errp); 1740f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 17419b9877eeSWenchao Xia if (ret != 0) { 174243de7e2dSAlberto Garcia return; 174343de7e2dSAlberto Garcia } 174443de7e2dSAlberto Garcia 174543de7e2dSAlberto Garcia if (state->new_bs->blk != NULL) { 174643de7e2dSAlberto Garcia error_setg(errp, "The snapshot is already in use by %s", 174743de7e2dSAlberto Garcia blk_name(state->new_bs->blk)); 174843de7e2dSAlberto Garcia return; 174943de7e2dSAlberto Garcia } 175043de7e2dSAlberto Garcia 175143de7e2dSAlberto Garcia if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 175243de7e2dSAlberto Garcia errp)) { 175343de7e2dSAlberto Garcia return; 175443de7e2dSAlberto Garcia } 175543de7e2dSAlberto Garcia 175643de7e2dSAlberto Garcia if (state->new_bs->backing != NULL) { 175743de7e2dSAlberto Garcia error_setg(errp, "The snapshot already has a backing image"); 175808b24cfeSAlberto Garcia return; 175908b24cfeSAlberto Garcia } 176008b24cfeSAlberto Garcia 176108b24cfeSAlberto Garcia if (!state->new_bs->drv->supports_backing) { 176208b24cfeSAlberto Garcia error_setg(errp, "The snapshot does not support backing images"); 17639b9877eeSWenchao Xia } 17649b9877eeSWenchao Xia } 17659b9877eeSWenchao Xia 176650f43f0fSJohn Snow static void external_snapshot_commit(BlkActionState *common) 17673b0047e8SWenchao Xia { 1768ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1769ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1770ba0c86a3SWenchao Xia 17715d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 17725d6e96efSStefan Hajnoczi 1773ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1774ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 17753b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 17763b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 17773b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1778dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 17793b0047e8SWenchao Xia NULL); 17803b0047e8SWenchao Xia } 17813b0047e8SWenchao Xia 178250f43f0fSJohn Snow static void external_snapshot_abort(BlkActionState *common) 178396b86bf7SWenchao Xia { 1784ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1785ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1786ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 17874f6fd349SFam Zheng bdrv_unref(state->new_bs); 178896b86bf7SWenchao Xia } 1789da763e83SFam Zheng } 1790da763e83SFam Zheng 179150f43f0fSJohn Snow static void external_snapshot_clean(BlkActionState *common) 1792da763e83SFam Zheng { 1793da763e83SFam Zheng ExternalSnapshotState *state = 1794da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 17955d6e96efSStefan Hajnoczi if (state->aio_context) { 1796da763e83SFam Zheng bdrv_drained_end(state->old_bs); 17975d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 17985d6e96efSStefan Hajnoczi } 179996b86bf7SWenchao Xia } 180096b86bf7SWenchao Xia 18013037f364SStefan Hajnoczi typedef struct DriveBackupState { 180250f43f0fSJohn Snow BlkActionState common; 18033037f364SStefan Hajnoczi BlockDriverState *bs; 18045d6e96efSStefan Hajnoczi AioContext *aio_context; 18053037f364SStefan Hajnoczi BlockJob *job; 18063037f364SStefan Hajnoczi } DriveBackupState; 18073037f364SStefan Hajnoczi 180878f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 180978f51fdeSJohn Snow bool has_format, const char *format, 181078f51fdeSJohn Snow enum MirrorSyncMode sync, 181178f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 181278f51fdeSJohn Snow bool has_speed, int64_t speed, 181378f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 181478f51fdeSJohn Snow bool has_on_source_error, 181578f51fdeSJohn Snow BlockdevOnError on_source_error, 181678f51fdeSJohn Snow bool has_on_target_error, 181778f51fdeSJohn Snow BlockdevOnError on_target_error, 181878f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 181978f51fdeSJohn Snow 182050f43f0fSJohn Snow static void drive_backup_prepare(BlkActionState *common, Error **errp) 18213037f364SStefan Hajnoczi { 18223037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1823a0e8544cSFam Zheng BlockBackend *blk; 18243037f364SStefan Hajnoczi DriveBackup *backup; 18253037f364SStefan Hajnoczi Error *local_err = NULL; 18263037f364SStefan Hajnoczi 18276a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 18286a8f9661SEric Blake backup = common->action->u.drive_backup; 18293037f364SStefan Hajnoczi 1830a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1831a0e8544cSFam Zheng if (!blk) { 183275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 183375158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 18345d6e96efSStefan Hajnoczi return; 18355d6e96efSStefan Hajnoczi } 18365d6e96efSStefan Hajnoczi 18371fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 18381fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 18391fdd4b7bSFam Zheng return; 18401fdd4b7bSFam Zheng } 18411fdd4b7bSFam Zheng 18425d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 18435433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 18445d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 18451fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 18461fdd4b7bSFam Zheng state->bs = blk_bs(blk); 18475d6e96efSStefan Hajnoczi 184878f51fdeSJohn Snow do_drive_backup(backup->device, backup->target, 18493037f364SStefan Hajnoczi backup->has_format, backup->format, 1850b53169eaSStefan Hajnoczi backup->sync, 18513037f364SStefan Hajnoczi backup->has_mode, backup->mode, 18523037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1853d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 18543037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 18553037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 185694d16a64SJohn Snow common->block_job_txn, &local_err); 185784d18f06SMarkus Armbruster if (local_err) { 18583037f364SStefan Hajnoczi error_propagate(errp, local_err); 18593037f364SStefan Hajnoczi return; 18603037f364SStefan Hajnoczi } 18613037f364SStefan Hajnoczi 18623037f364SStefan Hajnoczi state->job = state->bs->job; 18633037f364SStefan Hajnoczi } 18643037f364SStefan Hajnoczi 186550f43f0fSJohn Snow static void drive_backup_abort(BlkActionState *common) 18663037f364SStefan Hajnoczi { 18673037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18683037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 18693037f364SStefan Hajnoczi 18703037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 18713037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 18723037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 18733037f364SStefan Hajnoczi } 18743037f364SStefan Hajnoczi } 18753037f364SStefan Hajnoczi 187650f43f0fSJohn Snow static void drive_backup_clean(BlkActionState *common) 18775d6e96efSStefan Hajnoczi { 18785d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 18795d6e96efSStefan Hajnoczi 18805d6e96efSStefan Hajnoczi if (state->aio_context) { 18811fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 18825d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 18835d6e96efSStefan Hajnoczi } 18845d6e96efSStefan Hajnoczi } 18855d6e96efSStefan Hajnoczi 1886bd8baecdSFam Zheng typedef struct BlockdevBackupState { 188750f43f0fSJohn Snow BlkActionState common; 1888bd8baecdSFam Zheng BlockDriverState *bs; 1889bd8baecdSFam Zheng BlockJob *job; 1890bd8baecdSFam Zheng AioContext *aio_context; 1891bd8baecdSFam Zheng } BlockdevBackupState; 1892bd8baecdSFam Zheng 189378f51fdeSJohn Snow static void do_blockdev_backup(const char *device, const char *target, 189478f51fdeSJohn Snow enum MirrorSyncMode sync, 189578f51fdeSJohn Snow bool has_speed, int64_t speed, 189678f51fdeSJohn Snow bool has_on_source_error, 189778f51fdeSJohn Snow BlockdevOnError on_source_error, 189878f51fdeSJohn Snow bool has_on_target_error, 189978f51fdeSJohn Snow BlockdevOnError on_target_error, 190078f51fdeSJohn Snow BlockJobTxn *txn, Error **errp); 190178f51fdeSJohn Snow 190250f43f0fSJohn Snow static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1903bd8baecdSFam Zheng { 1904bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1905bd8baecdSFam Zheng BlockdevBackup *backup; 19065433c24fSMax Reitz BlockBackend *blk, *target; 1907bd8baecdSFam Zheng Error *local_err = NULL; 1908bd8baecdSFam Zheng 19096a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 19106a8f9661SEric Blake backup = common->action->u.blockdev_backup; 1911bd8baecdSFam Zheng 1912a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1913a0e8544cSFam Zheng if (!blk) { 19145b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1915bd8baecdSFam Zheng return; 1916bd8baecdSFam Zheng } 1917bd8baecdSFam Zheng 1918ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1919ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1920ff52bf36SFam Zheng return; 1921ff52bf36SFam Zheng } 1922ff52bf36SFam Zheng 19235433c24fSMax Reitz target = blk_by_name(backup->target); 19245433c24fSMax Reitz if (!target) { 19255b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1926bd8baecdSFam Zheng return; 1927bd8baecdSFam Zheng } 1928bd8baecdSFam Zheng 1929bd8baecdSFam Zheng /* AioContext is released in .clean() */ 19305433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 19315433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1932bd8baecdSFam Zheng state->aio_context = NULL; 1933bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1934bd8baecdSFam Zheng return; 1935bd8baecdSFam Zheng } 1936bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1937ff52bf36SFam Zheng state->bs = blk_bs(blk); 1938ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1939bd8baecdSFam Zheng 194078f51fdeSJohn Snow do_blockdev_backup(backup->device, backup->target, 1941bd8baecdSFam Zheng backup->sync, 1942bd8baecdSFam Zheng backup->has_speed, backup->speed, 1943bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1944bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 194594d16a64SJohn Snow common->block_job_txn, &local_err); 1946bd8baecdSFam Zheng if (local_err) { 1947bd8baecdSFam Zheng error_propagate(errp, local_err); 1948bd8baecdSFam Zheng return; 1949bd8baecdSFam Zheng } 1950bd8baecdSFam Zheng 1951bd8baecdSFam Zheng state->job = state->bs->job; 1952bd8baecdSFam Zheng } 1953bd8baecdSFam Zheng 195450f43f0fSJohn Snow static void blockdev_backup_abort(BlkActionState *common) 1955bd8baecdSFam Zheng { 1956bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1957bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1958bd8baecdSFam Zheng 1959bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1960bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1961bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1962bd8baecdSFam Zheng } 1963bd8baecdSFam Zheng } 1964bd8baecdSFam Zheng 196550f43f0fSJohn Snow static void blockdev_backup_clean(BlkActionState *common) 1966bd8baecdSFam Zheng { 1967bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1968bd8baecdSFam Zheng 1969bd8baecdSFam Zheng if (state->aio_context) { 1970ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1971bd8baecdSFam Zheng aio_context_release(state->aio_context); 1972bd8baecdSFam Zheng } 1973bd8baecdSFam Zheng } 1974bd8baecdSFam Zheng 1975df9a681dSFam Zheng typedef struct BlockDirtyBitmapState { 197650f43f0fSJohn Snow BlkActionState common; 1977df9a681dSFam Zheng BdrvDirtyBitmap *bitmap; 1978df9a681dSFam Zheng BlockDriverState *bs; 1979df9a681dSFam Zheng AioContext *aio_context; 1980df9a681dSFam Zheng HBitmap *backup; 1981df9a681dSFam Zheng bool prepared; 1982df9a681dSFam Zheng } BlockDirtyBitmapState; 1983df9a681dSFam Zheng 198450f43f0fSJohn Snow static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1985df9a681dSFam Zheng Error **errp) 1986df9a681dSFam Zheng { 1987df9a681dSFam Zheng Error *local_err = NULL; 1988df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 1989df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1990df9a681dSFam Zheng common, common); 1991df9a681dSFam Zheng 199294d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 199394d16a64SJohn Snow return; 199494d16a64SJohn Snow } 199594d16a64SJohn Snow 199650f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 1997df9a681dSFam Zheng /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1998df9a681dSFam Zheng qmp_block_dirty_bitmap_add(action->node, action->name, 1999df9a681dSFam Zheng action->has_granularity, action->granularity, 2000df9a681dSFam Zheng &local_err); 2001df9a681dSFam Zheng 2002df9a681dSFam Zheng if (!local_err) { 2003df9a681dSFam Zheng state->prepared = true; 2004df9a681dSFam Zheng } else { 2005df9a681dSFam Zheng error_propagate(errp, local_err); 2006df9a681dSFam Zheng } 2007df9a681dSFam Zheng } 2008df9a681dSFam Zheng 200950f43f0fSJohn Snow static void block_dirty_bitmap_add_abort(BlkActionState *common) 2010df9a681dSFam Zheng { 2011df9a681dSFam Zheng BlockDirtyBitmapAdd *action; 2012df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2013df9a681dSFam Zheng common, common); 2014df9a681dSFam Zheng 201550f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_add; 2016df9a681dSFam Zheng /* Should not be able to fail: IF the bitmap was added via .prepare(), 2017df9a681dSFam Zheng * then the node reference and bitmap name must have been valid. 2018df9a681dSFam Zheng */ 2019df9a681dSFam Zheng if (state->prepared) { 2020df9a681dSFam Zheng qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2021df9a681dSFam Zheng } 2022df9a681dSFam Zheng } 2023df9a681dSFam Zheng 202450f43f0fSJohn Snow static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2025df9a681dSFam Zheng Error **errp) 2026df9a681dSFam Zheng { 2027df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2028df9a681dSFam Zheng common, common); 2029df9a681dSFam Zheng BlockDirtyBitmap *action; 2030df9a681dSFam Zheng 203194d16a64SJohn Snow if (action_check_completion_mode(common, errp) < 0) { 203294d16a64SJohn Snow return; 203394d16a64SJohn Snow } 203494d16a64SJohn Snow 203550f43f0fSJohn Snow action = common->action->u.block_dirty_bitmap_clear; 2036df9a681dSFam Zheng state->bitmap = block_dirty_bitmap_lookup(action->node, 2037df9a681dSFam Zheng action->name, 2038df9a681dSFam Zheng &state->bs, 2039df9a681dSFam Zheng &state->aio_context, 2040df9a681dSFam Zheng errp); 2041df9a681dSFam Zheng if (!state->bitmap) { 2042df9a681dSFam Zheng return; 2043df9a681dSFam Zheng } 2044df9a681dSFam Zheng 2045df9a681dSFam Zheng if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2046df9a681dSFam Zheng error_setg(errp, "Cannot modify a frozen bitmap"); 2047df9a681dSFam Zheng return; 2048df9a681dSFam Zheng } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2049df9a681dSFam Zheng error_setg(errp, "Cannot clear a disabled bitmap"); 2050df9a681dSFam Zheng return; 2051df9a681dSFam Zheng } 2052df9a681dSFam Zheng 2053df9a681dSFam Zheng bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2054df9a681dSFam Zheng /* AioContext is released in .clean() */ 2055df9a681dSFam Zheng } 2056df9a681dSFam Zheng 205750f43f0fSJohn Snow static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2058df9a681dSFam Zheng { 2059df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2060df9a681dSFam Zheng common, common); 2061df9a681dSFam Zheng 2062df9a681dSFam Zheng bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2063df9a681dSFam Zheng } 2064df9a681dSFam Zheng 206550f43f0fSJohn Snow static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2066df9a681dSFam Zheng { 2067df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2068df9a681dSFam Zheng common, common); 2069df9a681dSFam Zheng 2070df9a681dSFam Zheng hbitmap_free(state->backup); 2071df9a681dSFam Zheng } 2072df9a681dSFam Zheng 207350f43f0fSJohn Snow static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2074df9a681dSFam Zheng { 2075df9a681dSFam Zheng BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2076df9a681dSFam Zheng common, common); 2077df9a681dSFam Zheng 2078df9a681dSFam Zheng if (state->aio_context) { 2079df9a681dSFam Zheng aio_context_release(state->aio_context); 2080df9a681dSFam Zheng } 2081df9a681dSFam Zheng } 2082df9a681dSFam Zheng 208350f43f0fSJohn Snow static void abort_prepare(BlkActionState *common, Error **errp) 208478b18b78SStefan Hajnoczi { 208578b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 208678b18b78SStefan Hajnoczi } 208778b18b78SStefan Hajnoczi 208850f43f0fSJohn Snow static void abort_commit(BlkActionState *common) 208978b18b78SStefan Hajnoczi { 2090dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 209178b18b78SStefan Hajnoczi } 209278b18b78SStefan Hajnoczi 209350f43f0fSJohn Snow static const BlkActionOps actions[] = { 209443de7e2dSAlberto Garcia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 209543de7e2dSAlberto Garcia .instance_size = sizeof(ExternalSnapshotState), 209643de7e2dSAlberto Garcia .prepare = external_snapshot_prepare, 209743de7e2dSAlberto Garcia .commit = external_snapshot_commit, 209843de7e2dSAlberto Garcia .abort = external_snapshot_abort, 20994ad6f3dbSAlberto Garcia .clean = external_snapshot_clean, 210043de7e2dSAlberto Garcia }, 2101c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2102ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 2103ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 2104ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 2105ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 2106da763e83SFam Zheng .clean = external_snapshot_clean, 2107ba0c86a3SWenchao Xia }, 21083037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 21093037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 21103037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 21113037f364SStefan Hajnoczi .abort = drive_backup_abort, 21125d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 21133037f364SStefan Hajnoczi }, 2114bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2115bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 2116bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 2117bd8baecdSFam Zheng .abort = blockdev_backup_abort, 2118bd8baecdSFam Zheng .clean = blockdev_backup_clean, 2119bd8baecdSFam Zheng }, 212078b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 212150f43f0fSJohn Snow .instance_size = sizeof(BlkActionState), 212278b18b78SStefan Hajnoczi .prepare = abort_prepare, 212378b18b78SStefan Hajnoczi .commit = abort_commit, 212478b18b78SStefan Hajnoczi }, 2125bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2126bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 2127bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 2128bbe86010SWenchao Xia .abort = internal_snapshot_abort, 21295d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 2130bbe86010SWenchao Xia }, 2131df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2132df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2133df9a681dSFam Zheng .prepare = block_dirty_bitmap_add_prepare, 2134df9a681dSFam Zheng .abort = block_dirty_bitmap_add_abort, 2135df9a681dSFam Zheng }, 2136df9a681dSFam Zheng [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2137df9a681dSFam Zheng .instance_size = sizeof(BlockDirtyBitmapState), 2138df9a681dSFam Zheng .prepare = block_dirty_bitmap_clear_prepare, 2139df9a681dSFam Zheng .commit = block_dirty_bitmap_clear_commit, 2140df9a681dSFam Zheng .abort = block_dirty_bitmap_clear_abort, 2141df9a681dSFam Zheng .clean = block_dirty_bitmap_clear_clean, 2142df9a681dSFam Zheng } 2143ba0c86a3SWenchao Xia }; 2144ba0c86a3SWenchao Xia 214594d16a64SJohn Snow /** 214694d16a64SJohn Snow * Allocate a TransactionProperties structure if necessary, and fill 214794d16a64SJohn Snow * that structure with desired defaults if they are unset. 214894d16a64SJohn Snow */ 214994d16a64SJohn Snow static TransactionProperties *get_transaction_properties( 215094d16a64SJohn Snow TransactionProperties *props) 215194d16a64SJohn Snow { 215294d16a64SJohn Snow if (!props) { 215394d16a64SJohn Snow props = g_new0(TransactionProperties, 1); 215494d16a64SJohn Snow } 215594d16a64SJohn Snow 215694d16a64SJohn Snow if (!props->has_completion_mode) { 215794d16a64SJohn Snow props->has_completion_mode = true; 215894d16a64SJohn Snow props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 215994d16a64SJohn Snow } 216094d16a64SJohn Snow 216194d16a64SJohn Snow return props; 216294d16a64SJohn Snow } 216394d16a64SJohn Snow 21648802d1fdSJeff Cody /* 2165b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 2166b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 21678802d1fdSJeff Cody */ 216894d16a64SJohn Snow void qmp_transaction(TransactionActionList *dev_list, 216994d16a64SJohn Snow bool has_props, 217094d16a64SJohn Snow struct TransactionProperties *props, 217194d16a64SJohn Snow Error **errp) 21728802d1fdSJeff Cody { 2173c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 217494d16a64SJohn Snow BlockJobTxn *block_job_txn = NULL; 217550f43f0fSJohn Snow BlkActionState *state, *next; 217643e17041SLuiz Capitulino Error *local_err = NULL; 21778802d1fdSJeff Cody 217850f43f0fSJohn Snow QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 21798802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 21808802d1fdSJeff Cody 218194d16a64SJohn Snow /* Does this transaction get canceled as a group on failure? 218294d16a64SJohn Snow * If not, we don't really need to make a BlockJobTxn. 218394d16a64SJohn Snow */ 218494d16a64SJohn Snow props = get_transaction_properties(props); 218594d16a64SJohn Snow if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 218694d16a64SJohn Snow block_job_txn = block_job_txn_new(); 218794d16a64SJohn Snow } 218894d16a64SJohn Snow 2189b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 21908802d1fdSJeff Cody bdrv_drain_all(); 21918802d1fdSJeff Cody 2192b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 21938802d1fdSJeff Cody while (NULL != dev_entry) { 2194c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 219550f43f0fSJohn Snow const BlkActionOps *ops; 219652e7c241SPaolo Bonzini 21978802d1fdSJeff Cody dev_info = dev_entry->value; 21988802d1fdSJeff Cody dev_entry = dev_entry->next; 21998802d1fdSJeff Cody 22006a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 2201ba0c86a3SWenchao Xia 22026a8f9661SEric Blake ops = &actions[dev_info->type]; 2203aa3fe714SMax Reitz assert(ops->instance_size > 0); 2204aa3fe714SMax Reitz 2205ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 2206ba5d6ab6SStefan Hajnoczi state->ops = ops; 2207ba5d6ab6SStefan Hajnoczi state->action = dev_info; 220894d16a64SJohn Snow state->block_job_txn = block_job_txn; 220994d16a64SJohn Snow state->txn_props = props; 2210ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 22118802d1fdSJeff Cody 2212ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 221384d18f06SMarkus Armbruster if (local_err) { 22149b9877eeSWenchao Xia error_propagate(errp, local_err); 22159b9877eeSWenchao Xia goto delete_and_fail; 22169b9877eeSWenchao Xia } 221752e7c241SPaolo Bonzini } 22188802d1fdSJeff Cody 2219ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2220f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 2221ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 22228802d1fdSJeff Cody } 2223f9ea81e8SStefan Hajnoczi } 22248802d1fdSJeff Cody 22258802d1fdSJeff Cody /* success */ 22268802d1fdSJeff Cody goto exit; 22278802d1fdSJeff Cody 22288802d1fdSJeff Cody delete_and_fail: 2229b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 2230ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2231ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 2232ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 2233ba0c86a3SWenchao Xia } 22348802d1fdSJeff Cody } 22358802d1fdSJeff Cody exit: 2236ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2237ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 2238ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 2239ba0c86a3SWenchao Xia } 2240ba5d6ab6SStefan Hajnoczi g_free(state); 22418802d1fdSJeff Cody } 224294d16a64SJohn Snow if (!has_props) { 224394d16a64SJohn Snow qapi_free_TransactionProperties(props); 224494d16a64SJohn Snow } 224594d16a64SJohn Snow block_job_txn_unref(block_job_txn); 22468802d1fdSJeff Cody } 22478802d1fdSJeff Cody 2248c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2249666daa68SMarkus Armbruster { 225038f54bd1SMax Reitz Error *local_err = NULL; 2251666daa68SMarkus Armbruster 225238f54bd1SMax Reitz qmp_blockdev_open_tray(device, has_force, force, &local_err); 225338f54bd1SMax Reitz if (local_err) { 225438f54bd1SMax Reitz error_propagate(errp, local_err); 2255c245b6a3SLuiz Capitulino return; 2256666daa68SMarkus Armbruster } 225792d48558SLuiz Capitulino 22586e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, errp); 2259666daa68SMarkus Armbruster } 2260666daa68SMarkus Armbruster 226112d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 226212d3ba82SBenoît Canet bool has_node_name, const char *node_name, 226312d3ba82SBenoît Canet const char *password, Error **errp) 2264666daa68SMarkus Armbruster { 226512d3ba82SBenoît Canet Error *local_err = NULL; 2266666daa68SMarkus Armbruster BlockDriverState *bs; 2267e3442099SStefan Hajnoczi AioContext *aio_context; 2268666daa68SMarkus Armbruster 226912d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 227012d3ba82SBenoît Canet has_node_name ? node_name : NULL, 227112d3ba82SBenoît Canet &local_err); 227284d18f06SMarkus Armbruster if (local_err) { 227312d3ba82SBenoît Canet error_propagate(errp, local_err); 2274a4dea8a9SLuiz Capitulino return; 2275666daa68SMarkus Armbruster } 2276666daa68SMarkus Armbruster 2277e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2278e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2279e3442099SStefan Hajnoczi 22804d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2281666daa68SMarkus Armbruster 2282e3442099SStefan Hajnoczi aio_context_release(aio_context); 2283e3442099SStefan Hajnoczi } 2284e3442099SStefan Hajnoczi 22857d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 22867d8a9f71SMax Reitz Error **errp) 22877d8a9f71SMax Reitz { 22887d8a9f71SMax Reitz BlockBackend *blk; 22897d8a9f71SMax Reitz bool locked; 22907d8a9f71SMax Reitz 22917d8a9f71SMax Reitz if (!has_force) { 22927d8a9f71SMax Reitz force = false; 22937d8a9f71SMax Reitz } 22947d8a9f71SMax Reitz 22957d8a9f71SMax Reitz blk = blk_by_name(device); 22967d8a9f71SMax Reitz if (!blk) { 22977d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 22987d8a9f71SMax Reitz "Device '%s' not found", device); 22997d8a9f71SMax Reitz return; 23007d8a9f71SMax Reitz } 23017d8a9f71SMax Reitz 23027d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 23037d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23047d8a9f71SMax Reitz return; 23057d8a9f71SMax Reitz } 23067d8a9f71SMax Reitz 23077d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 23087d8a9f71SMax Reitz return; 23097d8a9f71SMax Reitz } 23107d8a9f71SMax Reitz 23117d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 23127d8a9f71SMax Reitz if (locked) { 23137d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 23147d8a9f71SMax Reitz } 23157d8a9f71SMax Reitz 23167d8a9f71SMax Reitz if (!locked || force) { 23177d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 23187d8a9f71SMax Reitz } 23197d8a9f71SMax Reitz } 23207d8a9f71SMax Reitz 2321abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2322abaaf59dSMax Reitz { 2323abaaf59dSMax Reitz BlockBackend *blk; 2324abaaf59dSMax Reitz 2325abaaf59dSMax Reitz blk = blk_by_name(device); 2326abaaf59dSMax Reitz if (!blk) { 2327abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2328abaaf59dSMax Reitz "Device '%s' not found", device); 2329abaaf59dSMax Reitz return; 2330abaaf59dSMax Reitz } 2331abaaf59dSMax Reitz 2332abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2333abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2334abaaf59dSMax Reitz return; 2335abaaf59dSMax Reitz } 2336abaaf59dSMax Reitz 2337abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2338abaaf59dSMax Reitz return; 2339abaaf59dSMax Reitz } 2340abaaf59dSMax Reitz 2341abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2342abaaf59dSMax Reitz } 2343abaaf59dSMax Reitz 23446e0abc25SMax Reitz void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 23452814f672SMax Reitz { 23462814f672SMax Reitz BlockBackend *blk; 23472814f672SMax Reitz BlockDriverState *bs; 23482814f672SMax Reitz AioContext *aio_context; 23492814f672SMax Reitz bool has_device; 23502814f672SMax Reitz 23512814f672SMax Reitz blk = blk_by_name(device); 23522814f672SMax Reitz if (!blk) { 23532814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 23542814f672SMax Reitz "Device '%s' not found", device); 23552814f672SMax Reitz return; 23562814f672SMax Reitz } 23572814f672SMax Reitz 23582814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 23592814f672SMax Reitz has_device = blk_get_attached_dev(blk); 23602814f672SMax Reitz 23612814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 23622814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 23632814f672SMax Reitz return; 23642814f672SMax Reitz } 23652814f672SMax Reitz 23662814f672SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 23672814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 23682814f672SMax Reitz return; 23692814f672SMax Reitz } 23702814f672SMax Reitz 23712814f672SMax Reitz bs = blk_bs(blk); 23722814f672SMax Reitz if (!bs) { 23732814f672SMax Reitz return; 23742814f672SMax Reitz } 23752814f672SMax Reitz 23762814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 23772814f672SMax Reitz aio_context_acquire(aio_context); 23782814f672SMax Reitz 23792814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 23802814f672SMax Reitz goto out; 23812814f672SMax Reitz } 23822814f672SMax Reitz 23832814f672SMax Reitz /* This follows the convention established by bdrv_make_anon() */ 23842814f672SMax Reitz if (bs->device_list.tqe_prev) { 23852814f672SMax Reitz QTAILQ_REMOVE(&bdrv_states, bs, device_list); 23862814f672SMax Reitz bs->device_list.tqe_prev = NULL; 23872814f672SMax Reitz } 23882814f672SMax Reitz 23892814f672SMax Reitz blk_remove_bs(blk); 23902814f672SMax Reitz 23912814f672SMax Reitz out: 23922814f672SMax Reitz aio_context_release(aio_context); 23932814f672SMax Reitz } 23942814f672SMax Reitz 2395d1299882SMax Reitz static void qmp_blockdev_insert_anon_medium(const char *device, 2396d1299882SMax Reitz BlockDriverState *bs, Error **errp) 2397d1299882SMax Reitz { 2398d1299882SMax Reitz BlockBackend *blk; 2399d1299882SMax Reitz bool has_device; 2400d1299882SMax Reitz 2401d1299882SMax Reitz blk = blk_by_name(device); 2402d1299882SMax Reitz if (!blk) { 2403d1299882SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2404d1299882SMax Reitz "Device '%s' not found", device); 2405d1299882SMax Reitz return; 2406d1299882SMax Reitz } 2407d1299882SMax Reitz 2408d1299882SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2409d1299882SMax Reitz has_device = blk_get_attached_dev(blk); 2410d1299882SMax Reitz 2411d1299882SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2412d1299882SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2413d1299882SMax Reitz return; 2414d1299882SMax Reitz } 2415d1299882SMax Reitz 2416d1299882SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 2417d1299882SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2418d1299882SMax Reitz return; 2419d1299882SMax Reitz } 2420d1299882SMax Reitz 2421d1299882SMax Reitz if (blk_bs(blk)) { 2422d1299882SMax Reitz error_setg(errp, "There already is a medium in device '%s'", device); 2423d1299882SMax Reitz return; 2424d1299882SMax Reitz } 2425d1299882SMax Reitz 2426d1299882SMax Reitz blk_insert_bs(blk, bs); 2427d1299882SMax Reitz 2428d1299882SMax Reitz QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 2429d1299882SMax Reitz } 2430d1299882SMax Reitz 24316e0abc25SMax Reitz void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2432d1299882SMax Reitz Error **errp) 2433d1299882SMax Reitz { 2434d1299882SMax Reitz BlockDriverState *bs; 2435d1299882SMax Reitz 2436d1299882SMax Reitz bs = bdrv_find_node(node_name); 2437d1299882SMax Reitz if (!bs) { 2438d1299882SMax Reitz error_setg(errp, "Node '%s' not found", node_name); 2439d1299882SMax Reitz return; 2440d1299882SMax Reitz } 2441d1299882SMax Reitz 2442d1299882SMax Reitz if (bs->blk) { 2443d1299882SMax Reitz error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2444d1299882SMax Reitz blk_name(bs->blk)); 2445d1299882SMax Reitz return; 2446d1299882SMax Reitz } 2447d1299882SMax Reitz 2448d1299882SMax Reitz qmp_blockdev_insert_anon_medium(device, bs, errp); 2449d1299882SMax Reitz } 2450d1299882SMax Reitz 245124fb4133SMax Reitz void qmp_blockdev_change_medium(const char *device, const char *filename, 245224fb4133SMax Reitz bool has_format, const char *format, 245339ff43d9SMax Reitz bool has_read_only, 245439ff43d9SMax Reitz BlockdevChangeReadOnlyMode read_only, 245524fb4133SMax Reitz Error **errp) 2456de2c6c05SMax Reitz { 2457de2c6c05SMax Reitz BlockBackend *blk; 2458de2c6c05SMax Reitz BlockDriverState *medium_bs = NULL; 2459de2c6c05SMax Reitz int bdrv_flags, ret; 2460de2c6c05SMax Reitz QDict *options = NULL; 2461de2c6c05SMax Reitz Error *err = NULL; 2462de2c6c05SMax Reitz 2463de2c6c05SMax Reitz blk = blk_by_name(device); 2464de2c6c05SMax Reitz if (!blk) { 2465de2c6c05SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2466de2c6c05SMax Reitz "Device '%s' not found", device); 2467de2c6c05SMax Reitz goto fail; 2468de2c6c05SMax Reitz } 2469de2c6c05SMax Reitz 2470de2c6c05SMax Reitz if (blk_bs(blk)) { 2471de2c6c05SMax Reitz blk_update_root_state(blk); 2472de2c6c05SMax Reitz } 2473de2c6c05SMax Reitz 2474de2c6c05SMax Reitz bdrv_flags = blk_get_open_flags_from_root_state(blk); 2475de2c6c05SMax Reitz 247639ff43d9SMax Reitz if (!has_read_only) { 247739ff43d9SMax Reitz read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 247839ff43d9SMax Reitz } 247939ff43d9SMax Reitz 248039ff43d9SMax Reitz switch (read_only) { 248139ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 248239ff43d9SMax Reitz break; 248339ff43d9SMax Reitz 248439ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 248539ff43d9SMax Reitz bdrv_flags &= ~BDRV_O_RDWR; 248639ff43d9SMax Reitz break; 248739ff43d9SMax Reitz 248839ff43d9SMax Reitz case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 248939ff43d9SMax Reitz bdrv_flags |= BDRV_O_RDWR; 249039ff43d9SMax Reitz break; 249139ff43d9SMax Reitz 249239ff43d9SMax Reitz default: 249339ff43d9SMax Reitz abort(); 249439ff43d9SMax Reitz } 249539ff43d9SMax Reitz 249624fb4133SMax Reitz if (has_format) { 2497de2c6c05SMax Reitz options = qdict_new(); 2498de2c6c05SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2499de2c6c05SMax Reitz } 2500de2c6c05SMax Reitz 2501de2c6c05SMax Reitz assert(!medium_bs); 2502de2c6c05SMax Reitz ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2503de2c6c05SMax Reitz if (ret < 0) { 2504de2c6c05SMax Reitz goto fail; 2505de2c6c05SMax Reitz } 2506de2c6c05SMax Reitz 2507de2c6c05SMax Reitz blk_apply_root_state(blk, medium_bs); 2508de2c6c05SMax Reitz 2509de2c6c05SMax Reitz bdrv_add_key(medium_bs, NULL, &err); 2510de2c6c05SMax Reitz if (err) { 2511de2c6c05SMax Reitz error_propagate(errp, err); 2512de2c6c05SMax Reitz goto fail; 2513de2c6c05SMax Reitz } 2514de2c6c05SMax Reitz 2515de2c6c05SMax Reitz qmp_blockdev_open_tray(device, false, false, &err); 2516de2c6c05SMax Reitz if (err) { 2517de2c6c05SMax Reitz error_propagate(errp, err); 2518de2c6c05SMax Reitz goto fail; 2519de2c6c05SMax Reitz } 2520de2c6c05SMax Reitz 25216e0abc25SMax Reitz qmp_x_blockdev_remove_medium(device, &err); 2522de2c6c05SMax Reitz if (err) { 2523de2c6c05SMax Reitz error_propagate(errp, err); 2524de2c6c05SMax Reitz goto fail; 2525de2c6c05SMax Reitz } 2526de2c6c05SMax Reitz 2527de2c6c05SMax Reitz qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2528de2c6c05SMax Reitz if (err) { 2529de2c6c05SMax Reitz error_propagate(errp, err); 2530de2c6c05SMax Reitz goto fail; 2531de2c6c05SMax Reitz } 2532de2c6c05SMax Reitz 2533de2c6c05SMax Reitz qmp_blockdev_close_tray(device, errp); 2534de2c6c05SMax Reitz 2535de2c6c05SMax Reitz fail: 2536de2c6c05SMax Reitz /* If the medium has been inserted, the device has its own reference, so 2537de2c6c05SMax Reitz * ours must be relinquished; and if it has not been inserted successfully, 2538de2c6c05SMax Reitz * the reference must be relinquished anyway */ 2539de2c6c05SMax Reitz bdrv_unref(medium_bs); 2540de2c6c05SMax Reitz } 2541de2c6c05SMax Reitz 2542727f005eSZhi Yong Wu /* throttling disk I/O limits */ 254380047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 25443e9fab69SBenoît Canet int64_t bps_wr, 25453e9fab69SBenoît Canet int64_t iops, 25463e9fab69SBenoît Canet int64_t iops_rd, 25473e9fab69SBenoît Canet int64_t iops_wr, 25483e9fab69SBenoît Canet bool has_bps_max, 25493e9fab69SBenoît Canet int64_t bps_max, 25503e9fab69SBenoît Canet bool has_bps_rd_max, 25513e9fab69SBenoît Canet int64_t bps_rd_max, 25523e9fab69SBenoît Canet bool has_bps_wr_max, 25533e9fab69SBenoît Canet int64_t bps_wr_max, 25543e9fab69SBenoît Canet bool has_iops_max, 25553e9fab69SBenoît Canet int64_t iops_max, 25563e9fab69SBenoît Canet bool has_iops_rd_max, 25573e9fab69SBenoît Canet int64_t iops_rd_max, 25583e9fab69SBenoît Canet bool has_iops_wr_max, 25592024c1dfSBenoît Canet int64_t iops_wr_max, 25602024c1dfSBenoît Canet bool has_iops_size, 256176f4afb4SAlberto Garcia int64_t iops_size, 256276f4afb4SAlberto Garcia bool has_group, 256376f4afb4SAlberto Garcia const char *group, Error **errp) 2564727f005eSZhi Yong Wu { 2565cc0681c4SBenoît Canet ThrottleConfig cfg; 2566727f005eSZhi Yong Wu BlockDriverState *bs; 2567a0e8544cSFam Zheng BlockBackend *blk; 2568b15446fdSStefan Hajnoczi AioContext *aio_context; 2569727f005eSZhi Yong Wu 2570a0e8544cSFam Zheng blk = blk_by_name(device); 2571a0e8544cSFam Zheng if (!blk) { 257275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 257375158ebbSMarkus Armbruster "Device '%s' not found", device); 257480047da5SLuiz Capitulino return; 2575727f005eSZhi Yong Wu } 25765433c24fSMax Reitz 25775433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 25785433c24fSMax Reitz aio_context_acquire(aio_context); 25795433c24fSMax Reitz 2580a0e8544cSFam Zheng bs = blk_bs(blk); 25815433c24fSMax Reitz if (!bs) { 25825433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 25835433c24fSMax Reitz goto out; 25845433c24fSMax Reitz } 2585727f005eSZhi Yong Wu 2586cc0681c4SBenoît Canet memset(&cfg, 0, sizeof(cfg)); 2587cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2588cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2589cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2590727f005eSZhi Yong Wu 2591cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2592cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2593cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2594cc0681c4SBenoît Canet 25953e9fab69SBenoît Canet if (has_bps_max) { 25963e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 25973e9fab69SBenoît Canet } 25983e9fab69SBenoît Canet if (has_bps_rd_max) { 25993e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 26003e9fab69SBenoît Canet } 26013e9fab69SBenoît Canet if (has_bps_wr_max) { 26023e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 26033e9fab69SBenoît Canet } 26043e9fab69SBenoît Canet if (has_iops_max) { 26053e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 26063e9fab69SBenoît Canet } 26073e9fab69SBenoît Canet if (has_iops_rd_max) { 26083e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 26093e9fab69SBenoît Canet } 26103e9fab69SBenoît Canet if (has_iops_wr_max) { 26113e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 26123e9fab69SBenoît Canet } 2613cc0681c4SBenoît Canet 26142024c1dfSBenoît Canet if (has_iops_size) { 26152024c1dfSBenoît Canet cfg.op_size = iops_size; 26162024c1dfSBenoît Canet } 2617cc0681c4SBenoît Canet 2618cc0681c4SBenoît Canet if (!check_throttle_config(&cfg, errp)) { 26195433c24fSMax Reitz goto out; 2620727f005eSZhi Yong Wu } 2621727f005eSZhi Yong Wu 262276f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 262376f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 262476f4afb4SAlberto Garcia * just update the throttling group. */ 2625a0d64a61SAlberto Garcia if (!bs->throttle_state) { 262676f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 262776f4afb4SAlberto Garcia } else if (has_group) { 262876f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2629727f005eSZhi Yong Wu } 263076f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2631cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 2632a0d64a61SAlberto Garcia } else if (bs->throttle_state) { 263376f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 263476f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2635727f005eSZhi Yong Wu } 2636b15446fdSStefan Hajnoczi 26375433c24fSMax Reitz out: 2638b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2639727f005eSZhi Yong Wu } 2640727f005eSZhi Yong Wu 2641341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2642341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2643341ebc2fSJohn Snow Error **errp) 2644341ebc2fSJohn Snow { 2645341ebc2fSJohn Snow AioContext *aio_context; 2646341ebc2fSJohn Snow BlockDriverState *bs; 2647341ebc2fSJohn Snow 2648341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2649341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2650341ebc2fSJohn Snow return; 2651341ebc2fSJohn Snow } 2652341ebc2fSJohn Snow 2653341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2654341ebc2fSJohn Snow if (!bs) { 2655341ebc2fSJohn Snow return; 2656341ebc2fSJohn Snow } 2657341ebc2fSJohn Snow 2658341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2659341ebc2fSJohn Snow aio_context_acquire(aio_context); 2660341ebc2fSJohn Snow 2661341ebc2fSJohn Snow if (has_granularity) { 2662341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2663341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2664341ebc2fSJohn Snow "and at least 512"); 2665341ebc2fSJohn Snow goto out; 2666341ebc2fSJohn Snow } 2667341ebc2fSJohn Snow } else { 2668341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2669341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2670341ebc2fSJohn Snow } 2671341ebc2fSJohn Snow 2672341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2673341ebc2fSJohn Snow 2674341ebc2fSJohn Snow out: 2675341ebc2fSJohn Snow aio_context_release(aio_context); 2676341ebc2fSJohn Snow } 2677341ebc2fSJohn Snow 2678341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2679341ebc2fSJohn Snow Error **errp) 2680341ebc2fSJohn Snow { 2681341ebc2fSJohn Snow AioContext *aio_context; 2682341ebc2fSJohn Snow BlockDriverState *bs; 2683341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2684341ebc2fSJohn Snow 2685341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2686341ebc2fSJohn Snow if (!bitmap || !bs) { 2687341ebc2fSJohn Snow return; 2688341ebc2fSJohn Snow } 2689341ebc2fSJohn Snow 26909bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 26919bd2b08fSJohn Snow error_setg(errp, 26929bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 26939bd2b08fSJohn Snow name); 26949bd2b08fSJohn Snow goto out; 26959bd2b08fSJohn Snow } 269620dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2697341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2698341ebc2fSJohn Snow 26999bd2b08fSJohn Snow out: 2700341ebc2fSJohn Snow aio_context_release(aio_context); 2701341ebc2fSJohn Snow } 2702341ebc2fSJohn Snow 2703e74e6b78SJohn Snow /** 2704e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2705e74e6b78SJohn Snow * immediately after a full backup operation. 2706e74e6b78SJohn Snow */ 2707e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2708e74e6b78SJohn Snow Error **errp) 2709e74e6b78SJohn Snow { 2710e74e6b78SJohn Snow AioContext *aio_context; 2711e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2712e74e6b78SJohn Snow BlockDriverState *bs; 2713e74e6b78SJohn Snow 2714e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2715e74e6b78SJohn Snow if (!bitmap || !bs) { 2716e74e6b78SJohn Snow return; 2717e74e6b78SJohn Snow } 2718e74e6b78SJohn Snow 2719e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2720e74e6b78SJohn Snow error_setg(errp, 2721e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2722e74e6b78SJohn Snow name); 2723e74e6b78SJohn Snow goto out; 2724e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2725e74e6b78SJohn Snow error_setg(errp, 2726e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2727e74e6b78SJohn Snow name); 2728e74e6b78SJohn Snow goto out; 2729e74e6b78SJohn Snow } 2730e74e6b78SJohn Snow 2731df9a681dSFam Zheng bdrv_clear_dirty_bitmap(bitmap, NULL); 2732e74e6b78SJohn Snow 2733e74e6b78SJohn Snow out: 2734e74e6b78SJohn Snow aio_context_release(aio_context); 2735e74e6b78SJohn Snow } 2736e74e6b78SJohn Snow 2737072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 27389063f814SRyan Harper { 27399063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 27407e7d56d9SMarkus Armbruster BlockBackend *blk; 27419063f814SRyan Harper BlockDriverState *bs; 27428ad4202bSStefan Hajnoczi AioContext *aio_context; 27433718d8abSFam Zheng Error *local_err = NULL; 27449063f814SRyan Harper 27457e7d56d9SMarkus Armbruster blk = blk_by_name(id); 27467e7d56d9SMarkus Armbruster if (!blk) { 2747b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2748072ebe6bSMarkus Armbruster return; 27499063f814SRyan Harper } 27508ad4202bSStefan Hajnoczi 275126f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 275248f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 275348f364ddSMarkus Armbruster " is not supported"); 2754072ebe6bSMarkus Armbruster return; 275548f364ddSMarkus Armbruster } 275648f364ddSMarkus Armbruster 27575433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 27588ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 27598ad4202bSStefan Hajnoczi 27605433c24fSMax Reitz bs = blk_bs(blk); 27615433c24fSMax Reitz if (bs) { 27623718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2763565f65d2SMarkus Armbruster error_report_err(local_err); 27648ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2765072ebe6bSMarkus Armbruster return; 27668591675fSMarcelo Tosatti } 27679063f814SRyan Harper 27689063f814SRyan Harper bdrv_close(bs); 27695433c24fSMax Reitz } 27709063f814SRyan Harper 2771fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 2772d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 2773d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 2774d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 2775d22b2f41SRyan Harper */ 2776a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 27773e5a50d6SMarkus Armbruster blk_hide_on_behalf_of_hmp_drive_del(blk); 2778293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2779373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2780293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2781d22b2f41SRyan Harper } else { 2782b9fe8a7aSMarkus Armbruster blk_unref(blk); 2783d22b2f41SRyan Harper } 27849063f814SRyan Harper 27858ad4202bSStefan Hajnoczi aio_context_release(aio_context); 27869063f814SRyan Harper } 27876d4a2b3aSChristoph Hellwig 27883b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 27893b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 27903b1dbd11SBenoît Canet int64_t size, Error **errp) 27916d4a2b3aSChristoph Hellwig { 27923b1dbd11SBenoît Canet Error *local_err = NULL; 27936d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2794927e0e76SStefan Hajnoczi AioContext *aio_context; 27958732901eSKevin Wolf int ret; 27966d4a2b3aSChristoph Hellwig 27973b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 27983b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 27993b1dbd11SBenoît Canet &local_err); 280084d18f06SMarkus Armbruster if (local_err) { 28013b1dbd11SBenoît Canet error_propagate(errp, local_err); 28023b1dbd11SBenoît Canet return; 28033b1dbd11SBenoît Canet } 28043b1dbd11SBenoît Canet 2805927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2806927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2807927e0e76SStefan Hajnoczi 28083b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2809c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2810927e0e76SStefan Hajnoczi goto out; 28116d4a2b3aSChristoph Hellwig } 28126d4a2b3aSChristoph Hellwig 28136d4a2b3aSChristoph Hellwig if (size < 0) { 2814c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2815927e0e76SStefan Hajnoczi goto out; 28166d4a2b3aSChristoph Hellwig } 28176d4a2b3aSChristoph Hellwig 28189c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2819c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2820927e0e76SStefan Hajnoczi goto out; 28219c75e168SJeff Cody } 28229c75e168SJeff Cody 282392b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 282492b7a08dSPeter Lieven bdrv_drain_all(); 282592b7a08dSPeter Lieven 28268732901eSKevin Wolf ret = bdrv_truncate(bs, size); 28278732901eSKevin Wolf switch (ret) { 2828939a1cc3SStefan Hajnoczi case 0: 2829939a1cc3SStefan Hajnoczi break; 2830939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2831c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2832939a1cc3SStefan Hajnoczi break; 2833939a1cc3SStefan Hajnoczi case -ENOTSUP: 2834c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2835939a1cc3SStefan Hajnoczi break; 2836939a1cc3SStefan Hajnoczi case -EACCES: 283781e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2838939a1cc3SStefan Hajnoczi break; 2839939a1cc3SStefan Hajnoczi case -EBUSY: 2840c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2841939a1cc3SStefan Hajnoczi break; 2842939a1cc3SStefan Hajnoczi default: 28438732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2844939a1cc3SStefan Hajnoczi break; 28456d4a2b3aSChristoph Hellwig } 2846927e0e76SStefan Hajnoczi 2847927e0e76SStefan Hajnoczi out: 2848927e0e76SStefan Hajnoczi aio_context_release(aio_context); 28496d4a2b3aSChristoph Hellwig } 285012bd451fSStefan Hajnoczi 28519abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 285212bd451fSStefan Hajnoczi { 2853723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2854723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2855723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2856723c5d93SStefan Hajnoczi */ 2857723c5d93SStefan Hajnoczi 285812bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2859bcada37bSWenchao Xia const char *msg = NULL; 286012bd451fSStefan Hajnoczi 28619abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 286212bd451fSStefan Hajnoczi 286312bd451fSStefan Hajnoczi assert(bs->job); 2864bcada37bSWenchao Xia 286512bd451fSStefan Hajnoczi if (ret < 0) { 2866bcada37bSWenchao Xia msg = strerror(-ret); 286712bd451fSStefan Hajnoczi } 286812bd451fSStefan Hajnoczi 2869370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2870bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2871370521a1SStefan Hajnoczi } else { 2872bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2873370521a1SStefan Hajnoczi } 287412bd451fSStefan Hajnoczi } 287512bd451fSStefan Hajnoczi 287613d8cc51SJeff Cody void qmp_block_stream(const char *device, 287713d8cc51SJeff Cody bool has_base, const char *base, 287813d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 287913d8cc51SJeff Cody bool has_speed, int64_t speed, 28801d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 28811d809098SPaolo Bonzini Error **errp) 288212bd451fSStefan Hajnoczi { 2883a0e8544cSFam Zheng BlockBackend *blk; 288412bd451fSStefan Hajnoczi BlockDriverState *bs; 2885c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2886f3e69bebSStefan Hajnoczi AioContext *aio_context; 2887fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 288813d8cc51SJeff Cody const char *base_name = NULL; 288912bd451fSStefan Hajnoczi 28901d809098SPaolo Bonzini if (!has_on_error) { 28911d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 28921d809098SPaolo Bonzini } 28931d809098SPaolo Bonzini 2894a0e8544cSFam Zheng blk = blk_by_name(device); 2895a0e8544cSFam Zheng if (!blk) { 289675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 289775158ebbSMarkus Armbruster "Device '%s' not found", device); 289812bd451fSStefan Hajnoczi return; 289912bd451fSStefan Hajnoczi } 290012bd451fSStefan Hajnoczi 29015433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2902f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2903f3e69bebSStefan Hajnoczi 29045433c24fSMax Reitz if (!blk_is_available(blk)) { 29055433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29065433c24fSMax Reitz goto out; 29075433c24fSMax Reitz } 29085433c24fSMax Reitz bs = blk_bs(blk); 29095433c24fSMax Reitz 2910628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2911f3e69bebSStefan Hajnoczi goto out; 2912628ff683SFam Zheng } 2913628ff683SFam Zheng 291413d8cc51SJeff Cody if (has_base) { 2915c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2916c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2917c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2918f3e69bebSStefan Hajnoczi goto out; 291912bd451fSStefan Hajnoczi } 2920f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 292113d8cc51SJeff Cody base_name = base; 2922c8c3080fSMarcelo Tosatti } 292312bd451fSStefan Hajnoczi 292413d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 292513d8cc51SJeff Cody * file, and specifying one is therefore an error */ 292613d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 292713d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 292813d8cc51SJeff Cody "entire chain"); 2929f3e69bebSStefan Hajnoczi goto out; 293013d8cc51SJeff Cody } 293113d8cc51SJeff Cody 293213d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 293313d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 293413d8cc51SJeff Cody 293513d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 29361d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 293784d18f06SMarkus Armbruster if (local_err) { 2938fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2939f3e69bebSStefan Hajnoczi goto out; 294012bd451fSStefan Hajnoczi } 294112bd451fSStefan Hajnoczi 294212bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 2943f3e69bebSStefan Hajnoczi 2944f3e69bebSStefan Hajnoczi out: 2945f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 294612bd451fSStefan Hajnoczi } 29472d47c6e9SStefan Hajnoczi 2948ed61fc10SJeff Cody void qmp_block_commit(const char *device, 29497676e2c5SJeff Cody bool has_base, const char *base, 29507676e2c5SJeff Cody bool has_top, const char *top, 295154e26900SJeff Cody bool has_backing_file, const char *backing_file, 2952ed61fc10SJeff Cody bool has_speed, int64_t speed, 2953ed61fc10SJeff Cody Error **errp) 2954ed61fc10SJeff Cody { 2955a0e8544cSFam Zheng BlockBackend *blk; 2956ed61fc10SJeff Cody BlockDriverState *bs; 2957ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 29589e85cd5cSStefan Hajnoczi AioContext *aio_context; 2959ed61fc10SJeff Cody Error *local_err = NULL; 2960ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 2961ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 2962ed61fc10SJeff Cody */ 296392aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2964ed61fc10SJeff Cody 296554504663SMax Reitz if (!has_speed) { 296654504663SMax Reitz speed = 0; 296754504663SMax Reitz } 296854504663SMax Reitz 29697676e2c5SJeff Cody /* Important Note: 29707676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 29717676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 29727676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 29737676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 2974a0e8544cSFam Zheng blk = blk_by_name(device); 2975a0e8544cSFam Zheng if (!blk) { 297675158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 297775158ebbSMarkus Armbruster "Device '%s' not found", device); 2978ed61fc10SJeff Cody return; 2979ed61fc10SJeff Cody } 2980ed61fc10SJeff Cody 29815433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29829e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 29839e85cd5cSStefan Hajnoczi 29845433c24fSMax Reitz if (!blk_is_available(blk)) { 29855433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 29865433c24fSMax Reitz goto out; 29875433c24fSMax Reitz } 29885433c24fSMax Reitz bs = blk_bs(blk); 29895433c24fSMax Reitz 2990bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 29919e85cd5cSStefan Hajnoczi goto out; 2992628ff683SFam Zheng } 2993628ff683SFam Zheng 2994ed61fc10SJeff Cody /* default top_bs is the active layer */ 2995ed61fc10SJeff Cody top_bs = bs; 2996ed61fc10SJeff Cody 29977676e2c5SJeff Cody if (has_top && top) { 2998ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 2999ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 3000ed61fc10SJeff Cody } 3001ed61fc10SJeff Cody } 3002ed61fc10SJeff Cody 3003ed61fc10SJeff Cody if (top_bs == NULL) { 3004ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 30059e85cd5cSStefan Hajnoczi goto out; 3006ed61fc10SJeff Cody } 3007ed61fc10SJeff Cody 30089e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 30099e85cd5cSStefan Hajnoczi 3010d5208c45SJeff Cody if (has_base && base) { 3011d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 3012d5208c45SJeff Cody } else { 3013d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 3014d5208c45SJeff Cody } 3015d5208c45SJeff Cody 3016d5208c45SJeff Cody if (base_bs == NULL) { 3017c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 30189e85cd5cSStefan Hajnoczi goto out; 3019d5208c45SJeff Cody } 3020d5208c45SJeff Cody 30219e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 30229e85cd5cSStefan Hajnoczi 3023bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3024bb00021dSFam Zheng goto out; 3025bb00021dSFam Zheng } 3026bb00021dSFam Zheng 30277676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 30287676e2c5SJeff Cody if (top_bs == base_bs) { 30297676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 30309e85cd5cSStefan Hajnoczi goto out; 30317676e2c5SJeff Cody } 30327676e2c5SJeff Cody 303320a63d2cSFam Zheng if (top_bs == bs) { 303454e26900SJeff Cody if (has_backing_file) { 303554e26900SJeff Cody error_setg(errp, "'backing-file' specified," 303654e26900SJeff Cody " but 'top' is the active layer"); 30379e85cd5cSStefan Hajnoczi goto out; 303854e26900SJeff Cody } 303920a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 304020a63d2cSFam Zheng bs, &local_err); 304120a63d2cSFam Zheng } else { 3042ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 304354e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 304420a63d2cSFam Zheng } 3045ed61fc10SJeff Cody if (local_err != NULL) { 3046ed61fc10SJeff Cody error_propagate(errp, local_err); 30479e85cd5cSStefan Hajnoczi goto out; 3048ed61fc10SJeff Cody } 30499e85cd5cSStefan Hajnoczi 30509e85cd5cSStefan Hajnoczi out: 30519e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 3052ed61fc10SJeff Cody } 3053ed61fc10SJeff Cody 305478f51fdeSJohn Snow static void do_drive_backup(const char *device, const char *target, 305599a9addfSStefan Hajnoczi bool has_format, const char *format, 3056b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 305799a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 305899a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 3059d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 306078f51fdeSJohn Snow bool has_on_source_error, 306178f51fdeSJohn Snow BlockdevOnError on_source_error, 306278f51fdeSJohn Snow bool has_on_target_error, 306378f51fdeSJohn Snow BlockdevOnError on_target_error, 306478f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 306599a9addfSStefan Hajnoczi { 3066a0e8544cSFam Zheng BlockBackend *blk; 306799a9addfSStefan Hajnoczi BlockDriverState *bs; 306899a9addfSStefan Hajnoczi BlockDriverState *target_bs; 3069fc5d3f84SIan Main BlockDriverState *source = NULL; 3070d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 3071761731b1SStefan Hajnoczi AioContext *aio_context; 3072e6641719SMax Reitz QDict *options = NULL; 307399a9addfSStefan Hajnoczi Error *local_err = NULL; 307499a9addfSStefan Hajnoczi int flags; 307599a9addfSStefan Hajnoczi int64_t size; 307699a9addfSStefan Hajnoczi int ret; 307799a9addfSStefan Hajnoczi 307899a9addfSStefan Hajnoczi if (!has_speed) { 307999a9addfSStefan Hajnoczi speed = 0; 308099a9addfSStefan Hajnoczi } 308199a9addfSStefan Hajnoczi if (!has_on_source_error) { 308299a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 308399a9addfSStefan Hajnoczi } 308499a9addfSStefan Hajnoczi if (!has_on_target_error) { 308599a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 308699a9addfSStefan Hajnoczi } 308799a9addfSStefan Hajnoczi if (!has_mode) { 308899a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 308999a9addfSStefan Hajnoczi } 309099a9addfSStefan Hajnoczi 3091a0e8544cSFam Zheng blk = blk_by_name(device); 3092a0e8544cSFam Zheng if (!blk) { 309375158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 309475158ebbSMarkus Armbruster "Device '%s' not found", device); 309599a9addfSStefan Hajnoczi return; 309699a9addfSStefan Hajnoczi } 309799a9addfSStefan Hajnoczi 30985433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3099761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 3100761731b1SStefan Hajnoczi 3101c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 3102c29c1dd3SFam Zheng * do an early check redundantly. */ 31035433c24fSMax Reitz if (!blk_is_available(blk)) { 3104c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3105761731b1SStefan Hajnoczi goto out; 310699a9addfSStefan Hajnoczi } 31075433c24fSMax Reitz bs = blk_bs(blk); 310899a9addfSStefan Hajnoczi 310999a9addfSStefan Hajnoczi if (!has_format) { 311099a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 311199a9addfSStefan Hajnoczi } 311299a9addfSStefan Hajnoczi 3113c29c1dd3SFam Zheng /* Early check to avoid creating target */ 31143718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3115761731b1SStefan Hajnoczi goto out; 311699a9addfSStefan Hajnoczi } 311799a9addfSStefan Hajnoczi 311899a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 311999a9addfSStefan Hajnoczi 3120fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 3121fc5d3f84SIan Main * on top of. */ 3122fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 3123760e0063SKevin Wolf source = backing_bs(bs); 3124fc5d3f84SIan Main if (!source) { 3125fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 3126fc5d3f84SIan Main } 3127fc5d3f84SIan Main } 3128fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 3129fc5d3f84SIan Main source = bs; 3130fc5d3f84SIan Main } 3131fc5d3f84SIan Main 313299a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 313399a9addfSStefan Hajnoczi if (size < 0) { 313499a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 3135761731b1SStefan Hajnoczi goto out; 313699a9addfSStefan Hajnoczi } 313799a9addfSStefan Hajnoczi 313899a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 3139e6641719SMax Reitz assert(format); 3140fc5d3f84SIan Main if (source) { 3141fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 3142fc5d3f84SIan Main source->drv->format_name, NULL, 3143fc5d3f84SIan Main size, flags, &local_err, false); 3144fc5d3f84SIan Main } else { 3145fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 3146fc5d3f84SIan Main size, flags, &local_err, false); 3147fc5d3f84SIan Main } 314899a9addfSStefan Hajnoczi } 314999a9addfSStefan Hajnoczi 315084d18f06SMarkus Armbruster if (local_err) { 315199a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3152761731b1SStefan Hajnoczi goto out; 315399a9addfSStefan Hajnoczi } 315499a9addfSStefan Hajnoczi 3155e6641719SMax Reitz if (format) { 3156e6641719SMax Reitz options = qdict_new(); 3157e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3158e6641719SMax Reitz } 3159e6641719SMax Reitz 3160f67503e5SMax Reitz target_bs = NULL; 31616ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 316299a9addfSStefan Hajnoczi if (ret < 0) { 316334b5d2c6SMax Reitz error_propagate(errp, local_err); 3164761731b1SStefan Hajnoczi goto out; 316599a9addfSStefan Hajnoczi } 316699a9addfSStefan Hajnoczi 3167761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 3168761731b1SStefan Hajnoczi 3169d58d8453SJohn Snow if (has_bitmap) { 3170d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3171d58d8453SJohn Snow if (!bmap) { 3172d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 31730702d3d8SMax Reitz bdrv_unref(target_bs); 3174d58d8453SJohn Snow goto out; 3175d58d8453SJohn Snow } 3176d58d8453SJohn Snow } 3177d58d8453SJohn Snow 3178d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 3179d58d8453SJohn Snow on_source_error, on_target_error, 318078f51fdeSJohn Snow block_job_cb, bs, txn, &local_err); 318199a9addfSStefan Hajnoczi if (local_err != NULL) { 31824f6fd349SFam Zheng bdrv_unref(target_bs); 318399a9addfSStefan Hajnoczi error_propagate(errp, local_err); 3184761731b1SStefan Hajnoczi goto out; 318599a9addfSStefan Hajnoczi } 3186761731b1SStefan Hajnoczi 3187761731b1SStefan Hajnoczi out: 3188761731b1SStefan Hajnoczi aio_context_release(aio_context); 318999a9addfSStefan Hajnoczi } 319099a9addfSStefan Hajnoczi 319178f51fdeSJohn Snow void qmp_drive_backup(const char *device, const char *target, 319278f51fdeSJohn Snow bool has_format, const char *format, 319378f51fdeSJohn Snow enum MirrorSyncMode sync, 319478f51fdeSJohn Snow bool has_mode, enum NewImageMode mode, 319578f51fdeSJohn Snow bool has_speed, int64_t speed, 319678f51fdeSJohn Snow bool has_bitmap, const char *bitmap, 319778f51fdeSJohn Snow bool has_on_source_error, BlockdevOnError on_source_error, 319878f51fdeSJohn Snow bool has_on_target_error, BlockdevOnError on_target_error, 319978f51fdeSJohn Snow Error **errp) 320078f51fdeSJohn Snow { 320178f51fdeSJohn Snow return do_drive_backup(device, target, has_format, format, sync, 320278f51fdeSJohn Snow has_mode, mode, has_speed, speed, 320378f51fdeSJohn Snow has_bitmap, bitmap, 320478f51fdeSJohn Snow has_on_source_error, on_source_error, 320578f51fdeSJohn Snow has_on_target_error, on_target_error, 320678f51fdeSJohn Snow NULL, errp); 320778f51fdeSJohn Snow } 320878f51fdeSJohn Snow 3209c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3210c13163fbSBenoît Canet { 3211d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 3212c13163fbSBenoît Canet } 3213c13163fbSBenoît Canet 321478f51fdeSJohn Snow void do_blockdev_backup(const char *device, const char *target, 3215c29c1dd3SFam Zheng enum MirrorSyncMode sync, 3216c29c1dd3SFam Zheng bool has_speed, int64_t speed, 3217c29c1dd3SFam Zheng bool has_on_source_error, 3218c29c1dd3SFam Zheng BlockdevOnError on_source_error, 3219c29c1dd3SFam Zheng bool has_on_target_error, 3220c29c1dd3SFam Zheng BlockdevOnError on_target_error, 322178f51fdeSJohn Snow BlockJobTxn *txn, Error **errp) 3222c29c1dd3SFam Zheng { 32235433c24fSMax Reitz BlockBackend *blk, *target_blk; 3224c29c1dd3SFam Zheng BlockDriverState *bs; 3225c29c1dd3SFam Zheng BlockDriverState *target_bs; 3226c29c1dd3SFam Zheng Error *local_err = NULL; 3227c29c1dd3SFam Zheng AioContext *aio_context; 3228c29c1dd3SFam Zheng 3229c29c1dd3SFam Zheng if (!has_speed) { 3230c29c1dd3SFam Zheng speed = 0; 3231c29c1dd3SFam Zheng } 3232c29c1dd3SFam Zheng if (!has_on_source_error) { 3233c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3234c29c1dd3SFam Zheng } 3235c29c1dd3SFam Zheng if (!has_on_target_error) { 3236c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3237c29c1dd3SFam Zheng } 3238c29c1dd3SFam Zheng 3239a0e8544cSFam Zheng blk = blk_by_name(device); 3240a0e8544cSFam Zheng if (!blk) { 32415b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 3242c29c1dd3SFam Zheng return; 3243c29c1dd3SFam Zheng } 3244c29c1dd3SFam Zheng 32455433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3246c29c1dd3SFam Zheng aio_context_acquire(aio_context); 3247c29c1dd3SFam Zheng 32485433c24fSMax Reitz if (!blk_is_available(blk)) { 32495433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 32505433c24fSMax Reitz goto out; 32515433c24fSMax Reitz } 32525433c24fSMax Reitz bs = blk_bs(blk); 32535433c24fSMax Reitz 32545433c24fSMax Reitz target_blk = blk_by_name(target); 32555433c24fSMax Reitz if (!target_blk) { 32565b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 3257c29c1dd3SFam Zheng goto out; 3258c29c1dd3SFam Zheng } 32595433c24fSMax Reitz 32605433c24fSMax Reitz if (!blk_is_available(target_blk)) { 32615433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 32625433c24fSMax Reitz goto out; 32635433c24fSMax Reitz } 32645433c24fSMax Reitz target_bs = blk_bs(target_blk); 3265c29c1dd3SFam Zheng 3266c29c1dd3SFam Zheng bdrv_ref(target_bs); 3267c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3268d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 326978f51fdeSJohn Snow on_target_error, block_job_cb, bs, txn, &local_err); 3270c29c1dd3SFam Zheng if (local_err != NULL) { 3271c29c1dd3SFam Zheng bdrv_unref(target_bs); 3272c29c1dd3SFam Zheng error_propagate(errp, local_err); 3273c29c1dd3SFam Zheng } 3274c29c1dd3SFam Zheng out: 3275c29c1dd3SFam Zheng aio_context_release(aio_context); 3276c29c1dd3SFam Zheng } 3277c29c1dd3SFam Zheng 327878f51fdeSJohn Snow void qmp_blockdev_backup(const char *device, const char *target, 327978f51fdeSJohn Snow enum MirrorSyncMode sync, 328078f51fdeSJohn Snow bool has_speed, int64_t speed, 328178f51fdeSJohn Snow bool has_on_source_error, 328278f51fdeSJohn Snow BlockdevOnError on_source_error, 328378f51fdeSJohn Snow bool has_on_target_error, 328478f51fdeSJohn Snow BlockdevOnError on_target_error, 328578f51fdeSJohn Snow Error **errp) 328678f51fdeSJohn Snow { 328778f51fdeSJohn Snow do_blockdev_backup(device, target, sync, has_speed, speed, 328878f51fdeSJohn Snow has_on_source_error, on_source_error, 328978f51fdeSJohn Snow has_on_target_error, on_target_error, 329078f51fdeSJohn Snow NULL, errp); 329178f51fdeSJohn Snow } 329278f51fdeSJohn Snow 32934193cdd7SFam Zheng /* Parameter check and block job starting for drive mirroring. 32944193cdd7SFam Zheng * Caller should hold @device and @target's aio context (must be the same). 32954193cdd7SFam Zheng **/ 32964193cdd7SFam Zheng static void blockdev_mirror_common(BlockDriverState *bs, 32974193cdd7SFam Zheng BlockDriverState *target, 329809158f00SBenoît Canet bool has_replaces, const char *replaces, 3299d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 3300b952b558SPaolo Bonzini bool has_speed, int64_t speed, 3301eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 330208e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 33034193cdd7SFam Zheng bool has_on_source_error, 33044193cdd7SFam Zheng BlockdevOnError on_source_error, 33054193cdd7SFam Zheng bool has_on_target_error, 33064193cdd7SFam Zheng BlockdevOnError on_target_error, 33070fc9f8eaSFam Zheng bool has_unmap, bool unmap, 3308b952b558SPaolo Bonzini Error **errp) 3309d9b902dbSPaolo Bonzini { 3310d9b902dbSPaolo Bonzini 3311d9b902dbSPaolo Bonzini if (!has_speed) { 3312d9b902dbSPaolo Bonzini speed = 0; 3313d9b902dbSPaolo Bonzini } 3314b952b558SPaolo Bonzini if (!has_on_source_error) { 3315b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3316b952b558SPaolo Bonzini } 3317b952b558SPaolo Bonzini if (!has_on_target_error) { 3318b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3319b952b558SPaolo Bonzini } 3320eee13dfeSPaolo Bonzini if (!has_granularity) { 3321eee13dfeSPaolo Bonzini granularity = 0; 3322eee13dfeSPaolo Bonzini } 332308e4ed6cSPaolo Bonzini if (!has_buf_size) { 332448ac0a4dSWen Congyang buf_size = 0; 332508e4ed6cSPaolo Bonzini } 33260fc9f8eaSFam Zheng if (!has_unmap) { 33270fc9f8eaSFam Zheng unmap = true; 33280fc9f8eaSFam Zheng } 332908e4ed6cSPaolo Bonzini 3330eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3331c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 33323cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 3333eee13dfeSPaolo Bonzini return; 3334eee13dfeSPaolo Bonzini } 3335eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 3336c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3337c6bd8c70SMarkus Armbruster "power of 2"); 3338eee13dfeSPaolo Bonzini return; 3339eee13dfeSPaolo Bonzini } 3340d9b902dbSPaolo Bonzini 33414193cdd7SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 33424193cdd7SFam Zheng return; 33434193cdd7SFam Zheng } 3344e40e5027SFam Zheng if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3345e40e5027SFam Zheng return; 3346e40e5027SFam Zheng } 3347df92562eSFam Zheng if (target->blk) { 3348df92562eSFam Zheng error_setg(errp, "Cannot mirror to an attached block device"); 3349df92562eSFam Zheng return; 3350df92562eSFam Zheng } 33514193cdd7SFam Zheng 33524193cdd7SFam Zheng if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 33534193cdd7SFam Zheng sync = MIRROR_SYNC_MODE_FULL; 33544193cdd7SFam Zheng } 33554193cdd7SFam Zheng 33564193cdd7SFam Zheng /* pass the node name to replace to mirror start since it's loose coupling 33574193cdd7SFam Zheng * and will allow to check whether the node still exist at mirror completion 33584193cdd7SFam Zheng */ 33594193cdd7SFam Zheng mirror_start(bs, target, 33604193cdd7SFam Zheng has_replaces ? replaces : NULL, 33614193cdd7SFam Zheng speed, granularity, buf_size, sync, 33624193cdd7SFam Zheng on_source_error, on_target_error, unmap, 33634193cdd7SFam Zheng block_job_cb, bs, errp); 33644193cdd7SFam Zheng } 33654193cdd7SFam Zheng 33664193cdd7SFam Zheng void qmp_drive_mirror(const char *device, const char *target, 33674193cdd7SFam Zheng bool has_format, const char *format, 33684193cdd7SFam Zheng bool has_node_name, const char *node_name, 33694193cdd7SFam Zheng bool has_replaces, const char *replaces, 33704193cdd7SFam Zheng enum MirrorSyncMode sync, 33714193cdd7SFam Zheng bool has_mode, enum NewImageMode mode, 33724193cdd7SFam Zheng bool has_speed, int64_t speed, 33734193cdd7SFam Zheng bool has_granularity, uint32_t granularity, 33744193cdd7SFam Zheng bool has_buf_size, int64_t buf_size, 33754193cdd7SFam Zheng bool has_on_source_error, BlockdevOnError on_source_error, 33764193cdd7SFam Zheng bool has_on_target_error, BlockdevOnError on_target_error, 33774193cdd7SFam Zheng bool has_unmap, bool unmap, 33784193cdd7SFam Zheng Error **errp) 33794193cdd7SFam Zheng { 33804193cdd7SFam Zheng BlockDriverState *bs; 33814193cdd7SFam Zheng BlockBackend *blk; 33824193cdd7SFam Zheng BlockDriverState *source, *target_bs; 33834193cdd7SFam Zheng AioContext *aio_context; 33844193cdd7SFam Zheng Error *local_err = NULL; 33854193cdd7SFam Zheng QDict *options = NULL; 33864193cdd7SFam Zheng int flags; 33874193cdd7SFam Zheng int64_t size; 33884193cdd7SFam Zheng int ret; 33894193cdd7SFam Zheng 3390a0e8544cSFam Zheng blk = blk_by_name(device); 3391a0e8544cSFam Zheng if (!blk) { 339275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 339375158ebbSMarkus Armbruster "Device '%s' not found", device); 3394d9b902dbSPaolo Bonzini return; 3395d9b902dbSPaolo Bonzini } 3396d9b902dbSPaolo Bonzini 33975433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 33985a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 33995a7e7a0bSStefan Hajnoczi 34005433c24fSMax Reitz if (!blk_is_available(blk)) { 3401c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 34025a7e7a0bSStefan Hajnoczi goto out; 3403d9b902dbSPaolo Bonzini } 34045433c24fSMax Reitz bs = blk_bs(blk); 34054193cdd7SFam Zheng if (!has_mode) { 34064193cdd7SFam Zheng mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 34074193cdd7SFam Zheng } 3408d9b902dbSPaolo Bonzini 3409d9b902dbSPaolo Bonzini if (!has_format) { 3410d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3411d9b902dbSPaolo Bonzini } 3412d9b902dbSPaolo Bonzini 3413d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3414760e0063SKevin Wolf source = backing_bs(bs); 3415d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3416d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3417d9b902dbSPaolo Bonzini } 3418117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3419117e0c82SMax Reitz source = bs; 3420117e0c82SMax Reitz } 3421d9b902dbSPaolo Bonzini 3422ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3423ac3c5d83SStefan Hajnoczi if (size < 0) { 3424ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 34255a7e7a0bSStefan Hajnoczi goto out; 3426ac3c5d83SStefan Hajnoczi } 3427ac3c5d83SStefan Hajnoczi 342809158f00SBenoît Canet if (has_replaces) { 342909158f00SBenoît Canet BlockDriverState *to_replace_bs; 34305a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 34315a7e7a0bSStefan Hajnoczi int64_t replace_size; 343209158f00SBenoît Canet 343309158f00SBenoît Canet if (!has_node_name) { 343409158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 343509158f00SBenoît Canet " named node of the graph"); 34365a7e7a0bSStefan Hajnoczi goto out; 343709158f00SBenoît Canet } 343809158f00SBenoît Canet 3439e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 344009158f00SBenoît Canet 344109158f00SBenoît Canet if (!to_replace_bs) { 344209158f00SBenoît Canet error_propagate(errp, local_err); 34435a7e7a0bSStefan Hajnoczi goto out; 344409158f00SBenoît Canet } 344509158f00SBenoît Canet 34465a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 34475a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 34485a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 34495a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 34505a7e7a0bSStefan Hajnoczi 34515a7e7a0bSStefan Hajnoczi if (size != replace_size) { 345209158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 345309158f00SBenoît Canet "different size"); 34545a7e7a0bSStefan Hajnoczi goto out; 345509158f00SBenoît Canet } 345609158f00SBenoît Canet } 345709158f00SBenoît Canet 345814526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 345914526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 346014526864SMax Reitz { 3461d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3462e6641719SMax Reitz assert(format); 3463cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3464f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3465d9b902dbSPaolo Bonzini } else { 3466d9b902dbSPaolo Bonzini switch (mode) { 3467d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3468d9b902dbSPaolo Bonzini break; 3469d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3470d9b902dbSPaolo Bonzini /* create new image with backing file */ 3471cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3472d9b902dbSPaolo Bonzini source->filename, 3473d9b902dbSPaolo Bonzini source->drv->format_name, 3474f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3475d9b902dbSPaolo Bonzini break; 3476d9b902dbSPaolo Bonzini default: 3477d9b902dbSPaolo Bonzini abort(); 3478d9b902dbSPaolo Bonzini } 3479d9b902dbSPaolo Bonzini } 3480d9b902dbSPaolo Bonzini 348184d18f06SMarkus Armbruster if (local_err) { 3482cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 34835a7e7a0bSStefan Hajnoczi goto out; 3484d9b902dbSPaolo Bonzini } 3485d9b902dbSPaolo Bonzini 34864c828dc6SBenoît Canet options = qdict_new(); 3487e6641719SMax Reitz if (has_node_name) { 34884c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 34894c828dc6SBenoît Canet } 3490e6641719SMax Reitz if (format) { 3491e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3492e6641719SMax Reitz } 34934c828dc6SBenoît Canet 3494b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3495b812f671SPaolo Bonzini * file. 3496b812f671SPaolo Bonzini */ 3497f67503e5SMax Reitz target_bs = NULL; 34984c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 34996ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3500d9b902dbSPaolo Bonzini if (ret < 0) { 350134b5d2c6SMax Reitz error_propagate(errp, local_err); 35025a7e7a0bSStefan Hajnoczi goto out; 3503d9b902dbSPaolo Bonzini } 3504d9b902dbSPaolo Bonzini 35055a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 35065a7e7a0bSStefan Hajnoczi 35074193cdd7SFam Zheng blockdev_mirror_common(bs, target_bs, 35084193cdd7SFam Zheng has_replaces, replaces, sync, 35094193cdd7SFam Zheng has_speed, speed, 35104193cdd7SFam Zheng has_granularity, granularity, 35114193cdd7SFam Zheng has_buf_size, buf_size, 35124193cdd7SFam Zheng has_on_source_error, on_source_error, 35134193cdd7SFam Zheng has_on_target_error, on_target_error, 35144193cdd7SFam Zheng has_unmap, unmap, 35154193cdd7SFam Zheng &local_err); 35164193cdd7SFam Zheng if (local_err) { 3517d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 35184193cdd7SFam Zheng bdrv_unref(target_bs); 3519d9b902dbSPaolo Bonzini } 35205a7e7a0bSStefan Hajnoczi out: 35215a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3522d9b902dbSPaolo Bonzini } 3523d9b902dbSPaolo Bonzini 3524df92562eSFam Zheng void qmp_blockdev_mirror(const char *device, const char *target, 3525df92562eSFam Zheng bool has_replaces, const char *replaces, 3526df92562eSFam Zheng MirrorSyncMode sync, 3527df92562eSFam Zheng bool has_speed, int64_t speed, 3528df92562eSFam Zheng bool has_granularity, uint32_t granularity, 3529df92562eSFam Zheng bool has_buf_size, int64_t buf_size, 3530df92562eSFam Zheng bool has_on_source_error, 3531df92562eSFam Zheng BlockdevOnError on_source_error, 3532df92562eSFam Zheng bool has_on_target_error, 3533df92562eSFam Zheng BlockdevOnError on_target_error, 3534df92562eSFam Zheng Error **errp) 3535df92562eSFam Zheng { 3536df92562eSFam Zheng BlockDriverState *bs; 3537df92562eSFam Zheng BlockBackend *blk; 3538df92562eSFam Zheng BlockDriverState *target_bs; 3539df92562eSFam Zheng AioContext *aio_context; 3540df92562eSFam Zheng Error *local_err = NULL; 3541df92562eSFam Zheng 3542df92562eSFam Zheng blk = blk_by_name(device); 3543df92562eSFam Zheng if (!blk) { 3544df92562eSFam Zheng error_setg(errp, "Device '%s' not found", device); 3545df92562eSFam Zheng return; 3546df92562eSFam Zheng } 3547df92562eSFam Zheng bs = blk_bs(blk); 3548df92562eSFam Zheng 3549df92562eSFam Zheng if (!bs) { 3550df92562eSFam Zheng error_setg(errp, "Device '%s' has no media", device); 3551df92562eSFam Zheng return; 3552df92562eSFam Zheng } 3553df92562eSFam Zheng 3554df92562eSFam Zheng target_bs = bdrv_lookup_bs(target, target, errp); 3555df92562eSFam Zheng if (!target_bs) { 3556df92562eSFam Zheng return; 3557df92562eSFam Zheng } 3558df92562eSFam Zheng 3559df92562eSFam Zheng aio_context = bdrv_get_aio_context(bs); 3560df92562eSFam Zheng aio_context_acquire(aio_context); 3561df92562eSFam Zheng 3562df92562eSFam Zheng bdrv_ref(target_bs); 3563df92562eSFam Zheng bdrv_set_aio_context(target_bs, aio_context); 3564df92562eSFam Zheng 3565df92562eSFam Zheng blockdev_mirror_common(bs, target_bs, 3566df92562eSFam Zheng has_replaces, replaces, sync, 3567df92562eSFam Zheng has_speed, speed, 3568df92562eSFam Zheng has_granularity, granularity, 3569df92562eSFam Zheng has_buf_size, buf_size, 3570df92562eSFam Zheng has_on_source_error, on_source_error, 3571df92562eSFam Zheng has_on_target_error, on_target_error, 3572df92562eSFam Zheng true, true, 3573df92562eSFam Zheng &local_err); 3574df92562eSFam Zheng if (local_err) { 3575df92562eSFam Zheng error_propagate(errp, local_err); 3576df92562eSFam Zheng bdrv_unref(target_bs); 3577df92562eSFam Zheng } 3578df92562eSFam Zheng 3579df92562eSFam Zheng aio_context_release(aio_context); 3580df92562eSFam Zheng } 3581df92562eSFam Zheng 35823d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 358324d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 358424d6bffeSMarkus Armbruster Error **errp) 35852d47c6e9SStefan Hajnoczi { 3586a0e8544cSFam Zheng BlockBackend *blk; 35872d47c6e9SStefan Hajnoczi BlockDriverState *bs; 35882d47c6e9SStefan Hajnoczi 35895433c24fSMax Reitz *aio_context = NULL; 35905433c24fSMax Reitz 3591a0e8544cSFam Zheng blk = blk_by_name(device); 3592a0e8544cSFam Zheng if (!blk) { 35933d948cdfSStefan Hajnoczi goto notfound; 35942d47c6e9SStefan Hajnoczi } 35953d948cdfSStefan Hajnoczi 35965433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 35973d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 35983d948cdfSStefan Hajnoczi 35995433c24fSMax Reitz if (!blk_is_available(blk)) { 36005433c24fSMax Reitz goto notfound; 36015433c24fSMax Reitz } 36025433c24fSMax Reitz bs = blk_bs(blk); 36035433c24fSMax Reitz 36043d948cdfSStefan Hajnoczi if (!bs->job) { 36053d948cdfSStefan Hajnoczi goto notfound; 36063d948cdfSStefan Hajnoczi } 36073d948cdfSStefan Hajnoczi 36082d47c6e9SStefan Hajnoczi return bs->job; 36093d948cdfSStefan Hajnoczi 36103d948cdfSStefan Hajnoczi notfound: 36112e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 36122e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 36135433c24fSMax Reitz if (*aio_context) { 36145433c24fSMax Reitz aio_context_release(*aio_context); 36153d948cdfSStefan Hajnoczi *aio_context = NULL; 36165433c24fSMax Reitz } 36173d948cdfSStefan Hajnoczi return NULL; 36182d47c6e9SStefan Hajnoczi } 36192d47c6e9SStefan Hajnoczi 3620882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 36212d47c6e9SStefan Hajnoczi { 36223d948cdfSStefan Hajnoczi AioContext *aio_context; 362324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36242d47c6e9SStefan Hajnoczi 36252d47c6e9SStefan Hajnoczi if (!job) { 36262d47c6e9SStefan Hajnoczi return; 36272d47c6e9SStefan Hajnoczi } 36282d47c6e9SStefan Hajnoczi 3629882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 36303d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36312d47c6e9SStefan Hajnoczi } 3632370521a1SStefan Hajnoczi 36336e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 36346e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 36356e37fb81SPaolo Bonzini { 36363d948cdfSStefan Hajnoczi AioContext *aio_context; 363724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36386e37fb81SPaolo Bonzini 36396e37fb81SPaolo Bonzini if (!job) { 36406e37fb81SPaolo Bonzini return; 36416e37fb81SPaolo Bonzini } 36423d948cdfSStefan Hajnoczi 36433d948cdfSStefan Hajnoczi if (!has_force) { 36443d948cdfSStefan Hajnoczi force = false; 36453d948cdfSStefan Hajnoczi } 36463d948cdfSStefan Hajnoczi 3647751ebd76SFam Zheng if (job->user_paused && !force) { 3648f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3649f231b88dSCole Robinson device); 36503d948cdfSStefan Hajnoczi goto out; 36516e37fb81SPaolo Bonzini } 36526e37fb81SPaolo Bonzini 36536e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 36546e37fb81SPaolo Bonzini block_job_cancel(job); 36553d948cdfSStefan Hajnoczi out: 36563d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36576e37fb81SPaolo Bonzini } 36586e37fb81SPaolo Bonzini 36596e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3660370521a1SStefan Hajnoczi { 36613d948cdfSStefan Hajnoczi AioContext *aio_context; 366224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3663370521a1SStefan Hajnoczi 3664751ebd76SFam Zheng if (!job || job->user_paused) { 3665370521a1SStefan Hajnoczi return; 3666370521a1SStefan Hajnoczi } 36676e37fb81SPaolo Bonzini 3668751ebd76SFam Zheng job->user_paused = true; 36696e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 36706e37fb81SPaolo Bonzini block_job_pause(job); 36713d948cdfSStefan Hajnoczi aio_context_release(aio_context); 36726e37fb81SPaolo Bonzini } 36736e37fb81SPaolo Bonzini 36746e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 36756e37fb81SPaolo Bonzini { 36763d948cdfSStefan Hajnoczi AioContext *aio_context; 367724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 36786e37fb81SPaolo Bonzini 3679751ebd76SFam Zheng if (!job || !job->user_paused) { 36808acc72a4SPaolo Bonzini return; 36818acc72a4SPaolo Bonzini } 3682370521a1SStefan Hajnoczi 3683751ebd76SFam Zheng job->user_paused = false; 36846e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 36856e37fb81SPaolo Bonzini block_job_resume(job); 36863d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3687370521a1SStefan Hajnoczi } 3688fb5458cdSStefan Hajnoczi 3689aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3690aeae883bSPaolo Bonzini { 36913d948cdfSStefan Hajnoczi AioContext *aio_context; 369224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3693aeae883bSPaolo Bonzini 3694aeae883bSPaolo Bonzini if (!job) { 3695aeae883bSPaolo Bonzini return; 3696aeae883bSPaolo Bonzini } 3697aeae883bSPaolo Bonzini 3698aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3699aeae883bSPaolo Bonzini block_job_complete(job, errp); 37003d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3701aeae883bSPaolo Bonzini } 3702aeae883bSPaolo Bonzini 3703fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3704fa40e656SJeff Cody const char *image_node_name, 3705fa40e656SJeff Cody const char *backing_file, 3706fa40e656SJeff Cody Error **errp) 3707fa40e656SJeff Cody { 3708a0e8544cSFam Zheng BlockBackend *blk; 3709fa40e656SJeff Cody BlockDriverState *bs = NULL; 3710729962f6SStefan Hajnoczi AioContext *aio_context; 3711fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3712fa40e656SJeff Cody Error *local_err = NULL; 3713fa40e656SJeff Cody bool ro; 3714fa40e656SJeff Cody int open_flags; 3715fa40e656SJeff Cody int ret; 3716fa40e656SJeff Cody 3717a0e8544cSFam Zheng blk = blk_by_name(device); 3718a0e8544cSFam Zheng if (!blk) { 371975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 372075158ebbSMarkus Armbruster "Device '%s' not found", device); 3721fa40e656SJeff Cody return; 3722fa40e656SJeff Cody } 3723fa40e656SJeff Cody 37245433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3725729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3726729962f6SStefan Hajnoczi 37275433c24fSMax Reitz if (!blk_is_available(blk)) { 37285433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 37295433c24fSMax Reitz goto out; 37305433c24fSMax Reitz } 37315433c24fSMax Reitz bs = blk_bs(blk); 37325433c24fSMax Reitz 3733fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3734fa40e656SJeff Cody if (local_err) { 3735fa40e656SJeff Cody error_propagate(errp, local_err); 3736729962f6SStefan Hajnoczi goto out; 3737fa40e656SJeff Cody } 3738fa40e656SJeff Cody 3739fa40e656SJeff Cody if (!image_bs) { 3740fa40e656SJeff Cody error_setg(errp, "image file not found"); 3741729962f6SStefan Hajnoczi goto out; 3742fa40e656SJeff Cody } 3743fa40e656SJeff Cody 3744fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3745fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3746fa40e656SJeff Cody "without a backing file"); 3747729962f6SStefan Hajnoczi goto out; 3748fa40e656SJeff Cody } 3749fa40e656SJeff Cody 3750fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3751fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3752fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3753729962f6SStefan Hajnoczi goto out; 3754fa40e656SJeff Cody } 3755fa40e656SJeff Cody 3756fa40e656SJeff Cody /* final sanity check */ 3757fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3758fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3759fa40e656SJeff Cody device); 3760729962f6SStefan Hajnoczi goto out; 3761fa40e656SJeff Cody } 3762fa40e656SJeff Cody 3763fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3764fa40e656SJeff Cody open_flags = image_bs->open_flags; 3765fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3766fa40e656SJeff Cody 3767fa40e656SJeff Cody if (ro) { 3768fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3769fa40e656SJeff Cody if (local_err) { 3770fa40e656SJeff Cody error_propagate(errp, local_err); 3771729962f6SStefan Hajnoczi goto out; 3772fa40e656SJeff Cody } 3773fa40e656SJeff Cody } 3774fa40e656SJeff Cody 3775fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3776fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3777fa40e656SJeff Cody 3778fa40e656SJeff Cody if (ret < 0) { 3779fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3780fa40e656SJeff Cody backing_file); 3781fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3782fa40e656SJeff Cody * appropriate */ 3783fa40e656SJeff Cody } 3784fa40e656SJeff Cody 3785fa40e656SJeff Cody if (ro) { 3786fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3787fa40e656SJeff Cody if (local_err) { 3788fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3789fa40e656SJeff Cody } 3790fa40e656SJeff Cody } 3791729962f6SStefan Hajnoczi 3792729962f6SStefan Hajnoczi out: 3793729962f6SStefan Hajnoczi aio_context_release(aio_context); 3794fa40e656SJeff Cody } 3795fa40e656SJeff Cody 3796d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3797d26c9a15SKevin Wolf { 3798d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3799be4b67bcSMax Reitz BlockDriverState *bs; 3800be4b67bcSMax Reitz BlockBackend *blk = NULL; 3801d26c9a15SKevin Wolf QObject *obj; 3802d26c9a15SKevin Wolf QDict *qdict; 3803d26c9a15SKevin Wolf Error *local_err = NULL; 3804d26c9a15SKevin Wolf 380560e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3806d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 380760e19e06SMarkus Armbruster * when called from drive_new(). 3808d26c9a15SKevin Wolf * 3809d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3810d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3811c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3812c6e0bd9bSKevin Wolf options->cache->has_direct && 3813c6e0bd9bSKevin Wolf options->cache->direct; 3814c6e0bd9bSKevin Wolf if (!direct) { 3815d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3816d26c9a15SKevin Wolf goto fail; 3817d26c9a15SKevin Wolf } 3818d26c9a15SKevin Wolf } 3819d26c9a15SKevin Wolf 3820d26c9a15SKevin Wolf visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3821d26c9a15SKevin Wolf &options, NULL, &local_err); 382284d18f06SMarkus Armbruster if (local_err) { 3823d26c9a15SKevin Wolf error_propagate(errp, local_err); 3824d26c9a15SKevin Wolf goto fail; 3825d26c9a15SKevin Wolf } 3826d26c9a15SKevin Wolf 3827d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3828d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3829d26c9a15SKevin Wolf 3830d26c9a15SKevin Wolf qdict_flatten(qdict); 3831d26c9a15SKevin Wolf 3832be4b67bcSMax Reitz if (options->has_id) { 383318e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 383484d18f06SMarkus Armbruster if (local_err) { 3835b681072dSKevin Wolf error_propagate(errp, local_err); 3836d26c9a15SKevin Wolf goto fail; 3837d26c9a15SKevin Wolf } 3838d26c9a15SKevin Wolf 3839be4b67bcSMax Reitz bs = blk_bs(blk); 3840be4b67bcSMax Reitz } else { 3841be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3842be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3843be4b67bcSMax Reitz "the root node"); 3844be4b67bcSMax Reitz goto fail; 3845be4b67bcSMax Reitz } 3846be4b67bcSMax Reitz 3847bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3848bd745e23SMax Reitz if (!bs) { 3849be4b67bcSMax Reitz goto fail; 3850be4b67bcSMax Reitz } 3851be4b67bcSMax Reitz } 3852be4b67bcSMax Reitz 3853be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3854be4b67bcSMax Reitz if (blk) { 385518e46a03SMarkus Armbruster blk_unref(blk); 3856be4b67bcSMax Reitz } else { 3857be4b67bcSMax Reitz bdrv_unref(bs); 3858be4b67bcSMax Reitz } 38598ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 38608ae8e904SKevin Wolf goto fail; 38618ae8e904SKevin Wolf } 38628ae8e904SKevin Wolf 3863d26c9a15SKevin Wolf fail: 3864d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3865d26c9a15SKevin Wolf } 3866d26c9a15SKevin Wolf 386781b936aeSAlberto Garcia void qmp_x_blockdev_del(bool has_id, const char *id, 386881b936aeSAlberto Garcia bool has_node_name, const char *node_name, Error **errp) 386981b936aeSAlberto Garcia { 387081b936aeSAlberto Garcia AioContext *aio_context; 387181b936aeSAlberto Garcia BlockBackend *blk; 387281b936aeSAlberto Garcia BlockDriverState *bs; 387381b936aeSAlberto Garcia 387481b936aeSAlberto Garcia if (has_id && has_node_name) { 387581b936aeSAlberto Garcia error_setg(errp, "Only one of id and node-name must be specified"); 387681b936aeSAlberto Garcia return; 387781b936aeSAlberto Garcia } else if (!has_id && !has_node_name) { 387881b936aeSAlberto Garcia error_setg(errp, "No block device specified"); 387981b936aeSAlberto Garcia return; 388081b936aeSAlberto Garcia } 388181b936aeSAlberto Garcia 388281b936aeSAlberto Garcia if (has_id) { 388381b936aeSAlberto Garcia blk = blk_by_name(id); 388481b936aeSAlberto Garcia if (!blk) { 388581b936aeSAlberto Garcia error_setg(errp, "Cannot find block backend %s", id); 388681b936aeSAlberto Garcia return; 388781b936aeSAlberto Garcia } 388881b936aeSAlberto Garcia if (blk_get_refcnt(blk) > 1) { 388981b936aeSAlberto Garcia error_setg(errp, "Block backend %s is in use", id); 389081b936aeSAlberto Garcia return; 389181b936aeSAlberto Garcia } 389281b936aeSAlberto Garcia bs = blk_bs(blk); 389381b936aeSAlberto Garcia aio_context = blk_get_aio_context(blk); 389481b936aeSAlberto Garcia } else { 389581b936aeSAlberto Garcia bs = bdrv_find_node(node_name); 389681b936aeSAlberto Garcia if (!bs) { 389781b936aeSAlberto Garcia error_setg(errp, "Cannot find node %s", node_name); 389881b936aeSAlberto Garcia return; 389981b936aeSAlberto Garcia } 390081b936aeSAlberto Garcia blk = bs->blk; 390181b936aeSAlberto Garcia if (blk) { 390281b936aeSAlberto Garcia error_setg(errp, "Node %s is in use by %s", 390381b936aeSAlberto Garcia node_name, blk_name(blk)); 390481b936aeSAlberto Garcia return; 390581b936aeSAlberto Garcia } 390681b936aeSAlberto Garcia aio_context = bdrv_get_aio_context(bs); 390781b936aeSAlberto Garcia } 390881b936aeSAlberto Garcia 390981b936aeSAlberto Garcia aio_context_acquire(aio_context); 391081b936aeSAlberto Garcia 391181b936aeSAlberto Garcia if (bs) { 391281b936aeSAlberto Garcia if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 391381b936aeSAlberto Garcia goto out; 391481b936aeSAlberto Garcia } 391581b936aeSAlberto Garcia 391681b936aeSAlberto Garcia if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { 391781b936aeSAlberto Garcia error_setg(errp, "Block device %s is in use", 391881b936aeSAlberto Garcia bdrv_get_device_or_node_name(bs)); 391981b936aeSAlberto Garcia goto out; 392081b936aeSAlberto Garcia } 392181b936aeSAlberto Garcia } 392281b936aeSAlberto Garcia 392381b936aeSAlberto Garcia if (blk) { 392481b936aeSAlberto Garcia blk_unref(blk); 392581b936aeSAlberto Garcia } else { 392681b936aeSAlberto Garcia bdrv_unref(bs); 392781b936aeSAlberto Garcia } 392881b936aeSAlberto Garcia 392981b936aeSAlberto Garcia out: 393081b936aeSAlberto Garcia aio_context_release(aio_context); 393181b936aeSAlberto Garcia } 393281b936aeSAlberto Garcia 3933fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3934fb5458cdSStefan Hajnoczi { 3935fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3936fea68bb6SMarkus Armbruster BlockDriverState *bs; 3937fea68bb6SMarkus Armbruster 3938fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 393969691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 394069691e72SStefan Hajnoczi 394169691e72SStefan Hajnoczi aio_context_acquire(aio_context); 394269691e72SStefan Hajnoczi 3943fea68bb6SMarkus Armbruster if (bs->job) { 3944fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3945fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 3946fea68bb6SMarkus Armbruster *p_next = elem; 3947fea68bb6SMarkus Armbruster p_next = &elem->next; 3948fea68bb6SMarkus Armbruster } 394969691e72SStefan Hajnoczi 395069691e72SStefan Hajnoczi aio_context_release(aio_context); 3951fea68bb6SMarkus Armbruster } 3952fea68bb6SMarkus Armbruster 3953fea68bb6SMarkus Armbruster return head; 3954fb5458cdSStefan Hajnoczi } 39554d454574SPaolo Bonzini 39560006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 39574d454574SPaolo Bonzini .name = "drive", 39580006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 39594d454574SPaolo Bonzini .desc = { 39604d454574SPaolo Bonzini { 39614d454574SPaolo Bonzini .name = "snapshot", 39624d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39634d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 39644d454574SPaolo Bonzini },{ 3965a9384affSPaolo Bonzini .name = "discard", 3966a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 3967a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 3968a9384affSPaolo Bonzini },{ 39694d454574SPaolo Bonzini .name = "aio", 39704d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39714d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 39724d454574SPaolo Bonzini },{ 39734d454574SPaolo Bonzini .name = "format", 39744d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39754d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 39764d454574SPaolo Bonzini },{ 39774d454574SPaolo Bonzini .name = "rerror", 39784d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39794d454574SPaolo Bonzini .help = "read error action", 39804d454574SPaolo Bonzini },{ 39814d454574SPaolo Bonzini .name = "werror", 39824d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 39834d454574SPaolo Bonzini .help = "write error action", 39844d454574SPaolo Bonzini },{ 39850f227a94SKevin Wolf .name = "read-only", 39864d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 39874d454574SPaolo Bonzini .help = "open drive file as read-only", 39884d454574SPaolo Bonzini },{ 398957975222SKevin Wolf .name = "throttling.iops-total", 39904d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39914d454574SPaolo Bonzini .help = "limit total I/O operations per second", 39924d454574SPaolo Bonzini },{ 399357975222SKevin Wolf .name = "throttling.iops-read", 39944d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39954d454574SPaolo Bonzini .help = "limit read operations per second", 39964d454574SPaolo Bonzini },{ 399757975222SKevin Wolf .name = "throttling.iops-write", 39984d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 39994d454574SPaolo Bonzini .help = "limit write operations per second", 40004d454574SPaolo Bonzini },{ 400157975222SKevin Wolf .name = "throttling.bps-total", 40024d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40034d454574SPaolo Bonzini .help = "limit total bytes per second", 40044d454574SPaolo Bonzini },{ 400557975222SKevin Wolf .name = "throttling.bps-read", 40064d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40074d454574SPaolo Bonzini .help = "limit read bytes per second", 40084d454574SPaolo Bonzini },{ 400957975222SKevin Wolf .name = "throttling.bps-write", 40104d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 40114d454574SPaolo Bonzini .help = "limit write bytes per second", 40124d454574SPaolo Bonzini },{ 40133e9fab69SBenoît Canet .name = "throttling.iops-total-max", 40143e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40153e9fab69SBenoît Canet .help = "I/O operations burst", 40163e9fab69SBenoît Canet },{ 40173e9fab69SBenoît Canet .name = "throttling.iops-read-max", 40183e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40193e9fab69SBenoît Canet .help = "I/O operations read burst", 40203e9fab69SBenoît Canet },{ 40213e9fab69SBenoît Canet .name = "throttling.iops-write-max", 40223e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40233e9fab69SBenoît Canet .help = "I/O operations write burst", 40243e9fab69SBenoît Canet },{ 40253e9fab69SBenoît Canet .name = "throttling.bps-total-max", 40263e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40273e9fab69SBenoît Canet .help = "total bytes burst", 40283e9fab69SBenoît Canet },{ 40293e9fab69SBenoît Canet .name = "throttling.bps-read-max", 40303e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40313e9fab69SBenoît Canet .help = "total bytes read burst", 40323e9fab69SBenoît Canet },{ 40333e9fab69SBenoît Canet .name = "throttling.bps-write-max", 40343e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 40353e9fab69SBenoît Canet .help = "total bytes write burst", 40363e9fab69SBenoît Canet },{ 40372024c1dfSBenoît Canet .name = "throttling.iops-size", 40382024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 40392024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 40402024c1dfSBenoît Canet },{ 404176f4afb4SAlberto Garcia .name = "throttling.group", 404276f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 404376f4afb4SAlberto Garcia .help = "name of the block throttling group", 404476f4afb4SAlberto Garcia },{ 40454d454574SPaolo Bonzini .name = "copy-on-read", 40464d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 40474d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 4048465bee1dSPeter Lieven },{ 4049465bee1dSPeter Lieven .name = "detect-zeroes", 4050465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 4051465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 4052362e9299SAlberto Garcia },{ 4053362e9299SAlberto Garcia .name = "stats-account-invalid", 4054362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4055362e9299SAlberto Garcia .help = "whether to account for invalid I/O operations " 4056362e9299SAlberto Garcia "in the statistics", 4057362e9299SAlberto Garcia },{ 4058362e9299SAlberto Garcia .name = "stats-account-failed", 4059362e9299SAlberto Garcia .type = QEMU_OPT_BOOL, 4060362e9299SAlberto Garcia .help = "whether to account for failed I/O operations " 4061362e9299SAlberto Garcia "in the statistics", 40624d454574SPaolo Bonzini }, 40634d454574SPaolo Bonzini { /* end of list */ } 40644d454574SPaolo Bonzini }, 40654d454574SPaolo Bonzini }; 40660006383eSKevin Wolf 4067bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 4068bd745e23SMax Reitz .name = "root-bds", 4069bd745e23SMax Reitz .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 4070bd745e23SMax Reitz .desc = { 4071bd745e23SMax Reitz { 4072bd745e23SMax Reitz .name = "discard", 4073bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4074bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 4075bd745e23SMax Reitz },{ 4076bd745e23SMax Reitz .name = "aio", 4077bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4078bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 4079bd745e23SMax Reitz },{ 4080bd745e23SMax Reitz .name = "read-only", 4081bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4082bd745e23SMax Reitz .help = "open drive file as read-only", 4083bd745e23SMax Reitz },{ 4084bd745e23SMax Reitz .name = "copy-on-read", 4085bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 4086bd745e23SMax Reitz .help = "copy read data from backing file into image file", 4087bd745e23SMax Reitz },{ 4088bd745e23SMax Reitz .name = "detect-zeroes", 4089bd745e23SMax Reitz .type = QEMU_OPT_STRING, 4090bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 4091bd745e23SMax Reitz }, 4092bd745e23SMax Reitz { /* end of list */ } 4093bd745e23SMax Reitz }, 4094bd745e23SMax Reitz }; 4095bd745e23SMax Reitz 40960006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 40970006383eSKevin Wolf .name = "drive", 40980006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 40990006383eSKevin Wolf .desc = { 4100492fdc6fSKevin Wolf /* 4101492fdc6fSKevin Wolf * no elements => accept any params 4102492fdc6fSKevin Wolf * validation will happen later 4103492fdc6fSKevin Wolf */ 41040006383eSKevin Wolf { /* end of list */ } 41050006383eSKevin Wolf }, 41060006383eSKevin Wolf }; 4107