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/block-backend.h" 34 #include "sysemu/blockdev.h" 35 #include "hw/block/block.h" 36 #include "block/blockjob.h" 37 #include "block/throttle-groups.h" 38 #include "monitor/monitor.h" 39 #include "qemu/option.h" 40 #include "qemu/config-file.h" 41 #include "qapi/qmp/types.h" 42 #include "qapi-visit.h" 43 #include "qapi/qmp-output-visitor.h" 44 #include "qapi/util.h" 45 #include "sysemu/sysemu.h" 46 #include "block/block_int.h" 47 #include "qmp-commands.h" 48 #include "trace.h" 49 #include "sysemu/arch_init.h" 50 51 static const char *const if_name[IF_COUNT] = { 52 [IF_NONE] = "none", 53 [IF_IDE] = "ide", 54 [IF_SCSI] = "scsi", 55 [IF_FLOPPY] = "floppy", 56 [IF_PFLASH] = "pflash", 57 [IF_MTD] = "mtd", 58 [IF_SD] = "sd", 59 [IF_VIRTIO] = "virtio", 60 [IF_XEN] = "xen", 61 }; 62 63 static int if_max_devs[IF_COUNT] = { 64 /* 65 * Do not change these numbers! They govern how drive option 66 * index maps to unit and bus. That mapping is ABI. 67 * 68 * All controllers used to imlement if=T drives need to support 69 * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 70 * Otherwise, some index values map to "impossible" bus, unit 71 * values. 72 * 73 * For instance, if you change [IF_SCSI] to 255, -drive 74 * if=scsi,index=12 no longer means bus=1,unit=5, but 75 * bus=0,unit=12. With an lsi53c895a controller (7 units max), 76 * the drive can't be set up. Regression. 77 */ 78 [IF_IDE] = 2, 79 [IF_SCSI] = 7, 80 }; 81 82 /** 83 * Boards may call this to offer board-by-board overrides 84 * of the default, global values. 85 */ 86 void override_max_devs(BlockInterfaceType type, int max_devs) 87 { 88 BlockBackend *blk; 89 DriveInfo *dinfo; 90 91 if (max_devs <= 0) { 92 return; 93 } 94 95 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 96 dinfo = blk_legacy_dinfo(blk); 97 if (dinfo->type == type) { 98 fprintf(stderr, "Cannot override units-per-bus property of" 99 " the %s interface, because a drive of that type has" 100 " already been added.\n", if_name[type]); 101 g_assert_not_reached(); 102 } 103 } 104 105 if_max_devs[type] = max_devs; 106 } 107 108 /* 109 * We automatically delete the drive when a device using it gets 110 * unplugged. Questionable feature, but we can't just drop it. 111 * Device models call blockdev_mark_auto_del() to schedule the 112 * automatic deletion, and generic qdev code calls blockdev_auto_del() 113 * when deletion is actually safe. 114 */ 115 void blockdev_mark_auto_del(BlockBackend *blk) 116 { 117 DriveInfo *dinfo = blk_legacy_dinfo(blk); 118 BlockDriverState *bs = blk_bs(blk); 119 AioContext *aio_context; 120 121 if (!dinfo) { 122 return; 123 } 124 125 aio_context = bdrv_get_aio_context(bs); 126 aio_context_acquire(aio_context); 127 128 if (bs->job) { 129 block_job_cancel(bs->job); 130 } 131 132 aio_context_release(aio_context); 133 134 dinfo->auto_del = 1; 135 } 136 137 void blockdev_auto_del(BlockBackend *blk) 138 { 139 DriveInfo *dinfo = blk_legacy_dinfo(blk); 140 141 if (dinfo && dinfo->auto_del) { 142 blk_unref(blk); 143 } 144 } 145 146 /** 147 * Returns the current mapping of how many units per bus 148 * a particular interface can support. 149 * 150 * A positive integer indicates n units per bus. 151 * 0 implies the mapping has not been established. 152 * -1 indicates an invalid BlockInterfaceType was given. 153 */ 154 int drive_get_max_devs(BlockInterfaceType type) 155 { 156 if (type >= IF_IDE && type < IF_COUNT) { 157 return if_max_devs[type]; 158 } 159 160 return -1; 161 } 162 163 static int drive_index_to_bus_id(BlockInterfaceType type, int index) 164 { 165 int max_devs = if_max_devs[type]; 166 return max_devs ? index / max_devs : 0; 167 } 168 169 static int drive_index_to_unit_id(BlockInterfaceType type, int index) 170 { 171 int max_devs = if_max_devs[type]; 172 return max_devs ? index % max_devs : index; 173 } 174 175 QemuOpts *drive_def(const char *optstr) 176 { 177 return qemu_opts_parse(qemu_find_opts("drive"), optstr, 0); 178 } 179 180 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 181 const char *optstr) 182 { 183 QemuOpts *opts; 184 185 opts = drive_def(optstr); 186 if (!opts) { 187 return NULL; 188 } 189 if (type != IF_DEFAULT) { 190 qemu_opt_set(opts, "if", if_name[type], &error_abort); 191 } 192 if (index >= 0) { 193 qemu_opt_set_number(opts, "index", index, &error_abort); 194 } 195 if (file) 196 qemu_opt_set(opts, "file", file, &error_abort); 197 return opts; 198 } 199 200 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 201 { 202 BlockBackend *blk; 203 DriveInfo *dinfo; 204 205 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 206 dinfo = blk_legacy_dinfo(blk); 207 if (dinfo && dinfo->type == type 208 && dinfo->bus == bus && dinfo->unit == unit) { 209 return dinfo; 210 } 211 } 212 213 return NULL; 214 } 215 216 bool drive_check_orphaned(void) 217 { 218 BlockBackend *blk; 219 DriveInfo *dinfo; 220 bool rs = false; 221 222 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 223 dinfo = blk_legacy_dinfo(blk); 224 /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 225 /* Unless this is a default drive, this may be an oversight. */ 226 if (!blk_get_attached_dev(blk) && !dinfo->is_default && 227 dinfo->type != IF_NONE) { 228 fprintf(stderr, "Warning: Orphaned drive without device: " 229 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 230 blk_name(blk), blk_bs(blk)->filename, if_name[dinfo->type], 231 dinfo->bus, dinfo->unit); 232 rs = true; 233 } 234 } 235 236 return rs; 237 } 238 239 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 240 { 241 return drive_get(type, 242 drive_index_to_bus_id(type, index), 243 drive_index_to_unit_id(type, index)); 244 } 245 246 int drive_get_max_bus(BlockInterfaceType type) 247 { 248 int max_bus; 249 BlockBackend *blk; 250 DriveInfo *dinfo; 251 252 max_bus = -1; 253 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 254 dinfo = blk_legacy_dinfo(blk); 255 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 256 max_bus = dinfo->bus; 257 } 258 } 259 return max_bus; 260 } 261 262 /* Get a block device. This should only be used for single-drive devices 263 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 264 appropriate bus. */ 265 DriveInfo *drive_get_next(BlockInterfaceType type) 266 { 267 static int next_block_unit[IF_COUNT]; 268 269 return drive_get(type, 0, next_block_unit[type]++); 270 } 271 272 static void bdrv_format_print(void *opaque, const char *name) 273 { 274 error_printf(" %s", name); 275 } 276 277 typedef struct { 278 QEMUBH *bh; 279 BlockDriverState *bs; 280 } BDRVPutRefBH; 281 282 static void bdrv_put_ref_bh(void *opaque) 283 { 284 BDRVPutRefBH *s = opaque; 285 286 bdrv_unref(s->bs); 287 qemu_bh_delete(s->bh); 288 g_free(s); 289 } 290 291 /* 292 * Release a BDS reference in a BH 293 * 294 * It is not safe to use bdrv_unref() from a callback function when the callers 295 * still need the BlockDriverState. In such cases we schedule a BH to release 296 * the reference. 297 */ 298 static void bdrv_put_ref_bh_schedule(BlockDriverState *bs) 299 { 300 BDRVPutRefBH *s; 301 302 s = g_new(BDRVPutRefBH, 1); 303 s->bh = qemu_bh_new(bdrv_put_ref_bh, s); 304 s->bs = bs; 305 qemu_bh_schedule(s->bh); 306 } 307 308 static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 309 { 310 if (!strcmp(buf, "ignore")) { 311 return BLOCKDEV_ON_ERROR_IGNORE; 312 } else if (!is_read && !strcmp(buf, "enospc")) { 313 return BLOCKDEV_ON_ERROR_ENOSPC; 314 } else if (!strcmp(buf, "stop")) { 315 return BLOCKDEV_ON_ERROR_STOP; 316 } else if (!strcmp(buf, "report")) { 317 return BLOCKDEV_ON_ERROR_REPORT; 318 } else { 319 error_setg(errp, "'%s' invalid %s error action", 320 buf, is_read ? "read" : "write"); 321 return -1; 322 } 323 } 324 325 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) 326 { 327 if (throttle_conflicting(cfg)) { 328 error_setg(errp, "bps/iops/max total values and read/write values" 329 " cannot be used at the same time"); 330 return false; 331 } 332 333 if (!throttle_is_valid(cfg)) { 334 error_setg(errp, "bps/iops/maxs values must be 0 or greater"); 335 return false; 336 } 337 338 return true; 339 } 340 341 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 342 343 /* Takes the ownership of bs_opts */ 344 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 345 Error **errp) 346 { 347 const char *buf; 348 int ro = 0; 349 int bdrv_flags = 0; 350 int on_read_error, on_write_error; 351 BlockBackend *blk; 352 BlockDriverState *bs; 353 ThrottleConfig cfg; 354 int snapshot = 0; 355 bool copy_on_read; 356 Error *error = NULL; 357 QemuOpts *opts; 358 const char *id; 359 bool has_driver_specific_opts; 360 BlockdevDetectZeroesOptions detect_zeroes; 361 const char *throttling_group; 362 363 /* Check common options by copying from bs_opts to opts, all other options 364 * stay in bs_opts for processing by bdrv_open(). */ 365 id = qdict_get_try_str(bs_opts, "id"); 366 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 367 if (error) { 368 error_propagate(errp, error); 369 goto err_no_opts; 370 } 371 372 qemu_opts_absorb_qdict(opts, bs_opts, &error); 373 if (error) { 374 error_propagate(errp, error); 375 goto early_err; 376 } 377 378 if (id) { 379 qdict_del(bs_opts, "id"); 380 } 381 382 has_driver_specific_opts = !!qdict_size(bs_opts); 383 384 /* extract parameters */ 385 snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 386 ro = qemu_opt_get_bool(opts, "read-only", 0); 387 copy_on_read = qemu_opt_get_bool(opts, "copy-on-read", false); 388 389 if ((buf = qemu_opt_get(opts, "discard")) != NULL) { 390 if (bdrv_parse_discard_flags(buf, &bdrv_flags) != 0) { 391 error_setg(errp, "invalid discard option"); 392 goto early_err; 393 } 394 } 395 396 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_WB, true)) { 397 bdrv_flags |= BDRV_O_CACHE_WB; 398 } 399 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_DIRECT, false)) { 400 bdrv_flags |= BDRV_O_NOCACHE; 401 } 402 if (qemu_opt_get_bool(opts, BDRV_OPT_CACHE_NO_FLUSH, false)) { 403 bdrv_flags |= BDRV_O_NO_FLUSH; 404 } 405 406 #ifdef CONFIG_LINUX_AIO 407 if ((buf = qemu_opt_get(opts, "aio")) != NULL) { 408 if (!strcmp(buf, "native")) { 409 bdrv_flags |= BDRV_O_NATIVE_AIO; 410 } else if (!strcmp(buf, "threads")) { 411 /* this is the default */ 412 } else { 413 error_setg(errp, "invalid aio option"); 414 goto early_err; 415 } 416 } 417 #endif 418 419 if ((buf = qemu_opt_get(opts, "format")) != NULL) { 420 if (is_help_option(buf)) { 421 error_printf("Supported formats:"); 422 bdrv_iterate_format(bdrv_format_print, NULL); 423 error_printf("\n"); 424 goto early_err; 425 } 426 427 if (qdict_haskey(bs_opts, "driver")) { 428 error_setg(errp, "Cannot specify both 'driver' and 'format'"); 429 goto early_err; 430 } 431 qdict_put(bs_opts, "driver", qstring_from_str(buf)); 432 } 433 434 /* disk I/O throttling */ 435 memset(&cfg, 0, sizeof(cfg)); 436 cfg.buckets[THROTTLE_BPS_TOTAL].avg = 437 qemu_opt_get_number(opts, "throttling.bps-total", 0); 438 cfg.buckets[THROTTLE_BPS_READ].avg = 439 qemu_opt_get_number(opts, "throttling.bps-read", 0); 440 cfg.buckets[THROTTLE_BPS_WRITE].avg = 441 qemu_opt_get_number(opts, "throttling.bps-write", 0); 442 cfg.buckets[THROTTLE_OPS_TOTAL].avg = 443 qemu_opt_get_number(opts, "throttling.iops-total", 0); 444 cfg.buckets[THROTTLE_OPS_READ].avg = 445 qemu_opt_get_number(opts, "throttling.iops-read", 0); 446 cfg.buckets[THROTTLE_OPS_WRITE].avg = 447 qemu_opt_get_number(opts, "throttling.iops-write", 0); 448 449 cfg.buckets[THROTTLE_BPS_TOTAL].max = 450 qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 451 cfg.buckets[THROTTLE_BPS_READ].max = 452 qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 453 cfg.buckets[THROTTLE_BPS_WRITE].max = 454 qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 455 cfg.buckets[THROTTLE_OPS_TOTAL].max = 456 qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 457 cfg.buckets[THROTTLE_OPS_READ].max = 458 qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 459 cfg.buckets[THROTTLE_OPS_WRITE].max = 460 qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 461 462 cfg.op_size = qemu_opt_get_number(opts, "throttling.iops-size", 0); 463 464 throttling_group = qemu_opt_get(opts, "throttling.group"); 465 466 if (!check_throttle_config(&cfg, &error)) { 467 error_propagate(errp, error); 468 goto early_err; 469 } 470 471 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 472 if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 473 on_write_error = parse_block_error_action(buf, 0, &error); 474 if (error) { 475 error_propagate(errp, error); 476 goto early_err; 477 } 478 } 479 480 on_read_error = BLOCKDEV_ON_ERROR_REPORT; 481 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 482 on_read_error = parse_block_error_action(buf, 1, &error); 483 if (error) { 484 error_propagate(errp, error); 485 goto early_err; 486 } 487 } 488 489 detect_zeroes = 490 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 491 qemu_opt_get(opts, "detect-zeroes"), 492 BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, 493 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 494 &error); 495 if (error) { 496 error_propagate(errp, error); 497 goto early_err; 498 } 499 500 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 501 !(bdrv_flags & BDRV_O_UNMAP)) { 502 error_setg(errp, "setting detect-zeroes to unmap is not allowed " 503 "without setting discard operation to unmap"); 504 goto early_err; 505 } 506 507 /* init */ 508 if ((!file || !*file) && !has_driver_specific_opts) { 509 blk = blk_new_with_bs(qemu_opts_id(opts), errp); 510 if (!blk) { 511 goto early_err; 512 } 513 514 bs = blk_bs(blk); 515 bs->open_flags = snapshot ? BDRV_O_SNAPSHOT : 0; 516 bs->read_only = ro; 517 518 QDECREF(bs_opts); 519 } else { 520 if (file && !*file) { 521 file = NULL; 522 } 523 524 if (snapshot) { 525 /* always use cache=unsafe with snapshot */ 526 bdrv_flags &= ~BDRV_O_CACHE_MASK; 527 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH); 528 } 529 530 if (copy_on_read) { 531 bdrv_flags |= BDRV_O_COPY_ON_READ; 532 } 533 534 if (runstate_check(RUN_STATE_INMIGRATE)) { 535 bdrv_flags |= BDRV_O_INCOMING; 536 } 537 538 bdrv_flags |= ro ? 0 : BDRV_O_RDWR; 539 540 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, 541 errp); 542 if (!blk) { 543 goto err_no_bs_opts; 544 } 545 bs = blk_bs(blk); 546 } 547 548 bs->detect_zeroes = detect_zeroes; 549 550 bdrv_set_on_error(bs, on_read_error, on_write_error); 551 552 /* disk I/O throttling */ 553 if (throttle_enabled(&cfg)) { 554 if (!throttling_group) { 555 throttling_group = blk_name(blk); 556 } 557 bdrv_io_limits_enable(bs, throttling_group); 558 bdrv_set_io_limits(bs, &cfg); 559 } 560 561 if (bdrv_key_required(bs)) { 562 autostart = 0; 563 } 564 565 err_no_bs_opts: 566 qemu_opts_del(opts); 567 return blk; 568 569 early_err: 570 qemu_opts_del(opts); 571 err_no_opts: 572 QDECREF(bs_opts); 573 return NULL; 574 } 575 576 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 577 Error **errp) 578 { 579 const char *value; 580 581 value = qemu_opt_get(opts, from); 582 if (value) { 583 if (qemu_opt_find(opts, to)) { 584 error_setg(errp, "'%s' and its alias '%s' can't be used at the " 585 "same time", to, from); 586 return; 587 } 588 } 589 590 /* rename all items in opts */ 591 while ((value = qemu_opt_get(opts, from))) { 592 qemu_opt_set(opts, to, value, &error_abort); 593 qemu_opt_unset(opts, from); 594 } 595 } 596 597 QemuOptsList qemu_legacy_drive_opts = { 598 .name = "drive", 599 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 600 .desc = { 601 { 602 .name = "bus", 603 .type = QEMU_OPT_NUMBER, 604 .help = "bus number", 605 },{ 606 .name = "unit", 607 .type = QEMU_OPT_NUMBER, 608 .help = "unit number (i.e. lun for scsi)", 609 },{ 610 .name = "index", 611 .type = QEMU_OPT_NUMBER, 612 .help = "index number", 613 },{ 614 .name = "media", 615 .type = QEMU_OPT_STRING, 616 .help = "media type (disk, cdrom)", 617 },{ 618 .name = "if", 619 .type = QEMU_OPT_STRING, 620 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 621 },{ 622 .name = "cyls", 623 .type = QEMU_OPT_NUMBER, 624 .help = "number of cylinders (ide disk geometry)", 625 },{ 626 .name = "heads", 627 .type = QEMU_OPT_NUMBER, 628 .help = "number of heads (ide disk geometry)", 629 },{ 630 .name = "secs", 631 .type = QEMU_OPT_NUMBER, 632 .help = "number of sectors (ide disk geometry)", 633 },{ 634 .name = "trans", 635 .type = QEMU_OPT_STRING, 636 .help = "chs translation (auto, lba, none)", 637 },{ 638 .name = "boot", 639 .type = QEMU_OPT_BOOL, 640 .help = "(deprecated, ignored)", 641 },{ 642 .name = "addr", 643 .type = QEMU_OPT_STRING, 644 .help = "pci address (virtio only)", 645 },{ 646 .name = "serial", 647 .type = QEMU_OPT_STRING, 648 .help = "disk serial number", 649 },{ 650 .name = "file", 651 .type = QEMU_OPT_STRING, 652 .help = "file name", 653 }, 654 655 /* Options that are passed on, but have special semantics with -drive */ 656 { 657 .name = "read-only", 658 .type = QEMU_OPT_BOOL, 659 .help = "open drive file as read-only", 660 },{ 661 .name = "rerror", 662 .type = QEMU_OPT_STRING, 663 .help = "read error action", 664 },{ 665 .name = "werror", 666 .type = QEMU_OPT_STRING, 667 .help = "write error action", 668 },{ 669 .name = "copy-on-read", 670 .type = QEMU_OPT_BOOL, 671 .help = "copy read data from backing file into image file", 672 }, 673 674 { /* end of list */ } 675 }, 676 }; 677 678 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 679 { 680 const char *value; 681 BlockBackend *blk; 682 DriveInfo *dinfo = NULL; 683 QDict *bs_opts; 684 QemuOpts *legacy_opts; 685 DriveMediaType media = MEDIA_DISK; 686 BlockInterfaceType type; 687 int cyls, heads, secs, translation; 688 int max_devs, bus_id, unit_id, index; 689 const char *devaddr; 690 const char *werror, *rerror; 691 bool read_only = false; 692 bool copy_on_read; 693 const char *serial; 694 const char *filename; 695 Error *local_err = NULL; 696 int i; 697 698 /* Change legacy command line options into QMP ones */ 699 static const struct { 700 const char *from; 701 const char *to; 702 } opt_renames[] = { 703 { "iops", "throttling.iops-total" }, 704 { "iops_rd", "throttling.iops-read" }, 705 { "iops_wr", "throttling.iops-write" }, 706 707 { "bps", "throttling.bps-total" }, 708 { "bps_rd", "throttling.bps-read" }, 709 { "bps_wr", "throttling.bps-write" }, 710 711 { "iops_max", "throttling.iops-total-max" }, 712 { "iops_rd_max", "throttling.iops-read-max" }, 713 { "iops_wr_max", "throttling.iops-write-max" }, 714 715 { "bps_max", "throttling.bps-total-max" }, 716 { "bps_rd_max", "throttling.bps-read-max" }, 717 { "bps_wr_max", "throttling.bps-write-max" }, 718 719 { "iops_size", "throttling.iops-size" }, 720 721 { "group", "throttling.group" }, 722 723 { "readonly", "read-only" }, 724 }; 725 726 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 727 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 728 &local_err); 729 if (local_err) { 730 error_report_err(local_err); 731 return NULL; 732 } 733 } 734 735 value = qemu_opt_get(all_opts, "cache"); 736 if (value) { 737 int flags = 0; 738 739 if (bdrv_parse_cache_flags(value, &flags) != 0) { 740 error_report("invalid cache option"); 741 return NULL; 742 } 743 744 /* Specific options take precedence */ 745 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 746 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 747 !!(flags & BDRV_O_CACHE_WB), &error_abort); 748 } 749 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 750 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 751 !!(flags & BDRV_O_NOCACHE), &error_abort); 752 } 753 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 754 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 755 !!(flags & BDRV_O_NO_FLUSH), &error_abort); 756 } 757 qemu_opt_unset(all_opts, "cache"); 758 } 759 760 /* Get a QDict for processing the options */ 761 bs_opts = qdict_new(); 762 qemu_opts_to_qdict(all_opts, bs_opts); 763 764 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 765 &error_abort); 766 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 767 if (local_err) { 768 error_report_err(local_err); 769 goto fail; 770 } 771 772 /* Deprecated option boot=[on|off] */ 773 if (qemu_opt_get(legacy_opts, "boot") != NULL) { 774 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 775 "ignored. Future versions will reject this parameter. Please " 776 "update your scripts.\n"); 777 } 778 779 /* Media type */ 780 value = qemu_opt_get(legacy_opts, "media"); 781 if (value) { 782 if (!strcmp(value, "disk")) { 783 media = MEDIA_DISK; 784 } else if (!strcmp(value, "cdrom")) { 785 media = MEDIA_CDROM; 786 read_only = true; 787 } else { 788 error_report("'%s' invalid media", value); 789 goto fail; 790 } 791 } 792 793 /* copy-on-read is disabled with a warning for read-only devices */ 794 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 795 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 796 797 if (read_only && copy_on_read) { 798 error_report("warning: disabling copy-on-read on read-only drive"); 799 copy_on_read = false; 800 } 801 802 qdict_put(bs_opts, "read-only", 803 qstring_from_str(read_only ? "on" : "off")); 804 qdict_put(bs_opts, "copy-on-read", 805 qstring_from_str(copy_on_read ? "on" :"off")); 806 807 /* Controller type */ 808 value = qemu_opt_get(legacy_opts, "if"); 809 if (value) { 810 for (type = 0; 811 type < IF_COUNT && strcmp(value, if_name[type]); 812 type++) { 813 } 814 if (type == IF_COUNT) { 815 error_report("unsupported bus type '%s'", value); 816 goto fail; 817 } 818 } else { 819 type = block_default_type; 820 } 821 822 /* Geometry */ 823 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 824 heads = qemu_opt_get_number(legacy_opts, "heads", 0); 825 secs = qemu_opt_get_number(legacy_opts, "secs", 0); 826 827 if (cyls || heads || secs) { 828 if (cyls < 1) { 829 error_report("invalid physical cyls number"); 830 goto fail; 831 } 832 if (heads < 1) { 833 error_report("invalid physical heads number"); 834 goto fail; 835 } 836 if (secs < 1) { 837 error_report("invalid physical secs number"); 838 goto fail; 839 } 840 } 841 842 translation = BIOS_ATA_TRANSLATION_AUTO; 843 value = qemu_opt_get(legacy_opts, "trans"); 844 if (value != NULL) { 845 if (!cyls) { 846 error_report("'%s' trans must be used with cyls, heads and secs", 847 value); 848 goto fail; 849 } 850 if (!strcmp(value, "none")) { 851 translation = BIOS_ATA_TRANSLATION_NONE; 852 } else if (!strcmp(value, "lba")) { 853 translation = BIOS_ATA_TRANSLATION_LBA; 854 } else if (!strcmp(value, "large")) { 855 translation = BIOS_ATA_TRANSLATION_LARGE; 856 } else if (!strcmp(value, "rechs")) { 857 translation = BIOS_ATA_TRANSLATION_RECHS; 858 } else if (!strcmp(value, "auto")) { 859 translation = BIOS_ATA_TRANSLATION_AUTO; 860 } else { 861 error_report("'%s' invalid translation type", value); 862 goto fail; 863 } 864 } 865 866 if (media == MEDIA_CDROM) { 867 if (cyls || secs || heads) { 868 error_report("CHS can't be set with media=cdrom"); 869 goto fail; 870 } 871 } 872 873 /* Device address specified by bus/unit or index. 874 * If none was specified, try to find the first free one. */ 875 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 876 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 877 index = qemu_opt_get_number(legacy_opts, "index", -1); 878 879 max_devs = if_max_devs[type]; 880 881 if (index != -1) { 882 if (bus_id != 0 || unit_id != -1) { 883 error_report("index cannot be used with bus and unit"); 884 goto fail; 885 } 886 bus_id = drive_index_to_bus_id(type, index); 887 unit_id = drive_index_to_unit_id(type, index); 888 } 889 890 if (unit_id == -1) { 891 unit_id = 0; 892 while (drive_get(type, bus_id, unit_id) != NULL) { 893 unit_id++; 894 if (max_devs && unit_id >= max_devs) { 895 unit_id -= max_devs; 896 bus_id++; 897 } 898 } 899 } 900 901 if (max_devs && unit_id >= max_devs) { 902 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 903 goto fail; 904 } 905 906 if (drive_get(type, bus_id, unit_id) != NULL) { 907 error_report("drive with bus=%d, unit=%d (index=%d) exists", 908 bus_id, unit_id, index); 909 goto fail; 910 } 911 912 /* Serial number */ 913 serial = qemu_opt_get(legacy_opts, "serial"); 914 915 /* no id supplied -> create one */ 916 if (qemu_opts_id(all_opts) == NULL) { 917 char *new_id; 918 const char *mediastr = ""; 919 if (type == IF_IDE || type == IF_SCSI) { 920 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 921 } 922 if (max_devs) { 923 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 924 mediastr, unit_id); 925 } else { 926 new_id = g_strdup_printf("%s%s%i", if_name[type], 927 mediastr, unit_id); 928 } 929 qdict_put(bs_opts, "id", qstring_from_str(new_id)); 930 g_free(new_id); 931 } 932 933 /* Add virtio block device */ 934 devaddr = qemu_opt_get(legacy_opts, "addr"); 935 if (devaddr && type != IF_VIRTIO) { 936 error_report("addr is not supported by this bus type"); 937 goto fail; 938 } 939 940 if (type == IF_VIRTIO) { 941 QemuOpts *devopts; 942 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 943 &error_abort); 944 if (arch_type == QEMU_ARCH_S390X) { 945 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 946 } else { 947 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 948 } 949 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 950 &error_abort); 951 if (devaddr) { 952 qemu_opt_set(devopts, "addr", devaddr, &error_abort); 953 } 954 } 955 956 filename = qemu_opt_get(legacy_opts, "file"); 957 958 /* Check werror/rerror compatibility with if=... */ 959 werror = qemu_opt_get(legacy_opts, "werror"); 960 if (werror != NULL) { 961 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 962 type != IF_NONE) { 963 error_report("werror is not supported by this bus type"); 964 goto fail; 965 } 966 qdict_put(bs_opts, "werror", qstring_from_str(werror)); 967 } 968 969 rerror = qemu_opt_get(legacy_opts, "rerror"); 970 if (rerror != NULL) { 971 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 972 type != IF_NONE) { 973 error_report("rerror is not supported by this bus type"); 974 goto fail; 975 } 976 qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 977 } 978 979 /* Actual block device init: Functionality shared with blockdev-add */ 980 blk = blockdev_init(filename, bs_opts, &local_err); 981 bs_opts = NULL; 982 if (!blk) { 983 if (local_err) { 984 error_report_err(local_err); 985 } 986 goto fail; 987 } else { 988 assert(!local_err); 989 } 990 991 /* Create legacy DriveInfo */ 992 dinfo = g_malloc0(sizeof(*dinfo)); 993 dinfo->opts = all_opts; 994 995 dinfo->cyls = cyls; 996 dinfo->heads = heads; 997 dinfo->secs = secs; 998 dinfo->trans = translation; 999 1000 dinfo->type = type; 1001 dinfo->bus = bus_id; 1002 dinfo->unit = unit_id; 1003 dinfo->devaddr = devaddr; 1004 dinfo->serial = g_strdup(serial); 1005 1006 blk_set_legacy_dinfo(blk, dinfo); 1007 1008 switch(type) { 1009 case IF_IDE: 1010 case IF_SCSI: 1011 case IF_XEN: 1012 case IF_NONE: 1013 dinfo->media_cd = media == MEDIA_CDROM; 1014 break; 1015 default: 1016 break; 1017 } 1018 1019 fail: 1020 qemu_opts_del(legacy_opts); 1021 QDECREF(bs_opts); 1022 return dinfo; 1023 } 1024 1025 void hmp_commit(Monitor *mon, const QDict *qdict) 1026 { 1027 const char *device = qdict_get_str(qdict, "device"); 1028 BlockBackend *blk; 1029 int ret; 1030 1031 if (!strcmp(device, "all")) { 1032 ret = bdrv_commit_all(); 1033 } else { 1034 blk = blk_by_name(device); 1035 if (!blk) { 1036 monitor_printf(mon, "Device '%s' not found\n", device); 1037 return; 1038 } 1039 ret = bdrv_commit(blk_bs(blk)); 1040 } 1041 if (ret < 0) { 1042 monitor_printf(mon, "'commit' error for '%s': %s\n", device, 1043 strerror(-ret)); 1044 } 1045 } 1046 1047 static void blockdev_do_action(int kind, void *data, Error **errp) 1048 { 1049 TransactionAction action; 1050 TransactionActionList list; 1051 1052 action.kind = kind; 1053 action.data = data; 1054 list.value = &action; 1055 list.next = NULL; 1056 qmp_transaction(&list, errp); 1057 } 1058 1059 void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 1060 bool has_node_name, const char *node_name, 1061 const char *snapshot_file, 1062 bool has_snapshot_node_name, 1063 const char *snapshot_node_name, 1064 bool has_format, const char *format, 1065 bool has_mode, NewImageMode mode, Error **errp) 1066 { 1067 BlockdevSnapshot snapshot = { 1068 .has_device = has_device, 1069 .device = (char *) device, 1070 .has_node_name = has_node_name, 1071 .node_name = (char *) node_name, 1072 .snapshot_file = (char *) snapshot_file, 1073 .has_snapshot_node_name = has_snapshot_node_name, 1074 .snapshot_node_name = (char *) snapshot_node_name, 1075 .has_format = has_format, 1076 .format = (char *) format, 1077 .has_mode = has_mode, 1078 .mode = mode, 1079 }; 1080 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 1081 &snapshot, errp); 1082 } 1083 1084 void qmp_blockdev_snapshot_internal_sync(const char *device, 1085 const char *name, 1086 Error **errp) 1087 { 1088 BlockdevSnapshotInternal snapshot = { 1089 .device = (char *) device, 1090 .name = (char *) name 1091 }; 1092 1093 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 1094 &snapshot, errp); 1095 } 1096 1097 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 1098 bool has_id, 1099 const char *id, 1100 bool has_name, 1101 const char *name, 1102 Error **errp) 1103 { 1104 BlockDriverState *bs; 1105 BlockBackend *blk; 1106 AioContext *aio_context; 1107 QEMUSnapshotInfo sn; 1108 Error *local_err = NULL; 1109 SnapshotInfo *info = NULL; 1110 int ret; 1111 1112 blk = blk_by_name(device); 1113 if (!blk) { 1114 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1115 return NULL; 1116 } 1117 bs = blk_bs(blk); 1118 1119 if (!has_id) { 1120 id = NULL; 1121 } 1122 1123 if (!has_name) { 1124 name = NULL; 1125 } 1126 1127 if (!id && !name) { 1128 error_setg(errp, "Name or id must be provided"); 1129 return NULL; 1130 } 1131 1132 aio_context = bdrv_get_aio_context(bs); 1133 aio_context_acquire(aio_context); 1134 1135 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 1136 goto out_aio_context; 1137 } 1138 1139 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 1140 if (local_err) { 1141 error_propagate(errp, local_err); 1142 goto out_aio_context; 1143 } 1144 if (!ret) { 1145 error_setg(errp, 1146 "Snapshot with id '%s' and name '%s' does not exist on " 1147 "device '%s'", 1148 STR_OR_NULL(id), STR_OR_NULL(name), device); 1149 goto out_aio_context; 1150 } 1151 1152 bdrv_snapshot_delete(bs, id, name, &local_err); 1153 if (local_err) { 1154 error_propagate(errp, local_err); 1155 goto out_aio_context; 1156 } 1157 1158 aio_context_release(aio_context); 1159 1160 info = g_new0(SnapshotInfo, 1); 1161 info->id = g_strdup(sn.id_str); 1162 info->name = g_strdup(sn.name); 1163 info->date_nsec = sn.date_nsec; 1164 info->date_sec = sn.date_sec; 1165 info->vm_state_size = sn.vm_state_size; 1166 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 1167 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 1168 1169 return info; 1170 1171 out_aio_context: 1172 aio_context_release(aio_context); 1173 return NULL; 1174 } 1175 1176 /** 1177 * block_dirty_bitmap_lookup: 1178 * Return a dirty bitmap (if present), after validating 1179 * the node reference and bitmap names. 1180 * 1181 * @node: The name of the BDS node to search for bitmaps 1182 * @name: The name of the bitmap to search for 1183 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1184 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1185 * @errp: Output pointer for error information. Can be NULL. 1186 * 1187 * @return: A bitmap object on success, or NULL on failure. 1188 */ 1189 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1190 const char *name, 1191 BlockDriverState **pbs, 1192 AioContext **paio, 1193 Error **errp) 1194 { 1195 BlockDriverState *bs; 1196 BdrvDirtyBitmap *bitmap; 1197 AioContext *aio_context; 1198 1199 if (!node) { 1200 error_setg(errp, "Node cannot be NULL"); 1201 return NULL; 1202 } 1203 if (!name) { 1204 error_setg(errp, "Bitmap name cannot be NULL"); 1205 return NULL; 1206 } 1207 bs = bdrv_lookup_bs(node, node, NULL); 1208 if (!bs) { 1209 error_setg(errp, "Node '%s' not found", node); 1210 return NULL; 1211 } 1212 1213 aio_context = bdrv_get_aio_context(bs); 1214 aio_context_acquire(aio_context); 1215 1216 bitmap = bdrv_find_dirty_bitmap(bs, name); 1217 if (!bitmap) { 1218 error_setg(errp, "Dirty bitmap '%s' not found", name); 1219 goto fail; 1220 } 1221 1222 if (pbs) { 1223 *pbs = bs; 1224 } 1225 if (paio) { 1226 *paio = aio_context; 1227 } else { 1228 aio_context_release(aio_context); 1229 } 1230 1231 return bitmap; 1232 1233 fail: 1234 aio_context_release(aio_context); 1235 return NULL; 1236 } 1237 1238 /* New and old BlockDriverState structs for atomic group operations */ 1239 1240 typedef struct BlkTransactionState BlkTransactionState; 1241 1242 /* Only prepare() may fail. In a single transaction, only one of commit() or 1243 abort() will be called, clean() will always be called if it present. */ 1244 typedef struct BdrvActionOps { 1245 /* Size of state struct, in bytes. */ 1246 size_t instance_size; 1247 /* Prepare the work, must NOT be NULL. */ 1248 void (*prepare)(BlkTransactionState *common, Error **errp); 1249 /* Commit the changes, can be NULL. */ 1250 void (*commit)(BlkTransactionState *common); 1251 /* Abort the changes on fail, can be NULL. */ 1252 void (*abort)(BlkTransactionState *common); 1253 /* Clean up resource in the end, can be NULL. */ 1254 void (*clean)(BlkTransactionState *common); 1255 } BdrvActionOps; 1256 1257 /* 1258 * This structure must be arranged as first member in child type, assuming 1259 * that compiler will also arrange it to the same address with parent instance. 1260 * Later it will be used in free(). 1261 */ 1262 struct BlkTransactionState { 1263 TransactionAction *action; 1264 const BdrvActionOps *ops; 1265 QSIMPLEQ_ENTRY(BlkTransactionState) entry; 1266 }; 1267 1268 /* internal snapshot private data */ 1269 typedef struct InternalSnapshotState { 1270 BlkTransactionState common; 1271 BlockDriverState *bs; 1272 AioContext *aio_context; 1273 QEMUSnapshotInfo sn; 1274 } InternalSnapshotState; 1275 1276 static void internal_snapshot_prepare(BlkTransactionState *common, 1277 Error **errp) 1278 { 1279 Error *local_err = NULL; 1280 const char *device; 1281 const char *name; 1282 BlockBackend *blk; 1283 BlockDriverState *bs; 1284 QEMUSnapshotInfo old_sn, *sn; 1285 bool ret; 1286 qemu_timeval tv; 1287 BlockdevSnapshotInternal *internal; 1288 InternalSnapshotState *state; 1289 int ret1; 1290 1291 g_assert(common->action->kind == 1292 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 1293 internal = common->action->blockdev_snapshot_internal_sync; 1294 state = DO_UPCAST(InternalSnapshotState, common, common); 1295 1296 /* 1. parse input */ 1297 device = internal->device; 1298 name = internal->name; 1299 1300 /* 2. check for validation */ 1301 blk = blk_by_name(device); 1302 if (!blk) { 1303 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1304 return; 1305 } 1306 bs = blk_bs(blk); 1307 1308 /* AioContext is released in .clean() */ 1309 state->aio_context = bdrv_get_aio_context(bs); 1310 aio_context_acquire(state->aio_context); 1311 1312 if (!bdrv_is_inserted(bs)) { 1313 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1314 return; 1315 } 1316 1317 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1318 return; 1319 } 1320 1321 if (bdrv_is_read_only(bs)) { 1322 error_setg(errp, "Device '%s' is read only", device); 1323 return; 1324 } 1325 1326 if (!bdrv_can_snapshot(bs)) { 1327 error_setg(errp, "Block format '%s' used by device '%s' " 1328 "does not support internal snapshots", 1329 bs->drv->format_name, device); 1330 return; 1331 } 1332 1333 if (!strlen(name)) { 1334 error_setg(errp, "Name is empty"); 1335 return; 1336 } 1337 1338 /* check whether a snapshot with name exist */ 1339 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1340 &local_err); 1341 if (local_err) { 1342 error_propagate(errp, local_err); 1343 return; 1344 } else if (ret) { 1345 error_setg(errp, 1346 "Snapshot with name '%s' already exists on device '%s'", 1347 name, device); 1348 return; 1349 } 1350 1351 /* 3. take the snapshot */ 1352 sn = &state->sn; 1353 pstrcpy(sn->name, sizeof(sn->name), name); 1354 qemu_gettimeofday(&tv); 1355 sn->date_sec = tv.tv_sec; 1356 sn->date_nsec = tv.tv_usec * 1000; 1357 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1358 1359 ret1 = bdrv_snapshot_create(bs, sn); 1360 if (ret1 < 0) { 1361 error_setg_errno(errp, -ret1, 1362 "Failed to create snapshot '%s' on device '%s'", 1363 name, device); 1364 return; 1365 } 1366 1367 /* 4. succeed, mark a snapshot is created */ 1368 state->bs = bs; 1369 } 1370 1371 static void internal_snapshot_abort(BlkTransactionState *common) 1372 { 1373 InternalSnapshotState *state = 1374 DO_UPCAST(InternalSnapshotState, common, common); 1375 BlockDriverState *bs = state->bs; 1376 QEMUSnapshotInfo *sn = &state->sn; 1377 Error *local_error = NULL; 1378 1379 if (!bs) { 1380 return; 1381 } 1382 1383 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1384 error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1385 "device '%s' in abort: %s", 1386 sn->id_str, 1387 sn->name, 1388 bdrv_get_device_name(bs), 1389 error_get_pretty(local_error)); 1390 error_free(local_error); 1391 } 1392 } 1393 1394 static void internal_snapshot_clean(BlkTransactionState *common) 1395 { 1396 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 1397 common, common); 1398 1399 if (state->aio_context) { 1400 aio_context_release(state->aio_context); 1401 } 1402 } 1403 1404 /* external snapshot private data */ 1405 typedef struct ExternalSnapshotState { 1406 BlkTransactionState common; 1407 BlockDriverState *old_bs; 1408 BlockDriverState *new_bs; 1409 AioContext *aio_context; 1410 } ExternalSnapshotState; 1411 1412 static void external_snapshot_prepare(BlkTransactionState *common, 1413 Error **errp) 1414 { 1415 BlockDriver *drv; 1416 int flags, ret; 1417 QDict *options = NULL; 1418 Error *local_err = NULL; 1419 bool has_device = false; 1420 const char *device; 1421 bool has_node_name = false; 1422 const char *node_name; 1423 bool has_snapshot_node_name = false; 1424 const char *snapshot_node_name; 1425 const char *new_image_file; 1426 const char *format = "qcow2"; 1427 enum NewImageMode mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1428 ExternalSnapshotState *state = 1429 DO_UPCAST(ExternalSnapshotState, common, common); 1430 TransactionAction *action = common->action; 1431 1432 /* get parameters */ 1433 g_assert(action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC); 1434 1435 has_device = action->blockdev_snapshot_sync->has_device; 1436 device = action->blockdev_snapshot_sync->device; 1437 has_node_name = action->blockdev_snapshot_sync->has_node_name; 1438 node_name = action->blockdev_snapshot_sync->node_name; 1439 has_snapshot_node_name = 1440 action->blockdev_snapshot_sync->has_snapshot_node_name; 1441 snapshot_node_name = action->blockdev_snapshot_sync->snapshot_node_name; 1442 1443 new_image_file = action->blockdev_snapshot_sync->snapshot_file; 1444 if (action->blockdev_snapshot_sync->has_format) { 1445 format = action->blockdev_snapshot_sync->format; 1446 } 1447 if (action->blockdev_snapshot_sync->has_mode) { 1448 mode = action->blockdev_snapshot_sync->mode; 1449 } 1450 1451 /* start processing */ 1452 drv = bdrv_find_format(format); 1453 if (!drv) { 1454 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1455 return; 1456 } 1457 1458 state->old_bs = bdrv_lookup_bs(has_device ? device : NULL, 1459 has_node_name ? node_name : NULL, 1460 &local_err); 1461 if (local_err) { 1462 error_propagate(errp, local_err); 1463 return; 1464 } 1465 1466 if (has_node_name && !has_snapshot_node_name) { 1467 error_setg(errp, "New snapshot node name missing"); 1468 return; 1469 } 1470 1471 if (has_snapshot_node_name && bdrv_find_node(snapshot_node_name)) { 1472 error_setg(errp, "New snapshot node name already existing"); 1473 return; 1474 } 1475 1476 /* Acquire AioContext now so any threads operating on old_bs stop */ 1477 state->aio_context = bdrv_get_aio_context(state->old_bs); 1478 aio_context_acquire(state->aio_context); 1479 1480 if (!bdrv_is_inserted(state->old_bs)) { 1481 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1482 return; 1483 } 1484 1485 if (bdrv_op_is_blocked(state->old_bs, 1486 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 1487 return; 1488 } 1489 1490 if (!bdrv_is_read_only(state->old_bs)) { 1491 if (bdrv_flush(state->old_bs)) { 1492 error_set(errp, QERR_IO_ERROR); 1493 return; 1494 } 1495 } 1496 1497 if (!bdrv_is_first_non_filter(state->old_bs)) { 1498 error_set(errp, QERR_FEATURE_DISABLED, "snapshot"); 1499 return; 1500 } 1501 1502 flags = state->old_bs->open_flags; 1503 1504 /* create new image w/backing file */ 1505 if (mode != NEW_IMAGE_MODE_EXISTING) { 1506 bdrv_img_create(new_image_file, format, 1507 state->old_bs->filename, 1508 state->old_bs->drv->format_name, 1509 NULL, -1, flags, &local_err, false); 1510 if (local_err) { 1511 error_propagate(errp, local_err); 1512 return; 1513 } 1514 } 1515 1516 if (has_snapshot_node_name) { 1517 options = qdict_new(); 1518 qdict_put(options, "node-name", 1519 qstring_from_str(snapshot_node_name)); 1520 } 1521 1522 /* TODO Inherit bs->options or only take explicit options with an 1523 * extended QMP command? */ 1524 assert(state->new_bs == NULL); 1525 ret = bdrv_open(&state->new_bs, new_image_file, NULL, options, 1526 flags | BDRV_O_NO_BACKING, drv, &local_err); 1527 /* We will manually add the backing_hd field to the bs later */ 1528 if (ret != 0) { 1529 error_propagate(errp, local_err); 1530 } 1531 } 1532 1533 static void external_snapshot_commit(BlkTransactionState *common) 1534 { 1535 ExternalSnapshotState *state = 1536 DO_UPCAST(ExternalSnapshotState, common, common); 1537 1538 bdrv_set_aio_context(state->new_bs, state->aio_context); 1539 1540 /* This removes our old bs and adds the new bs */ 1541 bdrv_append(state->new_bs, state->old_bs); 1542 /* We don't need (or want) to use the transactional 1543 * bdrv_reopen_multiple() across all the entries at once, because we 1544 * don't want to abort all of them if one of them fails the reopen */ 1545 bdrv_reopen(state->new_bs, state->new_bs->open_flags & ~BDRV_O_RDWR, 1546 NULL); 1547 1548 aio_context_release(state->aio_context); 1549 } 1550 1551 static void external_snapshot_abort(BlkTransactionState *common) 1552 { 1553 ExternalSnapshotState *state = 1554 DO_UPCAST(ExternalSnapshotState, common, common); 1555 if (state->new_bs) { 1556 bdrv_unref(state->new_bs); 1557 } 1558 if (state->aio_context) { 1559 aio_context_release(state->aio_context); 1560 } 1561 } 1562 1563 typedef struct DriveBackupState { 1564 BlkTransactionState common; 1565 BlockDriverState *bs; 1566 AioContext *aio_context; 1567 BlockJob *job; 1568 } DriveBackupState; 1569 1570 static void drive_backup_prepare(BlkTransactionState *common, Error **errp) 1571 { 1572 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1573 BlockDriverState *bs; 1574 BlockBackend *blk; 1575 DriveBackup *backup; 1576 Error *local_err = NULL; 1577 1578 assert(common->action->kind == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 1579 backup = common->action->drive_backup; 1580 1581 blk = blk_by_name(backup->device); 1582 if (!blk) { 1583 error_set(errp, QERR_DEVICE_NOT_FOUND, backup->device); 1584 return; 1585 } 1586 bs = blk_bs(blk); 1587 1588 /* AioContext is released in .clean() */ 1589 state->aio_context = bdrv_get_aio_context(bs); 1590 aio_context_acquire(state->aio_context); 1591 1592 qmp_drive_backup(backup->device, backup->target, 1593 backup->has_format, backup->format, 1594 backup->sync, 1595 backup->has_mode, backup->mode, 1596 backup->has_speed, backup->speed, 1597 backup->has_bitmap, backup->bitmap, 1598 backup->has_on_source_error, backup->on_source_error, 1599 backup->has_on_target_error, backup->on_target_error, 1600 &local_err); 1601 if (local_err) { 1602 error_propagate(errp, local_err); 1603 return; 1604 } 1605 1606 state->bs = bs; 1607 state->job = state->bs->job; 1608 } 1609 1610 static void drive_backup_abort(BlkTransactionState *common) 1611 { 1612 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1613 BlockDriverState *bs = state->bs; 1614 1615 /* Only cancel if it's the job we started */ 1616 if (bs && bs->job && bs->job == state->job) { 1617 block_job_cancel_sync(bs->job); 1618 } 1619 } 1620 1621 static void drive_backup_clean(BlkTransactionState *common) 1622 { 1623 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1624 1625 if (state->aio_context) { 1626 aio_context_release(state->aio_context); 1627 } 1628 } 1629 1630 typedef struct BlockdevBackupState { 1631 BlkTransactionState common; 1632 BlockDriverState *bs; 1633 BlockJob *job; 1634 AioContext *aio_context; 1635 } BlockdevBackupState; 1636 1637 static void blockdev_backup_prepare(BlkTransactionState *common, Error **errp) 1638 { 1639 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1640 BlockdevBackup *backup; 1641 BlockDriverState *bs, *target; 1642 BlockBackend *blk; 1643 Error *local_err = NULL; 1644 1645 assert(common->action->kind == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 1646 backup = common->action->blockdev_backup; 1647 1648 blk = blk_by_name(backup->device); 1649 if (!blk) { 1650 error_setg(errp, "Device '%s' not found", backup->device); 1651 return; 1652 } 1653 bs = blk_bs(blk); 1654 1655 blk = blk_by_name(backup->target); 1656 if (!blk) { 1657 error_setg(errp, "Device '%s' not found", backup->target); 1658 return; 1659 } 1660 target = blk_bs(blk); 1661 1662 /* AioContext is released in .clean() */ 1663 state->aio_context = bdrv_get_aio_context(bs); 1664 if (state->aio_context != bdrv_get_aio_context(target)) { 1665 state->aio_context = NULL; 1666 error_setg(errp, "Backup between two IO threads is not implemented"); 1667 return; 1668 } 1669 aio_context_acquire(state->aio_context); 1670 1671 qmp_blockdev_backup(backup->device, backup->target, 1672 backup->sync, 1673 backup->has_speed, backup->speed, 1674 backup->has_on_source_error, backup->on_source_error, 1675 backup->has_on_target_error, backup->on_target_error, 1676 &local_err); 1677 if (local_err) { 1678 error_propagate(errp, local_err); 1679 return; 1680 } 1681 1682 state->bs = bs; 1683 state->job = state->bs->job; 1684 } 1685 1686 static void blockdev_backup_abort(BlkTransactionState *common) 1687 { 1688 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1689 BlockDriverState *bs = state->bs; 1690 1691 /* Only cancel if it's the job we started */ 1692 if (bs && bs->job && bs->job == state->job) { 1693 block_job_cancel_sync(bs->job); 1694 } 1695 } 1696 1697 static void blockdev_backup_clean(BlkTransactionState *common) 1698 { 1699 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1700 1701 if (state->aio_context) { 1702 aio_context_release(state->aio_context); 1703 } 1704 } 1705 1706 static void abort_prepare(BlkTransactionState *common, Error **errp) 1707 { 1708 error_setg(errp, "Transaction aborted using Abort action"); 1709 } 1710 1711 static void abort_commit(BlkTransactionState *common) 1712 { 1713 g_assert_not_reached(); /* this action never succeeds */ 1714 } 1715 1716 static const BdrvActionOps actions[] = { 1717 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 1718 .instance_size = sizeof(ExternalSnapshotState), 1719 .prepare = external_snapshot_prepare, 1720 .commit = external_snapshot_commit, 1721 .abort = external_snapshot_abort, 1722 }, 1723 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 1724 .instance_size = sizeof(DriveBackupState), 1725 .prepare = drive_backup_prepare, 1726 .abort = drive_backup_abort, 1727 .clean = drive_backup_clean, 1728 }, 1729 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 1730 .instance_size = sizeof(BlockdevBackupState), 1731 .prepare = blockdev_backup_prepare, 1732 .abort = blockdev_backup_abort, 1733 .clean = blockdev_backup_clean, 1734 }, 1735 [TRANSACTION_ACTION_KIND_ABORT] = { 1736 .instance_size = sizeof(BlkTransactionState), 1737 .prepare = abort_prepare, 1738 .commit = abort_commit, 1739 }, 1740 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 1741 .instance_size = sizeof(InternalSnapshotState), 1742 .prepare = internal_snapshot_prepare, 1743 .abort = internal_snapshot_abort, 1744 .clean = internal_snapshot_clean, 1745 }, 1746 }; 1747 1748 /* 1749 * 'Atomic' group operations. The operations are performed as a set, and if 1750 * any fail then we roll back all operations in the group. 1751 */ 1752 void qmp_transaction(TransactionActionList *dev_list, Error **errp) 1753 { 1754 TransactionActionList *dev_entry = dev_list; 1755 BlkTransactionState *state, *next; 1756 Error *local_err = NULL; 1757 1758 QSIMPLEQ_HEAD(snap_bdrv_states, BlkTransactionState) snap_bdrv_states; 1759 QSIMPLEQ_INIT(&snap_bdrv_states); 1760 1761 /* drain all i/o before any operations */ 1762 bdrv_drain_all(); 1763 1764 /* We don't do anything in this loop that commits us to the operations */ 1765 while (NULL != dev_entry) { 1766 TransactionAction *dev_info = NULL; 1767 const BdrvActionOps *ops; 1768 1769 dev_info = dev_entry->value; 1770 dev_entry = dev_entry->next; 1771 1772 assert(dev_info->kind < ARRAY_SIZE(actions)); 1773 1774 ops = &actions[dev_info->kind]; 1775 assert(ops->instance_size > 0); 1776 1777 state = g_malloc0(ops->instance_size); 1778 state->ops = ops; 1779 state->action = dev_info; 1780 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 1781 1782 state->ops->prepare(state, &local_err); 1783 if (local_err) { 1784 error_propagate(errp, local_err); 1785 goto delete_and_fail; 1786 } 1787 } 1788 1789 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1790 if (state->ops->commit) { 1791 state->ops->commit(state); 1792 } 1793 } 1794 1795 /* success */ 1796 goto exit; 1797 1798 delete_and_fail: 1799 /* failure, and it is all-or-none; roll back all operations */ 1800 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 1801 if (state->ops->abort) { 1802 state->ops->abort(state); 1803 } 1804 } 1805 exit: 1806 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 1807 if (state->ops->clean) { 1808 state->ops->clean(state); 1809 } 1810 g_free(state); 1811 } 1812 } 1813 1814 1815 static void eject_device(BlockBackend *blk, int force, Error **errp) 1816 { 1817 BlockDriverState *bs = blk_bs(blk); 1818 AioContext *aio_context; 1819 1820 aio_context = bdrv_get_aio_context(bs); 1821 aio_context_acquire(aio_context); 1822 1823 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 1824 goto out; 1825 } 1826 if (!blk_dev_has_removable_media(blk)) { 1827 error_setg(errp, "Device '%s' is not removable", 1828 bdrv_get_device_name(bs)); 1829 goto out; 1830 } 1831 1832 if (blk_dev_is_medium_locked(blk) && !blk_dev_is_tray_open(blk)) { 1833 blk_dev_eject_request(blk, force); 1834 if (!force) { 1835 error_setg(errp, "Device '%s' is locked", 1836 bdrv_get_device_name(bs)); 1837 goto out; 1838 } 1839 } 1840 1841 bdrv_close(bs); 1842 1843 out: 1844 aio_context_release(aio_context); 1845 } 1846 1847 void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 1848 { 1849 BlockBackend *blk; 1850 1851 blk = blk_by_name(device); 1852 if (!blk) { 1853 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1854 return; 1855 } 1856 1857 eject_device(blk, force, errp); 1858 } 1859 1860 void qmp_block_passwd(bool has_device, const char *device, 1861 bool has_node_name, const char *node_name, 1862 const char *password, Error **errp) 1863 { 1864 Error *local_err = NULL; 1865 BlockDriverState *bs; 1866 AioContext *aio_context; 1867 1868 bs = bdrv_lookup_bs(has_device ? device : NULL, 1869 has_node_name ? node_name : NULL, 1870 &local_err); 1871 if (local_err) { 1872 error_propagate(errp, local_err); 1873 return; 1874 } 1875 1876 aio_context = bdrv_get_aio_context(bs); 1877 aio_context_acquire(aio_context); 1878 1879 bdrv_add_key(bs, password, errp); 1880 1881 aio_context_release(aio_context); 1882 } 1883 1884 /* Assumes AioContext is held */ 1885 static void qmp_bdrv_open_encrypted(BlockDriverState *bs, const char *filename, 1886 int bdrv_flags, BlockDriver *drv, 1887 const char *password, Error **errp) 1888 { 1889 Error *local_err = NULL; 1890 int ret; 1891 1892 ret = bdrv_open(&bs, filename, NULL, NULL, bdrv_flags, drv, &local_err); 1893 if (ret < 0) { 1894 error_propagate(errp, local_err); 1895 return; 1896 } 1897 1898 bdrv_add_key(bs, password, errp); 1899 } 1900 1901 void qmp_change_blockdev(const char *device, const char *filename, 1902 const char *format, Error **errp) 1903 { 1904 BlockBackend *blk; 1905 BlockDriverState *bs; 1906 AioContext *aio_context; 1907 BlockDriver *drv = NULL; 1908 int bdrv_flags; 1909 Error *err = NULL; 1910 1911 blk = blk_by_name(device); 1912 if (!blk) { 1913 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1914 return; 1915 } 1916 bs = blk_bs(blk); 1917 1918 aio_context = bdrv_get_aio_context(bs); 1919 aio_context_acquire(aio_context); 1920 1921 if (format) { 1922 drv = bdrv_find_whitelisted_format(format, bs->read_only); 1923 if (!drv) { 1924 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 1925 goto out; 1926 } 1927 } 1928 1929 eject_device(blk, 0, &err); 1930 if (err) { 1931 error_propagate(errp, err); 1932 goto out; 1933 } 1934 1935 bdrv_flags = bdrv_is_read_only(bs) ? 0 : BDRV_O_RDWR; 1936 bdrv_flags |= bdrv_is_snapshot(bs) ? BDRV_O_SNAPSHOT : 0; 1937 1938 qmp_bdrv_open_encrypted(bs, filename, bdrv_flags, drv, NULL, errp); 1939 1940 out: 1941 aio_context_release(aio_context); 1942 } 1943 1944 /* throttling disk I/O limits */ 1945 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 1946 int64_t bps_wr, 1947 int64_t iops, 1948 int64_t iops_rd, 1949 int64_t iops_wr, 1950 bool has_bps_max, 1951 int64_t bps_max, 1952 bool has_bps_rd_max, 1953 int64_t bps_rd_max, 1954 bool has_bps_wr_max, 1955 int64_t bps_wr_max, 1956 bool has_iops_max, 1957 int64_t iops_max, 1958 bool has_iops_rd_max, 1959 int64_t iops_rd_max, 1960 bool has_iops_wr_max, 1961 int64_t iops_wr_max, 1962 bool has_iops_size, 1963 int64_t iops_size, 1964 bool has_group, 1965 const char *group, Error **errp) 1966 { 1967 ThrottleConfig cfg; 1968 BlockDriverState *bs; 1969 BlockBackend *blk; 1970 AioContext *aio_context; 1971 1972 blk = blk_by_name(device); 1973 if (!blk) { 1974 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 1975 return; 1976 } 1977 bs = blk_bs(blk); 1978 1979 memset(&cfg, 0, sizeof(cfg)); 1980 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 1981 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 1982 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 1983 1984 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 1985 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 1986 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 1987 1988 if (has_bps_max) { 1989 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 1990 } 1991 if (has_bps_rd_max) { 1992 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 1993 } 1994 if (has_bps_wr_max) { 1995 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 1996 } 1997 if (has_iops_max) { 1998 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 1999 } 2000 if (has_iops_rd_max) { 2001 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 2002 } 2003 if (has_iops_wr_max) { 2004 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 2005 } 2006 2007 if (has_iops_size) { 2008 cfg.op_size = iops_size; 2009 } 2010 2011 if (!check_throttle_config(&cfg, errp)) { 2012 return; 2013 } 2014 2015 aio_context = bdrv_get_aio_context(bs); 2016 aio_context_acquire(aio_context); 2017 2018 if (throttle_enabled(&cfg)) { 2019 /* Enable I/O limits if they're not enabled yet, otherwise 2020 * just update the throttling group. */ 2021 if (!bs->io_limits_enabled) { 2022 bdrv_io_limits_enable(bs, has_group ? group : device); 2023 } else if (has_group) { 2024 bdrv_io_limits_update_group(bs, group); 2025 } 2026 /* Set the new throttling configuration */ 2027 bdrv_set_io_limits(bs, &cfg); 2028 } else if (bs->io_limits_enabled) { 2029 /* If all throttling settings are set to 0, disable I/O limits */ 2030 bdrv_io_limits_disable(bs); 2031 } 2032 2033 aio_context_release(aio_context); 2034 } 2035 2036 void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2037 bool has_granularity, uint32_t granularity, 2038 Error **errp) 2039 { 2040 AioContext *aio_context; 2041 BlockDriverState *bs; 2042 2043 if (!name || name[0] == '\0') { 2044 error_setg(errp, "Bitmap name cannot be empty"); 2045 return; 2046 } 2047 2048 bs = bdrv_lookup_bs(node, node, errp); 2049 if (!bs) { 2050 return; 2051 } 2052 2053 aio_context = bdrv_get_aio_context(bs); 2054 aio_context_acquire(aio_context); 2055 2056 if (has_granularity) { 2057 if (granularity < 512 || !is_power_of_2(granularity)) { 2058 error_setg(errp, "Granularity must be power of 2 " 2059 "and at least 512"); 2060 goto out; 2061 } 2062 } else { 2063 /* Default to cluster size, if available: */ 2064 granularity = bdrv_get_default_bitmap_granularity(bs); 2065 } 2066 2067 bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2068 2069 out: 2070 aio_context_release(aio_context); 2071 } 2072 2073 void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2074 Error **errp) 2075 { 2076 AioContext *aio_context; 2077 BlockDriverState *bs; 2078 BdrvDirtyBitmap *bitmap; 2079 2080 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2081 if (!bitmap || !bs) { 2082 return; 2083 } 2084 2085 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2086 error_setg(errp, 2087 "Bitmap '%s' is currently frozen and cannot be removed", 2088 name); 2089 goto out; 2090 } 2091 bdrv_dirty_bitmap_make_anon(bitmap); 2092 bdrv_release_dirty_bitmap(bs, bitmap); 2093 2094 out: 2095 aio_context_release(aio_context); 2096 } 2097 2098 /** 2099 * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2100 * immediately after a full backup operation. 2101 */ 2102 void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2103 Error **errp) 2104 { 2105 AioContext *aio_context; 2106 BdrvDirtyBitmap *bitmap; 2107 BlockDriverState *bs; 2108 2109 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2110 if (!bitmap || !bs) { 2111 return; 2112 } 2113 2114 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2115 error_setg(errp, 2116 "Bitmap '%s' is currently frozen and cannot be modified", 2117 name); 2118 goto out; 2119 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2120 error_setg(errp, 2121 "Bitmap '%s' is currently disabled and cannot be cleared", 2122 name); 2123 goto out; 2124 } 2125 2126 bdrv_clear_dirty_bitmap(bitmap); 2127 2128 out: 2129 aio_context_release(aio_context); 2130 } 2131 2132 void hmp_drive_del(Monitor *mon, const QDict *qdict) 2133 { 2134 const char *id = qdict_get_str(qdict, "id"); 2135 BlockBackend *blk; 2136 BlockDriverState *bs; 2137 AioContext *aio_context; 2138 Error *local_err = NULL; 2139 2140 blk = blk_by_name(id); 2141 if (!blk) { 2142 error_report("Device '%s' not found", id); 2143 return; 2144 } 2145 bs = blk_bs(blk); 2146 2147 if (!blk_legacy_dinfo(blk)) { 2148 error_report("Deleting device added with blockdev-add" 2149 " is not supported"); 2150 return; 2151 } 2152 2153 aio_context = bdrv_get_aio_context(bs); 2154 aio_context_acquire(aio_context); 2155 2156 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2157 error_report_err(local_err); 2158 aio_context_release(aio_context); 2159 return; 2160 } 2161 2162 /* quiesce block driver; prevent further io */ 2163 bdrv_drain_all(); 2164 bdrv_flush(bs); 2165 bdrv_close(bs); 2166 2167 /* if we have a device attached to this BlockDriverState 2168 * then we need to make the drive anonymous until the device 2169 * can be removed. If this is a drive with no device backing 2170 * then we can just get rid of the block driver state right here. 2171 */ 2172 if (blk_get_attached_dev(blk)) { 2173 blk_hide_on_behalf_of_hmp_drive_del(blk); 2174 /* Further I/O must not pause the guest */ 2175 bdrv_set_on_error(bs, BLOCKDEV_ON_ERROR_REPORT, 2176 BLOCKDEV_ON_ERROR_REPORT); 2177 } else { 2178 blk_unref(blk); 2179 } 2180 2181 aio_context_release(aio_context); 2182 } 2183 2184 void qmp_block_resize(bool has_device, const char *device, 2185 bool has_node_name, const char *node_name, 2186 int64_t size, Error **errp) 2187 { 2188 Error *local_err = NULL; 2189 BlockDriverState *bs; 2190 AioContext *aio_context; 2191 int ret; 2192 2193 bs = bdrv_lookup_bs(has_device ? device : NULL, 2194 has_node_name ? node_name : NULL, 2195 &local_err); 2196 if (local_err) { 2197 error_propagate(errp, local_err); 2198 return; 2199 } 2200 2201 aio_context = bdrv_get_aio_context(bs); 2202 aio_context_acquire(aio_context); 2203 2204 if (!bdrv_is_first_non_filter(bs)) { 2205 error_set(errp, QERR_FEATURE_DISABLED, "resize"); 2206 goto out; 2207 } 2208 2209 if (size < 0) { 2210 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2211 goto out; 2212 } 2213 2214 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2215 error_set(errp, QERR_DEVICE_IN_USE, device); 2216 goto out; 2217 } 2218 2219 /* complete all in-flight operations before resizing the device */ 2220 bdrv_drain_all(); 2221 2222 ret = bdrv_truncate(bs, size); 2223 switch (ret) { 2224 case 0: 2225 break; 2226 case -ENOMEDIUM: 2227 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2228 break; 2229 case -ENOTSUP: 2230 error_set(errp, QERR_UNSUPPORTED); 2231 break; 2232 case -EACCES: 2233 error_setg(errp, "Device '%s' is read only", device); 2234 break; 2235 case -EBUSY: 2236 error_set(errp, QERR_DEVICE_IN_USE, device); 2237 break; 2238 default: 2239 error_setg_errno(errp, -ret, "Could not resize"); 2240 break; 2241 } 2242 2243 out: 2244 aio_context_release(aio_context); 2245 } 2246 2247 static void block_job_cb(void *opaque, int ret) 2248 { 2249 /* Note that this function may be executed from another AioContext besides 2250 * the QEMU main loop. If you need to access anything that assumes the 2251 * QEMU global mutex, use a BH or introduce a mutex. 2252 */ 2253 2254 BlockDriverState *bs = opaque; 2255 const char *msg = NULL; 2256 2257 trace_block_job_cb(bs, bs->job, ret); 2258 2259 assert(bs->job); 2260 2261 if (ret < 0) { 2262 msg = strerror(-ret); 2263 } 2264 2265 if (block_job_is_cancelled(bs->job)) { 2266 block_job_event_cancelled(bs->job); 2267 } else { 2268 block_job_event_completed(bs->job, msg); 2269 } 2270 2271 bdrv_put_ref_bh_schedule(bs); 2272 } 2273 2274 void qmp_block_stream(const char *device, 2275 bool has_base, const char *base, 2276 bool has_backing_file, const char *backing_file, 2277 bool has_speed, int64_t speed, 2278 bool has_on_error, BlockdevOnError on_error, 2279 Error **errp) 2280 { 2281 BlockBackend *blk; 2282 BlockDriverState *bs; 2283 BlockDriverState *base_bs = NULL; 2284 AioContext *aio_context; 2285 Error *local_err = NULL; 2286 const char *base_name = NULL; 2287 2288 if (!has_on_error) { 2289 on_error = BLOCKDEV_ON_ERROR_REPORT; 2290 } 2291 2292 blk = blk_by_name(device); 2293 if (!blk) { 2294 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 2295 return; 2296 } 2297 bs = blk_bs(blk); 2298 2299 aio_context = bdrv_get_aio_context(bs); 2300 aio_context_acquire(aio_context); 2301 2302 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2303 goto out; 2304 } 2305 2306 if (has_base) { 2307 base_bs = bdrv_find_backing_image(bs, base); 2308 if (base_bs == NULL) { 2309 error_set(errp, QERR_BASE_NOT_FOUND, base); 2310 goto out; 2311 } 2312 assert(bdrv_get_aio_context(base_bs) == aio_context); 2313 base_name = base; 2314 } 2315 2316 /* if we are streaming the entire chain, the result will have no backing 2317 * file, and specifying one is therefore an error */ 2318 if (base_bs == NULL && has_backing_file) { 2319 error_setg(errp, "backing file specified, but streaming the " 2320 "entire chain"); 2321 goto out; 2322 } 2323 2324 /* backing_file string overrides base bs filename */ 2325 base_name = has_backing_file ? backing_file : base_name; 2326 2327 stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 2328 on_error, block_job_cb, bs, &local_err); 2329 if (local_err) { 2330 error_propagate(errp, local_err); 2331 goto out; 2332 } 2333 2334 trace_qmp_block_stream(bs, bs->job); 2335 2336 out: 2337 aio_context_release(aio_context); 2338 } 2339 2340 void qmp_block_commit(const char *device, 2341 bool has_base, const char *base, 2342 bool has_top, const char *top, 2343 bool has_backing_file, const char *backing_file, 2344 bool has_speed, int64_t speed, 2345 Error **errp) 2346 { 2347 BlockBackend *blk; 2348 BlockDriverState *bs; 2349 BlockDriverState *base_bs, *top_bs; 2350 AioContext *aio_context; 2351 Error *local_err = NULL; 2352 /* This will be part of the QMP command, if/when the 2353 * BlockdevOnError change for blkmirror makes it in 2354 */ 2355 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2356 2357 if (!has_speed) { 2358 speed = 0; 2359 } 2360 2361 /* Important Note: 2362 * libvirt relies on the DeviceNotFound error class in order to probe for 2363 * live commit feature versions; for this to work, we must make sure to 2364 * perform the device lookup before any generic errors that may occur in a 2365 * scenario in which all optional arguments are omitted. */ 2366 blk = blk_by_name(device); 2367 if (!blk) { 2368 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 2369 return; 2370 } 2371 bs = blk_bs(blk); 2372 2373 aio_context = bdrv_get_aio_context(bs); 2374 aio_context_acquire(aio_context); 2375 2376 /* drain all i/o before commits */ 2377 bdrv_drain_all(); 2378 2379 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 2380 goto out; 2381 } 2382 2383 /* default top_bs is the active layer */ 2384 top_bs = bs; 2385 2386 if (has_top && top) { 2387 if (strcmp(bs->filename, top) != 0) { 2388 top_bs = bdrv_find_backing_image(bs, top); 2389 } 2390 } 2391 2392 if (top_bs == NULL) { 2393 error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 2394 goto out; 2395 } 2396 2397 assert(bdrv_get_aio_context(top_bs) == aio_context); 2398 2399 if (has_base && base) { 2400 base_bs = bdrv_find_backing_image(top_bs, base); 2401 } else { 2402 base_bs = bdrv_find_base(top_bs); 2403 } 2404 2405 if (base_bs == NULL) { 2406 error_set(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 2407 goto out; 2408 } 2409 2410 assert(bdrv_get_aio_context(base_bs) == aio_context); 2411 2412 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 2413 goto out; 2414 } 2415 2416 /* Do not allow attempts to commit an image into itself */ 2417 if (top_bs == base_bs) { 2418 error_setg(errp, "cannot commit an image into itself"); 2419 goto out; 2420 } 2421 2422 if (top_bs == bs) { 2423 if (has_backing_file) { 2424 error_setg(errp, "'backing-file' specified," 2425 " but 'top' is the active layer"); 2426 goto out; 2427 } 2428 commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 2429 bs, &local_err); 2430 } else { 2431 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 2432 has_backing_file ? backing_file : NULL, &local_err); 2433 } 2434 if (local_err != NULL) { 2435 error_propagate(errp, local_err); 2436 goto out; 2437 } 2438 2439 out: 2440 aio_context_release(aio_context); 2441 } 2442 2443 void qmp_drive_backup(const char *device, const char *target, 2444 bool has_format, const char *format, 2445 enum MirrorSyncMode sync, 2446 bool has_mode, enum NewImageMode mode, 2447 bool has_speed, int64_t speed, 2448 bool has_bitmap, const char *bitmap, 2449 bool has_on_source_error, BlockdevOnError on_source_error, 2450 bool has_on_target_error, BlockdevOnError on_target_error, 2451 Error **errp) 2452 { 2453 BlockBackend *blk; 2454 BlockDriverState *bs; 2455 BlockDriverState *target_bs; 2456 BlockDriverState *source = NULL; 2457 BdrvDirtyBitmap *bmap = NULL; 2458 AioContext *aio_context; 2459 BlockDriver *drv = NULL; 2460 Error *local_err = NULL; 2461 int flags; 2462 int64_t size; 2463 int ret; 2464 2465 if (!has_speed) { 2466 speed = 0; 2467 } 2468 if (!has_on_source_error) { 2469 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 2470 } 2471 if (!has_on_target_error) { 2472 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 2473 } 2474 if (!has_mode) { 2475 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 2476 } 2477 2478 blk = blk_by_name(device); 2479 if (!blk) { 2480 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 2481 return; 2482 } 2483 bs = blk_bs(blk); 2484 2485 aio_context = bdrv_get_aio_context(bs); 2486 aio_context_acquire(aio_context); 2487 2488 /* Although backup_run has this check too, we need to use bs->drv below, so 2489 * do an early check redundantly. */ 2490 if (!bdrv_is_inserted(bs)) { 2491 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2492 goto out; 2493 } 2494 2495 if (!has_format) { 2496 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 2497 } 2498 if (format) { 2499 drv = bdrv_find_format(format); 2500 if (!drv) { 2501 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 2502 goto out; 2503 } 2504 } 2505 2506 /* Early check to avoid creating target */ 2507 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 2508 goto out; 2509 } 2510 2511 flags = bs->open_flags | BDRV_O_RDWR; 2512 2513 /* See if we have a backing HD we can use to create our new image 2514 * on top of. */ 2515 if (sync == MIRROR_SYNC_MODE_TOP) { 2516 source = bs->backing_hd; 2517 if (!source) { 2518 sync = MIRROR_SYNC_MODE_FULL; 2519 } 2520 } 2521 if (sync == MIRROR_SYNC_MODE_NONE) { 2522 source = bs; 2523 } 2524 2525 size = bdrv_getlength(bs); 2526 if (size < 0) { 2527 error_setg_errno(errp, -size, "bdrv_getlength failed"); 2528 goto out; 2529 } 2530 2531 if (mode != NEW_IMAGE_MODE_EXISTING) { 2532 assert(format && drv); 2533 if (source) { 2534 bdrv_img_create(target, format, source->filename, 2535 source->drv->format_name, NULL, 2536 size, flags, &local_err, false); 2537 } else { 2538 bdrv_img_create(target, format, NULL, NULL, NULL, 2539 size, flags, &local_err, false); 2540 } 2541 } 2542 2543 if (local_err) { 2544 error_propagate(errp, local_err); 2545 goto out; 2546 } 2547 2548 target_bs = NULL; 2549 ret = bdrv_open(&target_bs, target, NULL, NULL, flags, drv, &local_err); 2550 if (ret < 0) { 2551 error_propagate(errp, local_err); 2552 goto out; 2553 } 2554 2555 bdrv_set_aio_context(target_bs, aio_context); 2556 2557 if (has_bitmap) { 2558 bmap = bdrv_find_dirty_bitmap(bs, bitmap); 2559 if (!bmap) { 2560 error_setg(errp, "Bitmap '%s' could not be found", bitmap); 2561 goto out; 2562 } 2563 } 2564 2565 backup_start(bs, target_bs, speed, sync, bmap, 2566 on_source_error, on_target_error, 2567 block_job_cb, bs, &local_err); 2568 if (local_err != NULL) { 2569 bdrv_unref(target_bs); 2570 error_propagate(errp, local_err); 2571 goto out; 2572 } 2573 2574 out: 2575 aio_context_release(aio_context); 2576 } 2577 2578 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 2579 { 2580 return bdrv_named_nodes_list(errp); 2581 } 2582 2583 void qmp_blockdev_backup(const char *device, const char *target, 2584 enum MirrorSyncMode sync, 2585 bool has_speed, int64_t speed, 2586 bool has_on_source_error, 2587 BlockdevOnError on_source_error, 2588 bool has_on_target_error, 2589 BlockdevOnError on_target_error, 2590 Error **errp) 2591 { 2592 BlockBackend *blk; 2593 BlockDriverState *bs; 2594 BlockDriverState *target_bs; 2595 Error *local_err = NULL; 2596 AioContext *aio_context; 2597 2598 if (!has_speed) { 2599 speed = 0; 2600 } 2601 if (!has_on_source_error) { 2602 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 2603 } 2604 if (!has_on_target_error) { 2605 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 2606 } 2607 2608 blk = blk_by_name(device); 2609 if (!blk) { 2610 error_setg(errp, "Device '%s' not found", device); 2611 return; 2612 } 2613 bs = blk_bs(blk); 2614 2615 aio_context = bdrv_get_aio_context(bs); 2616 aio_context_acquire(aio_context); 2617 2618 blk = blk_by_name(target); 2619 if (!blk) { 2620 error_setg(errp, "Device '%s' not found", target); 2621 goto out; 2622 } 2623 target_bs = blk_bs(blk); 2624 2625 bdrv_ref(target_bs); 2626 bdrv_set_aio_context(target_bs, aio_context); 2627 backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 2628 on_target_error, block_job_cb, bs, &local_err); 2629 if (local_err != NULL) { 2630 bdrv_unref(target_bs); 2631 error_propagate(errp, local_err); 2632 } 2633 out: 2634 aio_context_release(aio_context); 2635 } 2636 2637 #define DEFAULT_MIRROR_BUF_SIZE (10 << 20) 2638 2639 void qmp_drive_mirror(const char *device, const char *target, 2640 bool has_format, const char *format, 2641 bool has_node_name, const char *node_name, 2642 bool has_replaces, const char *replaces, 2643 enum MirrorSyncMode sync, 2644 bool has_mode, enum NewImageMode mode, 2645 bool has_speed, int64_t speed, 2646 bool has_granularity, uint32_t granularity, 2647 bool has_buf_size, int64_t buf_size, 2648 bool has_on_source_error, BlockdevOnError on_source_error, 2649 bool has_on_target_error, BlockdevOnError on_target_error, 2650 Error **errp) 2651 { 2652 BlockBackend *blk; 2653 BlockDriverState *bs; 2654 BlockDriverState *source, *target_bs; 2655 AioContext *aio_context; 2656 BlockDriver *drv = NULL; 2657 Error *local_err = NULL; 2658 QDict *options = NULL; 2659 int flags; 2660 int64_t size; 2661 int ret; 2662 2663 if (!has_speed) { 2664 speed = 0; 2665 } 2666 if (!has_on_source_error) { 2667 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 2668 } 2669 if (!has_on_target_error) { 2670 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 2671 } 2672 if (!has_mode) { 2673 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 2674 } 2675 if (!has_granularity) { 2676 granularity = 0; 2677 } 2678 if (!has_buf_size) { 2679 buf_size = DEFAULT_MIRROR_BUF_SIZE; 2680 } 2681 2682 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 2683 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 2684 "a value in range [512B, 64MB]"); 2685 return; 2686 } 2687 if (granularity & (granularity - 1)) { 2688 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", "power of 2"); 2689 return; 2690 } 2691 2692 blk = blk_by_name(device); 2693 if (!blk) { 2694 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 2695 return; 2696 } 2697 bs = blk_bs(blk); 2698 2699 aio_context = bdrv_get_aio_context(bs); 2700 aio_context_acquire(aio_context); 2701 2702 if (!bdrv_is_inserted(bs)) { 2703 error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2704 goto out; 2705 } 2706 2707 if (!has_format) { 2708 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 2709 } 2710 if (format) { 2711 drv = bdrv_find_format(format); 2712 if (!drv) { 2713 error_set(errp, QERR_INVALID_BLOCK_FORMAT, format); 2714 goto out; 2715 } 2716 } 2717 2718 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR, errp)) { 2719 goto out; 2720 } 2721 2722 flags = bs->open_flags | BDRV_O_RDWR; 2723 source = bs->backing_hd; 2724 if (!source && sync == MIRROR_SYNC_MODE_TOP) { 2725 sync = MIRROR_SYNC_MODE_FULL; 2726 } 2727 if (sync == MIRROR_SYNC_MODE_NONE) { 2728 source = bs; 2729 } 2730 2731 size = bdrv_getlength(bs); 2732 if (size < 0) { 2733 error_setg_errno(errp, -size, "bdrv_getlength failed"); 2734 goto out; 2735 } 2736 2737 if (has_replaces) { 2738 BlockDriverState *to_replace_bs; 2739 AioContext *replace_aio_context; 2740 int64_t replace_size; 2741 2742 if (!has_node_name) { 2743 error_setg(errp, "a node-name must be provided when replacing a" 2744 " named node of the graph"); 2745 goto out; 2746 } 2747 2748 to_replace_bs = check_to_replace_node(replaces, &local_err); 2749 2750 if (!to_replace_bs) { 2751 error_propagate(errp, local_err); 2752 goto out; 2753 } 2754 2755 replace_aio_context = bdrv_get_aio_context(to_replace_bs); 2756 aio_context_acquire(replace_aio_context); 2757 replace_size = bdrv_getlength(to_replace_bs); 2758 aio_context_release(replace_aio_context); 2759 2760 if (size != replace_size) { 2761 error_setg(errp, "cannot replace image with a mirror image of " 2762 "different size"); 2763 goto out; 2764 } 2765 } 2766 2767 if ((sync == MIRROR_SYNC_MODE_FULL || !source) 2768 && mode != NEW_IMAGE_MODE_EXISTING) 2769 { 2770 /* create new image w/o backing file */ 2771 assert(format && drv); 2772 bdrv_img_create(target, format, 2773 NULL, NULL, NULL, size, flags, &local_err, false); 2774 } else { 2775 switch (mode) { 2776 case NEW_IMAGE_MODE_EXISTING: 2777 break; 2778 case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 2779 /* create new image with backing file */ 2780 bdrv_img_create(target, format, 2781 source->filename, 2782 source->drv->format_name, 2783 NULL, size, flags, &local_err, false); 2784 break; 2785 default: 2786 abort(); 2787 } 2788 } 2789 2790 if (local_err) { 2791 error_propagate(errp, local_err); 2792 goto out; 2793 } 2794 2795 if (has_node_name) { 2796 options = qdict_new(); 2797 qdict_put(options, "node-name", qstring_from_str(node_name)); 2798 } 2799 2800 /* Mirroring takes care of copy-on-write using the source's backing 2801 * file. 2802 */ 2803 target_bs = NULL; 2804 ret = bdrv_open(&target_bs, target, NULL, options, 2805 flags | BDRV_O_NO_BACKING, drv, &local_err); 2806 if (ret < 0) { 2807 error_propagate(errp, local_err); 2808 goto out; 2809 } 2810 2811 bdrv_set_aio_context(target_bs, aio_context); 2812 2813 /* pass the node name to replace to mirror start since it's loose coupling 2814 * and will allow to check whether the node still exist at mirror completion 2815 */ 2816 mirror_start(bs, target_bs, 2817 has_replaces ? replaces : NULL, 2818 speed, granularity, buf_size, sync, 2819 on_source_error, on_target_error, 2820 block_job_cb, bs, &local_err); 2821 if (local_err != NULL) { 2822 bdrv_unref(target_bs); 2823 error_propagate(errp, local_err); 2824 goto out; 2825 } 2826 2827 out: 2828 aio_context_release(aio_context); 2829 } 2830 2831 /* Get the block job for a given device name and acquire its AioContext */ 2832 static BlockJob *find_block_job(const char *device, AioContext **aio_context, 2833 Error **errp) 2834 { 2835 BlockBackend *blk; 2836 BlockDriverState *bs; 2837 2838 blk = blk_by_name(device); 2839 if (!blk) { 2840 goto notfound; 2841 } 2842 bs = blk_bs(blk); 2843 2844 *aio_context = bdrv_get_aio_context(bs); 2845 aio_context_acquire(*aio_context); 2846 2847 if (!bs->job) { 2848 aio_context_release(*aio_context); 2849 goto notfound; 2850 } 2851 2852 return bs->job; 2853 2854 notfound: 2855 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 2856 "No active block job on device '%s'", device); 2857 *aio_context = NULL; 2858 return NULL; 2859 } 2860 2861 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 2862 { 2863 AioContext *aio_context; 2864 BlockJob *job = find_block_job(device, &aio_context, errp); 2865 2866 if (!job) { 2867 return; 2868 } 2869 2870 block_job_set_speed(job, speed, errp); 2871 aio_context_release(aio_context); 2872 } 2873 2874 void qmp_block_job_cancel(const char *device, 2875 bool has_force, bool force, Error **errp) 2876 { 2877 AioContext *aio_context; 2878 BlockJob *job = find_block_job(device, &aio_context, errp); 2879 2880 if (!job) { 2881 return; 2882 } 2883 2884 if (!has_force) { 2885 force = false; 2886 } 2887 2888 if (job->user_paused && !force) { 2889 error_setg(errp, "The block job for device '%s' is currently paused", 2890 device); 2891 goto out; 2892 } 2893 2894 trace_qmp_block_job_cancel(job); 2895 block_job_cancel(job); 2896 out: 2897 aio_context_release(aio_context); 2898 } 2899 2900 void qmp_block_job_pause(const char *device, Error **errp) 2901 { 2902 AioContext *aio_context; 2903 BlockJob *job = find_block_job(device, &aio_context, errp); 2904 2905 if (!job || job->user_paused) { 2906 return; 2907 } 2908 2909 job->user_paused = true; 2910 trace_qmp_block_job_pause(job); 2911 block_job_pause(job); 2912 aio_context_release(aio_context); 2913 } 2914 2915 void qmp_block_job_resume(const char *device, Error **errp) 2916 { 2917 AioContext *aio_context; 2918 BlockJob *job = find_block_job(device, &aio_context, errp); 2919 2920 if (!job || !job->user_paused) { 2921 return; 2922 } 2923 2924 job->user_paused = false; 2925 trace_qmp_block_job_resume(job); 2926 block_job_resume(job); 2927 aio_context_release(aio_context); 2928 } 2929 2930 void qmp_block_job_complete(const char *device, Error **errp) 2931 { 2932 AioContext *aio_context; 2933 BlockJob *job = find_block_job(device, &aio_context, errp); 2934 2935 if (!job) { 2936 return; 2937 } 2938 2939 trace_qmp_block_job_complete(job); 2940 block_job_complete(job, errp); 2941 aio_context_release(aio_context); 2942 } 2943 2944 void qmp_change_backing_file(const char *device, 2945 const char *image_node_name, 2946 const char *backing_file, 2947 Error **errp) 2948 { 2949 BlockBackend *blk; 2950 BlockDriverState *bs = NULL; 2951 AioContext *aio_context; 2952 BlockDriverState *image_bs = NULL; 2953 Error *local_err = NULL; 2954 bool ro; 2955 int open_flags; 2956 int ret; 2957 2958 blk = blk_by_name(device); 2959 if (!blk) { 2960 error_set(errp, QERR_DEVICE_NOT_FOUND, device); 2961 return; 2962 } 2963 bs = blk_bs(blk); 2964 2965 aio_context = bdrv_get_aio_context(bs); 2966 aio_context_acquire(aio_context); 2967 2968 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 2969 if (local_err) { 2970 error_propagate(errp, local_err); 2971 goto out; 2972 } 2973 2974 if (!image_bs) { 2975 error_setg(errp, "image file not found"); 2976 goto out; 2977 } 2978 2979 if (bdrv_find_base(image_bs) == image_bs) { 2980 error_setg(errp, "not allowing backing file change on an image " 2981 "without a backing file"); 2982 goto out; 2983 } 2984 2985 /* even though we are not necessarily operating on bs, we need it to 2986 * determine if block ops are currently prohibited on the chain */ 2987 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 2988 goto out; 2989 } 2990 2991 /* final sanity check */ 2992 if (!bdrv_chain_contains(bs, image_bs)) { 2993 error_setg(errp, "'%s' and image file are not in the same chain", 2994 device); 2995 goto out; 2996 } 2997 2998 /* if not r/w, reopen to make r/w */ 2999 open_flags = image_bs->open_flags; 3000 ro = bdrv_is_read_only(image_bs); 3001 3002 if (ro) { 3003 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3004 if (local_err) { 3005 error_propagate(errp, local_err); 3006 goto out; 3007 } 3008 } 3009 3010 ret = bdrv_change_backing_file(image_bs, backing_file, 3011 image_bs->drv ? image_bs->drv->format_name : ""); 3012 3013 if (ret < 0) { 3014 error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3015 backing_file); 3016 /* don't exit here, so we can try to restore open flags if 3017 * appropriate */ 3018 } 3019 3020 if (ro) { 3021 bdrv_reopen(image_bs, open_flags, &local_err); 3022 if (local_err) { 3023 error_propagate(errp, local_err); /* will preserve prior errp */ 3024 } 3025 } 3026 3027 out: 3028 aio_context_release(aio_context); 3029 } 3030 3031 void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3032 { 3033 QmpOutputVisitor *ov = qmp_output_visitor_new(); 3034 BlockBackend *blk; 3035 QObject *obj; 3036 QDict *qdict; 3037 Error *local_err = NULL; 3038 3039 /* Require an ID in the top level */ 3040 if (!options->has_id) { 3041 error_setg(errp, "Block device needs an ID"); 3042 goto fail; 3043 } 3044 3045 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3046 * cache.direct=false instead of silently switching to aio=threads, except 3047 * when called from drive_new(). 3048 * 3049 * For now, simply forbidding the combination for all drivers will do. */ 3050 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3051 bool direct = options->has_cache && 3052 options->cache->has_direct && 3053 options->cache->direct; 3054 if (!direct) { 3055 error_setg(errp, "aio=native requires cache.direct=true"); 3056 goto fail; 3057 } 3058 } 3059 3060 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3061 &options, NULL, &local_err); 3062 if (local_err) { 3063 error_propagate(errp, local_err); 3064 goto fail; 3065 } 3066 3067 obj = qmp_output_get_qobject(ov); 3068 qdict = qobject_to_qdict(obj); 3069 3070 qdict_flatten(qdict); 3071 3072 blk = blockdev_init(NULL, qdict, &local_err); 3073 if (local_err) { 3074 error_propagate(errp, local_err); 3075 goto fail; 3076 } 3077 3078 if (bdrv_key_required(blk_bs(blk))) { 3079 blk_unref(blk); 3080 error_setg(errp, "blockdev-add doesn't support encrypted devices"); 3081 goto fail; 3082 } 3083 3084 fail: 3085 qmp_output_visitor_cleanup(ov); 3086 } 3087 3088 BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3089 { 3090 BlockJobInfoList *head = NULL, **p_next = &head; 3091 BlockDriverState *bs; 3092 3093 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 3094 AioContext *aio_context = bdrv_get_aio_context(bs); 3095 3096 aio_context_acquire(aio_context); 3097 3098 if (bs->job) { 3099 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3100 elem->value = block_job_query(bs->job); 3101 *p_next = elem; 3102 p_next = &elem->next; 3103 } 3104 3105 aio_context_release(aio_context); 3106 } 3107 3108 return head; 3109 } 3110 3111 QemuOptsList qemu_common_drive_opts = { 3112 .name = "drive", 3113 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 3114 .desc = { 3115 { 3116 .name = "snapshot", 3117 .type = QEMU_OPT_BOOL, 3118 .help = "enable/disable snapshot mode", 3119 },{ 3120 .name = "discard", 3121 .type = QEMU_OPT_STRING, 3122 .help = "discard operation (ignore/off, unmap/on)", 3123 },{ 3124 .name = BDRV_OPT_CACHE_WB, 3125 .type = QEMU_OPT_BOOL, 3126 .help = "enables writeback mode for any caches", 3127 },{ 3128 .name = BDRV_OPT_CACHE_DIRECT, 3129 .type = QEMU_OPT_BOOL, 3130 .help = "enables use of O_DIRECT (bypass the host page cache)", 3131 },{ 3132 .name = BDRV_OPT_CACHE_NO_FLUSH, 3133 .type = QEMU_OPT_BOOL, 3134 .help = "ignore any flush requests for the device", 3135 },{ 3136 .name = "aio", 3137 .type = QEMU_OPT_STRING, 3138 .help = "host AIO implementation (threads, native)", 3139 },{ 3140 .name = "format", 3141 .type = QEMU_OPT_STRING, 3142 .help = "disk format (raw, qcow2, ...)", 3143 },{ 3144 .name = "rerror", 3145 .type = QEMU_OPT_STRING, 3146 .help = "read error action", 3147 },{ 3148 .name = "werror", 3149 .type = QEMU_OPT_STRING, 3150 .help = "write error action", 3151 },{ 3152 .name = "read-only", 3153 .type = QEMU_OPT_BOOL, 3154 .help = "open drive file as read-only", 3155 },{ 3156 .name = "throttling.iops-total", 3157 .type = QEMU_OPT_NUMBER, 3158 .help = "limit total I/O operations per second", 3159 },{ 3160 .name = "throttling.iops-read", 3161 .type = QEMU_OPT_NUMBER, 3162 .help = "limit read operations per second", 3163 },{ 3164 .name = "throttling.iops-write", 3165 .type = QEMU_OPT_NUMBER, 3166 .help = "limit write operations per second", 3167 },{ 3168 .name = "throttling.bps-total", 3169 .type = QEMU_OPT_NUMBER, 3170 .help = "limit total bytes per second", 3171 },{ 3172 .name = "throttling.bps-read", 3173 .type = QEMU_OPT_NUMBER, 3174 .help = "limit read bytes per second", 3175 },{ 3176 .name = "throttling.bps-write", 3177 .type = QEMU_OPT_NUMBER, 3178 .help = "limit write bytes per second", 3179 },{ 3180 .name = "throttling.iops-total-max", 3181 .type = QEMU_OPT_NUMBER, 3182 .help = "I/O operations burst", 3183 },{ 3184 .name = "throttling.iops-read-max", 3185 .type = QEMU_OPT_NUMBER, 3186 .help = "I/O operations read burst", 3187 },{ 3188 .name = "throttling.iops-write-max", 3189 .type = QEMU_OPT_NUMBER, 3190 .help = "I/O operations write burst", 3191 },{ 3192 .name = "throttling.bps-total-max", 3193 .type = QEMU_OPT_NUMBER, 3194 .help = "total bytes burst", 3195 },{ 3196 .name = "throttling.bps-read-max", 3197 .type = QEMU_OPT_NUMBER, 3198 .help = "total bytes read burst", 3199 },{ 3200 .name = "throttling.bps-write-max", 3201 .type = QEMU_OPT_NUMBER, 3202 .help = "total bytes write burst", 3203 },{ 3204 .name = "throttling.iops-size", 3205 .type = QEMU_OPT_NUMBER, 3206 .help = "when limiting by iops max size of an I/O in bytes", 3207 },{ 3208 .name = "throttling.group", 3209 .type = QEMU_OPT_STRING, 3210 .help = "name of the block throttling group", 3211 },{ 3212 .name = "copy-on-read", 3213 .type = QEMU_OPT_BOOL, 3214 .help = "copy read data from backing file into image file", 3215 },{ 3216 .name = "detect-zeroes", 3217 .type = QEMU_OPT_STRING, 3218 .help = "try to optimize zero writes (off, on, unmap)", 3219 }, 3220 { /* end of list */ } 3221 }, 3222 }; 3223 3224 QemuOptsList qemu_drive_opts = { 3225 .name = "drive", 3226 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 3227 .desc = { 3228 /* 3229 * no elements => accept any params 3230 * validation will happen later 3231 */ 3232 { /* end of list */ } 3233 }, 3234 }; 3235