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