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