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. 8666daa68SMarkus Armbruster */ 9666daa68SMarkus Armbruster 10666daa68SMarkus Armbruster #include "blockdev.h" 112b584959SMarkus Armbruster #include "hw/block-common.h" 122f0c9fe6SPaolo Bonzini #include "blockjob.h" 13666daa68SMarkus Armbruster #include "monitor.h" 14*7b1b5d19SPaolo Bonzini #include "qapi/qmp/qerror.h" 15666daa68SMarkus Armbruster #include "qemu-option.h" 16666daa68SMarkus Armbruster #include "qemu-config.h" 17*7b1b5d19SPaolo Bonzini #include "qapi/qmp/types.h" 18666daa68SMarkus Armbruster #include "sysemu.h" 199063f814SRyan Harper #include "block_int.h" 20a4dea8a9SLuiz Capitulino #include "qmp-commands.h" 2112bd451fSStefan Hajnoczi #include "trace.h" 22c9344f22SAnthony Liguori #include "arch_init.h" 23666daa68SMarkus Armbruster 24c9b62a7eSMarkus Armbruster static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); 25666daa68SMarkus Armbruster 261960966dSMarkus Armbruster static const char *const if_name[IF_COUNT] = { 271960966dSMarkus Armbruster [IF_NONE] = "none", 281960966dSMarkus Armbruster [IF_IDE] = "ide", 291960966dSMarkus Armbruster [IF_SCSI] = "scsi", 301960966dSMarkus Armbruster [IF_FLOPPY] = "floppy", 311960966dSMarkus Armbruster [IF_PFLASH] = "pflash", 321960966dSMarkus Armbruster [IF_MTD] = "mtd", 331960966dSMarkus Armbruster [IF_SD] = "sd", 341960966dSMarkus Armbruster [IF_VIRTIO] = "virtio", 351960966dSMarkus Armbruster [IF_XEN] = "xen", 361960966dSMarkus Armbruster }; 371960966dSMarkus Armbruster 381960966dSMarkus Armbruster static const int if_max_devs[IF_COUNT] = { 3927d6bf40SMarkus Armbruster /* 4027d6bf40SMarkus Armbruster * Do not change these numbers! They govern how drive option 4127d6bf40SMarkus Armbruster * index maps to unit and bus. That mapping is ABI. 4227d6bf40SMarkus Armbruster * 4327d6bf40SMarkus Armbruster * All controllers used to imlement if=T drives need to support 4427d6bf40SMarkus Armbruster * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 4527d6bf40SMarkus Armbruster * Otherwise, some index values map to "impossible" bus, unit 4627d6bf40SMarkus Armbruster * values. 4727d6bf40SMarkus Armbruster * 4827d6bf40SMarkus Armbruster * For instance, if you change [IF_SCSI] to 255, -drive 4927d6bf40SMarkus Armbruster * if=scsi,index=12 no longer means bus=1,unit=5, but 5027d6bf40SMarkus Armbruster * bus=0,unit=12. With an lsi53c895a controller (7 units max), 5127d6bf40SMarkus Armbruster * the drive can't be set up. Regression. 5227d6bf40SMarkus Armbruster */ 5327d6bf40SMarkus Armbruster [IF_IDE] = 2, 5427d6bf40SMarkus Armbruster [IF_SCSI] = 7, 551960966dSMarkus Armbruster }; 561960966dSMarkus Armbruster 5714bafc54SMarkus Armbruster /* 5814bafc54SMarkus Armbruster * We automatically delete the drive when a device using it gets 5914bafc54SMarkus Armbruster * unplugged. Questionable feature, but we can't just drop it. 6014bafc54SMarkus Armbruster * Device models call blockdev_mark_auto_del() to schedule the 6114bafc54SMarkus Armbruster * automatic deletion, and generic qdev code calls blockdev_auto_del() 6214bafc54SMarkus Armbruster * when deletion is actually safe. 6314bafc54SMarkus Armbruster */ 6414bafc54SMarkus Armbruster void blockdev_mark_auto_del(BlockDriverState *bs) 6514bafc54SMarkus Armbruster { 6614bafc54SMarkus Armbruster DriveInfo *dinfo = drive_get_by_blockdev(bs); 6714bafc54SMarkus Armbruster 6812bde0eeSPaolo Bonzini if (bs->job) { 6912bde0eeSPaolo Bonzini block_job_cancel(bs->job); 7012bde0eeSPaolo Bonzini } 710fc0f1faSRyan Harper if (dinfo) { 7214bafc54SMarkus Armbruster dinfo->auto_del = 1; 7314bafc54SMarkus Armbruster } 740fc0f1faSRyan Harper } 7514bafc54SMarkus Armbruster 7614bafc54SMarkus Armbruster void blockdev_auto_del(BlockDriverState *bs) 7714bafc54SMarkus Armbruster { 7814bafc54SMarkus Armbruster DriveInfo *dinfo = drive_get_by_blockdev(bs); 7914bafc54SMarkus Armbruster 800fc0f1faSRyan Harper if (dinfo && dinfo->auto_del) { 8184fb3925SMarcelo Tosatti drive_put_ref(dinfo); 8214bafc54SMarkus Armbruster } 8314bafc54SMarkus Armbruster } 8414bafc54SMarkus Armbruster 85505a7fb1SMarkus Armbruster static int drive_index_to_bus_id(BlockInterfaceType type, int index) 86505a7fb1SMarkus Armbruster { 87505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 88505a7fb1SMarkus Armbruster return max_devs ? index / max_devs : 0; 89505a7fb1SMarkus Armbruster } 90505a7fb1SMarkus Armbruster 91505a7fb1SMarkus Armbruster static int drive_index_to_unit_id(BlockInterfaceType type, int index) 92505a7fb1SMarkus Armbruster { 93505a7fb1SMarkus Armbruster int max_devs = if_max_devs[type]; 94505a7fb1SMarkus Armbruster return max_devs ? index % max_devs : index; 95505a7fb1SMarkus Armbruster } 96505a7fb1SMarkus Armbruster 972292ddaeSMarkus Armbruster QemuOpts *drive_def(const char *optstr) 982292ddaeSMarkus Armbruster { 992292ddaeSMarkus Armbruster return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); 1002292ddaeSMarkus Armbruster } 1012292ddaeSMarkus Armbruster 1022292ddaeSMarkus Armbruster QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 1035645b0f4SMarkus Armbruster const char *optstr) 104666daa68SMarkus Armbruster { 105666daa68SMarkus Armbruster QemuOpts *opts; 1062292ddaeSMarkus Armbruster char buf[32]; 107666daa68SMarkus Armbruster 1082292ddaeSMarkus Armbruster opts = drive_def(optstr); 109666daa68SMarkus Armbruster if (!opts) { 110666daa68SMarkus Armbruster return NULL; 111666daa68SMarkus Armbruster } 1122292ddaeSMarkus Armbruster if (type != IF_DEFAULT) { 1132292ddaeSMarkus Armbruster qemu_opt_set(opts, "if", if_name[type]); 1142292ddaeSMarkus Armbruster } 1152292ddaeSMarkus Armbruster if (index >= 0) { 1162292ddaeSMarkus Armbruster snprintf(buf, sizeof(buf), "%d", index); 1172292ddaeSMarkus Armbruster qemu_opt_set(opts, "index", buf); 1182292ddaeSMarkus Armbruster } 119666daa68SMarkus Armbruster if (file) 120666daa68SMarkus Armbruster qemu_opt_set(opts, "file", file); 121666daa68SMarkus Armbruster return opts; 122666daa68SMarkus Armbruster } 123666daa68SMarkus Armbruster 124666daa68SMarkus Armbruster DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 125666daa68SMarkus Armbruster { 126666daa68SMarkus Armbruster DriveInfo *dinfo; 127666daa68SMarkus Armbruster 128666daa68SMarkus Armbruster /* seek interface, bus and unit */ 129666daa68SMarkus Armbruster 130666daa68SMarkus Armbruster QTAILQ_FOREACH(dinfo, &drives, next) { 131666daa68SMarkus Armbruster if (dinfo->type == type && 132666daa68SMarkus Armbruster dinfo->bus == bus && 133666daa68SMarkus Armbruster dinfo->unit == unit) 134666daa68SMarkus Armbruster return dinfo; 135666daa68SMarkus Armbruster } 136666daa68SMarkus Armbruster 137666daa68SMarkus Armbruster return NULL; 138666daa68SMarkus Armbruster } 139666daa68SMarkus Armbruster 140f1bd51acSMarkus Armbruster DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 141f1bd51acSMarkus Armbruster { 142f1bd51acSMarkus Armbruster return drive_get(type, 143f1bd51acSMarkus Armbruster drive_index_to_bus_id(type, index), 144f1bd51acSMarkus Armbruster drive_index_to_unit_id(type, index)); 145f1bd51acSMarkus Armbruster } 146f1bd51acSMarkus Armbruster 147666daa68SMarkus Armbruster int drive_get_max_bus(BlockInterfaceType type) 148666daa68SMarkus Armbruster { 149666daa68SMarkus Armbruster int max_bus; 150666daa68SMarkus Armbruster DriveInfo *dinfo; 151666daa68SMarkus Armbruster 152666daa68SMarkus Armbruster max_bus = -1; 153666daa68SMarkus Armbruster QTAILQ_FOREACH(dinfo, &drives, next) { 154666daa68SMarkus Armbruster if(dinfo->type == type && 155666daa68SMarkus Armbruster dinfo->bus > max_bus) 156666daa68SMarkus Armbruster max_bus = dinfo->bus; 157666daa68SMarkus Armbruster } 158666daa68SMarkus Armbruster return max_bus; 159666daa68SMarkus Armbruster } 160666daa68SMarkus Armbruster 16113839974SMarkus Armbruster /* Get a block device. This should only be used for single-drive devices 16213839974SMarkus Armbruster (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 16313839974SMarkus Armbruster appropriate bus. */ 16413839974SMarkus Armbruster DriveInfo *drive_get_next(BlockInterfaceType type) 16513839974SMarkus Armbruster { 16613839974SMarkus Armbruster static int next_block_unit[IF_COUNT]; 16713839974SMarkus Armbruster 16813839974SMarkus Armbruster return drive_get(type, 0, next_block_unit[type]++); 16913839974SMarkus Armbruster } 17013839974SMarkus Armbruster 171e4700e59SMarkus Armbruster DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) 172666daa68SMarkus Armbruster { 173666daa68SMarkus Armbruster DriveInfo *dinfo; 174666daa68SMarkus Armbruster 175666daa68SMarkus Armbruster QTAILQ_FOREACH(dinfo, &drives, next) { 176e4700e59SMarkus Armbruster if (dinfo->bdrv == bs) { 177e4700e59SMarkus Armbruster return dinfo; 178666daa68SMarkus Armbruster } 179e4700e59SMarkus Armbruster } 180e4700e59SMarkus Armbruster return NULL; 181666daa68SMarkus Armbruster } 182666daa68SMarkus Armbruster 183666daa68SMarkus Armbruster static void bdrv_format_print(void *opaque, const char *name) 184666daa68SMarkus Armbruster { 185807105a7SMarkus Armbruster error_printf(" %s", name); 186666daa68SMarkus Armbruster } 187666daa68SMarkus Armbruster 18884fb3925SMarcelo Tosatti static void drive_uninit(DriveInfo *dinfo) 189666daa68SMarkus Armbruster { 190666daa68SMarkus Armbruster qemu_opts_del(dinfo->opts); 191666daa68SMarkus Armbruster bdrv_delete(dinfo->bdrv); 1927267c094SAnthony Liguori g_free(dinfo->id); 193666daa68SMarkus Armbruster QTAILQ_REMOVE(&drives, dinfo, next); 1947267c094SAnthony Liguori g_free(dinfo); 195666daa68SMarkus Armbruster } 196666daa68SMarkus Armbruster 19784fb3925SMarcelo Tosatti void drive_put_ref(DriveInfo *dinfo) 19884fb3925SMarcelo Tosatti { 19984fb3925SMarcelo Tosatti assert(dinfo->refcount); 20084fb3925SMarcelo Tosatti if (--dinfo->refcount == 0) { 20184fb3925SMarcelo Tosatti drive_uninit(dinfo); 20284fb3925SMarcelo Tosatti } 20384fb3925SMarcelo Tosatti } 20484fb3925SMarcelo Tosatti 20584fb3925SMarcelo Tosatti void drive_get_ref(DriveInfo *dinfo) 20684fb3925SMarcelo Tosatti { 20784fb3925SMarcelo Tosatti dinfo->refcount++; 20884fb3925SMarcelo Tosatti } 20984fb3925SMarcelo Tosatti 210aa398a5cSStefan Hajnoczi typedef struct { 211aa398a5cSStefan Hajnoczi QEMUBH *bh; 212aa398a5cSStefan Hajnoczi DriveInfo *dinfo; 213aa398a5cSStefan Hajnoczi } DrivePutRefBH; 214aa398a5cSStefan Hajnoczi 215aa398a5cSStefan Hajnoczi static void drive_put_ref_bh(void *opaque) 216aa398a5cSStefan Hajnoczi { 217aa398a5cSStefan Hajnoczi DrivePutRefBH *s = opaque; 218aa398a5cSStefan Hajnoczi 219aa398a5cSStefan Hajnoczi drive_put_ref(s->dinfo); 220aa398a5cSStefan Hajnoczi qemu_bh_delete(s->bh); 221aa398a5cSStefan Hajnoczi g_free(s); 222aa398a5cSStefan Hajnoczi } 223aa398a5cSStefan Hajnoczi 224aa398a5cSStefan Hajnoczi /* 225aa398a5cSStefan Hajnoczi * Release a drive reference in a BH 226aa398a5cSStefan Hajnoczi * 227aa398a5cSStefan Hajnoczi * It is not possible to use drive_put_ref() from a callback function when the 228aa398a5cSStefan Hajnoczi * callers still need the drive. In such cases we schedule a BH to release the 229aa398a5cSStefan Hajnoczi * reference. 230aa398a5cSStefan Hajnoczi */ 231aa398a5cSStefan Hajnoczi static void drive_put_ref_bh_schedule(DriveInfo *dinfo) 232aa398a5cSStefan Hajnoczi { 233aa398a5cSStefan Hajnoczi DrivePutRefBH *s; 234aa398a5cSStefan Hajnoczi 235aa398a5cSStefan Hajnoczi s = g_new(DrivePutRefBH, 1); 236aa398a5cSStefan Hajnoczi s->bh = qemu_bh_new(drive_put_ref_bh, s); 237aa398a5cSStefan Hajnoczi s->dinfo = dinfo; 238aa398a5cSStefan Hajnoczi qemu_bh_schedule(s->bh); 239aa398a5cSStefan Hajnoczi } 240aa398a5cSStefan Hajnoczi 2411ceee0d5SPaolo Bonzini static int parse_block_error_action(const char *buf, bool is_read) 242666daa68SMarkus Armbruster { 243666daa68SMarkus Armbruster if (!strcmp(buf, "ignore")) { 24492aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_IGNORE; 245666daa68SMarkus Armbruster } else if (!is_read && !strcmp(buf, "enospc")) { 24692aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_ENOSPC; 247666daa68SMarkus Armbruster } else if (!strcmp(buf, "stop")) { 24892aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_STOP; 249666daa68SMarkus Armbruster } else if (!strcmp(buf, "report")) { 25092aa5c6dSPaolo Bonzini return BLOCKDEV_ON_ERROR_REPORT; 251666daa68SMarkus Armbruster } else { 252807105a7SMarkus Armbruster error_report("'%s' invalid %s error action", 253666daa68SMarkus Armbruster buf, is_read ? "read" : "write"); 254666daa68SMarkus Armbruster return -1; 255666daa68SMarkus Armbruster } 256666daa68SMarkus Armbruster } 257666daa68SMarkus Armbruster 2580563e191SZhi Yong Wu static bool do_check_io_limits(BlockIOLimit *io_limits) 2590563e191SZhi Yong Wu { 2600563e191SZhi Yong Wu bool bps_flag; 2610563e191SZhi Yong Wu bool iops_flag; 2620563e191SZhi Yong Wu 2630563e191SZhi Yong Wu assert(io_limits); 2640563e191SZhi Yong Wu 2650563e191SZhi Yong Wu bps_flag = (io_limits->bps[BLOCK_IO_LIMIT_TOTAL] != 0) 2660563e191SZhi Yong Wu && ((io_limits->bps[BLOCK_IO_LIMIT_READ] != 0) 2670563e191SZhi Yong Wu || (io_limits->bps[BLOCK_IO_LIMIT_WRITE] != 0)); 2680563e191SZhi Yong Wu iops_flag = (io_limits->iops[BLOCK_IO_LIMIT_TOTAL] != 0) 2690563e191SZhi Yong Wu && ((io_limits->iops[BLOCK_IO_LIMIT_READ] != 0) 2700563e191SZhi Yong Wu || (io_limits->iops[BLOCK_IO_LIMIT_WRITE] != 0)); 2710563e191SZhi Yong Wu if (bps_flag || iops_flag) { 2720563e191SZhi Yong Wu return false; 2730563e191SZhi Yong Wu } 2740563e191SZhi Yong Wu 2750563e191SZhi Yong Wu return true; 2760563e191SZhi Yong Wu } 2770563e191SZhi Yong Wu 2782d0d2837SChristian Borntraeger DriveInfo *drive_init(QemuOpts *opts, BlockInterfaceType block_default_type) 279666daa68SMarkus Armbruster { 280666daa68SMarkus Armbruster const char *buf; 281666daa68SMarkus Armbruster const char *file = NULL; 282666daa68SMarkus Armbruster const char *serial; 283666daa68SMarkus Armbruster const char *mediastr = ""; 284666daa68SMarkus Armbruster BlockInterfaceType type; 285666daa68SMarkus Armbruster enum { MEDIA_DISK, MEDIA_CDROM } media; 286666daa68SMarkus Armbruster int bus_id, unit_id; 287666daa68SMarkus Armbruster int cyls, heads, secs, translation; 288666daa68SMarkus Armbruster BlockDriver *drv = NULL; 289666daa68SMarkus Armbruster int max_devs; 290666daa68SMarkus Armbruster int index; 291666daa68SMarkus Armbruster int ro = 0; 292666daa68SMarkus Armbruster int bdrv_flags = 0; 293666daa68SMarkus Armbruster int on_read_error, on_write_error; 294666daa68SMarkus Armbruster const char *devaddr; 295666daa68SMarkus Armbruster DriveInfo *dinfo; 2960563e191SZhi Yong Wu BlockIOLimit io_limits; 297666daa68SMarkus Armbruster int snapshot = 0; 298fb0490f6SStefan Hajnoczi bool copy_on_read; 299666daa68SMarkus Armbruster int ret; 300666daa68SMarkus Armbruster 301666daa68SMarkus Armbruster translation = BIOS_ATA_TRANSLATION_AUTO; 302666daa68SMarkus Armbruster media = MEDIA_DISK; 303666daa68SMarkus Armbruster 304666daa68SMarkus Armbruster /* extract parameters */ 305666daa68SMarkus Armbruster bus_id = qemu_opt_get_number(opts, "bus", 0); 306666daa68SMarkus Armbruster unit_id = qemu_opt_get_number(opts, "unit", -1); 307666daa68SMarkus Armbruster index = qemu_opt_get_number(opts, "index", -1); 308666daa68SMarkus Armbruster 309666daa68SMarkus Armbruster cyls = qemu_opt_get_number(opts, "cyls", 0); 310666daa68SMarkus Armbruster heads = qemu_opt_get_number(opts, "heads", 0); 311666daa68SMarkus Armbruster secs = qemu_opt_get_number(opts, "secs", 0); 312666daa68SMarkus Armbruster 313666daa68SMarkus Armbruster snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 314666daa68SMarkus Armbruster ro = qemu_opt_get_bool(opts, "readonly", 0); 315fb0490f6SStefan Hajnoczi copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); 316666daa68SMarkus Armbruster 317666daa68SMarkus Armbruster file = qemu_opt_get(opts, "file"); 318666daa68SMarkus Armbruster serial = qemu_opt_get(opts, "serial"); 319666daa68SMarkus Armbruster 320666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "if")) != NULL) { 3211960966dSMarkus Armbruster for (type = 0; type < IF_COUNT && strcmp(buf, if_name[type]); type++) 3221960966dSMarkus Armbruster ; 3231960966dSMarkus Armbruster if (type == IF_COUNT) { 324807105a7SMarkus Armbruster error_report("unsupported bus type '%s'", buf); 325666daa68SMarkus Armbruster return NULL; 326666daa68SMarkus Armbruster } 3272d3999feSLuiz Capitulino } else { 3282d0d2837SChristian Borntraeger type = block_default_type; 329666daa68SMarkus Armbruster } 3302d3999feSLuiz Capitulino 3311960966dSMarkus Armbruster max_devs = if_max_devs[type]; 332666daa68SMarkus Armbruster 333666daa68SMarkus Armbruster if (cyls || heads || secs) { 334aaea3f36SMarkus Armbruster if (cyls < 1) { 335807105a7SMarkus Armbruster error_report("invalid physical cyls number"); 336666daa68SMarkus Armbruster return NULL; 337666daa68SMarkus Armbruster } 338aaea3f36SMarkus Armbruster if (heads < 1) { 339807105a7SMarkus Armbruster error_report("invalid physical heads number"); 340666daa68SMarkus Armbruster return NULL; 341666daa68SMarkus Armbruster } 342aaea3f36SMarkus Armbruster if (secs < 1) { 343807105a7SMarkus Armbruster error_report("invalid physical secs number"); 344666daa68SMarkus Armbruster return NULL; 345666daa68SMarkus Armbruster } 346666daa68SMarkus Armbruster } 347666daa68SMarkus Armbruster 348666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "trans")) != NULL) { 349666daa68SMarkus Armbruster if (!cyls) { 350807105a7SMarkus Armbruster error_report("'%s' trans must be used with cyls, heads and secs", 351666daa68SMarkus Armbruster buf); 352666daa68SMarkus Armbruster return NULL; 353666daa68SMarkus Armbruster } 354666daa68SMarkus Armbruster if (!strcmp(buf, "none")) 355666daa68SMarkus Armbruster translation = BIOS_ATA_TRANSLATION_NONE; 356666daa68SMarkus Armbruster else if (!strcmp(buf, "lba")) 357666daa68SMarkus Armbruster translation = BIOS_ATA_TRANSLATION_LBA; 358666daa68SMarkus Armbruster else if (!strcmp(buf, "auto")) 359666daa68SMarkus Armbruster translation = BIOS_ATA_TRANSLATION_AUTO; 360666daa68SMarkus Armbruster else { 361807105a7SMarkus Armbruster error_report("'%s' invalid translation type", buf); 362666daa68SMarkus Armbruster return NULL; 363666daa68SMarkus Armbruster } 364666daa68SMarkus Armbruster } 365666daa68SMarkus Armbruster 366666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "media")) != NULL) { 367666daa68SMarkus Armbruster if (!strcmp(buf, "disk")) { 368666daa68SMarkus Armbruster media = MEDIA_DISK; 369666daa68SMarkus Armbruster } else if (!strcmp(buf, "cdrom")) { 370666daa68SMarkus Armbruster if (cyls || secs || heads) { 371e7ff8f0eSLuiz Capitulino error_report("CHS can't be set with media=%s", buf); 372666daa68SMarkus Armbruster return NULL; 373666daa68SMarkus Armbruster } 374666daa68SMarkus Armbruster media = MEDIA_CDROM; 375666daa68SMarkus Armbruster } else { 376807105a7SMarkus Armbruster error_report("'%s' invalid media", buf); 377666daa68SMarkus Armbruster return NULL; 378666daa68SMarkus Armbruster } 379666daa68SMarkus Armbruster } 380666daa68SMarkus Armbruster 3811f212b9dSPaolo Bonzini bdrv_flags |= BDRV_O_CACHE_WB; 382666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "cache")) != NULL) { 383c3993cdcSStefan Hajnoczi if (bdrv_parse_cache_flags(buf, &bdrv_flags) != 0) { 384807105a7SMarkus Armbruster error_report("invalid cache option"); 385666daa68SMarkus Armbruster return NULL; 386666daa68SMarkus Armbruster } 387666daa68SMarkus Armbruster } 388666daa68SMarkus Armbruster 389666daa68SMarkus Armbruster #ifdef CONFIG_LINUX_AIO 390666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "aio")) != NULL) { 391666daa68SMarkus Armbruster if (!strcmp(buf, "native")) { 392666daa68SMarkus Armbruster bdrv_flags |= BDRV_O_NATIVE_AIO; 393666daa68SMarkus Armbruster } else if (!strcmp(buf, "threads")) { 394666daa68SMarkus Armbruster /* this is the default */ 395666daa68SMarkus Armbruster } else { 396807105a7SMarkus Armbruster error_report("invalid aio option"); 397666daa68SMarkus Armbruster return NULL; 398666daa68SMarkus Armbruster } 399666daa68SMarkus Armbruster } 400666daa68SMarkus Armbruster #endif 401666daa68SMarkus Armbruster 402666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "format")) != NULL) { 403c8057f95SPeter Maydell if (is_help_option(buf)) { 404807105a7SMarkus Armbruster error_printf("Supported formats:"); 405666daa68SMarkus Armbruster bdrv_iterate_format(bdrv_format_print, NULL); 406807105a7SMarkus Armbruster error_printf("\n"); 407666daa68SMarkus Armbruster return NULL; 408666daa68SMarkus Armbruster } 409666daa68SMarkus Armbruster drv = bdrv_find_whitelisted_format(buf); 410666daa68SMarkus Armbruster if (!drv) { 411807105a7SMarkus Armbruster error_report("'%s' invalid format", buf); 412666daa68SMarkus Armbruster return NULL; 413666daa68SMarkus Armbruster } 414666daa68SMarkus Armbruster } 415666daa68SMarkus Armbruster 4160563e191SZhi Yong Wu /* disk I/O throttling */ 4170563e191SZhi Yong Wu io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = 4180563e191SZhi Yong Wu qemu_opt_get_number(opts, "bps", 0); 4190563e191SZhi Yong Wu io_limits.bps[BLOCK_IO_LIMIT_READ] = 4200563e191SZhi Yong Wu qemu_opt_get_number(opts, "bps_rd", 0); 4210563e191SZhi Yong Wu io_limits.bps[BLOCK_IO_LIMIT_WRITE] = 4220563e191SZhi Yong Wu qemu_opt_get_number(opts, "bps_wr", 0); 4230563e191SZhi Yong Wu io_limits.iops[BLOCK_IO_LIMIT_TOTAL] = 4240563e191SZhi Yong Wu qemu_opt_get_number(opts, "iops", 0); 4250563e191SZhi Yong Wu io_limits.iops[BLOCK_IO_LIMIT_READ] = 4260563e191SZhi Yong Wu qemu_opt_get_number(opts, "iops_rd", 0); 4270563e191SZhi Yong Wu io_limits.iops[BLOCK_IO_LIMIT_WRITE] = 4280563e191SZhi Yong Wu qemu_opt_get_number(opts, "iops_wr", 0); 4290563e191SZhi Yong Wu 4300563e191SZhi Yong Wu if (!do_check_io_limits(&io_limits)) { 4310563e191SZhi Yong Wu error_report("bps(iops) and bps_rd/bps_wr(iops_rd/iops_wr) " 4320563e191SZhi Yong Wu "cannot be used at the same time"); 4330563e191SZhi Yong Wu return NULL; 4340563e191SZhi Yong Wu } 4350563e191SZhi Yong Wu 4360d92d17aSJan Kiszka if (qemu_opt_get(opts, "boot") != NULL) { 4370d92d17aSJan Kiszka fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 4380d92d17aSJan Kiszka "ignored. Future versions will reject this parameter. Please " 4390d92d17aSJan Kiszka "update your scripts.\n"); 4400d92d17aSJan Kiszka } 4410d92d17aSJan Kiszka 44292aa5c6dSPaolo Bonzini on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 443666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 444666daa68SMarkus Armbruster if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { 445807105a7SMarkus Armbruster error_report("werror is not supported by this bus type"); 446666daa68SMarkus Armbruster return NULL; 447666daa68SMarkus Armbruster } 448666daa68SMarkus Armbruster 449666daa68SMarkus Armbruster on_write_error = parse_block_error_action(buf, 0); 450666daa68SMarkus Armbruster if (on_write_error < 0) { 451666daa68SMarkus Armbruster return NULL; 452666daa68SMarkus Armbruster } 453666daa68SMarkus Armbruster } 454666daa68SMarkus Armbruster 45592aa5c6dSPaolo Bonzini on_read_error = BLOCKDEV_ON_ERROR_REPORT; 456666daa68SMarkus Armbruster if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 4575dba48a8SKevin Wolf if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { 458807105a7SMarkus Armbruster error_report("rerror is not supported by this bus type"); 459666daa68SMarkus Armbruster return NULL; 460666daa68SMarkus Armbruster } 461666daa68SMarkus Armbruster 462666daa68SMarkus Armbruster on_read_error = parse_block_error_action(buf, 1); 463666daa68SMarkus Armbruster if (on_read_error < 0) { 464666daa68SMarkus Armbruster return NULL; 465666daa68SMarkus Armbruster } 466666daa68SMarkus Armbruster } 467666daa68SMarkus Armbruster 468666daa68SMarkus Armbruster if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) { 469666daa68SMarkus Armbruster if (type != IF_VIRTIO) { 470807105a7SMarkus Armbruster error_report("addr is not supported by this bus type"); 471666daa68SMarkus Armbruster return NULL; 472666daa68SMarkus Armbruster } 473666daa68SMarkus Armbruster } 474666daa68SMarkus Armbruster 475666daa68SMarkus Armbruster /* compute bus and unit according index */ 476666daa68SMarkus Armbruster 477666daa68SMarkus Armbruster if (index != -1) { 478666daa68SMarkus Armbruster if (bus_id != 0 || unit_id != -1) { 479807105a7SMarkus Armbruster error_report("index cannot be used with bus and unit"); 480666daa68SMarkus Armbruster return NULL; 481666daa68SMarkus Armbruster } 482505a7fb1SMarkus Armbruster bus_id = drive_index_to_bus_id(type, index); 483505a7fb1SMarkus Armbruster unit_id = drive_index_to_unit_id(type, index); 484666daa68SMarkus Armbruster } 485666daa68SMarkus Armbruster 486666daa68SMarkus Armbruster /* if user doesn't specify a unit_id, 487666daa68SMarkus Armbruster * try to find the first free 488666daa68SMarkus Armbruster */ 489666daa68SMarkus Armbruster 490666daa68SMarkus Armbruster if (unit_id == -1) { 491666daa68SMarkus Armbruster unit_id = 0; 492666daa68SMarkus Armbruster while (drive_get(type, bus_id, unit_id) != NULL) { 493666daa68SMarkus Armbruster unit_id++; 494666daa68SMarkus Armbruster if (max_devs && unit_id >= max_devs) { 495666daa68SMarkus Armbruster unit_id -= max_devs; 496666daa68SMarkus Armbruster bus_id++; 497666daa68SMarkus Armbruster } 498666daa68SMarkus Armbruster } 499666daa68SMarkus Armbruster } 500666daa68SMarkus Armbruster 501666daa68SMarkus Armbruster /* check unit id */ 502666daa68SMarkus Armbruster 503666daa68SMarkus Armbruster if (max_devs && unit_id >= max_devs) { 504807105a7SMarkus Armbruster error_report("unit %d too big (max is %d)", 505666daa68SMarkus Armbruster unit_id, max_devs - 1); 506666daa68SMarkus Armbruster return NULL; 507666daa68SMarkus Armbruster } 508666daa68SMarkus Armbruster 509666daa68SMarkus Armbruster /* 5104e5d9b57SMarkus Armbruster * catch multiple definitions 511666daa68SMarkus Armbruster */ 512666daa68SMarkus Armbruster 513666daa68SMarkus Armbruster if (drive_get(type, bus_id, unit_id) != NULL) { 5144e5d9b57SMarkus Armbruster error_report("drive with bus=%d, unit=%d (index=%d) exists", 5154e5d9b57SMarkus Armbruster bus_id, unit_id, index); 516666daa68SMarkus Armbruster return NULL; 517666daa68SMarkus Armbruster } 518666daa68SMarkus Armbruster 519666daa68SMarkus Armbruster /* init */ 520666daa68SMarkus Armbruster 5217267c094SAnthony Liguori dinfo = g_malloc0(sizeof(*dinfo)); 522666daa68SMarkus Armbruster if ((buf = qemu_opts_id(opts)) != NULL) { 5237267c094SAnthony Liguori dinfo->id = g_strdup(buf); 524666daa68SMarkus Armbruster } else { 525666daa68SMarkus Armbruster /* no id supplied -> create one */ 5267267c094SAnthony Liguori dinfo->id = g_malloc0(32); 527666daa68SMarkus Armbruster if (type == IF_IDE || type == IF_SCSI) 528666daa68SMarkus Armbruster mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 529666daa68SMarkus Armbruster if (max_devs) 530666daa68SMarkus Armbruster snprintf(dinfo->id, 32, "%s%i%s%i", 53179d21d5bSMarkus Armbruster if_name[type], bus_id, mediastr, unit_id); 532666daa68SMarkus Armbruster else 533666daa68SMarkus Armbruster snprintf(dinfo->id, 32, "%s%s%i", 53479d21d5bSMarkus Armbruster if_name[type], mediastr, unit_id); 535666daa68SMarkus Armbruster } 536666daa68SMarkus Armbruster dinfo->bdrv = bdrv_new(dinfo->id); 53780dd1aaeSKevin Shanahan dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; 53880dd1aaeSKevin Shanahan dinfo->bdrv->read_only = ro; 539666daa68SMarkus Armbruster dinfo->devaddr = devaddr; 540666daa68SMarkus Armbruster dinfo->type = type; 541666daa68SMarkus Armbruster dinfo->bus = bus_id; 542666daa68SMarkus Armbruster dinfo->unit = unit_id; 543317bb412SMarkus Armbruster dinfo->cyls = cyls; 544317bb412SMarkus Armbruster dinfo->heads = heads; 545317bb412SMarkus Armbruster dinfo->secs = secs; 546317bb412SMarkus Armbruster dinfo->trans = translation; 547666daa68SMarkus Armbruster dinfo->opts = opts; 54884fb3925SMarcelo Tosatti dinfo->refcount = 1; 549577d0a38SMarkus Armbruster dinfo->serial = serial; 550666daa68SMarkus Armbruster QTAILQ_INSERT_TAIL(&drives, dinfo, next); 551666daa68SMarkus Armbruster 552abd7f68dSMarkus Armbruster bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); 553abd7f68dSMarkus Armbruster 5540563e191SZhi Yong Wu /* disk I/O throttling */ 5550563e191SZhi Yong Wu bdrv_set_io_limits(dinfo->bdrv, &io_limits); 5560563e191SZhi Yong Wu 557666daa68SMarkus Armbruster switch(type) { 558666daa68SMarkus Armbruster case IF_IDE: 559666daa68SMarkus Armbruster case IF_SCSI: 560666daa68SMarkus Armbruster case IF_XEN: 561666daa68SMarkus Armbruster case IF_NONE: 5622b584959SMarkus Armbruster dinfo->media_cd = media == MEDIA_CDROM; 563666daa68SMarkus Armbruster break; 564666daa68SMarkus Armbruster case IF_SD: 565666daa68SMarkus Armbruster case IF_FLOPPY: 566666daa68SMarkus Armbruster case IF_PFLASH: 567666daa68SMarkus Armbruster case IF_MTD: 568666daa68SMarkus Armbruster break; 569666daa68SMarkus Armbruster case IF_VIRTIO: 570666daa68SMarkus Armbruster /* add virtio block device */ 571e478b448SDong Xu Wang opts = qemu_opts_create_nofail(qemu_find_opts("device")); 572eeb9c1b5SAnthony Liguori if (arch_type == QEMU_ARCH_S390X) { 573eeb9c1b5SAnthony Liguori qemu_opt_set(opts, "driver", "virtio-blk-s390"); 574eeb9c1b5SAnthony Liguori } else { 575eeb9c1b5SAnthony Liguori qemu_opt_set(opts, "driver", "virtio-blk-pci"); 576eeb9c1b5SAnthony Liguori } 577666daa68SMarkus Armbruster qemu_opt_set(opts, "drive", dinfo->id); 578666daa68SMarkus Armbruster if (devaddr) 579666daa68SMarkus Armbruster qemu_opt_set(opts, "addr", devaddr); 580666daa68SMarkus Armbruster break; 5812292ddaeSMarkus Armbruster default: 582666daa68SMarkus Armbruster abort(); 583666daa68SMarkus Armbruster } 584dd5b0d71SMarkus Armbruster if (!file || !*file) { 585319ae529SMarkus Armbruster return dinfo; 586666daa68SMarkus Armbruster } 587666daa68SMarkus Armbruster if (snapshot) { 588666daa68SMarkus Armbruster /* always use cache=unsafe with snapshot */ 589666daa68SMarkus Armbruster bdrv_flags &= ~BDRV_O_CACHE_MASK; 590666daa68SMarkus Armbruster bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); 591666daa68SMarkus Armbruster } 592666daa68SMarkus Armbruster 593fb0490f6SStefan Hajnoczi if (copy_on_read) { 594fb0490f6SStefan Hajnoczi bdrv_flags |= BDRV_O_COPY_ON_READ; 595fb0490f6SStefan Hajnoczi } 596fb0490f6SStefan Hajnoczi 597ed9d4205SBenoît Canet if (runstate_check(RUN_STATE_INMIGRATE)) { 598ed9d4205SBenoît Canet bdrv_flags |= BDRV_O_INCOMING; 599ed9d4205SBenoît Canet } 600ed9d4205SBenoît Canet 601666daa68SMarkus Armbruster if (media == MEDIA_CDROM) { 602666daa68SMarkus Armbruster /* CDROM is fine for any interface, don't check. */ 603666daa68SMarkus Armbruster ro = 1; 604666daa68SMarkus Armbruster } else if (ro == 1) { 6051e9eb78aSJordan Justen if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && 6061e9eb78aSJordan Justen type != IF_NONE && type != IF_PFLASH) { 607807105a7SMarkus Armbruster error_report("readonly not supported by this bus type"); 608a9ae2bffSMarkus Armbruster goto err; 609666daa68SMarkus Armbruster } 610666daa68SMarkus Armbruster } 611666daa68SMarkus Armbruster 612666daa68SMarkus Armbruster bdrv_flags |= ro ? 0 : BDRV_O_RDWR; 613666daa68SMarkus Armbruster 61404d4abe9SStefan Hajnoczi if (ro && copy_on_read) { 61504d4abe9SStefan Hajnoczi error_report("warning: disabling copy_on_read on readonly drive"); 61604d4abe9SStefan Hajnoczi } 61704d4abe9SStefan Hajnoczi 618666daa68SMarkus Armbruster ret = bdrv_open(dinfo->bdrv, file, bdrv_flags, drv); 619666daa68SMarkus Armbruster if (ret < 0) { 620807105a7SMarkus Armbruster error_report("could not open disk image %s: %s", 621666daa68SMarkus Armbruster file, strerror(-ret)); 622a9ae2bffSMarkus Armbruster goto err; 623666daa68SMarkus Armbruster } 624666daa68SMarkus Armbruster 625666daa68SMarkus Armbruster if (bdrv_key_required(dinfo->bdrv)) 626666daa68SMarkus Armbruster autostart = 0; 627666daa68SMarkus Armbruster return dinfo; 628a9ae2bffSMarkus Armbruster 629a9ae2bffSMarkus Armbruster err: 630a9ae2bffSMarkus Armbruster bdrv_delete(dinfo->bdrv); 6317267c094SAnthony Liguori g_free(dinfo->id); 632a9ae2bffSMarkus Armbruster QTAILQ_REMOVE(&drives, dinfo, next); 6337267c094SAnthony Liguori g_free(dinfo); 634a9ae2bffSMarkus Armbruster return NULL; 635666daa68SMarkus Armbruster } 636666daa68SMarkus Armbruster 637666daa68SMarkus Armbruster void do_commit(Monitor *mon, const QDict *qdict) 638666daa68SMarkus Armbruster { 639666daa68SMarkus Armbruster const char *device = qdict_get_str(qdict, "device"); 6406ab4b5abSMarkus Armbruster BlockDriverState *bs; 6412d3735d3SStefan Hajnoczi int ret; 6422d3735d3SStefan Hajnoczi 643e8877497SStefan Hajnoczi if (!strcmp(device, "all")) { 644e8877497SStefan Hajnoczi ret = bdrv_commit_all(); 645e8877497SStefan Hajnoczi if (ret == -EBUSY) { 646e8877497SStefan Hajnoczi qerror_report(QERR_DEVICE_IN_USE, device); 647e8877497SStefan Hajnoczi return; 648e8877497SStefan Hajnoczi } 649e8877497SStefan Hajnoczi } else { 6506ab4b5abSMarkus Armbruster bs = bdrv_find(device); 651ac59eb95SMarkus Armbruster if (!bs) { 652ac59eb95SMarkus Armbruster qerror_report(QERR_DEVICE_NOT_FOUND, device); 653ac59eb95SMarkus Armbruster return; 6546ab4b5abSMarkus Armbruster } 6552d3735d3SStefan Hajnoczi ret = bdrv_commit(bs); 6562d3735d3SStefan Hajnoczi if (ret == -EBUSY) { 6572d3735d3SStefan Hajnoczi qerror_report(QERR_DEVICE_IN_USE, device); 6582d3735d3SStefan Hajnoczi return; 6592d3735d3SStefan Hajnoczi } 660666daa68SMarkus Armbruster } 661666daa68SMarkus Armbruster } 662666daa68SMarkus Armbruster 6636cc2a415SPaolo Bonzini static void blockdev_do_action(int kind, void *data, Error **errp) 6646cc2a415SPaolo Bonzini { 6656cc2a415SPaolo Bonzini BlockdevAction action; 6666cc2a415SPaolo Bonzini BlockdevActionList list; 6676cc2a415SPaolo Bonzini 6686cc2a415SPaolo Bonzini action.kind = kind; 6696cc2a415SPaolo Bonzini action.data = data; 6706cc2a415SPaolo Bonzini list.value = &action; 6716cc2a415SPaolo Bonzini list.next = NULL; 6726cc2a415SPaolo Bonzini qmp_transaction(&list, errp); 6736cc2a415SPaolo Bonzini } 6746cc2a415SPaolo Bonzini 6756106e249SLuiz Capitulino void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, 6766106e249SLuiz Capitulino bool has_format, const char *format, 6776cc2a415SPaolo Bonzini bool has_mode, enum NewImageMode mode, 6786106e249SLuiz Capitulino Error **errp) 679f8882568SJes Sorensen { 6806cc2a415SPaolo Bonzini BlockdevSnapshot snapshot = { 6816cc2a415SPaolo Bonzini .device = (char *) device, 6826cc2a415SPaolo Bonzini .snapshot_file = (char *) snapshot_file, 6836cc2a415SPaolo Bonzini .has_format = has_format, 6846cc2a415SPaolo Bonzini .format = (char *) format, 6856cc2a415SPaolo Bonzini .has_mode = has_mode, 6866cc2a415SPaolo Bonzini .mode = mode, 6876cc2a415SPaolo Bonzini }; 6886cc2a415SPaolo Bonzini blockdev_do_action(BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, &snapshot, 6896cc2a415SPaolo Bonzini errp); 690f8882568SJes Sorensen } 691f8882568SJes Sorensen 6928802d1fdSJeff Cody 6938802d1fdSJeff Cody /* New and old BlockDriverState structs for group snapshots */ 69452e7c241SPaolo Bonzini typedef struct BlkTransactionStates { 6958802d1fdSJeff Cody BlockDriverState *old_bs; 6968802d1fdSJeff Cody BlockDriverState *new_bs; 69752e7c241SPaolo Bonzini QSIMPLEQ_ENTRY(BlkTransactionStates) entry; 69852e7c241SPaolo Bonzini } BlkTransactionStates; 6998802d1fdSJeff Cody 7008802d1fdSJeff Cody /* 7018802d1fdSJeff Cody * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail 7028802d1fdSJeff Cody * then we do not pivot any of the devices in the group, and abandon the 7038802d1fdSJeff Cody * snapshots 7048802d1fdSJeff Cody */ 70552e7c241SPaolo Bonzini void qmp_transaction(BlockdevActionList *dev_list, Error **errp) 7068802d1fdSJeff Cody { 7078802d1fdSJeff Cody int ret = 0; 70852e7c241SPaolo Bonzini BlockdevActionList *dev_entry = dev_list; 70952e7c241SPaolo Bonzini BlkTransactionStates *states, *next; 71043e17041SLuiz Capitulino Error *local_err = NULL; 7118802d1fdSJeff Cody 71252e7c241SPaolo Bonzini QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionStates) snap_bdrv_states; 7138802d1fdSJeff Cody QSIMPLEQ_INIT(&snap_bdrv_states); 7148802d1fdSJeff Cody 7158802d1fdSJeff Cody /* drain all i/o before any snapshots */ 7168802d1fdSJeff Cody bdrv_drain_all(); 7178802d1fdSJeff Cody 7188802d1fdSJeff Cody /* We don't do anything in this loop that commits us to the snapshot */ 7198802d1fdSJeff Cody while (NULL != dev_entry) { 72052e7c241SPaolo Bonzini BlockdevAction *dev_info = NULL; 72152e7c241SPaolo Bonzini BlockDriver *proto_drv; 72252e7c241SPaolo Bonzini BlockDriver *drv; 72352e7c241SPaolo Bonzini int flags; 724bc8b094fSPaolo Bonzini enum NewImageMode mode; 725bc8b094fSPaolo Bonzini const char *new_image_file; 72652e7c241SPaolo Bonzini const char *device; 72752e7c241SPaolo Bonzini const char *format = "qcow2"; 72852e7c241SPaolo Bonzini 7298802d1fdSJeff Cody dev_info = dev_entry->value; 7308802d1fdSJeff Cody dev_entry = dev_entry->next; 7318802d1fdSJeff Cody 73252e7c241SPaolo Bonzini states = g_malloc0(sizeof(BlkTransactionStates)); 7338802d1fdSJeff Cody QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, states, entry); 7348802d1fdSJeff Cody 73552e7c241SPaolo Bonzini switch (dev_info->kind) { 73652e7c241SPaolo Bonzini case BLOCKDEV_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 73752e7c241SPaolo Bonzini device = dev_info->blockdev_snapshot_sync->device; 738bc8b094fSPaolo Bonzini if (!dev_info->blockdev_snapshot_sync->has_mode) { 739bc8b094fSPaolo Bonzini dev_info->blockdev_snapshot_sync->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 740bc8b094fSPaolo Bonzini } 741bc8b094fSPaolo Bonzini new_image_file = dev_info->blockdev_snapshot_sync->snapshot_file; 74252e7c241SPaolo Bonzini if (dev_info->blockdev_snapshot_sync->has_format) { 74352e7c241SPaolo Bonzini format = dev_info->blockdev_snapshot_sync->format; 74452e7c241SPaolo Bonzini } 745bc8b094fSPaolo Bonzini mode = dev_info->blockdev_snapshot_sync->mode; 74652e7c241SPaolo Bonzini break; 74752e7c241SPaolo Bonzini default: 74852e7c241SPaolo Bonzini abort(); 74952e7c241SPaolo Bonzini } 7508802d1fdSJeff Cody 75152e7c241SPaolo Bonzini drv = bdrv_find_format(format); 75252e7c241SPaolo Bonzini if (!drv) { 75352e7c241SPaolo Bonzini error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 75452e7c241SPaolo Bonzini goto delete_and_fail; 75552e7c241SPaolo Bonzini } 75652e7c241SPaolo Bonzini 75752e7c241SPaolo Bonzini states->old_bs = bdrv_find(device); 7588802d1fdSJeff Cody if (!states->old_bs) { 75952e7c241SPaolo Bonzini error_set(errp, QERR_DEVICE_NOT_FOUND, device); 7608802d1fdSJeff Cody goto delete_and_fail; 7618802d1fdSJeff Cody } 7628802d1fdSJeff Cody 763e86fe18aSPaolo Bonzini if (!bdrv_is_inserted(states->old_bs)) { 764e86fe18aSPaolo Bonzini error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 765e86fe18aSPaolo Bonzini goto delete_and_fail; 766e86fe18aSPaolo Bonzini } 767e86fe18aSPaolo Bonzini 7688802d1fdSJeff Cody if (bdrv_in_use(states->old_bs)) { 76952e7c241SPaolo Bonzini error_set(errp, QERR_DEVICE_IN_USE, device); 7708802d1fdSJeff Cody goto delete_and_fail; 7718802d1fdSJeff Cody } 7728802d1fdSJeff Cody 773e86fe18aSPaolo Bonzini if (!bdrv_is_read_only(states->old_bs)) { 7748802d1fdSJeff Cody if (bdrv_flush(states->old_bs)) { 7758802d1fdSJeff Cody error_set(errp, QERR_IO_ERROR); 7768802d1fdSJeff Cody goto delete_and_fail; 7778802d1fdSJeff Cody } 7788802d1fdSJeff Cody } 7798802d1fdSJeff Cody 7808802d1fdSJeff Cody flags = states->old_bs->open_flags; 7818802d1fdSJeff Cody 78252e7c241SPaolo Bonzini proto_drv = bdrv_find_protocol(new_image_file); 7838802d1fdSJeff Cody if (!proto_drv) { 7848802d1fdSJeff Cody error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 7858802d1fdSJeff Cody goto delete_and_fail; 7868802d1fdSJeff Cody } 7878802d1fdSJeff Cody 7888802d1fdSJeff Cody /* create new image w/backing file */ 789bc8b094fSPaolo Bonzini if (mode != NEW_IMAGE_MODE_EXISTING) { 79043e17041SLuiz Capitulino bdrv_img_create(new_image_file, format, 7918802d1fdSJeff Cody states->old_bs->filename, 792d0895d6eSPaolo Bonzini states->old_bs->drv->format_name, 79343e17041SLuiz Capitulino NULL, -1, flags, &local_err); 79443e17041SLuiz Capitulino if (error_is_set(&local_err)) { 79543e17041SLuiz Capitulino error_propagate(errp, local_err); 7968802d1fdSJeff Cody goto delete_and_fail; 7978802d1fdSJeff Cody } 798bc8b094fSPaolo Bonzini } 7998802d1fdSJeff Cody 8008802d1fdSJeff Cody /* We will manually add the backing_hd field to the bs later */ 8018802d1fdSJeff Cody states->new_bs = bdrv_new(""); 80252e7c241SPaolo Bonzini ret = bdrv_open(states->new_bs, new_image_file, 8038802d1fdSJeff Cody flags | BDRV_O_NO_BACKING, drv); 8048802d1fdSJeff Cody if (ret != 0) { 80552e7c241SPaolo Bonzini error_set(errp, QERR_OPEN_FILE_FAILED, new_image_file); 8068802d1fdSJeff Cody goto delete_and_fail; 8078802d1fdSJeff Cody } 8088802d1fdSJeff Cody } 8098802d1fdSJeff Cody 8108802d1fdSJeff Cody 8118802d1fdSJeff Cody /* Now we are going to do the actual pivot. Everything up to this point 8128802d1fdSJeff Cody * is reversible, but we are committed at this point */ 8138802d1fdSJeff Cody QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { 8148802d1fdSJeff Cody /* This removes our old bs from the bdrv_states, and adds the new bs */ 8158802d1fdSJeff Cody bdrv_append(states->new_bs, states->old_bs); 816870f5681SJeff Cody /* We don't need (or want) to use the transactional 817870f5681SJeff Cody * bdrv_reopen_multiple() across all the entries at once, because we 818870f5681SJeff Cody * don't want to abort all of them if one of them fails the reopen */ 819870f5681SJeff Cody bdrv_reopen(states->new_bs, states->new_bs->open_flags & ~BDRV_O_RDWR, 820870f5681SJeff Cody NULL); 8218802d1fdSJeff Cody } 8228802d1fdSJeff Cody 8238802d1fdSJeff Cody /* success */ 8248802d1fdSJeff Cody goto exit; 8258802d1fdSJeff Cody 8268802d1fdSJeff Cody delete_and_fail: 8278802d1fdSJeff Cody /* 8288802d1fdSJeff Cody * failure, and it is all-or-none; abandon each new bs, and keep using 8298802d1fdSJeff Cody * the original bs for all images 8308802d1fdSJeff Cody */ 8318802d1fdSJeff Cody QSIMPLEQ_FOREACH(states, &snap_bdrv_states, entry) { 8328802d1fdSJeff Cody if (states->new_bs) { 8338802d1fdSJeff Cody bdrv_delete(states->new_bs); 8348802d1fdSJeff Cody } 8358802d1fdSJeff Cody } 8368802d1fdSJeff Cody exit: 837622d2419SPaolo Bonzini QSIMPLEQ_FOREACH_SAFE(states, &snap_bdrv_states, entry, next) { 8388802d1fdSJeff Cody g_free(states); 8398802d1fdSJeff Cody } 8408802d1fdSJeff Cody } 8418802d1fdSJeff Cody 8428802d1fdSJeff Cody 84392d48558SLuiz Capitulino static void eject_device(BlockDriverState *bs, int force, Error **errp) 844666daa68SMarkus Armbruster { 8452d3735d3SStefan Hajnoczi if (bdrv_in_use(bs)) { 8462d3735d3SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 8472d3735d3SStefan Hajnoczi return; 8482d3735d3SStefan Hajnoczi } 8492c6942faSMarkus Armbruster if (!bdrv_dev_has_removable_media(bs)) { 85092d48558SLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs)); 85192d48558SLuiz Capitulino return; 852666daa68SMarkus Armbruster } 85392d48558SLuiz Capitulino 854025ccaa7SPaolo Bonzini if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) { 855025ccaa7SPaolo Bonzini bdrv_dev_eject_request(bs, force); 856025ccaa7SPaolo Bonzini if (!force) { 85792d48558SLuiz Capitulino error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); 85892d48558SLuiz Capitulino return; 859666daa68SMarkus Armbruster } 860025ccaa7SPaolo Bonzini } 86192d48558SLuiz Capitulino 862666daa68SMarkus Armbruster bdrv_close(bs); 863666daa68SMarkus Armbruster } 864666daa68SMarkus Armbruster 865c245b6a3SLuiz Capitulino void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 866666daa68SMarkus Armbruster { 867666daa68SMarkus Armbruster BlockDriverState *bs; 868666daa68SMarkus Armbruster 869c245b6a3SLuiz Capitulino bs = bdrv_find(device); 870666daa68SMarkus Armbruster if (!bs) { 871c245b6a3SLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_FOUND, device); 872c245b6a3SLuiz Capitulino return; 873666daa68SMarkus Armbruster } 87492d48558SLuiz Capitulino 875c245b6a3SLuiz Capitulino eject_device(bs, force, errp); 876666daa68SMarkus Armbruster } 877666daa68SMarkus Armbruster 878a4dea8a9SLuiz Capitulino void qmp_block_passwd(const char *device, const char *password, Error **errp) 879666daa68SMarkus Armbruster { 880666daa68SMarkus Armbruster BlockDriverState *bs; 881666daa68SMarkus Armbruster int err; 882666daa68SMarkus Armbruster 883a4dea8a9SLuiz Capitulino bs = bdrv_find(device); 884666daa68SMarkus Armbruster if (!bs) { 885a4dea8a9SLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_FOUND, device); 886a4dea8a9SLuiz Capitulino return; 887666daa68SMarkus Armbruster } 888666daa68SMarkus Armbruster 889a4dea8a9SLuiz Capitulino err = bdrv_set_key(bs, password); 890666daa68SMarkus Armbruster if (err == -EINVAL) { 891a4dea8a9SLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); 892a4dea8a9SLuiz Capitulino return; 893666daa68SMarkus Armbruster } else if (err < 0) { 894a4dea8a9SLuiz Capitulino error_set(errp, QERR_INVALID_PASSWORD); 895a4dea8a9SLuiz Capitulino return; 896666daa68SMarkus Armbruster } 897666daa68SMarkus Armbruster } 898666daa68SMarkus Armbruster 899333a96ecSLuiz Capitulino static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, 900333a96ecSLuiz Capitulino int bdrv_flags, BlockDriver *drv, 901333a96ecSLuiz Capitulino const char *password, Error **errp) 902333a96ecSLuiz Capitulino { 903333a96ecSLuiz Capitulino if (bdrv_open(bs, filename, bdrv_flags, drv) < 0) { 904333a96ecSLuiz Capitulino error_set(errp, QERR_OPEN_FILE_FAILED, filename); 905333a96ecSLuiz Capitulino return; 906333a96ecSLuiz Capitulino } 907333a96ecSLuiz Capitulino 908333a96ecSLuiz Capitulino if (bdrv_key_required(bs)) { 909333a96ecSLuiz Capitulino if (password) { 910333a96ecSLuiz Capitulino if (bdrv_set_key(bs, password) < 0) { 911333a96ecSLuiz Capitulino error_set(errp, QERR_INVALID_PASSWORD); 912333a96ecSLuiz Capitulino } 913333a96ecSLuiz Capitulino } else { 914333a96ecSLuiz Capitulino error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), 915333a96ecSLuiz Capitulino bdrv_get_encrypted_filename(bs)); 916333a96ecSLuiz Capitulino } 917333a96ecSLuiz Capitulino } else if (password) { 918333a96ecSLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); 919333a96ecSLuiz Capitulino } 920333a96ecSLuiz Capitulino } 921333a96ecSLuiz Capitulino 922333a96ecSLuiz Capitulino void qmp_change_blockdev(const char *device, const char *filename, 923333a96ecSLuiz Capitulino bool has_format, const char *format, Error **errp) 924666daa68SMarkus Armbruster { 925666daa68SMarkus Armbruster BlockDriverState *bs; 926666daa68SMarkus Armbruster BlockDriver *drv = NULL; 927666daa68SMarkus Armbruster int bdrv_flags; 92892d48558SLuiz Capitulino Error *err = NULL; 929666daa68SMarkus Armbruster 930666daa68SMarkus Armbruster bs = bdrv_find(device); 931666daa68SMarkus Armbruster if (!bs) { 932333a96ecSLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_FOUND, device); 933333a96ecSLuiz Capitulino return; 934666daa68SMarkus Armbruster } 935333a96ecSLuiz Capitulino 936333a96ecSLuiz Capitulino if (format) { 937333a96ecSLuiz Capitulino drv = bdrv_find_whitelisted_format(format); 938666daa68SMarkus Armbruster if (!drv) { 939333a96ecSLuiz Capitulino error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 940333a96ecSLuiz Capitulino return; 941666daa68SMarkus Armbruster } 942666daa68SMarkus Armbruster } 943333a96ecSLuiz Capitulino 94492d48558SLuiz Capitulino eject_device(bs, 0, &err); 94592d48558SLuiz Capitulino if (error_is_set(&err)) { 946333a96ecSLuiz Capitulino error_propagate(errp, err); 947333a96ecSLuiz Capitulino return; 948666daa68SMarkus Armbruster } 949333a96ecSLuiz Capitulino 950528f7663SMarkus Armbruster bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; 951199630b6SBlue Swirl bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; 952333a96ecSLuiz Capitulino 953333a96ecSLuiz Capitulino qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); 954666daa68SMarkus Armbruster } 9559063f814SRyan Harper 956727f005eSZhi Yong Wu /* throttling disk I/O limits */ 95780047da5SLuiz Capitulino void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 95880047da5SLuiz Capitulino int64_t bps_wr, int64_t iops, int64_t iops_rd, 95980047da5SLuiz Capitulino int64_t iops_wr, Error **errp) 960727f005eSZhi Yong Wu { 961727f005eSZhi Yong Wu BlockIOLimit io_limits; 962727f005eSZhi Yong Wu BlockDriverState *bs; 963727f005eSZhi Yong Wu 96480047da5SLuiz Capitulino bs = bdrv_find(device); 965727f005eSZhi Yong Wu if (!bs) { 96680047da5SLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_FOUND, device); 96780047da5SLuiz Capitulino return; 968727f005eSZhi Yong Wu } 969727f005eSZhi Yong Wu 97080047da5SLuiz Capitulino io_limits.bps[BLOCK_IO_LIMIT_TOTAL] = bps; 97180047da5SLuiz Capitulino io_limits.bps[BLOCK_IO_LIMIT_READ] = bps_rd; 97280047da5SLuiz Capitulino io_limits.bps[BLOCK_IO_LIMIT_WRITE] = bps_wr; 97380047da5SLuiz Capitulino io_limits.iops[BLOCK_IO_LIMIT_TOTAL]= iops; 97480047da5SLuiz Capitulino io_limits.iops[BLOCK_IO_LIMIT_READ] = iops_rd; 97580047da5SLuiz Capitulino io_limits.iops[BLOCK_IO_LIMIT_WRITE]= iops_wr; 976727f005eSZhi Yong Wu 977727f005eSZhi Yong Wu if (!do_check_io_limits(&io_limits)) { 97880047da5SLuiz Capitulino error_set(errp, QERR_INVALID_PARAMETER_COMBINATION); 97980047da5SLuiz Capitulino return; 980727f005eSZhi Yong Wu } 981727f005eSZhi Yong Wu 982727f005eSZhi Yong Wu bs->io_limits = io_limits; 983727f005eSZhi Yong Wu bs->slice_time = BLOCK_IO_SLICE_TIME; 984727f005eSZhi Yong Wu 985727f005eSZhi Yong Wu if (!bs->io_limits_enabled && bdrv_io_limits_enabled(bs)) { 986727f005eSZhi Yong Wu bdrv_io_limits_enable(bs); 987727f005eSZhi Yong Wu } else if (bs->io_limits_enabled && !bdrv_io_limits_enabled(bs)) { 988727f005eSZhi Yong Wu bdrv_io_limits_disable(bs); 989727f005eSZhi Yong Wu } else { 990727f005eSZhi Yong Wu if (bs->block_timer) { 991727f005eSZhi Yong Wu qemu_mod_timer(bs->block_timer, qemu_get_clock_ns(vm_clock)); 992727f005eSZhi Yong Wu } 993727f005eSZhi Yong Wu } 994727f005eSZhi Yong Wu } 995727f005eSZhi Yong Wu 9969063f814SRyan Harper int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) 9979063f814SRyan Harper { 9989063f814SRyan Harper const char *id = qdict_get_str(qdict, "id"); 9999063f814SRyan Harper BlockDriverState *bs; 10009063f814SRyan Harper 10019063f814SRyan Harper bs = bdrv_find(id); 10029063f814SRyan Harper if (!bs) { 10039063f814SRyan Harper qerror_report(QERR_DEVICE_NOT_FOUND, id); 10049063f814SRyan Harper return -1; 10059063f814SRyan Harper } 10068591675fSMarcelo Tosatti if (bdrv_in_use(bs)) { 10078591675fSMarcelo Tosatti qerror_report(QERR_DEVICE_IN_USE, id); 10088591675fSMarcelo Tosatti return -1; 10098591675fSMarcelo Tosatti } 10109063f814SRyan Harper 10119063f814SRyan Harper /* quiesce block driver; prevent further io */ 1012922453bcSStefan Hajnoczi bdrv_drain_all(); 10139063f814SRyan Harper bdrv_flush(bs); 10149063f814SRyan Harper bdrv_close(bs); 10159063f814SRyan Harper 1016fa879d62SMarkus Armbruster /* if we have a device attached to this BlockDriverState 1017d22b2f41SRyan Harper * then we need to make the drive anonymous until the device 1018d22b2f41SRyan Harper * can be removed. If this is a drive with no device backing 1019d22b2f41SRyan Harper * then we can just get rid of the block driver state right here. 1020d22b2f41SRyan Harper */ 1021fa879d62SMarkus Armbruster if (bdrv_get_attached_dev(bs)) { 1022d22b2f41SRyan Harper bdrv_make_anon(bs); 1023d22b2f41SRyan Harper } else { 10249063f814SRyan Harper drive_uninit(drive_get_by_blockdev(bs)); 1025d22b2f41SRyan Harper } 10269063f814SRyan Harper 10279063f814SRyan Harper return 0; 10289063f814SRyan Harper } 10296d4a2b3aSChristoph Hellwig 10305e7caacbSLuiz Capitulino void qmp_block_resize(const char *device, int64_t size, Error **errp) 10316d4a2b3aSChristoph Hellwig { 10326d4a2b3aSChristoph Hellwig BlockDriverState *bs; 10336d4a2b3aSChristoph Hellwig 10346d4a2b3aSChristoph Hellwig bs = bdrv_find(device); 10356d4a2b3aSChristoph Hellwig if (!bs) { 10365e7caacbSLuiz Capitulino error_set(errp, QERR_DEVICE_NOT_FOUND, device); 10375e7caacbSLuiz Capitulino return; 10386d4a2b3aSChristoph Hellwig } 10396d4a2b3aSChristoph Hellwig 10406d4a2b3aSChristoph Hellwig if (size < 0) { 1041939a1cc3SStefan Hajnoczi error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 10425e7caacbSLuiz Capitulino return; 10436d4a2b3aSChristoph Hellwig } 10446d4a2b3aSChristoph Hellwig 1045939a1cc3SStefan Hajnoczi switch (bdrv_truncate(bs, size)) { 1046939a1cc3SStefan Hajnoczi case 0: 1047939a1cc3SStefan Hajnoczi break; 1048939a1cc3SStefan Hajnoczi case -ENOMEDIUM: 1049939a1cc3SStefan Hajnoczi error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1050939a1cc3SStefan Hajnoczi break; 1051939a1cc3SStefan Hajnoczi case -ENOTSUP: 1052939a1cc3SStefan Hajnoczi error_set(errp, QERR_UNSUPPORTED); 1053939a1cc3SStefan Hajnoczi break; 1054939a1cc3SStefan Hajnoczi case -EACCES: 1055939a1cc3SStefan Hajnoczi error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); 1056939a1cc3SStefan Hajnoczi break; 1057939a1cc3SStefan Hajnoczi case -EBUSY: 1058939a1cc3SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, device); 1059939a1cc3SStefan Hajnoczi break; 1060939a1cc3SStefan Hajnoczi default: 10615e7caacbSLuiz Capitulino error_set(errp, QERR_UNDEFINED_ERROR); 1062939a1cc3SStefan Hajnoczi break; 10636d4a2b3aSChristoph Hellwig } 10646d4a2b3aSChristoph Hellwig } 106512bd451fSStefan Hajnoczi 10669abf2dbaSJeff Cody static void block_job_cb(void *opaque, int ret) 106712bd451fSStefan Hajnoczi { 106812bd451fSStefan Hajnoczi BlockDriverState *bs = opaque; 106912bd451fSStefan Hajnoczi QObject *obj; 107012bd451fSStefan Hajnoczi 10719abf2dbaSJeff Cody trace_block_job_cb(bs, bs->job, ret); 107212bd451fSStefan Hajnoczi 107312bd451fSStefan Hajnoczi assert(bs->job); 107412bd451fSStefan Hajnoczi obj = qobject_from_block_job(bs->job); 107512bd451fSStefan Hajnoczi if (ret < 0) { 107612bd451fSStefan Hajnoczi QDict *dict = qobject_to_qdict(obj); 107712bd451fSStefan Hajnoczi qdict_put(dict, "error", qstring_from_str(strerror(-ret))); 107812bd451fSStefan Hajnoczi } 107912bd451fSStefan Hajnoczi 1080370521a1SStefan Hajnoczi if (block_job_is_cancelled(bs->job)) { 1081370521a1SStefan Hajnoczi monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj); 1082370521a1SStefan Hajnoczi } else { 108312bd451fSStefan Hajnoczi monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj); 1084370521a1SStefan Hajnoczi } 108512bd451fSStefan Hajnoczi qobject_decref(obj); 1086aa398a5cSStefan Hajnoczi 1087aa398a5cSStefan Hajnoczi drive_put_ref_bh_schedule(drive_get_by_blockdev(bs)); 108812bd451fSStefan Hajnoczi } 108912bd451fSStefan Hajnoczi 109012bd451fSStefan Hajnoczi void qmp_block_stream(const char *device, bool has_base, 10911d809098SPaolo Bonzini const char *base, bool has_speed, int64_t speed, 10921d809098SPaolo Bonzini bool has_on_error, BlockdevOnError on_error, 10931d809098SPaolo Bonzini Error **errp) 109412bd451fSStefan Hajnoczi { 109512bd451fSStefan Hajnoczi BlockDriverState *bs; 1096c8c3080fSMarcelo Tosatti BlockDriverState *base_bs = NULL; 1097fd7f8c65SStefan Hajnoczi Error *local_err = NULL; 109812bd451fSStefan Hajnoczi 10991d809098SPaolo Bonzini if (!has_on_error) { 11001d809098SPaolo Bonzini on_error = BLOCKDEV_ON_ERROR_REPORT; 11011d809098SPaolo Bonzini } 11021d809098SPaolo Bonzini 110312bd451fSStefan Hajnoczi bs = bdrv_find(device); 110412bd451fSStefan Hajnoczi if (!bs) { 110512bd451fSStefan Hajnoczi error_set(errp, QERR_DEVICE_NOT_FOUND, device); 110612bd451fSStefan Hajnoczi return; 110712bd451fSStefan Hajnoczi } 110812bd451fSStefan Hajnoczi 110912bd451fSStefan Hajnoczi if (base) { 1110c8c3080fSMarcelo Tosatti base_bs = bdrv_find_backing_image(bs, base); 1111c8c3080fSMarcelo Tosatti if (base_bs == NULL) { 1112c8c3080fSMarcelo Tosatti error_set(errp, QERR_BASE_NOT_FOUND, base); 111312bd451fSStefan Hajnoczi return; 111412bd451fSStefan Hajnoczi } 1115c8c3080fSMarcelo Tosatti } 111612bd451fSStefan Hajnoczi 1117c83c66c3SStefan Hajnoczi stream_start(bs, base_bs, base, has_speed ? speed : 0, 11181d809098SPaolo Bonzini on_error, block_job_cb, bs, &local_err); 1119fd7f8c65SStefan Hajnoczi if (error_is_set(&local_err)) { 1120fd7f8c65SStefan Hajnoczi error_propagate(errp, local_err); 112112bd451fSStefan Hajnoczi return; 112212bd451fSStefan Hajnoczi } 112312bd451fSStefan Hajnoczi 1124aa398a5cSStefan Hajnoczi /* Grab a reference so hotplug does not delete the BlockDriverState from 1125aa398a5cSStefan Hajnoczi * underneath us. 1126aa398a5cSStefan Hajnoczi */ 1127aa398a5cSStefan Hajnoczi drive_get_ref(drive_get_by_blockdev(bs)); 1128aa398a5cSStefan Hajnoczi 112912bd451fSStefan Hajnoczi trace_qmp_block_stream(bs, bs->job); 113012bd451fSStefan Hajnoczi } 11312d47c6e9SStefan Hajnoczi 1132ed61fc10SJeff Cody void qmp_block_commit(const char *device, 1133ed61fc10SJeff Cody bool has_base, const char *base, const char *top, 1134ed61fc10SJeff Cody bool has_speed, int64_t speed, 1135ed61fc10SJeff Cody Error **errp) 1136ed61fc10SJeff Cody { 1137ed61fc10SJeff Cody BlockDriverState *bs; 1138ed61fc10SJeff Cody BlockDriverState *base_bs, *top_bs; 1139ed61fc10SJeff Cody Error *local_err = NULL; 1140ed61fc10SJeff Cody /* This will be part of the QMP command, if/when the 1141ed61fc10SJeff Cody * BlockdevOnError change for blkmirror makes it in 1142ed61fc10SJeff Cody */ 114392aa5c6dSPaolo Bonzini BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 1144ed61fc10SJeff Cody 1145ed61fc10SJeff Cody /* drain all i/o before commits */ 1146ed61fc10SJeff Cody bdrv_drain_all(); 1147ed61fc10SJeff Cody 1148ed61fc10SJeff Cody bs = bdrv_find(device); 1149ed61fc10SJeff Cody if (!bs) { 1150ed61fc10SJeff Cody error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1151ed61fc10SJeff Cody return; 1152ed61fc10SJeff Cody } 1153ed61fc10SJeff Cody 1154ed61fc10SJeff Cody /* default top_bs is the active layer */ 1155ed61fc10SJeff Cody top_bs = bs; 1156ed61fc10SJeff Cody 1157ed61fc10SJeff Cody if (top) { 1158ed61fc10SJeff Cody if (strcmp(bs->filename, top) != 0) { 1159ed61fc10SJeff Cody top_bs = bdrv_find_backing_image(bs, top); 1160ed61fc10SJeff Cody } 1161ed61fc10SJeff Cody } 1162ed61fc10SJeff Cody 1163ed61fc10SJeff Cody if (top_bs == NULL) { 1164ed61fc10SJeff Cody error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 1165ed61fc10SJeff Cody return; 1166ed61fc10SJeff Cody } 1167ed61fc10SJeff Cody 1168d5208c45SJeff Cody if (has_base && base) { 1169d5208c45SJeff Cody base_bs = bdrv_find_backing_image(top_bs, base); 1170d5208c45SJeff Cody } else { 1171d5208c45SJeff Cody base_bs = bdrv_find_base(top_bs); 1172d5208c45SJeff Cody } 1173d5208c45SJeff Cody 1174d5208c45SJeff Cody if (base_bs == NULL) { 1175d5208c45SJeff Cody error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 1176d5208c45SJeff Cody return; 1177d5208c45SJeff Cody } 1178d5208c45SJeff Cody 1179ed61fc10SJeff Cody commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 1180ed61fc10SJeff Cody &local_err); 1181ed61fc10SJeff Cody if (local_err != NULL) { 1182ed61fc10SJeff Cody error_propagate(errp, local_err); 1183ed61fc10SJeff Cody return; 1184ed61fc10SJeff Cody } 1185ed61fc10SJeff Cody /* Grab a reference so hotplug does not delete the BlockDriverState from 1186ed61fc10SJeff Cody * underneath us. 1187ed61fc10SJeff Cody */ 1188ed61fc10SJeff Cody drive_get_ref(drive_get_by_blockdev(bs)); 1189ed61fc10SJeff Cody } 1190ed61fc10SJeff Cody 1191d9b902dbSPaolo Bonzini void qmp_drive_mirror(const char *device, const char *target, 1192d9b902dbSPaolo Bonzini bool has_format, const char *format, 1193d9b902dbSPaolo Bonzini enum MirrorSyncMode sync, 1194d9b902dbSPaolo Bonzini bool has_mode, enum NewImageMode mode, 1195b952b558SPaolo Bonzini bool has_speed, int64_t speed, 1196b952b558SPaolo Bonzini bool has_on_source_error, BlockdevOnError on_source_error, 1197b952b558SPaolo Bonzini bool has_on_target_error, BlockdevOnError on_target_error, 1198b952b558SPaolo Bonzini Error **errp) 1199d9b902dbSPaolo Bonzini { 1200d9b902dbSPaolo Bonzini BlockDriverInfo bdi; 1201d9b902dbSPaolo Bonzini BlockDriverState *bs; 1202d9b902dbSPaolo Bonzini BlockDriverState *source, *target_bs; 1203d9b902dbSPaolo Bonzini BlockDriver *proto_drv; 1204d9b902dbSPaolo Bonzini BlockDriver *drv = NULL; 1205d9b902dbSPaolo Bonzini Error *local_err = NULL; 1206d9b902dbSPaolo Bonzini int flags; 1207d9b902dbSPaolo Bonzini uint64_t size; 1208d9b902dbSPaolo Bonzini int ret; 1209d9b902dbSPaolo Bonzini 1210d9b902dbSPaolo Bonzini if (!has_speed) { 1211d9b902dbSPaolo Bonzini speed = 0; 1212d9b902dbSPaolo Bonzini } 1213b952b558SPaolo Bonzini if (!has_on_source_error) { 1214b952b558SPaolo Bonzini on_source_error = BLOCKDEV_ON_ERROR_REPORT; 1215b952b558SPaolo Bonzini } 1216b952b558SPaolo Bonzini if (!has_on_target_error) { 1217b952b558SPaolo Bonzini on_target_error = BLOCKDEV_ON_ERROR_REPORT; 1218b952b558SPaolo Bonzini } 1219d9b902dbSPaolo Bonzini if (!has_mode) { 1220d9b902dbSPaolo Bonzini mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1221d9b902dbSPaolo Bonzini } 1222d9b902dbSPaolo Bonzini 1223d9b902dbSPaolo Bonzini bs = bdrv_find(device); 1224d9b902dbSPaolo Bonzini if (!bs) { 1225d9b902dbSPaolo Bonzini error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1226d9b902dbSPaolo Bonzini return; 1227d9b902dbSPaolo Bonzini } 1228d9b902dbSPaolo Bonzini 1229d9b902dbSPaolo Bonzini if (!bdrv_is_inserted(bs)) { 1230d9b902dbSPaolo Bonzini error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1231d9b902dbSPaolo Bonzini return; 1232d9b902dbSPaolo Bonzini } 1233d9b902dbSPaolo Bonzini 1234d9b902dbSPaolo Bonzini if (!has_format) { 1235d9b902dbSPaolo Bonzini format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 1236d9b902dbSPaolo Bonzini } 1237d9b902dbSPaolo Bonzini if (format) { 1238d9b902dbSPaolo Bonzini drv = bdrv_find_format(format); 1239d9b902dbSPaolo Bonzini if (!drv) { 1240d9b902dbSPaolo Bonzini error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1241d9b902dbSPaolo Bonzini return; 1242d9b902dbSPaolo Bonzini } 1243d9b902dbSPaolo Bonzini } 1244d9b902dbSPaolo Bonzini 1245d9b902dbSPaolo Bonzini if (bdrv_in_use(bs)) { 1246d9b902dbSPaolo Bonzini error_set(errp, QERR_DEVICE_IN_USE, device); 1247d9b902dbSPaolo Bonzini return; 1248d9b902dbSPaolo Bonzini } 1249d9b902dbSPaolo Bonzini 1250d9b902dbSPaolo Bonzini flags = bs->open_flags | BDRV_O_RDWR; 1251d9b902dbSPaolo Bonzini source = bs->backing_hd; 1252d9b902dbSPaolo Bonzini if (!source && sync == MIRROR_SYNC_MODE_TOP) { 1253d9b902dbSPaolo Bonzini sync = MIRROR_SYNC_MODE_FULL; 1254d9b902dbSPaolo Bonzini } 1255d9b902dbSPaolo Bonzini 1256d9b902dbSPaolo Bonzini proto_drv = bdrv_find_protocol(target); 1257d9b902dbSPaolo Bonzini if (!proto_drv) { 1258d9b902dbSPaolo Bonzini error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1259d9b902dbSPaolo Bonzini return; 1260d9b902dbSPaolo Bonzini } 1261d9b902dbSPaolo Bonzini 1262d9b902dbSPaolo Bonzini if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) { 1263d9b902dbSPaolo Bonzini /* create new image w/o backing file */ 1264d9b902dbSPaolo Bonzini assert(format && drv); 1265d9b902dbSPaolo Bonzini bdrv_get_geometry(bs, &size); 1266d9b902dbSPaolo Bonzini size *= 512; 1267cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 1268cf8f2426SLuiz Capitulino NULL, NULL, NULL, size, flags, &local_err); 1269d9b902dbSPaolo Bonzini } else { 1270d9b902dbSPaolo Bonzini switch (mode) { 1271d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_EXISTING: 1272d9b902dbSPaolo Bonzini ret = 0; 1273d9b902dbSPaolo Bonzini break; 1274d9b902dbSPaolo Bonzini case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 1275d9b902dbSPaolo Bonzini /* create new image with backing file */ 1276cf8f2426SLuiz Capitulino bdrv_img_create(target, format, 1277d9b902dbSPaolo Bonzini source->filename, 1278d9b902dbSPaolo Bonzini source->drv->format_name, 1279cf8f2426SLuiz Capitulino NULL, -1, flags, &local_err); 1280d9b902dbSPaolo Bonzini break; 1281d9b902dbSPaolo Bonzini default: 1282d9b902dbSPaolo Bonzini abort(); 1283d9b902dbSPaolo Bonzini } 1284d9b902dbSPaolo Bonzini } 1285d9b902dbSPaolo Bonzini 1286cf8f2426SLuiz Capitulino if (error_is_set(&local_err)) { 1287cf8f2426SLuiz Capitulino error_propagate(errp, local_err); 1288d9b902dbSPaolo Bonzini return; 1289d9b902dbSPaolo Bonzini } 1290d9b902dbSPaolo Bonzini 1291d9b902dbSPaolo Bonzini target_bs = bdrv_new(""); 1292d9b902dbSPaolo Bonzini ret = bdrv_open(target_bs, target, flags | BDRV_O_NO_BACKING, drv); 1293d9b902dbSPaolo Bonzini 1294d9b902dbSPaolo Bonzini if (ret < 0) { 1295d9b902dbSPaolo Bonzini bdrv_delete(target_bs); 1296d9b902dbSPaolo Bonzini error_set(errp, QERR_OPEN_FILE_FAILED, target); 1297d9b902dbSPaolo Bonzini return; 1298d9b902dbSPaolo Bonzini } 1299d9b902dbSPaolo Bonzini 1300d9b902dbSPaolo Bonzini /* We need a backing file if we will copy parts of a cluster. */ 1301d9b902dbSPaolo Bonzini if (bdrv_get_info(target_bs, &bdi) >= 0 && bdi.cluster_size != 0 && 1302d9b902dbSPaolo Bonzini bdi.cluster_size >= BDRV_SECTORS_PER_DIRTY_CHUNK * 512) { 1303d9b902dbSPaolo Bonzini ret = bdrv_open_backing_file(target_bs); 1304d9b902dbSPaolo Bonzini if (ret < 0) { 1305d9b902dbSPaolo Bonzini bdrv_delete(target_bs); 1306d9b902dbSPaolo Bonzini error_set(errp, QERR_OPEN_FILE_FAILED, target); 1307d9b902dbSPaolo Bonzini return; 1308d9b902dbSPaolo Bonzini } 1309d9b902dbSPaolo Bonzini } 1310d9b902dbSPaolo Bonzini 1311b952b558SPaolo Bonzini mirror_start(bs, target_bs, speed, sync, on_source_error, on_target_error, 1312b952b558SPaolo Bonzini block_job_cb, bs, &local_err); 1313d9b902dbSPaolo Bonzini if (local_err != NULL) { 1314d9b902dbSPaolo Bonzini bdrv_delete(target_bs); 1315d9b902dbSPaolo Bonzini error_propagate(errp, local_err); 1316d9b902dbSPaolo Bonzini return; 1317d9b902dbSPaolo Bonzini } 1318d9b902dbSPaolo Bonzini 1319d9b902dbSPaolo Bonzini /* Grab a reference so hotplug does not delete the BlockDriverState from 1320d9b902dbSPaolo Bonzini * underneath us. 1321d9b902dbSPaolo Bonzini */ 1322d9b902dbSPaolo Bonzini drive_get_ref(drive_get_by_blockdev(bs)); 1323d9b902dbSPaolo Bonzini } 1324d9b902dbSPaolo Bonzini 13252d47c6e9SStefan Hajnoczi static BlockJob *find_block_job(const char *device) 13262d47c6e9SStefan Hajnoczi { 13272d47c6e9SStefan Hajnoczi BlockDriverState *bs; 13282d47c6e9SStefan Hajnoczi 13292d47c6e9SStefan Hajnoczi bs = bdrv_find(device); 13302d47c6e9SStefan Hajnoczi if (!bs || !bs->job) { 13312d47c6e9SStefan Hajnoczi return NULL; 13322d47c6e9SStefan Hajnoczi } 13332d47c6e9SStefan Hajnoczi return bs->job; 13342d47c6e9SStefan Hajnoczi } 13352d47c6e9SStefan Hajnoczi 1336882ec7ceSStefan Hajnoczi void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 13372d47c6e9SStefan Hajnoczi { 13382d47c6e9SStefan Hajnoczi BlockJob *job = find_block_job(device); 13392d47c6e9SStefan Hajnoczi 13402d47c6e9SStefan Hajnoczi if (!job) { 13417ef15070SPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 13422d47c6e9SStefan Hajnoczi return; 13432d47c6e9SStefan Hajnoczi } 13442d47c6e9SStefan Hajnoczi 1345882ec7ceSStefan Hajnoczi block_job_set_speed(job, speed, errp); 13462d47c6e9SStefan Hajnoczi } 1347370521a1SStefan Hajnoczi 13486e37fb81SPaolo Bonzini void qmp_block_job_cancel(const char *device, 13496e37fb81SPaolo Bonzini bool has_force, bool force, Error **errp) 13506e37fb81SPaolo Bonzini { 13516e37fb81SPaolo Bonzini BlockJob *job = find_block_job(device); 13526e37fb81SPaolo Bonzini 13536e37fb81SPaolo Bonzini if (!has_force) { 13546e37fb81SPaolo Bonzini force = false; 13556e37fb81SPaolo Bonzini } 13566e37fb81SPaolo Bonzini 13576e37fb81SPaolo Bonzini if (!job) { 13586e37fb81SPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 13596e37fb81SPaolo Bonzini return; 13606e37fb81SPaolo Bonzini } 13616e37fb81SPaolo Bonzini if (job->paused && !force) { 13626e37fb81SPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_PAUSED, device); 13636e37fb81SPaolo Bonzini return; 13646e37fb81SPaolo Bonzini } 13656e37fb81SPaolo Bonzini 13666e37fb81SPaolo Bonzini trace_qmp_block_job_cancel(job); 13676e37fb81SPaolo Bonzini block_job_cancel(job); 13686e37fb81SPaolo Bonzini } 13696e37fb81SPaolo Bonzini 13706e37fb81SPaolo Bonzini void qmp_block_job_pause(const char *device, Error **errp) 1371370521a1SStefan Hajnoczi { 1372370521a1SStefan Hajnoczi BlockJob *job = find_block_job(device); 1373370521a1SStefan Hajnoczi 1374370521a1SStefan Hajnoczi if (!job) { 13757ef15070SPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 1376370521a1SStefan Hajnoczi return; 1377370521a1SStefan Hajnoczi } 13786e37fb81SPaolo Bonzini 13796e37fb81SPaolo Bonzini trace_qmp_block_job_pause(job); 13806e37fb81SPaolo Bonzini block_job_pause(job); 13816e37fb81SPaolo Bonzini } 13826e37fb81SPaolo Bonzini 13836e37fb81SPaolo Bonzini void qmp_block_job_resume(const char *device, Error **errp) 13846e37fb81SPaolo Bonzini { 13856e37fb81SPaolo Bonzini BlockJob *job = find_block_job(device); 13866e37fb81SPaolo Bonzini 13876e37fb81SPaolo Bonzini if (!job) { 13886e37fb81SPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 13898acc72a4SPaolo Bonzini return; 13908acc72a4SPaolo Bonzini } 1391370521a1SStefan Hajnoczi 13926e37fb81SPaolo Bonzini trace_qmp_block_job_resume(job); 13936e37fb81SPaolo Bonzini block_job_resume(job); 1394370521a1SStefan Hajnoczi } 1395fb5458cdSStefan Hajnoczi 1396aeae883bSPaolo Bonzini void qmp_block_job_complete(const char *device, Error **errp) 1397aeae883bSPaolo Bonzini { 1398aeae883bSPaolo Bonzini BlockJob *job = find_block_job(device); 1399aeae883bSPaolo Bonzini 1400aeae883bSPaolo Bonzini if (!job) { 1401aeae883bSPaolo Bonzini error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 1402aeae883bSPaolo Bonzini return; 1403aeae883bSPaolo Bonzini } 1404aeae883bSPaolo Bonzini 1405aeae883bSPaolo Bonzini trace_qmp_block_job_complete(job); 1406aeae883bSPaolo Bonzini block_job_complete(job, errp); 1407aeae883bSPaolo Bonzini } 1408aeae883bSPaolo Bonzini 1409fb5458cdSStefan Hajnoczi static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs) 1410fb5458cdSStefan Hajnoczi { 1411fb5458cdSStefan Hajnoczi BlockJobInfoList **prev = opaque; 1412fb5458cdSStefan Hajnoczi BlockJob *job = bs->job; 1413fb5458cdSStefan Hajnoczi 1414fb5458cdSStefan Hajnoczi if (job) { 141530e628b7SPaolo Bonzini BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 141630e628b7SPaolo Bonzini elem->value = block_job_query(bs->job); 1417fb5458cdSStefan Hajnoczi (*prev)->next = elem; 1418fb5458cdSStefan Hajnoczi *prev = elem; 1419fb5458cdSStefan Hajnoczi } 1420fb5458cdSStefan Hajnoczi } 1421fb5458cdSStefan Hajnoczi 1422fb5458cdSStefan Hajnoczi BlockJobInfoList *qmp_query_block_jobs(Error **errp) 1423fb5458cdSStefan Hajnoczi { 1424fb5458cdSStefan Hajnoczi /* Dummy is a fake list element for holding the head pointer */ 1425fb5458cdSStefan Hajnoczi BlockJobInfoList dummy = {}; 1426fb5458cdSStefan Hajnoczi BlockJobInfoList *prev = &dummy; 1427fb5458cdSStefan Hajnoczi bdrv_iterate(do_qmp_query_block_jobs_one, &prev); 1428fb5458cdSStefan Hajnoczi return dummy.next; 1429fb5458cdSStefan Hajnoczi } 1430