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