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