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 286fa510ebfSFam Zheng static void bdrv_put_ref_bh(void *opaque) 287aa398a5cSStefan Hajnoczi { 288fa510ebfSFam Zheng BDRVPutRefBH *s = opaque; 289aa398a5cSStefan Hajnoczi 290fa510ebfSFam Zheng bdrv_unref(s->bs); 291aa398a5cSStefan Hajnoczi qemu_bh_delete(s->bh); 292aa398a5cSStefan Hajnoczi g_free(s); 293aa398a5cSStefan Hajnoczi } 294aa398a5cSStefan Hajnoczi 295aa398a5cSStefan Hajnoczi /* 296fa510ebfSFam Zheng * Release a BDS reference in a BH 297aa398a5cSStefan Hajnoczi * 298fa510ebfSFam Zheng * It is not safe to use bdrv_unref() from a callback function when the callers 299fa510ebfSFam Zheng * still need the BlockDriverState. In such cases we schedule a BH to release 300fa510ebfSFam Zheng * the reference. 301aa398a5cSStefan Hajnoczi */ 302fa510ebfSFam Zheng static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) 303aa398a5cSStefan Hajnoczi { 304fa510ebfSFam Zheng BDRVPutRefBH *s; 305aa398a5cSStefan Hajnoczi 306fa510ebfSFam Zheng s = g_new(BDRVPutRefBH, 1); 307fa510ebfSFam Zheng s->bh = qemu_bh_new(bdrv_put_ref_bh, s); 308fa510ebfSFam Zheng s->bs = bs; 309aa398a5cSStefan Hajnoczi qemu_bh_schedule(s->bh); 310aa398a5cSStefan Hajnoczi } 311aa398a5cSStefan Hajnoczi 312b681072dSKevin Wolf static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 313666daa68SMarkus Armbruster { 314666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 31592aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 316666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 31792aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 318666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 31992aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 320666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 32192aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 322666daa68SMarkus Armbruster } else { 323b681072dSKevin Wolf error_setg(errp, "'%s' invalid %s error action", 324666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 325666daa68SMarkus Armbruster return -1; 326666daa68SMarkus Armbruster } 327666daa68SMarkus Armbruster } 328666daa68SMarkus Armbruster 329cc0681c4SBenoît Canet static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) 3300563e191SZhi Yong Wu { 331cc0681c4SBenoît Canet if (throttle_conflicting(cfg)) { 332cc0681c4SBenoît Canet error_setg(errp, "bps/iops/max total values and read/write values" 333c546194fSStefan Hajnoczi " cannot be used at the same time"); 3340563e191SZhi Yong Wu return false; 3350563e191SZhi Yong Wu } 3360563e191SZhi Yong Wu 337cc0681c4SBenoît Canet if (!throttle_is_valid(cfg)) { 338cc0681c4SBenoît Canet error_setg(errp, "bps/iops/maxs values must be 0 or greater"); 3397d81c141SStefan Hajnoczi return false; 3407d81c141SStefan Hajnoczi } 3417d81c141SStefan Hajnoczi 342ee2bdc33SStefan Hajnoczi if (throttle_max_is_missing_limit(cfg)) { 343ee2bdc33SStefan Hajnoczi error_setg(errp, "bps_max/iops_max require corresponding" 344ee2bdc33SStefan Hajnoczi " bps/iops values"); 345ee2bdc33SStefan Hajnoczi return false; 346ee2bdc33SStefan Hajnoczi } 347ee2bdc33SStefan Hajnoczi 3480563e191SZhi Yong Wu return true; 3490563e191SZhi Yong Wu } 3500563e191SZhi Yong Wu 35133cb7dc8SKevin Wolf typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 35233cb7dc8SKevin Wolf 353fbf8175eSMax Reitz /* All parameters but @opts are optional and may be set to NULL. */ 354fbf8175eSMax Reitz static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 355fbf8175eSMax Reitz const char **throttling_group, ThrottleConfig *throttle_cfg, 356fbf8175eSMax Reitz BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 357fbf8175eSMax Reitz { 358fbf8175eSMax Reitz const char *discard; 359fbf8175eSMax Reitz Error *local_error = NULL; 360fbf8175eSMax Reitz const char *aio; 361fbf8175eSMax Reitz 362fbf8175eSMax Reitz if (bdrv_flags) { 363fbf8175eSMax Reitz if (!qemu_opt_get_bool(opts, "read-only", false)) { 364fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_RDWR; 365fbf8175eSMax Reitz } 366fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 367fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_COPY_ON_READ; 368fbf8175eSMax Reitz } 369fbf8175eSMax Reitz 370fbf8175eSMax Reitz if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 371fbf8175eSMax Reitz if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 372fbf8175eSMax Reitz error_setg(errp, "Invalid discard option"); 373fbf8175eSMax Reitz return; 374fbf8175eSMax Reitz } 375fbf8175eSMax Reitz } 376fbf8175eSMax Reitz 377fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) { 378fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_CACHE_WB; 379fbf8175eSMax Reitz } 380fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { 381fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NOCACHE; 382fbf8175eSMax Reitz } 383fbf8175eSMax Reitz if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { 384fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NO_FLUSH; 385fbf8175eSMax Reitz } 386fbf8175eSMax Reitz 387fbf8175eSMax Reitz if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 388fbf8175eSMax Reitz if (!strcmp(aio, "native")) { 389fbf8175eSMax Reitz *bdrv_flags |= BDRV_O_NATIVE_AIO; 390fbf8175eSMax Reitz } else if (!strcmp(aio, "threads")) { 391fbf8175eSMax Reitz /* this is the default */ 392fbf8175eSMax Reitz } else { 393fbf8175eSMax Reitz error_setg(errp, "invalid aio option"); 394fbf8175eSMax Reitz return; 395fbf8175eSMax Reitz } 396fbf8175eSMax Reitz } 397fbf8175eSMax Reitz } 398fbf8175eSMax Reitz 399fbf8175eSMax Reitz /* disk I/O throttling */ 400fbf8175eSMax Reitz if (throttling_group) { 401fbf8175eSMax Reitz *throttling_group = qemu_opt_get(opts, "throttling.group"); 402fbf8175eSMax Reitz } 403fbf8175eSMax Reitz 404fbf8175eSMax Reitz if (throttle_cfg) { 405fbf8175eSMax Reitz memset(throttle_cfg, 0, sizeof(*throttle_cfg)); 406fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 407fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total", 0); 408fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 409fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read", 0); 410fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 411fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write", 0); 412fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 413fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total", 0); 414fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 415fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read", 0); 416fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 417fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write", 0); 418fbf8175eSMax Reitz 419fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 420fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 421fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_READ].max = 422fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 423fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 424fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 425fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 426fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 427fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_READ].max = 428fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 429fbf8175eSMax Reitz throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 430fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 431fbf8175eSMax Reitz 432fbf8175eSMax Reitz throttle_cfg->op_size = 433fbf8175eSMax Reitz qemu_opt_get_number(opts, "throttling.iops-size", 0); 434fbf8175eSMax Reitz 435fbf8175eSMax Reitz if (!check_throttle_config(throttle_cfg, errp)) { 436fbf8175eSMax Reitz return; 437fbf8175eSMax Reitz } 438fbf8175eSMax Reitz } 439fbf8175eSMax Reitz 440fbf8175eSMax Reitz if (detect_zeroes) { 441fbf8175eSMax Reitz *detect_zeroes = 442fbf8175eSMax Reitz qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 443fbf8175eSMax Reitz qemu_opt_get(opts, "detect-zeroes"), 444fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, 445fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 446fbf8175eSMax Reitz &local_error); 447fbf8175eSMax Reitz if (local_error) { 448fbf8175eSMax Reitz error_propagate(errp, local_error); 449fbf8175eSMax Reitz return; 450fbf8175eSMax Reitz } 451fbf8175eSMax Reitz 452fbf8175eSMax Reitz if (bdrv_flags && 453fbf8175eSMax Reitz *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 454fbf8175eSMax Reitz !(*bdrv_flags & BDRV_O_UNMAP)) 455fbf8175eSMax Reitz { 456fbf8175eSMax Reitz error_setg(errp, "setting detect-zeroes to unmap is not allowed " 457fbf8175eSMax Reitz "without setting discard operation to unmap"); 458fbf8175eSMax Reitz return; 459fbf8175eSMax Reitz } 460fbf8175eSMax Reitz } 461fbf8175eSMax Reitz } 462fbf8175eSMax Reitz 463f298d071SKevin Wolf /* Takes the ownership of bs_opts */ 46418e46a03SMarkus Armbruster static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 465b681072dSKevin Wolf Error **errp) 466666daa68SMarkus Armbruster { 467666daa68SMarkus Armbruster const char *buf; 468666daa68SMarkus Armbruster int bdrv_flags = 0; 469666daa68SMarkus Armbruster int on_read_error, on_write_error; 47026f54e9aSMarkus Armbruster BlockBackend *blk; 471a0f1eab1SMarkus Armbruster BlockDriverState *bs; 472cc0681c4SBenoît Canet ThrottleConfig cfg; 473666daa68SMarkus Armbruster int snapshot = 0; 474c546194fSStefan Hajnoczi Error *error = NULL; 4750006383eSKevin Wolf QemuOpts *opts; 4760006383eSKevin Wolf const char *id; 47774fe54f2SKevin Wolf bool has_driver_specific_opts; 478fbf8175eSMax Reitz BlockdevDetectZeroesOptions detect_zeroes = 479fbf8175eSMax Reitz BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 480fbf8175eSMax Reitz const char *throttling_group = NULL; 481666daa68SMarkus Armbruster 482f298d071SKevin Wolf /* Check common options by copying from bs_opts to opts, all other options 483f298d071SKevin Wolf * stay in bs_opts for processing by bdrv_open(). */ 484f298d071SKevin Wolf id = qdict_get_try_str(bs_opts, "id"); 4850006383eSKevin Wolf opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 48684d18f06SMarkus Armbruster if (error) { 487b681072dSKevin Wolf error_propagate(errp, error); 4886376f952SMarkus Armbruster goto err_no_opts; 4890006383eSKevin Wolf } 4900006383eSKevin Wolf 4910006383eSKevin Wolf qemu_opts_absorb_qdict(opts, bs_opts, &error); 49284d18f06SMarkus Armbruster if (error) { 493b681072dSKevin Wolf error_propagate(errp, error); 494ec9c10d2SStefan Hajnoczi goto early_err; 4950006383eSKevin Wolf } 4960006383eSKevin Wolf 4970006383eSKevin Wolf if (id) { 4980006383eSKevin Wolf qdict_del(bs_opts, "id"); 4990006383eSKevin Wolf } 5000006383eSKevin Wolf 50174fe54f2SKevin Wolf has_driver_specific_opts = !!qdict_size(bs_opts); 50274fe54f2SKevin Wolf 503666daa68SMarkus Armbruster /* extract parameters */ 504666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 505666daa68SMarkus Armbruster 506fbf8175eSMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 507fbf8175eSMax Reitz &detect_zeroes, &error); 508fbf8175eSMax Reitz if (error) { 509fbf8175eSMax Reitz error_propagate(errp, error); 510ec9c10d2SStefan Hajnoczi goto early_err; 511a9384affSPaolo Bonzini } 512666daa68SMarkus Armbruster 513666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 514c8057f95SPeter Maydell if (is_help_option(buf)) { 515807105a7SMarkus Armbruster error_printf("Supported formats:"); 516666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 517807105a7SMarkus Armbruster error_printf("\n"); 518ec9c10d2SStefan Hajnoczi goto early_err; 519666daa68SMarkus Armbruster } 52074fe54f2SKevin Wolf 521e4342ce5SMax Reitz if (qdict_haskey(bs_opts, "driver")) { 522e4342ce5SMax Reitz error_setg(errp, "Cannot specify both 'driver' and 'format'"); 523ec9c10d2SStefan Hajnoczi goto early_err; 5246db5f5d6SMike Qiu } 525e4342ce5SMax Reitz qdict_put(bs_opts, "driver", qstring_from_str(buf)); 526666daa68SMarkus Armbruster } 527666daa68SMarkus Armbruster 52892aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 529666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 530b681072dSKevin Wolf on_write_error = parse_block_error_action(buf, 0, &error); 53184d18f06SMarkus Armbruster if (error) { 532b681072dSKevin Wolf error_propagate(errp, error); 533ec9c10d2SStefan Hajnoczi goto early_err; 534666daa68SMarkus Armbruster } 535666daa68SMarkus Armbruster } 536666daa68SMarkus Armbruster 53792aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 538666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 539b681072dSKevin Wolf on_read_error = parse_block_error_action(buf, 1, &error); 54084d18f06SMarkus Armbruster if (error) { 541b681072dSKevin Wolf error_propagate(errp, error); 542ec9c10d2SStefan Hajnoczi goto early_err; 543666daa68SMarkus Armbruster } 544666daa68SMarkus Armbruster } 545666daa68SMarkus Armbruster 546666daa68SMarkus Armbruster if (snapshot) { 547666daa68SMarkus Armbruster /* always use cache=unsafe with snapshot */ 548666daa68SMarkus Armbruster bdrv_flags &= ~BDRV_O_CACHE_MASK; 549666daa68SMarkus Armbruster bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); 550666daa68SMarkus Armbruster } 551666daa68SMarkus Armbruster 5525ec18f8cSMax Reitz /* init */ 5535ec18f8cSMax Reitz if ((!file || !*file) && !has_driver_specific_opts) { 5545ec18f8cSMax Reitz BlockBackendRootState *blk_rs; 5555ec18f8cSMax Reitz 5565ec18f8cSMax Reitz blk = blk_new(qemu_opts_id(opts), errp); 5575ec18f8cSMax Reitz if (!blk) { 5585ec18f8cSMax Reitz goto early_err; 5595ec18f8cSMax Reitz } 5605ec18f8cSMax Reitz 5615ec18f8cSMax Reitz blk_rs = blk_get_root_state(blk); 5625ec18f8cSMax Reitz blk_rs->open_flags = bdrv_flags; 563fbf8175eSMax Reitz blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 5645ec18f8cSMax Reitz blk_rs->detect_zeroes = detect_zeroes; 5655ec18f8cSMax Reitz 5665ec18f8cSMax Reitz if (throttle_enabled(&cfg)) { 5675ec18f8cSMax Reitz if (!throttling_group) { 5685ec18f8cSMax Reitz throttling_group = blk_name(blk); 5695ec18f8cSMax Reitz } 5705ec18f8cSMax Reitz blk_rs->throttle_group = g_strdup(throttling_group); 5715ec18f8cSMax Reitz blk_rs->throttle_state = throttle_group_incref(throttling_group); 5725ec18f8cSMax Reitz blk_rs->throttle_state->cfg = cfg; 5735ec18f8cSMax Reitz } 5745ec18f8cSMax Reitz 5755ec18f8cSMax Reitz QDECREF(bs_opts); 5765ec18f8cSMax Reitz } else { 5775ec18f8cSMax Reitz if (file && !*file) { 5785ec18f8cSMax Reitz file = NULL; 5795ec18f8cSMax Reitz } 5805ec18f8cSMax Reitz 581e4342ce5SMax Reitz blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, 582e4342ce5SMax Reitz errp); 583e4342ce5SMax Reitz if (!blk) { 584e4342ce5SMax Reitz goto err_no_bs_opts; 585e4342ce5SMax Reitz } 586e4342ce5SMax Reitz bs = blk_bs(blk); 5870006383eSKevin Wolf 588e4342ce5SMax Reitz bs->detect_zeroes = detect_zeroes; 589e4342ce5SMax Reitz 590e4342ce5SMax Reitz /* disk I/O throttling */ 591e4342ce5SMax Reitz if (throttle_enabled(&cfg)) { 59276f4afb4SAlberto Garcia if (!throttling_group) { 59376f4afb4SAlberto Garcia throttling_group = blk_name(blk); 59476f4afb4SAlberto Garcia } 59576f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, throttling_group); 596e4342ce5SMax Reitz bdrv_set_io_limits(bs, &cfg); 597666daa68SMarkus Armbruster } 598666daa68SMarkus Armbruster 599a0f1eab1SMarkus Armbruster if (bdrv_key_required(bs)) { 600666daa68SMarkus Armbruster autostart = 0; 601a0f1eab1SMarkus Armbruster } 6025ec18f8cSMax Reitz } 6035ec18f8cSMax Reitz 6045ec18f8cSMax Reitz blk_set_on_error(blk, on_read_error, on_write_error); 6050006383eSKevin Wolf 606e4342ce5SMax Reitz err_no_bs_opts: 6070006383eSKevin Wolf qemu_opts_del(opts); 60818e46a03SMarkus Armbruster return blk; 609a9ae2bffSMarkus Armbruster 610ec9c10d2SStefan Hajnoczi early_err: 611ec9c10d2SStefan Hajnoczi qemu_opts_del(opts); 6126376f952SMarkus Armbruster err_no_opts: 6136376f952SMarkus Armbruster QDECREF(bs_opts); 614a9ae2bffSMarkus Armbruster return NULL; 615666daa68SMarkus Armbruster } 616666daa68SMarkus Armbruster 617bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts; 618bd745e23SMax Reitz 619bd745e23SMax Reitz /* Takes the ownership of bs_opts */ 620bd745e23SMax Reitz static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 621bd745e23SMax Reitz { 622bd745e23SMax Reitz BlockDriverState *bs; 623bd745e23SMax Reitz QemuOpts *opts; 624bd745e23SMax Reitz Error *local_error = NULL; 625bd745e23SMax Reitz BlockdevDetectZeroesOptions detect_zeroes; 626bd745e23SMax Reitz int ret; 627bd745e23SMax Reitz int bdrv_flags = 0; 628bd745e23SMax Reitz 629bd745e23SMax Reitz opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 630bd745e23SMax Reitz if (!opts) { 631bd745e23SMax Reitz goto fail; 632bd745e23SMax Reitz } 633bd745e23SMax Reitz 634bd745e23SMax Reitz qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 635bd745e23SMax Reitz if (local_error) { 636bd745e23SMax Reitz error_propagate(errp, local_error); 637bd745e23SMax Reitz goto fail; 638bd745e23SMax Reitz } 639bd745e23SMax Reitz 640bd745e23SMax Reitz extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 641bd745e23SMax Reitz &detect_zeroes, &local_error); 642bd745e23SMax Reitz if (local_error) { 643bd745e23SMax Reitz error_propagate(errp, local_error); 644bd745e23SMax Reitz goto fail; 645bd745e23SMax Reitz } 646bd745e23SMax Reitz 647bd745e23SMax Reitz bs = NULL; 648bd745e23SMax Reitz ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 649bd745e23SMax Reitz if (ret < 0) { 650bd745e23SMax Reitz goto fail_no_bs_opts; 651bd745e23SMax Reitz } 652bd745e23SMax Reitz 653bd745e23SMax Reitz bs->detect_zeroes = detect_zeroes; 654bd745e23SMax Reitz 655bd745e23SMax Reitz fail_no_bs_opts: 656bd745e23SMax Reitz qemu_opts_del(opts); 657bd745e23SMax Reitz return bs; 658bd745e23SMax Reitz 659bd745e23SMax Reitz fail: 660bd745e23SMax Reitz qemu_opts_del(opts); 661bd745e23SMax Reitz QDECREF(bs_opts); 662bd745e23SMax Reitz return NULL; 663bd745e23SMax Reitz } 664bd745e23SMax Reitz 6655abbf0eeSKevin Wolf static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 6665abbf0eeSKevin Wolf Error **errp) 66757975222SKevin Wolf { 66857975222SKevin Wolf const char *value; 66957975222SKevin Wolf 67057975222SKevin Wolf value = qemu_opt_get(opts, from); 67157975222SKevin Wolf if (value) { 6725abbf0eeSKevin Wolf if (qemu_opt_find(opts, to)) { 6735abbf0eeSKevin Wolf error_setg(errp, "'%s' and its alias '%s' can't be used at the " 6745abbf0eeSKevin Wolf "same time", to, from); 6755abbf0eeSKevin Wolf return; 6765abbf0eeSKevin Wolf } 67720d6cd47SJun Li } 67820d6cd47SJun Li 67920d6cd47SJun Li /* rename all items in opts */ 68020d6cd47SJun Li while ((value = qemu_opt_get(opts, from))) { 681f43e47dbSMarkus Armbruster qemu_opt_set(opts, to, value, &error_abort); 68257975222SKevin Wolf qemu_opt_unset(opts, from); 68357975222SKevin Wolf } 68457975222SKevin Wolf } 68557975222SKevin Wolf 68633cb7dc8SKevin Wolf QemuOptsList qemu_legacy_drive_opts = { 68733cb7dc8SKevin Wolf .name = "drive", 68833cb7dc8SKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 68933cb7dc8SKevin Wolf .desc = { 69033cb7dc8SKevin Wolf { 69187a899c5SKevin Wolf .name = "bus", 69287a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 69387a899c5SKevin Wolf .help = "bus number", 69487a899c5SKevin Wolf },{ 69587a899c5SKevin Wolf .name = "unit", 69687a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 69787a899c5SKevin Wolf .help = "unit number (i.e. lun for scsi)", 69887a899c5SKevin Wolf },{ 69987a899c5SKevin Wolf .name = "index", 70087a899c5SKevin Wolf .type = QEMU_OPT_NUMBER, 70187a899c5SKevin Wolf .help = "index number", 70287a899c5SKevin Wolf },{ 70333cb7dc8SKevin Wolf .name = "media", 70433cb7dc8SKevin Wolf .type = QEMU_OPT_STRING, 70533cb7dc8SKevin Wolf .help = "media type (disk, cdrom)", 706593d464bSKevin Wolf },{ 707593d464bSKevin Wolf .name = "if", 708593d464bSKevin Wolf .type = QEMU_OPT_STRING, 709593d464bSKevin Wolf .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 710b41a7338SKevin Wolf },{ 711b41a7338SKevin Wolf .name = "cyls", 712b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 713b41a7338SKevin Wolf .help = "number of cylinders (ide disk geometry)", 714b41a7338SKevin Wolf },{ 715b41a7338SKevin Wolf .name = "heads", 716b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 717b41a7338SKevin Wolf .help = "number of heads (ide disk geometry)", 718b41a7338SKevin Wolf },{ 719b41a7338SKevin Wolf .name = "secs", 720b41a7338SKevin Wolf .type = QEMU_OPT_NUMBER, 721b41a7338SKevin Wolf .help = "number of sectors (ide disk geometry)", 722b41a7338SKevin Wolf },{ 723b41a7338SKevin Wolf .name = "trans", 724b41a7338SKevin Wolf .type = QEMU_OPT_STRING, 725b41a7338SKevin Wolf .help = "chs translation (auto, lba, none)", 72626929298SKevin Wolf },{ 72726929298SKevin Wolf .name = "boot", 72826929298SKevin Wolf .type = QEMU_OPT_BOOL, 72926929298SKevin Wolf .help = "(deprecated, ignored)", 730394c7d4dSKevin Wolf },{ 731394c7d4dSKevin Wolf .name = "addr", 732394c7d4dSKevin Wolf .type = QEMU_OPT_STRING, 733394c7d4dSKevin Wolf .help = "pci address (virtio only)", 734d095b465SMax Reitz },{ 735bcf83158SKevin Wolf .name = "serial", 736bcf83158SKevin Wolf .type = QEMU_OPT_STRING, 737bcf83158SKevin Wolf .help = "disk serial number", 738bcf83158SKevin Wolf },{ 739d095b465SMax Reitz .name = "file", 740d095b465SMax Reitz .type = QEMU_OPT_STRING, 741d095b465SMax Reitz .help = "file name", 74233cb7dc8SKevin Wolf }, 7430ebd24e0SKevin Wolf 7440ebd24e0SKevin Wolf /* Options that are passed on, but have special semantics with -drive */ 7450ebd24e0SKevin Wolf { 7460ebd24e0SKevin Wolf .name = "read-only", 7470ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7480ebd24e0SKevin Wolf .help = "open drive file as read-only", 7490ebd24e0SKevin Wolf },{ 750ee13ed1cSKevin Wolf .name = "rerror", 751ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 752ee13ed1cSKevin Wolf .help = "read error action", 753ee13ed1cSKevin Wolf },{ 754ee13ed1cSKevin Wolf .name = "werror", 755ee13ed1cSKevin Wolf .type = QEMU_OPT_STRING, 756ee13ed1cSKevin Wolf .help = "write error action", 757ee13ed1cSKevin Wolf },{ 7580ebd24e0SKevin Wolf .name = "copy-on-read", 7590ebd24e0SKevin Wolf .type = QEMU_OPT_BOOL, 7600ebd24e0SKevin Wolf .help = "copy read data from backing file into image file", 7610ebd24e0SKevin Wolf }, 7620ebd24e0SKevin Wolf 76333cb7dc8SKevin Wolf { /* end of list */ } 76433cb7dc8SKevin Wolf }, 76533cb7dc8SKevin Wolf }; 76633cb7dc8SKevin Wolf 76760e19e06SMarkus Armbruster DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 76857975222SKevin Wolf { 76929c4e2b5SKevin Wolf const char *value; 77018e46a03SMarkus Armbruster BlockBackend *blk; 77133cb7dc8SKevin Wolf DriveInfo *dinfo = NULL; 772f298d071SKevin Wolf QDict *bs_opts; 77333cb7dc8SKevin Wolf QemuOpts *legacy_opts; 77433cb7dc8SKevin Wolf DriveMediaType media = MEDIA_DISK; 775593d464bSKevin Wolf BlockInterfaceType type; 776b41a7338SKevin Wolf int cyls, heads, secs, translation; 77787a899c5SKevin Wolf int max_devs, bus_id, unit_id, index; 778394c7d4dSKevin Wolf const char *devaddr; 779ee13ed1cSKevin Wolf const char *werror, *rerror; 780a7fdbcf0SFam Zheng bool read_only = false; 781a7fdbcf0SFam Zheng bool copy_on_read; 782bcf83158SKevin Wolf const char *serial; 783d095b465SMax Reitz const char *filename; 78433cb7dc8SKevin Wolf Error *local_err = NULL; 785247147fbSKevin Wolf int i; 78629c4e2b5SKevin Wolf 78757975222SKevin Wolf /* Change legacy command line options into QMP ones */ 788247147fbSKevin Wolf static const struct { 789247147fbSKevin Wolf const char *from; 790247147fbSKevin Wolf const char *to; 791247147fbSKevin Wolf } opt_renames[] = { 792247147fbSKevin Wolf { "iops", "throttling.iops-total" }, 793247147fbSKevin Wolf { "iops_rd", "throttling.iops-read" }, 794247147fbSKevin Wolf { "iops_wr", "throttling.iops-write" }, 79557975222SKevin Wolf 796247147fbSKevin Wolf { "bps", "throttling.bps-total" }, 797247147fbSKevin Wolf { "bps_rd", "throttling.bps-read" }, 798247147fbSKevin Wolf { "bps_wr", "throttling.bps-write" }, 79957975222SKevin Wolf 800247147fbSKevin Wolf { "iops_max", "throttling.iops-total-max" }, 801247147fbSKevin Wolf { "iops_rd_max", "throttling.iops-read-max" }, 802247147fbSKevin Wolf { "iops_wr_max", "throttling.iops-write-max" }, 8033e9fab69SBenoît Canet 804247147fbSKevin Wolf { "bps_max", "throttling.bps-total-max" }, 805247147fbSKevin Wolf { "bps_rd_max", "throttling.bps-read-max" }, 806247147fbSKevin Wolf { "bps_wr_max", "throttling.bps-write-max" }, 8073e9fab69SBenoît Canet 808247147fbSKevin Wolf { "iops_size", "throttling.iops-size" }, 8092024c1dfSBenoît Canet 81076f4afb4SAlberto Garcia { "group", "throttling.group" }, 81176f4afb4SAlberto Garcia 812247147fbSKevin Wolf { "readonly", "read-only" }, 813247147fbSKevin Wolf }; 814247147fbSKevin Wolf 815247147fbSKevin Wolf for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 8165abbf0eeSKevin Wolf qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 8175abbf0eeSKevin Wolf &local_err); 8185abbf0eeSKevin Wolf if (local_err) { 819565f65d2SMarkus Armbruster error_report_err(local_err); 8205abbf0eeSKevin Wolf return NULL; 8215abbf0eeSKevin Wolf } 822247147fbSKevin Wolf } 8230f227a94SKevin Wolf 82429c4e2b5SKevin Wolf value = qemu_opt_get(all_opts, "cache"); 82529c4e2b5SKevin Wolf if (value) { 82629c4e2b5SKevin Wolf int flags = 0; 82729c4e2b5SKevin Wolf 82829c4e2b5SKevin Wolf if (bdrv_parse_cache_flags(value, &flags) != 0) { 82929c4e2b5SKevin Wolf error_report("invalid cache option"); 83029c4e2b5SKevin Wolf return NULL; 83129c4e2b5SKevin Wolf } 83229c4e2b5SKevin Wolf 83329c4e2b5SKevin Wolf /* Specific options take precedence */ 83454861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 83554861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 836cccb7967SMarkus Armbruster !!(flags & BDRV_O_CACHE_WB), &error_abort); 83729c4e2b5SKevin Wolf } 83854861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 83954861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 840cccb7967SMarkus Armbruster !!(flags & BDRV_O_NOCACHE), &error_abort); 84129c4e2b5SKevin Wolf } 84254861b92SKevin Wolf if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 84354861b92SKevin Wolf qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 844cccb7967SMarkus Armbruster !!(flags & BDRV_O_NO_FLUSH), &error_abort); 84529c4e2b5SKevin Wolf } 84629c4e2b5SKevin Wolf qemu_opt_unset(all_opts, "cache"); 84729c4e2b5SKevin Wolf } 84829c4e2b5SKevin Wolf 849f298d071SKevin Wolf /* Get a QDict for processing the options */ 850f298d071SKevin Wolf bs_opts = qdict_new(); 851f298d071SKevin Wolf qemu_opts_to_qdict(all_opts, bs_opts); 852f298d071SKevin Wolf 85387ea75d5SPeter Crosthwaite legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 85487ea75d5SPeter Crosthwaite &error_abort); 85533cb7dc8SKevin Wolf qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 85684d18f06SMarkus Armbruster if (local_err) { 857565f65d2SMarkus Armbruster error_report_err(local_err); 85833cb7dc8SKevin Wolf goto fail; 85933cb7dc8SKevin Wolf } 86033cb7dc8SKevin Wolf 86126929298SKevin Wolf /* Deprecated option boot=[on|off] */ 86226929298SKevin Wolf if (qemu_opt_get(legacy_opts, "boot") != NULL) { 86326929298SKevin Wolf fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 86426929298SKevin Wolf "ignored. Future versions will reject this parameter. Please " 86526929298SKevin Wolf "update your scripts.\n"); 86626929298SKevin Wolf } 86726929298SKevin Wolf 86833cb7dc8SKevin Wolf /* Media type */ 86933cb7dc8SKevin Wolf value = qemu_opt_get(legacy_opts, "media"); 87033cb7dc8SKevin Wolf if (value) { 87133cb7dc8SKevin Wolf if (!strcmp(value, "disk")) { 87233cb7dc8SKevin Wolf media = MEDIA_DISK; 87333cb7dc8SKevin Wolf } else if (!strcmp(value, "cdrom")) { 87433cb7dc8SKevin Wolf media = MEDIA_CDROM; 875a7fdbcf0SFam Zheng read_only = true; 87633cb7dc8SKevin Wolf } else { 87733cb7dc8SKevin Wolf error_report("'%s' invalid media", value); 87833cb7dc8SKevin Wolf goto fail; 87933cb7dc8SKevin Wolf } 88033cb7dc8SKevin Wolf } 88133cb7dc8SKevin Wolf 8820ebd24e0SKevin Wolf /* copy-on-read is disabled with a warning for read-only devices */ 883a7fdbcf0SFam Zheng read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 8840ebd24e0SKevin Wolf copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 8850ebd24e0SKevin Wolf 8860ebd24e0SKevin Wolf if (read_only && copy_on_read) { 8870ebd24e0SKevin Wolf error_report("warning: disabling copy-on-read on read-only drive"); 8880ebd24e0SKevin Wolf copy_on_read = false; 8890ebd24e0SKevin Wolf } 8900ebd24e0SKevin Wolf 8910ebd24e0SKevin Wolf qdict_put(bs_opts, "read-only", 8920ebd24e0SKevin Wolf qstring_from_str(read_only ? "on" : "off")); 8930ebd24e0SKevin Wolf qdict_put(bs_opts, "copy-on-read", 8940ebd24e0SKevin Wolf qstring_from_str(copy_on_read ? "on" :"off")); 8950ebd24e0SKevin Wolf 896593d464bSKevin Wolf /* Controller type */ 897593d464bSKevin Wolf value = qemu_opt_get(legacy_opts, "if"); 898593d464bSKevin Wolf if (value) { 899593d464bSKevin Wolf for (type = 0; 900593d464bSKevin Wolf type < IF_COUNT && strcmp(value, if_name[type]); 901593d464bSKevin Wolf type++) { 902593d464bSKevin Wolf } 903593d464bSKevin Wolf if (type == IF_COUNT) { 904593d464bSKevin Wolf error_report("unsupported bus type '%s'", value); 905593d464bSKevin Wolf goto fail; 906593d464bSKevin Wolf } 907593d464bSKevin Wolf } else { 908593d464bSKevin Wolf type = block_default_type; 909593d464bSKevin Wolf } 910593d464bSKevin Wolf 911b41a7338SKevin Wolf /* Geometry */ 912b41a7338SKevin Wolf cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 913b41a7338SKevin Wolf heads = qemu_opt_get_number(legacy_opts, "heads", 0); 914b41a7338SKevin Wolf secs = qemu_opt_get_number(legacy_opts, "secs", 0); 915b41a7338SKevin Wolf 916b41a7338SKevin Wolf if (cyls || heads || secs) { 917b41a7338SKevin Wolf if (cyls < 1) { 918b41a7338SKevin Wolf error_report("invalid physical cyls number"); 919b41a7338SKevin Wolf goto fail; 920b41a7338SKevin Wolf } 921b41a7338SKevin Wolf if (heads < 1) { 922b41a7338SKevin Wolf error_report("invalid physical heads number"); 923b41a7338SKevin Wolf goto fail; 924b41a7338SKevin Wolf } 925b41a7338SKevin Wolf if (secs < 1) { 926b41a7338SKevin Wolf error_report("invalid physical secs number"); 927b41a7338SKevin Wolf goto fail; 928b41a7338SKevin Wolf } 929b41a7338SKevin Wolf } 930b41a7338SKevin Wolf 931b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 932b41a7338SKevin Wolf value = qemu_opt_get(legacy_opts, "trans"); 933b41a7338SKevin Wolf if (value != NULL) { 934b41a7338SKevin Wolf if (!cyls) { 935b41a7338SKevin Wolf error_report("'%s' trans must be used with cyls, heads and secs", 936b41a7338SKevin Wolf value); 937b41a7338SKevin Wolf goto fail; 938b41a7338SKevin Wolf } 939b41a7338SKevin Wolf if (!strcmp(value, "none")) { 940b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_NONE; 941b41a7338SKevin Wolf } else if (!strcmp(value, "lba")) { 942b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_LBA; 943f31c41ffSPaolo Bonzini } else if (!strcmp(value, "large")) { 944f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_LARGE; 945f31c41ffSPaolo Bonzini } else if (!strcmp(value, "rechs")) { 946f31c41ffSPaolo Bonzini translation = BIOS_ATA_TRANSLATION_RECHS; 947b41a7338SKevin Wolf } else if (!strcmp(value, "auto")) { 948b41a7338SKevin Wolf translation = BIOS_ATA_TRANSLATION_AUTO; 949b41a7338SKevin Wolf } else { 950b41a7338SKevin Wolf error_report("'%s' invalid translation type", value); 951b41a7338SKevin Wolf goto fail; 952b41a7338SKevin Wolf } 953b41a7338SKevin Wolf } 954b41a7338SKevin Wolf 955b41a7338SKevin Wolf if (media == MEDIA_CDROM) { 956b41a7338SKevin Wolf if (cyls || secs || heads) { 957b41a7338SKevin Wolf error_report("CHS can't be set with media=cdrom"); 958b41a7338SKevin Wolf goto fail; 959b41a7338SKevin Wolf } 960b41a7338SKevin Wolf } 961b41a7338SKevin Wolf 96287a899c5SKevin Wolf /* Device address specified by bus/unit or index. 96387a899c5SKevin Wolf * If none was specified, try to find the first free one. */ 96487a899c5SKevin Wolf bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 96587a899c5SKevin Wolf unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 96687a899c5SKevin Wolf index = qemu_opt_get_number(legacy_opts, "index", -1); 96787a899c5SKevin Wolf 96887a899c5SKevin Wolf max_devs = if_max_devs[type]; 96987a899c5SKevin Wolf 97087a899c5SKevin Wolf if (index != -1) { 97187a899c5SKevin Wolf if (bus_id != 0 || unit_id != -1) { 97287a899c5SKevin Wolf error_report("index cannot be used with bus and unit"); 97387a899c5SKevin Wolf goto fail; 97487a899c5SKevin Wolf } 97587a899c5SKevin Wolf bus_id = drive_index_to_bus_id(type, index); 97687a899c5SKevin Wolf unit_id = drive_index_to_unit_id(type, index); 97787a899c5SKevin Wolf } 97887a899c5SKevin Wolf 97987a899c5SKevin Wolf if (unit_id == -1) { 98087a899c5SKevin Wolf unit_id = 0; 98187a899c5SKevin Wolf while (drive_get(type, bus_id, unit_id) != NULL) { 98287a899c5SKevin Wolf unit_id++; 98387a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 98487a899c5SKevin Wolf unit_id -= max_devs; 98587a899c5SKevin Wolf bus_id++; 98687a899c5SKevin Wolf } 98787a899c5SKevin Wolf } 98887a899c5SKevin Wolf } 98987a899c5SKevin Wolf 99087a899c5SKevin Wolf if (max_devs && unit_id >= max_devs) { 99187a899c5SKevin Wolf error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 99287a899c5SKevin Wolf goto fail; 99387a899c5SKevin Wolf } 99487a899c5SKevin Wolf 99587a899c5SKevin Wolf if (drive_get(type, bus_id, unit_id) != NULL) { 99687a899c5SKevin Wolf error_report("drive with bus=%d, unit=%d (index=%d) exists", 99787a899c5SKevin Wolf bus_id, unit_id, index); 99887a899c5SKevin Wolf goto fail; 99987a899c5SKevin Wolf } 100087a899c5SKevin Wolf 1001bcf83158SKevin Wolf /* Serial number */ 1002bcf83158SKevin Wolf serial = qemu_opt_get(legacy_opts, "serial"); 1003bcf83158SKevin Wolf 100487a899c5SKevin Wolf /* no id supplied -> create one */ 100587a899c5SKevin Wolf if (qemu_opts_id(all_opts) == NULL) { 100687a899c5SKevin Wolf char *new_id; 100787a899c5SKevin Wolf const char *mediastr = ""; 100887a899c5SKevin Wolf if (type == IF_IDE || type == IF_SCSI) { 100987a899c5SKevin Wolf mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 101087a899c5SKevin Wolf } 101187a899c5SKevin Wolf if (max_devs) { 101287a899c5SKevin Wolf new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 101387a899c5SKevin Wolf mediastr, unit_id); 101487a899c5SKevin Wolf } else { 101587a899c5SKevin Wolf new_id = g_strdup_printf("%s%s%i", if_name[type], 101687a899c5SKevin Wolf mediastr, unit_id); 101787a899c5SKevin Wolf } 101887a899c5SKevin Wolf qdict_put(bs_opts, "id", qstring_from_str(new_id)); 101987a899c5SKevin Wolf g_free(new_id); 102087a899c5SKevin Wolf } 102187a899c5SKevin Wolf 1022394c7d4dSKevin Wolf /* Add virtio block device */ 1023394c7d4dSKevin Wolf devaddr = qemu_opt_get(legacy_opts, "addr"); 1024394c7d4dSKevin Wolf if (devaddr && type != IF_VIRTIO) { 1025394c7d4dSKevin Wolf error_report("addr is not supported by this bus type"); 1026394c7d4dSKevin Wolf goto fail; 1027394c7d4dSKevin Wolf } 1028394c7d4dSKevin Wolf 1029394c7d4dSKevin Wolf if (type == IF_VIRTIO) { 1030394c7d4dSKevin Wolf QemuOpts *devopts; 103187ea75d5SPeter Crosthwaite devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 103287ea75d5SPeter Crosthwaite &error_abort); 1033394c7d4dSKevin Wolf if (arch_type == QEMU_ARCH_S390X) { 10341f68f1d3SAlexander Graf qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1035394c7d4dSKevin Wolf } else { 1036f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1037394c7d4dSKevin Wolf } 1038f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1039f43e47dbSMarkus Armbruster &error_abort); 1040394c7d4dSKevin Wolf if (devaddr) { 1041f43e47dbSMarkus Armbruster qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1042394c7d4dSKevin Wolf } 1043394c7d4dSKevin Wolf } 1044394c7d4dSKevin Wolf 1045d095b465SMax Reitz filename = qemu_opt_get(legacy_opts, "file"); 1046d095b465SMax Reitz 1047ee13ed1cSKevin Wolf /* Check werror/rerror compatibility with if=... */ 1048ee13ed1cSKevin Wolf werror = qemu_opt_get(legacy_opts, "werror"); 1049ee13ed1cSKevin Wolf if (werror != NULL) { 1050ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1051ee13ed1cSKevin Wolf type != IF_NONE) { 1052ee13ed1cSKevin Wolf error_report("werror is not supported by this bus type"); 1053ee13ed1cSKevin Wolf goto fail; 1054ee13ed1cSKevin Wolf } 1055ee13ed1cSKevin Wolf qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1056ee13ed1cSKevin Wolf } 1057ee13ed1cSKevin Wolf 1058ee13ed1cSKevin Wolf rerror = qemu_opt_get(legacy_opts, "rerror"); 1059ee13ed1cSKevin Wolf if (rerror != NULL) { 1060ee13ed1cSKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1061ee13ed1cSKevin Wolf type != IF_NONE) { 1062ee13ed1cSKevin Wolf error_report("rerror is not supported by this bus type"); 1063ee13ed1cSKevin Wolf goto fail; 1064ee13ed1cSKevin Wolf } 1065ee13ed1cSKevin Wolf qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1066ee13ed1cSKevin Wolf } 1067ee13ed1cSKevin Wolf 10682d246f01SKevin Wolf /* Actual block device init: Functionality shared with blockdev-add */ 106918e46a03SMarkus Armbruster blk = blockdev_init(filename, bs_opts, &local_err); 10703cb0e25cSMarkus Armbruster bs_opts = NULL; 107118e46a03SMarkus Armbruster if (!blk) { 107284d18f06SMarkus Armbruster if (local_err) { 1073565f65d2SMarkus Armbruster error_report_err(local_err); 1074b681072dSKevin Wolf } 10752d246f01SKevin Wolf goto fail; 1076b681072dSKevin Wolf } else { 107784d18f06SMarkus Armbruster assert(!local_err); 10782d246f01SKevin Wolf } 10792d246f01SKevin Wolf 108026f8b3a8SMarkus Armbruster /* Create legacy DriveInfo */ 108126f8b3a8SMarkus Armbruster dinfo = g_malloc0(sizeof(*dinfo)); 1082f298d071SKevin Wolf dinfo->opts = all_opts; 10832d246f01SKevin Wolf 1084b41a7338SKevin Wolf dinfo->cyls = cyls; 1085b41a7338SKevin Wolf dinfo->heads = heads; 1086b41a7338SKevin Wolf dinfo->secs = secs; 1087b41a7338SKevin Wolf dinfo->trans = translation; 1088b41a7338SKevin Wolf 1089ee13ed1cSKevin Wolf dinfo->type = type; 109087a899c5SKevin Wolf dinfo->bus = bus_id; 109187a899c5SKevin Wolf dinfo->unit = unit_id; 1092394c7d4dSKevin Wolf dinfo->devaddr = devaddr; 1093bcf83158SKevin Wolf dinfo->serial = g_strdup(serial); 1094bcf83158SKevin Wolf 109526f8b3a8SMarkus Armbruster blk_set_legacy_dinfo(blk, dinfo); 109626f8b3a8SMarkus Armbruster 1097e34ef046SKevin Wolf switch(type) { 1098e34ef046SKevin Wolf case IF_IDE: 1099e34ef046SKevin Wolf case IF_SCSI: 1100e34ef046SKevin Wolf case IF_XEN: 1101e34ef046SKevin Wolf case IF_NONE: 1102e34ef046SKevin Wolf dinfo->media_cd = media == MEDIA_CDROM; 1103e34ef046SKevin Wolf break; 1104e34ef046SKevin Wolf default: 1105e34ef046SKevin Wolf break; 1106e34ef046SKevin Wolf } 1107e34ef046SKevin Wolf 11082d246f01SKevin Wolf fail: 110933cb7dc8SKevin Wolf qemu_opts_del(legacy_opts); 11103cb0e25cSMarkus Armbruster QDECREF(bs_opts); 11112d246f01SKevin Wolf return dinfo; 111257975222SKevin Wolf } 111357975222SKevin Wolf 11143e5a50d6SMarkus Armbruster void hmp_commit(Monitor *mon, const QDict *qdict) 1115666daa68SMarkus Armbruster { 1116666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 1117a0e8544cSFam Zheng BlockBackend *blk; 11182d3735d3SStefan Hajnoczi int ret; 11192d3735d3SStefan Hajnoczi 1120e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 1121e8877497SStefan Hajnoczi ret = bdrv_commit_all(); 1122e8877497SStefan Hajnoczi } else { 112384aa0140SStefan Hajnoczi BlockDriverState *bs; 112484aa0140SStefan Hajnoczi AioContext *aio_context; 112584aa0140SStefan Hajnoczi 1126a0e8544cSFam Zheng blk = blk_by_name(device); 1127a0e8544cSFam Zheng if (!blk) { 112858513bdeSJeff Cody monitor_printf(mon, "Device '%s' not found\n", device); 1129ac59eb95SMarkus Armbruster return; 11306ab4b5abSMarkus Armbruster } 11315433c24fSMax Reitz if (!blk_is_available(blk)) { 11325433c24fSMax Reitz monitor_printf(mon, "Device '%s' has no medium\n", device); 11335433c24fSMax Reitz return; 11345433c24fSMax Reitz } 113584aa0140SStefan Hajnoczi 113684aa0140SStefan Hajnoczi bs = blk_bs(blk); 113784aa0140SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 113884aa0140SStefan Hajnoczi aio_context_acquire(aio_context); 113984aa0140SStefan Hajnoczi 114084aa0140SStefan Hajnoczi ret = bdrv_commit(bs); 114184aa0140SStefan Hajnoczi 114284aa0140SStefan Hajnoczi aio_context_release(aio_context); 11432d3735d3SStefan Hajnoczi } 114458513bdeSJeff Cody if (ret < 0) { 114558513bdeSJeff Cody monitor_printf(mon, "'commit' error for '%s': %s\n", device, 114658513bdeSJeff Cody strerror(-ret)); 1147666daa68SMarkus Armbruster } 1148666daa68SMarkus Armbruster } 1149666daa68SMarkus Armbruster 11506a8f9661SEric Blake static void blockdev_do_action(TransactionActionKind type, void *data, 11516a8f9661SEric Blake Error **errp) 11526cc2a415SPaolo Bonzini { 1153c8a83e85SKevin Wolf TransactionAction action; 1154c8a83e85SKevin Wolf TransactionActionList list; 11556cc2a415SPaolo Bonzini 11566a8f9661SEric Blake action.type = type; 11576a8f9661SEric Blake action.u.data = data; 11586cc2a415SPaolo Bonzini list.value = &action; 11596cc2a415SPaolo Bonzini list.next = NULL; 11606cc2a415SPaolo Bonzini qmp_transaction(&list, errp); 11616cc2a415SPaolo Bonzini } 11626cc2a415SPaolo Bonzini 11630901f67eSBenoît Canet void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 11640901f67eSBenoît Canet bool has_node_name, const char *node_name, 11650901f67eSBenoît Canet const char *snapshot_file, 11660901f67eSBenoît Canet bool has_snapshot_node_name, 11670901f67eSBenoît Canet const char *snapshot_node_name, 11686106e249SLuiz Capitulino bool has_format, const char *format, 11690901f67eSBenoît Canet bool has_mode, NewImageMode mode, Error **errp) 1170f8882568SJes Sorensen { 11716cc2a415SPaolo Bonzini BlockdevSnapshot snapshot = { 11720901f67eSBenoît Canet .has_device = has_device, 11736cc2a415SPaolo Bonzini .device = (char *) device, 11740901f67eSBenoît Canet .has_node_name = has_node_name, 11750901f67eSBenoît Canet .node_name = (char *) node_name, 11766cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 11770901f67eSBenoît Canet .has_snapshot_node_name = has_snapshot_node_name, 11780901f67eSBenoît Canet .snapshot_node_name = (char *) snapshot_node_name, 11796cc2a415SPaolo Bonzini .has_format = has_format, 11806cc2a415SPaolo Bonzini .format = (char *) format, 11816cc2a415SPaolo Bonzini .has_mode = has_mode, 11826cc2a415SPaolo Bonzini .mode = mode, 11836cc2a415SPaolo Bonzini }; 1184c8a83e85SKevin Wolf blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 1185c8a83e85SKevin Wolf &snapshot, errp); 1186f8882568SJes Sorensen } 1187f8882568SJes Sorensen 1188f323bc9eSWenchao Xia void qmp_blockdev_snapshot_internal_sync(const char *device, 1189f323bc9eSWenchao Xia const char *name, 1190f323bc9eSWenchao Xia Error **errp) 1191f323bc9eSWenchao Xia { 1192f323bc9eSWenchao Xia BlockdevSnapshotInternal snapshot = { 1193f323bc9eSWenchao Xia .device = (char *) device, 1194f323bc9eSWenchao Xia .name = (char *) name 1195f323bc9eSWenchao Xia }; 1196f323bc9eSWenchao Xia 1197f323bc9eSWenchao Xia blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 1198f323bc9eSWenchao Xia &snapshot, errp); 1199f323bc9eSWenchao Xia } 1200f323bc9eSWenchao Xia 120144e3e053SWenchao Xia SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 120244e3e053SWenchao Xia bool has_id, 120344e3e053SWenchao Xia const char *id, 120444e3e053SWenchao Xia bool has_name, 120544e3e053SWenchao Xia const char *name, 120644e3e053SWenchao Xia Error **errp) 120744e3e053SWenchao Xia { 1208a0e8544cSFam Zheng BlockDriverState *bs; 1209a0e8544cSFam Zheng BlockBackend *blk; 12104ef3982aSStefan Hajnoczi AioContext *aio_context; 121144e3e053SWenchao Xia QEMUSnapshotInfo sn; 121244e3e053SWenchao Xia Error *local_err = NULL; 121344e3e053SWenchao Xia SnapshotInfo *info = NULL; 121444e3e053SWenchao Xia int ret; 121544e3e053SWenchao Xia 1216a0e8544cSFam Zheng blk = blk_by_name(device); 1217a0e8544cSFam Zheng if (!blk) { 121875158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 121975158ebbSMarkus Armbruster "Device '%s' not found", device); 122044e3e053SWenchao Xia return NULL; 122144e3e053SWenchao Xia } 12225433c24fSMax Reitz 12235433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 12245433c24fSMax Reitz aio_context_acquire(aio_context); 122544e3e053SWenchao Xia 122644e3e053SWenchao Xia if (!has_id) { 122744e3e053SWenchao Xia id = NULL; 122844e3e053SWenchao Xia } 122944e3e053SWenchao Xia 123044e3e053SWenchao Xia if (!has_name) { 123144e3e053SWenchao Xia name = NULL; 123244e3e053SWenchao Xia } 123344e3e053SWenchao Xia 123444e3e053SWenchao Xia if (!id && !name) { 123544e3e053SWenchao Xia error_setg(errp, "Name or id must be provided"); 12365433c24fSMax Reitz goto out_aio_context; 123744e3e053SWenchao Xia } 123844e3e053SWenchao Xia 12395433c24fSMax Reitz if (!blk_is_available(blk)) { 12405433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 12415433c24fSMax Reitz goto out_aio_context; 12425433c24fSMax Reitz } 12435433c24fSMax Reitz bs = blk_bs(blk); 12444ef3982aSStefan Hajnoczi 12450b928854SStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 12460b928854SStefan Hajnoczi goto out_aio_context; 12470b928854SStefan Hajnoczi } 12480b928854SStefan Hajnoczi 124944e3e053SWenchao Xia ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 125084d18f06SMarkus Armbruster if (local_err) { 125144e3e053SWenchao Xia error_propagate(errp, local_err); 12524ef3982aSStefan Hajnoczi goto out_aio_context; 125344e3e053SWenchao Xia } 125444e3e053SWenchao Xia if (!ret) { 125544e3e053SWenchao Xia error_setg(errp, 125644e3e053SWenchao Xia "Snapshot with id '%s' and name '%s' does not exist on " 125744e3e053SWenchao Xia "device '%s'", 125844e3e053SWenchao Xia STR_OR_NULL(id), STR_OR_NULL(name), device); 12594ef3982aSStefan Hajnoczi goto out_aio_context; 126044e3e053SWenchao Xia } 126144e3e053SWenchao Xia 126244e3e053SWenchao Xia bdrv_snapshot_delete(bs, id, name, &local_err); 126384d18f06SMarkus Armbruster if (local_err) { 126444e3e053SWenchao Xia error_propagate(errp, local_err); 12654ef3982aSStefan Hajnoczi goto out_aio_context; 126644e3e053SWenchao Xia } 126744e3e053SWenchao Xia 12684ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12694ef3982aSStefan Hajnoczi 12705839e53bSMarkus Armbruster info = g_new0(SnapshotInfo, 1); 127144e3e053SWenchao Xia info->id = g_strdup(sn.id_str); 127244e3e053SWenchao Xia info->name = g_strdup(sn.name); 127344e3e053SWenchao Xia info->date_nsec = sn.date_nsec; 127444e3e053SWenchao Xia info->date_sec = sn.date_sec; 127544e3e053SWenchao Xia info->vm_state_size = sn.vm_state_size; 127644e3e053SWenchao Xia info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 127744e3e053SWenchao Xia info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 127844e3e053SWenchao Xia 127944e3e053SWenchao Xia return info; 12804ef3982aSStefan Hajnoczi 12814ef3982aSStefan Hajnoczi out_aio_context: 12824ef3982aSStefan Hajnoczi aio_context_release(aio_context); 12834ef3982aSStefan Hajnoczi return NULL; 128444e3e053SWenchao Xia } 12858802d1fdSJeff Cody 1286341ebc2fSJohn Snow /** 1287341ebc2fSJohn Snow * block_dirty_bitmap_lookup: 1288341ebc2fSJohn Snow * Return a dirty bitmap (if present), after validating 1289341ebc2fSJohn Snow * the node reference and bitmap names. 1290341ebc2fSJohn Snow * 1291341ebc2fSJohn Snow * @node: The name of the BDS node to search for bitmaps 1292341ebc2fSJohn Snow * @name: The name of the bitmap to search for 1293341ebc2fSJohn Snow * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1294341ebc2fSJohn Snow * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1295341ebc2fSJohn Snow * @errp: Output pointer for error information. Can be NULL. 1296341ebc2fSJohn Snow * 1297341ebc2fSJohn Snow * @return: A bitmap object on success, or NULL on failure. 1298341ebc2fSJohn Snow */ 1299341ebc2fSJohn Snow static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1300341ebc2fSJohn Snow const char *name, 1301341ebc2fSJohn Snow BlockDriverState **pbs, 1302341ebc2fSJohn Snow AioContext **paio, 1303341ebc2fSJohn Snow Error **errp) 1304341ebc2fSJohn Snow { 1305341ebc2fSJohn Snow BlockDriverState *bs; 1306341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 1307341ebc2fSJohn Snow AioContext *aio_context; 1308341ebc2fSJohn Snow 1309341ebc2fSJohn Snow if (!node) { 1310341ebc2fSJohn Snow error_setg(errp, "Node cannot be NULL"); 1311341ebc2fSJohn Snow return NULL; 1312341ebc2fSJohn Snow } 1313341ebc2fSJohn Snow if (!name) { 1314341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be NULL"); 1315341ebc2fSJohn Snow return NULL; 1316341ebc2fSJohn Snow } 1317341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, NULL); 1318341ebc2fSJohn Snow if (!bs) { 1319341ebc2fSJohn Snow error_setg(errp, "Node '%s' not found", node); 1320341ebc2fSJohn Snow return NULL; 1321341ebc2fSJohn Snow } 1322341ebc2fSJohn Snow 1323341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 1324341ebc2fSJohn Snow aio_context_acquire(aio_context); 1325341ebc2fSJohn Snow 1326341ebc2fSJohn Snow bitmap = bdrv_find_dirty_bitmap(bs, name); 1327341ebc2fSJohn Snow if (!bitmap) { 1328341ebc2fSJohn Snow error_setg(errp, "Dirty bitmap '%s' not found", name); 1329341ebc2fSJohn Snow goto fail; 1330341ebc2fSJohn Snow } 1331341ebc2fSJohn Snow 1332341ebc2fSJohn Snow if (pbs) { 1333341ebc2fSJohn Snow *pbs = bs; 1334341ebc2fSJohn Snow } 1335341ebc2fSJohn Snow if (paio) { 1336341ebc2fSJohn Snow *paio = aio_context; 1337341ebc2fSJohn Snow } else { 1338341ebc2fSJohn Snow aio_context_release(aio_context); 1339341ebc2fSJohn Snow } 1340341ebc2fSJohn Snow 1341341ebc2fSJohn Snow return bitmap; 1342341ebc2fSJohn Snow 1343341ebc2fSJohn Snow fail: 1344341ebc2fSJohn Snow aio_context_release(aio_context); 1345341ebc2fSJohn Snow return NULL; 1346341ebc2fSJohn Snow } 1347341ebc2fSJohn Snow 1348b756b9ceSStefan Hajnoczi /* New and old BlockDriverState structs for atomic group operations */ 1349ba0c86a3SWenchao Xia 1350ba5d6ab6SStefan Hajnoczi typedef struct BlkTransactionState BlkTransactionState; 1351ba0c86a3SWenchao Xia 1352ba0c86a3SWenchao Xia /* Only prepare() may fail. In a single transaction, only one of commit() or 1353ba0c86a3SWenchao Xia abort() will be called, clean() will always be called if it present. */ 1354ba0c86a3SWenchao Xia typedef struct BdrvActionOps { 1355ba0c86a3SWenchao Xia /* Size of state struct, in bytes. */ 1356ba0c86a3SWenchao Xia size_t instance_size; 1357ba0c86a3SWenchao Xia /* Prepare the work, must NOT be NULL. */ 1358ba5d6ab6SStefan Hajnoczi void (*prepare)(BlkTransactionState *common, Error **errp); 1359f9ea81e8SStefan Hajnoczi /* Commit the changes, can be NULL. */ 1360ba5d6ab6SStefan Hajnoczi void (*commit)(BlkTransactionState *common); 1361ba0c86a3SWenchao Xia /* Abort the changes on fail, can be NULL. */ 1362ba5d6ab6SStefan Hajnoczi void (*abort)(BlkTransactionState *common); 1363ba0c86a3SWenchao Xia /* Clean up resource in the end, can be NULL. */ 1364ba5d6ab6SStefan Hajnoczi void (*clean)(BlkTransactionState *common); 1365ba0c86a3SWenchao Xia } BdrvActionOps; 1366ba0c86a3SWenchao Xia 1367ba0c86a3SWenchao Xia /* 1368ba0c86a3SWenchao Xia * This structure must be arranged as first member in child type, assuming 1369ba0c86a3SWenchao Xia * that compiler will also arrange it to the same address with parent instance. 1370ba0c86a3SWenchao Xia * Later it will be used in free(). 1371ba0c86a3SWenchao Xia */ 1372ba5d6ab6SStefan Hajnoczi struct BlkTransactionState { 1373c8a83e85SKevin Wolf TransactionAction *action; 1374ba0c86a3SWenchao Xia const BdrvActionOps *ops; 1375ba5d6ab6SStefan Hajnoczi QSIMPLEQ_ENTRY(BlkTransactionState) entry; 1376ba0c86a3SWenchao Xia }; 1377ba0c86a3SWenchao Xia 1378bbe86010SWenchao Xia /* internal snapshot private data */ 1379bbe86010SWenchao Xia typedef struct InternalSnapshotState { 1380bbe86010SWenchao Xia BlkTransactionState common; 1381bbe86010SWenchao Xia BlockDriverState *bs; 13825d6e96efSStefan Hajnoczi AioContext *aio_context; 1383bbe86010SWenchao Xia QEMUSnapshotInfo sn; 1384507306ccSFam Zheng bool created; 1385bbe86010SWenchao Xia } InternalSnapshotState; 1386bbe86010SWenchao Xia 1387bbe86010SWenchao Xia static void internal_snapshot_prepare(BlkTransactionState *common, 1388bbe86010SWenchao Xia Error **errp) 1389bbe86010SWenchao Xia { 1390f70edf99SMarkus Armbruster Error *local_err = NULL; 1391bbe86010SWenchao Xia const char *device; 1392bbe86010SWenchao Xia const char *name; 1393a0e8544cSFam Zheng BlockBackend *blk; 1394bbe86010SWenchao Xia BlockDriverState *bs; 1395bbe86010SWenchao Xia QEMUSnapshotInfo old_sn, *sn; 1396bbe86010SWenchao Xia bool ret; 1397bbe86010SWenchao Xia qemu_timeval tv; 1398bbe86010SWenchao Xia BlockdevSnapshotInternal *internal; 1399bbe86010SWenchao Xia InternalSnapshotState *state; 1400bbe86010SWenchao Xia int ret1; 1401bbe86010SWenchao Xia 14026a8f9661SEric Blake g_assert(common->action->type == 1403bbe86010SWenchao Xia TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 14046a8f9661SEric Blake internal = common->action->u.blockdev_snapshot_internal_sync; 1405bbe86010SWenchao Xia state = DO_UPCAST(InternalSnapshotState, common, common); 1406bbe86010SWenchao Xia 1407bbe86010SWenchao Xia /* 1. parse input */ 1408bbe86010SWenchao Xia device = internal->device; 1409bbe86010SWenchao Xia name = internal->name; 1410bbe86010SWenchao Xia 1411bbe86010SWenchao Xia /* 2. check for validation */ 1412a0e8544cSFam Zheng blk = blk_by_name(device); 1413a0e8544cSFam Zheng if (!blk) { 141475158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 141575158ebbSMarkus Armbruster "Device '%s' not found", device); 1416bbe86010SWenchao Xia return; 1417bbe86010SWenchao Xia } 1418bbe86010SWenchao Xia 14195d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 14205433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 14215d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 14225d6e96efSStefan Hajnoczi 14235433c24fSMax Reitz if (!blk_is_available(blk)) { 1424c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1425bbe86010SWenchao Xia return; 1426bbe86010SWenchao Xia } 14275433c24fSMax Reitz bs = blk_bs(blk); 1428bbe86010SWenchao Xia 1429507306ccSFam Zheng state->bs = bs; 1430507306ccSFam Zheng bdrv_drained_begin(bs); 1431507306ccSFam Zheng 14323dc7ca3cSStefan Hajnoczi if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 14333dc7ca3cSStefan Hajnoczi return; 14343dc7ca3cSStefan Hajnoczi } 14353dc7ca3cSStefan Hajnoczi 1436bbe86010SWenchao Xia if (bdrv_is_read_only(bs)) { 143781e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 1438bbe86010SWenchao Xia return; 1439bbe86010SWenchao Xia } 1440bbe86010SWenchao Xia 1441bbe86010SWenchao Xia if (!bdrv_can_snapshot(bs)) { 144281e5f78aSAlberto Garcia error_setg(errp, "Block format '%s' used by device '%s' " 144381e5f78aSAlberto Garcia "does not support internal snapshots", 144481e5f78aSAlberto Garcia bs->drv->format_name, device); 1445bbe86010SWenchao Xia return; 1446bbe86010SWenchao Xia } 1447bbe86010SWenchao Xia 1448bbe86010SWenchao Xia if (!strlen(name)) { 1449bbe86010SWenchao Xia error_setg(errp, "Name is empty"); 1450bbe86010SWenchao Xia return; 1451bbe86010SWenchao Xia } 1452bbe86010SWenchao Xia 1453bbe86010SWenchao Xia /* check whether a snapshot with name exist */ 1454f70edf99SMarkus Armbruster ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1455f70edf99SMarkus Armbruster &local_err); 1456f70edf99SMarkus Armbruster if (local_err) { 1457f70edf99SMarkus Armbruster error_propagate(errp, local_err); 1458bbe86010SWenchao Xia return; 1459bbe86010SWenchao Xia } else if (ret) { 1460bbe86010SWenchao Xia error_setg(errp, 1461bbe86010SWenchao Xia "Snapshot with name '%s' already exists on device '%s'", 1462bbe86010SWenchao Xia name, device); 1463bbe86010SWenchao Xia return; 1464bbe86010SWenchao Xia } 1465bbe86010SWenchao Xia 1466bbe86010SWenchao Xia /* 3. take the snapshot */ 1467bbe86010SWenchao Xia sn = &state->sn; 1468bbe86010SWenchao Xia pstrcpy(sn->name, sizeof(sn->name), name); 1469bbe86010SWenchao Xia qemu_gettimeofday(&tv); 1470bbe86010SWenchao Xia sn->date_sec = tv.tv_sec; 1471bbe86010SWenchao Xia sn->date_nsec = tv.tv_usec * 1000; 1472bbe86010SWenchao Xia sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1473bbe86010SWenchao Xia 1474bbe86010SWenchao Xia ret1 = bdrv_snapshot_create(bs, sn); 1475bbe86010SWenchao Xia if (ret1 < 0) { 1476bbe86010SWenchao Xia error_setg_errno(errp, -ret1, 1477bbe86010SWenchao Xia "Failed to create snapshot '%s' on device '%s'", 1478bbe86010SWenchao Xia name, device); 1479bbe86010SWenchao Xia return; 1480bbe86010SWenchao Xia } 1481bbe86010SWenchao Xia 1482bbe86010SWenchao Xia /* 4. succeed, mark a snapshot is created */ 1483507306ccSFam Zheng state->created = true; 1484bbe86010SWenchao Xia } 1485bbe86010SWenchao Xia 1486bbe86010SWenchao Xia static void internal_snapshot_abort(BlkTransactionState *common) 1487bbe86010SWenchao Xia { 1488bbe86010SWenchao Xia InternalSnapshotState *state = 1489bbe86010SWenchao Xia DO_UPCAST(InternalSnapshotState, common, common); 1490bbe86010SWenchao Xia BlockDriverState *bs = state->bs; 1491bbe86010SWenchao Xia QEMUSnapshotInfo *sn = &state->sn; 1492bbe86010SWenchao Xia Error *local_error = NULL; 1493bbe86010SWenchao Xia 1494507306ccSFam Zheng if (!state->created) { 1495bbe86010SWenchao Xia return; 1496bbe86010SWenchao Xia } 1497bbe86010SWenchao Xia 1498bbe86010SWenchao Xia if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1499bbe86010SWenchao Xia error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1500bbe86010SWenchao Xia "device '%s' in abort: %s", 1501bbe86010SWenchao Xia sn->id_str, 1502bbe86010SWenchao Xia sn->name, 1503bbe86010SWenchao Xia bdrv_get_device_name(bs), 1504bbe86010SWenchao Xia error_get_pretty(local_error)); 1505bbe86010SWenchao Xia error_free(local_error); 1506bbe86010SWenchao Xia } 1507bbe86010SWenchao Xia } 1508bbe86010SWenchao Xia 15095d6e96efSStefan Hajnoczi static void internal_snapshot_clean(BlkTransactionState *common) 15105d6e96efSStefan Hajnoczi { 15115d6e96efSStefan Hajnoczi InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 15125d6e96efSStefan Hajnoczi common, common); 15135d6e96efSStefan Hajnoczi 15145d6e96efSStefan Hajnoczi if (state->aio_context) { 1515507306ccSFam Zheng if (state->bs) { 1516507306ccSFam Zheng bdrv_drained_end(state->bs); 1517507306ccSFam Zheng } 15185d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 15195d6e96efSStefan Hajnoczi } 15205d6e96efSStefan Hajnoczi } 15215d6e96efSStefan Hajnoczi 1522ba0c86a3SWenchao Xia /* external snapshot private data */ 1523ba5d6ab6SStefan Hajnoczi typedef struct ExternalSnapshotState { 1524ba5d6ab6SStefan Hajnoczi BlkTransactionState common; 15258802d1fdSJeff Cody BlockDriverState *old_bs; 15268802d1fdSJeff Cody BlockDriverState *new_bs; 15275d6e96efSStefan Hajnoczi AioContext *aio_context; 1528ba5d6ab6SStefan Hajnoczi } ExternalSnapshotState; 15298802d1fdSJeff Cody 1530ba5d6ab6SStefan Hajnoczi static void external_snapshot_prepare(BlkTransactionState *common, 15319b9877eeSWenchao Xia Error **errp) 15329b9877eeSWenchao Xia { 15339b9877eeSWenchao Xia int flags, ret; 1534e6641719SMax Reitz QDict *options; 15359b9877eeSWenchao Xia Error *local_err = NULL; 15360901f67eSBenoît Canet bool has_device = false; 1537e2a31e87SWenchao Xia const char *device; 15380901f67eSBenoît Canet bool has_node_name = false; 15390901f67eSBenoît Canet const char *node_name; 15400901f67eSBenoît Canet bool has_snapshot_node_name = false; 15410901f67eSBenoît Canet const char *snapshot_node_name; 1542e2a31e87SWenchao Xia const char *new_image_file; 1543e2a31e87SWenchao Xia const char *format = "qcow2"; 1544e2a31e87SWenchao Xia enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1545ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1546ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1547c8a83e85SKevin Wolf TransactionAction *action = common->action; 15489b9877eeSWenchao Xia 1549e2a31e87SWenchao Xia /* get parameters */ 15506a8f9661SEric Blake g_assert(action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); 1551e2a31e87SWenchao Xia 15526a8f9661SEric Blake has_device = action->u.blockdev_snapshot_sync->has_device; 15536a8f9661SEric Blake device = action->u.blockdev_snapshot_sync->device; 15546a8f9661SEric Blake has_node_name = action->u.blockdev_snapshot_sync->has_node_name; 15556a8f9661SEric Blake node_name = action->u.blockdev_snapshot_sync->node_name; 15560901f67eSBenoît Canet has_snapshot_node_name = 15576a8f9661SEric Blake action->u.blockdev_snapshot_sync->has_snapshot_node_name; 15586a8f9661SEric Blake snapshot_node_name = action->u.blockdev_snapshot_sync->snapshot_node_name; 15590901f67eSBenoît Canet 15606a8f9661SEric Blake new_image_file = action->u.blockdev_snapshot_sync->snapshot_file; 15616a8f9661SEric Blake if (action->u.blockdev_snapshot_sync->has_format) { 15626a8f9661SEric Blake format = action->u.blockdev_snapshot_sync->format; 1563e2a31e87SWenchao Xia } 15646a8f9661SEric Blake if (action->u.blockdev_snapshot_sync->has_mode) { 15656a8f9661SEric Blake mode = action->u.blockdev_snapshot_sync->mode; 1566e2a31e87SWenchao Xia } 1567e2a31e87SWenchao Xia 1568e2a31e87SWenchao Xia /* start processing */ 15690901f67eSBenoît Canet state->old_bs = bdrv_lookup_bs(has_device ? device : NULL, 15700901f67eSBenoît Canet has_node_name ? node_name : NULL, 15710901f67eSBenoît Canet &local_err); 157284d18f06SMarkus Armbruster if (local_err) { 15730901f67eSBenoît Canet error_propagate(errp, local_err); 15740901f67eSBenoît Canet return; 15750901f67eSBenoît Canet } 15760901f67eSBenoît Canet 15770901f67eSBenoît Canet if (has_node_name && !has_snapshot_node_name) { 15780901f67eSBenoît Canet error_setg(errp, "New snapshot node name missing"); 15790901f67eSBenoît Canet return; 15800901f67eSBenoît Canet } 15810901f67eSBenoît Canet 15820901f67eSBenoît Canet if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) { 15830901f67eSBenoît Canet error_setg(errp, "New snapshot node name already existing"); 15849b9877eeSWenchao Xia return; 15859b9877eeSWenchao Xia } 15869b9877eeSWenchao Xia 15875d6e96efSStefan Hajnoczi /* Acquire AioContext now so any threads operating on old_bs stop */ 15885d6e96efSStefan Hajnoczi state->aio_context = bdrv_get_aio_context(state->old_bs); 15895d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 1590da763e83SFam Zheng bdrv_drained_begin(state->old_bs); 15915d6e96efSStefan Hajnoczi 1592ba5d6ab6SStefan Hajnoczi if (!bdrv_is_inserted(state->old_bs)) { 1593c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 15949b9877eeSWenchao Xia return; 15959b9877eeSWenchao Xia } 15969b9877eeSWenchao Xia 15973718d8abSFam Zheng if (bdrv_op_is_blocked(state->old_bs, 15983718d8abSFam Zheng BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 15999b9877eeSWenchao Xia return; 16009b9877eeSWenchao Xia } 16019b9877eeSWenchao Xia 1602ba5d6ab6SStefan Hajnoczi if (!bdrv_is_read_only(state->old_bs)) { 1603ba5d6ab6SStefan Hajnoczi if (bdrv_flush(state->old_bs)) { 1604c6bd8c70SMarkus Armbruster error_setg(errp, QERR_IO_ERROR); 16059b9877eeSWenchao Xia return; 16069b9877eeSWenchao Xia } 16079b9877eeSWenchao Xia } 16089b9877eeSWenchao Xia 1609212a5a8fSBenoît Canet if (!bdrv_is_first_non_filter(state->old_bs)) { 1610c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1611f6186f49SBenoît Canet return; 1612f6186f49SBenoît Canet } 1613f6186f49SBenoît Canet 1614ba5d6ab6SStefan Hajnoczi flags = state->old_bs->open_flags; 16159b9877eeSWenchao Xia 16169b9877eeSWenchao Xia /* create new image w/backing file */ 16179b9877eeSWenchao Xia if (mode != NEW_IMAGE_MODE_EXISTING) { 16189b9877eeSWenchao Xia bdrv_img_create(new_image_file, format, 1619ba5d6ab6SStefan Hajnoczi state->old_bs->filename, 1620ba5d6ab6SStefan Hajnoczi state->old_bs->drv->format_name, 16219b9877eeSWenchao Xia NULL, -1, flags, &local_err, false); 162284d18f06SMarkus Armbruster if (local_err) { 16239b9877eeSWenchao Xia error_propagate(errp, local_err); 16249b9877eeSWenchao Xia return; 16259b9877eeSWenchao Xia } 16269b9877eeSWenchao Xia } 16279b9877eeSWenchao Xia 16280901f67eSBenoît Canet options = qdict_new(); 1629e6641719SMax Reitz if (has_snapshot_node_name) { 16300901f67eSBenoît Canet qdict_put(options, "node-name", 16310901f67eSBenoît Canet qstring_from_str(snapshot_node_name)); 16320901f67eSBenoît Canet } 1633e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 16340901f67eSBenoît Canet 16359b9877eeSWenchao Xia /* TODO Inherit bs->options or only take explicit options with an 16369b9877eeSWenchao Xia * extended QMP command? */ 1637f67503e5SMax Reitz assert(state->new_bs == NULL); 1638ddf5636dSMax Reitz ret = bdrv_open(&state->new_bs, new_image_file, NULL, options, 16396ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 1640f67503e5SMax Reitz /* We will manually add the backing_hd field to the bs later */ 16419b9877eeSWenchao Xia if (ret != 0) { 164234b5d2c6SMax Reitz error_propagate(errp, local_err); 16439b9877eeSWenchao Xia } 16449b9877eeSWenchao Xia } 16459b9877eeSWenchao Xia 1646ba5d6ab6SStefan Hajnoczi static void external_snapshot_commit(BlkTransactionState *common) 16473b0047e8SWenchao Xia { 1648ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1649ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1650ba0c86a3SWenchao Xia 16515d6e96efSStefan Hajnoczi bdrv_set_aio_context(state->new_bs, state->aio_context); 16525d6e96efSStefan Hajnoczi 1653ba5d6ab6SStefan Hajnoczi /* This removes our old bs and adds the new bs */ 1654ba5d6ab6SStefan Hajnoczi bdrv_append(state->new_bs, state->old_bs); 16553b0047e8SWenchao Xia /* We don't need (or want) to use the transactional 16563b0047e8SWenchao Xia * bdrv_reopen_multiple() across all the entries at once, because we 16573b0047e8SWenchao Xia * don't want to abort all of them if one of them fails the reopen */ 1658dd62f1caSKevin Wolf bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 16593b0047e8SWenchao Xia NULL); 16603b0047e8SWenchao Xia } 16613b0047e8SWenchao Xia 1662ba5d6ab6SStefan Hajnoczi static void external_snapshot_abort(BlkTransactionState *common) 166396b86bf7SWenchao Xia { 1664ba5d6ab6SStefan Hajnoczi ExternalSnapshotState *state = 1665ba5d6ab6SStefan Hajnoczi DO_UPCAST(ExternalSnapshotState, common, common); 1666ba5d6ab6SStefan Hajnoczi if (state->new_bs) { 16674f6fd349SFam Zheng bdrv_unref(state->new_bs); 166896b86bf7SWenchao Xia } 1669da763e83SFam Zheng } 1670da763e83SFam Zheng 1671da763e83SFam Zheng static void external_snapshot_clean(BlkTransactionState *common) 1672da763e83SFam Zheng { 1673da763e83SFam Zheng ExternalSnapshotState *state = 1674da763e83SFam Zheng DO_UPCAST(ExternalSnapshotState, common, common); 16755d6e96efSStefan Hajnoczi if (state->aio_context) { 1676da763e83SFam Zheng bdrv_drained_end(state->old_bs); 16775d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 16785d6e96efSStefan Hajnoczi } 167996b86bf7SWenchao Xia } 168096b86bf7SWenchao Xia 16813037f364SStefan Hajnoczi typedef struct DriveBackupState { 16823037f364SStefan Hajnoczi BlkTransactionState common; 16833037f364SStefan Hajnoczi BlockDriverState *bs; 16845d6e96efSStefan Hajnoczi AioContext *aio_context; 16853037f364SStefan Hajnoczi BlockJob *job; 16863037f364SStefan Hajnoczi } DriveBackupState; 16873037f364SStefan Hajnoczi 16883037f364SStefan Hajnoczi static void drive_backup_prepare(BlkTransactionState *common, Error **errp) 16893037f364SStefan Hajnoczi { 16903037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1691a0e8544cSFam Zheng BlockBackend *blk; 16923037f364SStefan Hajnoczi DriveBackup *backup; 16933037f364SStefan Hajnoczi Error *local_err = NULL; 16943037f364SStefan Hajnoczi 16956a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 16966a8f9661SEric Blake backup = common->action->u.drive_backup; 16973037f364SStefan Hajnoczi 1698a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1699a0e8544cSFam Zheng if (!blk) { 170075158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 170175158ebbSMarkus Armbruster "Device '%s' not found", backup->device); 17025d6e96efSStefan Hajnoczi return; 17035d6e96efSStefan Hajnoczi } 17045d6e96efSStefan Hajnoczi 17051fdd4b7bSFam Zheng if (!blk_is_available(blk)) { 17061fdd4b7bSFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 17071fdd4b7bSFam Zheng return; 17081fdd4b7bSFam Zheng } 17091fdd4b7bSFam Zheng 17105d6e96efSStefan Hajnoczi /* AioContext is released in .clean() */ 17115433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 17125d6e96efSStefan Hajnoczi aio_context_acquire(state->aio_context); 17131fdd4b7bSFam Zheng bdrv_drained_begin(blk_bs(blk)); 17141fdd4b7bSFam Zheng state->bs = blk_bs(blk); 17155d6e96efSStefan Hajnoczi 17163037f364SStefan Hajnoczi qmp_drive_backup(backup->device, backup->target, 17173037f364SStefan Hajnoczi backup->has_format, backup->format, 1718b53169eaSStefan Hajnoczi backup->sync, 17193037f364SStefan Hajnoczi backup->has_mode, backup->mode, 17203037f364SStefan Hajnoczi backup->has_speed, backup->speed, 1721d58d8453SJohn Snow backup->has_bitmap, backup->bitmap, 17223037f364SStefan Hajnoczi backup->has_on_source_error, backup->on_source_error, 17233037f364SStefan Hajnoczi backup->has_on_target_error, backup->on_target_error, 17243037f364SStefan Hajnoczi &local_err); 172584d18f06SMarkus Armbruster if (local_err) { 17263037f364SStefan Hajnoczi error_propagate(errp, local_err); 17273037f364SStefan Hajnoczi return; 17283037f364SStefan Hajnoczi } 17293037f364SStefan Hajnoczi 17303037f364SStefan Hajnoczi state->job = state->bs->job; 17313037f364SStefan Hajnoczi } 17323037f364SStefan Hajnoczi 17333037f364SStefan Hajnoczi static void drive_backup_abort(BlkTransactionState *common) 17343037f364SStefan Hajnoczi { 17353037f364SStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 17363037f364SStefan Hajnoczi BlockDriverState *bs = state->bs; 17373037f364SStefan Hajnoczi 17383037f364SStefan Hajnoczi /* Only cancel if it's the job we started */ 17393037f364SStefan Hajnoczi if (bs && bs->job && bs->job == state->job) { 17403037f364SStefan Hajnoczi block_job_cancel_sync(bs->job); 17413037f364SStefan Hajnoczi } 17423037f364SStefan Hajnoczi } 17433037f364SStefan Hajnoczi 17445d6e96efSStefan Hajnoczi static void drive_backup_clean(BlkTransactionState *common) 17455d6e96efSStefan Hajnoczi { 17465d6e96efSStefan Hajnoczi DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 17475d6e96efSStefan Hajnoczi 17485d6e96efSStefan Hajnoczi if (state->aio_context) { 17491fdd4b7bSFam Zheng bdrv_drained_end(state->bs); 17505d6e96efSStefan Hajnoczi aio_context_release(state->aio_context); 17515d6e96efSStefan Hajnoczi } 17525d6e96efSStefan Hajnoczi } 17535d6e96efSStefan Hajnoczi 1754bd8baecdSFam Zheng typedef struct BlockdevBackupState { 1755bd8baecdSFam Zheng BlkTransactionState common; 1756bd8baecdSFam Zheng BlockDriverState *bs; 1757bd8baecdSFam Zheng BlockJob *job; 1758bd8baecdSFam Zheng AioContext *aio_context; 1759bd8baecdSFam Zheng } BlockdevBackupState; 1760bd8baecdSFam Zheng 1761bd8baecdSFam Zheng static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp) 1762bd8baecdSFam Zheng { 1763bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1764bd8baecdSFam Zheng BlockdevBackup *backup; 17655433c24fSMax Reitz BlockBackend *blk, *target; 1766bd8baecdSFam Zheng Error *local_err = NULL; 1767bd8baecdSFam Zheng 17686a8f9661SEric Blake assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 17696a8f9661SEric Blake backup = common->action->u.blockdev_backup; 1770bd8baecdSFam Zheng 1771a0e8544cSFam Zheng blk = blk_by_name(backup->device); 1772a0e8544cSFam Zheng if (!blk) { 17735b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->device); 1774bd8baecdSFam Zheng return; 1775bd8baecdSFam Zheng } 1776bd8baecdSFam Zheng 1777ff52bf36SFam Zheng if (!blk_is_available(blk)) { 1778ff52bf36SFam Zheng error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1779ff52bf36SFam Zheng return; 1780ff52bf36SFam Zheng } 1781ff52bf36SFam Zheng 17825433c24fSMax Reitz target = blk_by_name(backup->target); 17835433c24fSMax Reitz if (!target) { 17845b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", backup->target); 1785bd8baecdSFam Zheng return; 1786bd8baecdSFam Zheng } 1787bd8baecdSFam Zheng 1788bd8baecdSFam Zheng /* AioContext is released in .clean() */ 17895433c24fSMax Reitz state->aio_context = blk_get_aio_context(blk); 17905433c24fSMax Reitz if (state->aio_context != blk_get_aio_context(target)) { 1791bd8baecdSFam Zheng state->aio_context = NULL; 1792bd8baecdSFam Zheng error_setg(errp, "Backup between two IO threads is not implemented"); 1793bd8baecdSFam Zheng return; 1794bd8baecdSFam Zheng } 1795bd8baecdSFam Zheng aio_context_acquire(state->aio_context); 1796ff52bf36SFam Zheng state->bs = blk_bs(blk); 1797ff52bf36SFam Zheng bdrv_drained_begin(state->bs); 1798bd8baecdSFam Zheng 1799bd8baecdSFam Zheng qmp_blockdev_backup(backup->device, backup->target, 1800bd8baecdSFam Zheng backup->sync, 1801bd8baecdSFam Zheng backup->has_speed, backup->speed, 1802bd8baecdSFam Zheng backup->has_on_source_error, backup->on_source_error, 1803bd8baecdSFam Zheng backup->has_on_target_error, backup->on_target_error, 1804bd8baecdSFam Zheng &local_err); 1805bd8baecdSFam Zheng if (local_err) { 1806bd8baecdSFam Zheng error_propagate(errp, local_err); 1807bd8baecdSFam Zheng return; 1808bd8baecdSFam Zheng } 1809bd8baecdSFam Zheng 1810bd8baecdSFam Zheng state->job = state->bs->job; 1811bd8baecdSFam Zheng } 1812bd8baecdSFam Zheng 1813bd8baecdSFam Zheng static void blockdev_backup_abort(BlkTransactionState *common) 1814bd8baecdSFam Zheng { 1815bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1816bd8baecdSFam Zheng BlockDriverState *bs = state->bs; 1817bd8baecdSFam Zheng 1818bd8baecdSFam Zheng /* Only cancel if it's the job we started */ 1819bd8baecdSFam Zheng if (bs && bs->job && bs->job == state->job) { 1820bd8baecdSFam Zheng block_job_cancel_sync(bs->job); 1821bd8baecdSFam Zheng } 1822bd8baecdSFam Zheng } 1823bd8baecdSFam Zheng 1824bd8baecdSFam Zheng static void blockdev_backup_clean(BlkTransactionState *common) 1825bd8baecdSFam Zheng { 1826bd8baecdSFam Zheng BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1827bd8baecdSFam Zheng 1828bd8baecdSFam Zheng if (state->aio_context) { 1829ff52bf36SFam Zheng bdrv_drained_end(state->bs); 1830bd8baecdSFam Zheng aio_context_release(state->aio_context); 1831bd8baecdSFam Zheng } 1832bd8baecdSFam Zheng } 1833bd8baecdSFam Zheng 183478b18b78SStefan Hajnoczi static void abort_prepare(BlkTransactionState *common, Error **errp) 183578b18b78SStefan Hajnoczi { 183678b18b78SStefan Hajnoczi error_setg(errp, "Transaction aborted using Abort action"); 183778b18b78SStefan Hajnoczi } 183878b18b78SStefan Hajnoczi 183978b18b78SStefan Hajnoczi static void abort_commit(BlkTransactionState *common) 184078b18b78SStefan Hajnoczi { 1841dfc6f865SStefan Weil g_assert_not_reached(); /* this action never succeeds */ 184278b18b78SStefan Hajnoczi } 184378b18b78SStefan Hajnoczi 1844ba0c86a3SWenchao Xia static const BdrvActionOps actions[] = { 1845c8a83e85SKevin Wolf [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 1846ba5d6ab6SStefan Hajnoczi .instance_size = sizeof(ExternalSnapshotState), 1847ba0c86a3SWenchao Xia .prepare = external_snapshot_prepare, 1848ba0c86a3SWenchao Xia .commit = external_snapshot_commit, 1849ba0c86a3SWenchao Xia .abort = external_snapshot_abort, 1850da763e83SFam Zheng .clean = external_snapshot_clean, 1851ba0c86a3SWenchao Xia }, 18523037f364SStefan Hajnoczi [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 18533037f364SStefan Hajnoczi .instance_size = sizeof(DriveBackupState), 18543037f364SStefan Hajnoczi .prepare = drive_backup_prepare, 18553037f364SStefan Hajnoczi .abort = drive_backup_abort, 18565d6e96efSStefan Hajnoczi .clean = drive_backup_clean, 18573037f364SStefan Hajnoczi }, 1858bd8baecdSFam Zheng [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 1859bd8baecdSFam Zheng .instance_size = sizeof(BlockdevBackupState), 1860bd8baecdSFam Zheng .prepare = blockdev_backup_prepare, 1861bd8baecdSFam Zheng .abort = blockdev_backup_abort, 1862bd8baecdSFam Zheng .clean = blockdev_backup_clean, 1863bd8baecdSFam Zheng }, 186478b18b78SStefan Hajnoczi [TRANSACTION_ACTION_KIND_ABORT] = { 186578b18b78SStefan Hajnoczi .instance_size = sizeof(BlkTransactionState), 186678b18b78SStefan Hajnoczi .prepare = abort_prepare, 186778b18b78SStefan Hajnoczi .commit = abort_commit, 186878b18b78SStefan Hajnoczi }, 1869bbe86010SWenchao Xia [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 1870bbe86010SWenchao Xia .instance_size = sizeof(InternalSnapshotState), 1871bbe86010SWenchao Xia .prepare = internal_snapshot_prepare, 1872bbe86010SWenchao Xia .abort = internal_snapshot_abort, 18735d6e96efSStefan Hajnoczi .clean = internal_snapshot_clean, 1874bbe86010SWenchao Xia }, 1875ba0c86a3SWenchao Xia }; 1876ba0c86a3SWenchao Xia 18778802d1fdSJeff Cody /* 1878b756b9ceSStefan Hajnoczi * 'Atomic' group operations. The operations are performed as a set, and if 1879b756b9ceSStefan Hajnoczi * any fail then we roll back all operations in the group. 18808802d1fdSJeff Cody */ 1881c8a83e85SKevin Wolf void qmp_transaction(TransactionActionList *dev_list, Error **errp) 18828802d1fdSJeff Cody { 1883c8a83e85SKevin Wolf TransactionActionList *dev_entry = dev_list; 1884ba5d6ab6SStefan Hajnoczi BlkTransactionState *state, *next; 188543e17041SLuiz Capitulino Error *local_err = NULL; 18868802d1fdSJeff Cody 1887ba5d6ab6SStefan Hajnoczi QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; 18888802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 18898802d1fdSJeff Cody 1890b756b9ceSStefan Hajnoczi /* drain all i/o before any operations */ 18918802d1fdSJeff Cody bdrv_drain_all(); 18928802d1fdSJeff Cody 1893b756b9ceSStefan Hajnoczi /* We don't do anything in this loop that commits us to the operations */ 18948802d1fdSJeff Cody while (NULL != dev_entry) { 1895c8a83e85SKevin Wolf TransactionAction *dev_info = NULL; 1896ba0c86a3SWenchao Xia const BdrvActionOps *ops; 189752e7c241SPaolo Bonzini 18988802d1fdSJeff Cody dev_info = dev_entry->value; 18998802d1fdSJeff Cody dev_entry = dev_entry->next; 19008802d1fdSJeff Cody 19016a8f9661SEric Blake assert(dev_info->type < ARRAY_SIZE(actions)); 1902ba0c86a3SWenchao Xia 19036a8f9661SEric Blake ops = &actions[dev_info->type]; 1904aa3fe714SMax Reitz assert(ops->instance_size > 0); 1905aa3fe714SMax Reitz 1906ba5d6ab6SStefan Hajnoczi state = g_malloc0(ops->instance_size); 1907ba5d6ab6SStefan Hajnoczi state->ops = ops; 1908ba5d6ab6SStefan Hajnoczi state->action = dev_info; 1909ba5d6ab6SStefan Hajnoczi QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 19108802d1fdSJeff Cody 1911ba5d6ab6SStefan Hajnoczi state->ops->prepare(state, &local_err); 191284d18f06SMarkus Armbruster if (local_err) { 19139b9877eeSWenchao Xia error_propagate(errp, local_err); 19149b9877eeSWenchao Xia goto delete_and_fail; 19159b9877eeSWenchao Xia } 191652e7c241SPaolo Bonzini } 19178802d1fdSJeff Cody 1918ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1919f9ea81e8SStefan Hajnoczi if (state->ops->commit) { 1920ba5d6ab6SStefan Hajnoczi state->ops->commit(state); 19218802d1fdSJeff Cody } 1922f9ea81e8SStefan Hajnoczi } 19238802d1fdSJeff Cody 19248802d1fdSJeff Cody /* success */ 19258802d1fdSJeff Cody goto exit; 19268802d1fdSJeff Cody 19278802d1fdSJeff Cody delete_and_fail: 1928b756b9ceSStefan Hajnoczi /* failure, and it is all-or-none; roll back all operations */ 1929ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1930ba5d6ab6SStefan Hajnoczi if (state->ops->abort) { 1931ba5d6ab6SStefan Hajnoczi state->ops->abort(state); 1932ba0c86a3SWenchao Xia } 19338802d1fdSJeff Cody } 19348802d1fdSJeff Cody exit: 1935ba5d6ab6SStefan Hajnoczi QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 1936ba5d6ab6SStefan Hajnoczi if (state->ops->clean) { 1937ba5d6ab6SStefan Hajnoczi state->ops->clean(state); 1938ba0c86a3SWenchao Xia } 1939ba5d6ab6SStefan Hajnoczi g_free(state); 19408802d1fdSJeff Cody } 19418802d1fdSJeff Cody } 19428802d1fdSJeff Cody 19438802d1fdSJeff Cody 19446007cdd4SMarkus Armbruster static void eject_device(BlockBackend *blk, int force, Error **errp) 1945666daa68SMarkus Armbruster { 19466007cdd4SMarkus Armbruster BlockDriverState *bs = blk_bs(blk); 1947e3442099SStefan Hajnoczi AioContext *aio_context; 1948e3442099SStefan Hajnoczi 19495433c24fSMax Reitz if (!bs) { 19505433c24fSMax Reitz /* No medium inserted, so there is nothing to do */ 19515433c24fSMax Reitz return; 19525433c24fSMax Reitz } 19535433c24fSMax Reitz 1954e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 1955e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 19566007cdd4SMarkus Armbruster 19573718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 1958e3442099SStefan Hajnoczi goto out; 19592d3735d3SStefan Hajnoczi } 1960a7f53e26SMarkus Armbruster if (!blk_dev_has_removable_media(blk)) { 1961f231b88dSCole Robinson error_setg(errp, "Device '%s' is not removable", 1962f231b88dSCole Robinson bdrv_get_device_name(bs)); 1963e3442099SStefan Hajnoczi goto out; 1964666daa68SMarkus Armbruster } 196592d48558SLuiz Capitulino 1966a7f53e26SMarkus Armbruster if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) { 1967a7f53e26SMarkus Armbruster blk_dev_eject_request(blk, force); 1968025ccaa7SPaolo Bonzini if (!force) { 1969f231b88dSCole Robinson error_setg(errp, "Device '%s' is locked", 1970f231b88dSCole Robinson bdrv_get_device_name(bs)); 1971e3442099SStefan Hajnoczi goto out; 1972666daa68SMarkus Armbruster } 1973025ccaa7SPaolo Bonzini } 197492d48558SLuiz Capitulino 1975666daa68SMarkus Armbruster bdrv_close(bs); 1976e3442099SStefan Hajnoczi 1977e3442099SStefan Hajnoczi out: 1978e3442099SStefan Hajnoczi aio_context_release(aio_context); 1979666daa68SMarkus Armbruster } 1980666daa68SMarkus Armbruster 1981c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 1982666daa68SMarkus Armbruster { 19836007cdd4SMarkus Armbruster BlockBackend *blk; 1984666daa68SMarkus Armbruster 19856007cdd4SMarkus Armbruster blk = blk_by_name(device); 19866007cdd4SMarkus Armbruster if (!blk) { 198775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 198875158ebbSMarkus Armbruster "Device '%s' not found", device); 1989c245b6a3SLuiz Capitulino return; 1990666daa68SMarkus Armbruster } 199192d48558SLuiz Capitulino 19926007cdd4SMarkus Armbruster eject_device(blk, force, errp); 1993666daa68SMarkus Armbruster } 1994666daa68SMarkus Armbruster 199512d3ba82SBenoît Canet void qmp_block_passwd(bool has_device, const char *device, 199612d3ba82SBenoît Canet bool has_node_name, const char *node_name, 199712d3ba82SBenoît Canet const char *password, Error **errp) 1998666daa68SMarkus Armbruster { 199912d3ba82SBenoît Canet Error *local_err = NULL; 2000666daa68SMarkus Armbruster BlockDriverState *bs; 2001e3442099SStefan Hajnoczi AioContext *aio_context; 2002666daa68SMarkus Armbruster 200312d3ba82SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 200412d3ba82SBenoît Canet has_node_name ? node_name : NULL, 200512d3ba82SBenoît Canet &local_err); 200684d18f06SMarkus Armbruster if (local_err) { 200712d3ba82SBenoît Canet error_propagate(errp, local_err); 2008a4dea8a9SLuiz Capitulino return; 2009666daa68SMarkus Armbruster } 2010666daa68SMarkus Armbruster 2011e3442099SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2012e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2013e3442099SStefan Hajnoczi 20144d2855a3SMarkus Armbruster bdrv_add_key(bs, password, errp); 2015666daa68SMarkus Armbruster 2016e3442099SStefan Hajnoczi aio_context_release(aio_context); 2017e3442099SStefan Hajnoczi } 2018e3442099SStefan Hajnoczi 2019e3442099SStefan Hajnoczi /* Assumes AioContext is held */ 20205433c24fSMax Reitz static void qmp_bdrv_open_encrypted(BlockDriverState **pbs, 20215433c24fSMax Reitz const char *filename, 2022e6641719SMax Reitz int bdrv_flags, const char *format, 2023333a96ecSLuiz Capitulino const char *password, Error **errp) 2024333a96ecSLuiz Capitulino { 202534b5d2c6SMax Reitz Error *local_err = NULL; 2026e6641719SMax Reitz QDict *options = NULL; 20270eef407cSLuiz Capitulino int ret; 20280eef407cSLuiz Capitulino 2029e6641719SMax Reitz if (format) { 2030e6641719SMax Reitz options = qdict_new(); 2031e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2032e6641719SMax Reitz } 2033e6641719SMax Reitz 20345433c24fSMax Reitz ret = bdrv_open(pbs, filename, NULL, options, bdrv_flags, &local_err); 20350eef407cSLuiz Capitulino if (ret < 0) { 203634b5d2c6SMax Reitz error_propagate(errp, local_err); 2037333a96ecSLuiz Capitulino return; 2038333a96ecSLuiz Capitulino } 2039333a96ecSLuiz Capitulino 20405433c24fSMax Reitz bdrv_add_key(*pbs, password, errp); 2041333a96ecSLuiz Capitulino } 2042333a96ecSLuiz Capitulino 2043333a96ecSLuiz Capitulino void qmp_change_blockdev(const char *device, const char *filename, 2044314f7ea7SMarc-André Lureau const char *format, Error **errp) 2045666daa68SMarkus Armbruster { 20466007cdd4SMarkus Armbruster BlockBackend *blk; 2047666daa68SMarkus Armbruster BlockDriverState *bs; 2048e3442099SStefan Hajnoczi AioContext *aio_context; 2049666daa68SMarkus Armbruster int bdrv_flags; 20505433c24fSMax Reitz bool new_bs; 205192d48558SLuiz Capitulino Error *err = NULL; 2052666daa68SMarkus Armbruster 20536007cdd4SMarkus Armbruster blk = blk_by_name(device); 20546007cdd4SMarkus Armbruster if (!blk) { 205575158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 205675158ebbSMarkus Armbruster "Device '%s' not found", device); 2057333a96ecSLuiz Capitulino return; 2058666daa68SMarkus Armbruster } 20596007cdd4SMarkus Armbruster bs = blk_bs(blk); 20605433c24fSMax Reitz new_bs = !bs; 2061333a96ecSLuiz Capitulino 20625433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2063e3442099SStefan Hajnoczi aio_context_acquire(aio_context); 2064e3442099SStefan Hajnoczi 20656007cdd4SMarkus Armbruster eject_device(blk, 0, &err); 206684d18f06SMarkus Armbruster if (err) { 2067333a96ecSLuiz Capitulino error_propagate(errp, err); 2068e3442099SStefan Hajnoczi goto out; 2069666daa68SMarkus Armbruster } 2070333a96ecSLuiz Capitulino 20715433c24fSMax Reitz bdrv_flags = blk_is_read_only(blk) ? 0 : BDRV_O_RDWR; 20725433c24fSMax Reitz bdrv_flags |= blk_get_root_state(blk)->open_flags & ~BDRV_O_RDWR; 2073333a96ecSLuiz Capitulino 20745433c24fSMax Reitz qmp_bdrv_open_encrypted(&bs, filename, bdrv_flags, format, NULL, &err); 20755433c24fSMax Reitz if (err) { 20765433c24fSMax Reitz error_propagate(errp, err); 20775433c24fSMax Reitz goto out; 20785433c24fSMax Reitz } 20795433c24fSMax Reitz 20805433c24fSMax Reitz if (new_bs) { 20815433c24fSMax Reitz blk_insert_bs(blk, bs); 20825433c24fSMax Reitz /* Has been sent automatically by bdrv_open() if blk_bs(blk) was not 20835433c24fSMax Reitz * NULL */ 20845433c24fSMax Reitz blk_dev_change_media_cb(blk, true); 20855433c24fSMax Reitz } 2086e3442099SStefan Hajnoczi 2087e3442099SStefan Hajnoczi out: 2088e3442099SStefan Hajnoczi aio_context_release(aio_context); 2089666daa68SMarkus Armbruster } 20909063f814SRyan Harper 20917d8a9f71SMax Reitz void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 20927d8a9f71SMax Reitz Error **errp) 20937d8a9f71SMax Reitz { 20947d8a9f71SMax Reitz BlockBackend *blk; 20957d8a9f71SMax Reitz bool locked; 20967d8a9f71SMax Reitz 20977d8a9f71SMax Reitz if (!has_force) { 20987d8a9f71SMax Reitz force = false; 20997d8a9f71SMax Reitz } 21007d8a9f71SMax Reitz 21017d8a9f71SMax Reitz blk = blk_by_name(device); 21027d8a9f71SMax Reitz if (!blk) { 21037d8a9f71SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 21047d8a9f71SMax Reitz "Device '%s' not found", device); 21057d8a9f71SMax Reitz return; 21067d8a9f71SMax Reitz } 21077d8a9f71SMax Reitz 21087d8a9f71SMax Reitz if (!blk_dev_has_removable_media(blk)) { 21097d8a9f71SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 21107d8a9f71SMax Reitz return; 21117d8a9f71SMax Reitz } 21127d8a9f71SMax Reitz 21137d8a9f71SMax Reitz if (blk_dev_is_tray_open(blk)) { 21147d8a9f71SMax Reitz return; 21157d8a9f71SMax Reitz } 21167d8a9f71SMax Reitz 21177d8a9f71SMax Reitz locked = blk_dev_is_medium_locked(blk); 21187d8a9f71SMax Reitz if (locked) { 21197d8a9f71SMax Reitz blk_dev_eject_request(blk, force); 21207d8a9f71SMax Reitz } 21217d8a9f71SMax Reitz 21227d8a9f71SMax Reitz if (!locked || force) { 21237d8a9f71SMax Reitz blk_dev_change_media_cb(blk, false); 21247d8a9f71SMax Reitz } 21257d8a9f71SMax Reitz } 21267d8a9f71SMax Reitz 2127abaaf59dSMax Reitz void qmp_blockdev_close_tray(const char *device, Error **errp) 2128abaaf59dSMax Reitz { 2129abaaf59dSMax Reitz BlockBackend *blk; 2130abaaf59dSMax Reitz 2131abaaf59dSMax Reitz blk = blk_by_name(device); 2132abaaf59dSMax Reitz if (!blk) { 2133abaaf59dSMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2134abaaf59dSMax Reitz "Device '%s' not found", device); 2135abaaf59dSMax Reitz return; 2136abaaf59dSMax Reitz } 2137abaaf59dSMax Reitz 2138abaaf59dSMax Reitz if (!blk_dev_has_removable_media(blk)) { 2139abaaf59dSMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2140abaaf59dSMax Reitz return; 2141abaaf59dSMax Reitz } 2142abaaf59dSMax Reitz 2143abaaf59dSMax Reitz if (!blk_dev_is_tray_open(blk)) { 2144abaaf59dSMax Reitz return; 2145abaaf59dSMax Reitz } 2146abaaf59dSMax Reitz 2147abaaf59dSMax Reitz blk_dev_change_media_cb(blk, true); 2148abaaf59dSMax Reitz } 2149abaaf59dSMax Reitz 2150*2814f672SMax Reitz void qmp_blockdev_remove_medium(const char *device, Error **errp) 2151*2814f672SMax Reitz { 2152*2814f672SMax Reitz BlockBackend *blk; 2153*2814f672SMax Reitz BlockDriverState *bs; 2154*2814f672SMax Reitz AioContext *aio_context; 2155*2814f672SMax Reitz bool has_device; 2156*2814f672SMax Reitz 2157*2814f672SMax Reitz blk = blk_by_name(device); 2158*2814f672SMax Reitz if (!blk) { 2159*2814f672SMax Reitz error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2160*2814f672SMax Reitz "Device '%s' not found", device); 2161*2814f672SMax Reitz return; 2162*2814f672SMax Reitz } 2163*2814f672SMax Reitz 2164*2814f672SMax Reitz /* For BBs without a device, we can exchange the BDS tree at will */ 2165*2814f672SMax Reitz has_device = blk_get_attached_dev(blk); 2166*2814f672SMax Reitz 2167*2814f672SMax Reitz if (has_device && !blk_dev_has_removable_media(blk)) { 2168*2814f672SMax Reitz error_setg(errp, "Device '%s' is not removable", device); 2169*2814f672SMax Reitz return; 2170*2814f672SMax Reitz } 2171*2814f672SMax Reitz 2172*2814f672SMax Reitz if (has_device && !blk_dev_is_tray_open(blk)) { 2173*2814f672SMax Reitz error_setg(errp, "Tray of device '%s' is not open", device); 2174*2814f672SMax Reitz return; 2175*2814f672SMax Reitz } 2176*2814f672SMax Reitz 2177*2814f672SMax Reitz bs = blk_bs(blk); 2178*2814f672SMax Reitz if (!bs) { 2179*2814f672SMax Reitz return; 2180*2814f672SMax Reitz } 2181*2814f672SMax Reitz 2182*2814f672SMax Reitz aio_context = bdrv_get_aio_context(bs); 2183*2814f672SMax Reitz aio_context_acquire(aio_context); 2184*2814f672SMax Reitz 2185*2814f672SMax Reitz if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 2186*2814f672SMax Reitz goto out; 2187*2814f672SMax Reitz } 2188*2814f672SMax Reitz 2189*2814f672SMax Reitz /* This follows the convention established by bdrv_make_anon() */ 2190*2814f672SMax Reitz if (bs->device_list.tqe_prev) { 2191*2814f672SMax Reitz QTAILQ_REMOVE(&bdrv_states, bs, device_list); 2192*2814f672SMax Reitz bs->device_list.tqe_prev = NULL; 2193*2814f672SMax Reitz } 2194*2814f672SMax Reitz 2195*2814f672SMax Reitz blk_remove_bs(blk); 2196*2814f672SMax Reitz 2197*2814f672SMax Reitz out: 2198*2814f672SMax Reitz aio_context_release(aio_context); 2199*2814f672SMax Reitz } 2200*2814f672SMax Reitz 2201727f005eSZhi Yong Wu /* throttling disk I/O limits */ 220280047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 22033e9fab69SBenoît Canet int64_t bps_wr, 22043e9fab69SBenoît Canet int64_t iops, 22053e9fab69SBenoît Canet int64_t iops_rd, 22063e9fab69SBenoît Canet int64_t iops_wr, 22073e9fab69SBenoît Canet bool has_bps_max, 22083e9fab69SBenoît Canet int64_t bps_max, 22093e9fab69SBenoît Canet bool has_bps_rd_max, 22103e9fab69SBenoît Canet int64_t bps_rd_max, 22113e9fab69SBenoît Canet bool has_bps_wr_max, 22123e9fab69SBenoît Canet int64_t bps_wr_max, 22133e9fab69SBenoît Canet bool has_iops_max, 22143e9fab69SBenoît Canet int64_t iops_max, 22153e9fab69SBenoît Canet bool has_iops_rd_max, 22163e9fab69SBenoît Canet int64_t iops_rd_max, 22173e9fab69SBenoît Canet bool has_iops_wr_max, 22182024c1dfSBenoît Canet int64_t iops_wr_max, 22192024c1dfSBenoît Canet bool has_iops_size, 222076f4afb4SAlberto Garcia int64_t iops_size, 222176f4afb4SAlberto Garcia bool has_group, 222276f4afb4SAlberto Garcia const char *group, Error **errp) 2223727f005eSZhi Yong Wu { 2224cc0681c4SBenoît Canet ThrottleConfig cfg; 2225727f005eSZhi Yong Wu BlockDriverState *bs; 2226a0e8544cSFam Zheng BlockBackend *blk; 2227b15446fdSStefan Hajnoczi AioContext *aio_context; 2228727f005eSZhi Yong Wu 2229a0e8544cSFam Zheng blk = blk_by_name(device); 2230a0e8544cSFam Zheng if (!blk) { 223175158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 223275158ebbSMarkus Armbruster "Device '%s' not found", device); 223380047da5SLuiz Capitulino return; 2234727f005eSZhi Yong Wu } 22355433c24fSMax Reitz 22365433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 22375433c24fSMax Reitz aio_context_acquire(aio_context); 22385433c24fSMax Reitz 2239a0e8544cSFam Zheng bs = blk_bs(blk); 22405433c24fSMax Reitz if (!bs) { 22415433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 22425433c24fSMax Reitz goto out; 22435433c24fSMax Reitz } 2244727f005eSZhi Yong Wu 2245cc0681c4SBenoît Canet memset(&cfg, 0, sizeof(cfg)); 2246cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2247cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2248cc0681c4SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2249727f005eSZhi Yong Wu 2250cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2251cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2252cc0681c4SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2253cc0681c4SBenoît Canet 22543e9fab69SBenoît Canet if (has_bps_max) { 22553e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 22563e9fab69SBenoît Canet } 22573e9fab69SBenoît Canet if (has_bps_rd_max) { 22583e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 22593e9fab69SBenoît Canet } 22603e9fab69SBenoît Canet if (has_bps_wr_max) { 22613e9fab69SBenoît Canet cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 22623e9fab69SBenoît Canet } 22633e9fab69SBenoît Canet if (has_iops_max) { 22643e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 22653e9fab69SBenoît Canet } 22663e9fab69SBenoît Canet if (has_iops_rd_max) { 22673e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 22683e9fab69SBenoît Canet } 22693e9fab69SBenoît Canet if (has_iops_wr_max) { 22703e9fab69SBenoît Canet cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 22713e9fab69SBenoît Canet } 2272cc0681c4SBenoît Canet 22732024c1dfSBenoît Canet if (has_iops_size) { 22742024c1dfSBenoît Canet cfg.op_size = iops_size; 22752024c1dfSBenoît Canet } 2276cc0681c4SBenoît Canet 2277cc0681c4SBenoît Canet if (!check_throttle_config(&cfg, errp)) { 22785433c24fSMax Reitz goto out; 2279727f005eSZhi Yong Wu } 2280727f005eSZhi Yong Wu 228176f4afb4SAlberto Garcia if (throttle_enabled(&cfg)) { 228276f4afb4SAlberto Garcia /* Enable I/O limits if they're not enabled yet, otherwise 228376f4afb4SAlberto Garcia * just update the throttling group. */ 228476f4afb4SAlberto Garcia if (!bs->io_limits_enabled) { 228576f4afb4SAlberto Garcia bdrv_io_limits_enable(bs, has_group ? group : device); 228676f4afb4SAlberto Garcia } else if (has_group) { 228776f4afb4SAlberto Garcia bdrv_io_limits_update_group(bs, group); 2288727f005eSZhi Yong Wu } 228976f4afb4SAlberto Garcia /* Set the new throttling configuration */ 2290cc0681c4SBenoît Canet bdrv_set_io_limits(bs, &cfg); 229176f4afb4SAlberto Garcia } else if (bs->io_limits_enabled) { 229276f4afb4SAlberto Garcia /* If all throttling settings are set to 0, disable I/O limits */ 229376f4afb4SAlberto Garcia bdrv_io_limits_disable(bs); 2294727f005eSZhi Yong Wu } 2295b15446fdSStefan Hajnoczi 22965433c24fSMax Reitz out: 2297b15446fdSStefan Hajnoczi aio_context_release(aio_context); 2298727f005eSZhi Yong Wu } 2299727f005eSZhi Yong Wu 2300341ebc2fSJohn Snow void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2301341ebc2fSJohn Snow bool has_granularity, uint32_t granularity, 2302341ebc2fSJohn Snow Error **errp) 2303341ebc2fSJohn Snow { 2304341ebc2fSJohn Snow AioContext *aio_context; 2305341ebc2fSJohn Snow BlockDriverState *bs; 2306341ebc2fSJohn Snow 2307341ebc2fSJohn Snow if (!name || name[0] == '\0') { 2308341ebc2fSJohn Snow error_setg(errp, "Bitmap name cannot be empty"); 2309341ebc2fSJohn Snow return; 2310341ebc2fSJohn Snow } 2311341ebc2fSJohn Snow 2312341ebc2fSJohn Snow bs = bdrv_lookup_bs(node, node, errp); 2313341ebc2fSJohn Snow if (!bs) { 2314341ebc2fSJohn Snow return; 2315341ebc2fSJohn Snow } 2316341ebc2fSJohn Snow 2317341ebc2fSJohn Snow aio_context = bdrv_get_aio_context(bs); 2318341ebc2fSJohn Snow aio_context_acquire(aio_context); 2319341ebc2fSJohn Snow 2320341ebc2fSJohn Snow if (has_granularity) { 2321341ebc2fSJohn Snow if (granularity < 512 || !is_power_of_2(granularity)) { 2322341ebc2fSJohn Snow error_setg(errp, "Granularity must be power of 2 " 2323341ebc2fSJohn Snow "and at least 512"); 2324341ebc2fSJohn Snow goto out; 2325341ebc2fSJohn Snow } 2326341ebc2fSJohn Snow } else { 2327341ebc2fSJohn Snow /* Default to cluster size, if available: */ 2328341ebc2fSJohn Snow granularity = bdrv_get_default_bitmap_granularity(bs); 2329341ebc2fSJohn Snow } 2330341ebc2fSJohn Snow 2331341ebc2fSJohn Snow bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2332341ebc2fSJohn Snow 2333341ebc2fSJohn Snow out: 2334341ebc2fSJohn Snow aio_context_release(aio_context); 2335341ebc2fSJohn Snow } 2336341ebc2fSJohn Snow 2337341ebc2fSJohn Snow void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2338341ebc2fSJohn Snow Error **errp) 2339341ebc2fSJohn Snow { 2340341ebc2fSJohn Snow AioContext *aio_context; 2341341ebc2fSJohn Snow BlockDriverState *bs; 2342341ebc2fSJohn Snow BdrvDirtyBitmap *bitmap; 2343341ebc2fSJohn Snow 2344341ebc2fSJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2345341ebc2fSJohn Snow if (!bitmap || !bs) { 2346341ebc2fSJohn Snow return; 2347341ebc2fSJohn Snow } 2348341ebc2fSJohn Snow 23499bd2b08fSJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 23509bd2b08fSJohn Snow error_setg(errp, 23519bd2b08fSJohn Snow "Bitmap '%s' is currently frozen and cannot be removed", 23529bd2b08fSJohn Snow name); 23539bd2b08fSJohn Snow goto out; 23549bd2b08fSJohn Snow } 235520dca810SJohn Snow bdrv_dirty_bitmap_make_anon(bitmap); 2356341ebc2fSJohn Snow bdrv_release_dirty_bitmap(bs, bitmap); 2357341ebc2fSJohn Snow 23589bd2b08fSJohn Snow out: 2359341ebc2fSJohn Snow aio_context_release(aio_context); 2360341ebc2fSJohn Snow } 2361341ebc2fSJohn Snow 2362e74e6b78SJohn Snow /** 2363e74e6b78SJohn Snow * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2364e74e6b78SJohn Snow * immediately after a full backup operation. 2365e74e6b78SJohn Snow */ 2366e74e6b78SJohn Snow void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2367e74e6b78SJohn Snow Error **errp) 2368e74e6b78SJohn Snow { 2369e74e6b78SJohn Snow AioContext *aio_context; 2370e74e6b78SJohn Snow BdrvDirtyBitmap *bitmap; 2371e74e6b78SJohn Snow BlockDriverState *bs; 2372e74e6b78SJohn Snow 2373e74e6b78SJohn Snow bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2374e74e6b78SJohn Snow if (!bitmap || !bs) { 2375e74e6b78SJohn Snow return; 2376e74e6b78SJohn Snow } 2377e74e6b78SJohn Snow 2378e74e6b78SJohn Snow if (bdrv_dirty_bitmap_frozen(bitmap)) { 2379e74e6b78SJohn Snow error_setg(errp, 2380e74e6b78SJohn Snow "Bitmap '%s' is currently frozen and cannot be modified", 2381e74e6b78SJohn Snow name); 2382e74e6b78SJohn Snow goto out; 2383e74e6b78SJohn Snow } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2384e74e6b78SJohn Snow error_setg(errp, 2385e74e6b78SJohn Snow "Bitmap '%s' is currently disabled and cannot be cleared", 2386e74e6b78SJohn Snow name); 2387e74e6b78SJohn Snow goto out; 2388e74e6b78SJohn Snow } 2389e74e6b78SJohn Snow 2390e74e6b78SJohn Snow bdrv_clear_dirty_bitmap(bitmap); 2391e74e6b78SJohn Snow 2392e74e6b78SJohn Snow out: 2393e74e6b78SJohn Snow aio_context_release(aio_context); 2394e74e6b78SJohn Snow } 2395e74e6b78SJohn Snow 2396072ebe6bSMarkus Armbruster void hmp_drive_del(Monitor *mon, const QDict *qdict) 23979063f814SRyan Harper { 23989063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 23997e7d56d9SMarkus Armbruster BlockBackend *blk; 24009063f814SRyan Harper BlockDriverState *bs; 24018ad4202bSStefan Hajnoczi AioContext *aio_context; 24023718d8abSFam Zheng Error *local_err = NULL; 24039063f814SRyan Harper 24047e7d56d9SMarkus Armbruster blk = blk_by_name(id); 24057e7d56d9SMarkus Armbruster if (!blk) { 2406b1422f20SMarkus Armbruster error_report("Device '%s' not found", id); 2407072ebe6bSMarkus Armbruster return; 24089063f814SRyan Harper } 24098ad4202bSStefan Hajnoczi 241026f8b3a8SMarkus Armbruster if (!blk_legacy_dinfo(blk)) { 241148f364ddSMarkus Armbruster error_report("Deleting device added with blockdev-add" 241248f364ddSMarkus Armbruster " is not supported"); 2413072ebe6bSMarkus Armbruster return; 241448f364ddSMarkus Armbruster } 241548f364ddSMarkus Armbruster 24165433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 24178ad4202bSStefan Hajnoczi aio_context_acquire(aio_context); 24188ad4202bSStefan Hajnoczi 24195433c24fSMax Reitz bs = blk_bs(blk); 24205433c24fSMax Reitz if (bs) { 24213718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2422565f65d2SMarkus Armbruster error_report_err(local_err); 24238ad4202bSStefan Hajnoczi aio_context_release(aio_context); 2424072ebe6bSMarkus Armbruster return; 24258591675fSMarcelo Tosatti } 24269063f814SRyan Harper 24279063f814SRyan Harper bdrv_close(bs); 24285433c24fSMax Reitz } 24299063f814SRyan Harper 2430fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 2431d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 2432d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 2433d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 2434d22b2f41SRyan Harper */ 2435a7f53e26SMarkus Armbruster if (blk_get_attached_dev(blk)) { 24363e5a50d6SMarkus Armbruster blk_hide_on_behalf_of_hmp_drive_del(blk); 2437293c51a6SStefan Hajnoczi /* Further I/O must not pause the guest */ 2438373340b2SMax Reitz blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2439293c51a6SStefan Hajnoczi BLOCKDEV_ON_ERROR_REPORT); 2440d22b2f41SRyan Harper } else { 2441b9fe8a7aSMarkus Armbruster blk_unref(blk); 2442d22b2f41SRyan Harper } 24439063f814SRyan Harper 24448ad4202bSStefan Hajnoczi aio_context_release(aio_context); 24459063f814SRyan Harper } 24466d4a2b3aSChristoph Hellwig 24473b1dbd11SBenoît Canet void qmp_block_resize(bool has_device, const char *device, 24483b1dbd11SBenoît Canet bool has_node_name, const char *node_name, 24493b1dbd11SBenoît Canet int64_t size, Error **errp) 24506d4a2b3aSChristoph Hellwig { 24513b1dbd11SBenoît Canet Error *local_err = NULL; 24526d4a2b3aSChristoph Hellwig BlockDriverState *bs; 2453927e0e76SStefan Hajnoczi AioContext *aio_context; 24548732901eSKevin Wolf int ret; 24556d4a2b3aSChristoph Hellwig 24563b1dbd11SBenoît Canet bs = bdrv_lookup_bs(has_device ? device : NULL, 24573b1dbd11SBenoît Canet has_node_name ? node_name : NULL, 24583b1dbd11SBenoît Canet &local_err); 245984d18f06SMarkus Armbruster if (local_err) { 24603b1dbd11SBenoît Canet error_propagate(errp, local_err); 24613b1dbd11SBenoît Canet return; 24623b1dbd11SBenoît Canet } 24633b1dbd11SBenoît Canet 2464927e0e76SStefan Hajnoczi aio_context = bdrv_get_aio_context(bs); 2465927e0e76SStefan Hajnoczi aio_context_acquire(aio_context); 2466927e0e76SStefan Hajnoczi 24673b1dbd11SBenoît Canet if (!bdrv_is_first_non_filter(bs)) { 2468c6bd8c70SMarkus Armbruster error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2469927e0e76SStefan Hajnoczi goto out; 24706d4a2b3aSChristoph Hellwig } 24716d4a2b3aSChristoph Hellwig 24726d4a2b3aSChristoph Hellwig if (size < 0) { 2473c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2474927e0e76SStefan Hajnoczi goto out; 24756d4a2b3aSChristoph Hellwig } 24766d4a2b3aSChristoph Hellwig 24779c75e168SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2478c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2479927e0e76SStefan Hajnoczi goto out; 24809c75e168SJeff Cody } 24819c75e168SJeff Cody 248292b7a08dSPeter Lieven /* complete all in-flight operations before resizing the device */ 248392b7a08dSPeter Lieven bdrv_drain_all(); 248492b7a08dSPeter Lieven 24858732901eSKevin Wolf ret = bdrv_truncate(bs, size); 24868732901eSKevin Wolf switch (ret) { 2487939a1cc3SStefan Hajnoczi case 0: 2488939a1cc3SStefan Hajnoczi break; 2489939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 2490c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2491939a1cc3SStefan Hajnoczi break; 2492939a1cc3SStefan Hajnoczi case -ENOTSUP: 2493c6bd8c70SMarkus Armbruster error_setg(errp, QERR_UNSUPPORTED); 2494939a1cc3SStefan Hajnoczi break; 2495939a1cc3SStefan Hajnoczi case -EACCES: 249681e5f78aSAlberto Garcia error_setg(errp, "Device '%s' is read only", device); 2497939a1cc3SStefan Hajnoczi break; 2498939a1cc3SStefan Hajnoczi case -EBUSY: 2499c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_IN_USE, device); 2500939a1cc3SStefan Hajnoczi break; 2501939a1cc3SStefan Hajnoczi default: 25028732901eSKevin Wolf error_setg_errno(errp, -ret, "Could not resize"); 2503939a1cc3SStefan Hajnoczi break; 25046d4a2b3aSChristoph Hellwig } 2505927e0e76SStefan Hajnoczi 2506927e0e76SStefan Hajnoczi out: 2507927e0e76SStefan Hajnoczi aio_context_release(aio_context); 25086d4a2b3aSChristoph Hellwig } 250912bd451fSStefan Hajnoczi 25109abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 251112bd451fSStefan Hajnoczi { 2512723c5d93SStefan Hajnoczi /* Note that this function may be executed from another AioContext besides 2513723c5d93SStefan Hajnoczi * the QEMU main loop. If you need to access anything that assumes the 2514723c5d93SStefan Hajnoczi * QEMU global mutex, use a BH or introduce a mutex. 2515723c5d93SStefan Hajnoczi */ 2516723c5d93SStefan Hajnoczi 251712bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 2518bcada37bSWenchao Xia const char *msg = NULL; 251912bd451fSStefan Hajnoczi 25209abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 252112bd451fSStefan Hajnoczi 252212bd451fSStefan Hajnoczi assert(bs->job); 2523bcada37bSWenchao Xia 252412bd451fSStefan Hajnoczi if (ret < 0) { 2525bcada37bSWenchao Xia msg = strerror(-ret); 252612bd451fSStefan Hajnoczi } 252712bd451fSStefan Hajnoczi 2528370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 2529bcada37bSWenchao Xia block_job_event_cancelled(bs->job); 2530370521a1SStefan Hajnoczi } else { 2531bcada37bSWenchao Xia block_job_event_completed(bs->job, msg); 2532370521a1SStefan Hajnoczi } 2533aa398a5cSStefan Hajnoczi 2534fa510ebfSFam Zheng bdrv_put_ref_bh_schedule(bs); 253512bd451fSStefan Hajnoczi } 253612bd451fSStefan Hajnoczi 253713d8cc51SJeff Cody void qmp_block_stream(const char *device, 253813d8cc51SJeff Cody bool has_base, const char *base, 253913d8cc51SJeff Cody bool has_backing_file, const char *backing_file, 254013d8cc51SJeff Cody bool has_speed, int64_t speed, 25411d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 25421d809098SPaolo Bonzini Error **errp) 254312bd451fSStefan Hajnoczi { 2544a0e8544cSFam Zheng BlockBackend *blk; 254512bd451fSStefan Hajnoczi BlockDriverState *bs; 2546c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 2547f3e69bebSStefan Hajnoczi AioContext *aio_context; 2548fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 254913d8cc51SJeff Cody const char *base_name = NULL; 255012bd451fSStefan Hajnoczi 25511d809098SPaolo Bonzini if (!has_on_error) { 25521d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 25531d809098SPaolo Bonzini } 25541d809098SPaolo Bonzini 2555a0e8544cSFam Zheng blk = blk_by_name(device); 2556a0e8544cSFam Zheng if (!blk) { 255775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 255875158ebbSMarkus Armbruster "Device '%s' not found", device); 255912bd451fSStefan Hajnoczi return; 256012bd451fSStefan Hajnoczi } 256112bd451fSStefan Hajnoczi 25625433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2563f3e69bebSStefan Hajnoczi aio_context_acquire(aio_context); 2564f3e69bebSStefan Hajnoczi 25655433c24fSMax Reitz if (!blk_is_available(blk)) { 25665433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 25675433c24fSMax Reitz goto out; 25685433c24fSMax Reitz } 25695433c24fSMax Reitz bs = blk_bs(blk); 25705433c24fSMax Reitz 2571628ff683SFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2572f3e69bebSStefan Hajnoczi goto out; 2573628ff683SFam Zheng } 2574628ff683SFam Zheng 257513d8cc51SJeff Cody if (has_base) { 2576c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 2577c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 2578c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base); 2579f3e69bebSStefan Hajnoczi goto out; 258012bd451fSStefan Hajnoczi } 2581f3e69bebSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 258213d8cc51SJeff Cody base_name = base; 2583c8c3080fSMarcelo Tosatti } 258412bd451fSStefan Hajnoczi 258513d8cc51SJeff Cody /* if we are streaming the entire chain, the result will have no backing 258613d8cc51SJeff Cody * file, and specifying one is therefore an error */ 258713d8cc51SJeff Cody if (base_bs == NULL && has_backing_file) { 258813d8cc51SJeff Cody error_setg(errp, "backing file specified, but streaming the " 258913d8cc51SJeff Cody "entire chain"); 2590f3e69bebSStefan Hajnoczi goto out; 259113d8cc51SJeff Cody } 259213d8cc51SJeff Cody 259313d8cc51SJeff Cody /* backing_file string overrides base bs filename */ 259413d8cc51SJeff Cody base_name = has_backing_file ? backing_file : base_name; 259513d8cc51SJeff Cody 259613d8cc51SJeff Cody stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 25971d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 259884d18f06SMarkus Armbruster if (local_err) { 2599fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 2600f3e69bebSStefan Hajnoczi goto out; 260112bd451fSStefan Hajnoczi } 260212bd451fSStefan Hajnoczi 260312bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 2604f3e69bebSStefan Hajnoczi 2605f3e69bebSStefan Hajnoczi out: 2606f3e69bebSStefan Hajnoczi aio_context_release(aio_context); 260712bd451fSStefan Hajnoczi } 26082d47c6e9SStefan Hajnoczi 2609ed61fc10SJeff Cody void qmp_block_commit(const char *device, 26107676e2c5SJeff Cody bool has_base, const char *base, 26117676e2c5SJeff Cody bool has_top, const char *top, 261254e26900SJeff Cody bool has_backing_file, const char *backing_file, 2613ed61fc10SJeff Cody bool has_speed, int64_t speed, 2614ed61fc10SJeff Cody Error **errp) 2615ed61fc10SJeff Cody { 2616a0e8544cSFam Zheng BlockBackend *blk; 2617ed61fc10SJeff Cody BlockDriverState *bs; 2618ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 26199e85cd5cSStefan Hajnoczi AioContext *aio_context; 2620ed61fc10SJeff Cody Error *local_err = NULL; 2621ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 2622ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 2623ed61fc10SJeff Cody */ 262492aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2625ed61fc10SJeff Cody 262654504663SMax Reitz if (!has_speed) { 262754504663SMax Reitz speed = 0; 262854504663SMax Reitz } 262954504663SMax Reitz 26307676e2c5SJeff Cody /* Important Note: 26317676e2c5SJeff Cody * libvirt relies on the DeviceNotFound error class in order to probe for 26327676e2c5SJeff Cody * live commit feature versions; for this to work, we must make sure to 26337676e2c5SJeff Cody * perform the device lookup before any generic errors that may occur in a 26347676e2c5SJeff Cody * scenario in which all optional arguments are omitted. */ 2635a0e8544cSFam Zheng blk = blk_by_name(device); 2636a0e8544cSFam Zheng if (!blk) { 263775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 263875158ebbSMarkus Armbruster "Device '%s' not found", device); 2639ed61fc10SJeff Cody return; 2640ed61fc10SJeff Cody } 2641ed61fc10SJeff Cody 26425433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 26439e85cd5cSStefan Hajnoczi aio_context_acquire(aio_context); 26449e85cd5cSStefan Hajnoczi 26455433c24fSMax Reitz if (!blk_is_available(blk)) { 26465433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 26475433c24fSMax Reitz goto out; 26485433c24fSMax Reitz } 26495433c24fSMax Reitz bs = blk_bs(blk); 26505433c24fSMax Reitz 2651bb00021dSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 26529e85cd5cSStefan Hajnoczi goto out; 2653628ff683SFam Zheng } 2654628ff683SFam Zheng 2655ed61fc10SJeff Cody /* default top_bs is the active layer */ 2656ed61fc10SJeff Cody top_bs = bs; 2657ed61fc10SJeff Cody 26587676e2c5SJeff Cody if (has_top && top) { 2659ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 2660ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 2661ed61fc10SJeff Cody } 2662ed61fc10SJeff Cody } 2663ed61fc10SJeff Cody 2664ed61fc10SJeff Cody if (top_bs == NULL) { 2665ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 26669e85cd5cSStefan Hajnoczi goto out; 2667ed61fc10SJeff Cody } 2668ed61fc10SJeff Cody 26699e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(top_bs) == aio_context); 26709e85cd5cSStefan Hajnoczi 2671d5208c45SJeff Cody if (has_base && base) { 2672d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 2673d5208c45SJeff Cody } else { 2674d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 2675d5208c45SJeff Cody } 2676d5208c45SJeff Cody 2677d5208c45SJeff Cody if (base_bs == NULL) { 2678c6bd8c70SMarkus Armbruster error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 26799e85cd5cSStefan Hajnoczi goto out; 2680d5208c45SJeff Cody } 2681d5208c45SJeff Cody 26829e85cd5cSStefan Hajnoczi assert(bdrv_get_aio_context(base_bs) == aio_context); 26839e85cd5cSStefan Hajnoczi 2684bb00021dSFam Zheng if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 2685bb00021dSFam Zheng goto out; 2686bb00021dSFam Zheng } 2687bb00021dSFam Zheng 26887676e2c5SJeff Cody /* Do not allow attempts to commit an image into itself */ 26897676e2c5SJeff Cody if (top_bs == base_bs) { 26907676e2c5SJeff Cody error_setg(errp, "cannot commit an image into itself"); 26919e85cd5cSStefan Hajnoczi goto out; 26927676e2c5SJeff Cody } 26937676e2c5SJeff Cody 269420a63d2cSFam Zheng if (top_bs == bs) { 269554e26900SJeff Cody if (has_backing_file) { 269654e26900SJeff Cody error_setg(errp, "'backing-file' specified," 269754e26900SJeff Cody " but 'top' is the active layer"); 26989e85cd5cSStefan Hajnoczi goto out; 269954e26900SJeff Cody } 270020a63d2cSFam Zheng commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 270120a63d2cSFam Zheng bs, &local_err); 270220a63d2cSFam Zheng } else { 2703ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 270454e26900SJeff Cody has_backing_file ? backing_file : NULL, &local_err); 270520a63d2cSFam Zheng } 2706ed61fc10SJeff Cody if (local_err != NULL) { 2707ed61fc10SJeff Cody error_propagate(errp, local_err); 27089e85cd5cSStefan Hajnoczi goto out; 2709ed61fc10SJeff Cody } 27109e85cd5cSStefan Hajnoczi 27119e85cd5cSStefan Hajnoczi out: 27129e85cd5cSStefan Hajnoczi aio_context_release(aio_context); 2713ed61fc10SJeff Cody } 2714ed61fc10SJeff Cody 271599a9addfSStefan Hajnoczi void qmp_drive_backup(const char *device, const char *target, 271699a9addfSStefan Hajnoczi bool has_format, const char *format, 2717b53169eaSStefan Hajnoczi enum MirrorSyncMode sync, 271899a9addfSStefan Hajnoczi bool has_mode, enum NewImageMode mode, 271999a9addfSStefan Hajnoczi bool has_speed, int64_t speed, 2720d58d8453SJohn Snow bool has_bitmap, const char *bitmap, 272199a9addfSStefan Hajnoczi bool has_on_source_error, BlockdevOnError on_source_error, 272299a9addfSStefan Hajnoczi bool has_on_target_error, BlockdevOnError on_target_error, 272399a9addfSStefan Hajnoczi Error **errp) 272499a9addfSStefan Hajnoczi { 2725a0e8544cSFam Zheng BlockBackend *blk; 272699a9addfSStefan Hajnoczi BlockDriverState *bs; 272799a9addfSStefan Hajnoczi BlockDriverState *target_bs; 2728fc5d3f84SIan Main BlockDriverState *source = NULL; 2729d58d8453SJohn Snow BdrvDirtyBitmap *bmap = NULL; 2730761731b1SStefan Hajnoczi AioContext *aio_context; 2731e6641719SMax Reitz QDict *options = NULL; 273299a9addfSStefan Hajnoczi Error *local_err = NULL; 273399a9addfSStefan Hajnoczi int flags; 273499a9addfSStefan Hajnoczi int64_t size; 273599a9addfSStefan Hajnoczi int ret; 273699a9addfSStefan Hajnoczi 273799a9addfSStefan Hajnoczi if (!has_speed) { 273899a9addfSStefan Hajnoczi speed = 0; 273999a9addfSStefan Hajnoczi } 274099a9addfSStefan Hajnoczi if (!has_on_source_error) { 274199a9addfSStefan Hajnoczi on_source_error = BLOCKDEV_ON_ERROR_REPORT; 274299a9addfSStefan Hajnoczi } 274399a9addfSStefan Hajnoczi if (!has_on_target_error) { 274499a9addfSStefan Hajnoczi on_target_error = BLOCKDEV_ON_ERROR_REPORT; 274599a9addfSStefan Hajnoczi } 274699a9addfSStefan Hajnoczi if (!has_mode) { 274799a9addfSStefan Hajnoczi mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 274899a9addfSStefan Hajnoczi } 274999a9addfSStefan Hajnoczi 2750a0e8544cSFam Zheng blk = blk_by_name(device); 2751a0e8544cSFam Zheng if (!blk) { 275275158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 275375158ebbSMarkus Armbruster "Device '%s' not found", device); 275499a9addfSStefan Hajnoczi return; 275599a9addfSStefan Hajnoczi } 275699a9addfSStefan Hajnoczi 27575433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2758761731b1SStefan Hajnoczi aio_context_acquire(aio_context); 2759761731b1SStefan Hajnoczi 2760c29c1dd3SFam Zheng /* Although backup_run has this check too, we need to use bs->drv below, so 2761c29c1dd3SFam Zheng * do an early check redundantly. */ 27625433c24fSMax Reitz if (!blk_is_available(blk)) { 2763c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2764761731b1SStefan Hajnoczi goto out; 276599a9addfSStefan Hajnoczi } 27665433c24fSMax Reitz bs = blk_bs(blk); 276799a9addfSStefan Hajnoczi 276899a9addfSStefan Hajnoczi if (!has_format) { 276999a9addfSStefan Hajnoczi format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 277099a9addfSStefan Hajnoczi } 277199a9addfSStefan Hajnoczi 2772c29c1dd3SFam Zheng /* Early check to avoid creating target */ 27733718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 2774761731b1SStefan Hajnoczi goto out; 277599a9addfSStefan Hajnoczi } 277699a9addfSStefan Hajnoczi 277799a9addfSStefan Hajnoczi flags = bs->open_flags | BDRV_O_RDWR; 277899a9addfSStefan Hajnoczi 2779fc5d3f84SIan Main /* See if we have a backing HD we can use to create our new image 2780fc5d3f84SIan Main * on top of. */ 2781fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_TOP) { 2782760e0063SKevin Wolf source = backing_bs(bs); 2783fc5d3f84SIan Main if (!source) { 2784fc5d3f84SIan Main sync = MIRROR_SYNC_MODE_FULL; 2785fc5d3f84SIan Main } 2786fc5d3f84SIan Main } 2787fc5d3f84SIan Main if (sync == MIRROR_SYNC_MODE_NONE) { 2788fc5d3f84SIan Main source = bs; 2789fc5d3f84SIan Main } 2790fc5d3f84SIan Main 279199a9addfSStefan Hajnoczi size = bdrv_getlength(bs); 279299a9addfSStefan Hajnoczi if (size < 0) { 279399a9addfSStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 2794761731b1SStefan Hajnoczi goto out; 279599a9addfSStefan Hajnoczi } 279699a9addfSStefan Hajnoczi 279799a9addfSStefan Hajnoczi if (mode != NEW_IMAGE_MODE_EXISTING) { 2798e6641719SMax Reitz assert(format); 2799fc5d3f84SIan Main if (source) { 2800fc5d3f84SIan Main bdrv_img_create(target, format, source->filename, 2801fc5d3f84SIan Main source->drv->format_name, NULL, 2802fc5d3f84SIan Main size, flags, &local_err, false); 2803fc5d3f84SIan Main } else { 2804fc5d3f84SIan Main bdrv_img_create(target, format, NULL, NULL, NULL, 2805fc5d3f84SIan Main size, flags, &local_err, false); 2806fc5d3f84SIan Main } 280799a9addfSStefan Hajnoczi } 280899a9addfSStefan Hajnoczi 280984d18f06SMarkus Armbruster if (local_err) { 281099a9addfSStefan Hajnoczi error_propagate(errp, local_err); 2811761731b1SStefan Hajnoczi goto out; 281299a9addfSStefan Hajnoczi } 281399a9addfSStefan Hajnoczi 2814e6641719SMax Reitz if (format) { 2815e6641719SMax Reitz options = qdict_new(); 2816e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 2817e6641719SMax Reitz } 2818e6641719SMax Reitz 2819f67503e5SMax Reitz target_bs = NULL; 28206ebf9aa2SMax Reitz ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 282199a9addfSStefan Hajnoczi if (ret < 0) { 282234b5d2c6SMax Reitz error_propagate(errp, local_err); 2823761731b1SStefan Hajnoczi goto out; 282499a9addfSStefan Hajnoczi } 282599a9addfSStefan Hajnoczi 2826761731b1SStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 2827761731b1SStefan Hajnoczi 2828d58d8453SJohn Snow if (has_bitmap) { 2829d58d8453SJohn Snow bmap = bdrv_find_dirty_bitmap(bs, bitmap); 2830d58d8453SJohn Snow if (!bmap) { 2831d58d8453SJohn Snow error_setg(errp, "Bitmap '%s' could not be found", bitmap); 2832d58d8453SJohn Snow goto out; 2833d58d8453SJohn Snow } 2834d58d8453SJohn Snow } 2835d58d8453SJohn Snow 2836d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, bmap, 2837d58d8453SJohn Snow on_source_error, on_target_error, 283899a9addfSStefan Hajnoczi block_job_cb, bs, &local_err); 283999a9addfSStefan Hajnoczi if (local_err != NULL) { 28404f6fd349SFam Zheng bdrv_unref(target_bs); 284199a9addfSStefan Hajnoczi error_propagate(errp, local_err); 2842761731b1SStefan Hajnoczi goto out; 284399a9addfSStefan Hajnoczi } 2844761731b1SStefan Hajnoczi 2845761731b1SStefan Hajnoczi out: 2846761731b1SStefan Hajnoczi aio_context_release(aio_context); 284799a9addfSStefan Hajnoczi } 284899a9addfSStefan Hajnoczi 2849c13163fbSBenoît Canet BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 2850c13163fbSBenoît Canet { 2851d5a8ee60SAlberto Garcia return bdrv_named_nodes_list(errp); 2852c13163fbSBenoît Canet } 2853c13163fbSBenoît Canet 2854c29c1dd3SFam Zheng void qmp_blockdev_backup(const char *device, const char *target, 2855c29c1dd3SFam Zheng enum MirrorSyncMode sync, 2856c29c1dd3SFam Zheng bool has_speed, int64_t speed, 2857c29c1dd3SFam Zheng bool has_on_source_error, 2858c29c1dd3SFam Zheng BlockdevOnError on_source_error, 2859c29c1dd3SFam Zheng bool has_on_target_error, 2860c29c1dd3SFam Zheng BlockdevOnError on_target_error, 2861c29c1dd3SFam Zheng Error **errp) 2862c29c1dd3SFam Zheng { 28635433c24fSMax Reitz BlockBackend *blk, *target_blk; 2864c29c1dd3SFam Zheng BlockDriverState *bs; 2865c29c1dd3SFam Zheng BlockDriverState *target_bs; 2866c29c1dd3SFam Zheng Error *local_err = NULL; 2867c29c1dd3SFam Zheng AioContext *aio_context; 2868c29c1dd3SFam Zheng 2869c29c1dd3SFam Zheng if (!has_speed) { 2870c29c1dd3SFam Zheng speed = 0; 2871c29c1dd3SFam Zheng } 2872c29c1dd3SFam Zheng if (!has_on_source_error) { 2873c29c1dd3SFam Zheng on_source_error = BLOCKDEV_ON_ERROR_REPORT; 2874c29c1dd3SFam Zheng } 2875c29c1dd3SFam Zheng if (!has_on_target_error) { 2876c29c1dd3SFam Zheng on_target_error = BLOCKDEV_ON_ERROR_REPORT; 2877c29c1dd3SFam Zheng } 2878c29c1dd3SFam Zheng 2879a0e8544cSFam Zheng blk = blk_by_name(device); 2880a0e8544cSFam Zheng if (!blk) { 28815b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", device); 2882c29c1dd3SFam Zheng return; 2883c29c1dd3SFam Zheng } 2884c29c1dd3SFam Zheng 28855433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 2886c29c1dd3SFam Zheng aio_context_acquire(aio_context); 2887c29c1dd3SFam Zheng 28885433c24fSMax Reitz if (!blk_is_available(blk)) { 28895433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 28905433c24fSMax Reitz goto out; 28915433c24fSMax Reitz } 28925433c24fSMax Reitz bs = blk_bs(blk); 28935433c24fSMax Reitz 28945433c24fSMax Reitz target_blk = blk_by_name(target); 28955433c24fSMax Reitz if (!target_blk) { 28965b347c54SMarkus Armbruster error_setg(errp, "Device '%s' not found", target); 2897c29c1dd3SFam Zheng goto out; 2898c29c1dd3SFam Zheng } 28995433c24fSMax Reitz 29005433c24fSMax Reitz if (!blk_is_available(target_blk)) { 29015433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", target); 29025433c24fSMax Reitz goto out; 29035433c24fSMax Reitz } 29045433c24fSMax Reitz target_bs = blk_bs(target_blk); 2905c29c1dd3SFam Zheng 2906c29c1dd3SFam Zheng bdrv_ref(target_bs); 2907c29c1dd3SFam Zheng bdrv_set_aio_context(target_bs, aio_context); 2908d58d8453SJohn Snow backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 2909d58d8453SJohn Snow on_target_error, block_job_cb, bs, &local_err); 2910c29c1dd3SFam Zheng if (local_err != NULL) { 2911c29c1dd3SFam Zheng bdrv_unref(target_bs); 2912c29c1dd3SFam Zheng error_propagate(errp, local_err); 2913c29c1dd3SFam Zheng } 2914c29c1dd3SFam Zheng out: 2915c29c1dd3SFam Zheng aio_context_release(aio_context); 2916c29c1dd3SFam Zheng } 2917c29c1dd3SFam Zheng 2918d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target, 2919d9b902dbSPaolo Bonzini bool has_format, const char *format, 29204c828dc6SBenoît Canet bool has_node_name, const char *node_name, 292109158f00SBenoît Canet bool has_replaces, const char *replaces, 2922d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 2923d9b902dbSPaolo Bonzini bool has_mode, enum NewImageMode mode, 2924b952b558SPaolo Bonzini bool has_speed, int64_t speed, 2925eee13dfeSPaolo Bonzini bool has_granularity, uint32_t granularity, 292608e4ed6cSPaolo Bonzini bool has_buf_size, int64_t buf_size, 2927b952b558SPaolo Bonzini bool has_on_source_error, BlockdevOnError on_source_error, 2928b952b558SPaolo Bonzini bool has_on_target_error, BlockdevOnError on_target_error, 29290fc9f8eaSFam Zheng bool has_unmap, bool unmap, 2930b952b558SPaolo Bonzini Error **errp) 2931d9b902dbSPaolo Bonzini { 2932a0e8544cSFam Zheng BlockBackend *blk; 2933d9b902dbSPaolo Bonzini BlockDriverState *bs; 2934d9b902dbSPaolo Bonzini BlockDriverState *source, *target_bs; 29355a7e7a0bSStefan Hajnoczi AioContext *aio_context; 2936d9b902dbSPaolo Bonzini Error *local_err = NULL; 2937e6641719SMax Reitz QDict *options; 2938d9b902dbSPaolo Bonzini int flags; 2939ac3c5d83SStefan Hajnoczi int64_t size; 2940d9b902dbSPaolo Bonzini int ret; 2941d9b902dbSPaolo Bonzini 2942d9b902dbSPaolo Bonzini if (!has_speed) { 2943d9b902dbSPaolo Bonzini speed = 0; 2944d9b902dbSPaolo Bonzini } 2945b952b558SPaolo Bonzini if (!has_on_source_error) { 2946b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 2947b952b558SPaolo Bonzini } 2948b952b558SPaolo Bonzini if (!has_on_target_error) { 2949b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 2950b952b558SPaolo Bonzini } 2951d9b902dbSPaolo Bonzini if (!has_mode) { 2952d9b902dbSPaolo Bonzini mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 2953d9b902dbSPaolo Bonzini } 2954eee13dfeSPaolo Bonzini if (!has_granularity) { 2955eee13dfeSPaolo Bonzini granularity = 0; 2956eee13dfeSPaolo Bonzini } 295708e4ed6cSPaolo Bonzini if (!has_buf_size) { 295848ac0a4dSWen Congyang buf_size = 0; 295908e4ed6cSPaolo Bonzini } 29600fc9f8eaSFam Zheng if (!has_unmap) { 29610fc9f8eaSFam Zheng unmap = true; 29620fc9f8eaSFam Zheng } 296308e4ed6cSPaolo Bonzini 2964eee13dfeSPaolo Bonzini if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 2965c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 29663cbbe9fdSStefan Hajnoczi "a value in range [512B, 64MB]"); 2967eee13dfeSPaolo Bonzini return; 2968eee13dfeSPaolo Bonzini } 2969eee13dfeSPaolo Bonzini if (granularity & (granularity - 1)) { 2970c6bd8c70SMarkus Armbruster error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 2971c6bd8c70SMarkus Armbruster "power of 2"); 2972eee13dfeSPaolo Bonzini return; 2973eee13dfeSPaolo Bonzini } 2974d9b902dbSPaolo Bonzini 2975a0e8544cSFam Zheng blk = blk_by_name(device); 2976a0e8544cSFam Zheng if (!blk) { 297775158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 297875158ebbSMarkus Armbruster "Device '%s' not found", device); 2979d9b902dbSPaolo Bonzini return; 2980d9b902dbSPaolo Bonzini } 2981d9b902dbSPaolo Bonzini 29825433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 29835a7e7a0bSStefan Hajnoczi aio_context_acquire(aio_context); 29845a7e7a0bSStefan Hajnoczi 29855433c24fSMax Reitz if (!blk_is_available(blk)) { 2986c6bd8c70SMarkus Armbruster error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 29875a7e7a0bSStefan Hajnoczi goto out; 2988d9b902dbSPaolo Bonzini } 29895433c24fSMax Reitz bs = blk_bs(blk); 2990d9b902dbSPaolo Bonzini 2991d9b902dbSPaolo Bonzini if (!has_format) { 2992d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 2993d9b902dbSPaolo Bonzini } 2994d9b902dbSPaolo Bonzini 29953718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { 29965a7e7a0bSStefan Hajnoczi goto out; 2997d9b902dbSPaolo Bonzini } 2998d9b902dbSPaolo Bonzini 2999d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 3000760e0063SKevin Wolf source = backing_bs(bs); 3001d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3002d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 3003d9b902dbSPaolo Bonzini } 3004117e0c82SMax Reitz if (sync == MIRROR_SYNC_MODE_NONE) { 3005117e0c82SMax Reitz source = bs; 3006117e0c82SMax Reitz } 3007d9b902dbSPaolo Bonzini 3008ac3c5d83SStefan Hajnoczi size = bdrv_getlength(bs); 3009ac3c5d83SStefan Hajnoczi if (size < 0) { 3010ac3c5d83SStefan Hajnoczi error_setg_errno(errp, -size, "bdrv_getlength failed"); 30115a7e7a0bSStefan Hajnoczi goto out; 3012ac3c5d83SStefan Hajnoczi } 3013ac3c5d83SStefan Hajnoczi 301409158f00SBenoît Canet if (has_replaces) { 301509158f00SBenoît Canet BlockDriverState *to_replace_bs; 30165a7e7a0bSStefan Hajnoczi AioContext *replace_aio_context; 30175a7e7a0bSStefan Hajnoczi int64_t replace_size; 301809158f00SBenoît Canet 301909158f00SBenoît Canet if (!has_node_name) { 302009158f00SBenoît Canet error_setg(errp, "a node-name must be provided when replacing a" 302109158f00SBenoît Canet " named node of the graph"); 30225a7e7a0bSStefan Hajnoczi goto out; 302309158f00SBenoît Canet } 302409158f00SBenoît Canet 3025e12f3784SWen Congyang to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 302609158f00SBenoît Canet 302709158f00SBenoît Canet if (!to_replace_bs) { 302809158f00SBenoît Canet error_propagate(errp, local_err); 30295a7e7a0bSStefan Hajnoczi goto out; 303009158f00SBenoît Canet } 303109158f00SBenoît Canet 30325a7e7a0bSStefan Hajnoczi replace_aio_context = bdrv_get_aio_context(to_replace_bs); 30335a7e7a0bSStefan Hajnoczi aio_context_acquire(replace_aio_context); 30345a7e7a0bSStefan Hajnoczi replace_size = bdrv_getlength(to_replace_bs); 30355a7e7a0bSStefan Hajnoczi aio_context_release(replace_aio_context); 30365a7e7a0bSStefan Hajnoczi 30375a7e7a0bSStefan Hajnoczi if (size != replace_size) { 303809158f00SBenoît Canet error_setg(errp, "cannot replace image with a mirror image of " 303909158f00SBenoît Canet "different size"); 30405a7e7a0bSStefan Hajnoczi goto out; 304109158f00SBenoît Canet } 304209158f00SBenoît Canet } 304309158f00SBenoît Canet 304414526864SMax Reitz if ((sync == MIRROR_SYNC_MODE_FULL || !source) 304514526864SMax Reitz && mode != NEW_IMAGE_MODE_EXISTING) 304614526864SMax Reitz { 3047d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 3048e6641719SMax Reitz assert(format); 3049cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3050f382d43aSMiroslav Rezanina NULL, NULL, NULL, size, flags, &local_err, false); 3051d9b902dbSPaolo Bonzini } else { 3052d9b902dbSPaolo Bonzini switch (mode) { 3053d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 3054d9b902dbSPaolo Bonzini break; 3055d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3056d9b902dbSPaolo Bonzini /* create new image with backing file */ 3057cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 3058d9b902dbSPaolo Bonzini source->filename, 3059d9b902dbSPaolo Bonzini source->drv->format_name, 3060f382d43aSMiroslav Rezanina NULL, size, flags, &local_err, false); 3061d9b902dbSPaolo Bonzini break; 3062d9b902dbSPaolo Bonzini default: 3063d9b902dbSPaolo Bonzini abort(); 3064d9b902dbSPaolo Bonzini } 3065d9b902dbSPaolo Bonzini } 3066d9b902dbSPaolo Bonzini 306784d18f06SMarkus Armbruster if (local_err) { 3068cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 30695a7e7a0bSStefan Hajnoczi goto out; 3070d9b902dbSPaolo Bonzini } 3071d9b902dbSPaolo Bonzini 30724c828dc6SBenoît Canet options = qdict_new(); 3073e6641719SMax Reitz if (has_node_name) { 30744c828dc6SBenoît Canet qdict_put(options, "node-name", qstring_from_str(node_name)); 30754c828dc6SBenoît Canet } 3076e6641719SMax Reitz if (format) { 3077e6641719SMax Reitz qdict_put(options, "driver", qstring_from_str(format)); 3078e6641719SMax Reitz } 30794c828dc6SBenoît Canet 3080b812f671SPaolo Bonzini /* Mirroring takes care of copy-on-write using the source's backing 3081b812f671SPaolo Bonzini * file. 3082b812f671SPaolo Bonzini */ 3083f67503e5SMax Reitz target_bs = NULL; 30844c828dc6SBenoît Canet ret = bdrv_open(&target_bs, target, NULL, options, 30856ebf9aa2SMax Reitz flags | BDRV_O_NO_BACKING, &local_err); 3086d9b902dbSPaolo Bonzini if (ret < 0) { 308734b5d2c6SMax Reitz error_propagate(errp, local_err); 30885a7e7a0bSStefan Hajnoczi goto out; 3089d9b902dbSPaolo Bonzini } 3090d9b902dbSPaolo Bonzini 30915a7e7a0bSStefan Hajnoczi bdrv_set_aio_context(target_bs, aio_context); 30925a7e7a0bSStefan Hajnoczi 309309158f00SBenoît Canet /* pass the node name to replace to mirror start since it's loose coupling 309409158f00SBenoît Canet * and will allow to check whether the node still exist at mirror completion 309509158f00SBenoît Canet */ 309609158f00SBenoît Canet mirror_start(bs, target_bs, 309709158f00SBenoît Canet has_replaces ? replaces : NULL, 309809158f00SBenoît Canet speed, granularity, buf_size, sync, 3099eee13dfeSPaolo Bonzini on_source_error, on_target_error, 31000fc9f8eaSFam Zheng unmap, 3101b952b558SPaolo Bonzini block_job_cb, bs, &local_err); 3102d9b902dbSPaolo Bonzini if (local_err != NULL) { 31034f6fd349SFam Zheng bdrv_unref(target_bs); 3104d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 31055a7e7a0bSStefan Hajnoczi goto out; 3106d9b902dbSPaolo Bonzini } 31075a7e7a0bSStefan Hajnoczi 31085a7e7a0bSStefan Hajnoczi out: 31095a7e7a0bSStefan Hajnoczi aio_context_release(aio_context); 3110d9b902dbSPaolo Bonzini } 3111d9b902dbSPaolo Bonzini 31123d948cdfSStefan Hajnoczi /* Get the block job for a given device name and acquire its AioContext */ 311324d6bffeSMarkus Armbruster static BlockJob *find_block_job(const char *device, AioContext **aio_context, 311424d6bffeSMarkus Armbruster Error **errp) 31152d47c6e9SStefan Hajnoczi { 3116a0e8544cSFam Zheng BlockBackend *blk; 31172d47c6e9SStefan Hajnoczi BlockDriverState *bs; 31182d47c6e9SStefan Hajnoczi 31195433c24fSMax Reitz *aio_context = NULL; 31205433c24fSMax Reitz 3121a0e8544cSFam Zheng blk = blk_by_name(device); 3122a0e8544cSFam Zheng if (!blk) { 31233d948cdfSStefan Hajnoczi goto notfound; 31242d47c6e9SStefan Hajnoczi } 31253d948cdfSStefan Hajnoczi 31265433c24fSMax Reitz *aio_context = blk_get_aio_context(blk); 31273d948cdfSStefan Hajnoczi aio_context_acquire(*aio_context); 31283d948cdfSStefan Hajnoczi 31295433c24fSMax Reitz if (!blk_is_available(blk)) { 31305433c24fSMax Reitz goto notfound; 31315433c24fSMax Reitz } 31325433c24fSMax Reitz bs = blk_bs(blk); 31335433c24fSMax Reitz 31343d948cdfSStefan Hajnoczi if (!bs->job) { 31353d948cdfSStefan Hajnoczi goto notfound; 31363d948cdfSStefan Hajnoczi } 31373d948cdfSStefan Hajnoczi 31382d47c6e9SStefan Hajnoczi return bs->job; 31393d948cdfSStefan Hajnoczi 31403d948cdfSStefan Hajnoczi notfound: 31412e3a0266SMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 31422e3a0266SMarkus Armbruster "No active block job on device '%s'", device); 31435433c24fSMax Reitz if (*aio_context) { 31445433c24fSMax Reitz aio_context_release(*aio_context); 31453d948cdfSStefan Hajnoczi *aio_context = NULL; 31465433c24fSMax Reitz } 31473d948cdfSStefan Hajnoczi return NULL; 31482d47c6e9SStefan Hajnoczi } 31492d47c6e9SStefan Hajnoczi 3150882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 31512d47c6e9SStefan Hajnoczi { 31523d948cdfSStefan Hajnoczi AioContext *aio_context; 315324d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 31542d47c6e9SStefan Hajnoczi 31552d47c6e9SStefan Hajnoczi if (!job) { 31562d47c6e9SStefan Hajnoczi return; 31572d47c6e9SStefan Hajnoczi } 31582d47c6e9SStefan Hajnoczi 3159882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 31603d948cdfSStefan Hajnoczi aio_context_release(aio_context); 31612d47c6e9SStefan Hajnoczi } 3162370521a1SStefan Hajnoczi 31636e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 31646e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 31656e37fb81SPaolo Bonzini { 31663d948cdfSStefan Hajnoczi AioContext *aio_context; 316724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 31686e37fb81SPaolo Bonzini 31696e37fb81SPaolo Bonzini if (!job) { 31706e37fb81SPaolo Bonzini return; 31716e37fb81SPaolo Bonzini } 31723d948cdfSStefan Hajnoczi 31733d948cdfSStefan Hajnoczi if (!has_force) { 31743d948cdfSStefan Hajnoczi force = false; 31753d948cdfSStefan Hajnoczi } 31763d948cdfSStefan Hajnoczi 3177751ebd76SFam Zheng if (job->user_paused && !force) { 3178f231b88dSCole Robinson error_setg(errp, "The block job for device '%s' is currently paused", 3179f231b88dSCole Robinson device); 31803d948cdfSStefan Hajnoczi goto out; 31816e37fb81SPaolo Bonzini } 31826e37fb81SPaolo Bonzini 31836e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 31846e37fb81SPaolo Bonzini block_job_cancel(job); 31853d948cdfSStefan Hajnoczi out: 31863d948cdfSStefan Hajnoczi aio_context_release(aio_context); 31876e37fb81SPaolo Bonzini } 31886e37fb81SPaolo Bonzini 31896e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 3190370521a1SStefan Hajnoczi { 31913d948cdfSStefan Hajnoczi AioContext *aio_context; 319224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3193370521a1SStefan Hajnoczi 3194751ebd76SFam Zheng if (!job || job->user_paused) { 3195370521a1SStefan Hajnoczi return; 3196370521a1SStefan Hajnoczi } 31976e37fb81SPaolo Bonzini 3198751ebd76SFam Zheng job->user_paused = true; 31996e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 32006e37fb81SPaolo Bonzini block_job_pause(job); 32013d948cdfSStefan Hajnoczi aio_context_release(aio_context); 32026e37fb81SPaolo Bonzini } 32036e37fb81SPaolo Bonzini 32046e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 32056e37fb81SPaolo Bonzini { 32063d948cdfSStefan Hajnoczi AioContext *aio_context; 320724d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 32086e37fb81SPaolo Bonzini 3209751ebd76SFam Zheng if (!job || !job->user_paused) { 32108acc72a4SPaolo Bonzini return; 32118acc72a4SPaolo Bonzini } 3212370521a1SStefan Hajnoczi 3213751ebd76SFam Zheng job->user_paused = false; 32146e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 32156e37fb81SPaolo Bonzini block_job_resume(job); 32163d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3217370521a1SStefan Hajnoczi } 3218fb5458cdSStefan Hajnoczi 3219aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 3220aeae883bSPaolo Bonzini { 32213d948cdfSStefan Hajnoczi AioContext *aio_context; 322224d6bffeSMarkus Armbruster BlockJob *job = find_block_job(device, &aio_context, errp); 3223aeae883bSPaolo Bonzini 3224aeae883bSPaolo Bonzini if (!job) { 3225aeae883bSPaolo Bonzini return; 3226aeae883bSPaolo Bonzini } 3227aeae883bSPaolo Bonzini 3228aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 3229aeae883bSPaolo Bonzini block_job_complete(job, errp); 32303d948cdfSStefan Hajnoczi aio_context_release(aio_context); 3231aeae883bSPaolo Bonzini } 3232aeae883bSPaolo Bonzini 3233fa40e656SJeff Cody void qmp_change_backing_file(const char *device, 3234fa40e656SJeff Cody const char *image_node_name, 3235fa40e656SJeff Cody const char *backing_file, 3236fa40e656SJeff Cody Error **errp) 3237fa40e656SJeff Cody { 3238a0e8544cSFam Zheng BlockBackend *blk; 3239fa40e656SJeff Cody BlockDriverState *bs = NULL; 3240729962f6SStefan Hajnoczi AioContext *aio_context; 3241fa40e656SJeff Cody BlockDriverState *image_bs = NULL; 3242fa40e656SJeff Cody Error *local_err = NULL; 3243fa40e656SJeff Cody bool ro; 3244fa40e656SJeff Cody int open_flags; 3245fa40e656SJeff Cody int ret; 3246fa40e656SJeff Cody 3247a0e8544cSFam Zheng blk = blk_by_name(device); 3248a0e8544cSFam Zheng if (!blk) { 324975158ebbSMarkus Armbruster error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 325075158ebbSMarkus Armbruster "Device '%s' not found", device); 3251fa40e656SJeff Cody return; 3252fa40e656SJeff Cody } 3253fa40e656SJeff Cody 32545433c24fSMax Reitz aio_context = blk_get_aio_context(blk); 3255729962f6SStefan Hajnoczi aio_context_acquire(aio_context); 3256729962f6SStefan Hajnoczi 32575433c24fSMax Reitz if (!blk_is_available(blk)) { 32585433c24fSMax Reitz error_setg(errp, "Device '%s' has no medium", device); 32595433c24fSMax Reitz goto out; 32605433c24fSMax Reitz } 32615433c24fSMax Reitz bs = blk_bs(blk); 32625433c24fSMax Reitz 3263fa40e656SJeff Cody image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3264fa40e656SJeff Cody if (local_err) { 3265fa40e656SJeff Cody error_propagate(errp, local_err); 3266729962f6SStefan Hajnoczi goto out; 3267fa40e656SJeff Cody } 3268fa40e656SJeff Cody 3269fa40e656SJeff Cody if (!image_bs) { 3270fa40e656SJeff Cody error_setg(errp, "image file not found"); 3271729962f6SStefan Hajnoczi goto out; 3272fa40e656SJeff Cody } 3273fa40e656SJeff Cody 3274fa40e656SJeff Cody if (bdrv_find_base(image_bs) == image_bs) { 3275fa40e656SJeff Cody error_setg(errp, "not allowing backing file change on an image " 3276fa40e656SJeff Cody "without a backing file"); 3277729962f6SStefan Hajnoczi goto out; 3278fa40e656SJeff Cody } 3279fa40e656SJeff Cody 3280fa40e656SJeff Cody /* even though we are not necessarily operating on bs, we need it to 3281fa40e656SJeff Cody * determine if block ops are currently prohibited on the chain */ 3282fa40e656SJeff Cody if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3283729962f6SStefan Hajnoczi goto out; 3284fa40e656SJeff Cody } 3285fa40e656SJeff Cody 3286fa40e656SJeff Cody /* final sanity check */ 3287fa40e656SJeff Cody if (!bdrv_chain_contains(bs, image_bs)) { 3288fa40e656SJeff Cody error_setg(errp, "'%s' and image file are not in the same chain", 3289fa40e656SJeff Cody device); 3290729962f6SStefan Hajnoczi goto out; 3291fa40e656SJeff Cody } 3292fa40e656SJeff Cody 3293fa40e656SJeff Cody /* if not r/w, reopen to make r/w */ 3294fa40e656SJeff Cody open_flags = image_bs->open_flags; 3295fa40e656SJeff Cody ro = bdrv_is_read_only(image_bs); 3296fa40e656SJeff Cody 3297fa40e656SJeff Cody if (ro) { 3298fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3299fa40e656SJeff Cody if (local_err) { 3300fa40e656SJeff Cody error_propagate(errp, local_err); 3301729962f6SStefan Hajnoczi goto out; 3302fa40e656SJeff Cody } 3303fa40e656SJeff Cody } 3304fa40e656SJeff Cody 3305fa40e656SJeff Cody ret = bdrv_change_backing_file(image_bs, backing_file, 3306fa40e656SJeff Cody image_bs->drv ? image_bs->drv->format_name : ""); 3307fa40e656SJeff Cody 3308fa40e656SJeff Cody if (ret < 0) { 3309fa40e656SJeff Cody error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3310fa40e656SJeff Cody backing_file); 3311fa40e656SJeff Cody /* don't exit here, so we can try to restore open flags if 3312fa40e656SJeff Cody * appropriate */ 3313fa40e656SJeff Cody } 3314fa40e656SJeff Cody 3315fa40e656SJeff Cody if (ro) { 3316fa40e656SJeff Cody bdrv_reopen(image_bs, open_flags, &local_err); 3317fa40e656SJeff Cody if (local_err) { 3318fa40e656SJeff Cody error_propagate(errp, local_err); /* will preserve prior errp */ 3319fa40e656SJeff Cody } 3320fa40e656SJeff Cody } 3321729962f6SStefan Hajnoczi 3322729962f6SStefan Hajnoczi out: 3323729962f6SStefan Hajnoczi aio_context_release(aio_context); 3324fa40e656SJeff Cody } 3325fa40e656SJeff Cody 3326d26c9a15SKevin Wolf void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3327d26c9a15SKevin Wolf { 3328d26c9a15SKevin Wolf QmpOutputVisitor *ov = qmp_output_visitor_new(); 3329be4b67bcSMax Reitz BlockDriverState *bs; 3330be4b67bcSMax Reitz BlockBackend *blk = NULL; 3331d26c9a15SKevin Wolf QObject *obj; 3332d26c9a15SKevin Wolf QDict *qdict; 3333d26c9a15SKevin Wolf Error *local_err = NULL; 3334d26c9a15SKevin Wolf 333560e19e06SMarkus Armbruster /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3336d26c9a15SKevin Wolf * cache.direct=false instead of silently switching to aio=threads, except 333760e19e06SMarkus Armbruster * when called from drive_new(). 3338d26c9a15SKevin Wolf * 3339d26c9a15SKevin Wolf * For now, simply forbidding the combination for all drivers will do. */ 3340d26c9a15SKevin Wolf if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3341c6e0bd9bSKevin Wolf bool direct = options->has_cache && 3342c6e0bd9bSKevin Wolf options->cache->has_direct && 3343c6e0bd9bSKevin Wolf options->cache->direct; 3344c6e0bd9bSKevin Wolf if (!direct) { 3345d26c9a15SKevin Wolf error_setg(errp, "aio=native requires cache.direct=true"); 3346d26c9a15SKevin Wolf goto fail; 3347d26c9a15SKevin Wolf } 3348d26c9a15SKevin Wolf } 3349d26c9a15SKevin Wolf 3350d26c9a15SKevin Wolf visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3351d26c9a15SKevin Wolf &options, NULL, &local_err); 335284d18f06SMarkus Armbruster if (local_err) { 3353d26c9a15SKevin Wolf error_propagate(errp, local_err); 3354d26c9a15SKevin Wolf goto fail; 3355d26c9a15SKevin Wolf } 3356d26c9a15SKevin Wolf 3357d26c9a15SKevin Wolf obj = qmp_output_get_qobject(ov); 3358d26c9a15SKevin Wolf qdict = qobject_to_qdict(obj); 3359d26c9a15SKevin Wolf 3360d26c9a15SKevin Wolf qdict_flatten(qdict); 3361d26c9a15SKevin Wolf 3362be4b67bcSMax Reitz if (options->has_id) { 336318e46a03SMarkus Armbruster blk = blockdev_init(NULL, qdict, &local_err); 336484d18f06SMarkus Armbruster if (local_err) { 3365b681072dSKevin Wolf error_propagate(errp, local_err); 3366d26c9a15SKevin Wolf goto fail; 3367d26c9a15SKevin Wolf } 3368d26c9a15SKevin Wolf 3369be4b67bcSMax Reitz bs = blk_bs(blk); 3370be4b67bcSMax Reitz } else { 3371be4b67bcSMax Reitz if (!qdict_get_try_str(qdict, "node-name")) { 3372be4b67bcSMax Reitz error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3373be4b67bcSMax Reitz "the root node"); 3374be4b67bcSMax Reitz goto fail; 3375be4b67bcSMax Reitz } 3376be4b67bcSMax Reitz 3377bd745e23SMax Reitz bs = bds_tree_init(qdict, errp); 3378bd745e23SMax Reitz if (!bs) { 3379be4b67bcSMax Reitz goto fail; 3380be4b67bcSMax Reitz } 3381be4b67bcSMax Reitz } 3382be4b67bcSMax Reitz 3383be4b67bcSMax Reitz if (bs && bdrv_key_required(bs)) { 3384be4b67bcSMax Reitz if (blk) { 338518e46a03SMarkus Armbruster blk_unref(blk); 3386be4b67bcSMax Reitz } else { 3387be4b67bcSMax Reitz bdrv_unref(bs); 3388be4b67bcSMax Reitz } 33898ae8e904SKevin Wolf error_setg(errp, "blockdev-add doesn't support encrypted devices"); 33908ae8e904SKevin Wolf goto fail; 33918ae8e904SKevin Wolf } 33928ae8e904SKevin Wolf 3393d26c9a15SKevin Wolf fail: 3394d26c9a15SKevin Wolf qmp_output_visitor_cleanup(ov); 3395d26c9a15SKevin Wolf } 3396d26c9a15SKevin Wolf 3397fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3398fb5458cdSStefan Hajnoczi { 3399fea68bb6SMarkus Armbruster BlockJobInfoList *head = NULL, **p_next = &head; 3400fea68bb6SMarkus Armbruster BlockDriverState *bs; 3401fea68bb6SMarkus Armbruster 3402fea68bb6SMarkus Armbruster for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 340369691e72SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 340469691e72SStefan Hajnoczi 340569691e72SStefan Hajnoczi aio_context_acquire(aio_context); 340669691e72SStefan Hajnoczi 3407fea68bb6SMarkus Armbruster if (bs->job) { 3408fea68bb6SMarkus Armbruster BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3409fea68bb6SMarkus Armbruster elem->value = block_job_query(bs->job); 3410fea68bb6SMarkus Armbruster *p_next = elem; 3411fea68bb6SMarkus Armbruster p_next = &elem->next; 3412fea68bb6SMarkus Armbruster } 341369691e72SStefan Hajnoczi 341469691e72SStefan Hajnoczi aio_context_release(aio_context); 3415fea68bb6SMarkus Armbruster } 3416fea68bb6SMarkus Armbruster 3417fea68bb6SMarkus Armbruster return head; 3418fb5458cdSStefan Hajnoczi } 34194d454574SPaolo Bonzini 34200006383eSKevin Wolf QemuOptsList qemu_common_drive_opts = { 34214d454574SPaolo Bonzini .name = "drive", 34220006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 34234d454574SPaolo Bonzini .desc = { 34244d454574SPaolo Bonzini { 34254d454574SPaolo Bonzini .name = "snapshot", 34264d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 34274d454574SPaolo Bonzini .help = "enable/disable snapshot mode", 34284d454574SPaolo Bonzini },{ 3429a9384affSPaolo Bonzini .name = "discard", 3430a9384affSPaolo Bonzini .type = QEMU_OPT_STRING, 3431a9384affSPaolo Bonzini .help = "discard operation (ignore/off, unmap/on)", 3432a9384affSPaolo Bonzini },{ 343354861b92SKevin Wolf .name = BDRV_OPT_CACHE_WB, 343429c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 343529c4e2b5SKevin Wolf .help = "enables writeback mode for any caches", 343629c4e2b5SKevin Wolf },{ 343754861b92SKevin Wolf .name = BDRV_OPT_CACHE_DIRECT, 343829c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 343929c4e2b5SKevin Wolf .help = "enables use of O_DIRECT (bypass the host page cache)", 344029c4e2b5SKevin Wolf },{ 344154861b92SKevin Wolf .name = BDRV_OPT_CACHE_NO_FLUSH, 344229c4e2b5SKevin Wolf .type = QEMU_OPT_BOOL, 344329c4e2b5SKevin Wolf .help = "ignore any flush requests for the device", 34444d454574SPaolo Bonzini },{ 34454d454574SPaolo Bonzini .name = "aio", 34464d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 34474d454574SPaolo Bonzini .help = "host AIO implementation (threads, native)", 34484d454574SPaolo Bonzini },{ 34494d454574SPaolo Bonzini .name = "format", 34504d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 34514d454574SPaolo Bonzini .help = "disk format (raw, qcow2, ...)", 34524d454574SPaolo Bonzini },{ 34534d454574SPaolo Bonzini .name = "rerror", 34544d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 34554d454574SPaolo Bonzini .help = "read error action", 34564d454574SPaolo Bonzini },{ 34574d454574SPaolo Bonzini .name = "werror", 34584d454574SPaolo Bonzini .type = QEMU_OPT_STRING, 34594d454574SPaolo Bonzini .help = "write error action", 34604d454574SPaolo Bonzini },{ 34610f227a94SKevin Wolf .name = "read-only", 34624d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 34634d454574SPaolo Bonzini .help = "open drive file as read-only", 34644d454574SPaolo Bonzini },{ 346557975222SKevin Wolf .name = "throttling.iops-total", 34664d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34674d454574SPaolo Bonzini .help = "limit total I/O operations per second", 34684d454574SPaolo Bonzini },{ 346957975222SKevin Wolf .name = "throttling.iops-read", 34704d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34714d454574SPaolo Bonzini .help = "limit read operations per second", 34724d454574SPaolo Bonzini },{ 347357975222SKevin Wolf .name = "throttling.iops-write", 34744d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34754d454574SPaolo Bonzini .help = "limit write operations per second", 34764d454574SPaolo Bonzini },{ 347757975222SKevin Wolf .name = "throttling.bps-total", 34784d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34794d454574SPaolo Bonzini .help = "limit total bytes per second", 34804d454574SPaolo Bonzini },{ 348157975222SKevin Wolf .name = "throttling.bps-read", 34824d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34834d454574SPaolo Bonzini .help = "limit read bytes per second", 34844d454574SPaolo Bonzini },{ 348557975222SKevin Wolf .name = "throttling.bps-write", 34864d454574SPaolo Bonzini .type = QEMU_OPT_NUMBER, 34874d454574SPaolo Bonzini .help = "limit write bytes per second", 34884d454574SPaolo Bonzini },{ 34893e9fab69SBenoît Canet .name = "throttling.iops-total-max", 34903e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 34913e9fab69SBenoît Canet .help = "I/O operations burst", 34923e9fab69SBenoît Canet },{ 34933e9fab69SBenoît Canet .name = "throttling.iops-read-max", 34943e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 34953e9fab69SBenoît Canet .help = "I/O operations read burst", 34963e9fab69SBenoît Canet },{ 34973e9fab69SBenoît Canet .name = "throttling.iops-write-max", 34983e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 34993e9fab69SBenoît Canet .help = "I/O operations write burst", 35003e9fab69SBenoît Canet },{ 35013e9fab69SBenoît Canet .name = "throttling.bps-total-max", 35023e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 35033e9fab69SBenoît Canet .help = "total bytes burst", 35043e9fab69SBenoît Canet },{ 35053e9fab69SBenoît Canet .name = "throttling.bps-read-max", 35063e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 35073e9fab69SBenoît Canet .help = "total bytes read burst", 35083e9fab69SBenoît Canet },{ 35093e9fab69SBenoît Canet .name = "throttling.bps-write-max", 35103e9fab69SBenoît Canet .type = QEMU_OPT_NUMBER, 35113e9fab69SBenoît Canet .help = "total bytes write burst", 35123e9fab69SBenoît Canet },{ 35132024c1dfSBenoît Canet .name = "throttling.iops-size", 35142024c1dfSBenoît Canet .type = QEMU_OPT_NUMBER, 35152024c1dfSBenoît Canet .help = "when limiting by iops max size of an I/O in bytes", 35162024c1dfSBenoît Canet },{ 351776f4afb4SAlberto Garcia .name = "throttling.group", 351876f4afb4SAlberto Garcia .type = QEMU_OPT_STRING, 351976f4afb4SAlberto Garcia .help = "name of the block throttling group", 352076f4afb4SAlberto Garcia },{ 35214d454574SPaolo Bonzini .name = "copy-on-read", 35224d454574SPaolo Bonzini .type = QEMU_OPT_BOOL, 35234d454574SPaolo Bonzini .help = "copy read data from backing file into image file", 3524465bee1dSPeter Lieven },{ 3525465bee1dSPeter Lieven .name = "detect-zeroes", 3526465bee1dSPeter Lieven .type = QEMU_OPT_STRING, 3527465bee1dSPeter Lieven .help = "try to optimize zero writes (off, on, unmap)", 35284d454574SPaolo Bonzini }, 35294d454574SPaolo Bonzini { /* end of list */ } 35304d454574SPaolo Bonzini }, 35314d454574SPaolo Bonzini }; 35320006383eSKevin Wolf 3533bd745e23SMax Reitz static QemuOptsList qemu_root_bds_opts = { 3534bd745e23SMax Reitz .name = "root-bds", 3535bd745e23SMax Reitz .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 3536bd745e23SMax Reitz .desc = { 3537bd745e23SMax Reitz { 3538bd745e23SMax Reitz .name = "discard", 3539bd745e23SMax Reitz .type = QEMU_OPT_STRING, 3540bd745e23SMax Reitz .help = "discard operation (ignore/off, unmap/on)", 3541bd745e23SMax Reitz },{ 3542bd745e23SMax Reitz .name = "cache.writeback", 3543bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3544bd745e23SMax Reitz .help = "enables writeback mode for any caches", 3545bd745e23SMax Reitz },{ 3546bd745e23SMax Reitz .name = "cache.direct", 3547bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3548bd745e23SMax Reitz .help = "enables use of O_DIRECT (bypass the host page cache)", 3549bd745e23SMax Reitz },{ 3550bd745e23SMax Reitz .name = "cache.no-flush", 3551bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3552bd745e23SMax Reitz .help = "ignore any flush requests for the device", 3553bd745e23SMax Reitz },{ 3554bd745e23SMax Reitz .name = "aio", 3555bd745e23SMax Reitz .type = QEMU_OPT_STRING, 3556bd745e23SMax Reitz .help = "host AIO implementation (threads, native)", 3557bd745e23SMax Reitz },{ 3558bd745e23SMax Reitz .name = "read-only", 3559bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3560bd745e23SMax Reitz .help = "open drive file as read-only", 3561bd745e23SMax Reitz },{ 3562bd745e23SMax Reitz .name = "copy-on-read", 3563bd745e23SMax Reitz .type = QEMU_OPT_BOOL, 3564bd745e23SMax Reitz .help = "copy read data from backing file into image file", 3565bd745e23SMax Reitz },{ 3566bd745e23SMax Reitz .name = "detect-zeroes", 3567bd745e23SMax Reitz .type = QEMU_OPT_STRING, 3568bd745e23SMax Reitz .help = "try to optimize zero writes (off, on, unmap)", 3569bd745e23SMax Reitz }, 3570bd745e23SMax Reitz { /* end of list */ } 3571bd745e23SMax Reitz }, 3572bd745e23SMax Reitz }; 3573bd745e23SMax Reitz 35740006383eSKevin Wolf QemuOptsList qemu_drive_opts = { 35750006383eSKevin Wolf .name = "drive", 35760006383eSKevin Wolf .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 35770006383eSKevin Wolf .desc = { 3578492fdc6fSKevin Wolf /* 3579492fdc6fSKevin Wolf * no elements => accept any params 3580492fdc6fSKevin Wolf * validation will happen later 3581492fdc6fSKevin Wolf */ 35820006383eSKevin Wolf { /* end of list */ } 35830006383eSKevin Wolf }, 35840006383eSKevin Wolf }; 3585