1 /* 2 * QEMU host block devices 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or 7 * later. See the COPYING file in the top-level directory. 8 * 9 * This file incorporates work covered by the following copyright and 10 * permission notice: 11 * 12 * Copyright (c) 2003-2008 Fabrice Bellard 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a copy 15 * of this software and associated documentation files (the "Software"), to deal 16 * in the Software without restriction, including without limitation the rights 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 18 * copies of the Software, and to permit persons to whom the Software is 19 * furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be included in 22 * all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 30 * THE SOFTWARE. 31 */ 32 33 #include "sysemu/blockdev.h" 34 #include "hw/block/block.h" 35 #include "block/blockjob.h" 36 #include "monitor/monitor.h" 37 #include "qapi/qmp/qerror.h" 38 #include "qemu/option.h" 39 #include "qemu/config-file.h" 40 #include "qapi/qmp/types.h" 41 #include "qapi-visit.h" 42 #include "qapi/qmp-output-visitor.h" 43 #include "sysemu/sysemu.h" 44 #include "block/block_int.h" 45 #include "qmp-commands.h" 46 #include "trace.h" 47 #include "sysemu/arch_init.h" 48 49 static QTAILQ_HEAD(drivelist, DriveInfo) drives = QTAILQ_HEAD_INITIALIZER(drives); 50 extern QemuOptsList qemu_common_drive_opts; 51 52 static const char *const if_name[IF_COUNT] = { 53 [IF_NONE] = "none", 54 [IF_IDE] = "ide", 55 [IF_SCSI] = "scsi", 56 [IF_FLOPPY] = "floppy", 57 [IF_PFLASH] = "pflash", 58 [IF_MTD] = "mtd", 59 [IF_SD] = "sd", 60 [IF_VIRTIO] = "virtio", 61 [IF_XEN] = "xen", 62 }; 63 64 static const int if_max_devs[IF_COUNT] = { 65 /* 66 * Do not change these numbers! They govern how drive option 67 * index maps to unit and bus. That mapping is ABI. 68 * 69 * All controllers used to imlement if=T drives need to support 70 * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 71 * Otherwise, some index values map to "impossible" bus, unit 72 * values. 73 * 74 * For instance, if you change [IF_SCSI] to 255, -drive 75 * if=scsi,index=12 no longer means bus=1,unit=5, but 76 * bus=0,unit=12. With an lsi53c895a controller (7 units max), 77 * the drive can't be set up. Regression. 78 */ 79 [IF_IDE] = 2, 80 [IF_SCSI] = 7, 81 }; 82 83 /* 84 * We automatically delete the drive when a device using it gets 85 * unplugged. Questionable feature, but we can't just drop it. 86 * Device models call blockdev_mark_auto_del() to schedule the 87 * automatic deletion, and generic qdev code calls blockdev_auto_del() 88 * when deletion is actually safe. 89 */ 90 void blockdev_mark_auto_del(BlockDriverState *bs) 91 { 92 DriveInfo *dinfo = drive_get_by_blockdev(bs); 93 94 if (dinfo && !dinfo->enable_auto_del) { 95 return; 96 } 97 98 if (bs->job) { 99 block_job_cancel(bs->job); 100 } 101 if (dinfo) { 102 dinfo->auto_del = 1; 103 } 104 } 105 106 void blockdev_auto_del(BlockDriverState *bs) 107 { 108 DriveInfo *dinfo = drive_get_by_blockdev(bs); 109 110 if (dinfo && dinfo->auto_del) { 111 drive_put_ref(dinfo); 112 } 113 } 114 115 static int drive_index_to_bus_id(BlockInterfaceType type, int index) 116 { 117 int max_devs = if_max_devs[type]; 118 return max_devs ? index / max_devs : 0; 119 } 120 121 static int drive_index_to_unit_id(BlockInterfaceType type, int index) 122 { 123 int max_devs = if_max_devs[type]; 124 return max_devs ? index % max_devs : index; 125 } 126 127 QemuOpts *drive_def(const char *optstr) 128 { 129 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); 130 } 131 132 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 133 const char *optstr) 134 { 135 QemuOpts *opts; 136 char buf[32]; 137 138 opts = drive_def(optstr); 139 if (!opts) { 140 return NULL; 141 } 142 if (type != IF_DEFAULT) { 143 qemu_opt_set(opts, "if", if_name[type]); 144 } 145 if (index >= 0) { 146 snprintf(buf, sizeof(buf), "%d", index); 147 qemu_opt_set(opts, "index", buf); 148 } 149 if (file) 150 qemu_opt_set(opts, "file", file); 151 return opts; 152 } 153 154 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 155 { 156 DriveInfo *dinfo; 157 158 /* seek interface, bus and unit */ 159 160 QTAILQ_FOREACH(dinfo, &drives, next) { 161 if (dinfo->type == type && 162 dinfo->bus == bus && 163 dinfo->unit == unit) 164 return dinfo; 165 } 166 167 return NULL; 168 } 169 170 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 171 { 172 return drive_get(type, 173 drive_index_to_bus_id(type, index), 174 drive_index_to_unit_id(type, index)); 175 } 176 177 int drive_get_max_bus(BlockInterfaceType type) 178 { 179 int max_bus; 180 DriveInfo *dinfo; 181 182 max_bus = -1; 183 QTAILQ_FOREACH(dinfo, &drives, next) { 184 if(dinfo->type == type && 185 dinfo->bus > max_bus) 186 max_bus = dinfo->bus; 187 } 188 return max_bus; 189 } 190 191 /* Get a block device. This should only be used for single-drive devices 192 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 193 appropriate bus. */ 194 DriveInfo *drive_get_next(BlockInterfaceType type) 195 { 196 static int next_block_unit[IF_COUNT]; 197 198 return drive_get(type, 0, next_block_unit[type]++); 199 } 200 201 DriveInfo *drive_get_by_blockdev(BlockDriverState *bs) 202 { 203 DriveInfo *dinfo; 204 205 QTAILQ_FOREACH(dinfo, &drives, next) { 206 if (dinfo->bdrv == bs) { 207 return dinfo; 208 } 209 } 210 return NULL; 211 } 212 213 static void bdrv_format_print(void *opaque, const char *name) 214 { 215 error_printf(" %s", name); 216 } 217 218 static void drive_uninit(DriveInfo *dinfo) 219 { 220 if (dinfo->opts) { 221 qemu_opts_del(dinfo->opts); 222 } 223 224 bdrv_unref(dinfo->bdrv); 225 g_free(dinfo->id); 226 QTAILQ_REMOVE(&drives, dinfo, next); 227 g_free(dinfo->serial); 228 g_free(dinfo); 229 } 230 231 void drive_put_ref(DriveInfo *dinfo) 232 { 233 assert(dinfo->refcount); 234 if (--dinfo->refcount == 0) { 235 drive_uninit(dinfo); 236 } 237 } 238 239 void drive_get_ref(DriveInfo *dinfo) 240 { 241 dinfo->refcount++; 242 } 243 244 typedef struct { 245 QEMUBH *bh; 246 BlockDriverState *bs; 247 } BDRVPutRefBH; 248 249 static void bdrv_put_ref_bh(void *opaque) 250 { 251 BDRVPutRefBH *s = opaque; 252 253 bdrv_unref(s->bs); 254 qemu_bh_delete(s->bh); 255 g_free(s); 256 } 257 258 /* 259 * Release a BDS reference in a BH 260 * 261 * It is not safe to use bdrv_unref() from a callback function when the callers 262 * still need the BlockDriverState. In such cases we schedule a BH to release 263 * the reference. 264 */ 265 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) 266 { 267 BDRVPutRefBH *s; 268 269 s = g_new(BDRVPutRefBH, 1); 270 s->bh = qemu_bh_new(bdrv_put_ref_bh, s); 271 s->bs = bs; 272 qemu_bh_schedule(s->bh); 273 } 274 275 static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 276 { 277 if (!strcmp(buf, "ignore")) { 278 return BLOCKDEV_ON_ERROR_IGNORE; 279 } else if (!is_read && !strcmp(buf, "enospc")) { 280 return BLOCKDEV_ON_ERROR_ENOSPC; 281 } else if (!strcmp(buf, "stop")) { 282 return BLOCKDEV_ON_ERROR_STOP; 283 } else if (!strcmp(buf, "report")) { 284 return BLOCKDEV_ON_ERROR_REPORT; 285 } else { 286 error_setg(errp, "'%s' invalid %s error action", 287 buf, is_read ? "read" : "write"); 288 return -1; 289 } 290 } 291 292 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) 293 { 294 if (throttle_conflicting(cfg)) { 295 error_setg(errp, "bps/iops/max total values and read/write values" 296 " cannot be used at the same time"); 297 return false; 298 } 299 300 if (!throttle_is_valid(cfg)) { 301 error_setg(errp, "bps/iops/maxs values must be 0 or greater"); 302 return false; 303 } 304 305 return true; 306 } 307 308 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 309 310 /* Takes the ownership of bs_opts */ 311 static DriveInfo *blockdev_init(QDict *bs_opts, 312 BlockInterfaceType type, 313 Error **errp) 314 { 315 const char *buf; 316 const char *file = NULL; 317 const char *serial; 318 int ro = 0; 319 int bdrv_flags = 0; 320 int on_read_error, on_write_error; 321 DriveInfo *dinfo; 322 ThrottleConfig cfg; 323 int snapshot = 0; 324 bool copy_on_read; 325 int ret; 326 Error *error = NULL; 327 QemuOpts *opts; 328 const char *id; 329 bool has_driver_specific_opts; 330 BlockDriver *drv = NULL; 331 332 /* Check common options by copying from bs_opts to opts, all other options 333 * stay in bs_opts for processing by bdrv_open(). */ 334 id = qdict_get_try_str(bs_opts, "id"); 335 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 336 if (error_is_set(&error)) { 337 error_propagate(errp, error); 338 return NULL; 339 } 340 341 qemu_opts_absorb_qdict(opts, bs_opts, &error); 342 if (error_is_set(&error)) { 343 error_propagate(errp, error); 344 return NULL; 345 } 346 347 if (id) { 348 qdict_del(bs_opts, "id"); 349 } 350 351 has_driver_specific_opts = !!qdict_size(bs_opts); 352 353 /* extract parameters */ 354 snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 355 ro = qemu_opt_get_bool(opts, "read-only", 0); 356 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); 357 358 file = qemu_opt_get(opts, "file"); 359 serial = qemu_opt_get(opts, "serial"); 360 361 if ((buf = qemu_opt_get(opts, "discard")) != NULL) { 362 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { 363 error_setg(errp, "invalid discard option"); 364 return NULL; 365 } 366 } 367 368 if (qemu_opt_get_bool(opts, "cache.writeback", true)) { 369 bdrv_flags |= BDRV_O_CACHE_WB; 370 } 371 if (qemu_opt_get_bool(opts, "cache.direct", false)) { 372 bdrv_flags |= BDRV_O_NOCACHE; 373 } 374 if (qemu_opt_get_bool(opts, "cache.no-flush", false)) { 375 bdrv_flags |= BDRV_O_NO_FLUSH; 376 } 377 378 #ifdef CONFIG_LINUX_AIO 379 if ((buf = qemu_opt_get(opts, "aio")) != NULL) { 380 if (!strcmp(buf, "native")) { 381 bdrv_flags |= BDRV_O_NATIVE_AIO; 382 } else if (!strcmp(buf, "threads")) { 383 /* this is the default */ 384 } else { 385 error_setg(errp, "invalid aio option"); 386 return NULL; 387 } 388 } 389 #endif 390 391 if ((buf = qemu_opt_get(opts, "format")) != NULL) { 392 if (is_help_option(buf)) { 393 error_printf("Supported formats:"); 394 bdrv_iterate_format(bdrv_format_print, NULL); 395 error_printf("\n"); 396 return NULL; 397 } 398 399 drv = bdrv_find_format(buf); 400 if (!drv) { 401 error_setg(errp, "'%s' invalid format", buf); 402 return NULL; 403 } 404 } 405 406 /* disk I/O throttling */ 407 memset(&cfg, 0, sizeof(cfg)); 408 cfg.buckets[THROTTLE_BPS_TOTAL].avg = 409 qemu_opt_get_number(opts, "throttling.bps-total", 0); 410 cfg.buckets[THROTTLE_BPS_READ].avg = 411 qemu_opt_get_number(opts, "throttling.bps-read", 0); 412 cfg.buckets[THROTTLE_BPS_WRITE].avg = 413 qemu_opt_get_number(opts, "throttling.bps-write", 0); 414 cfg.buckets[THROTTLE_OPS_TOTAL].avg = 415 qemu_opt_get_number(opts, "throttling.iops-total", 0); 416 cfg.buckets[THROTTLE_OPS_READ].avg = 417 qemu_opt_get_number(opts, "throttling.iops-read", 0); 418 cfg.buckets[THROTTLE_OPS_WRITE].avg = 419 qemu_opt_get_number(opts, "throttling.iops-write", 0); 420 421 cfg.buckets[THROTTLE_BPS_TOTAL].max = 422 qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 423 cfg.buckets[THROTTLE_BPS_READ].max = 424 qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 425 cfg.buckets[THROTTLE_BPS_WRITE].max = 426 qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 427 cfg.buckets[THROTTLE_OPS_TOTAL].max = 428 qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 429 cfg.buckets[THROTTLE_OPS_READ].max = 430 qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 431 cfg.buckets[THROTTLE_OPS_WRITE].max = 432 qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 433 434 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); 435 436 if (!check_throttle_config(&cfg, &error)) { 437 error_propagate(errp, error); 438 return NULL; 439 } 440 441 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 442 if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 443 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) { 444 error_setg(errp, "werror is not supported by this bus type"); 445 return NULL; 446 } 447 448 on_write_error = parse_block_error_action(buf, 0, &error); 449 if (error_is_set(&error)) { 450 error_propagate(errp, error); 451 return NULL; 452 } 453 } 454 455 on_read_error = BLOCKDEV_ON_ERROR_REPORT; 456 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 457 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && type != IF_NONE) { 458 error_report("rerror is not supported by this bus type"); 459 return NULL; 460 } 461 462 on_read_error = parse_block_error_action(buf, 1, &error); 463 if (error_is_set(&error)) { 464 error_propagate(errp, error); 465 return NULL; 466 } 467 } 468 469 /* init */ 470 dinfo = g_malloc0(sizeof(*dinfo)); 471 dinfo->id = g_strdup(qemu_opts_id(opts)); 472 dinfo->bdrv = bdrv_new(dinfo->id); 473 dinfo->bdrv->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; 474 dinfo->bdrv->read_only = ro; 475 dinfo->type = type; 476 dinfo->refcount = 1; 477 if (serial != NULL) { 478 dinfo->serial = g_strdup(serial); 479 } 480 QTAILQ_INSERT_TAIL(&drives, dinfo, next); 481 482 bdrv_set_on_error(dinfo->bdrv, on_read_error, on_write_error); 483 484 /* disk I/O throttling */ 485 if (throttle_enabled(&cfg)) { 486 bdrv_io_limits_enable(dinfo->bdrv); 487 bdrv_set_io_limits(dinfo->bdrv, &cfg); 488 } 489 490 if (!file || !*file) { 491 if (has_driver_specific_opts) { 492 file = NULL; 493 } else { 494 return dinfo; 495 } 496 } 497 if (snapshot) { 498 /* always use cache=unsafe with snapshot */ 499 bdrv_flags &= ~BDRV_O_CACHE_MASK; 500 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); 501 } 502 503 if (copy_on_read) { 504 bdrv_flags |= BDRV_O_COPY_ON_READ; 505 } 506 507 if (runstate_check(RUN_STATE_INMIGRATE)) { 508 bdrv_flags |= BDRV_O_INCOMING; 509 } 510 511 bdrv_flags |= ro ? 0 : BDRV_O_RDWR; 512 513 QINCREF(bs_opts); 514 ret = bdrv_open(dinfo->bdrv, file, bs_opts, bdrv_flags, drv, &error); 515 516 if (ret < 0) { 517 error_setg(errp, "could not open disk image %s: %s", 518 file ?: dinfo->id, error_get_pretty(error)); 519 error_free(error); 520 goto err; 521 } 522 523 if (bdrv_key_required(dinfo->bdrv)) 524 autostart = 0; 525 526 QDECREF(bs_opts); 527 qemu_opts_del(opts); 528 529 return dinfo; 530 531 err: 532 qemu_opts_del(opts); 533 QDECREF(bs_opts); 534 bdrv_unref(dinfo->bdrv); 535 g_free(dinfo->id); 536 QTAILQ_REMOVE(&drives, dinfo, next); 537 g_free(dinfo); 538 return NULL; 539 } 540 541 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to) 542 { 543 const char *value; 544 545 value = qemu_opt_get(opts, from); 546 if (value) { 547 qemu_opt_set(opts, to, value); 548 qemu_opt_unset(opts, from); 549 } 550 } 551 552 QemuOptsList qemu_legacy_drive_opts = { 553 .name = "drive", 554 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 555 .desc = { 556 { 557 .name = "bus", 558 .type = QEMU_OPT_NUMBER, 559 .help = "bus number", 560 },{ 561 .name = "unit", 562 .type = QEMU_OPT_NUMBER, 563 .help = "unit number (i.e. lun for scsi)", 564 },{ 565 .name = "index", 566 .type = QEMU_OPT_NUMBER, 567 .help = "index number", 568 },{ 569 .name = "media", 570 .type = QEMU_OPT_STRING, 571 .help = "media type (disk, cdrom)", 572 },{ 573 .name = "if", 574 .type = QEMU_OPT_STRING, 575 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 576 },{ 577 .name = "cyls", 578 .type = QEMU_OPT_NUMBER, 579 .help = "number of cylinders (ide disk geometry)", 580 },{ 581 .name = "heads", 582 .type = QEMU_OPT_NUMBER, 583 .help = "number of heads (ide disk geometry)", 584 },{ 585 .name = "secs", 586 .type = QEMU_OPT_NUMBER, 587 .help = "number of sectors (ide disk geometry)", 588 },{ 589 .name = "trans", 590 .type = QEMU_OPT_STRING, 591 .help = "chs translation (auto, lba, none)", 592 },{ 593 .name = "boot", 594 .type = QEMU_OPT_BOOL, 595 .help = "(deprecated, ignored)", 596 },{ 597 .name = "addr", 598 .type = QEMU_OPT_STRING, 599 .help = "pci address (virtio only)", 600 }, 601 602 /* Options that are passed on, but have special semantics with -drive */ 603 { 604 .name = "read-only", 605 .type = QEMU_OPT_BOOL, 606 .help = "open drive file as read-only", 607 },{ 608 .name = "copy-on-read", 609 .type = QEMU_OPT_BOOL, 610 .help = "copy read data from backing file into image file", 611 }, 612 613 { /* end of list */ } 614 }, 615 }; 616 617 DriveInfo *drive_init(QemuOpts *all_opts, BlockInterfaceType block_default_type) 618 { 619 const char *value; 620 DriveInfo *dinfo = NULL; 621 QDict *bs_opts; 622 QemuOpts *legacy_opts; 623 DriveMediaType media = MEDIA_DISK; 624 BlockInterfaceType type; 625 int cyls, heads, secs, translation; 626 int max_devs, bus_id, unit_id, index; 627 const char *devaddr; 628 bool read_only, copy_on_read; 629 Error *local_err = NULL; 630 631 /* Change legacy command line options into QMP ones */ 632 qemu_opt_rename(all_opts, "iops", "throttling.iops-total"); 633 qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read"); 634 qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write"); 635 636 qemu_opt_rename(all_opts, "bps", "throttling.bps-total"); 637 qemu_opt_rename(all_opts, "bps_rd", "throttling.bps-read"); 638 qemu_opt_rename(all_opts, "bps_wr", "throttling.bps-write"); 639 640 qemu_opt_rename(all_opts, "iops_max", "throttling.iops-total-max"); 641 qemu_opt_rename(all_opts, "iops_rd_max", "throttling.iops-read-max"); 642 qemu_opt_rename(all_opts, "iops_wr_max", "throttling.iops-write-max"); 643 644 qemu_opt_rename(all_opts, "bps_max", "throttling.bps-total-max"); 645 qemu_opt_rename(all_opts, "bps_rd_max", "throttling.bps-read-max"); 646 qemu_opt_rename(all_opts, "bps_wr_max", "throttling.bps-write-max"); 647 648 qemu_opt_rename(all_opts, 649 "iops_size", "throttling.iops-size"); 650 651 qemu_opt_rename(all_opts, "readonly", "read-only"); 652 653 value = qemu_opt_get(all_opts, "cache"); 654 if (value) { 655 int flags = 0; 656 657 if (bdrv_parse_cache_flags(value, &flags) != 0) { 658 error_report("invalid cache option"); 659 return NULL; 660 } 661 662 /* Specific options take precedence */ 663 if (!qemu_opt_get(all_opts, "cache.writeback")) { 664 qemu_opt_set_bool(all_opts, "cache.writeback", 665 !!(flags & BDRV_O_CACHE_WB)); 666 } 667 if (!qemu_opt_get(all_opts, "cache.direct")) { 668 qemu_opt_set_bool(all_opts, "cache.direct", 669 !!(flags & BDRV_O_NOCACHE)); 670 } 671 if (!qemu_opt_get(all_opts, "cache.no-flush")) { 672 qemu_opt_set_bool(all_opts, "cache.no-flush", 673 !!(flags & BDRV_O_NO_FLUSH)); 674 } 675 qemu_opt_unset(all_opts, "cache"); 676 } 677 678 /* Get a QDict for processing the options */ 679 bs_opts = qdict_new(); 680 qemu_opts_to_qdict(all_opts, bs_opts); 681 682 legacy_opts = qemu_opts_create_nofail(&qemu_legacy_drive_opts); 683 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 684 if (error_is_set(&local_err)) { 685 qerror_report_err(local_err); 686 error_free(local_err); 687 goto fail; 688 } 689 690 /* Deprecated option boot=[on|off] */ 691 if (qemu_opt_get(legacy_opts, "boot") != NULL) { 692 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 693 "ignored. Future versions will reject this parameter. Please " 694 "update your scripts.\n"); 695 } 696 697 /* Media type */ 698 value = qemu_opt_get(legacy_opts, "media"); 699 if (value) { 700 if (!strcmp(value, "disk")) { 701 media = MEDIA_DISK; 702 } else if (!strcmp(value, "cdrom")) { 703 media = MEDIA_CDROM; 704 qdict_put(bs_opts, "read-only", qstring_from_str("on")); 705 } else { 706 error_report("'%s' invalid media", value); 707 goto fail; 708 } 709 } 710 711 /* copy-on-read is disabled with a warning for read-only devices */ 712 read_only = qemu_opt_get_bool(legacy_opts, "read-only", false); 713 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 714 715 if (read_only && copy_on_read) { 716 error_report("warning: disabling copy-on-read on read-only drive"); 717 copy_on_read = false; 718 } 719 720 qdict_put(bs_opts, "read-only", 721 qstring_from_str(read_only ? "on" : "off")); 722 qdict_put(bs_opts, "copy-on-read", 723 qstring_from_str(copy_on_read ? "on" :"off")); 724 725 /* Controller type */ 726 value = qemu_opt_get(legacy_opts, "if"); 727 if (value) { 728 for (type = 0; 729 type < IF_COUNT && strcmp(value, if_name[type]); 730 type++) { 731 } 732 if (type == IF_COUNT) { 733 error_report("unsupported bus type '%s'", value); 734 goto fail; 735 } 736 } else { 737 type = block_default_type; 738 } 739 740 /* Geometry */ 741 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 742 heads = qemu_opt_get_number(legacy_opts, "heads", 0); 743 secs = qemu_opt_get_number(legacy_opts, "secs", 0); 744 745 if (cyls || heads || secs) { 746 if (cyls < 1) { 747 error_report("invalid physical cyls number"); 748 goto fail; 749 } 750 if (heads < 1) { 751 error_report("invalid physical heads number"); 752 goto fail; 753 } 754 if (secs < 1) { 755 error_report("invalid physical secs number"); 756 goto fail; 757 } 758 } 759 760 translation = BIOS_ATA_TRANSLATION_AUTO; 761 value = qemu_opt_get(legacy_opts, "trans"); 762 if (value != NULL) { 763 if (!cyls) { 764 error_report("'%s' trans must be used with cyls, heads and secs", 765 value); 766 goto fail; 767 } 768 if (!strcmp(value, "none")) { 769 translation = BIOS_ATA_TRANSLATION_NONE; 770 } else if (!strcmp(value, "lba")) { 771 translation = BIOS_ATA_TRANSLATION_LBA; 772 } else if (!strcmp(value, "auto")) { 773 translation = BIOS_ATA_TRANSLATION_AUTO; 774 } else { 775 error_report("'%s' invalid translation type", value); 776 goto fail; 777 } 778 } 779 780 if (media == MEDIA_CDROM) { 781 if (cyls || secs || heads) { 782 error_report("CHS can't be set with media=cdrom"); 783 goto fail; 784 } 785 } 786 787 /* Device address specified by bus/unit or index. 788 * If none was specified, try to find the first free one. */ 789 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 790 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 791 index = qemu_opt_get_number(legacy_opts, "index", -1); 792 793 max_devs = if_max_devs[type]; 794 795 if (index != -1) { 796 if (bus_id != 0 || unit_id != -1) { 797 error_report("index cannot be used with bus and unit"); 798 goto fail; 799 } 800 bus_id = drive_index_to_bus_id(type, index); 801 unit_id = drive_index_to_unit_id(type, index); 802 } 803 804 if (unit_id == -1) { 805 unit_id = 0; 806 while (drive_get(type, bus_id, unit_id) != NULL) { 807 unit_id++; 808 if (max_devs && unit_id >= max_devs) { 809 unit_id -= max_devs; 810 bus_id++; 811 } 812 } 813 } 814 815 if (max_devs && unit_id >= max_devs) { 816 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 817 goto fail; 818 } 819 820 if (drive_get(type, bus_id, unit_id) != NULL) { 821 error_report("drive with bus=%d, unit=%d (index=%d) exists", 822 bus_id, unit_id, index); 823 goto fail; 824 } 825 826 /* no id supplied -> create one */ 827 if (qemu_opts_id(all_opts) == NULL) { 828 char *new_id; 829 const char *mediastr = ""; 830 if (type == IF_IDE || type == IF_SCSI) { 831 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 832 } 833 if (max_devs) { 834 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 835 mediastr, unit_id); 836 } else { 837 new_id = g_strdup_printf("%s%s%i", if_name[type], 838 mediastr, unit_id); 839 } 840 qdict_put(bs_opts, "id", qstring_from_str(new_id)); 841 g_free(new_id); 842 } 843 844 /* Add virtio block device */ 845 devaddr = qemu_opt_get(legacy_opts, "addr"); 846 if (devaddr && type != IF_VIRTIO) { 847 error_report("addr is not supported by this bus type"); 848 goto fail; 849 } 850 851 if (type == IF_VIRTIO) { 852 QemuOpts *devopts; 853 devopts = qemu_opts_create_nofail(qemu_find_opts("device")); 854 if (arch_type == QEMU_ARCH_S390X) { 855 qemu_opt_set(devopts, "driver", "virtio-blk-s390"); 856 } else { 857 qemu_opt_set(devopts, "driver", "virtio-blk-pci"); 858 } 859 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id")); 860 if (devaddr) { 861 qemu_opt_set(devopts, "addr", devaddr); 862 } 863 } 864 865 /* Actual block device init: Functionality shared with blockdev-add */ 866 dinfo = blockdev_init(bs_opts, type, &local_err); 867 if (dinfo == NULL) { 868 if (error_is_set(&local_err)) { 869 qerror_report_err(local_err); 870 error_free(local_err); 871 } 872 goto fail; 873 } else { 874 assert(!error_is_set(&local_err)); 875 } 876 877 /* Set legacy DriveInfo fields */ 878 dinfo->enable_auto_del = true; 879 dinfo->opts = all_opts; 880 881 dinfo->cyls = cyls; 882 dinfo->heads = heads; 883 dinfo->secs = secs; 884 dinfo->trans = translation; 885 886 dinfo->bus = bus_id; 887 dinfo->unit = unit_id; 888 dinfo->devaddr = devaddr; 889 890 switch(type) { 891 case IF_IDE: 892 case IF_SCSI: 893 case IF_XEN: 894 case IF_NONE: 895 dinfo->media_cd = media == MEDIA_CDROM; 896 break; 897 default: 898 break; 899 } 900 901 fail: 902 qemu_opts_del(legacy_opts); 903 return dinfo; 904 } 905 906 void do_commit(Monitor *mon, const QDict *qdict) 907 { 908 const char *device = qdict_get_str(qdict, "device"); 909 BlockDriverState *bs; 910 int ret; 911 912 if (!strcmp(device, "all")) { 913 ret = bdrv_commit_all(); 914 } else { 915 bs = bdrv_find(device); 916 if (!bs) { 917 monitor_printf(mon, "Device '%s' not found\n", device); 918 return; 919 } 920 ret = bdrv_commit(bs); 921 } 922 if (ret < 0) { 923 monitor_printf(mon, "'commit' error for '%s': %s\n", device, 924 strerror(-ret)); 925 } 926 } 927 928 static void blockdev_do_action(int kind, void *data, Error **errp) 929 { 930 TransactionAction action; 931 TransactionActionList list; 932 933 action.kind = kind; 934 action.data = data; 935 list.value = &action; 936 list.next = NULL; 937 qmp_transaction(&list, errp); 938 } 939 940 void qmp_blockdev_snapshot_sync(const char *device, const char *snapshot_file, 941 bool has_format, const char *format, 942 bool has_mode, enum NewImageMode mode, 943 Error **errp) 944 { 945 BlockdevSnapshot snapshot = { 946 .device = (char *) device, 947 .snapshot_file = (char *) snapshot_file, 948 .has_format = has_format, 949 .format = (char *) format, 950 .has_mode = has_mode, 951 .mode = mode, 952 }; 953 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 954 &snapshot, errp); 955 } 956 957 void qmp_blockdev_snapshot_internal_sync(const char *device, 958 const char *name, 959 Error **errp) 960 { 961 BlockdevSnapshotInternal snapshot = { 962 .device = (char *) device, 963 .name = (char *) name 964 }; 965 966 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 967 &snapshot, errp); 968 } 969 970 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 971 bool has_id, 972 const char *id, 973 bool has_name, 974 const char *name, 975 Error **errp) 976 { 977 BlockDriverState *bs = bdrv_find(device); 978 QEMUSnapshotInfo sn; 979 Error *local_err = NULL; 980 SnapshotInfo *info = NULL; 981 int ret; 982 983 if (!bs) { 984 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 985 return NULL; 986 } 987 988 if (!has_id) { 989 id = NULL; 990 } 991 992 if (!has_name) { 993 name = NULL; 994 } 995 996 if (!id && !name) { 997 error_setg(errp, "Name or id must be provided"); 998 return NULL; 999 } 1000 1001 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 1002 if (error_is_set(&local_err)) { 1003 error_propagate(errp, local_err); 1004 return NULL; 1005 } 1006 if (!ret) { 1007 error_setg(errp, 1008 "Snapshot with id '%s' and name '%s' does not exist on " 1009 "device '%s'", 1010 STR_OR_NULL(id), STR_OR_NULL(name), device); 1011 return NULL; 1012 } 1013 1014 bdrv_snapshot_delete(bs, id, name, &local_err); 1015 if (error_is_set(&local_err)) { 1016 error_propagate(errp, local_err); 1017 return NULL; 1018 } 1019 1020 info = g_malloc0(sizeof(SnapshotInfo)); 1021 info->id = g_strdup(sn.id_str); 1022 info->name = g_strdup(sn.name); 1023 info->date_nsec = sn.date_nsec; 1024 info->date_sec = sn.date_sec; 1025 info->vm_state_size = sn.vm_state_size; 1026 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 1027 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 1028 1029 return info; 1030 } 1031 1032 /* New and old BlockDriverState structs for group snapshots */ 1033 1034 typedef struct BlkTransactionState BlkTransactionState; 1035 1036 /* Only prepare() may fail. In a single transaction, only one of commit() or 1037 abort() will be called, clean() will always be called if it present. */ 1038 typedef struct BdrvActionOps { 1039 /* Size of state struct, in bytes. */ 1040 size_t instance_size; 1041 /* Prepare the work, must NOT be NULL. */ 1042 void (*prepare)(BlkTransactionState *common, Error **errp); 1043 /* Commit the changes, can be NULL. */ 1044 void (*commit)(BlkTransactionState *common); 1045 /* Abort the changes on fail, can be NULL. */ 1046 void (*abort)(BlkTransactionState *common); 1047 /* Clean up resource in the end, can be NULL. */ 1048 void (*clean)(BlkTransactionState *common); 1049 } BdrvActionOps; 1050 1051 /* 1052 * This structure must be arranged as first member in child type, assuming 1053 * that compiler will also arrange it to the same address with parent instance. 1054 * Later it will be used in free(). 1055 */ 1056 struct BlkTransactionState { 1057 TransactionAction *action; 1058 const BdrvActionOps *ops; 1059 QSIMPLEQ_ENTRY(BlkTransactionState) entry; 1060 }; 1061 1062 /* internal snapshot private data */ 1063 typedef struct InternalSnapshotState { 1064 BlkTransactionState common; 1065 BlockDriverState *bs; 1066 QEMUSnapshotInfo sn; 1067 } InternalSnapshotState; 1068 1069 static void internal_snapshot_prepare(BlkTransactionState *common, 1070 Error **errp) 1071 { 1072 const char *device; 1073 const char *name; 1074 BlockDriverState *bs; 1075 QEMUSnapshotInfo old_sn, *sn; 1076 bool ret; 1077 qemu_timeval tv; 1078 BlockdevSnapshotInternal *internal; 1079 InternalSnapshotState *state; 1080 int ret1; 1081 1082 g_assert(common->action->kind == 1083 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 1084 internal = common->action->blockdev_snapshot_internal_sync; 1085 state = DO_UPCAST(InternalSnapshotState, common, common); 1086 1087 /* 1. parse input */ 1088 device = internal->device; 1089 name = internal->name; 1090 1091 /* 2. check for validation */ 1092 bs = bdrv_find(device); 1093 if (!bs) { 1094 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1095 return; 1096 } 1097 1098 if (!bdrv_is_inserted(bs)) { 1099 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1100 return; 1101 } 1102 1103 if (bdrv_is_read_only(bs)) { 1104 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); 1105 return; 1106 } 1107 1108 if (!bdrv_can_snapshot(bs)) { 1109 error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1110 bs->drv->format_name, device, "internal snapshot"); 1111 return; 1112 } 1113 1114 if (!strlen(name)) { 1115 error_setg(errp, "Name is empty"); 1116 return; 1117 } 1118 1119 /* check whether a snapshot with name exist */ 1120 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, errp); 1121 if (error_is_set(errp)) { 1122 return; 1123 } else if (ret) { 1124 error_setg(errp, 1125 "Snapshot with name '%s' already exists on device '%s'", 1126 name, device); 1127 return; 1128 } 1129 1130 /* 3. take the snapshot */ 1131 sn = &state->sn; 1132 pstrcpy(sn->name, sizeof(sn->name), name); 1133 qemu_gettimeofday(&tv); 1134 sn->date_sec = tv.tv_sec; 1135 sn->date_nsec = tv.tv_usec * 1000; 1136 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1137 1138 ret1 = bdrv_snapshot_create(bs, sn); 1139 if (ret1 < 0) { 1140 error_setg_errno(errp, -ret1, 1141 "Failed to create snapshot '%s' on device '%s'", 1142 name, device); 1143 return; 1144 } 1145 1146 /* 4. succeed, mark a snapshot is created */ 1147 state->bs = bs; 1148 } 1149 1150 static void internal_snapshot_abort(BlkTransactionState *common) 1151 { 1152 InternalSnapshotState *state = 1153 DO_UPCAST(InternalSnapshotState, common, common); 1154 BlockDriverState *bs = state->bs; 1155 QEMUSnapshotInfo *sn = &state->sn; 1156 Error *local_error = NULL; 1157 1158 if (!bs) { 1159 return; 1160 } 1161 1162 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1163 error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1164 "device '%s' in abort: %s", 1165 sn->id_str, 1166 sn->name, 1167 bdrv_get_device_name(bs), 1168 error_get_pretty(local_error)); 1169 error_free(local_error); 1170 } 1171 } 1172 1173 /* external snapshot private data */ 1174 typedef struct ExternalSnapshotState { 1175 BlkTransactionState common; 1176 BlockDriverState *old_bs; 1177 BlockDriverState *new_bs; 1178 } ExternalSnapshotState; 1179 1180 static void external_snapshot_prepare(BlkTransactionState *common, 1181 Error **errp) 1182 { 1183 BlockDriver *drv; 1184 int flags, ret; 1185 Error *local_err = NULL; 1186 const char *device; 1187 const char *new_image_file; 1188 const char *format = "qcow2"; 1189 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1190 ExternalSnapshotState *state = 1191 DO_UPCAST(ExternalSnapshotState, common, common); 1192 TransactionAction *action = common->action; 1193 1194 /* get parameters */ 1195 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); 1196 1197 device = action->blockdev_snapshot_sync->device; 1198 new_image_file = action->blockdev_snapshot_sync->snapshot_file; 1199 if (action->blockdev_snapshot_sync->has_format) { 1200 format = action->blockdev_snapshot_sync->format; 1201 } 1202 if (action->blockdev_snapshot_sync->has_mode) { 1203 mode = action->blockdev_snapshot_sync->mode; 1204 } 1205 1206 /* start processing */ 1207 drv = bdrv_find_format(format); 1208 if (!drv) { 1209 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1210 return; 1211 } 1212 1213 state->old_bs = bdrv_find(device); 1214 if (!state->old_bs) { 1215 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1216 return; 1217 } 1218 1219 if (!bdrv_is_inserted(state->old_bs)) { 1220 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1221 return; 1222 } 1223 1224 if (bdrv_in_use(state->old_bs)) { 1225 error_set(errp, QERR_DEVICE_IN_USE, device); 1226 return; 1227 } 1228 1229 if (!bdrv_is_read_only(state->old_bs)) { 1230 if (bdrv_flush(state->old_bs)) { 1231 error_set(errp, QERR_IO_ERROR); 1232 return; 1233 } 1234 } 1235 1236 if (bdrv_check_ext_snapshot(state->old_bs) != EXT_SNAPSHOT_ALLOWED) { 1237 error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); 1238 return; 1239 } 1240 1241 flags = state->old_bs->open_flags; 1242 1243 /* create new image w/backing file */ 1244 if (mode != NEW_IMAGE_MODE_EXISTING) { 1245 bdrv_img_create(new_image_file, format, 1246 state->old_bs->filename, 1247 state->old_bs->drv->format_name, 1248 NULL, -1, flags, &local_err, false); 1249 if (error_is_set(&local_err)) { 1250 error_propagate(errp, local_err); 1251 return; 1252 } 1253 } 1254 1255 /* We will manually add the backing_hd field to the bs later */ 1256 state->new_bs = bdrv_new(""); 1257 /* TODO Inherit bs->options or only take explicit options with an 1258 * extended QMP command? */ 1259 ret = bdrv_open(state->new_bs, new_image_file, NULL, 1260 flags | BDRV_O_NO_BACKING, drv, &local_err); 1261 if (ret != 0) { 1262 error_propagate(errp, local_err); 1263 } 1264 } 1265 1266 static void external_snapshot_commit(BlkTransactionState *common) 1267 { 1268 ExternalSnapshotState *state = 1269 DO_UPCAST(ExternalSnapshotState, common, common); 1270 1271 /* This removes our old bs and adds the new bs */ 1272 bdrv_append(state->new_bs, state->old_bs); 1273 /* We don't need (or want) to use the transactional 1274 * bdrv_reopen_multiple() across all the entries at once, because we 1275 * don't want to abort all of them if one of them fails the reopen */ 1276 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, 1277 NULL); 1278 } 1279 1280 static void external_snapshot_abort(BlkTransactionState *common) 1281 { 1282 ExternalSnapshotState *state = 1283 DO_UPCAST(ExternalSnapshotState, common, common); 1284 if (state->new_bs) { 1285 bdrv_unref(state->new_bs); 1286 } 1287 } 1288 1289 typedef struct DriveBackupState { 1290 BlkTransactionState common; 1291 BlockDriverState *bs; 1292 BlockJob *job; 1293 } DriveBackupState; 1294 1295 static void drive_backup_prepare(BlkTransactionState *common, Error **errp) 1296 { 1297 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1298 DriveBackup *backup; 1299 Error *local_err = NULL; 1300 1301 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 1302 backup = common->action->drive_backup; 1303 1304 qmp_drive_backup(backup->device, backup->target, 1305 backup->has_format, backup->format, 1306 backup->sync, 1307 backup->has_mode, backup->mode, 1308 backup->has_speed, backup->speed, 1309 backup->has_on_source_error, backup->on_source_error, 1310 backup->has_on_target_error, backup->on_target_error, 1311 &local_err); 1312 if (error_is_set(&local_err)) { 1313 error_propagate(errp, local_err); 1314 state->bs = NULL; 1315 state->job = NULL; 1316 return; 1317 } 1318 1319 state->bs = bdrv_find(backup->device); 1320 state->job = state->bs->job; 1321 } 1322 1323 static void drive_backup_abort(BlkTransactionState *common) 1324 { 1325 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1326 BlockDriverState *bs = state->bs; 1327 1328 /* Only cancel if it's the job we started */ 1329 if (bs && bs->job && bs->job == state->job) { 1330 block_job_cancel_sync(bs->job); 1331 } 1332 } 1333 1334 static void abort_prepare(BlkTransactionState *common, Error **errp) 1335 { 1336 error_setg(errp, "Transaction aborted using Abort action"); 1337 } 1338 1339 static void abort_commit(BlkTransactionState *common) 1340 { 1341 g_assert_not_reached(); /* this action never succeeds */ 1342 } 1343 1344 static const BdrvActionOps actions[] = { 1345 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 1346 .instance_size = sizeof(ExternalSnapshotState), 1347 .prepare = external_snapshot_prepare, 1348 .commit = external_snapshot_commit, 1349 .abort = external_snapshot_abort, 1350 }, 1351 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 1352 .instance_size = sizeof(DriveBackupState), 1353 .prepare = drive_backup_prepare, 1354 .abort = drive_backup_abort, 1355 }, 1356 [TRANSACTION_ACTION_KIND_ABORT] = { 1357 .instance_size = sizeof(BlkTransactionState), 1358 .prepare = abort_prepare, 1359 .commit = abort_commit, 1360 }, 1361 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 1362 .instance_size = sizeof(InternalSnapshotState), 1363 .prepare = internal_snapshot_prepare, 1364 .abort = internal_snapshot_abort, 1365 }, 1366 }; 1367 1368 /* 1369 * 'Atomic' group snapshots. The snapshots are taken as a set, and if any fail 1370 * then we do not pivot any of the devices in the group, and abandon the 1371 * snapshots 1372 */ 1373 void qmp_transaction(TransactionActionList *dev_list, Error **errp) 1374 { 1375 TransactionActionList *dev_entry = dev_list; 1376 BlkTransactionState *state, *next; 1377 Error *local_err = NULL; 1378 1379 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; 1380 QSIMPLEQ_INIT(&snap_bdrv_states); 1381 1382 /* drain all i/o before any snapshots */ 1383 bdrv_drain_all(); 1384 1385 /* We don't do anything in this loop that commits us to the snapshot */ 1386 while (NULL != dev_entry) { 1387 TransactionAction *dev_info = NULL; 1388 const BdrvActionOps *ops; 1389 1390 dev_info = dev_entry->value; 1391 dev_entry = dev_entry->next; 1392 1393 assert(dev_info->kind < ARRAY_SIZE(actions)); 1394 1395 ops = &actions[dev_info->kind]; 1396 assert(ops->instance_size > 0); 1397 1398 state = g_malloc0(ops->instance_size); 1399 state->ops = ops; 1400 state->action = dev_info; 1401 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 1402 1403 state->ops->prepare(state, &local_err); 1404 if (error_is_set(&local_err)) { 1405 error_propagate(errp, local_err); 1406 goto delete_and_fail; 1407 } 1408 } 1409 1410 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1411 if (state->ops->commit) { 1412 state->ops->commit(state); 1413 } 1414 } 1415 1416 /* success */ 1417 goto exit; 1418 1419 delete_and_fail: 1420 /* 1421 * failure, and it is all-or-none; abandon each new bs, and keep using 1422 * the original bs for all images 1423 */ 1424 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1425 if (state->ops->abort) { 1426 state->ops->abort(state); 1427 } 1428 } 1429 exit: 1430 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 1431 if (state->ops->clean) { 1432 state->ops->clean(state); 1433 } 1434 g_free(state); 1435 } 1436 } 1437 1438 1439 static void eject_device(BlockDriverState *bs, int force, Error **errp) 1440 { 1441 if (bdrv_in_use(bs)) { 1442 error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 1443 return; 1444 } 1445 if (!bdrv_dev_has_removable_media(bs)) { 1446 error_set(errp, QERR_DEVICE_NOT_REMOVABLE, bdrv_get_device_name(bs)); 1447 return; 1448 } 1449 1450 if (bdrv_dev_is_medium_locked(bs) && !bdrv_dev_is_tray_open(bs)) { 1451 bdrv_dev_eject_request(bs, force); 1452 if (!force) { 1453 error_set(errp, QERR_DEVICE_LOCKED, bdrv_get_device_name(bs)); 1454 return; 1455 } 1456 } 1457 1458 bdrv_close(bs); 1459 } 1460 1461 void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 1462 { 1463 BlockDriverState *bs; 1464 1465 bs = bdrv_find(device); 1466 if (!bs) { 1467 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1468 return; 1469 } 1470 1471 eject_device(bs, force, errp); 1472 } 1473 1474 void qmp_block_passwd(const char *device, const char *password, Error **errp) 1475 { 1476 BlockDriverState *bs; 1477 int err; 1478 1479 bs = bdrv_find(device); 1480 if (!bs) { 1481 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1482 return; 1483 } 1484 1485 err = bdrv_set_key(bs, password); 1486 if (err == -EINVAL) { 1487 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); 1488 return; 1489 } else if (err < 0) { 1490 error_set(errp, QERR_INVALID_PASSWORD); 1491 return; 1492 } 1493 } 1494 1495 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, 1496 int bdrv_flags, BlockDriver *drv, 1497 const char *password, Error **errp) 1498 { 1499 Error *local_err = NULL; 1500 int ret; 1501 1502 ret = bdrv_open(bs, filename, NULL, bdrv_flags, drv, &local_err); 1503 if (ret < 0) { 1504 error_propagate(errp, local_err); 1505 return; 1506 } 1507 1508 if (bdrv_key_required(bs)) { 1509 if (password) { 1510 if (bdrv_set_key(bs, password) < 0) { 1511 error_set(errp, QERR_INVALID_PASSWORD); 1512 } 1513 } else { 1514 error_set(errp, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs), 1515 bdrv_get_encrypted_filename(bs)); 1516 } 1517 } else if (password) { 1518 error_set(errp, QERR_DEVICE_NOT_ENCRYPTED, bdrv_get_device_name(bs)); 1519 } 1520 } 1521 1522 void qmp_change_blockdev(const char *device, const char *filename, 1523 bool has_format, const char *format, Error **errp) 1524 { 1525 BlockDriverState *bs; 1526 BlockDriver *drv = NULL; 1527 int bdrv_flags; 1528 Error *err = NULL; 1529 1530 bs = bdrv_find(device); 1531 if (!bs) { 1532 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1533 return; 1534 } 1535 1536 if (format) { 1537 drv = bdrv_find_whitelisted_format(format, bs->read_only); 1538 if (!drv) { 1539 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1540 return; 1541 } 1542 } 1543 1544 eject_device(bs, 0, &err); 1545 if (error_is_set(&err)) { 1546 error_propagate(errp, err); 1547 return; 1548 } 1549 1550 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; 1551 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; 1552 1553 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); 1554 } 1555 1556 /* throttling disk I/O limits */ 1557 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 1558 int64_t bps_wr, 1559 int64_t iops, 1560 int64_t iops_rd, 1561 int64_t iops_wr, 1562 bool has_bps_max, 1563 int64_t bps_max, 1564 bool has_bps_rd_max, 1565 int64_t bps_rd_max, 1566 bool has_bps_wr_max, 1567 int64_t bps_wr_max, 1568 bool has_iops_max, 1569 int64_t iops_max, 1570 bool has_iops_rd_max, 1571 int64_t iops_rd_max, 1572 bool has_iops_wr_max, 1573 int64_t iops_wr_max, 1574 bool has_iops_size, 1575 int64_t iops_size, Error **errp) 1576 { 1577 ThrottleConfig cfg; 1578 BlockDriverState *bs; 1579 1580 bs = bdrv_find(device); 1581 if (!bs) { 1582 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1583 return; 1584 } 1585 1586 memset(&cfg, 0, sizeof(cfg)); 1587 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 1588 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 1589 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 1590 1591 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 1592 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 1593 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 1594 1595 if (has_bps_max) { 1596 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 1597 } 1598 if (has_bps_rd_max) { 1599 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 1600 } 1601 if (has_bps_wr_max) { 1602 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 1603 } 1604 if (has_iops_max) { 1605 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 1606 } 1607 if (has_iops_rd_max) { 1608 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 1609 } 1610 if (has_iops_wr_max) { 1611 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 1612 } 1613 1614 if (has_iops_size) { 1615 cfg.op_size = iops_size; 1616 } 1617 1618 if (!check_throttle_config(&cfg, errp)) { 1619 return; 1620 } 1621 1622 if (!bs->io_limits_enabled && throttle_enabled(&cfg)) { 1623 bdrv_io_limits_enable(bs); 1624 } else if (bs->io_limits_enabled && !throttle_enabled(&cfg)) { 1625 bdrv_io_limits_disable(bs); 1626 } 1627 1628 if (bs->io_limits_enabled) { 1629 bdrv_set_io_limits(bs, &cfg); 1630 } 1631 } 1632 1633 int do_drive_del(Monitor *mon, const QDict *qdict, QObject **ret_data) 1634 { 1635 const char *id = qdict_get_str(qdict, "id"); 1636 BlockDriverState *bs; 1637 1638 bs = bdrv_find(id); 1639 if (!bs) { 1640 qerror_report(QERR_DEVICE_NOT_FOUND, id); 1641 return -1; 1642 } 1643 if (bdrv_in_use(bs)) { 1644 qerror_report(QERR_DEVICE_IN_USE, id); 1645 return -1; 1646 } 1647 1648 /* quiesce block driver; prevent further io */ 1649 bdrv_drain_all(); 1650 bdrv_flush(bs); 1651 bdrv_close(bs); 1652 1653 /* if we have a device attached to this BlockDriverState 1654 * then we need to make the drive anonymous until the device 1655 * can be removed. If this is a drive with no device backing 1656 * then we can just get rid of the block driver state right here. 1657 */ 1658 if (bdrv_get_attached_dev(bs)) { 1659 bdrv_make_anon(bs); 1660 1661 /* Further I/O must not pause the guest */ 1662 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, 1663 BLOCKDEV_ON_ERROR_REPORT); 1664 } else { 1665 drive_uninit(drive_get_by_blockdev(bs)); 1666 } 1667 1668 return 0; 1669 } 1670 1671 void qmp_block_resize(const char *device, int64_t size, Error **errp) 1672 { 1673 BlockDriverState *bs; 1674 int ret; 1675 1676 bs = bdrv_find(device); 1677 if (!bs) { 1678 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1679 return; 1680 } 1681 1682 if (size < 0) { 1683 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 1684 return; 1685 } 1686 1687 /* complete all in-flight operations before resizing the device */ 1688 bdrv_drain_all(); 1689 1690 ret = bdrv_truncate(bs, size); 1691 switch (ret) { 1692 case 0: 1693 break; 1694 case -ENOMEDIUM: 1695 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1696 break; 1697 case -ENOTSUP: 1698 error_set(errp, QERR_UNSUPPORTED); 1699 break; 1700 case -EACCES: 1701 error_set(errp, QERR_DEVICE_IS_READ_ONLY, device); 1702 break; 1703 case -EBUSY: 1704 error_set(errp, QERR_DEVICE_IN_USE, device); 1705 break; 1706 default: 1707 error_setg_errno(errp, -ret, "Could not resize"); 1708 break; 1709 } 1710 } 1711 1712 static void block_job_cb(void *opaque, int ret) 1713 { 1714 BlockDriverState *bs = opaque; 1715 QObject *obj; 1716 1717 trace_block_job_cb(bs, bs->job, ret); 1718 1719 assert(bs->job); 1720 obj = qobject_from_block_job(bs->job); 1721 if (ret < 0) { 1722 QDict *dict = qobject_to_qdict(obj); 1723 qdict_put(dict, "error", qstring_from_str(strerror(-ret))); 1724 } 1725 1726 if (block_job_is_cancelled(bs->job)) { 1727 monitor_protocol_event(QEVENT_BLOCK_JOB_CANCELLED, obj); 1728 } else { 1729 monitor_protocol_event(QEVENT_BLOCK_JOB_COMPLETED, obj); 1730 } 1731 qobject_decref(obj); 1732 1733 bdrv_put_ref_bh_schedule(bs); 1734 } 1735 1736 void qmp_block_stream(const char *device, bool has_base, 1737 const char *base, bool has_speed, int64_t speed, 1738 bool has_on_error, BlockdevOnError on_error, 1739 Error **errp) 1740 { 1741 BlockDriverState *bs; 1742 BlockDriverState *base_bs = NULL; 1743 Error *local_err = NULL; 1744 1745 if (!has_on_error) { 1746 on_error = BLOCKDEV_ON_ERROR_REPORT; 1747 } 1748 1749 bs = bdrv_find(device); 1750 if (!bs) { 1751 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1752 return; 1753 } 1754 1755 if (base) { 1756 base_bs = bdrv_find_backing_image(bs, base); 1757 if (base_bs == NULL) { 1758 error_set(errp, QERR_BASE_NOT_FOUND, base); 1759 return; 1760 } 1761 } 1762 1763 stream_start(bs, base_bs, base, has_speed ? speed : 0, 1764 on_error, block_job_cb, bs, &local_err); 1765 if (error_is_set(&local_err)) { 1766 error_propagate(errp, local_err); 1767 return; 1768 } 1769 1770 trace_qmp_block_stream(bs, bs->job); 1771 } 1772 1773 void qmp_block_commit(const char *device, 1774 bool has_base, const char *base, const char *top, 1775 bool has_speed, int64_t speed, 1776 Error **errp) 1777 { 1778 BlockDriverState *bs; 1779 BlockDriverState *base_bs, *top_bs; 1780 Error *local_err = NULL; 1781 /* This will be part of the QMP command, if/when the 1782 * BlockdevOnError change for blkmirror makes it in 1783 */ 1784 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 1785 1786 /* drain all i/o before commits */ 1787 bdrv_drain_all(); 1788 1789 bs = bdrv_find(device); 1790 if (!bs) { 1791 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1792 return; 1793 } 1794 1795 /* default top_bs is the active layer */ 1796 top_bs = bs; 1797 1798 if (top) { 1799 if (strcmp(bs->filename, top) != 0) { 1800 top_bs = bdrv_find_backing_image(bs, top); 1801 } 1802 } 1803 1804 if (top_bs == NULL) { 1805 error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 1806 return; 1807 } 1808 1809 if (has_base && base) { 1810 base_bs = bdrv_find_backing_image(top_bs, base); 1811 } else { 1812 base_bs = bdrv_find_base(top_bs); 1813 } 1814 1815 if (base_bs == NULL) { 1816 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 1817 return; 1818 } 1819 1820 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 1821 &local_err); 1822 if (local_err != NULL) { 1823 error_propagate(errp, local_err); 1824 return; 1825 } 1826 } 1827 1828 void qmp_drive_backup(const char *device, const char *target, 1829 bool has_format, const char *format, 1830 enum MirrorSyncMode sync, 1831 bool has_mode, enum NewImageMode mode, 1832 bool has_speed, int64_t speed, 1833 bool has_on_source_error, BlockdevOnError on_source_error, 1834 bool has_on_target_error, BlockdevOnError on_target_error, 1835 Error **errp) 1836 { 1837 BlockDriverState *bs; 1838 BlockDriverState *target_bs; 1839 BlockDriverState *source = NULL; 1840 BlockDriver *drv = NULL; 1841 Error *local_err = NULL; 1842 int flags; 1843 int64_t size; 1844 int ret; 1845 1846 if (!has_speed) { 1847 speed = 0; 1848 } 1849 if (!has_on_source_error) { 1850 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 1851 } 1852 if (!has_on_target_error) { 1853 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 1854 } 1855 if (!has_mode) { 1856 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1857 } 1858 1859 bs = bdrv_find(device); 1860 if (!bs) { 1861 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1862 return; 1863 } 1864 1865 if (!bdrv_is_inserted(bs)) { 1866 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1867 return; 1868 } 1869 1870 if (!has_format) { 1871 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 1872 } 1873 if (format) { 1874 drv = bdrv_find_format(format); 1875 if (!drv) { 1876 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1877 return; 1878 } 1879 } 1880 1881 if (bdrv_in_use(bs)) { 1882 error_set(errp, QERR_DEVICE_IN_USE, device); 1883 return; 1884 } 1885 1886 flags = bs->open_flags | BDRV_O_RDWR; 1887 1888 /* See if we have a backing HD we can use to create our new image 1889 * on top of. */ 1890 if (sync == MIRROR_SYNC_MODE_TOP) { 1891 source = bs->backing_hd; 1892 if (!source) { 1893 sync = MIRROR_SYNC_MODE_FULL; 1894 } 1895 } 1896 if (sync == MIRROR_SYNC_MODE_NONE) { 1897 source = bs; 1898 } 1899 1900 size = bdrv_getlength(bs); 1901 if (size < 0) { 1902 error_setg_errno(errp, -size, "bdrv_getlength failed"); 1903 return; 1904 } 1905 1906 if (mode != NEW_IMAGE_MODE_EXISTING) { 1907 assert(format && drv); 1908 if (source) { 1909 bdrv_img_create(target, format, source->filename, 1910 source->drv->format_name, NULL, 1911 size, flags, &local_err, false); 1912 } else { 1913 bdrv_img_create(target, format, NULL, NULL, NULL, 1914 size, flags, &local_err, false); 1915 } 1916 } 1917 1918 if (error_is_set(&local_err)) { 1919 error_propagate(errp, local_err); 1920 return; 1921 } 1922 1923 target_bs = bdrv_new(""); 1924 ret = bdrv_open(target_bs, target, NULL, flags, drv, &local_err); 1925 if (ret < 0) { 1926 bdrv_unref(target_bs); 1927 error_propagate(errp, local_err); 1928 return; 1929 } 1930 1931 backup_start(bs, target_bs, speed, sync, on_source_error, on_target_error, 1932 block_job_cb, bs, &local_err); 1933 if (local_err != NULL) { 1934 bdrv_unref(target_bs); 1935 error_propagate(errp, local_err); 1936 return; 1937 } 1938 } 1939 1940 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) 1941 1942 void qmp_drive_mirror(const char *device, const char *target, 1943 bool has_format, const char *format, 1944 enum MirrorSyncMode sync, 1945 bool has_mode, enum NewImageMode mode, 1946 bool has_speed, int64_t speed, 1947 bool has_granularity, uint32_t granularity, 1948 bool has_buf_size, int64_t buf_size, 1949 bool has_on_source_error, BlockdevOnError on_source_error, 1950 bool has_on_target_error, BlockdevOnError on_target_error, 1951 Error **errp) 1952 { 1953 BlockDriverState *bs; 1954 BlockDriverState *source, *target_bs; 1955 BlockDriver *drv = NULL; 1956 Error *local_err = NULL; 1957 int flags; 1958 int64_t size; 1959 int ret; 1960 1961 if (!has_speed) { 1962 speed = 0; 1963 } 1964 if (!has_on_source_error) { 1965 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 1966 } 1967 if (!has_on_target_error) { 1968 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 1969 } 1970 if (!has_mode) { 1971 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1972 } 1973 if (!has_granularity) { 1974 granularity = 0; 1975 } 1976 if (!has_buf_size) { 1977 buf_size = DEFAULT_MIRROR_BUF_SIZE; 1978 } 1979 1980 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 1981 error_set(errp, QERR_INVALID_PARAMETER, device); 1982 return; 1983 } 1984 if (granularity & (granularity - 1)) { 1985 error_set(errp, QERR_INVALID_PARAMETER, device); 1986 return; 1987 } 1988 1989 bs = bdrv_find(device); 1990 if (!bs) { 1991 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1992 return; 1993 } 1994 1995 if (!bdrv_is_inserted(bs)) { 1996 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1997 return; 1998 } 1999 2000 if (!has_format) { 2001 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 2002 } 2003 if (format) { 2004 drv = bdrv_find_format(format); 2005 if (!drv) { 2006 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 2007 return; 2008 } 2009 } 2010 2011 if (bdrv_in_use(bs)) { 2012 error_set(errp, QERR_DEVICE_IN_USE, device); 2013 return; 2014 } 2015 2016 flags = bs->open_flags | BDRV_O_RDWR; 2017 source = bs->backing_hd; 2018 if (!source && sync == MIRROR_SYNC_MODE_TOP) { 2019 sync = MIRROR_SYNC_MODE_FULL; 2020 } 2021 2022 size = bdrv_getlength(bs); 2023 if (size < 0) { 2024 error_setg_errno(errp, -size, "bdrv_getlength failed"); 2025 return; 2026 } 2027 2028 if (sync == MIRROR_SYNC_MODE_FULL && mode != NEW_IMAGE_MODE_EXISTING) { 2029 /* create new image w/o backing file */ 2030 assert(format && drv); 2031 bdrv_img_create(target, format, 2032 NULL, NULL, NULL, size, flags, &local_err, false); 2033 } else { 2034 switch (mode) { 2035 case NEW_IMAGE_MODE_EXISTING: 2036 break; 2037 case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 2038 /* create new image with backing file */ 2039 bdrv_img_create(target, format, 2040 source->filename, 2041 source->drv->format_name, 2042 NULL, size, flags, &local_err, false); 2043 break; 2044 default: 2045 abort(); 2046 } 2047 } 2048 2049 if (error_is_set(&local_err)) { 2050 error_propagate(errp, local_err); 2051 return; 2052 } 2053 2054 /* Mirroring takes care of copy-on-write using the source's backing 2055 * file. 2056 */ 2057 target_bs = bdrv_new(""); 2058 ret = bdrv_open(target_bs, target, NULL, flags | BDRV_O_NO_BACKING, drv, 2059 &local_err); 2060 if (ret < 0) { 2061 bdrv_unref(target_bs); 2062 error_propagate(errp, local_err); 2063 return; 2064 } 2065 2066 mirror_start(bs, target_bs, speed, granularity, buf_size, sync, 2067 on_source_error, on_target_error, 2068 block_job_cb, bs, &local_err); 2069 if (local_err != NULL) { 2070 bdrv_unref(target_bs); 2071 error_propagate(errp, local_err); 2072 return; 2073 } 2074 } 2075 2076 static BlockJob *find_block_job(const char *device) 2077 { 2078 BlockDriverState *bs; 2079 2080 bs = bdrv_find(device); 2081 if (!bs || !bs->job) { 2082 return NULL; 2083 } 2084 return bs->job; 2085 } 2086 2087 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 2088 { 2089 BlockJob *job = find_block_job(device); 2090 2091 if (!job) { 2092 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 2093 return; 2094 } 2095 2096 block_job_set_speed(job, speed, errp); 2097 } 2098 2099 void qmp_block_job_cancel(const char *device, 2100 bool has_force, bool force, Error **errp) 2101 { 2102 BlockJob *job = find_block_job(device); 2103 2104 if (!has_force) { 2105 force = false; 2106 } 2107 2108 if (!job) { 2109 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 2110 return; 2111 } 2112 if (job->paused && !force) { 2113 error_set(errp, QERR_BLOCK_JOB_PAUSED, device); 2114 return; 2115 } 2116 2117 trace_qmp_block_job_cancel(job); 2118 block_job_cancel(job); 2119 } 2120 2121 void qmp_block_job_pause(const char *device, Error **errp) 2122 { 2123 BlockJob *job = find_block_job(device); 2124 2125 if (!job) { 2126 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 2127 return; 2128 } 2129 2130 trace_qmp_block_job_pause(job); 2131 block_job_pause(job); 2132 } 2133 2134 void qmp_block_job_resume(const char *device, Error **errp) 2135 { 2136 BlockJob *job = find_block_job(device); 2137 2138 if (!job) { 2139 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 2140 return; 2141 } 2142 2143 trace_qmp_block_job_resume(job); 2144 block_job_resume(job); 2145 } 2146 2147 void qmp_block_job_complete(const char *device, Error **errp) 2148 { 2149 BlockJob *job = find_block_job(device); 2150 2151 if (!job) { 2152 error_set(errp, QERR_BLOCK_JOB_NOT_ACTIVE, device); 2153 return; 2154 } 2155 2156 trace_qmp_block_job_complete(job); 2157 block_job_complete(job, errp); 2158 } 2159 2160 void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 2161 { 2162 QmpOutputVisitor *ov = qmp_output_visitor_new(); 2163 QObject *obj; 2164 QDict *qdict; 2165 Error *local_err = NULL; 2166 2167 /* Require an ID in the top level */ 2168 if (!options->has_id) { 2169 error_setg(errp, "Block device needs an ID"); 2170 goto fail; 2171 } 2172 2173 /* TODO Sort it out in raw-posix and drive_init: Reject aio=native with 2174 * cache.direct=false instead of silently switching to aio=threads, except 2175 * if called from drive_init. 2176 * 2177 * For now, simply forbidding the combination for all drivers will do. */ 2178 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 2179 bool direct = options->cache->has_direct && options->cache->direct; 2180 if (!options->has_cache && !direct) { 2181 error_setg(errp, "aio=native requires cache.direct=true"); 2182 goto fail; 2183 } 2184 } 2185 2186 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 2187 &options, NULL, &local_err); 2188 if (error_is_set(&local_err)) { 2189 error_propagate(errp, local_err); 2190 goto fail; 2191 } 2192 2193 obj = qmp_output_get_qobject(ov); 2194 qdict = qobject_to_qdict(obj); 2195 2196 qdict_flatten(qdict); 2197 2198 blockdev_init(qdict, IF_NONE, &local_err); 2199 if (error_is_set(&local_err)) { 2200 error_propagate(errp, local_err); 2201 goto fail; 2202 } 2203 2204 fail: 2205 qmp_output_visitor_cleanup(ov); 2206 } 2207 2208 static void do_qmp_query_block_jobs_one(void *opaque, BlockDriverState *bs) 2209 { 2210 BlockJobInfoList **prev = opaque; 2211 BlockJob *job = bs->job; 2212 2213 if (job) { 2214 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 2215 elem->value = block_job_query(bs->job); 2216 (*prev)->next = elem; 2217 *prev = elem; 2218 } 2219 } 2220 2221 BlockJobInfoList *qmp_query_block_jobs(Error **errp) 2222 { 2223 /* Dummy is a fake list element for holding the head pointer */ 2224 BlockJobInfoList dummy = {}; 2225 BlockJobInfoList *prev = &dummy; 2226 bdrv_iterate(do_qmp_query_block_jobs_one, &prev); 2227 return dummy.next; 2228 } 2229 2230 QemuOptsList qemu_common_drive_opts = { 2231 .name = "drive", 2232 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 2233 .desc = { 2234 { 2235 .name = "snapshot", 2236 .type = QEMU_OPT_BOOL, 2237 .help = "enable/disable snapshot mode", 2238 },{ 2239 .name = "file", 2240 .type = QEMU_OPT_STRING, 2241 .help = "disk image", 2242 },{ 2243 .name = "discard", 2244 .type = QEMU_OPT_STRING, 2245 .help = "discard operation (ignore/off, unmap/on)", 2246 },{ 2247 .name = "cache.writeback", 2248 .type = QEMU_OPT_BOOL, 2249 .help = "enables writeback mode for any caches", 2250 },{ 2251 .name = "cache.direct", 2252 .type = QEMU_OPT_BOOL, 2253 .help = "enables use of O_DIRECT (bypass the host page cache)", 2254 },{ 2255 .name = "cache.no-flush", 2256 .type = QEMU_OPT_BOOL, 2257 .help = "ignore any flush requests for the device", 2258 },{ 2259 .name = "aio", 2260 .type = QEMU_OPT_STRING, 2261 .help = "host AIO implementation (threads, native)", 2262 },{ 2263 .name = "format", 2264 .type = QEMU_OPT_STRING, 2265 .help = "disk format (raw, qcow2, ...)", 2266 },{ 2267 .name = "serial", 2268 .type = QEMU_OPT_STRING, 2269 .help = "disk serial number", 2270 },{ 2271 .name = "rerror", 2272 .type = QEMU_OPT_STRING, 2273 .help = "read error action", 2274 },{ 2275 .name = "werror", 2276 .type = QEMU_OPT_STRING, 2277 .help = "write error action", 2278 },{ 2279 .name = "read-only", 2280 .type = QEMU_OPT_BOOL, 2281 .help = "open drive file as read-only", 2282 },{ 2283 .name = "throttling.iops-total", 2284 .type = QEMU_OPT_NUMBER, 2285 .help = "limit total I/O operations per second", 2286 },{ 2287 .name = "throttling.iops-read", 2288 .type = QEMU_OPT_NUMBER, 2289 .help = "limit read operations per second", 2290 },{ 2291 .name = "throttling.iops-write", 2292 .type = QEMU_OPT_NUMBER, 2293 .help = "limit write operations per second", 2294 },{ 2295 .name = "throttling.bps-total", 2296 .type = QEMU_OPT_NUMBER, 2297 .help = "limit total bytes per second", 2298 },{ 2299 .name = "throttling.bps-read", 2300 .type = QEMU_OPT_NUMBER, 2301 .help = "limit read bytes per second", 2302 },{ 2303 .name = "throttling.bps-write", 2304 .type = QEMU_OPT_NUMBER, 2305 .help = "limit write bytes per second", 2306 },{ 2307 .name = "throttling.iops-total-max", 2308 .type = QEMU_OPT_NUMBER, 2309 .help = "I/O operations burst", 2310 },{ 2311 .name = "throttling.iops-read-max", 2312 .type = QEMU_OPT_NUMBER, 2313 .help = "I/O operations read burst", 2314 },{ 2315 .name = "throttling.iops-write-max", 2316 .type = QEMU_OPT_NUMBER, 2317 .help = "I/O operations write burst", 2318 },{ 2319 .name = "throttling.bps-total-max", 2320 .type = QEMU_OPT_NUMBER, 2321 .help = "total bytes burst", 2322 },{ 2323 .name = "throttling.bps-read-max", 2324 .type = QEMU_OPT_NUMBER, 2325 .help = "total bytes read burst", 2326 },{ 2327 .name = "throttling.bps-write-max", 2328 .type = QEMU_OPT_NUMBER, 2329 .help = "total bytes write burst", 2330 },{ 2331 .name = "throttling.iops-size", 2332 .type = QEMU_OPT_NUMBER, 2333 .help = "when limiting by iops max size of an I/O in bytes", 2334 },{ 2335 .name = "copy-on-read", 2336 .type = QEMU_OPT_BOOL, 2337 .help = "copy read data from backing file into image file", 2338 }, 2339 { /* end of list */ } 2340 }, 2341 }; 2342 2343 QemuOptsList qemu_drive_opts = { 2344 .name = "drive", 2345 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 2346 .desc = { 2347 /* 2348 * no elements => accept any params 2349 * validation will happen later 2350 */ 2351 { /* end of list */ } 2352 }, 2353 }; 2354