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/error-report.h" 40 #include "qemu/option.h" 41 #include "qemu/config-file.h" 42 #include "qapi/qmp/types.h" 43 #include "qapi-visit.h" 44 #include "qapi/qmp/qerror.h" 45 #include "qapi/qmp-output-visitor.h" 46 #include "qapi/util.h" 47 #include "sysemu/sysemu.h" 48 #include "block/block_int.h" 49 #include "qmp-commands.h" 50 #include "trace.h" 51 #include "sysemu/arch_init.h" 52 53 static const char *const if_name[IF_COUNT] = { 54 [IF_NONE] = "none", 55 [IF_IDE] = "ide", 56 [IF_SCSI] = "scsi", 57 [IF_FLOPPY] = "floppy", 58 [IF_PFLASH] = "pflash", 59 [IF_MTD] = "mtd", 60 [IF_SD] = "sd", 61 [IF_VIRTIO] = "virtio", 62 [IF_XEN] = "xen", 63 }; 64 65 static int if_max_devs[IF_COUNT] = { 66 /* 67 * Do not change these numbers! They govern how drive option 68 * index maps to unit and bus. That mapping is ABI. 69 * 70 * All controllers used to imlement if=T drives need to support 71 * if_max_devs[T] units, for any T with if_max_devs[T] != 0. 72 * Otherwise, some index values map to "impossible" bus, unit 73 * values. 74 * 75 * For instance, if you change [IF_SCSI] to 255, -drive 76 * if=scsi,index=12 no longer means bus=1,unit=5, but 77 * bus=0,unit=12. With an lsi53c895a controller (7 units max), 78 * the drive can't be set up. Regression. 79 */ 80 [IF_IDE] = 2, 81 [IF_SCSI] = 7, 82 }; 83 84 /** 85 * Boards may call this to offer board-by-board overrides 86 * of the default, global values. 87 */ 88 void override_max_devs(BlockInterfaceType type, int max_devs) 89 { 90 BlockBackend *blk; 91 DriveInfo *dinfo; 92 93 if (max_devs <= 0) { 94 return; 95 } 96 97 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 98 dinfo = blk_legacy_dinfo(blk); 99 if (dinfo->type == type) { 100 fprintf(stderr, "Cannot override units-per-bus property of" 101 " the %s interface, because a drive of that type has" 102 " already been added.\n", if_name[type]); 103 g_assert_not_reached(); 104 } 105 } 106 107 if_max_devs[type] = max_devs; 108 } 109 110 /* 111 * We automatically delete the drive when a device using it gets 112 * unplugged. Questionable feature, but we can't just drop it. 113 * Device models call blockdev_mark_auto_del() to schedule the 114 * automatic deletion, and generic qdev code calls blockdev_auto_del() 115 * when deletion is actually safe. 116 */ 117 void blockdev_mark_auto_del(BlockBackend *blk) 118 { 119 DriveInfo *dinfo = blk_legacy_dinfo(blk); 120 BlockDriverState *bs = blk_bs(blk); 121 AioContext *aio_context; 122 123 if (!dinfo) { 124 return; 125 } 126 127 if (bs) { 128 aio_context = bdrv_get_aio_context(bs); 129 aio_context_acquire(aio_context); 130 131 if (bs->job) { 132 block_job_cancel(bs->job); 133 } 134 135 aio_context_release(aio_context); 136 } 137 138 dinfo->auto_del = 1; 139 } 140 141 void blockdev_auto_del(BlockBackend *blk) 142 { 143 DriveInfo *dinfo = blk_legacy_dinfo(blk); 144 145 if (dinfo && dinfo->auto_del) { 146 blk_unref(blk); 147 } 148 } 149 150 /** 151 * Returns the current mapping of how many units per bus 152 * a particular interface can support. 153 * 154 * A positive integer indicates n units per bus. 155 * 0 implies the mapping has not been established. 156 * -1 indicates an invalid BlockInterfaceType was given. 157 */ 158 int drive_get_max_devs(BlockInterfaceType type) 159 { 160 if (type >= IF_IDE && type < IF_COUNT) { 161 return if_max_devs[type]; 162 } 163 164 return -1; 165 } 166 167 static int drive_index_to_bus_id(BlockInterfaceType type, int index) 168 { 169 int max_devs = if_max_devs[type]; 170 return max_devs ? index / max_devs : 0; 171 } 172 173 static int drive_index_to_unit_id(BlockInterfaceType type, int index) 174 { 175 int max_devs = if_max_devs[type]; 176 return max_devs ? index % max_devs : index; 177 } 178 179 QemuOpts *drive_def(const char *optstr) 180 { 181 return qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 182 } 183 184 QemuOpts *drive_add(BlockInterfaceType type, int index, const char *file, 185 const char *optstr) 186 { 187 QemuOpts *opts; 188 189 opts = drive_def(optstr); 190 if (!opts) { 191 return NULL; 192 } 193 if (type != IF_DEFAULT) { 194 qemu_opt_set(opts, "if", if_name[type], &error_abort); 195 } 196 if (index >= 0) { 197 qemu_opt_set_number(opts, "index", index, &error_abort); 198 } 199 if (file) 200 qemu_opt_set(opts, "file", file, &error_abort); 201 return opts; 202 } 203 204 DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit) 205 { 206 BlockBackend *blk; 207 DriveInfo *dinfo; 208 209 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 210 dinfo = blk_legacy_dinfo(blk); 211 if (dinfo && dinfo->type == type 212 && dinfo->bus == bus && dinfo->unit == unit) { 213 return dinfo; 214 } 215 } 216 217 return NULL; 218 } 219 220 bool drive_check_orphaned(void) 221 { 222 BlockBackend *blk; 223 DriveInfo *dinfo; 224 bool rs = false; 225 226 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 227 dinfo = blk_legacy_dinfo(blk); 228 /* If dinfo->bdrv->dev is NULL, it has no device attached. */ 229 /* Unless this is a default drive, this may be an oversight. */ 230 if (!blk_get_attached_dev(blk) && !dinfo->is_default && 231 dinfo->type != IF_NONE) { 232 fprintf(stderr, "Warning: Orphaned drive without device: " 233 "id=%s,file=%s,if=%s,bus=%d,unit=%d\n", 234 blk_name(blk), blk_bs(blk) ? blk_bs(blk)->filename : "", 235 if_name[dinfo->type], dinfo->bus, dinfo->unit); 236 rs = true; 237 } 238 } 239 240 return rs; 241 } 242 243 DriveInfo *drive_get_by_index(BlockInterfaceType type, int index) 244 { 245 return drive_get(type, 246 drive_index_to_bus_id(type, index), 247 drive_index_to_unit_id(type, index)); 248 } 249 250 int drive_get_max_bus(BlockInterfaceType type) 251 { 252 int max_bus; 253 BlockBackend *blk; 254 DriveInfo *dinfo; 255 256 max_bus = -1; 257 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 258 dinfo = blk_legacy_dinfo(blk); 259 if (dinfo && dinfo->type == type && dinfo->bus > max_bus) { 260 max_bus = dinfo->bus; 261 } 262 } 263 return max_bus; 264 } 265 266 /* Get a block device. This should only be used for single-drive devices 267 (e.g. SD/Floppy/MTD). Multi-disk devices (scsi/ide) should use the 268 appropriate bus. */ 269 DriveInfo *drive_get_next(BlockInterfaceType type) 270 { 271 static int next_block_unit[IF_COUNT]; 272 273 return drive_get(type, 0, next_block_unit[type]++); 274 } 275 276 static void bdrv_format_print(void *opaque, const char *name) 277 { 278 error_printf(" %s", name); 279 } 280 281 typedef struct { 282 QEMUBH *bh; 283 BlockDriverState *bs; 284 } BDRVPutRefBH; 285 286 static int parse_block_error_action(const char *buf, bool is_read, Error **errp) 287 { 288 if (!strcmp(buf, "ignore")) { 289 return BLOCKDEV_ON_ERROR_IGNORE; 290 } else if (!is_read && !strcmp(buf, "enospc")) { 291 return BLOCKDEV_ON_ERROR_ENOSPC; 292 } else if (!strcmp(buf, "stop")) { 293 return BLOCKDEV_ON_ERROR_STOP; 294 } else if (!strcmp(buf, "report")) { 295 return BLOCKDEV_ON_ERROR_REPORT; 296 } else { 297 error_setg(errp, "'%s' invalid %s error action", 298 buf, is_read ? "read" : "write"); 299 return -1; 300 } 301 } 302 303 static bool parse_stats_intervals(BlockAcctStats *stats, QList *intervals, 304 Error **errp) 305 { 306 const QListEntry *entry; 307 for (entry = qlist_first(intervals); entry; entry = qlist_next(entry)) { 308 switch (qobject_type(entry->value)) { 309 310 case QTYPE_QSTRING: { 311 unsigned long long length; 312 const char *str = qstring_get_str(qobject_to_qstring(entry->value)); 313 if (parse_uint_full(str, &length, 10) == 0 && 314 length > 0 && length <= UINT_MAX) { 315 block_acct_add_interval(stats, (unsigned) length); 316 } else { 317 error_setg(errp, "Invalid interval length: %s", str); 318 return false; 319 } 320 break; 321 } 322 323 case QTYPE_QINT: { 324 int64_t length = qint_get_int(qobject_to_qint(entry->value)); 325 if (length > 0 && length <= UINT_MAX) { 326 block_acct_add_interval(stats, (unsigned) length); 327 } else { 328 error_setg(errp, "Invalid interval length: %" PRId64, length); 329 return false; 330 } 331 break; 332 } 333 334 default: 335 error_setg(errp, "The specification of stats-intervals is invalid"); 336 return false; 337 } 338 } 339 return true; 340 } 341 342 static bool check_throttle_config(ThrottleConfig *cfg, Error **errp) 343 { 344 if (throttle_conflicting(cfg)) { 345 error_setg(errp, "bps/iops/max total values and read/write values" 346 " cannot be used at the same time"); 347 return false; 348 } 349 350 if (!throttle_is_valid(cfg)) { 351 error_setg(errp, "bps/iops/maxs values must be 0 or greater"); 352 return false; 353 } 354 355 if (throttle_max_is_missing_limit(cfg)) { 356 error_setg(errp, "bps_max/iops_max require corresponding" 357 " bps/iops values"); 358 return false; 359 } 360 361 return true; 362 } 363 364 typedef enum { MEDIA_DISK, MEDIA_CDROM } DriveMediaType; 365 366 /* All parameters but @opts are optional and may be set to NULL. */ 367 static void extract_common_blockdev_options(QemuOpts *opts, int *bdrv_flags, 368 const char **throttling_group, ThrottleConfig *throttle_cfg, 369 BlockdevDetectZeroesOptions *detect_zeroes, Error **errp) 370 { 371 const char *discard; 372 Error *local_error = NULL; 373 const char *aio; 374 375 if (bdrv_flags) { 376 if (!qemu_opt_get_bool(opts, "read-only", false)) { 377 *bdrv_flags |= BDRV_O_RDWR; 378 } 379 if (qemu_opt_get_bool(opts, "copy-on-read", false)) { 380 *bdrv_flags |= BDRV_O_COPY_ON_READ; 381 } 382 383 if ((discard = qemu_opt_get(opts, "discard")) != NULL) { 384 if (bdrv_parse_discard_flags(discard, bdrv_flags) != 0) { 385 error_setg(errp, "Invalid discard option"); 386 return; 387 } 388 } 389 390 if ((aio = qemu_opt_get(opts, "aio")) != NULL) { 391 if (!strcmp(aio, "native")) { 392 *bdrv_flags |= BDRV_O_NATIVE_AIO; 393 } else if (!strcmp(aio, "threads")) { 394 /* this is the default */ 395 } else { 396 error_setg(errp, "invalid aio option"); 397 return; 398 } 399 } 400 } 401 402 /* disk I/O throttling */ 403 if (throttling_group) { 404 *throttling_group = qemu_opt_get(opts, "throttling.group"); 405 } 406 407 if (throttle_cfg) { 408 memset(throttle_cfg, 0, sizeof(*throttle_cfg)); 409 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].avg = 410 qemu_opt_get_number(opts, "throttling.bps-total", 0); 411 throttle_cfg->buckets[THROTTLE_BPS_READ].avg = 412 qemu_opt_get_number(opts, "throttling.bps-read", 0); 413 throttle_cfg->buckets[THROTTLE_BPS_WRITE].avg = 414 qemu_opt_get_number(opts, "throttling.bps-write", 0); 415 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].avg = 416 qemu_opt_get_number(opts, "throttling.iops-total", 0); 417 throttle_cfg->buckets[THROTTLE_OPS_READ].avg = 418 qemu_opt_get_number(opts, "throttling.iops-read", 0); 419 throttle_cfg->buckets[THROTTLE_OPS_WRITE].avg = 420 qemu_opt_get_number(opts, "throttling.iops-write", 0); 421 422 throttle_cfg->buckets[THROTTLE_BPS_TOTAL].max = 423 qemu_opt_get_number(opts, "throttling.bps-total-max", 0); 424 throttle_cfg->buckets[THROTTLE_BPS_READ].max = 425 qemu_opt_get_number(opts, "throttling.bps-read-max", 0); 426 throttle_cfg->buckets[THROTTLE_BPS_WRITE].max = 427 qemu_opt_get_number(opts, "throttling.bps-write-max", 0); 428 throttle_cfg->buckets[THROTTLE_OPS_TOTAL].max = 429 qemu_opt_get_number(opts, "throttling.iops-total-max", 0); 430 throttle_cfg->buckets[THROTTLE_OPS_READ].max = 431 qemu_opt_get_number(opts, "throttling.iops-read-max", 0); 432 throttle_cfg->buckets[THROTTLE_OPS_WRITE].max = 433 qemu_opt_get_number(opts, "throttling.iops-write-max", 0); 434 435 throttle_cfg->op_size = 436 qemu_opt_get_number(opts, "throttling.iops-size", 0); 437 438 if (!check_throttle_config(throttle_cfg, errp)) { 439 return; 440 } 441 } 442 443 if (detect_zeroes) { 444 *detect_zeroes = 445 qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, 446 qemu_opt_get(opts, "detect-zeroes"), 447 BLOCKDEV_DETECT_ZEROES_OPTIONS__MAX, 448 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, 449 &local_error); 450 if (local_error) { 451 error_propagate(errp, local_error); 452 return; 453 } 454 455 if (bdrv_flags && 456 *detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && 457 !(*bdrv_flags & BDRV_O_UNMAP)) 458 { 459 error_setg(errp, "setting detect-zeroes to unmap is not allowed " 460 "without setting discard operation to unmap"); 461 return; 462 } 463 } 464 } 465 466 /* Takes the ownership of bs_opts */ 467 static BlockBackend *blockdev_init(const char *file, QDict *bs_opts, 468 Error **errp) 469 { 470 const char *buf; 471 int bdrv_flags = 0; 472 int on_read_error, on_write_error; 473 bool account_invalid, account_failed; 474 BlockBackend *blk; 475 BlockDriverState *bs; 476 ThrottleConfig cfg; 477 int snapshot = 0; 478 Error *error = NULL; 479 QemuOpts *opts; 480 QDict *interval_dict = NULL; 481 QList *interval_list = NULL; 482 const char *id; 483 BlockdevDetectZeroesOptions detect_zeroes = 484 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; 485 const char *throttling_group = NULL; 486 487 /* Check common options by copying from bs_opts to opts, all other options 488 * stay in bs_opts for processing by bdrv_open(). */ 489 id = qdict_get_try_str(bs_opts, "id"); 490 opts = qemu_opts_create(&qemu_common_drive_opts, id, 1, &error); 491 if (error) { 492 error_propagate(errp, error); 493 goto err_no_opts; 494 } 495 496 qemu_opts_absorb_qdict(opts, bs_opts, &error); 497 if (error) { 498 error_propagate(errp, error); 499 goto early_err; 500 } 501 502 if (id) { 503 qdict_del(bs_opts, "id"); 504 } 505 506 /* extract parameters */ 507 snapshot = qemu_opt_get_bool(opts, "snapshot", 0); 508 509 account_invalid = qemu_opt_get_bool(opts, "stats-account-invalid", true); 510 account_failed = qemu_opt_get_bool(opts, "stats-account-failed", true); 511 512 qdict_extract_subqdict(bs_opts, &interval_dict, "stats-intervals."); 513 qdict_array_split(interval_dict, &interval_list); 514 515 if (qdict_size(interval_dict) != 0) { 516 error_setg(errp, "Invalid option stats-intervals.%s", 517 qdict_first(interval_dict)->key); 518 goto early_err; 519 } 520 521 extract_common_blockdev_options(opts, &bdrv_flags, &throttling_group, &cfg, 522 &detect_zeroes, &error); 523 if (error) { 524 error_propagate(errp, error); 525 goto early_err; 526 } 527 528 if ((buf = qemu_opt_get(opts, "format")) != NULL) { 529 if (is_help_option(buf)) { 530 error_printf("Supported formats:"); 531 bdrv_iterate_format(bdrv_format_print, NULL); 532 error_printf("\n"); 533 goto early_err; 534 } 535 536 if (qdict_haskey(bs_opts, "driver")) { 537 error_setg(errp, "Cannot specify both 'driver' and 'format'"); 538 goto early_err; 539 } 540 qdict_put(bs_opts, "driver", qstring_from_str(buf)); 541 } 542 543 on_write_error = BLOCKDEV_ON_ERROR_ENOSPC; 544 if ((buf = qemu_opt_get(opts, "werror")) != NULL) { 545 on_write_error = parse_block_error_action(buf, 0, &error); 546 if (error) { 547 error_propagate(errp, error); 548 goto early_err; 549 } 550 } 551 552 on_read_error = BLOCKDEV_ON_ERROR_REPORT; 553 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) { 554 on_read_error = parse_block_error_action(buf, 1, &error); 555 if (error) { 556 error_propagate(errp, error); 557 goto early_err; 558 } 559 } 560 561 if (snapshot) { 562 bdrv_flags |= BDRV_O_SNAPSHOT; 563 } 564 565 /* init */ 566 if ((!file || !*file) && !qdict_size(bs_opts)) { 567 BlockBackendRootState *blk_rs; 568 569 blk = blk_new(qemu_opts_id(opts), errp); 570 if (!blk) { 571 goto early_err; 572 } 573 574 blk_rs = blk_get_root_state(blk); 575 blk_rs->open_flags = bdrv_flags; 576 blk_rs->read_only = !(bdrv_flags & BDRV_O_RDWR); 577 blk_rs->detect_zeroes = detect_zeroes; 578 579 if (throttle_enabled(&cfg)) { 580 if (!throttling_group) { 581 throttling_group = blk_name(blk); 582 } 583 blk_rs->throttle_group = g_strdup(throttling_group); 584 blk_rs->throttle_state = throttle_group_incref(throttling_group); 585 blk_rs->throttle_state->cfg = cfg; 586 } 587 588 QDECREF(bs_opts); 589 } else { 590 if (file && !*file) { 591 file = NULL; 592 } 593 594 /* bdrv_open() defaults to the values in bdrv_flags (for compatibility 595 * with other callers) rather than what we want as the real defaults. 596 * Apply the defaults here instead. */ 597 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_WB, "on"); 598 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_DIRECT, "off"); 599 qdict_set_default_str(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, "off"); 600 601 if (snapshot) { 602 /* always use cache=unsafe with snapshot */ 603 qdict_put(bs_opts, BDRV_OPT_CACHE_WB, qstring_from_str("on")); 604 qdict_put(bs_opts, BDRV_OPT_CACHE_DIRECT, qstring_from_str("off")); 605 qdict_put(bs_opts, BDRV_OPT_CACHE_NO_FLUSH, qstring_from_str("on")); 606 } 607 608 blk = blk_new_open(qemu_opts_id(opts), file, NULL, bs_opts, bdrv_flags, 609 errp); 610 if (!blk) { 611 goto err_no_bs_opts; 612 } 613 bs = blk_bs(blk); 614 615 bs->detect_zeroes = detect_zeroes; 616 617 /* disk I/O throttling */ 618 if (throttle_enabled(&cfg)) { 619 if (!throttling_group) { 620 throttling_group = blk_name(blk); 621 } 622 bdrv_io_limits_enable(bs, throttling_group); 623 bdrv_set_io_limits(bs, &cfg); 624 } 625 626 if (bdrv_key_required(bs)) { 627 autostart = 0; 628 } 629 630 block_acct_init(blk_get_stats(blk), account_invalid, account_failed); 631 632 if (!parse_stats_intervals(blk_get_stats(blk), interval_list, errp)) { 633 blk_unref(blk); 634 blk = NULL; 635 goto err_no_bs_opts; 636 } 637 } 638 639 blk_set_on_error(blk, on_read_error, on_write_error); 640 641 err_no_bs_opts: 642 qemu_opts_del(opts); 643 QDECREF(interval_dict); 644 QDECREF(interval_list); 645 return blk; 646 647 early_err: 648 qemu_opts_del(opts); 649 QDECREF(interval_dict); 650 QDECREF(interval_list); 651 err_no_opts: 652 QDECREF(bs_opts); 653 return NULL; 654 } 655 656 static QemuOptsList qemu_root_bds_opts; 657 658 /* Takes the ownership of bs_opts */ 659 static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) 660 { 661 BlockDriverState *bs; 662 QemuOpts *opts; 663 Error *local_error = NULL; 664 BlockdevDetectZeroesOptions detect_zeroes; 665 int ret; 666 int bdrv_flags = 0; 667 668 opts = qemu_opts_create(&qemu_root_bds_opts, NULL, 1, errp); 669 if (!opts) { 670 goto fail; 671 } 672 673 qemu_opts_absorb_qdict(opts, bs_opts, &local_error); 674 if (local_error) { 675 error_propagate(errp, local_error); 676 goto fail; 677 } 678 679 extract_common_blockdev_options(opts, &bdrv_flags, NULL, NULL, 680 &detect_zeroes, &local_error); 681 if (local_error) { 682 error_propagate(errp, local_error); 683 goto fail; 684 } 685 686 bs = NULL; 687 ret = bdrv_open(&bs, NULL, NULL, bs_opts, bdrv_flags, errp); 688 if (ret < 0) { 689 goto fail_no_bs_opts; 690 } 691 692 bs->detect_zeroes = detect_zeroes; 693 694 fail_no_bs_opts: 695 qemu_opts_del(opts); 696 return bs; 697 698 fail: 699 qemu_opts_del(opts); 700 QDECREF(bs_opts); 701 return NULL; 702 } 703 704 static void qemu_opt_rename(QemuOpts *opts, const char *from, const char *to, 705 Error **errp) 706 { 707 const char *value; 708 709 value = qemu_opt_get(opts, from); 710 if (value) { 711 if (qemu_opt_find(opts, to)) { 712 error_setg(errp, "'%s' and its alias '%s' can't be used at the " 713 "same time", to, from); 714 return; 715 } 716 } 717 718 /* rename all items in opts */ 719 while ((value = qemu_opt_get(opts, from))) { 720 qemu_opt_set(opts, to, value, &error_abort); 721 qemu_opt_unset(opts, from); 722 } 723 } 724 725 QemuOptsList qemu_legacy_drive_opts = { 726 .name = "drive", 727 .head = QTAILQ_HEAD_INITIALIZER(qemu_legacy_drive_opts.head), 728 .desc = { 729 { 730 .name = "bus", 731 .type = QEMU_OPT_NUMBER, 732 .help = "bus number", 733 },{ 734 .name = "unit", 735 .type = QEMU_OPT_NUMBER, 736 .help = "unit number (i.e. lun for scsi)", 737 },{ 738 .name = "index", 739 .type = QEMU_OPT_NUMBER, 740 .help = "index number", 741 },{ 742 .name = "media", 743 .type = QEMU_OPT_STRING, 744 .help = "media type (disk, cdrom)", 745 },{ 746 .name = "if", 747 .type = QEMU_OPT_STRING, 748 .help = "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)", 749 },{ 750 .name = "cyls", 751 .type = QEMU_OPT_NUMBER, 752 .help = "number of cylinders (ide disk geometry)", 753 },{ 754 .name = "heads", 755 .type = QEMU_OPT_NUMBER, 756 .help = "number of heads (ide disk geometry)", 757 },{ 758 .name = "secs", 759 .type = QEMU_OPT_NUMBER, 760 .help = "number of sectors (ide disk geometry)", 761 },{ 762 .name = "trans", 763 .type = QEMU_OPT_STRING, 764 .help = "chs translation (auto, lba, none)", 765 },{ 766 .name = "boot", 767 .type = QEMU_OPT_BOOL, 768 .help = "(deprecated, ignored)", 769 },{ 770 .name = "addr", 771 .type = QEMU_OPT_STRING, 772 .help = "pci address (virtio only)", 773 },{ 774 .name = "serial", 775 .type = QEMU_OPT_STRING, 776 .help = "disk serial number", 777 },{ 778 .name = "file", 779 .type = QEMU_OPT_STRING, 780 .help = "file name", 781 }, 782 783 /* Options that are passed on, but have special semantics with -drive */ 784 { 785 .name = "read-only", 786 .type = QEMU_OPT_BOOL, 787 .help = "open drive file as read-only", 788 },{ 789 .name = "rerror", 790 .type = QEMU_OPT_STRING, 791 .help = "read error action", 792 },{ 793 .name = "werror", 794 .type = QEMU_OPT_STRING, 795 .help = "write error action", 796 },{ 797 .name = "copy-on-read", 798 .type = QEMU_OPT_BOOL, 799 .help = "copy read data from backing file into image file", 800 }, 801 802 { /* end of list */ } 803 }, 804 }; 805 806 DriveInfo *drive_new(QemuOpts *all_opts, BlockInterfaceType block_default_type) 807 { 808 const char *value; 809 BlockBackend *blk; 810 DriveInfo *dinfo = NULL; 811 QDict *bs_opts; 812 QemuOpts *legacy_opts; 813 DriveMediaType media = MEDIA_DISK; 814 BlockInterfaceType type; 815 int cyls, heads, secs, translation; 816 int max_devs, bus_id, unit_id, index; 817 const char *devaddr; 818 const char *werror, *rerror; 819 bool read_only = false; 820 bool copy_on_read; 821 const char *serial; 822 const char *filename; 823 Error *local_err = NULL; 824 int i; 825 826 /* Change legacy command line options into QMP ones */ 827 static const struct { 828 const char *from; 829 const char *to; 830 } opt_renames[] = { 831 { "iops", "throttling.iops-total" }, 832 { "iops_rd", "throttling.iops-read" }, 833 { "iops_wr", "throttling.iops-write" }, 834 835 { "bps", "throttling.bps-total" }, 836 { "bps_rd", "throttling.bps-read" }, 837 { "bps_wr", "throttling.bps-write" }, 838 839 { "iops_max", "throttling.iops-total-max" }, 840 { "iops_rd_max", "throttling.iops-read-max" }, 841 { "iops_wr_max", "throttling.iops-write-max" }, 842 843 { "bps_max", "throttling.bps-total-max" }, 844 { "bps_rd_max", "throttling.bps-read-max" }, 845 { "bps_wr_max", "throttling.bps-write-max" }, 846 847 { "iops_size", "throttling.iops-size" }, 848 849 { "group", "throttling.group" }, 850 851 { "readonly", "read-only" }, 852 }; 853 854 for (i = 0; i < ARRAY_SIZE(opt_renames); i++) { 855 qemu_opt_rename(all_opts, opt_renames[i].from, opt_renames[i].to, 856 &local_err); 857 if (local_err) { 858 error_report_err(local_err); 859 return NULL; 860 } 861 } 862 863 value = qemu_opt_get(all_opts, "cache"); 864 if (value) { 865 int flags = 0; 866 867 if (bdrv_parse_cache_flags(value, &flags) != 0) { 868 error_report("invalid cache option"); 869 return NULL; 870 } 871 872 /* Specific options take precedence */ 873 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_WB)) { 874 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_WB, 875 !!(flags & BDRV_O_CACHE_WB), &error_abort); 876 } 877 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_DIRECT)) { 878 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_DIRECT, 879 !!(flags & BDRV_O_NOCACHE), &error_abort); 880 } 881 if (!qemu_opt_get(all_opts, BDRV_OPT_CACHE_NO_FLUSH)) { 882 qemu_opt_set_bool(all_opts, BDRV_OPT_CACHE_NO_FLUSH, 883 !!(flags & BDRV_O_NO_FLUSH), &error_abort); 884 } 885 qemu_opt_unset(all_opts, "cache"); 886 } 887 888 /* Get a QDict for processing the options */ 889 bs_opts = qdict_new(); 890 qemu_opts_to_qdict(all_opts, bs_opts); 891 892 legacy_opts = qemu_opts_create(&qemu_legacy_drive_opts, NULL, 0, 893 &error_abort); 894 qemu_opts_absorb_qdict(legacy_opts, bs_opts, &local_err); 895 if (local_err) { 896 error_report_err(local_err); 897 goto fail; 898 } 899 900 /* Deprecated option boot=[on|off] */ 901 if (qemu_opt_get(legacy_opts, "boot") != NULL) { 902 fprintf(stderr, "qemu-kvm: boot=on|off is deprecated and will be " 903 "ignored. Future versions will reject this parameter. Please " 904 "update your scripts.\n"); 905 } 906 907 /* Media type */ 908 value = qemu_opt_get(legacy_opts, "media"); 909 if (value) { 910 if (!strcmp(value, "disk")) { 911 media = MEDIA_DISK; 912 } else if (!strcmp(value, "cdrom")) { 913 media = MEDIA_CDROM; 914 read_only = true; 915 } else { 916 error_report("'%s' invalid media", value); 917 goto fail; 918 } 919 } 920 921 /* copy-on-read is disabled with a warning for read-only devices */ 922 read_only |= qemu_opt_get_bool(legacy_opts, "read-only", false); 923 copy_on_read = qemu_opt_get_bool(legacy_opts, "copy-on-read", false); 924 925 if (read_only && copy_on_read) { 926 error_report("warning: disabling copy-on-read on read-only drive"); 927 copy_on_read = false; 928 } 929 930 qdict_put(bs_opts, "read-only", 931 qstring_from_str(read_only ? "on" : "off")); 932 qdict_put(bs_opts, "copy-on-read", 933 qstring_from_str(copy_on_read ? "on" :"off")); 934 935 /* Controller type */ 936 value = qemu_opt_get(legacy_opts, "if"); 937 if (value) { 938 for (type = 0; 939 type < IF_COUNT && strcmp(value, if_name[type]); 940 type++) { 941 } 942 if (type == IF_COUNT) { 943 error_report("unsupported bus type '%s'", value); 944 goto fail; 945 } 946 } else { 947 type = block_default_type; 948 } 949 950 /* Geometry */ 951 cyls = qemu_opt_get_number(legacy_opts, "cyls", 0); 952 heads = qemu_opt_get_number(legacy_opts, "heads", 0); 953 secs = qemu_opt_get_number(legacy_opts, "secs", 0); 954 955 if (cyls || heads || secs) { 956 if (cyls < 1) { 957 error_report("invalid physical cyls number"); 958 goto fail; 959 } 960 if (heads < 1) { 961 error_report("invalid physical heads number"); 962 goto fail; 963 } 964 if (secs < 1) { 965 error_report("invalid physical secs number"); 966 goto fail; 967 } 968 } 969 970 translation = BIOS_ATA_TRANSLATION_AUTO; 971 value = qemu_opt_get(legacy_opts, "trans"); 972 if (value != NULL) { 973 if (!cyls) { 974 error_report("'%s' trans must be used with cyls, heads and secs", 975 value); 976 goto fail; 977 } 978 if (!strcmp(value, "none")) { 979 translation = BIOS_ATA_TRANSLATION_NONE; 980 } else if (!strcmp(value, "lba")) { 981 translation = BIOS_ATA_TRANSLATION_LBA; 982 } else if (!strcmp(value, "large")) { 983 translation = BIOS_ATA_TRANSLATION_LARGE; 984 } else if (!strcmp(value, "rechs")) { 985 translation = BIOS_ATA_TRANSLATION_RECHS; 986 } else if (!strcmp(value, "auto")) { 987 translation = BIOS_ATA_TRANSLATION_AUTO; 988 } else { 989 error_report("'%s' invalid translation type", value); 990 goto fail; 991 } 992 } 993 994 if (media == MEDIA_CDROM) { 995 if (cyls || secs || heads) { 996 error_report("CHS can't be set with media=cdrom"); 997 goto fail; 998 } 999 } 1000 1001 /* Device address specified by bus/unit or index. 1002 * If none was specified, try to find the first free one. */ 1003 bus_id = qemu_opt_get_number(legacy_opts, "bus", 0); 1004 unit_id = qemu_opt_get_number(legacy_opts, "unit", -1); 1005 index = qemu_opt_get_number(legacy_opts, "index", -1); 1006 1007 max_devs = if_max_devs[type]; 1008 1009 if (index != -1) { 1010 if (bus_id != 0 || unit_id != -1) { 1011 error_report("index cannot be used with bus and unit"); 1012 goto fail; 1013 } 1014 bus_id = drive_index_to_bus_id(type, index); 1015 unit_id = drive_index_to_unit_id(type, index); 1016 } 1017 1018 if (unit_id == -1) { 1019 unit_id = 0; 1020 while (drive_get(type, bus_id, unit_id) != NULL) { 1021 unit_id++; 1022 if (max_devs && unit_id >= max_devs) { 1023 unit_id -= max_devs; 1024 bus_id++; 1025 } 1026 } 1027 } 1028 1029 if (max_devs && unit_id >= max_devs) { 1030 error_report("unit %d too big (max is %d)", unit_id, max_devs - 1); 1031 goto fail; 1032 } 1033 1034 if (drive_get(type, bus_id, unit_id) != NULL) { 1035 error_report("drive with bus=%d, unit=%d (index=%d) exists", 1036 bus_id, unit_id, index); 1037 goto fail; 1038 } 1039 1040 /* Serial number */ 1041 serial = qemu_opt_get(legacy_opts, "serial"); 1042 1043 /* no id supplied -> create one */ 1044 if (qemu_opts_id(all_opts) == NULL) { 1045 char *new_id; 1046 const char *mediastr = ""; 1047 if (type == IF_IDE || type == IF_SCSI) { 1048 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; 1049 } 1050 if (max_devs) { 1051 new_id = g_strdup_printf("%s%i%s%i", if_name[type], bus_id, 1052 mediastr, unit_id); 1053 } else { 1054 new_id = g_strdup_printf("%s%s%i", if_name[type], 1055 mediastr, unit_id); 1056 } 1057 qdict_put(bs_opts, "id", qstring_from_str(new_id)); 1058 g_free(new_id); 1059 } 1060 1061 /* Add virtio block device */ 1062 devaddr = qemu_opt_get(legacy_opts, "addr"); 1063 if (devaddr && type != IF_VIRTIO) { 1064 error_report("addr is not supported by this bus type"); 1065 goto fail; 1066 } 1067 1068 if (type == IF_VIRTIO) { 1069 QemuOpts *devopts; 1070 devopts = qemu_opts_create(qemu_find_opts("device"), NULL, 0, 1071 &error_abort); 1072 if (arch_type == QEMU_ARCH_S390X) { 1073 qemu_opt_set(devopts, "driver", "virtio-blk-ccw", &error_abort); 1074 } else { 1075 qemu_opt_set(devopts, "driver", "virtio-blk-pci", &error_abort); 1076 } 1077 qemu_opt_set(devopts, "drive", qdict_get_str(bs_opts, "id"), 1078 &error_abort); 1079 if (devaddr) { 1080 qemu_opt_set(devopts, "addr", devaddr, &error_abort); 1081 } 1082 } 1083 1084 filename = qemu_opt_get(legacy_opts, "file"); 1085 1086 /* Check werror/rerror compatibility with if=... */ 1087 werror = qemu_opt_get(legacy_opts, "werror"); 1088 if (werror != NULL) { 1089 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && 1090 type != IF_NONE) { 1091 error_report("werror is not supported by this bus type"); 1092 goto fail; 1093 } 1094 qdict_put(bs_opts, "werror", qstring_from_str(werror)); 1095 } 1096 1097 rerror = qemu_opt_get(legacy_opts, "rerror"); 1098 if (rerror != NULL) { 1099 if (type != IF_IDE && type != IF_VIRTIO && type != IF_SCSI && 1100 type != IF_NONE) { 1101 error_report("rerror is not supported by this bus type"); 1102 goto fail; 1103 } 1104 qdict_put(bs_opts, "rerror", qstring_from_str(rerror)); 1105 } 1106 1107 /* Actual block device init: Functionality shared with blockdev-add */ 1108 blk = blockdev_init(filename, bs_opts, &local_err); 1109 bs_opts = NULL; 1110 if (!blk) { 1111 if (local_err) { 1112 error_report_err(local_err); 1113 } 1114 goto fail; 1115 } else { 1116 assert(!local_err); 1117 } 1118 1119 /* Create legacy DriveInfo */ 1120 dinfo = g_malloc0(sizeof(*dinfo)); 1121 dinfo->opts = all_opts; 1122 1123 dinfo->cyls = cyls; 1124 dinfo->heads = heads; 1125 dinfo->secs = secs; 1126 dinfo->trans = translation; 1127 1128 dinfo->type = type; 1129 dinfo->bus = bus_id; 1130 dinfo->unit = unit_id; 1131 dinfo->devaddr = devaddr; 1132 dinfo->serial = g_strdup(serial); 1133 1134 blk_set_legacy_dinfo(blk, dinfo); 1135 1136 switch(type) { 1137 case IF_IDE: 1138 case IF_SCSI: 1139 case IF_XEN: 1140 case IF_NONE: 1141 dinfo->media_cd = media == MEDIA_CDROM; 1142 break; 1143 default: 1144 break; 1145 } 1146 1147 fail: 1148 qemu_opts_del(legacy_opts); 1149 QDECREF(bs_opts); 1150 return dinfo; 1151 } 1152 1153 void hmp_commit(Monitor *mon, const QDict *qdict) 1154 { 1155 const char *device = qdict_get_str(qdict, "device"); 1156 BlockBackend *blk; 1157 int ret; 1158 1159 if (!strcmp(device, "all")) { 1160 ret = bdrv_commit_all(); 1161 } else { 1162 BlockDriverState *bs; 1163 AioContext *aio_context; 1164 1165 blk = blk_by_name(device); 1166 if (!blk) { 1167 monitor_printf(mon, "Device '%s' not found\n", device); 1168 return; 1169 } 1170 if (!blk_is_available(blk)) { 1171 monitor_printf(mon, "Device '%s' has no medium\n", device); 1172 return; 1173 } 1174 1175 bs = blk_bs(blk); 1176 aio_context = bdrv_get_aio_context(bs); 1177 aio_context_acquire(aio_context); 1178 1179 ret = bdrv_commit(bs); 1180 1181 aio_context_release(aio_context); 1182 } 1183 if (ret < 0) { 1184 monitor_printf(mon, "'commit' error for '%s': %s\n", device, 1185 strerror(-ret)); 1186 } 1187 } 1188 1189 static void blockdev_do_action(TransactionActionKind type, void *data, 1190 Error **errp) 1191 { 1192 TransactionAction action; 1193 TransactionActionList list; 1194 1195 action.type = type; 1196 action.u.data = data; 1197 list.value = &action; 1198 list.next = NULL; 1199 qmp_transaction(&list, false, NULL, errp); 1200 } 1201 1202 void qmp_blockdev_snapshot_sync(bool has_device, const char *device, 1203 bool has_node_name, const char *node_name, 1204 const char *snapshot_file, 1205 bool has_snapshot_node_name, 1206 const char *snapshot_node_name, 1207 bool has_format, const char *format, 1208 bool has_mode, NewImageMode mode, Error **errp) 1209 { 1210 BlockdevSnapshotSync snapshot = { 1211 .has_device = has_device, 1212 .device = (char *) device, 1213 .has_node_name = has_node_name, 1214 .node_name = (char *) node_name, 1215 .snapshot_file = (char *) snapshot_file, 1216 .has_snapshot_node_name = has_snapshot_node_name, 1217 .snapshot_node_name = (char *) snapshot_node_name, 1218 .has_format = has_format, 1219 .format = (char *) format, 1220 .has_mode = has_mode, 1221 .mode = mode, 1222 }; 1223 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC, 1224 &snapshot, errp); 1225 } 1226 1227 void qmp_blockdev_snapshot(const char *node, const char *overlay, 1228 Error **errp) 1229 { 1230 BlockdevSnapshot snapshot_data = { 1231 .node = (char *) node, 1232 .overlay = (char *) overlay 1233 }; 1234 1235 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT, 1236 &snapshot_data, errp); 1237 } 1238 1239 void qmp_blockdev_snapshot_internal_sync(const char *device, 1240 const char *name, 1241 Error **errp) 1242 { 1243 BlockdevSnapshotInternal snapshot = { 1244 .device = (char *) device, 1245 .name = (char *) name 1246 }; 1247 1248 blockdev_do_action(TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC, 1249 &snapshot, errp); 1250 } 1251 1252 SnapshotInfo *qmp_blockdev_snapshot_delete_internal_sync(const char *device, 1253 bool has_id, 1254 const char *id, 1255 bool has_name, 1256 const char *name, 1257 Error **errp) 1258 { 1259 BlockDriverState *bs; 1260 BlockBackend *blk; 1261 AioContext *aio_context; 1262 QEMUSnapshotInfo sn; 1263 Error *local_err = NULL; 1264 SnapshotInfo *info = NULL; 1265 int ret; 1266 1267 blk = blk_by_name(device); 1268 if (!blk) { 1269 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1270 "Device '%s' not found", device); 1271 return NULL; 1272 } 1273 1274 aio_context = blk_get_aio_context(blk); 1275 aio_context_acquire(aio_context); 1276 1277 if (!has_id) { 1278 id = NULL; 1279 } 1280 1281 if (!has_name) { 1282 name = NULL; 1283 } 1284 1285 if (!id && !name) { 1286 error_setg(errp, "Name or id must be provided"); 1287 goto out_aio_context; 1288 } 1289 1290 if (!blk_is_available(blk)) { 1291 error_setg(errp, "Device '%s' has no medium", device); 1292 goto out_aio_context; 1293 } 1294 bs = blk_bs(blk); 1295 1296 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, errp)) { 1297 goto out_aio_context; 1298 } 1299 1300 ret = bdrv_snapshot_find_by_id_and_name(bs, id, name, &sn, &local_err); 1301 if (local_err) { 1302 error_propagate(errp, local_err); 1303 goto out_aio_context; 1304 } 1305 if (!ret) { 1306 error_setg(errp, 1307 "Snapshot with id '%s' and name '%s' does not exist on " 1308 "device '%s'", 1309 STR_OR_NULL(id), STR_OR_NULL(name), device); 1310 goto out_aio_context; 1311 } 1312 1313 bdrv_snapshot_delete(bs, id, name, &local_err); 1314 if (local_err) { 1315 error_propagate(errp, local_err); 1316 goto out_aio_context; 1317 } 1318 1319 aio_context_release(aio_context); 1320 1321 info = g_new0(SnapshotInfo, 1); 1322 info->id = g_strdup(sn.id_str); 1323 info->name = g_strdup(sn.name); 1324 info->date_nsec = sn.date_nsec; 1325 info->date_sec = sn.date_sec; 1326 info->vm_state_size = sn.vm_state_size; 1327 info->vm_clock_nsec = sn.vm_clock_nsec % 1000000000; 1328 info->vm_clock_sec = sn.vm_clock_nsec / 1000000000; 1329 1330 return info; 1331 1332 out_aio_context: 1333 aio_context_release(aio_context); 1334 return NULL; 1335 } 1336 1337 /** 1338 * block_dirty_bitmap_lookup: 1339 * Return a dirty bitmap (if present), after validating 1340 * the node reference and bitmap names. 1341 * 1342 * @node: The name of the BDS node to search for bitmaps 1343 * @name: The name of the bitmap to search for 1344 * @pbs: Output pointer for BDS lookup, if desired. Can be NULL. 1345 * @paio: Output pointer for aio_context acquisition, if desired. Can be NULL. 1346 * @errp: Output pointer for error information. Can be NULL. 1347 * 1348 * @return: A bitmap object on success, or NULL on failure. 1349 */ 1350 static BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1351 const char *name, 1352 BlockDriverState **pbs, 1353 AioContext **paio, 1354 Error **errp) 1355 { 1356 BlockDriverState *bs; 1357 BdrvDirtyBitmap *bitmap; 1358 AioContext *aio_context; 1359 1360 if (!node) { 1361 error_setg(errp, "Node cannot be NULL"); 1362 return NULL; 1363 } 1364 if (!name) { 1365 error_setg(errp, "Bitmap name cannot be NULL"); 1366 return NULL; 1367 } 1368 bs = bdrv_lookup_bs(node, node, NULL); 1369 if (!bs) { 1370 error_setg(errp, "Node '%s' not found", node); 1371 return NULL; 1372 } 1373 1374 aio_context = bdrv_get_aio_context(bs); 1375 aio_context_acquire(aio_context); 1376 1377 bitmap = bdrv_find_dirty_bitmap(bs, name); 1378 if (!bitmap) { 1379 error_setg(errp, "Dirty bitmap '%s' not found", name); 1380 goto fail; 1381 } 1382 1383 if (pbs) { 1384 *pbs = bs; 1385 } 1386 if (paio) { 1387 *paio = aio_context; 1388 } else { 1389 aio_context_release(aio_context); 1390 } 1391 1392 return bitmap; 1393 1394 fail: 1395 aio_context_release(aio_context); 1396 return NULL; 1397 } 1398 1399 /* New and old BlockDriverState structs for atomic group operations */ 1400 1401 typedef struct BlkActionState BlkActionState; 1402 1403 /** 1404 * BlkActionOps: 1405 * Table of operations that define an Action. 1406 * 1407 * @instance_size: Size of state struct, in bytes. 1408 * @prepare: Prepare the work, must NOT be NULL. 1409 * @commit: Commit the changes, can be NULL. 1410 * @abort: Abort the changes on fail, can be NULL. 1411 * @clean: Clean up resources after all transaction actions have called 1412 * commit() or abort(). Can be NULL. 1413 * 1414 * Only prepare() may fail. In a single transaction, only one of commit() or 1415 * abort() will be called. clean() will always be called if it is present. 1416 */ 1417 typedef struct BlkActionOps { 1418 size_t instance_size; 1419 void (*prepare)(BlkActionState *common, Error **errp); 1420 void (*commit)(BlkActionState *common); 1421 void (*abort)(BlkActionState *common); 1422 void (*clean)(BlkActionState *common); 1423 } BlkActionOps; 1424 1425 /** 1426 * BlkActionState: 1427 * Describes one Action's state within a Transaction. 1428 * 1429 * @action: QAPI-defined enum identifying which Action to perform. 1430 * @ops: Table of ActionOps this Action can perform. 1431 * @block_job_txn: Transaction which this action belongs to. 1432 * @entry: List membership for all Actions in this Transaction. 1433 * 1434 * This structure must be arranged as first member in a subclassed type, 1435 * assuming that the compiler will also arrange it to the same offsets as the 1436 * base class. 1437 */ 1438 struct BlkActionState { 1439 TransactionAction *action; 1440 const BlkActionOps *ops; 1441 BlockJobTxn *block_job_txn; 1442 TransactionProperties *txn_props; 1443 QSIMPLEQ_ENTRY(BlkActionState) entry; 1444 }; 1445 1446 /* internal snapshot private data */ 1447 typedef struct InternalSnapshotState { 1448 BlkActionState common; 1449 BlockDriverState *bs; 1450 AioContext *aio_context; 1451 QEMUSnapshotInfo sn; 1452 bool created; 1453 } InternalSnapshotState; 1454 1455 1456 static int action_check_completion_mode(BlkActionState *s, Error **errp) 1457 { 1458 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 1459 error_setg(errp, 1460 "Action '%s' does not support Transaction property " 1461 "completion-mode = %s", 1462 TransactionActionKind_lookup[s->action->type], 1463 ActionCompletionMode_lookup[s->txn_props->completion_mode]); 1464 return -1; 1465 } 1466 return 0; 1467 } 1468 1469 static void internal_snapshot_prepare(BlkActionState *common, 1470 Error **errp) 1471 { 1472 Error *local_err = NULL; 1473 const char *device; 1474 const char *name; 1475 BlockBackend *blk; 1476 BlockDriverState *bs; 1477 QEMUSnapshotInfo old_sn, *sn; 1478 bool ret; 1479 qemu_timeval tv; 1480 BlockdevSnapshotInternal *internal; 1481 InternalSnapshotState *state; 1482 int ret1; 1483 1484 g_assert(common->action->type == 1485 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 1486 internal = common->action->u.blockdev_snapshot_internal_sync; 1487 state = DO_UPCAST(InternalSnapshotState, common, common); 1488 1489 /* 1. parse input */ 1490 device = internal->device; 1491 name = internal->name; 1492 1493 /* 2. check for validation */ 1494 if (action_check_completion_mode(common, errp) < 0) { 1495 return; 1496 } 1497 1498 blk = blk_by_name(device); 1499 if (!blk) { 1500 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1501 "Device '%s' not found", device); 1502 return; 1503 } 1504 1505 /* AioContext is released in .clean() */ 1506 state->aio_context = blk_get_aio_context(blk); 1507 aio_context_acquire(state->aio_context); 1508 1509 if (!blk_is_available(blk)) { 1510 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1511 return; 1512 } 1513 bs = blk_bs(blk); 1514 1515 state->bs = bs; 1516 bdrv_drained_begin(bs); 1517 1518 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1519 return; 1520 } 1521 1522 if (bdrv_is_read_only(bs)) { 1523 error_setg(errp, "Device '%s' is read only", device); 1524 return; 1525 } 1526 1527 if (!bdrv_can_snapshot(bs)) { 1528 error_setg(errp, "Block format '%s' used by device '%s' " 1529 "does not support internal snapshots", 1530 bs->drv->format_name, device); 1531 return; 1532 } 1533 1534 if (!strlen(name)) { 1535 error_setg(errp, "Name is empty"); 1536 return; 1537 } 1538 1539 /* check whether a snapshot with name exist */ 1540 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1541 &local_err); 1542 if (local_err) { 1543 error_propagate(errp, local_err); 1544 return; 1545 } else if (ret) { 1546 error_setg(errp, 1547 "Snapshot with name '%s' already exists on device '%s'", 1548 name, device); 1549 return; 1550 } 1551 1552 /* 3. take the snapshot */ 1553 sn = &state->sn; 1554 pstrcpy(sn->name, sizeof(sn->name), name); 1555 qemu_gettimeofday(&tv); 1556 sn->date_sec = tv.tv_sec; 1557 sn->date_nsec = tv.tv_usec * 1000; 1558 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1559 1560 ret1 = bdrv_snapshot_create(bs, sn); 1561 if (ret1 < 0) { 1562 error_setg_errno(errp, -ret1, 1563 "Failed to create snapshot '%s' on device '%s'", 1564 name, device); 1565 return; 1566 } 1567 1568 /* 4. succeed, mark a snapshot is created */ 1569 state->created = true; 1570 } 1571 1572 static void internal_snapshot_abort(BlkActionState *common) 1573 { 1574 InternalSnapshotState *state = 1575 DO_UPCAST(InternalSnapshotState, common, common); 1576 BlockDriverState *bs = state->bs; 1577 QEMUSnapshotInfo *sn = &state->sn; 1578 Error *local_error = NULL; 1579 1580 if (!state->created) { 1581 return; 1582 } 1583 1584 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1585 error_report("Failed to delete snapshot with id '%s' and name '%s' on " 1586 "device '%s' in abort: %s", 1587 sn->id_str, 1588 sn->name, 1589 bdrv_get_device_name(bs), 1590 error_get_pretty(local_error)); 1591 error_free(local_error); 1592 } 1593 } 1594 1595 static void internal_snapshot_clean(BlkActionState *common) 1596 { 1597 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 1598 common, common); 1599 1600 if (state->aio_context) { 1601 if (state->bs) { 1602 bdrv_drained_end(state->bs); 1603 } 1604 aio_context_release(state->aio_context); 1605 } 1606 } 1607 1608 /* external snapshot private data */ 1609 typedef struct ExternalSnapshotState { 1610 BlkActionState common; 1611 BlockDriverState *old_bs; 1612 BlockDriverState *new_bs; 1613 AioContext *aio_context; 1614 } ExternalSnapshotState; 1615 1616 static void external_snapshot_prepare(BlkActionState *common, 1617 Error **errp) 1618 { 1619 int flags = 0, ret; 1620 QDict *options = NULL; 1621 Error *local_err = NULL; 1622 /* Device and node name of the image to generate the snapshot from */ 1623 const char *device; 1624 const char *node_name; 1625 /* Reference to the new image (for 'blockdev-snapshot') */ 1626 const char *snapshot_ref; 1627 /* File name of the new image (for 'blockdev-snapshot-sync') */ 1628 const char *new_image_file; 1629 ExternalSnapshotState *state = 1630 DO_UPCAST(ExternalSnapshotState, common, common); 1631 TransactionAction *action = common->action; 1632 1633 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 1634 * purpose but a different set of parameters */ 1635 switch (action->type) { 1636 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 1637 { 1638 BlockdevSnapshot *s = action->u.blockdev_snapshot; 1639 device = s->node; 1640 node_name = s->node; 1641 new_image_file = NULL; 1642 snapshot_ref = s->overlay; 1643 } 1644 break; 1645 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 1646 { 1647 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 1648 device = s->has_device ? s->device : NULL; 1649 node_name = s->has_node_name ? s->node_name : NULL; 1650 new_image_file = s->snapshot_file; 1651 snapshot_ref = NULL; 1652 } 1653 break; 1654 default: 1655 g_assert_not_reached(); 1656 } 1657 1658 /* start processing */ 1659 if (action_check_completion_mode(common, errp) < 0) { 1660 return; 1661 } 1662 1663 state->old_bs = bdrv_lookup_bs(device, node_name, errp); 1664 if (!state->old_bs) { 1665 return; 1666 } 1667 1668 /* Acquire AioContext now so any threads operating on old_bs stop */ 1669 state->aio_context = bdrv_get_aio_context(state->old_bs); 1670 aio_context_acquire(state->aio_context); 1671 bdrv_drained_begin(state->old_bs); 1672 1673 if (!bdrv_is_inserted(state->old_bs)) { 1674 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1675 return; 1676 } 1677 1678 if (bdrv_op_is_blocked(state->old_bs, 1679 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 1680 return; 1681 } 1682 1683 if (!bdrv_is_read_only(state->old_bs)) { 1684 if (bdrv_flush(state->old_bs)) { 1685 error_setg(errp, QERR_IO_ERROR); 1686 return; 1687 } 1688 } 1689 1690 if (!bdrv_is_first_non_filter(state->old_bs)) { 1691 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1692 return; 1693 } 1694 1695 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 1696 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync; 1697 const char *format = s->has_format ? s->format : "qcow2"; 1698 enum NewImageMode mode; 1699 const char *snapshot_node_name = 1700 s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 1701 1702 if (node_name && !snapshot_node_name) { 1703 error_setg(errp, "New snapshot node name missing"); 1704 return; 1705 } 1706 1707 if (snapshot_node_name && 1708 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 1709 error_setg(errp, "New snapshot node name already in use"); 1710 return; 1711 } 1712 1713 flags = state->old_bs->open_flags; 1714 1715 /* create new image w/backing file */ 1716 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1717 if (mode != NEW_IMAGE_MODE_EXISTING) { 1718 bdrv_img_create(new_image_file, format, 1719 state->old_bs->filename, 1720 state->old_bs->drv->format_name, 1721 NULL, -1, flags, &local_err, false); 1722 if (local_err) { 1723 error_propagate(errp, local_err); 1724 return; 1725 } 1726 } 1727 1728 options = qdict_new(); 1729 if (s->has_snapshot_node_name) { 1730 qdict_put(options, "node-name", 1731 qstring_from_str(snapshot_node_name)); 1732 } 1733 qdict_put(options, "driver", qstring_from_str(format)); 1734 1735 flags |= BDRV_O_NO_BACKING; 1736 } 1737 1738 assert(state->new_bs == NULL); 1739 ret = bdrv_open(&state->new_bs, new_image_file, snapshot_ref, options, 1740 flags, errp); 1741 /* We will manually add the backing_hd field to the bs later */ 1742 if (ret != 0) { 1743 return; 1744 } 1745 1746 if (state->new_bs->blk != NULL) { 1747 error_setg(errp, "The snapshot is already in use by %s", 1748 blk_name(state->new_bs->blk)); 1749 return; 1750 } 1751 1752 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 1753 errp)) { 1754 return; 1755 } 1756 1757 if (state->new_bs->backing != NULL) { 1758 error_setg(errp, "The snapshot already has a backing image"); 1759 return; 1760 } 1761 1762 if (!state->new_bs->drv->supports_backing) { 1763 error_setg(errp, "The snapshot does not support backing images"); 1764 } 1765 } 1766 1767 static void external_snapshot_commit(BlkActionState *common) 1768 { 1769 ExternalSnapshotState *state = 1770 DO_UPCAST(ExternalSnapshotState, common, common); 1771 1772 bdrv_set_aio_context(state->new_bs, state->aio_context); 1773 1774 /* This removes our old bs and adds the new bs */ 1775 bdrv_append(state->new_bs, state->old_bs); 1776 /* We don't need (or want) to use the transactional 1777 * bdrv_reopen_multiple() across all the entries at once, because we 1778 * don't want to abort all of them if one of them fails the reopen */ 1779 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 1780 NULL); 1781 } 1782 1783 static void external_snapshot_abort(BlkActionState *common) 1784 { 1785 ExternalSnapshotState *state = 1786 DO_UPCAST(ExternalSnapshotState, common, common); 1787 if (state->new_bs) { 1788 bdrv_unref(state->new_bs); 1789 } 1790 } 1791 1792 static void external_snapshot_clean(BlkActionState *common) 1793 { 1794 ExternalSnapshotState *state = 1795 DO_UPCAST(ExternalSnapshotState, common, common); 1796 if (state->aio_context) { 1797 bdrv_drained_end(state->old_bs); 1798 aio_context_release(state->aio_context); 1799 } 1800 } 1801 1802 typedef struct DriveBackupState { 1803 BlkActionState common; 1804 BlockDriverState *bs; 1805 AioContext *aio_context; 1806 BlockJob *job; 1807 } DriveBackupState; 1808 1809 static void do_drive_backup(const char *device, const char *target, 1810 bool has_format, const char *format, 1811 enum MirrorSyncMode sync, 1812 bool has_mode, enum NewImageMode mode, 1813 bool has_speed, int64_t speed, 1814 bool has_bitmap, const char *bitmap, 1815 bool has_on_source_error, 1816 BlockdevOnError on_source_error, 1817 bool has_on_target_error, 1818 BlockdevOnError on_target_error, 1819 BlockJobTxn *txn, Error **errp); 1820 1821 static void drive_backup_prepare(BlkActionState *common, Error **errp) 1822 { 1823 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1824 BlockBackend *blk; 1825 DriveBackup *backup; 1826 Error *local_err = NULL; 1827 1828 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 1829 backup = common->action->u.drive_backup; 1830 1831 blk = blk_by_name(backup->device); 1832 if (!blk) { 1833 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 1834 "Device '%s' not found", backup->device); 1835 return; 1836 } 1837 1838 if (!blk_is_available(blk)) { 1839 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1840 return; 1841 } 1842 1843 /* AioContext is released in .clean() */ 1844 state->aio_context = blk_get_aio_context(blk); 1845 aio_context_acquire(state->aio_context); 1846 bdrv_drained_begin(blk_bs(blk)); 1847 state->bs = blk_bs(blk); 1848 1849 do_drive_backup(backup->device, backup->target, 1850 backup->has_format, backup->format, 1851 backup->sync, 1852 backup->has_mode, backup->mode, 1853 backup->has_speed, backup->speed, 1854 backup->has_bitmap, backup->bitmap, 1855 backup->has_on_source_error, backup->on_source_error, 1856 backup->has_on_target_error, backup->on_target_error, 1857 common->block_job_txn, &local_err); 1858 if (local_err) { 1859 error_propagate(errp, local_err); 1860 return; 1861 } 1862 1863 state->job = state->bs->job; 1864 } 1865 1866 static void drive_backup_abort(BlkActionState *common) 1867 { 1868 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1869 BlockDriverState *bs = state->bs; 1870 1871 /* Only cancel if it's the job we started */ 1872 if (bs && bs->job && bs->job == state->job) { 1873 block_job_cancel_sync(bs->job); 1874 } 1875 } 1876 1877 static void drive_backup_clean(BlkActionState *common) 1878 { 1879 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1880 1881 if (state->aio_context) { 1882 bdrv_drained_end(state->bs); 1883 aio_context_release(state->aio_context); 1884 } 1885 } 1886 1887 typedef struct BlockdevBackupState { 1888 BlkActionState common; 1889 BlockDriverState *bs; 1890 BlockJob *job; 1891 AioContext *aio_context; 1892 } BlockdevBackupState; 1893 1894 static void do_blockdev_backup(const char *device, const char *target, 1895 enum MirrorSyncMode sync, 1896 bool has_speed, int64_t speed, 1897 bool has_on_source_error, 1898 BlockdevOnError on_source_error, 1899 bool has_on_target_error, 1900 BlockdevOnError on_target_error, 1901 BlockJobTxn *txn, Error **errp); 1902 1903 static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1904 { 1905 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1906 BlockdevBackup *backup; 1907 BlockBackend *blk, *target; 1908 Error *local_err = NULL; 1909 1910 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 1911 backup = common->action->u.blockdev_backup; 1912 1913 blk = blk_by_name(backup->device); 1914 if (!blk) { 1915 error_setg(errp, "Device '%s' not found", backup->device); 1916 return; 1917 } 1918 1919 if (!blk_is_available(blk)) { 1920 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, backup->device); 1921 return; 1922 } 1923 1924 target = blk_by_name(backup->target); 1925 if (!target) { 1926 error_setg(errp, "Device '%s' not found", backup->target); 1927 return; 1928 } 1929 1930 /* AioContext is released in .clean() */ 1931 state->aio_context = blk_get_aio_context(blk); 1932 if (state->aio_context != blk_get_aio_context(target)) { 1933 state->aio_context = NULL; 1934 error_setg(errp, "Backup between two IO threads is not implemented"); 1935 return; 1936 } 1937 aio_context_acquire(state->aio_context); 1938 state->bs = blk_bs(blk); 1939 bdrv_drained_begin(state->bs); 1940 1941 do_blockdev_backup(backup->device, backup->target, 1942 backup->sync, 1943 backup->has_speed, backup->speed, 1944 backup->has_on_source_error, backup->on_source_error, 1945 backup->has_on_target_error, backup->on_target_error, 1946 common->block_job_txn, &local_err); 1947 if (local_err) { 1948 error_propagate(errp, local_err); 1949 return; 1950 } 1951 1952 state->job = state->bs->job; 1953 } 1954 1955 static void blockdev_backup_abort(BlkActionState *common) 1956 { 1957 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1958 BlockDriverState *bs = state->bs; 1959 1960 /* Only cancel if it's the job we started */ 1961 if (bs && bs->job && bs->job == state->job) { 1962 block_job_cancel_sync(bs->job); 1963 } 1964 } 1965 1966 static void blockdev_backup_clean(BlkActionState *common) 1967 { 1968 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1969 1970 if (state->aio_context) { 1971 bdrv_drained_end(state->bs); 1972 aio_context_release(state->aio_context); 1973 } 1974 } 1975 1976 typedef struct BlockDirtyBitmapState { 1977 BlkActionState common; 1978 BdrvDirtyBitmap *bitmap; 1979 BlockDriverState *bs; 1980 AioContext *aio_context; 1981 HBitmap *backup; 1982 bool prepared; 1983 } BlockDirtyBitmapState; 1984 1985 static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1986 Error **errp) 1987 { 1988 Error *local_err = NULL; 1989 BlockDirtyBitmapAdd *action; 1990 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1991 common, common); 1992 1993 if (action_check_completion_mode(common, errp) < 0) { 1994 return; 1995 } 1996 1997 action = common->action->u.block_dirty_bitmap_add; 1998 /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1999 qmp_block_dirty_bitmap_add(action->node, action->name, 2000 action->has_granularity, action->granularity, 2001 &local_err); 2002 2003 if (!local_err) { 2004 state->prepared = true; 2005 } else { 2006 error_propagate(errp, local_err); 2007 } 2008 } 2009 2010 static void block_dirty_bitmap_add_abort(BlkActionState *common) 2011 { 2012 BlockDirtyBitmapAdd *action; 2013 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2014 common, common); 2015 2016 action = common->action->u.block_dirty_bitmap_add; 2017 /* Should not be able to fail: IF the bitmap was added via .prepare(), 2018 * then the node reference and bitmap name must have been valid. 2019 */ 2020 if (state->prepared) { 2021 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2022 } 2023 } 2024 2025 static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2026 Error **errp) 2027 { 2028 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2029 common, common); 2030 BlockDirtyBitmap *action; 2031 2032 if (action_check_completion_mode(common, errp) < 0) { 2033 return; 2034 } 2035 2036 action = common->action->u.block_dirty_bitmap_clear; 2037 state->bitmap = block_dirty_bitmap_lookup(action->node, 2038 action->name, 2039 &state->bs, 2040 &state->aio_context, 2041 errp); 2042 if (!state->bitmap) { 2043 return; 2044 } 2045 2046 if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2047 error_setg(errp, "Cannot modify a frozen bitmap"); 2048 return; 2049 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2050 error_setg(errp, "Cannot clear a disabled bitmap"); 2051 return; 2052 } 2053 2054 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2055 /* AioContext is released in .clean() */ 2056 } 2057 2058 static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2059 { 2060 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2061 common, common); 2062 2063 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2064 } 2065 2066 static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2067 { 2068 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2069 common, common); 2070 2071 hbitmap_free(state->backup); 2072 } 2073 2074 static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2075 { 2076 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2077 common, common); 2078 2079 if (state->aio_context) { 2080 aio_context_release(state->aio_context); 2081 } 2082 } 2083 2084 static void abort_prepare(BlkActionState *common, Error **errp) 2085 { 2086 error_setg(errp, "Transaction aborted using Abort action"); 2087 } 2088 2089 static void abort_commit(BlkActionState *common) 2090 { 2091 g_assert_not_reached(); /* this action never succeeds */ 2092 } 2093 2094 static const BlkActionOps actions[] = { 2095 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 2096 .instance_size = sizeof(ExternalSnapshotState), 2097 .prepare = external_snapshot_prepare, 2098 .commit = external_snapshot_commit, 2099 .abort = external_snapshot_abort, 2100 .clean = external_snapshot_clean, 2101 }, 2102 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2103 .instance_size = sizeof(ExternalSnapshotState), 2104 .prepare = external_snapshot_prepare, 2105 .commit = external_snapshot_commit, 2106 .abort = external_snapshot_abort, 2107 .clean = external_snapshot_clean, 2108 }, 2109 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 2110 .instance_size = sizeof(DriveBackupState), 2111 .prepare = drive_backup_prepare, 2112 .abort = drive_backup_abort, 2113 .clean = drive_backup_clean, 2114 }, 2115 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2116 .instance_size = sizeof(BlockdevBackupState), 2117 .prepare = blockdev_backup_prepare, 2118 .abort = blockdev_backup_abort, 2119 .clean = blockdev_backup_clean, 2120 }, 2121 [TRANSACTION_ACTION_KIND_ABORT] = { 2122 .instance_size = sizeof(BlkActionState), 2123 .prepare = abort_prepare, 2124 .commit = abort_commit, 2125 }, 2126 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2127 .instance_size = sizeof(InternalSnapshotState), 2128 .prepare = internal_snapshot_prepare, 2129 .abort = internal_snapshot_abort, 2130 .clean = internal_snapshot_clean, 2131 }, 2132 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2133 .instance_size = sizeof(BlockDirtyBitmapState), 2134 .prepare = block_dirty_bitmap_add_prepare, 2135 .abort = block_dirty_bitmap_add_abort, 2136 }, 2137 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2138 .instance_size = sizeof(BlockDirtyBitmapState), 2139 .prepare = block_dirty_bitmap_clear_prepare, 2140 .commit = block_dirty_bitmap_clear_commit, 2141 .abort = block_dirty_bitmap_clear_abort, 2142 .clean = block_dirty_bitmap_clear_clean, 2143 } 2144 }; 2145 2146 /** 2147 * Allocate a TransactionProperties structure if necessary, and fill 2148 * that structure with desired defaults if they are unset. 2149 */ 2150 static TransactionProperties *get_transaction_properties( 2151 TransactionProperties *props) 2152 { 2153 if (!props) { 2154 props = g_new0(TransactionProperties, 1); 2155 } 2156 2157 if (!props->has_completion_mode) { 2158 props->has_completion_mode = true; 2159 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 2160 } 2161 2162 return props; 2163 } 2164 2165 /* 2166 * 'Atomic' group operations. The operations are performed as a set, and if 2167 * any fail then we roll back all operations in the group. 2168 */ 2169 void qmp_transaction(TransactionActionList *dev_list, 2170 bool has_props, 2171 struct TransactionProperties *props, 2172 Error **errp) 2173 { 2174 TransactionActionList *dev_entry = dev_list; 2175 BlockJobTxn *block_job_txn = NULL; 2176 BlkActionState *state, *next; 2177 Error *local_err = NULL; 2178 2179 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 2180 QSIMPLEQ_INIT(&snap_bdrv_states); 2181 2182 /* Does this transaction get canceled as a group on failure? 2183 * If not, we don't really need to make a BlockJobTxn. 2184 */ 2185 props = get_transaction_properties(props); 2186 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 2187 block_job_txn = block_job_txn_new(); 2188 } 2189 2190 /* drain all i/o before any operations */ 2191 bdrv_drain_all(); 2192 2193 /* We don't do anything in this loop that commits us to the operations */ 2194 while (NULL != dev_entry) { 2195 TransactionAction *dev_info = NULL; 2196 const BlkActionOps *ops; 2197 2198 dev_info = dev_entry->value; 2199 dev_entry = dev_entry->next; 2200 2201 assert(dev_info->type < ARRAY_SIZE(actions)); 2202 2203 ops = &actions[dev_info->type]; 2204 assert(ops->instance_size > 0); 2205 2206 state = g_malloc0(ops->instance_size); 2207 state->ops = ops; 2208 state->action = dev_info; 2209 state->block_job_txn = block_job_txn; 2210 state->txn_props = props; 2211 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 2212 2213 state->ops->prepare(state, &local_err); 2214 if (local_err) { 2215 error_propagate(errp, local_err); 2216 goto delete_and_fail; 2217 } 2218 } 2219 2220 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2221 if (state->ops->commit) { 2222 state->ops->commit(state); 2223 } 2224 } 2225 2226 /* success */ 2227 goto exit; 2228 2229 delete_and_fail: 2230 /* failure, and it is all-or-none; roll back all operations */ 2231 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2232 if (state->ops->abort) { 2233 state->ops->abort(state); 2234 } 2235 } 2236 exit: 2237 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2238 if (state->ops->clean) { 2239 state->ops->clean(state); 2240 } 2241 g_free(state); 2242 } 2243 if (!has_props) { 2244 qapi_free_TransactionProperties(props); 2245 } 2246 block_job_txn_unref(block_job_txn); 2247 } 2248 2249 void qmp_eject(const char *device, bool has_force, bool force, Error **errp) 2250 { 2251 Error *local_err = NULL; 2252 2253 qmp_blockdev_open_tray(device, has_force, force, &local_err); 2254 if (local_err) { 2255 error_propagate(errp, local_err); 2256 return; 2257 } 2258 2259 qmp_x_blockdev_remove_medium(device, errp); 2260 } 2261 2262 void qmp_block_passwd(bool has_device, const char *device, 2263 bool has_node_name, const char *node_name, 2264 const char *password, Error **errp) 2265 { 2266 Error *local_err = NULL; 2267 BlockDriverState *bs; 2268 AioContext *aio_context; 2269 2270 bs = bdrv_lookup_bs(has_device ? device : NULL, 2271 has_node_name ? node_name : NULL, 2272 &local_err); 2273 if (local_err) { 2274 error_propagate(errp, local_err); 2275 return; 2276 } 2277 2278 aio_context = bdrv_get_aio_context(bs); 2279 aio_context_acquire(aio_context); 2280 2281 bdrv_add_key(bs, password, errp); 2282 2283 aio_context_release(aio_context); 2284 } 2285 2286 void qmp_blockdev_open_tray(const char *device, bool has_force, bool force, 2287 Error **errp) 2288 { 2289 BlockBackend *blk; 2290 bool locked; 2291 2292 if (!has_force) { 2293 force = false; 2294 } 2295 2296 blk = blk_by_name(device); 2297 if (!blk) { 2298 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2299 "Device '%s' not found", device); 2300 return; 2301 } 2302 2303 if (!blk_dev_has_removable_media(blk)) { 2304 error_setg(errp, "Device '%s' is not removable", device); 2305 return; 2306 } 2307 2308 if (blk_dev_is_tray_open(blk)) { 2309 return; 2310 } 2311 2312 locked = blk_dev_is_medium_locked(blk); 2313 if (locked) { 2314 blk_dev_eject_request(blk, force); 2315 } 2316 2317 if (!locked || force) { 2318 blk_dev_change_media_cb(blk, false); 2319 } 2320 } 2321 2322 void qmp_blockdev_close_tray(const char *device, Error **errp) 2323 { 2324 BlockBackend *blk; 2325 2326 blk = blk_by_name(device); 2327 if (!blk) { 2328 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2329 "Device '%s' not found", device); 2330 return; 2331 } 2332 2333 if (!blk_dev_has_removable_media(blk)) { 2334 error_setg(errp, "Device '%s' is not removable", device); 2335 return; 2336 } 2337 2338 if (!blk_dev_is_tray_open(blk)) { 2339 return; 2340 } 2341 2342 blk_dev_change_media_cb(blk, true); 2343 } 2344 2345 void qmp_x_blockdev_remove_medium(const char *device, Error **errp) 2346 { 2347 BlockBackend *blk; 2348 BlockDriverState *bs; 2349 AioContext *aio_context; 2350 bool has_device; 2351 2352 blk = blk_by_name(device); 2353 if (!blk) { 2354 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2355 "Device '%s' not found", device); 2356 return; 2357 } 2358 2359 /* For BBs without a device, we can exchange the BDS tree at will */ 2360 has_device = blk_get_attached_dev(blk); 2361 2362 if (has_device && !blk_dev_has_removable_media(blk)) { 2363 error_setg(errp, "Device '%s' is not removable", device); 2364 return; 2365 } 2366 2367 if (has_device && !blk_dev_is_tray_open(blk)) { 2368 error_setg(errp, "Tray of device '%s' is not open", device); 2369 return; 2370 } 2371 2372 bs = blk_bs(blk); 2373 if (!bs) { 2374 return; 2375 } 2376 2377 aio_context = bdrv_get_aio_context(bs); 2378 aio_context_acquire(aio_context); 2379 2380 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 2381 goto out; 2382 } 2383 2384 /* This follows the convention established by bdrv_make_anon() */ 2385 if (bs->device_list.tqe_prev) { 2386 QTAILQ_REMOVE(&bdrv_states, bs, device_list); 2387 bs->device_list.tqe_prev = NULL; 2388 } 2389 2390 blk_remove_bs(blk); 2391 2392 out: 2393 aio_context_release(aio_context); 2394 } 2395 2396 static void qmp_blockdev_insert_anon_medium(const char *device, 2397 BlockDriverState *bs, Error **errp) 2398 { 2399 BlockBackend *blk; 2400 bool has_device; 2401 2402 blk = blk_by_name(device); 2403 if (!blk) { 2404 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2405 "Device '%s' not found", device); 2406 return; 2407 } 2408 2409 /* For BBs without a device, we can exchange the BDS tree at will */ 2410 has_device = blk_get_attached_dev(blk); 2411 2412 if (has_device && !blk_dev_has_removable_media(blk)) { 2413 error_setg(errp, "Device '%s' is not removable", device); 2414 return; 2415 } 2416 2417 if (has_device && !blk_dev_is_tray_open(blk)) { 2418 error_setg(errp, "Tray of device '%s' is not open", device); 2419 return; 2420 } 2421 2422 if (blk_bs(blk)) { 2423 error_setg(errp, "There already is a medium in device '%s'", device); 2424 return; 2425 } 2426 2427 blk_insert_bs(blk, bs); 2428 2429 QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 2430 } 2431 2432 void qmp_x_blockdev_insert_medium(const char *device, const char *node_name, 2433 Error **errp) 2434 { 2435 BlockDriverState *bs; 2436 2437 bs = bdrv_find_node(node_name); 2438 if (!bs) { 2439 error_setg(errp, "Node '%s' not found", node_name); 2440 return; 2441 } 2442 2443 if (bs->blk) { 2444 error_setg(errp, "Node '%s' is already in use by '%s'", node_name, 2445 blk_name(bs->blk)); 2446 return; 2447 } 2448 2449 qmp_blockdev_insert_anon_medium(device, bs, errp); 2450 } 2451 2452 void qmp_blockdev_change_medium(const char *device, const char *filename, 2453 bool has_format, const char *format, 2454 bool has_read_only, 2455 BlockdevChangeReadOnlyMode read_only, 2456 Error **errp) 2457 { 2458 BlockBackend *blk; 2459 BlockDriverState *medium_bs = NULL; 2460 int bdrv_flags, ret; 2461 QDict *options = NULL; 2462 Error *err = NULL; 2463 2464 blk = blk_by_name(device); 2465 if (!blk) { 2466 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2467 "Device '%s' not found", device); 2468 goto fail; 2469 } 2470 2471 if (blk_bs(blk)) { 2472 blk_update_root_state(blk); 2473 } 2474 2475 bdrv_flags = blk_get_open_flags_from_root_state(blk); 2476 2477 if (!has_read_only) { 2478 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 2479 } 2480 2481 switch (read_only) { 2482 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 2483 break; 2484 2485 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 2486 bdrv_flags &= ~BDRV_O_RDWR; 2487 break; 2488 2489 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 2490 bdrv_flags |= BDRV_O_RDWR; 2491 break; 2492 2493 default: 2494 abort(); 2495 } 2496 2497 if (has_format) { 2498 options = qdict_new(); 2499 qdict_put(options, "driver", qstring_from_str(format)); 2500 } 2501 2502 assert(!medium_bs); 2503 ret = bdrv_open(&medium_bs, filename, NULL, options, bdrv_flags, errp); 2504 if (ret < 0) { 2505 goto fail; 2506 } 2507 2508 blk_apply_root_state(blk, medium_bs); 2509 2510 bdrv_add_key(medium_bs, NULL, &err); 2511 if (err) { 2512 error_propagate(errp, err); 2513 goto fail; 2514 } 2515 2516 qmp_blockdev_open_tray(device, false, false, &err); 2517 if (err) { 2518 error_propagate(errp, err); 2519 goto fail; 2520 } 2521 2522 qmp_x_blockdev_remove_medium(device, &err); 2523 if (err) { 2524 error_propagate(errp, err); 2525 goto fail; 2526 } 2527 2528 qmp_blockdev_insert_anon_medium(device, medium_bs, &err); 2529 if (err) { 2530 error_propagate(errp, err); 2531 goto fail; 2532 } 2533 2534 qmp_blockdev_close_tray(device, errp); 2535 2536 fail: 2537 /* If the medium has been inserted, the device has its own reference, so 2538 * ours must be relinquished; and if it has not been inserted successfully, 2539 * the reference must be relinquished anyway */ 2540 bdrv_unref(medium_bs); 2541 } 2542 2543 /* throttling disk I/O limits */ 2544 void qmp_block_set_io_throttle(const char *device, int64_t bps, int64_t bps_rd, 2545 int64_t bps_wr, 2546 int64_t iops, 2547 int64_t iops_rd, 2548 int64_t iops_wr, 2549 bool has_bps_max, 2550 int64_t bps_max, 2551 bool has_bps_rd_max, 2552 int64_t bps_rd_max, 2553 bool has_bps_wr_max, 2554 int64_t bps_wr_max, 2555 bool has_iops_max, 2556 int64_t iops_max, 2557 bool has_iops_rd_max, 2558 int64_t iops_rd_max, 2559 bool has_iops_wr_max, 2560 int64_t iops_wr_max, 2561 bool has_iops_size, 2562 int64_t iops_size, 2563 bool has_group, 2564 const char *group, Error **errp) 2565 { 2566 ThrottleConfig cfg; 2567 BlockDriverState *bs; 2568 BlockBackend *blk; 2569 AioContext *aio_context; 2570 2571 blk = blk_by_name(device); 2572 if (!blk) { 2573 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2574 "Device '%s' not found", device); 2575 return; 2576 } 2577 2578 aio_context = blk_get_aio_context(blk); 2579 aio_context_acquire(aio_context); 2580 2581 bs = blk_bs(blk); 2582 if (!bs) { 2583 error_setg(errp, "Device '%s' has no medium", device); 2584 goto out; 2585 } 2586 2587 memset(&cfg, 0, sizeof(cfg)); 2588 cfg.buckets[THROTTLE_BPS_TOTAL].avg = bps; 2589 cfg.buckets[THROTTLE_BPS_READ].avg = bps_rd; 2590 cfg.buckets[THROTTLE_BPS_WRITE].avg = bps_wr; 2591 2592 cfg.buckets[THROTTLE_OPS_TOTAL].avg = iops; 2593 cfg.buckets[THROTTLE_OPS_READ].avg = iops_rd; 2594 cfg.buckets[THROTTLE_OPS_WRITE].avg = iops_wr; 2595 2596 if (has_bps_max) { 2597 cfg.buckets[THROTTLE_BPS_TOTAL].max = bps_max; 2598 } 2599 if (has_bps_rd_max) { 2600 cfg.buckets[THROTTLE_BPS_READ].max = bps_rd_max; 2601 } 2602 if (has_bps_wr_max) { 2603 cfg.buckets[THROTTLE_BPS_WRITE].max = bps_wr_max; 2604 } 2605 if (has_iops_max) { 2606 cfg.buckets[THROTTLE_OPS_TOTAL].max = iops_max; 2607 } 2608 if (has_iops_rd_max) { 2609 cfg.buckets[THROTTLE_OPS_READ].max = iops_rd_max; 2610 } 2611 if (has_iops_wr_max) { 2612 cfg.buckets[THROTTLE_OPS_WRITE].max = iops_wr_max; 2613 } 2614 2615 if (has_iops_size) { 2616 cfg.op_size = iops_size; 2617 } 2618 2619 if (!check_throttle_config(&cfg, errp)) { 2620 goto out; 2621 } 2622 2623 if (throttle_enabled(&cfg)) { 2624 /* Enable I/O limits if they're not enabled yet, otherwise 2625 * just update the throttling group. */ 2626 if (!bs->throttle_state) { 2627 bdrv_io_limits_enable(bs, has_group ? group : device); 2628 } else if (has_group) { 2629 bdrv_io_limits_update_group(bs, group); 2630 } 2631 /* Set the new throttling configuration */ 2632 bdrv_set_io_limits(bs, &cfg); 2633 } else if (bs->throttle_state) { 2634 /* If all throttling settings are set to 0, disable I/O limits */ 2635 bdrv_io_limits_disable(bs); 2636 } 2637 2638 out: 2639 aio_context_release(aio_context); 2640 } 2641 2642 void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2643 bool has_granularity, uint32_t granularity, 2644 Error **errp) 2645 { 2646 AioContext *aio_context; 2647 BlockDriverState *bs; 2648 2649 if (!name || name[0] == '\0') { 2650 error_setg(errp, "Bitmap name cannot be empty"); 2651 return; 2652 } 2653 2654 bs = bdrv_lookup_bs(node, node, errp); 2655 if (!bs) { 2656 return; 2657 } 2658 2659 aio_context = bdrv_get_aio_context(bs); 2660 aio_context_acquire(aio_context); 2661 2662 if (has_granularity) { 2663 if (granularity < 512 || !is_power_of_2(granularity)) { 2664 error_setg(errp, "Granularity must be power of 2 " 2665 "and at least 512"); 2666 goto out; 2667 } 2668 } else { 2669 /* Default to cluster size, if available: */ 2670 granularity = bdrv_get_default_bitmap_granularity(bs); 2671 } 2672 2673 bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2674 2675 out: 2676 aio_context_release(aio_context); 2677 } 2678 2679 void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2680 Error **errp) 2681 { 2682 AioContext *aio_context; 2683 BlockDriverState *bs; 2684 BdrvDirtyBitmap *bitmap; 2685 2686 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2687 if (!bitmap || !bs) { 2688 return; 2689 } 2690 2691 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2692 error_setg(errp, 2693 "Bitmap '%s' is currently frozen and cannot be removed", 2694 name); 2695 goto out; 2696 } 2697 bdrv_dirty_bitmap_make_anon(bitmap); 2698 bdrv_release_dirty_bitmap(bs, bitmap); 2699 2700 out: 2701 aio_context_release(aio_context); 2702 } 2703 2704 /** 2705 * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2706 * immediately after a full backup operation. 2707 */ 2708 void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2709 Error **errp) 2710 { 2711 AioContext *aio_context; 2712 BdrvDirtyBitmap *bitmap; 2713 BlockDriverState *bs; 2714 2715 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2716 if (!bitmap || !bs) { 2717 return; 2718 } 2719 2720 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2721 error_setg(errp, 2722 "Bitmap '%s' is currently frozen and cannot be modified", 2723 name); 2724 goto out; 2725 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2726 error_setg(errp, 2727 "Bitmap '%s' is currently disabled and cannot be cleared", 2728 name); 2729 goto out; 2730 } 2731 2732 bdrv_clear_dirty_bitmap(bitmap, NULL); 2733 2734 out: 2735 aio_context_release(aio_context); 2736 } 2737 2738 void hmp_drive_del(Monitor *mon, const QDict *qdict) 2739 { 2740 const char *id = qdict_get_str(qdict, "id"); 2741 BlockBackend *blk; 2742 BlockDriverState *bs; 2743 AioContext *aio_context; 2744 Error *local_err = NULL; 2745 2746 blk = blk_by_name(id); 2747 if (!blk) { 2748 error_report("Device '%s' not found", id); 2749 return; 2750 } 2751 2752 if (!blk_legacy_dinfo(blk)) { 2753 error_report("Deleting device added with blockdev-add" 2754 " is not supported"); 2755 return; 2756 } 2757 2758 aio_context = blk_get_aio_context(blk); 2759 aio_context_acquire(aio_context); 2760 2761 bs = blk_bs(blk); 2762 if (bs) { 2763 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2764 error_report_err(local_err); 2765 aio_context_release(aio_context); 2766 return; 2767 } 2768 2769 bdrv_close(bs); 2770 } 2771 2772 /* if we have a device attached to this BlockDriverState 2773 * then we need to make the drive anonymous until the device 2774 * can be removed. If this is a drive with no device backing 2775 * then we can just get rid of the block driver state right here. 2776 */ 2777 if (blk_get_attached_dev(blk)) { 2778 blk_hide_on_behalf_of_hmp_drive_del(blk); 2779 /* Further I/O must not pause the guest */ 2780 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2781 BLOCKDEV_ON_ERROR_REPORT); 2782 } else { 2783 blk_unref(blk); 2784 } 2785 2786 aio_context_release(aio_context); 2787 } 2788 2789 void qmp_block_resize(bool has_device, const char *device, 2790 bool has_node_name, const char *node_name, 2791 int64_t size, Error **errp) 2792 { 2793 Error *local_err = NULL; 2794 BlockDriverState *bs; 2795 AioContext *aio_context; 2796 int ret; 2797 2798 bs = bdrv_lookup_bs(has_device ? device : NULL, 2799 has_node_name ? node_name : NULL, 2800 &local_err); 2801 if (local_err) { 2802 error_propagate(errp, local_err); 2803 return; 2804 } 2805 2806 aio_context = bdrv_get_aio_context(bs); 2807 aio_context_acquire(aio_context); 2808 2809 if (!bdrv_is_first_non_filter(bs)) { 2810 error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2811 goto out; 2812 } 2813 2814 if (size < 0) { 2815 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2816 goto out; 2817 } 2818 2819 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2820 error_setg(errp, QERR_DEVICE_IN_USE, device); 2821 goto out; 2822 } 2823 2824 /* complete all in-flight operations before resizing the device */ 2825 bdrv_drain_all(); 2826 2827 ret = bdrv_truncate(bs, size); 2828 switch (ret) { 2829 case 0: 2830 break; 2831 case -ENOMEDIUM: 2832 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 2833 break; 2834 case -ENOTSUP: 2835 error_setg(errp, QERR_UNSUPPORTED); 2836 break; 2837 case -EACCES: 2838 error_setg(errp, "Device '%s' is read only", device); 2839 break; 2840 case -EBUSY: 2841 error_setg(errp, QERR_DEVICE_IN_USE, device); 2842 break; 2843 default: 2844 error_setg_errno(errp, -ret, "Could not resize"); 2845 break; 2846 } 2847 2848 out: 2849 aio_context_release(aio_context); 2850 } 2851 2852 static void block_job_cb(void *opaque, int ret) 2853 { 2854 /* Note that this function may be executed from another AioContext besides 2855 * the QEMU main loop. If you need to access anything that assumes the 2856 * QEMU global mutex, use a BH or introduce a mutex. 2857 */ 2858 2859 BlockDriverState *bs = opaque; 2860 const char *msg = NULL; 2861 2862 trace_block_job_cb(bs, bs->job, ret); 2863 2864 assert(bs->job); 2865 2866 if (ret < 0) { 2867 msg = strerror(-ret); 2868 } 2869 2870 if (block_job_is_cancelled(bs->job)) { 2871 block_job_event_cancelled(bs->job); 2872 } else { 2873 block_job_event_completed(bs->job, msg); 2874 } 2875 } 2876 2877 void qmp_block_stream(const char *device, 2878 bool has_base, const char *base, 2879 bool has_backing_file, const char *backing_file, 2880 bool has_speed, int64_t speed, 2881 bool has_on_error, BlockdevOnError on_error, 2882 Error **errp) 2883 { 2884 BlockBackend *blk; 2885 BlockDriverState *bs; 2886 BlockDriverState *base_bs = NULL; 2887 AioContext *aio_context; 2888 Error *local_err = NULL; 2889 const char *base_name = NULL; 2890 2891 if (!has_on_error) { 2892 on_error = BLOCKDEV_ON_ERROR_REPORT; 2893 } 2894 2895 blk = blk_by_name(device); 2896 if (!blk) { 2897 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2898 "Device '%s' not found", device); 2899 return; 2900 } 2901 2902 aio_context = blk_get_aio_context(blk); 2903 aio_context_acquire(aio_context); 2904 2905 if (!blk_is_available(blk)) { 2906 error_setg(errp, "Device '%s' has no medium", device); 2907 goto out; 2908 } 2909 bs = blk_bs(blk); 2910 2911 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_STREAM, errp)) { 2912 goto out; 2913 } 2914 2915 if (has_base) { 2916 base_bs = bdrv_find_backing_image(bs, base); 2917 if (base_bs == NULL) { 2918 error_setg(errp, QERR_BASE_NOT_FOUND, base); 2919 goto out; 2920 } 2921 assert(bdrv_get_aio_context(base_bs) == aio_context); 2922 base_name = base; 2923 } 2924 2925 /* if we are streaming the entire chain, the result will have no backing 2926 * file, and specifying one is therefore an error */ 2927 if (base_bs == NULL && has_backing_file) { 2928 error_setg(errp, "backing file specified, but streaming the " 2929 "entire chain"); 2930 goto out; 2931 } 2932 2933 /* backing_file string overrides base bs filename */ 2934 base_name = has_backing_file ? backing_file : base_name; 2935 2936 stream_start(bs, base_bs, base_name, has_speed ? speed : 0, 2937 on_error, block_job_cb, bs, &local_err); 2938 if (local_err) { 2939 error_propagate(errp, local_err); 2940 goto out; 2941 } 2942 2943 trace_qmp_block_stream(bs, bs->job); 2944 2945 out: 2946 aio_context_release(aio_context); 2947 } 2948 2949 void qmp_block_commit(const char *device, 2950 bool has_base, const char *base, 2951 bool has_top, const char *top, 2952 bool has_backing_file, const char *backing_file, 2953 bool has_speed, int64_t speed, 2954 Error **errp) 2955 { 2956 BlockBackend *blk; 2957 BlockDriverState *bs; 2958 BlockDriverState *base_bs, *top_bs; 2959 AioContext *aio_context; 2960 Error *local_err = NULL; 2961 /* This will be part of the QMP command, if/when the 2962 * BlockdevOnError change for blkmirror makes it in 2963 */ 2964 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 2965 2966 if (!has_speed) { 2967 speed = 0; 2968 } 2969 2970 /* Important Note: 2971 * libvirt relies on the DeviceNotFound error class in order to probe for 2972 * live commit feature versions; for this to work, we must make sure to 2973 * perform the device lookup before any generic errors that may occur in a 2974 * scenario in which all optional arguments are omitted. */ 2975 blk = blk_by_name(device); 2976 if (!blk) { 2977 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 2978 "Device '%s' not found", device); 2979 return; 2980 } 2981 2982 aio_context = blk_get_aio_context(blk); 2983 aio_context_acquire(aio_context); 2984 2985 if (!blk_is_available(blk)) { 2986 error_setg(errp, "Device '%s' has no medium", device); 2987 goto out; 2988 } 2989 bs = blk_bs(blk); 2990 2991 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 2992 goto out; 2993 } 2994 2995 /* default top_bs is the active layer */ 2996 top_bs = bs; 2997 2998 if (has_top && top) { 2999 if (strcmp(bs->filename, top) != 0) { 3000 top_bs = bdrv_find_backing_image(bs, top); 3001 } 3002 } 3003 3004 if (top_bs == NULL) { 3005 error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 3006 goto out; 3007 } 3008 3009 assert(bdrv_get_aio_context(top_bs) == aio_context); 3010 3011 if (has_base && base) { 3012 base_bs = bdrv_find_backing_image(top_bs, base); 3013 } else { 3014 base_bs = bdrv_find_base(top_bs); 3015 } 3016 3017 if (base_bs == NULL) { 3018 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 3019 goto out; 3020 } 3021 3022 assert(bdrv_get_aio_context(base_bs) == aio_context); 3023 3024 if (bdrv_op_is_blocked(base_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3025 goto out; 3026 } 3027 3028 /* Do not allow attempts to commit an image into itself */ 3029 if (top_bs == base_bs) { 3030 error_setg(errp, "cannot commit an image into itself"); 3031 goto out; 3032 } 3033 3034 if (top_bs == bs) { 3035 if (has_backing_file) { 3036 error_setg(errp, "'backing-file' specified," 3037 " but 'top' is the active layer"); 3038 goto out; 3039 } 3040 commit_active_start(bs, base_bs, speed, on_error, block_job_cb, 3041 bs, &local_err); 3042 } else { 3043 commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs, 3044 has_backing_file ? backing_file : NULL, &local_err); 3045 } 3046 if (local_err != NULL) { 3047 error_propagate(errp, local_err); 3048 goto out; 3049 } 3050 3051 out: 3052 aio_context_release(aio_context); 3053 } 3054 3055 static void do_drive_backup(const char *device, const char *target, 3056 bool has_format, const char *format, 3057 enum MirrorSyncMode sync, 3058 bool has_mode, enum NewImageMode mode, 3059 bool has_speed, int64_t speed, 3060 bool has_bitmap, const char *bitmap, 3061 bool has_on_source_error, 3062 BlockdevOnError on_source_error, 3063 bool has_on_target_error, 3064 BlockdevOnError on_target_error, 3065 BlockJobTxn *txn, Error **errp) 3066 { 3067 BlockBackend *blk; 3068 BlockDriverState *bs; 3069 BlockDriverState *target_bs; 3070 BlockDriverState *source = NULL; 3071 BdrvDirtyBitmap *bmap = NULL; 3072 AioContext *aio_context; 3073 QDict *options = NULL; 3074 Error *local_err = NULL; 3075 int flags; 3076 int64_t size; 3077 int ret; 3078 3079 if (!has_speed) { 3080 speed = 0; 3081 } 3082 if (!has_on_source_error) { 3083 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3084 } 3085 if (!has_on_target_error) { 3086 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3087 } 3088 if (!has_mode) { 3089 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 3090 } 3091 3092 blk = blk_by_name(device); 3093 if (!blk) { 3094 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 3095 "Device '%s' not found", device); 3096 return; 3097 } 3098 3099 aio_context = blk_get_aio_context(blk); 3100 aio_context_acquire(aio_context); 3101 3102 /* Although backup_run has this check too, we need to use bs->drv below, so 3103 * do an early check redundantly. */ 3104 if (!blk_is_available(blk)) { 3105 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3106 goto out; 3107 } 3108 bs = blk_bs(blk); 3109 3110 if (!has_format) { 3111 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3112 } 3113 3114 /* Early check to avoid creating target */ 3115 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3116 goto out; 3117 } 3118 3119 flags = bs->open_flags | BDRV_O_RDWR; 3120 3121 /* See if we have a backing HD we can use to create our new image 3122 * on top of. */ 3123 if (sync == MIRROR_SYNC_MODE_TOP) { 3124 source = backing_bs(bs); 3125 if (!source) { 3126 sync = MIRROR_SYNC_MODE_FULL; 3127 } 3128 } 3129 if (sync == MIRROR_SYNC_MODE_NONE) { 3130 source = bs; 3131 } 3132 3133 size = bdrv_getlength(bs); 3134 if (size < 0) { 3135 error_setg_errno(errp, -size, "bdrv_getlength failed"); 3136 goto out; 3137 } 3138 3139 if (mode != NEW_IMAGE_MODE_EXISTING) { 3140 assert(format); 3141 if (source) { 3142 bdrv_img_create(target, format, source->filename, 3143 source->drv->format_name, NULL, 3144 size, flags, &local_err, false); 3145 } else { 3146 bdrv_img_create(target, format, NULL, NULL, NULL, 3147 size, flags, &local_err, false); 3148 } 3149 } 3150 3151 if (local_err) { 3152 error_propagate(errp, local_err); 3153 goto out; 3154 } 3155 3156 if (format) { 3157 options = qdict_new(); 3158 qdict_put(options, "driver", qstring_from_str(format)); 3159 } 3160 3161 target_bs = NULL; 3162 ret = bdrv_open(&target_bs, target, NULL, options, flags, &local_err); 3163 if (ret < 0) { 3164 error_propagate(errp, local_err); 3165 goto out; 3166 } 3167 3168 bdrv_set_aio_context(target_bs, aio_context); 3169 3170 if (has_bitmap) { 3171 bmap = bdrv_find_dirty_bitmap(bs, bitmap); 3172 if (!bmap) { 3173 error_setg(errp, "Bitmap '%s' could not be found", bitmap); 3174 bdrv_unref(target_bs); 3175 goto out; 3176 } 3177 } 3178 3179 backup_start(bs, target_bs, speed, sync, bmap, 3180 on_source_error, on_target_error, 3181 block_job_cb, bs, txn, &local_err); 3182 if (local_err != NULL) { 3183 bdrv_unref(target_bs); 3184 error_propagate(errp, local_err); 3185 goto out; 3186 } 3187 3188 out: 3189 aio_context_release(aio_context); 3190 } 3191 3192 void qmp_drive_backup(const char *device, const char *target, 3193 bool has_format, const char *format, 3194 enum MirrorSyncMode sync, 3195 bool has_mode, enum NewImageMode mode, 3196 bool has_speed, int64_t speed, 3197 bool has_bitmap, const char *bitmap, 3198 bool has_on_source_error, BlockdevOnError on_source_error, 3199 bool has_on_target_error, BlockdevOnError on_target_error, 3200 Error **errp) 3201 { 3202 return do_drive_backup(device, target, has_format, format, sync, 3203 has_mode, mode, has_speed, speed, 3204 has_bitmap, bitmap, 3205 has_on_source_error, on_source_error, 3206 has_on_target_error, on_target_error, 3207 NULL, errp); 3208 } 3209 3210 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3211 { 3212 return bdrv_named_nodes_list(errp); 3213 } 3214 3215 void do_blockdev_backup(const char *device, const char *target, 3216 enum MirrorSyncMode sync, 3217 bool has_speed, int64_t speed, 3218 bool has_on_source_error, 3219 BlockdevOnError on_source_error, 3220 bool has_on_target_error, 3221 BlockdevOnError on_target_error, 3222 BlockJobTxn *txn, Error **errp) 3223 { 3224 BlockBackend *blk, *target_blk; 3225 BlockDriverState *bs; 3226 BlockDriverState *target_bs; 3227 Error *local_err = NULL; 3228 AioContext *aio_context; 3229 3230 if (!has_speed) { 3231 speed = 0; 3232 } 3233 if (!has_on_source_error) { 3234 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3235 } 3236 if (!has_on_target_error) { 3237 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3238 } 3239 3240 blk = blk_by_name(device); 3241 if (!blk) { 3242 error_setg(errp, "Device '%s' not found", device); 3243 return; 3244 } 3245 3246 aio_context = blk_get_aio_context(blk); 3247 aio_context_acquire(aio_context); 3248 3249 if (!blk_is_available(blk)) { 3250 error_setg(errp, "Device '%s' has no medium", device); 3251 goto out; 3252 } 3253 bs = blk_bs(blk); 3254 3255 target_blk = blk_by_name(target); 3256 if (!target_blk) { 3257 error_setg(errp, "Device '%s' not found", target); 3258 goto out; 3259 } 3260 3261 if (!blk_is_available(target_blk)) { 3262 error_setg(errp, "Device '%s' has no medium", target); 3263 goto out; 3264 } 3265 target_bs = blk_bs(target_blk); 3266 3267 bdrv_ref(target_bs); 3268 bdrv_set_aio_context(target_bs, aio_context); 3269 backup_start(bs, target_bs, speed, sync, NULL, on_source_error, 3270 on_target_error, block_job_cb, bs, txn, &local_err); 3271 if (local_err != NULL) { 3272 bdrv_unref(target_bs); 3273 error_propagate(errp, local_err); 3274 } 3275 out: 3276 aio_context_release(aio_context); 3277 } 3278 3279 void qmp_blockdev_backup(const char *device, const char *target, 3280 enum MirrorSyncMode sync, 3281 bool has_speed, int64_t speed, 3282 bool has_on_source_error, 3283 BlockdevOnError on_source_error, 3284 bool has_on_target_error, 3285 BlockdevOnError on_target_error, 3286 Error **errp) 3287 { 3288 do_blockdev_backup(device, target, sync, has_speed, speed, 3289 has_on_source_error, on_source_error, 3290 has_on_target_error, on_target_error, 3291 NULL, errp); 3292 } 3293 3294 /* Parameter check and block job starting for drive mirroring. 3295 * Caller should hold @device and @target's aio context (must be the same). 3296 **/ 3297 static void blockdev_mirror_common(BlockDriverState *bs, 3298 BlockDriverState *target, 3299 bool has_replaces, const char *replaces, 3300 enum MirrorSyncMode sync, 3301 bool has_speed, int64_t speed, 3302 bool has_granularity, uint32_t granularity, 3303 bool has_buf_size, int64_t buf_size, 3304 bool has_on_source_error, 3305 BlockdevOnError on_source_error, 3306 bool has_on_target_error, 3307 BlockdevOnError on_target_error, 3308 bool has_unmap, bool unmap, 3309 Error **errp) 3310 { 3311 3312 if (!has_speed) { 3313 speed = 0; 3314 } 3315 if (!has_on_source_error) { 3316 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3317 } 3318 if (!has_on_target_error) { 3319 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3320 } 3321 if (!has_granularity) { 3322 granularity = 0; 3323 } 3324 if (!has_buf_size) { 3325 buf_size = 0; 3326 } 3327 if (!has_unmap) { 3328 unmap = true; 3329 } 3330 3331 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3332 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3333 "a value in range [512B, 64MB]"); 3334 return; 3335 } 3336 if (granularity & (granularity - 1)) { 3337 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3338 "power of 2"); 3339 return; 3340 } 3341 3342 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3343 return; 3344 } 3345 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3346 return; 3347 } 3348 if (target->blk) { 3349 error_setg(errp, "Cannot mirror to an attached block device"); 3350 return; 3351 } 3352 3353 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 3354 sync = MIRROR_SYNC_MODE_FULL; 3355 } 3356 3357 /* pass the node name to replace to mirror start since it's loose coupling 3358 * and will allow to check whether the node still exist at mirror completion 3359 */ 3360 mirror_start(bs, target, 3361 has_replaces ? replaces : NULL, 3362 speed, granularity, buf_size, sync, 3363 on_source_error, on_target_error, unmap, 3364 block_job_cb, bs, errp); 3365 } 3366 3367 void qmp_drive_mirror(const char *device, const char *target, 3368 bool has_format, const char *format, 3369 bool has_node_name, const char *node_name, 3370 bool has_replaces, const char *replaces, 3371 enum MirrorSyncMode sync, 3372 bool has_mode, enum NewImageMode mode, 3373 bool has_speed, int64_t speed, 3374 bool has_granularity, uint32_t granularity, 3375 bool has_buf_size, int64_t buf_size, 3376 bool has_on_source_error, BlockdevOnError on_source_error, 3377 bool has_on_target_error, BlockdevOnError on_target_error, 3378 bool has_unmap, bool unmap, 3379 Error **errp) 3380 { 3381 BlockDriverState *bs; 3382 BlockBackend *blk; 3383 BlockDriverState *source, *target_bs; 3384 AioContext *aio_context; 3385 Error *local_err = NULL; 3386 QDict *options = NULL; 3387 int flags; 3388 int64_t size; 3389 int ret; 3390 3391 blk = blk_by_name(device); 3392 if (!blk) { 3393 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 3394 "Device '%s' not found", device); 3395 return; 3396 } 3397 3398 aio_context = blk_get_aio_context(blk); 3399 aio_context_acquire(aio_context); 3400 3401 if (!blk_is_available(blk)) { 3402 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 3403 goto out; 3404 } 3405 bs = blk_bs(blk); 3406 if (!has_mode) { 3407 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 3408 } 3409 3410 if (!has_format) { 3411 format = mode == NEW_IMAGE_MODE_EXISTING ? NULL : bs->drv->format_name; 3412 } 3413 3414 flags = bs->open_flags | BDRV_O_RDWR; 3415 source = backing_bs(bs); 3416 if (!source && sync == MIRROR_SYNC_MODE_TOP) { 3417 sync = MIRROR_SYNC_MODE_FULL; 3418 } 3419 if (sync == MIRROR_SYNC_MODE_NONE) { 3420 source = bs; 3421 } 3422 3423 size = bdrv_getlength(bs); 3424 if (size < 0) { 3425 error_setg_errno(errp, -size, "bdrv_getlength failed"); 3426 goto out; 3427 } 3428 3429 if (has_replaces) { 3430 BlockDriverState *to_replace_bs; 3431 AioContext *replace_aio_context; 3432 int64_t replace_size; 3433 3434 if (!has_node_name) { 3435 error_setg(errp, "a node-name must be provided when replacing a" 3436 " named node of the graph"); 3437 goto out; 3438 } 3439 3440 to_replace_bs = check_to_replace_node(bs, replaces, &local_err); 3441 3442 if (!to_replace_bs) { 3443 error_propagate(errp, local_err); 3444 goto out; 3445 } 3446 3447 replace_aio_context = bdrv_get_aio_context(to_replace_bs); 3448 aio_context_acquire(replace_aio_context); 3449 replace_size = bdrv_getlength(to_replace_bs); 3450 aio_context_release(replace_aio_context); 3451 3452 if (size != replace_size) { 3453 error_setg(errp, "cannot replace image with a mirror image of " 3454 "different size"); 3455 goto out; 3456 } 3457 } 3458 3459 if ((sync == MIRROR_SYNC_MODE_FULL || !source) 3460 && mode != NEW_IMAGE_MODE_EXISTING) 3461 { 3462 /* create new image w/o backing file */ 3463 assert(format); 3464 bdrv_img_create(target, format, 3465 NULL, NULL, NULL, size, flags, &local_err, false); 3466 } else { 3467 switch (mode) { 3468 case NEW_IMAGE_MODE_EXISTING: 3469 break; 3470 case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3471 /* create new image with backing file */ 3472 bdrv_img_create(target, format, 3473 source->filename, 3474 source->drv->format_name, 3475 NULL, size, flags, &local_err, false); 3476 break; 3477 default: 3478 abort(); 3479 } 3480 } 3481 3482 if (local_err) { 3483 error_propagate(errp, local_err); 3484 goto out; 3485 } 3486 3487 options = qdict_new(); 3488 if (has_node_name) { 3489 qdict_put(options, "node-name", qstring_from_str(node_name)); 3490 } 3491 if (format) { 3492 qdict_put(options, "driver", qstring_from_str(format)); 3493 } 3494 3495 /* Mirroring takes care of copy-on-write using the source's backing 3496 * file. 3497 */ 3498 target_bs = NULL; 3499 ret = bdrv_open(&target_bs, target, NULL, options, 3500 flags | BDRV_O_NO_BACKING, &local_err); 3501 if (ret < 0) { 3502 error_propagate(errp, local_err); 3503 goto out; 3504 } 3505 3506 bdrv_set_aio_context(target_bs, aio_context); 3507 3508 blockdev_mirror_common(bs, target_bs, 3509 has_replaces, replaces, sync, 3510 has_speed, speed, 3511 has_granularity, granularity, 3512 has_buf_size, buf_size, 3513 has_on_source_error, on_source_error, 3514 has_on_target_error, on_target_error, 3515 has_unmap, unmap, 3516 &local_err); 3517 if (local_err) { 3518 error_propagate(errp, local_err); 3519 bdrv_unref(target_bs); 3520 } 3521 out: 3522 aio_context_release(aio_context); 3523 } 3524 3525 void qmp_blockdev_mirror(const char *device, const char *target, 3526 bool has_replaces, const char *replaces, 3527 MirrorSyncMode sync, 3528 bool has_speed, int64_t speed, 3529 bool has_granularity, uint32_t granularity, 3530 bool has_buf_size, int64_t buf_size, 3531 bool has_on_source_error, 3532 BlockdevOnError on_source_error, 3533 bool has_on_target_error, 3534 BlockdevOnError on_target_error, 3535 Error **errp) 3536 { 3537 BlockDriverState *bs; 3538 BlockBackend *blk; 3539 BlockDriverState *target_bs; 3540 AioContext *aio_context; 3541 Error *local_err = NULL; 3542 3543 blk = blk_by_name(device); 3544 if (!blk) { 3545 error_setg(errp, "Device '%s' not found", device); 3546 return; 3547 } 3548 bs = blk_bs(blk); 3549 3550 if (!bs) { 3551 error_setg(errp, "Device '%s' has no media", device); 3552 return; 3553 } 3554 3555 target_bs = bdrv_lookup_bs(target, target, errp); 3556 if (!target_bs) { 3557 return; 3558 } 3559 3560 aio_context = bdrv_get_aio_context(bs); 3561 aio_context_acquire(aio_context); 3562 3563 bdrv_ref(target_bs); 3564 bdrv_set_aio_context(target_bs, aio_context); 3565 3566 blockdev_mirror_common(bs, target_bs, 3567 has_replaces, replaces, sync, 3568 has_speed, speed, 3569 has_granularity, granularity, 3570 has_buf_size, buf_size, 3571 has_on_source_error, on_source_error, 3572 has_on_target_error, on_target_error, 3573 true, true, 3574 &local_err); 3575 if (local_err) { 3576 error_propagate(errp, local_err); 3577 bdrv_unref(target_bs); 3578 } 3579 3580 aio_context_release(aio_context); 3581 } 3582 3583 /* Get the block job for a given device name and acquire its AioContext */ 3584 static BlockJob *find_block_job(const char *device, AioContext **aio_context, 3585 Error **errp) 3586 { 3587 BlockBackend *blk; 3588 BlockDriverState *bs; 3589 3590 *aio_context = NULL; 3591 3592 blk = blk_by_name(device); 3593 if (!blk) { 3594 goto notfound; 3595 } 3596 3597 *aio_context = blk_get_aio_context(blk); 3598 aio_context_acquire(*aio_context); 3599 3600 if (!blk_is_available(blk)) { 3601 goto notfound; 3602 } 3603 bs = blk_bs(blk); 3604 3605 if (!bs->job) { 3606 goto notfound; 3607 } 3608 3609 return bs->job; 3610 3611 notfound: 3612 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 3613 "No active block job on device '%s'", device); 3614 if (*aio_context) { 3615 aio_context_release(*aio_context); 3616 *aio_context = NULL; 3617 } 3618 return NULL; 3619 } 3620 3621 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 3622 { 3623 AioContext *aio_context; 3624 BlockJob *job = find_block_job(device, &aio_context, errp); 3625 3626 if (!job) { 3627 return; 3628 } 3629 3630 block_job_set_speed(job, speed, errp); 3631 aio_context_release(aio_context); 3632 } 3633 3634 void qmp_block_job_cancel(const char *device, 3635 bool has_force, bool force, Error **errp) 3636 { 3637 AioContext *aio_context; 3638 BlockJob *job = find_block_job(device, &aio_context, errp); 3639 3640 if (!job) { 3641 return; 3642 } 3643 3644 if (!has_force) { 3645 force = false; 3646 } 3647 3648 if (job->user_paused && !force) { 3649 error_setg(errp, "The block job for device '%s' is currently paused", 3650 device); 3651 goto out; 3652 } 3653 3654 trace_qmp_block_job_cancel(job); 3655 block_job_cancel(job); 3656 out: 3657 aio_context_release(aio_context); 3658 } 3659 3660 void qmp_block_job_pause(const char *device, Error **errp) 3661 { 3662 AioContext *aio_context; 3663 BlockJob *job = find_block_job(device, &aio_context, errp); 3664 3665 if (!job || job->user_paused) { 3666 return; 3667 } 3668 3669 job->user_paused = true; 3670 trace_qmp_block_job_pause(job); 3671 block_job_pause(job); 3672 aio_context_release(aio_context); 3673 } 3674 3675 void qmp_block_job_resume(const char *device, Error **errp) 3676 { 3677 AioContext *aio_context; 3678 BlockJob *job = find_block_job(device, &aio_context, errp); 3679 3680 if (!job || !job->user_paused) { 3681 return; 3682 } 3683 3684 job->user_paused = false; 3685 trace_qmp_block_job_resume(job); 3686 block_job_resume(job); 3687 aio_context_release(aio_context); 3688 } 3689 3690 void qmp_block_job_complete(const char *device, Error **errp) 3691 { 3692 AioContext *aio_context; 3693 BlockJob *job = find_block_job(device, &aio_context, errp); 3694 3695 if (!job) { 3696 return; 3697 } 3698 3699 trace_qmp_block_job_complete(job); 3700 block_job_complete(job, errp); 3701 aio_context_release(aio_context); 3702 } 3703 3704 void qmp_change_backing_file(const char *device, 3705 const char *image_node_name, 3706 const char *backing_file, 3707 Error **errp) 3708 { 3709 BlockBackend *blk; 3710 BlockDriverState *bs = NULL; 3711 AioContext *aio_context; 3712 BlockDriverState *image_bs = NULL; 3713 Error *local_err = NULL; 3714 bool ro; 3715 int open_flags; 3716 int ret; 3717 3718 blk = blk_by_name(device); 3719 if (!blk) { 3720 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 3721 "Device '%s' not found", device); 3722 return; 3723 } 3724 3725 aio_context = blk_get_aio_context(blk); 3726 aio_context_acquire(aio_context); 3727 3728 if (!blk_is_available(blk)) { 3729 error_setg(errp, "Device '%s' has no medium", device); 3730 goto out; 3731 } 3732 bs = blk_bs(blk); 3733 3734 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3735 if (local_err) { 3736 error_propagate(errp, local_err); 3737 goto out; 3738 } 3739 3740 if (!image_bs) { 3741 error_setg(errp, "image file not found"); 3742 goto out; 3743 } 3744 3745 if (bdrv_find_base(image_bs) == image_bs) { 3746 error_setg(errp, "not allowing backing file change on an image " 3747 "without a backing file"); 3748 goto out; 3749 } 3750 3751 /* even though we are not necessarily operating on bs, we need it to 3752 * determine if block ops are currently prohibited on the chain */ 3753 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3754 goto out; 3755 } 3756 3757 /* final sanity check */ 3758 if (!bdrv_chain_contains(bs, image_bs)) { 3759 error_setg(errp, "'%s' and image file are not in the same chain", 3760 device); 3761 goto out; 3762 } 3763 3764 /* if not r/w, reopen to make r/w */ 3765 open_flags = image_bs->open_flags; 3766 ro = bdrv_is_read_only(image_bs); 3767 3768 if (ro) { 3769 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3770 if (local_err) { 3771 error_propagate(errp, local_err); 3772 goto out; 3773 } 3774 } 3775 3776 ret = bdrv_change_backing_file(image_bs, backing_file, 3777 image_bs->drv ? image_bs->drv->format_name : ""); 3778 3779 if (ret < 0) { 3780 error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3781 backing_file); 3782 /* don't exit here, so we can try to restore open flags if 3783 * appropriate */ 3784 } 3785 3786 if (ro) { 3787 bdrv_reopen(image_bs, open_flags, &local_err); 3788 if (local_err) { 3789 error_propagate(errp, local_err); /* will preserve prior errp */ 3790 } 3791 } 3792 3793 out: 3794 aio_context_release(aio_context); 3795 } 3796 3797 void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3798 { 3799 QmpOutputVisitor *ov = qmp_output_visitor_new(); 3800 BlockDriverState *bs; 3801 BlockBackend *blk = NULL; 3802 QObject *obj; 3803 QDict *qdict; 3804 Error *local_err = NULL; 3805 3806 /* TODO Sort it out in raw-posix and drive_new(): Reject aio=native with 3807 * cache.direct=false instead of silently switching to aio=threads, except 3808 * when called from drive_new(). 3809 * 3810 * For now, simply forbidding the combination for all drivers will do. */ 3811 if (options->has_aio && options->aio == BLOCKDEV_AIO_OPTIONS_NATIVE) { 3812 bool direct = options->has_cache && 3813 options->cache->has_direct && 3814 options->cache->direct; 3815 if (!direct) { 3816 error_setg(errp, "aio=native requires cache.direct=true"); 3817 goto fail; 3818 } 3819 } 3820 3821 visit_type_BlockdevOptions(qmp_output_get_visitor(ov), 3822 &options, NULL, &local_err); 3823 if (local_err) { 3824 error_propagate(errp, local_err); 3825 goto fail; 3826 } 3827 3828 obj = qmp_output_get_qobject(ov); 3829 qdict = qobject_to_qdict(obj); 3830 3831 qdict_flatten(qdict); 3832 3833 if (options->has_id) { 3834 blk = blockdev_init(NULL, qdict, &local_err); 3835 if (local_err) { 3836 error_propagate(errp, local_err); 3837 goto fail; 3838 } 3839 3840 bs = blk_bs(blk); 3841 } else { 3842 if (!qdict_get_try_str(qdict, "node-name")) { 3843 error_setg(errp, "'id' and/or 'node-name' need to be specified for " 3844 "the root node"); 3845 goto fail; 3846 } 3847 3848 bs = bds_tree_init(qdict, errp); 3849 if (!bs) { 3850 goto fail; 3851 } 3852 } 3853 3854 if (bs && bdrv_key_required(bs)) { 3855 if (blk) { 3856 blk_unref(blk); 3857 } else { 3858 bdrv_unref(bs); 3859 } 3860 error_setg(errp, "blockdev-add doesn't support encrypted devices"); 3861 goto fail; 3862 } 3863 3864 fail: 3865 qmp_output_visitor_cleanup(ov); 3866 } 3867 3868 void qmp_x_blockdev_del(bool has_id, const char *id, 3869 bool has_node_name, const char *node_name, Error **errp) 3870 { 3871 AioContext *aio_context; 3872 BlockBackend *blk; 3873 BlockDriverState *bs; 3874 3875 if (has_id && has_node_name) { 3876 error_setg(errp, "Only one of id and node-name must be specified"); 3877 return; 3878 } else if (!has_id && !has_node_name) { 3879 error_setg(errp, "No block device specified"); 3880 return; 3881 } 3882 3883 if (has_id) { 3884 blk = blk_by_name(id); 3885 if (!blk) { 3886 error_setg(errp, "Cannot find block backend %s", id); 3887 return; 3888 } 3889 if (blk_get_refcnt(blk) > 1) { 3890 error_setg(errp, "Block backend %s is in use", id); 3891 return; 3892 } 3893 bs = blk_bs(blk); 3894 aio_context = blk_get_aio_context(blk); 3895 } else { 3896 bs = bdrv_find_node(node_name); 3897 if (!bs) { 3898 error_setg(errp, "Cannot find node %s", node_name); 3899 return; 3900 } 3901 blk = bs->blk; 3902 if (blk) { 3903 error_setg(errp, "Node %s is in use by %s", 3904 node_name, blk_name(blk)); 3905 return; 3906 } 3907 aio_context = bdrv_get_aio_context(bs); 3908 } 3909 3910 aio_context_acquire(aio_context); 3911 3912 if (bs) { 3913 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 3914 goto out; 3915 } 3916 3917 if (bs->refcnt > 1 || !QLIST_EMPTY(&bs->parents)) { 3918 error_setg(errp, "Block device %s is in use", 3919 bdrv_get_device_or_node_name(bs)); 3920 goto out; 3921 } 3922 } 3923 3924 if (blk) { 3925 blk_unref(blk); 3926 } else { 3927 bdrv_unref(bs); 3928 } 3929 3930 out: 3931 aio_context_release(aio_context); 3932 } 3933 3934 BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3935 { 3936 BlockJobInfoList *head = NULL, **p_next = &head; 3937 BlockDriverState *bs; 3938 3939 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) { 3940 AioContext *aio_context = bdrv_get_aio_context(bs); 3941 3942 aio_context_acquire(aio_context); 3943 3944 if (bs->job) { 3945 BlockJobInfoList *elem = g_new0(BlockJobInfoList, 1); 3946 elem->value = block_job_query(bs->job); 3947 *p_next = elem; 3948 p_next = &elem->next; 3949 } 3950 3951 aio_context_release(aio_context); 3952 } 3953 3954 return head; 3955 } 3956 3957 QemuOptsList qemu_common_drive_opts = { 3958 .name = "drive", 3959 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 3960 .desc = { 3961 { 3962 .name = "snapshot", 3963 .type = QEMU_OPT_BOOL, 3964 .help = "enable/disable snapshot mode", 3965 },{ 3966 .name = "discard", 3967 .type = QEMU_OPT_STRING, 3968 .help = "discard operation (ignore/off, unmap/on)", 3969 },{ 3970 .name = "aio", 3971 .type = QEMU_OPT_STRING, 3972 .help = "host AIO implementation (threads, native)", 3973 },{ 3974 .name = "format", 3975 .type = QEMU_OPT_STRING, 3976 .help = "disk format (raw, qcow2, ...)", 3977 },{ 3978 .name = "rerror", 3979 .type = QEMU_OPT_STRING, 3980 .help = "read error action", 3981 },{ 3982 .name = "werror", 3983 .type = QEMU_OPT_STRING, 3984 .help = "write error action", 3985 },{ 3986 .name = "read-only", 3987 .type = QEMU_OPT_BOOL, 3988 .help = "open drive file as read-only", 3989 },{ 3990 .name = "throttling.iops-total", 3991 .type = QEMU_OPT_NUMBER, 3992 .help = "limit total I/O operations per second", 3993 },{ 3994 .name = "throttling.iops-read", 3995 .type = QEMU_OPT_NUMBER, 3996 .help = "limit read operations per second", 3997 },{ 3998 .name = "throttling.iops-write", 3999 .type = QEMU_OPT_NUMBER, 4000 .help = "limit write operations per second", 4001 },{ 4002 .name = "throttling.bps-total", 4003 .type = QEMU_OPT_NUMBER, 4004 .help = "limit total bytes per second", 4005 },{ 4006 .name = "throttling.bps-read", 4007 .type = QEMU_OPT_NUMBER, 4008 .help = "limit read bytes per second", 4009 },{ 4010 .name = "throttling.bps-write", 4011 .type = QEMU_OPT_NUMBER, 4012 .help = "limit write bytes per second", 4013 },{ 4014 .name = "throttling.iops-total-max", 4015 .type = QEMU_OPT_NUMBER, 4016 .help = "I/O operations burst", 4017 },{ 4018 .name = "throttling.iops-read-max", 4019 .type = QEMU_OPT_NUMBER, 4020 .help = "I/O operations read burst", 4021 },{ 4022 .name = "throttling.iops-write-max", 4023 .type = QEMU_OPT_NUMBER, 4024 .help = "I/O operations write burst", 4025 },{ 4026 .name = "throttling.bps-total-max", 4027 .type = QEMU_OPT_NUMBER, 4028 .help = "total bytes burst", 4029 },{ 4030 .name = "throttling.bps-read-max", 4031 .type = QEMU_OPT_NUMBER, 4032 .help = "total bytes read burst", 4033 },{ 4034 .name = "throttling.bps-write-max", 4035 .type = QEMU_OPT_NUMBER, 4036 .help = "total bytes write burst", 4037 },{ 4038 .name = "throttling.iops-size", 4039 .type = QEMU_OPT_NUMBER, 4040 .help = "when limiting by iops max size of an I/O in bytes", 4041 },{ 4042 .name = "throttling.group", 4043 .type = QEMU_OPT_STRING, 4044 .help = "name of the block throttling group", 4045 },{ 4046 .name = "copy-on-read", 4047 .type = QEMU_OPT_BOOL, 4048 .help = "copy read data from backing file into image file", 4049 },{ 4050 .name = "detect-zeroes", 4051 .type = QEMU_OPT_STRING, 4052 .help = "try to optimize zero writes (off, on, unmap)", 4053 },{ 4054 .name = "stats-account-invalid", 4055 .type = QEMU_OPT_BOOL, 4056 .help = "whether to account for invalid I/O operations " 4057 "in the statistics", 4058 },{ 4059 .name = "stats-account-failed", 4060 .type = QEMU_OPT_BOOL, 4061 .help = "whether to account for failed I/O operations " 4062 "in the statistics", 4063 }, 4064 { /* end of list */ } 4065 }, 4066 }; 4067 4068 static QemuOptsList qemu_root_bds_opts = { 4069 .name = "root-bds", 4070 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 4071 .desc = { 4072 { 4073 .name = "discard", 4074 .type = QEMU_OPT_STRING, 4075 .help = "discard operation (ignore/off, unmap/on)", 4076 },{ 4077 .name = "aio", 4078 .type = QEMU_OPT_STRING, 4079 .help = "host AIO implementation (threads, native)", 4080 },{ 4081 .name = "read-only", 4082 .type = QEMU_OPT_BOOL, 4083 .help = "open drive file as read-only", 4084 },{ 4085 .name = "copy-on-read", 4086 .type = QEMU_OPT_BOOL, 4087 .help = "copy read data from backing file into image file", 4088 },{ 4089 .name = "detect-zeroes", 4090 .type = QEMU_OPT_STRING, 4091 .help = "try to optimize zero writes (off, on, unmap)", 4092 }, 4093 { /* end of list */ } 4094 }, 4095 }; 4096 4097 QemuOptsList qemu_drive_opts = { 4098 .name = "drive", 4099 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 4100 .desc = { 4101 /* 4102 * no elements => accept any params 4103 * validation will happen later 4104 */ 4105 { /* end of list */ } 4106 }, 4107 }; 4108