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_init(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 AioContext **paio, 1366 Error **errp) 1367 { 1368 BlockDriverState *bs; 1369 BdrvDirtyBitmap *bitmap; 1370 AioContext *aio_context; 1371 1372 if (!node) { 1373 error_setg(errp, "Node cannot be NULL"); 1374 return NULL; 1375 } 1376 if (!name) { 1377 error_setg(errp, "Bitmap name cannot be NULL"); 1378 return NULL; 1379 } 1380 bs = bdrv_lookup_bs(node, node, NULL); 1381 if (!bs) { 1382 error_setg(errp, "Node '%s' not found", node); 1383 return NULL; 1384 } 1385 1386 aio_context = bdrv_get_aio_context(bs); 1387 aio_context_acquire(aio_context); 1388 1389 bitmap = bdrv_find_dirty_bitmap(bs, name); 1390 if (!bitmap) { 1391 error_setg(errp, "Dirty bitmap '%s' not found", name); 1392 goto fail; 1393 } 1394 1395 if (pbs) { 1396 *pbs = bs; 1397 } 1398 if (paio) { 1399 *paio = aio_context; 1400 } else { 1401 aio_context_release(aio_context); 1402 } 1403 1404 return bitmap; 1405 1406 fail: 1407 aio_context_release(aio_context); 1408 return NULL; 1409 } 1410 1411 /* New and old BlockDriverState structs for atomic group operations */ 1412 1413 typedef struct BlkActionState BlkActionState; 1414 1415 /** 1416 * BlkActionOps: 1417 * Table of operations that define an Action. 1418 * 1419 * @instance_size: Size of state struct, in bytes. 1420 * @prepare: Prepare the work, must NOT be NULL. 1421 * @commit: Commit the changes, can be NULL. 1422 * @abort: Abort the changes on fail, can be NULL. 1423 * @clean: Clean up resources after all transaction actions have called 1424 * commit() or abort(). Can be NULL. 1425 * 1426 * Only prepare() may fail. In a single transaction, only one of commit() or 1427 * abort() will be called. clean() will always be called if it is present. 1428 */ 1429 typedef struct BlkActionOps { 1430 size_t instance_size; 1431 void (*prepare)(BlkActionState *common, Error **errp); 1432 void (*commit)(BlkActionState *common); 1433 void (*abort)(BlkActionState *common); 1434 void (*clean)(BlkActionState *common); 1435 } BlkActionOps; 1436 1437 /** 1438 * BlkActionState: 1439 * Describes one Action's state within a Transaction. 1440 * 1441 * @action: QAPI-defined enum identifying which Action to perform. 1442 * @ops: Table of ActionOps this Action can perform. 1443 * @block_job_txn: Transaction which this action belongs to. 1444 * @entry: List membership for all Actions in this Transaction. 1445 * 1446 * This structure must be arranged as first member in a subclassed type, 1447 * assuming that the compiler will also arrange it to the same offsets as the 1448 * base class. 1449 */ 1450 struct BlkActionState { 1451 TransactionAction *action; 1452 const BlkActionOps *ops; 1453 BlockJobTxn *block_job_txn; 1454 TransactionProperties *txn_props; 1455 QSIMPLEQ_ENTRY(BlkActionState) entry; 1456 }; 1457 1458 /* internal snapshot private data */ 1459 typedef struct InternalSnapshotState { 1460 BlkActionState common; 1461 BlockDriverState *bs; 1462 AioContext *aio_context; 1463 QEMUSnapshotInfo sn; 1464 bool created; 1465 } InternalSnapshotState; 1466 1467 1468 static int action_check_completion_mode(BlkActionState *s, Error **errp) 1469 { 1470 if (s->txn_props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 1471 error_setg(errp, 1472 "Action '%s' does not support Transaction property " 1473 "completion-mode = %s", 1474 TransactionActionKind_lookup[s->action->type], 1475 ActionCompletionMode_lookup[s->txn_props->completion_mode]); 1476 return -1; 1477 } 1478 return 0; 1479 } 1480 1481 static void internal_snapshot_prepare(BlkActionState *common, 1482 Error **errp) 1483 { 1484 Error *local_err = NULL; 1485 const char *device; 1486 const char *name; 1487 BlockDriverState *bs; 1488 QEMUSnapshotInfo old_sn, *sn; 1489 bool ret; 1490 qemu_timeval tv; 1491 BlockdevSnapshotInternal *internal; 1492 InternalSnapshotState *state; 1493 int ret1; 1494 1495 g_assert(common->action->type == 1496 TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC); 1497 internal = common->action->u.blockdev_snapshot_internal_sync.data; 1498 state = DO_UPCAST(InternalSnapshotState, common, common); 1499 1500 /* 1. parse input */ 1501 device = internal->device; 1502 name = internal->name; 1503 1504 /* 2. check for validation */ 1505 if (action_check_completion_mode(common, errp) < 0) { 1506 return; 1507 } 1508 1509 bs = qmp_get_root_bs(device, errp); 1510 if (!bs) { 1511 return; 1512 } 1513 1514 /* AioContext is released in .clean() */ 1515 state->aio_context = bdrv_get_aio_context(bs); 1516 aio_context_acquire(state->aio_context); 1517 1518 state->bs = bs; 1519 bdrv_drained_begin(bs); 1520 1521 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, errp)) { 1522 return; 1523 } 1524 1525 if (bdrv_is_read_only(bs)) { 1526 error_setg(errp, "Device '%s' is read only", device); 1527 return; 1528 } 1529 1530 if (!bdrv_can_snapshot(bs)) { 1531 error_setg(errp, "Block format '%s' used by device '%s' " 1532 "does not support internal snapshots", 1533 bs->drv->format_name, device); 1534 return; 1535 } 1536 1537 if (!strlen(name)) { 1538 error_setg(errp, "Name is empty"); 1539 return; 1540 } 1541 1542 /* check whether a snapshot with name exist */ 1543 ret = bdrv_snapshot_find_by_id_and_name(bs, NULL, name, &old_sn, 1544 &local_err); 1545 if (local_err) { 1546 error_propagate(errp, local_err); 1547 return; 1548 } else if (ret) { 1549 error_setg(errp, 1550 "Snapshot with name '%s' already exists on device '%s'", 1551 name, device); 1552 return; 1553 } 1554 1555 /* 3. take the snapshot */ 1556 sn = &state->sn; 1557 pstrcpy(sn->name, sizeof(sn->name), name); 1558 qemu_gettimeofday(&tv); 1559 sn->date_sec = tv.tv_sec; 1560 sn->date_nsec = tv.tv_usec * 1000; 1561 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1562 1563 ret1 = bdrv_snapshot_create(bs, sn); 1564 if (ret1 < 0) { 1565 error_setg_errno(errp, -ret1, 1566 "Failed to create snapshot '%s' on device '%s'", 1567 name, device); 1568 return; 1569 } 1570 1571 /* 4. succeed, mark a snapshot is created */ 1572 state->created = true; 1573 } 1574 1575 static void internal_snapshot_abort(BlkActionState *common) 1576 { 1577 InternalSnapshotState *state = 1578 DO_UPCAST(InternalSnapshotState, common, common); 1579 BlockDriverState *bs = state->bs; 1580 QEMUSnapshotInfo *sn = &state->sn; 1581 Error *local_error = NULL; 1582 1583 if (!state->created) { 1584 return; 1585 } 1586 1587 if (bdrv_snapshot_delete(bs, sn->id_str, sn->name, &local_error) < 0) { 1588 error_reportf_err(local_error, 1589 "Failed to delete snapshot with id '%s' and " 1590 "name '%s' on device '%s' in abort: ", 1591 sn->id_str, sn->name, 1592 bdrv_get_device_name(bs)); 1593 } 1594 } 1595 1596 static void internal_snapshot_clean(BlkActionState *common) 1597 { 1598 InternalSnapshotState *state = DO_UPCAST(InternalSnapshotState, 1599 common, common); 1600 1601 if (state->aio_context) { 1602 if (state->bs) { 1603 bdrv_drained_end(state->bs); 1604 } 1605 aio_context_release(state->aio_context); 1606 } 1607 } 1608 1609 /* external snapshot private data */ 1610 typedef struct ExternalSnapshotState { 1611 BlkActionState common; 1612 BlockDriverState *old_bs; 1613 BlockDriverState *new_bs; 1614 AioContext *aio_context; 1615 bool overlay_appended; 1616 } ExternalSnapshotState; 1617 1618 static void external_snapshot_prepare(BlkActionState *common, 1619 Error **errp) 1620 { 1621 int flags = 0; 1622 QDict *options = NULL; 1623 Error *local_err = NULL; 1624 /* Device and node name of the image to generate the snapshot from */ 1625 const char *device; 1626 const char *node_name; 1627 /* Reference to the new image (for 'blockdev-snapshot') */ 1628 const char *snapshot_ref; 1629 /* File name of the new image (for 'blockdev-snapshot-sync') */ 1630 const char *new_image_file; 1631 ExternalSnapshotState *state = 1632 DO_UPCAST(ExternalSnapshotState, common, common); 1633 TransactionAction *action = common->action; 1634 1635 /* 'blockdev-snapshot' and 'blockdev-snapshot-sync' have similar 1636 * purpose but a different set of parameters */ 1637 switch (action->type) { 1638 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT: 1639 { 1640 BlockdevSnapshot *s = action->u.blockdev_snapshot.data; 1641 device = s->node; 1642 node_name = s->node; 1643 new_image_file = NULL; 1644 snapshot_ref = s->overlay; 1645 } 1646 break; 1647 case TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC: 1648 { 1649 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 1650 device = s->has_device ? s->device : NULL; 1651 node_name = s->has_node_name ? s->node_name : NULL; 1652 new_image_file = s->snapshot_file; 1653 snapshot_ref = NULL; 1654 } 1655 break; 1656 default: 1657 g_assert_not_reached(); 1658 } 1659 1660 /* start processing */ 1661 if (action_check_completion_mode(common, errp) < 0) { 1662 return; 1663 } 1664 1665 state->old_bs = bdrv_lookup_bs(device, node_name, errp); 1666 if (!state->old_bs) { 1667 return; 1668 } 1669 1670 /* Acquire AioContext now so any threads operating on old_bs stop */ 1671 state->aio_context = bdrv_get_aio_context(state->old_bs); 1672 aio_context_acquire(state->aio_context); 1673 bdrv_drained_begin(state->old_bs); 1674 1675 if (!bdrv_is_inserted(state->old_bs)) { 1676 error_setg(errp, QERR_DEVICE_HAS_NO_MEDIUM, device); 1677 return; 1678 } 1679 1680 if (bdrv_op_is_blocked(state->old_bs, 1681 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, errp)) { 1682 return; 1683 } 1684 1685 if (!bdrv_is_read_only(state->old_bs)) { 1686 if (bdrv_flush(state->old_bs)) { 1687 error_setg(errp, QERR_IO_ERROR); 1688 return; 1689 } 1690 } 1691 1692 if (!bdrv_is_first_non_filter(state->old_bs)) { 1693 error_setg(errp, QERR_FEATURE_DISABLED, "snapshot"); 1694 return; 1695 } 1696 1697 if (action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC) { 1698 BlockdevSnapshotSync *s = action->u.blockdev_snapshot_sync.data; 1699 const char *format = s->has_format ? s->format : "qcow2"; 1700 enum NewImageMode mode; 1701 const char *snapshot_node_name = 1702 s->has_snapshot_node_name ? s->snapshot_node_name : NULL; 1703 1704 if (node_name && !snapshot_node_name) { 1705 error_setg(errp, "New snapshot node name missing"); 1706 return; 1707 } 1708 1709 if (snapshot_node_name && 1710 bdrv_lookup_bs(snapshot_node_name, snapshot_node_name, NULL)) { 1711 error_setg(errp, "New snapshot node name already in use"); 1712 return; 1713 } 1714 1715 flags = state->old_bs->open_flags; 1716 flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 1717 1718 /* create new image w/backing file */ 1719 mode = s->has_mode ? s->mode : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 1720 if (mode != NEW_IMAGE_MODE_EXISTING) { 1721 int64_t size = bdrv_getlength(state->old_bs); 1722 if (size < 0) { 1723 error_setg_errno(errp, -size, "bdrv_getlength failed"); 1724 return; 1725 } 1726 bdrv_img_create(new_image_file, format, 1727 state->old_bs->filename, 1728 state->old_bs->drv->format_name, 1729 NULL, size, flags, false, &local_err); 1730 if (local_err) { 1731 error_propagate(errp, local_err); 1732 return; 1733 } 1734 } 1735 1736 options = qdict_new(); 1737 if (s->has_snapshot_node_name) { 1738 qdict_put_str(options, "node-name", snapshot_node_name); 1739 } 1740 qdict_put_str(options, "driver", format); 1741 1742 flags |= BDRV_O_NO_BACKING; 1743 } 1744 1745 state->new_bs = bdrv_open(new_image_file, snapshot_ref, options, flags, 1746 errp); 1747 /* We will manually add the backing_hd field to the bs later */ 1748 if (!state->new_bs) { 1749 return; 1750 } 1751 1752 if (bdrv_has_blk(state->new_bs)) { 1753 error_setg(errp, "The snapshot is already in use"); 1754 return; 1755 } 1756 1757 if (bdrv_op_is_blocked(state->new_bs, BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 1758 errp)) { 1759 return; 1760 } 1761 1762 if (state->new_bs->backing != NULL) { 1763 error_setg(errp, "The snapshot already has a backing image"); 1764 return; 1765 } 1766 1767 if (!state->new_bs->drv->supports_backing) { 1768 error_setg(errp, "The snapshot does not support backing images"); 1769 return; 1770 } 1771 1772 bdrv_set_aio_context(state->new_bs, state->aio_context); 1773 1774 /* This removes our old bs and adds the new bs. This is an operation that 1775 * can fail, so we need to do it in .prepare; undoing it for abort is 1776 * always possible. */ 1777 bdrv_ref(state->new_bs); 1778 bdrv_append(state->new_bs, state->old_bs, &local_err); 1779 if (local_err) { 1780 error_propagate(errp, local_err); 1781 return; 1782 } 1783 state->overlay_appended = true; 1784 } 1785 1786 static void external_snapshot_commit(BlkActionState *common) 1787 { 1788 ExternalSnapshotState *state = 1789 DO_UPCAST(ExternalSnapshotState, common, common); 1790 1791 /* We don't need (or want) to use the transactional 1792 * bdrv_reopen_multiple() across all the entries at once, because we 1793 * don't want to abort all of them if one of them fails the reopen */ 1794 if (!state->old_bs->copy_on_read) { 1795 bdrv_reopen(state->old_bs, state->old_bs->open_flags & ~BDRV_O_RDWR, 1796 NULL); 1797 } 1798 } 1799 1800 static void external_snapshot_abort(BlkActionState *common) 1801 { 1802 ExternalSnapshotState *state = 1803 DO_UPCAST(ExternalSnapshotState, common, common); 1804 if (state->new_bs) { 1805 if (state->overlay_appended) { 1806 bdrv_ref(state->old_bs); /* we can't let bdrv_set_backind_hd() 1807 close state->old_bs; we need it */ 1808 bdrv_set_backing_hd(state->new_bs, NULL, &error_abort); 1809 bdrv_replace_node(state->new_bs, state->old_bs, &error_abort); 1810 bdrv_unref(state->old_bs); /* bdrv_replace_node() ref'ed old_bs */ 1811 } 1812 } 1813 } 1814 1815 static void external_snapshot_clean(BlkActionState *common) 1816 { 1817 ExternalSnapshotState *state = 1818 DO_UPCAST(ExternalSnapshotState, common, common); 1819 if (state->aio_context) { 1820 bdrv_drained_end(state->old_bs); 1821 aio_context_release(state->aio_context); 1822 bdrv_unref(state->new_bs); 1823 } 1824 } 1825 1826 typedef struct DriveBackupState { 1827 BlkActionState common; 1828 BlockDriverState *bs; 1829 AioContext *aio_context; 1830 BlockJob *job; 1831 } DriveBackupState; 1832 1833 static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, 1834 Error **errp); 1835 1836 static void drive_backup_prepare(BlkActionState *common, Error **errp) 1837 { 1838 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1839 BlockDriverState *bs; 1840 DriveBackup *backup; 1841 Error *local_err = NULL; 1842 1843 assert(common->action->type == TRANSACTION_ACTION_KIND_DRIVE_BACKUP); 1844 backup = common->action->u.drive_backup.data; 1845 1846 bs = qmp_get_root_bs(backup->device, errp); 1847 if (!bs) { 1848 return; 1849 } 1850 1851 /* AioContext is released in .clean() */ 1852 state->aio_context = bdrv_get_aio_context(bs); 1853 aio_context_acquire(state->aio_context); 1854 bdrv_drained_begin(bs); 1855 state->bs = bs; 1856 1857 state->job = do_drive_backup(backup, common->block_job_txn, &local_err); 1858 if (local_err) { 1859 error_propagate(errp, local_err); 1860 return; 1861 } 1862 } 1863 1864 static void drive_backup_commit(BlkActionState *common) 1865 { 1866 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1867 assert(state->job); 1868 block_job_start(state->job); 1869 } 1870 1871 static void drive_backup_abort(BlkActionState *common) 1872 { 1873 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1874 1875 if (state->job) { 1876 block_job_cancel_sync(state->job); 1877 } 1878 } 1879 1880 static void drive_backup_clean(BlkActionState *common) 1881 { 1882 DriveBackupState *state = DO_UPCAST(DriveBackupState, common, common); 1883 1884 if (state->aio_context) { 1885 bdrv_drained_end(state->bs); 1886 aio_context_release(state->aio_context); 1887 } 1888 } 1889 1890 typedef struct BlockdevBackupState { 1891 BlkActionState common; 1892 BlockDriverState *bs; 1893 BlockJob *job; 1894 AioContext *aio_context; 1895 } BlockdevBackupState; 1896 1897 static BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, 1898 Error **errp); 1899 1900 static void blockdev_backup_prepare(BlkActionState *common, Error **errp) 1901 { 1902 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1903 BlockdevBackup *backup; 1904 BlockDriverState *bs, *target; 1905 Error *local_err = NULL; 1906 1907 assert(common->action->type == TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP); 1908 backup = common->action->u.blockdev_backup.data; 1909 1910 bs = qmp_get_root_bs(backup->device, errp); 1911 if (!bs) { 1912 return; 1913 } 1914 1915 target = bdrv_lookup_bs(backup->target, backup->target, errp); 1916 if (!target) { 1917 return; 1918 } 1919 1920 /* AioContext is released in .clean() */ 1921 state->aio_context = bdrv_get_aio_context(bs); 1922 if (state->aio_context != bdrv_get_aio_context(target)) { 1923 state->aio_context = NULL; 1924 error_setg(errp, "Backup between two IO threads is not implemented"); 1925 return; 1926 } 1927 aio_context_acquire(state->aio_context); 1928 state->bs = bs; 1929 bdrv_drained_begin(state->bs); 1930 1931 state->job = do_blockdev_backup(backup, common->block_job_txn, &local_err); 1932 if (local_err) { 1933 error_propagate(errp, local_err); 1934 return; 1935 } 1936 } 1937 1938 static void blockdev_backup_commit(BlkActionState *common) 1939 { 1940 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1941 assert(state->job); 1942 block_job_start(state->job); 1943 } 1944 1945 static void blockdev_backup_abort(BlkActionState *common) 1946 { 1947 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1948 1949 if (state->job) { 1950 block_job_cancel_sync(state->job); 1951 } 1952 } 1953 1954 static void blockdev_backup_clean(BlkActionState *common) 1955 { 1956 BlockdevBackupState *state = DO_UPCAST(BlockdevBackupState, common, common); 1957 1958 if (state->aio_context) { 1959 bdrv_drained_end(state->bs); 1960 aio_context_release(state->aio_context); 1961 } 1962 } 1963 1964 typedef struct BlockDirtyBitmapState { 1965 BlkActionState common; 1966 BdrvDirtyBitmap *bitmap; 1967 BlockDriverState *bs; 1968 AioContext *aio_context; 1969 HBitmap *backup; 1970 bool prepared; 1971 } BlockDirtyBitmapState; 1972 1973 static void block_dirty_bitmap_add_prepare(BlkActionState *common, 1974 Error **errp) 1975 { 1976 Error *local_err = NULL; 1977 BlockDirtyBitmapAdd *action; 1978 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 1979 common, common); 1980 1981 if (action_check_completion_mode(common, errp) < 0) { 1982 return; 1983 } 1984 1985 action = common->action->u.block_dirty_bitmap_add.data; 1986 /* AIO context taken and released within qmp_block_dirty_bitmap_add */ 1987 qmp_block_dirty_bitmap_add(action->node, action->name, 1988 action->has_granularity, action->granularity, 1989 &local_err); 1990 1991 if (!local_err) { 1992 state->prepared = true; 1993 } else { 1994 error_propagate(errp, local_err); 1995 } 1996 } 1997 1998 static void block_dirty_bitmap_add_abort(BlkActionState *common) 1999 { 2000 BlockDirtyBitmapAdd *action; 2001 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2002 common, common); 2003 2004 action = common->action->u.block_dirty_bitmap_add.data; 2005 /* Should not be able to fail: IF the bitmap was added via .prepare(), 2006 * then the node reference and bitmap name must have been valid. 2007 */ 2008 if (state->prepared) { 2009 qmp_block_dirty_bitmap_remove(action->node, action->name, &error_abort); 2010 } 2011 } 2012 2013 static void block_dirty_bitmap_clear_prepare(BlkActionState *common, 2014 Error **errp) 2015 { 2016 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2017 common, common); 2018 BlockDirtyBitmap *action; 2019 2020 if (action_check_completion_mode(common, errp) < 0) { 2021 return; 2022 } 2023 2024 action = common->action->u.block_dirty_bitmap_clear.data; 2025 state->bitmap = block_dirty_bitmap_lookup(action->node, 2026 action->name, 2027 &state->bs, 2028 &state->aio_context, 2029 errp); 2030 if (!state->bitmap) { 2031 return; 2032 } 2033 2034 if (bdrv_dirty_bitmap_frozen(state->bitmap)) { 2035 error_setg(errp, "Cannot modify a frozen bitmap"); 2036 return; 2037 } else if (!bdrv_dirty_bitmap_enabled(state->bitmap)) { 2038 error_setg(errp, "Cannot clear a disabled bitmap"); 2039 return; 2040 } 2041 2042 bdrv_clear_dirty_bitmap(state->bitmap, &state->backup); 2043 /* AioContext is released in .clean() */ 2044 } 2045 2046 static void block_dirty_bitmap_clear_abort(BlkActionState *common) 2047 { 2048 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2049 common, common); 2050 2051 if (state->backup) { 2052 bdrv_undo_clear_dirty_bitmap(state->bitmap, state->backup); 2053 } 2054 } 2055 2056 static void block_dirty_bitmap_clear_commit(BlkActionState *common) 2057 { 2058 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2059 common, common); 2060 2061 hbitmap_free(state->backup); 2062 } 2063 2064 static void block_dirty_bitmap_clear_clean(BlkActionState *common) 2065 { 2066 BlockDirtyBitmapState *state = DO_UPCAST(BlockDirtyBitmapState, 2067 common, common); 2068 2069 if (state->aio_context) { 2070 aio_context_release(state->aio_context); 2071 } 2072 } 2073 2074 static void abort_prepare(BlkActionState *common, Error **errp) 2075 { 2076 error_setg(errp, "Transaction aborted using Abort action"); 2077 } 2078 2079 static void abort_commit(BlkActionState *common) 2080 { 2081 g_assert_not_reached(); /* this action never succeeds */ 2082 } 2083 2084 static const BlkActionOps actions[] = { 2085 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT] = { 2086 .instance_size = sizeof(ExternalSnapshotState), 2087 .prepare = external_snapshot_prepare, 2088 .commit = external_snapshot_commit, 2089 .abort = external_snapshot_abort, 2090 .clean = external_snapshot_clean, 2091 }, 2092 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_SYNC] = { 2093 .instance_size = sizeof(ExternalSnapshotState), 2094 .prepare = external_snapshot_prepare, 2095 .commit = external_snapshot_commit, 2096 .abort = external_snapshot_abort, 2097 .clean = external_snapshot_clean, 2098 }, 2099 [TRANSACTION_ACTION_KIND_DRIVE_BACKUP] = { 2100 .instance_size = sizeof(DriveBackupState), 2101 .prepare = drive_backup_prepare, 2102 .commit = drive_backup_commit, 2103 .abort = drive_backup_abort, 2104 .clean = drive_backup_clean, 2105 }, 2106 [TRANSACTION_ACTION_KIND_BLOCKDEV_BACKUP] = { 2107 .instance_size = sizeof(BlockdevBackupState), 2108 .prepare = blockdev_backup_prepare, 2109 .commit = blockdev_backup_commit, 2110 .abort = blockdev_backup_abort, 2111 .clean = blockdev_backup_clean, 2112 }, 2113 [TRANSACTION_ACTION_KIND_ABORT] = { 2114 .instance_size = sizeof(BlkActionState), 2115 .prepare = abort_prepare, 2116 .commit = abort_commit, 2117 }, 2118 [TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC] = { 2119 .instance_size = sizeof(InternalSnapshotState), 2120 .prepare = internal_snapshot_prepare, 2121 .abort = internal_snapshot_abort, 2122 .clean = internal_snapshot_clean, 2123 }, 2124 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_ADD] = { 2125 .instance_size = sizeof(BlockDirtyBitmapState), 2126 .prepare = block_dirty_bitmap_add_prepare, 2127 .abort = block_dirty_bitmap_add_abort, 2128 }, 2129 [TRANSACTION_ACTION_KIND_BLOCK_DIRTY_BITMAP_CLEAR] = { 2130 .instance_size = sizeof(BlockDirtyBitmapState), 2131 .prepare = block_dirty_bitmap_clear_prepare, 2132 .commit = block_dirty_bitmap_clear_commit, 2133 .abort = block_dirty_bitmap_clear_abort, 2134 .clean = block_dirty_bitmap_clear_clean, 2135 } 2136 }; 2137 2138 /** 2139 * Allocate a TransactionProperties structure if necessary, and fill 2140 * that structure with desired defaults if they are unset. 2141 */ 2142 static TransactionProperties *get_transaction_properties( 2143 TransactionProperties *props) 2144 { 2145 if (!props) { 2146 props = g_new0(TransactionProperties, 1); 2147 } 2148 2149 if (!props->has_completion_mode) { 2150 props->has_completion_mode = true; 2151 props->completion_mode = ACTION_COMPLETION_MODE_INDIVIDUAL; 2152 } 2153 2154 return props; 2155 } 2156 2157 /* 2158 * 'Atomic' group operations. The operations are performed as a set, and if 2159 * any fail then we roll back all operations in the group. 2160 */ 2161 void qmp_transaction(TransactionActionList *dev_list, 2162 bool has_props, 2163 struct TransactionProperties *props, 2164 Error **errp) 2165 { 2166 TransactionActionList *dev_entry = dev_list; 2167 BlockJobTxn *block_job_txn = NULL; 2168 BlkActionState *state, *next; 2169 Error *local_err = NULL; 2170 2171 QSIMPLEQ_HEAD(snap_bdrv_states, BlkActionState) snap_bdrv_states; 2172 QSIMPLEQ_INIT(&snap_bdrv_states); 2173 2174 /* Does this transaction get canceled as a group on failure? 2175 * If not, we don't really need to make a BlockJobTxn. 2176 */ 2177 props = get_transaction_properties(props); 2178 if (props->completion_mode != ACTION_COMPLETION_MODE_INDIVIDUAL) { 2179 block_job_txn = block_job_txn_new(); 2180 } 2181 2182 /* drain all i/o before any operations */ 2183 bdrv_drain_all(); 2184 2185 /* We don't do anything in this loop that commits us to the operations */ 2186 while (NULL != dev_entry) { 2187 TransactionAction *dev_info = NULL; 2188 const BlkActionOps *ops; 2189 2190 dev_info = dev_entry->value; 2191 dev_entry = dev_entry->next; 2192 2193 assert(dev_info->type < ARRAY_SIZE(actions)); 2194 2195 ops = &actions[dev_info->type]; 2196 assert(ops->instance_size > 0); 2197 2198 state = g_malloc0(ops->instance_size); 2199 state->ops = ops; 2200 state->action = dev_info; 2201 state->block_job_txn = block_job_txn; 2202 state->txn_props = props; 2203 QSIMPLEQ_INSERT_TAIL(&snap_bdrv_states, state, entry); 2204 2205 state->ops->prepare(state, &local_err); 2206 if (local_err) { 2207 error_propagate(errp, local_err); 2208 goto delete_and_fail; 2209 } 2210 } 2211 2212 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2213 if (state->ops->commit) { 2214 state->ops->commit(state); 2215 } 2216 } 2217 2218 /* success */ 2219 goto exit; 2220 2221 delete_and_fail: 2222 /* failure, and it is all-or-none; roll back all operations */ 2223 QSIMPLEQ_FOREACH(state, &snap_bdrv_states, entry) { 2224 if (state->ops->abort) { 2225 state->ops->abort(state); 2226 } 2227 } 2228 exit: 2229 QSIMPLEQ_FOREACH_SAFE(state, &snap_bdrv_states, entry, next) { 2230 if (state->ops->clean) { 2231 state->ops->clean(state); 2232 } 2233 g_free(state); 2234 } 2235 if (!has_props) { 2236 qapi_free_TransactionProperties(props); 2237 } 2238 block_job_txn_unref(block_job_txn); 2239 } 2240 2241 void qmp_eject(bool has_device, const char *device, 2242 bool has_id, const char *id, 2243 bool has_force, bool force, Error **errp) 2244 { 2245 Error *local_err = NULL; 2246 int rc; 2247 2248 if (!has_force) { 2249 force = false; 2250 } 2251 2252 rc = do_open_tray(has_device ? device : NULL, 2253 has_id ? id : NULL, 2254 force, &local_err); 2255 if (rc && rc != -ENOSYS) { 2256 error_propagate(errp, local_err); 2257 return; 2258 } 2259 error_free(local_err); 2260 2261 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, errp); 2262 } 2263 2264 void qmp_block_passwd(bool has_device, const char *device, 2265 bool has_node_name, const char *node_name, 2266 const char *password, Error **errp) 2267 { 2268 Error *local_err = NULL; 2269 BlockDriverState *bs; 2270 AioContext *aio_context; 2271 2272 bs = bdrv_lookup_bs(has_device ? device : NULL, 2273 has_node_name ? node_name : NULL, 2274 &local_err); 2275 if (local_err) { 2276 error_propagate(errp, local_err); 2277 return; 2278 } 2279 2280 aio_context = bdrv_get_aio_context(bs); 2281 aio_context_acquire(aio_context); 2282 2283 bdrv_add_key(bs, password, errp); 2284 2285 aio_context_release(aio_context); 2286 } 2287 2288 /* 2289 * Attempt to open the tray of @device. 2290 * If @force, ignore its tray lock. 2291 * Else, if the tray is locked, don't open it, but ask the guest to open it. 2292 * On error, store an error through @errp and return -errno. 2293 * If @device does not exist, return -ENODEV. 2294 * If it has no removable media, return -ENOTSUP. 2295 * If it has no tray, return -ENOSYS. 2296 * If the guest was asked to open the tray, return -EINPROGRESS. 2297 * Else, return 0. 2298 */ 2299 static int do_open_tray(const char *blk_name, const char *qdev_id, 2300 bool force, Error **errp) 2301 { 2302 BlockBackend *blk; 2303 const char *device = qdev_id ?: blk_name; 2304 bool locked; 2305 2306 blk = qmp_get_blk(blk_name, qdev_id, errp); 2307 if (!blk) { 2308 return -ENODEV; 2309 } 2310 2311 if (!blk_dev_has_removable_media(blk)) { 2312 error_setg(errp, "Device '%s' is not removable", device); 2313 return -ENOTSUP; 2314 } 2315 2316 if (!blk_dev_has_tray(blk)) { 2317 error_setg(errp, "Device '%s' does not have a tray", device); 2318 return -ENOSYS; 2319 } 2320 2321 if (blk_dev_is_tray_open(blk)) { 2322 return 0; 2323 } 2324 2325 locked = blk_dev_is_medium_locked(blk); 2326 if (locked) { 2327 blk_dev_eject_request(blk, force); 2328 } 2329 2330 if (!locked || force) { 2331 blk_dev_change_media_cb(blk, false, &error_abort); 2332 } 2333 2334 if (locked && !force) { 2335 error_setg(errp, "Device '%s' is locked and force was not specified, " 2336 "wait for tray to open and try again", device); 2337 return -EINPROGRESS; 2338 } 2339 2340 return 0; 2341 } 2342 2343 void qmp_blockdev_open_tray(bool has_device, const char *device, 2344 bool has_id, const char *id, 2345 bool has_force, bool force, 2346 Error **errp) 2347 { 2348 Error *local_err = NULL; 2349 int rc; 2350 2351 if (!has_force) { 2352 force = false; 2353 } 2354 rc = do_open_tray(has_device ? device : NULL, 2355 has_id ? id : NULL, 2356 force, &local_err); 2357 if (rc && rc != -ENOSYS && rc != -EINPROGRESS) { 2358 error_propagate(errp, local_err); 2359 return; 2360 } 2361 error_free(local_err); 2362 } 2363 2364 void qmp_blockdev_close_tray(bool has_device, const char *device, 2365 bool has_id, const char *id, 2366 Error **errp) 2367 { 2368 BlockBackend *blk; 2369 Error *local_err = NULL; 2370 2371 device = has_device ? device : NULL; 2372 id = has_id ? id : NULL; 2373 2374 blk = qmp_get_blk(device, id, errp); 2375 if (!blk) { 2376 return; 2377 } 2378 2379 if (!blk_dev_has_removable_media(blk)) { 2380 error_setg(errp, "Device '%s' is not removable", device ?: id); 2381 return; 2382 } 2383 2384 if (!blk_dev_has_tray(blk)) { 2385 /* Ignore this command on tray-less devices */ 2386 return; 2387 } 2388 2389 if (!blk_dev_is_tray_open(blk)) { 2390 return; 2391 } 2392 2393 blk_dev_change_media_cb(blk, true, &local_err); 2394 if (local_err) { 2395 error_propagate(errp, local_err); 2396 return; 2397 } 2398 } 2399 2400 void qmp_x_blockdev_remove_medium(bool has_device, const char *device, 2401 bool has_id, const char *id, Error **errp) 2402 { 2403 BlockBackend *blk; 2404 BlockDriverState *bs; 2405 AioContext *aio_context; 2406 bool has_attached_device; 2407 2408 device = has_device ? device : NULL; 2409 id = has_id ? id : NULL; 2410 2411 blk = qmp_get_blk(device, id, errp); 2412 if (!blk) { 2413 return; 2414 } 2415 2416 /* For BBs without a device, we can exchange the BDS tree at will */ 2417 has_attached_device = blk_get_attached_dev(blk); 2418 2419 if (has_attached_device && !blk_dev_has_removable_media(blk)) { 2420 error_setg(errp, "Device '%s' is not removable", device ?: id); 2421 return; 2422 } 2423 2424 if (has_attached_device && blk_dev_has_tray(blk) && 2425 !blk_dev_is_tray_open(blk)) 2426 { 2427 error_setg(errp, "Tray of device '%s' is not open", device ?: id); 2428 return; 2429 } 2430 2431 bs = blk_bs(blk); 2432 if (!bs) { 2433 return; 2434 } 2435 2436 aio_context = bdrv_get_aio_context(bs); 2437 aio_context_acquire(aio_context); 2438 2439 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_EJECT, errp)) { 2440 goto out; 2441 } 2442 2443 blk_remove_bs(blk); 2444 2445 if (!blk_dev_has_tray(blk)) { 2446 /* For tray-less devices, blockdev-open-tray is a no-op (or may not be 2447 * called at all); therefore, the medium needs to be ejected here. 2448 * Do it after blk_remove_bs() so blk_is_inserted(blk) returns the @load 2449 * value passed here (i.e. false). */ 2450 blk_dev_change_media_cb(blk, false, &error_abort); 2451 } 2452 2453 out: 2454 aio_context_release(aio_context); 2455 } 2456 2457 static void qmp_blockdev_insert_anon_medium(BlockBackend *blk, 2458 BlockDriverState *bs, Error **errp) 2459 { 2460 Error *local_err = NULL; 2461 bool has_device; 2462 int ret; 2463 2464 /* For BBs without a device, we can exchange the BDS tree at will */ 2465 has_device = blk_get_attached_dev(blk); 2466 2467 if (has_device && !blk_dev_has_removable_media(blk)) { 2468 error_setg(errp, "Device is not removable"); 2469 return; 2470 } 2471 2472 if (has_device && blk_dev_has_tray(blk) && !blk_dev_is_tray_open(blk)) { 2473 error_setg(errp, "Tray of the device is not open"); 2474 return; 2475 } 2476 2477 if (blk_bs(blk)) { 2478 error_setg(errp, "There already is a medium in the device"); 2479 return; 2480 } 2481 2482 ret = blk_insert_bs(blk, bs, errp); 2483 if (ret < 0) { 2484 return; 2485 } 2486 2487 if (!blk_dev_has_tray(blk)) { 2488 /* For tray-less devices, blockdev-close-tray is a no-op (or may not be 2489 * called at all); therefore, the medium needs to be pushed into the 2490 * slot here. 2491 * Do it after blk_insert_bs() so blk_is_inserted(blk) returns the @load 2492 * value passed here (i.e. true). */ 2493 blk_dev_change_media_cb(blk, true, &local_err); 2494 if (local_err) { 2495 error_propagate(errp, local_err); 2496 blk_remove_bs(blk); 2497 return; 2498 } 2499 } 2500 } 2501 2502 void qmp_x_blockdev_insert_medium(bool has_device, const char *device, 2503 bool has_id, const char *id, 2504 const char *node_name, Error **errp) 2505 { 2506 BlockBackend *blk; 2507 BlockDriverState *bs; 2508 2509 blk = qmp_get_blk(has_device ? device : NULL, 2510 has_id ? id : NULL, 2511 errp); 2512 if (!blk) { 2513 return; 2514 } 2515 2516 bs = bdrv_find_node(node_name); 2517 if (!bs) { 2518 error_setg(errp, "Node '%s' not found", node_name); 2519 return; 2520 } 2521 2522 if (bdrv_has_blk(bs)) { 2523 error_setg(errp, "Node '%s' is already in use", node_name); 2524 return; 2525 } 2526 2527 qmp_blockdev_insert_anon_medium(blk, bs, errp); 2528 } 2529 2530 void qmp_blockdev_change_medium(bool has_device, const char *device, 2531 bool has_id, const char *id, 2532 const char *filename, 2533 bool has_format, const char *format, 2534 bool has_read_only, 2535 BlockdevChangeReadOnlyMode read_only, 2536 Error **errp) 2537 { 2538 BlockBackend *blk; 2539 BlockDriverState *medium_bs = NULL; 2540 int bdrv_flags; 2541 bool detect_zeroes; 2542 int rc; 2543 QDict *options = NULL; 2544 Error *err = NULL; 2545 2546 blk = qmp_get_blk(has_device ? device : NULL, 2547 has_id ? id : NULL, 2548 errp); 2549 if (!blk) { 2550 goto fail; 2551 } 2552 2553 if (blk_bs(blk)) { 2554 blk_update_root_state(blk); 2555 } 2556 2557 bdrv_flags = blk_get_open_flags_from_root_state(blk); 2558 bdrv_flags &= ~(BDRV_O_TEMPORARY | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | 2559 BDRV_O_PROTOCOL); 2560 2561 if (!has_read_only) { 2562 read_only = BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN; 2563 } 2564 2565 switch (read_only) { 2566 case BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN: 2567 break; 2568 2569 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_ONLY: 2570 bdrv_flags &= ~BDRV_O_RDWR; 2571 break; 2572 2573 case BLOCKDEV_CHANGE_READ_ONLY_MODE_READ_WRITE: 2574 bdrv_flags |= BDRV_O_RDWR; 2575 break; 2576 2577 default: 2578 abort(); 2579 } 2580 2581 options = qdict_new(); 2582 detect_zeroes = blk_get_detect_zeroes_from_root_state(blk); 2583 qdict_put_str(options, "detect-zeroes", detect_zeroes ? "on" : "off"); 2584 2585 if (has_format) { 2586 qdict_put_str(options, "driver", format); 2587 } 2588 2589 medium_bs = bdrv_open(filename, NULL, options, bdrv_flags, errp); 2590 if (!medium_bs) { 2591 goto fail; 2592 } 2593 2594 bdrv_add_key(medium_bs, NULL, &err); 2595 if (err) { 2596 error_propagate(errp, err); 2597 goto fail; 2598 } 2599 2600 rc = do_open_tray(has_device ? device : NULL, 2601 has_id ? id : NULL, 2602 false, &err); 2603 if (rc && rc != -ENOSYS) { 2604 error_propagate(errp, err); 2605 goto fail; 2606 } 2607 error_free(err); 2608 err = NULL; 2609 2610 qmp_x_blockdev_remove_medium(has_device, device, has_id, id, &err); 2611 if (err) { 2612 error_propagate(errp, err); 2613 goto fail; 2614 } 2615 2616 qmp_blockdev_insert_anon_medium(blk, medium_bs, &err); 2617 if (err) { 2618 error_propagate(errp, err); 2619 goto fail; 2620 } 2621 2622 qmp_blockdev_close_tray(has_device, device, has_id, id, errp); 2623 2624 fail: 2625 /* If the medium has been inserted, the device has its own reference, so 2626 * ours must be relinquished; and if it has not been inserted successfully, 2627 * the reference must be relinquished anyway */ 2628 bdrv_unref(medium_bs); 2629 } 2630 2631 /* throttling disk I/O limits */ 2632 void qmp_block_set_io_throttle(BlockIOThrottle *arg, Error **errp) 2633 { 2634 ThrottleConfig cfg; 2635 BlockDriverState *bs; 2636 BlockBackend *blk; 2637 AioContext *aio_context; 2638 2639 blk = qmp_get_blk(arg->has_device ? arg->device : NULL, 2640 arg->has_id ? arg->id : NULL, 2641 errp); 2642 if (!blk) { 2643 return; 2644 } 2645 2646 aio_context = blk_get_aio_context(blk); 2647 aio_context_acquire(aio_context); 2648 2649 bs = blk_bs(blk); 2650 if (!bs) { 2651 error_setg(errp, "Device has no medium"); 2652 goto out; 2653 } 2654 2655 throttle_config_init(&cfg); 2656 cfg.buckets[THROTTLE_BPS_TOTAL].avg = arg->bps; 2657 cfg.buckets[THROTTLE_BPS_READ].avg = arg->bps_rd; 2658 cfg.buckets[THROTTLE_BPS_WRITE].avg = arg->bps_wr; 2659 2660 cfg.buckets[THROTTLE_OPS_TOTAL].avg = arg->iops; 2661 cfg.buckets[THROTTLE_OPS_READ].avg = arg->iops_rd; 2662 cfg.buckets[THROTTLE_OPS_WRITE].avg = arg->iops_wr; 2663 2664 if (arg->has_bps_max) { 2665 cfg.buckets[THROTTLE_BPS_TOTAL].max = arg->bps_max; 2666 } 2667 if (arg->has_bps_rd_max) { 2668 cfg.buckets[THROTTLE_BPS_READ].max = arg->bps_rd_max; 2669 } 2670 if (arg->has_bps_wr_max) { 2671 cfg.buckets[THROTTLE_BPS_WRITE].max = arg->bps_wr_max; 2672 } 2673 if (arg->has_iops_max) { 2674 cfg.buckets[THROTTLE_OPS_TOTAL].max = arg->iops_max; 2675 } 2676 if (arg->has_iops_rd_max) { 2677 cfg.buckets[THROTTLE_OPS_READ].max = arg->iops_rd_max; 2678 } 2679 if (arg->has_iops_wr_max) { 2680 cfg.buckets[THROTTLE_OPS_WRITE].max = arg->iops_wr_max; 2681 } 2682 2683 if (arg->has_bps_max_length) { 2684 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length = arg->bps_max_length; 2685 } 2686 if (arg->has_bps_rd_max_length) { 2687 cfg.buckets[THROTTLE_BPS_READ].burst_length = arg->bps_rd_max_length; 2688 } 2689 if (arg->has_bps_wr_max_length) { 2690 cfg.buckets[THROTTLE_BPS_WRITE].burst_length = arg->bps_wr_max_length; 2691 } 2692 if (arg->has_iops_max_length) { 2693 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length = arg->iops_max_length; 2694 } 2695 if (arg->has_iops_rd_max_length) { 2696 cfg.buckets[THROTTLE_OPS_READ].burst_length = arg->iops_rd_max_length; 2697 } 2698 if (arg->has_iops_wr_max_length) { 2699 cfg.buckets[THROTTLE_OPS_WRITE].burst_length = arg->iops_wr_max_length; 2700 } 2701 2702 if (arg->has_iops_size) { 2703 cfg.op_size = arg->iops_size; 2704 } 2705 2706 if (!throttle_is_valid(&cfg, errp)) { 2707 goto out; 2708 } 2709 2710 if (throttle_enabled(&cfg)) { 2711 /* Enable I/O limits if they're not enabled yet, otherwise 2712 * just update the throttling group. */ 2713 if (!blk_get_public(blk)->throttle_state) { 2714 blk_io_limits_enable(blk, 2715 arg->has_group ? arg->group : 2716 arg->has_device ? arg->device : 2717 arg->id); 2718 } else if (arg->has_group) { 2719 blk_io_limits_update_group(blk, arg->group); 2720 } 2721 /* Set the new throttling configuration */ 2722 blk_set_io_limits(blk, &cfg); 2723 } else if (blk_get_public(blk)->throttle_state) { 2724 /* If all throttling settings are set to 0, disable I/O limits */ 2725 blk_io_limits_disable(blk); 2726 } 2727 2728 out: 2729 aio_context_release(aio_context); 2730 } 2731 2732 void qmp_block_dirty_bitmap_add(const char *node, const char *name, 2733 bool has_granularity, uint32_t granularity, 2734 Error **errp) 2735 { 2736 AioContext *aio_context; 2737 BlockDriverState *bs; 2738 2739 if (!name || name[0] == '\0') { 2740 error_setg(errp, "Bitmap name cannot be empty"); 2741 return; 2742 } 2743 2744 bs = bdrv_lookup_bs(node, node, errp); 2745 if (!bs) { 2746 return; 2747 } 2748 2749 aio_context = bdrv_get_aio_context(bs); 2750 aio_context_acquire(aio_context); 2751 2752 if (has_granularity) { 2753 if (granularity < 512 || !is_power_of_2(granularity)) { 2754 error_setg(errp, "Granularity must be power of 2 " 2755 "and at least 512"); 2756 goto out; 2757 } 2758 } else { 2759 /* Default to cluster size, if available: */ 2760 granularity = bdrv_get_default_bitmap_granularity(bs); 2761 } 2762 2763 bdrv_create_dirty_bitmap(bs, granularity, name, errp); 2764 2765 out: 2766 aio_context_release(aio_context); 2767 } 2768 2769 void qmp_block_dirty_bitmap_remove(const char *node, const char *name, 2770 Error **errp) 2771 { 2772 AioContext *aio_context; 2773 BlockDriverState *bs; 2774 BdrvDirtyBitmap *bitmap; 2775 2776 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2777 if (!bitmap || !bs) { 2778 return; 2779 } 2780 2781 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2782 error_setg(errp, 2783 "Bitmap '%s' is currently frozen and cannot be removed", 2784 name); 2785 goto out; 2786 } 2787 bdrv_dirty_bitmap_make_anon(bitmap); 2788 bdrv_release_dirty_bitmap(bs, bitmap); 2789 2790 out: 2791 aio_context_release(aio_context); 2792 } 2793 2794 /** 2795 * Completely clear a bitmap, for the purposes of synchronizing a bitmap 2796 * immediately after a full backup operation. 2797 */ 2798 void qmp_block_dirty_bitmap_clear(const char *node, const char *name, 2799 Error **errp) 2800 { 2801 AioContext *aio_context; 2802 BdrvDirtyBitmap *bitmap; 2803 BlockDriverState *bs; 2804 2805 bitmap = block_dirty_bitmap_lookup(node, name, &bs, &aio_context, errp); 2806 if (!bitmap || !bs) { 2807 return; 2808 } 2809 2810 if (bdrv_dirty_bitmap_frozen(bitmap)) { 2811 error_setg(errp, 2812 "Bitmap '%s' is currently frozen and cannot be modified", 2813 name); 2814 goto out; 2815 } else if (!bdrv_dirty_bitmap_enabled(bitmap)) { 2816 error_setg(errp, 2817 "Bitmap '%s' is currently disabled and cannot be cleared", 2818 name); 2819 goto out; 2820 } 2821 2822 bdrv_clear_dirty_bitmap(bitmap, NULL); 2823 2824 out: 2825 aio_context_release(aio_context); 2826 } 2827 2828 void hmp_drive_del(Monitor *mon, const QDict *qdict) 2829 { 2830 const char *id = qdict_get_str(qdict, "id"); 2831 BlockBackend *blk; 2832 BlockDriverState *bs; 2833 AioContext *aio_context; 2834 Error *local_err = NULL; 2835 2836 bs = bdrv_find_node(id); 2837 if (bs) { 2838 qmp_blockdev_del(id, &local_err); 2839 if (local_err) { 2840 error_report_err(local_err); 2841 } 2842 return; 2843 } 2844 2845 blk = blk_by_name(id); 2846 if (!blk) { 2847 error_report("Device '%s' not found", id); 2848 return; 2849 } 2850 2851 if (!blk_legacy_dinfo(blk)) { 2852 error_report("Deleting device added with blockdev-add" 2853 " is not supported"); 2854 return; 2855 } 2856 2857 aio_context = blk_get_aio_context(blk); 2858 aio_context_acquire(aio_context); 2859 2860 bs = blk_bs(blk); 2861 if (bs) { 2862 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 2863 error_report_err(local_err); 2864 aio_context_release(aio_context); 2865 return; 2866 } 2867 2868 blk_remove_bs(blk); 2869 } 2870 2871 /* Make the BlockBackend and the attached BlockDriverState anonymous */ 2872 monitor_remove_blk(blk); 2873 2874 /* If this BlockBackend has a device attached to it, its refcount will be 2875 * decremented when the device is removed; otherwise we have to do so here. 2876 */ 2877 if (blk_get_attached_dev(blk)) { 2878 /* Further I/O must not pause the guest */ 2879 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 2880 BLOCKDEV_ON_ERROR_REPORT); 2881 } else { 2882 blk_unref(blk); 2883 } 2884 2885 aio_context_release(aio_context); 2886 } 2887 2888 void qmp_block_resize(bool has_device, const char *device, 2889 bool has_node_name, const char *node_name, 2890 int64_t size, Error **errp) 2891 { 2892 Error *local_err = NULL; 2893 BlockBackend *blk = NULL; 2894 BlockDriverState *bs; 2895 AioContext *aio_context; 2896 int ret; 2897 2898 bs = bdrv_lookup_bs(has_device ? device : NULL, 2899 has_node_name ? node_name : NULL, 2900 &local_err); 2901 if (local_err) { 2902 error_propagate(errp, local_err); 2903 return; 2904 } 2905 2906 aio_context = bdrv_get_aio_context(bs); 2907 aio_context_acquire(aio_context); 2908 2909 if (!bdrv_is_first_non_filter(bs)) { 2910 error_setg(errp, QERR_FEATURE_DISABLED, "resize"); 2911 goto out; 2912 } 2913 2914 if (size < 0) { 2915 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "size", "a >0 size"); 2916 goto out; 2917 } 2918 2919 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 2920 error_setg(errp, QERR_DEVICE_IN_USE, device); 2921 goto out; 2922 } 2923 2924 blk = blk_new(BLK_PERM_RESIZE, BLK_PERM_ALL); 2925 ret = blk_insert_bs(blk, bs, errp); 2926 if (ret < 0) { 2927 goto out; 2928 } 2929 2930 bdrv_drained_begin(bs); 2931 ret = blk_truncate(blk, size, errp); 2932 bdrv_drained_end(bs); 2933 2934 out: 2935 blk_unref(blk); 2936 aio_context_release(aio_context); 2937 } 2938 2939 void qmp_block_stream(bool has_job_id, const char *job_id, const char *device, 2940 bool has_base, const char *base, 2941 bool has_base_node, const char *base_node, 2942 bool has_backing_file, const char *backing_file, 2943 bool has_speed, int64_t speed, 2944 bool has_on_error, BlockdevOnError on_error, 2945 Error **errp) 2946 { 2947 BlockDriverState *bs, *iter; 2948 BlockDriverState *base_bs = NULL; 2949 AioContext *aio_context; 2950 Error *local_err = NULL; 2951 const char *base_name = NULL; 2952 2953 if (!has_on_error) { 2954 on_error = BLOCKDEV_ON_ERROR_REPORT; 2955 } 2956 2957 bs = bdrv_lookup_bs(device, device, errp); 2958 if (!bs) { 2959 return; 2960 } 2961 2962 aio_context = bdrv_get_aio_context(bs); 2963 aio_context_acquire(aio_context); 2964 2965 if (has_base && has_base_node) { 2966 error_setg(errp, "'base' and 'base-node' cannot be specified " 2967 "at the same time"); 2968 goto out; 2969 } 2970 2971 if (has_base) { 2972 base_bs = bdrv_find_backing_image(bs, base); 2973 if (base_bs == NULL) { 2974 error_setg(errp, QERR_BASE_NOT_FOUND, base); 2975 goto out; 2976 } 2977 assert(bdrv_get_aio_context(base_bs) == aio_context); 2978 base_name = base; 2979 } 2980 2981 if (has_base_node) { 2982 base_bs = bdrv_lookup_bs(NULL, base_node, errp); 2983 if (!base_bs) { 2984 goto out; 2985 } 2986 if (bs == base_bs || !bdrv_chain_contains(bs, base_bs)) { 2987 error_setg(errp, "Node '%s' is not a backing image of '%s'", 2988 base_node, device); 2989 goto out; 2990 } 2991 assert(bdrv_get_aio_context(base_bs) == aio_context); 2992 base_name = base_bs->filename; 2993 } 2994 2995 /* Check for op blockers in the whole chain between bs and base */ 2996 for (iter = bs; iter && iter != base_bs; iter = backing_bs(iter)) { 2997 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_STREAM, errp)) { 2998 goto out; 2999 } 3000 } 3001 3002 /* if we are streaming the entire chain, the result will have no backing 3003 * file, and specifying one is therefore an error */ 3004 if (base_bs == NULL && has_backing_file) { 3005 error_setg(errp, "backing file specified, but streaming the " 3006 "entire chain"); 3007 goto out; 3008 } 3009 3010 /* backing_file string overrides base bs filename */ 3011 base_name = has_backing_file ? backing_file : base_name; 3012 3013 stream_start(has_job_id ? job_id : NULL, bs, base_bs, base_name, 3014 has_speed ? speed : 0, on_error, &local_err); 3015 if (local_err) { 3016 error_propagate(errp, local_err); 3017 goto out; 3018 } 3019 3020 trace_qmp_block_stream(bs, bs->job); 3021 3022 out: 3023 aio_context_release(aio_context); 3024 } 3025 3026 void qmp_block_commit(bool has_job_id, const char *job_id, const char *device, 3027 bool has_base, const char *base, 3028 bool has_top, const char *top, 3029 bool has_backing_file, const char *backing_file, 3030 bool has_speed, int64_t speed, 3031 bool has_filter_node_name, const char *filter_node_name, 3032 Error **errp) 3033 { 3034 BlockDriverState *bs; 3035 BlockDriverState *iter; 3036 BlockDriverState *base_bs, *top_bs; 3037 AioContext *aio_context; 3038 Error *local_err = NULL; 3039 /* This will be part of the QMP command, if/when the 3040 * BlockdevOnError change for blkmirror makes it in 3041 */ 3042 BlockdevOnError on_error = BLOCKDEV_ON_ERROR_REPORT; 3043 3044 if (!has_speed) { 3045 speed = 0; 3046 } 3047 if (!has_filter_node_name) { 3048 filter_node_name = NULL; 3049 } 3050 3051 /* Important Note: 3052 * libvirt relies on the DeviceNotFound error class in order to probe for 3053 * live commit feature versions; for this to work, we must make sure to 3054 * perform the device lookup before any generic errors that may occur in a 3055 * scenario in which all optional arguments are omitted. */ 3056 bs = qmp_get_root_bs(device, &local_err); 3057 if (!bs) { 3058 bs = bdrv_lookup_bs(device, device, NULL); 3059 if (!bs) { 3060 error_free(local_err); 3061 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, 3062 "Device '%s' not found", device); 3063 } else { 3064 error_propagate(errp, local_err); 3065 } 3066 return; 3067 } 3068 3069 aio_context = bdrv_get_aio_context(bs); 3070 aio_context_acquire(aio_context); 3071 3072 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, errp)) { 3073 goto out; 3074 } 3075 3076 /* default top_bs is the active layer */ 3077 top_bs = bs; 3078 3079 if (has_top && top) { 3080 if (strcmp(bs->filename, top) != 0) { 3081 top_bs = bdrv_find_backing_image(bs, top); 3082 } 3083 } 3084 3085 if (top_bs == NULL) { 3086 error_setg(errp, "Top image file %s not found", top ? top : "NULL"); 3087 goto out; 3088 } 3089 3090 assert(bdrv_get_aio_context(top_bs) == aio_context); 3091 3092 if (has_base && base) { 3093 base_bs = bdrv_find_backing_image(top_bs, base); 3094 } else { 3095 base_bs = bdrv_find_base(top_bs); 3096 } 3097 3098 if (base_bs == NULL) { 3099 error_setg(errp, QERR_BASE_NOT_FOUND, base ? base : "NULL"); 3100 goto out; 3101 } 3102 3103 assert(bdrv_get_aio_context(base_bs) == aio_context); 3104 3105 for (iter = top_bs; iter != backing_bs(base_bs); iter = backing_bs(iter)) { 3106 if (bdrv_op_is_blocked(iter, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3107 goto out; 3108 } 3109 } 3110 3111 /* Do not allow attempts to commit an image into itself */ 3112 if (top_bs == base_bs) { 3113 error_setg(errp, "cannot commit an image into itself"); 3114 goto out; 3115 } 3116 3117 if (top_bs == bs) { 3118 if (has_backing_file) { 3119 error_setg(errp, "'backing-file' specified," 3120 " but 'top' is the active layer"); 3121 goto out; 3122 } 3123 commit_active_start(has_job_id ? job_id : NULL, bs, base_bs, 3124 BLOCK_JOB_DEFAULT, speed, on_error, 3125 filter_node_name, NULL, NULL, false, &local_err); 3126 } else { 3127 BlockDriverState *overlay_bs = bdrv_find_overlay(bs, top_bs); 3128 if (bdrv_op_is_blocked(overlay_bs, BLOCK_OP_TYPE_COMMIT_TARGET, errp)) { 3129 goto out; 3130 } 3131 commit_start(has_job_id ? job_id : NULL, bs, base_bs, top_bs, speed, 3132 on_error, has_backing_file ? backing_file : NULL, 3133 filter_node_name, &local_err); 3134 } 3135 if (local_err != NULL) { 3136 error_propagate(errp, local_err); 3137 goto out; 3138 } 3139 3140 out: 3141 aio_context_release(aio_context); 3142 } 3143 3144 static BlockJob *do_drive_backup(DriveBackup *backup, BlockJobTxn *txn, 3145 Error **errp) 3146 { 3147 BlockDriverState *bs; 3148 BlockDriverState *target_bs; 3149 BlockDriverState *source = NULL; 3150 BlockJob *job = NULL; 3151 BdrvDirtyBitmap *bmap = NULL; 3152 AioContext *aio_context; 3153 QDict *options = NULL; 3154 Error *local_err = NULL; 3155 int flags; 3156 int64_t size; 3157 bool set_backing_hd = false; 3158 3159 if (!backup->has_speed) { 3160 backup->speed = 0; 3161 } 3162 if (!backup->has_on_source_error) { 3163 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3164 } 3165 if (!backup->has_on_target_error) { 3166 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3167 } 3168 if (!backup->has_mode) { 3169 backup->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 3170 } 3171 if (!backup->has_job_id) { 3172 backup->job_id = NULL; 3173 } 3174 if (!backup->has_compress) { 3175 backup->compress = false; 3176 } 3177 3178 bs = qmp_get_root_bs(backup->device, errp); 3179 if (!bs) { 3180 return NULL; 3181 } 3182 3183 aio_context = bdrv_get_aio_context(bs); 3184 aio_context_acquire(aio_context); 3185 3186 if (!backup->has_format) { 3187 backup->format = backup->mode == NEW_IMAGE_MODE_EXISTING ? 3188 NULL : (char*) bs->drv->format_name; 3189 } 3190 3191 /* Early check to avoid creating target */ 3192 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) { 3193 goto out; 3194 } 3195 3196 flags = bs->open_flags | BDRV_O_RDWR; 3197 3198 /* See if we have a backing HD we can use to create our new image 3199 * on top of. */ 3200 if (backup->sync == MIRROR_SYNC_MODE_TOP) { 3201 source = backing_bs(bs); 3202 if (!source) { 3203 backup->sync = MIRROR_SYNC_MODE_FULL; 3204 } 3205 } 3206 if (backup->sync == MIRROR_SYNC_MODE_NONE) { 3207 source = bs; 3208 flags |= BDRV_O_NO_BACKING; 3209 set_backing_hd = true; 3210 } 3211 3212 size = bdrv_getlength(bs); 3213 if (size < 0) { 3214 error_setg_errno(errp, -size, "bdrv_getlength failed"); 3215 goto out; 3216 } 3217 3218 if (backup->mode != NEW_IMAGE_MODE_EXISTING) { 3219 assert(backup->format); 3220 if (source) { 3221 bdrv_img_create(backup->target, backup->format, source->filename, 3222 source->drv->format_name, NULL, 3223 size, flags, false, &local_err); 3224 } else { 3225 bdrv_img_create(backup->target, backup->format, NULL, NULL, NULL, 3226 size, flags, false, &local_err); 3227 } 3228 } 3229 3230 if (local_err) { 3231 error_propagate(errp, local_err); 3232 goto out; 3233 } 3234 3235 if (backup->format) { 3236 if (!options) { 3237 options = qdict_new(); 3238 } 3239 qdict_put_str(options, "driver", backup->format); 3240 } 3241 3242 target_bs = bdrv_open(backup->target, NULL, options, flags, errp); 3243 if (!target_bs) { 3244 goto out; 3245 } 3246 3247 bdrv_set_aio_context(target_bs, aio_context); 3248 3249 if (set_backing_hd) { 3250 bdrv_set_backing_hd(target_bs, source, &local_err); 3251 if (local_err) { 3252 bdrv_unref(target_bs); 3253 goto out; 3254 } 3255 } 3256 3257 if (backup->has_bitmap) { 3258 bmap = bdrv_find_dirty_bitmap(bs, backup->bitmap); 3259 if (!bmap) { 3260 error_setg(errp, "Bitmap '%s' could not be found", backup->bitmap); 3261 bdrv_unref(target_bs); 3262 goto out; 3263 } 3264 } 3265 3266 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3267 backup->sync, bmap, backup->compress, 3268 backup->on_source_error, backup->on_target_error, 3269 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); 3270 bdrv_unref(target_bs); 3271 if (local_err != NULL) { 3272 error_propagate(errp, local_err); 3273 goto out; 3274 } 3275 3276 out: 3277 aio_context_release(aio_context); 3278 return job; 3279 } 3280 3281 void qmp_drive_backup(DriveBackup *arg, Error **errp) 3282 { 3283 3284 BlockJob *job; 3285 job = do_drive_backup(arg, NULL, errp); 3286 if (job) { 3287 block_job_start(job); 3288 } 3289 } 3290 3291 BlockDeviceInfoList *qmp_query_named_block_nodes(Error **errp) 3292 { 3293 return bdrv_named_nodes_list(errp); 3294 } 3295 3296 BlockJob *do_blockdev_backup(BlockdevBackup *backup, BlockJobTxn *txn, 3297 Error **errp) 3298 { 3299 BlockDriverState *bs; 3300 BlockDriverState *target_bs; 3301 Error *local_err = NULL; 3302 AioContext *aio_context; 3303 BlockJob *job = NULL; 3304 3305 if (!backup->has_speed) { 3306 backup->speed = 0; 3307 } 3308 if (!backup->has_on_source_error) { 3309 backup->on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3310 } 3311 if (!backup->has_on_target_error) { 3312 backup->on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3313 } 3314 if (!backup->has_job_id) { 3315 backup->job_id = NULL; 3316 } 3317 if (!backup->has_compress) { 3318 backup->compress = false; 3319 } 3320 3321 bs = qmp_get_root_bs(backup->device, errp); 3322 if (!bs) { 3323 return NULL; 3324 } 3325 3326 aio_context = bdrv_get_aio_context(bs); 3327 aio_context_acquire(aio_context); 3328 3329 target_bs = bdrv_lookup_bs(backup->target, backup->target, errp); 3330 if (!target_bs) { 3331 goto out; 3332 } 3333 3334 if (bdrv_get_aio_context(target_bs) != aio_context) { 3335 if (!bdrv_has_blk(target_bs)) { 3336 /* The target BDS is not attached, we can safely move it to another 3337 * AioContext. */ 3338 bdrv_set_aio_context(target_bs, aio_context); 3339 } else { 3340 error_setg(errp, "Target is attached to a different thread from " 3341 "source."); 3342 goto out; 3343 } 3344 } 3345 job = backup_job_create(backup->job_id, bs, target_bs, backup->speed, 3346 backup->sync, NULL, backup->compress, 3347 backup->on_source_error, backup->on_target_error, 3348 BLOCK_JOB_DEFAULT, NULL, NULL, txn, &local_err); 3349 if (local_err != NULL) { 3350 error_propagate(errp, local_err); 3351 } 3352 out: 3353 aio_context_release(aio_context); 3354 return job; 3355 } 3356 3357 void qmp_blockdev_backup(BlockdevBackup *arg, Error **errp) 3358 { 3359 BlockJob *job; 3360 job = do_blockdev_backup(arg, NULL, errp); 3361 if (job) { 3362 block_job_start(job); 3363 } 3364 } 3365 3366 /* Parameter check and block job starting for drive mirroring. 3367 * Caller should hold @device and @target's aio context (must be the same). 3368 **/ 3369 static void blockdev_mirror_common(const char *job_id, BlockDriverState *bs, 3370 BlockDriverState *target, 3371 bool has_replaces, const char *replaces, 3372 enum MirrorSyncMode sync, 3373 BlockMirrorBackingMode backing_mode, 3374 bool has_speed, int64_t speed, 3375 bool has_granularity, uint32_t granularity, 3376 bool has_buf_size, int64_t buf_size, 3377 bool has_on_source_error, 3378 BlockdevOnError on_source_error, 3379 bool has_on_target_error, 3380 BlockdevOnError on_target_error, 3381 bool has_unmap, bool unmap, 3382 bool has_filter_node_name, 3383 const char *filter_node_name, 3384 Error **errp) 3385 { 3386 3387 if (!has_speed) { 3388 speed = 0; 3389 } 3390 if (!has_on_source_error) { 3391 on_source_error = BLOCKDEV_ON_ERROR_REPORT; 3392 } 3393 if (!has_on_target_error) { 3394 on_target_error = BLOCKDEV_ON_ERROR_REPORT; 3395 } 3396 if (!has_granularity) { 3397 granularity = 0; 3398 } 3399 if (!has_buf_size) { 3400 buf_size = 0; 3401 } 3402 if (!has_unmap) { 3403 unmap = true; 3404 } 3405 if (!has_filter_node_name) { 3406 filter_node_name = NULL; 3407 } 3408 3409 if (granularity != 0 && (granularity < 512 || granularity > 1048576 * 64)) { 3410 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3411 "a value in range [512B, 64MB]"); 3412 return; 3413 } 3414 if (granularity & (granularity - 1)) { 3415 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "granularity", 3416 "power of 2"); 3417 return; 3418 } 3419 3420 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_MIRROR_SOURCE, errp)) { 3421 return; 3422 } 3423 if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_MIRROR_TARGET, errp)) { 3424 return; 3425 } 3426 3427 if (!bs->backing && sync == MIRROR_SYNC_MODE_TOP) { 3428 sync = MIRROR_SYNC_MODE_FULL; 3429 } 3430 3431 /* pass the node name to replace to mirror start since it's loose coupling 3432 * and will allow to check whether the node still exist at mirror completion 3433 */ 3434 mirror_start(job_id, bs, target, 3435 has_replaces ? replaces : NULL, 3436 speed, granularity, buf_size, sync, backing_mode, 3437 on_source_error, on_target_error, unmap, filter_node_name, 3438 errp); 3439 } 3440 3441 void qmp_drive_mirror(DriveMirror *arg, Error **errp) 3442 { 3443 BlockDriverState *bs; 3444 BlockDriverState *source, *target_bs; 3445 AioContext *aio_context; 3446 BlockMirrorBackingMode backing_mode; 3447 Error *local_err = NULL; 3448 QDict *options = NULL; 3449 int flags; 3450 int64_t size; 3451 const char *format = arg->format; 3452 3453 bs = qmp_get_root_bs(arg->device, errp); 3454 if (!bs) { 3455 return; 3456 } 3457 3458 aio_context = bdrv_get_aio_context(bs); 3459 aio_context_acquire(aio_context); 3460 3461 if (!arg->has_mode) { 3462 arg->mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS; 3463 } 3464 3465 if (!arg->has_format) { 3466 format = (arg->mode == NEW_IMAGE_MODE_EXISTING 3467 ? NULL : bs->drv->format_name); 3468 } 3469 3470 flags = bs->open_flags | BDRV_O_RDWR; 3471 source = backing_bs(bs); 3472 if (!source && arg->sync == MIRROR_SYNC_MODE_TOP) { 3473 arg->sync = MIRROR_SYNC_MODE_FULL; 3474 } 3475 if (arg->sync == MIRROR_SYNC_MODE_NONE) { 3476 source = bs; 3477 } 3478 3479 size = bdrv_getlength(bs); 3480 if (size < 0) { 3481 error_setg_errno(errp, -size, "bdrv_getlength failed"); 3482 goto out; 3483 } 3484 3485 if (arg->has_replaces) { 3486 BlockDriverState *to_replace_bs; 3487 AioContext *replace_aio_context; 3488 int64_t replace_size; 3489 3490 if (!arg->has_node_name) { 3491 error_setg(errp, "a node-name must be provided when replacing a" 3492 " named node of the graph"); 3493 goto out; 3494 } 3495 3496 to_replace_bs = check_to_replace_node(bs, arg->replaces, &local_err); 3497 3498 if (!to_replace_bs) { 3499 error_propagate(errp, local_err); 3500 goto out; 3501 } 3502 3503 replace_aio_context = bdrv_get_aio_context(to_replace_bs); 3504 aio_context_acquire(replace_aio_context); 3505 replace_size = bdrv_getlength(to_replace_bs); 3506 aio_context_release(replace_aio_context); 3507 3508 if (size != replace_size) { 3509 error_setg(errp, "cannot replace image with a mirror image of " 3510 "different size"); 3511 goto out; 3512 } 3513 } 3514 3515 if (arg->mode == NEW_IMAGE_MODE_ABSOLUTE_PATHS) { 3516 backing_mode = MIRROR_SOURCE_BACKING_CHAIN; 3517 } else { 3518 backing_mode = MIRROR_OPEN_BACKING_CHAIN; 3519 } 3520 3521 if ((arg->sync == MIRROR_SYNC_MODE_FULL || !source) 3522 && arg->mode != NEW_IMAGE_MODE_EXISTING) 3523 { 3524 /* create new image w/o backing file */ 3525 assert(format); 3526 bdrv_img_create(arg->target, format, 3527 NULL, NULL, NULL, size, flags, false, &local_err); 3528 } else { 3529 switch (arg->mode) { 3530 case NEW_IMAGE_MODE_EXISTING: 3531 break; 3532 case NEW_IMAGE_MODE_ABSOLUTE_PATHS: 3533 /* create new image with backing file */ 3534 bdrv_img_create(arg->target, format, 3535 source->filename, 3536 source->drv->format_name, 3537 NULL, size, flags, false, &local_err); 3538 break; 3539 default: 3540 abort(); 3541 } 3542 } 3543 3544 if (local_err) { 3545 error_propagate(errp, local_err); 3546 goto out; 3547 } 3548 3549 options = qdict_new(); 3550 if (arg->has_node_name) { 3551 qdict_put_str(options, "node-name", arg->node_name); 3552 } 3553 if (format) { 3554 qdict_put_str(options, "driver", format); 3555 } 3556 3557 /* Mirroring takes care of copy-on-write using the source's backing 3558 * file. 3559 */ 3560 target_bs = bdrv_open(arg->target, NULL, options, 3561 flags | BDRV_O_NO_BACKING, errp); 3562 if (!target_bs) { 3563 goto out; 3564 } 3565 3566 bdrv_set_aio_context(target_bs, aio_context); 3567 3568 blockdev_mirror_common(arg->has_job_id ? arg->job_id : NULL, bs, target_bs, 3569 arg->has_replaces, arg->replaces, arg->sync, 3570 backing_mode, arg->has_speed, arg->speed, 3571 arg->has_granularity, arg->granularity, 3572 arg->has_buf_size, arg->buf_size, 3573 arg->has_on_source_error, arg->on_source_error, 3574 arg->has_on_target_error, arg->on_target_error, 3575 arg->has_unmap, arg->unmap, 3576 false, NULL, 3577 &local_err); 3578 bdrv_unref(target_bs); 3579 error_propagate(errp, local_err); 3580 out: 3581 aio_context_release(aio_context); 3582 } 3583 3584 void qmp_blockdev_mirror(bool has_job_id, const char *job_id, 3585 const char *device, const char *target, 3586 bool has_replaces, const char *replaces, 3587 MirrorSyncMode sync, 3588 bool has_speed, int64_t speed, 3589 bool has_granularity, uint32_t granularity, 3590 bool has_buf_size, int64_t buf_size, 3591 bool has_on_source_error, 3592 BlockdevOnError on_source_error, 3593 bool has_on_target_error, 3594 BlockdevOnError on_target_error, 3595 bool has_filter_node_name, 3596 const char *filter_node_name, 3597 Error **errp) 3598 { 3599 BlockDriverState *bs; 3600 BlockDriverState *target_bs; 3601 AioContext *aio_context; 3602 BlockMirrorBackingMode backing_mode = MIRROR_LEAVE_BACKING_CHAIN; 3603 Error *local_err = NULL; 3604 3605 bs = qmp_get_root_bs(device, errp); 3606 if (!bs) { 3607 return; 3608 } 3609 3610 target_bs = bdrv_lookup_bs(target, target, errp); 3611 if (!target_bs) { 3612 return; 3613 } 3614 3615 aio_context = bdrv_get_aio_context(bs); 3616 aio_context_acquire(aio_context); 3617 3618 bdrv_set_aio_context(target_bs, aio_context); 3619 3620 blockdev_mirror_common(has_job_id ? job_id : NULL, bs, target_bs, 3621 has_replaces, replaces, sync, backing_mode, 3622 has_speed, speed, 3623 has_granularity, granularity, 3624 has_buf_size, buf_size, 3625 has_on_source_error, on_source_error, 3626 has_on_target_error, on_target_error, 3627 true, true, 3628 has_filter_node_name, filter_node_name, 3629 &local_err); 3630 error_propagate(errp, local_err); 3631 3632 aio_context_release(aio_context); 3633 } 3634 3635 /* Get a block job using its ID and acquire its AioContext */ 3636 static BlockJob *find_block_job(const char *id, AioContext **aio_context, 3637 Error **errp) 3638 { 3639 BlockJob *job; 3640 3641 assert(id != NULL); 3642 3643 *aio_context = NULL; 3644 3645 job = block_job_get(id); 3646 3647 if (!job) { 3648 error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE, 3649 "Block job '%s' not found", id); 3650 return NULL; 3651 } 3652 3653 *aio_context = blk_get_aio_context(job->blk); 3654 aio_context_acquire(*aio_context); 3655 3656 return job; 3657 } 3658 3659 void qmp_block_job_set_speed(const char *device, int64_t speed, Error **errp) 3660 { 3661 AioContext *aio_context; 3662 BlockJob *job = find_block_job(device, &aio_context, errp); 3663 3664 if (!job) { 3665 return; 3666 } 3667 3668 block_job_set_speed(job, speed, errp); 3669 aio_context_release(aio_context); 3670 } 3671 3672 void qmp_block_job_cancel(const char *device, 3673 bool has_force, bool force, Error **errp) 3674 { 3675 AioContext *aio_context; 3676 BlockJob *job = find_block_job(device, &aio_context, errp); 3677 3678 if (!job) { 3679 return; 3680 } 3681 3682 if (!has_force) { 3683 force = false; 3684 } 3685 3686 if (block_job_user_paused(job) && !force) { 3687 error_setg(errp, "The block job for device '%s' is currently paused", 3688 device); 3689 goto out; 3690 } 3691 3692 trace_qmp_block_job_cancel(job); 3693 block_job_cancel(job); 3694 out: 3695 aio_context_release(aio_context); 3696 } 3697 3698 void qmp_block_job_pause(const char *device, Error **errp) 3699 { 3700 AioContext *aio_context; 3701 BlockJob *job = find_block_job(device, &aio_context, errp); 3702 3703 if (!job || block_job_user_paused(job)) { 3704 return; 3705 } 3706 3707 trace_qmp_block_job_pause(job); 3708 block_job_user_pause(job); 3709 aio_context_release(aio_context); 3710 } 3711 3712 void qmp_block_job_resume(const char *device, Error **errp) 3713 { 3714 AioContext *aio_context; 3715 BlockJob *job = find_block_job(device, &aio_context, errp); 3716 3717 if (!job || !block_job_user_paused(job)) { 3718 return; 3719 } 3720 3721 trace_qmp_block_job_resume(job); 3722 block_job_user_resume(job); 3723 aio_context_release(aio_context); 3724 } 3725 3726 void qmp_block_job_complete(const char *device, Error **errp) 3727 { 3728 AioContext *aio_context; 3729 BlockJob *job = find_block_job(device, &aio_context, errp); 3730 3731 if (!job) { 3732 return; 3733 } 3734 3735 trace_qmp_block_job_complete(job); 3736 block_job_complete(job, errp); 3737 aio_context_release(aio_context); 3738 } 3739 3740 void qmp_change_backing_file(const char *device, 3741 const char *image_node_name, 3742 const char *backing_file, 3743 Error **errp) 3744 { 3745 BlockDriverState *bs = NULL; 3746 AioContext *aio_context; 3747 BlockDriverState *image_bs = NULL; 3748 Error *local_err = NULL; 3749 bool ro; 3750 int open_flags; 3751 int ret; 3752 3753 bs = qmp_get_root_bs(device, errp); 3754 if (!bs) { 3755 return; 3756 } 3757 3758 aio_context = bdrv_get_aio_context(bs); 3759 aio_context_acquire(aio_context); 3760 3761 image_bs = bdrv_lookup_bs(NULL, image_node_name, &local_err); 3762 if (local_err) { 3763 error_propagate(errp, local_err); 3764 goto out; 3765 } 3766 3767 if (!image_bs) { 3768 error_setg(errp, "image file not found"); 3769 goto out; 3770 } 3771 3772 if (bdrv_find_base(image_bs) == image_bs) { 3773 error_setg(errp, "not allowing backing file change on an image " 3774 "without a backing file"); 3775 goto out; 3776 } 3777 3778 /* even though we are not necessarily operating on bs, we need it to 3779 * determine if block ops are currently prohibited on the chain */ 3780 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_CHANGE, errp)) { 3781 goto out; 3782 } 3783 3784 /* final sanity check */ 3785 if (!bdrv_chain_contains(bs, image_bs)) { 3786 error_setg(errp, "'%s' and image file are not in the same chain", 3787 device); 3788 goto out; 3789 } 3790 3791 /* if not r/w, reopen to make r/w */ 3792 open_flags = image_bs->open_flags; 3793 ro = bdrv_is_read_only(image_bs); 3794 3795 if (ro) { 3796 bdrv_reopen(image_bs, open_flags | BDRV_O_RDWR, &local_err); 3797 if (local_err) { 3798 error_propagate(errp, local_err); 3799 goto out; 3800 } 3801 } 3802 3803 ret = bdrv_change_backing_file(image_bs, backing_file, 3804 image_bs->drv ? image_bs->drv->format_name : ""); 3805 3806 if (ret < 0) { 3807 error_setg_errno(errp, -ret, "Could not change backing file to '%s'", 3808 backing_file); 3809 /* don't exit here, so we can try to restore open flags if 3810 * appropriate */ 3811 } 3812 3813 if (ro) { 3814 bdrv_reopen(image_bs, open_flags, &local_err); 3815 error_propagate(errp, local_err); 3816 } 3817 3818 out: 3819 aio_context_release(aio_context); 3820 } 3821 3822 void hmp_drive_add_node(Monitor *mon, const char *optstr) 3823 { 3824 QemuOpts *opts; 3825 QDict *qdict; 3826 Error *local_err = NULL; 3827 3828 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 3829 if (!opts) { 3830 return; 3831 } 3832 3833 qdict = qemu_opts_to_qdict(opts, NULL); 3834 3835 if (!qdict_get_try_str(qdict, "node-name")) { 3836 QDECREF(qdict); 3837 error_report("'node-name' needs to be specified"); 3838 goto out; 3839 } 3840 3841 BlockDriverState *bs = bds_tree_init(qdict, &local_err); 3842 if (!bs) { 3843 error_report_err(local_err); 3844 goto out; 3845 } 3846 3847 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3848 3849 out: 3850 qemu_opts_del(opts); 3851 } 3852 3853 void qmp_blockdev_add(BlockdevOptions *options, Error **errp) 3854 { 3855 BlockDriverState *bs; 3856 QObject *obj; 3857 Visitor *v = qobject_output_visitor_new(&obj); 3858 QDict *qdict; 3859 Error *local_err = NULL; 3860 3861 visit_type_BlockdevOptions(v, NULL, &options, &local_err); 3862 if (local_err) { 3863 error_propagate(errp, local_err); 3864 goto fail; 3865 } 3866 3867 visit_complete(v, &obj); 3868 qdict = qobject_to_qdict(obj); 3869 3870 qdict_flatten(qdict); 3871 3872 if (!qdict_get_try_str(qdict, "node-name")) { 3873 error_setg(errp, "'node-name' must be specified for the root node"); 3874 goto fail; 3875 } 3876 3877 bs = bds_tree_init(qdict, errp); 3878 if (!bs) { 3879 goto fail; 3880 } 3881 3882 QTAILQ_INSERT_TAIL(&monitor_bdrv_states, bs, monitor_list); 3883 3884 if (bs && bdrv_key_required(bs)) { 3885 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3886 bdrv_unref(bs); 3887 error_setg(errp, "blockdev-add doesn't support encrypted devices"); 3888 goto fail; 3889 } 3890 3891 fail: 3892 visit_free(v); 3893 } 3894 3895 void qmp_blockdev_del(const char *node_name, Error **errp) 3896 { 3897 AioContext *aio_context; 3898 BlockDriverState *bs; 3899 3900 bs = bdrv_find_node(node_name); 3901 if (!bs) { 3902 error_setg(errp, "Cannot find node %s", node_name); 3903 return; 3904 } 3905 if (bdrv_has_blk(bs)) { 3906 error_setg(errp, "Node %s is in use", node_name); 3907 return; 3908 } 3909 aio_context = bdrv_get_aio_context(bs); 3910 aio_context_acquire(aio_context); 3911 3912 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, errp)) { 3913 goto out; 3914 } 3915 3916 if (!bs->monitor_list.tqe_prev) { 3917 error_setg(errp, "Node %s is not owned by the monitor", 3918 bs->node_name); 3919 goto out; 3920 } 3921 3922 if (bs->refcnt > 1) { 3923 error_setg(errp, "Block device %s is in use", 3924 bdrv_get_device_or_node_name(bs)); 3925 goto out; 3926 } 3927 3928 QTAILQ_REMOVE(&monitor_bdrv_states, bs, monitor_list); 3929 bdrv_unref(bs); 3930 3931 out: 3932 aio_context_release(aio_context); 3933 } 3934 3935 static BdrvChild *bdrv_find_child(BlockDriverState *parent_bs, 3936 const char *child_name) 3937 { 3938 BdrvChild *child; 3939 3940 QLIST_FOREACH(child, &parent_bs->children, next) { 3941 if (strcmp(child->name, child_name) == 0) { 3942 return child; 3943 } 3944 } 3945 3946 return NULL; 3947 } 3948 3949 void qmp_x_blockdev_change(const char *parent, bool has_child, 3950 const char *child, bool has_node, 3951 const char *node, Error **errp) 3952 { 3953 BlockDriverState *parent_bs, *new_bs = NULL; 3954 BdrvChild *p_child; 3955 3956 parent_bs = bdrv_lookup_bs(parent, parent, errp); 3957 if (!parent_bs) { 3958 return; 3959 } 3960 3961 if (has_child == has_node) { 3962 if (has_child) { 3963 error_setg(errp, "The parameters child and node are in conflict"); 3964 } else { 3965 error_setg(errp, "Either child or node must be specified"); 3966 } 3967 return; 3968 } 3969 3970 if (has_child) { 3971 p_child = bdrv_find_child(parent_bs, child); 3972 if (!p_child) { 3973 error_setg(errp, "Node '%s' does not have child '%s'", 3974 parent, child); 3975 return; 3976 } 3977 bdrv_del_child(parent_bs, p_child, errp); 3978 } 3979 3980 if (has_node) { 3981 new_bs = bdrv_find_node(node); 3982 if (!new_bs) { 3983 error_setg(errp, "Node '%s' not found", node); 3984 return; 3985 } 3986 bdrv_add_child(parent_bs, new_bs, errp); 3987 } 3988 } 3989 3990 BlockJobInfoList *qmp_query_block_jobs(Error **errp) 3991 { 3992 BlockJobInfoList *head = NULL, **p_next = &head; 3993 BlockJob *job; 3994 3995 for (job = block_job_next(NULL); job; job = block_job_next(job)) { 3996 BlockJobInfoList *elem; 3997 AioContext *aio_context; 3998 3999 if (block_job_is_internal(job)) { 4000 continue; 4001 } 4002 elem = g_new0(BlockJobInfoList, 1); 4003 aio_context = blk_get_aio_context(job->blk); 4004 aio_context_acquire(aio_context); 4005 elem->value = block_job_query(job, errp); 4006 aio_context_release(aio_context); 4007 if (!elem->value) { 4008 g_free(elem); 4009 qapi_free_BlockJobInfoList(head); 4010 return NULL; 4011 } 4012 *p_next = elem; 4013 p_next = &elem->next; 4014 } 4015 4016 return head; 4017 } 4018 4019 QemuOptsList qemu_common_drive_opts = { 4020 .name = "drive", 4021 .head = QTAILQ_HEAD_INITIALIZER(qemu_common_drive_opts.head), 4022 .desc = { 4023 { 4024 .name = "snapshot", 4025 .type = QEMU_OPT_BOOL, 4026 .help = "enable/disable snapshot mode", 4027 },{ 4028 .name = "aio", 4029 .type = QEMU_OPT_STRING, 4030 .help = "host AIO implementation (threads, native)", 4031 },{ 4032 .name = BDRV_OPT_CACHE_WB, 4033 .type = QEMU_OPT_BOOL, 4034 .help = "Enable writeback mode", 4035 },{ 4036 .name = "format", 4037 .type = QEMU_OPT_STRING, 4038 .help = "disk format (raw, qcow2, ...)", 4039 },{ 4040 .name = "rerror", 4041 .type = QEMU_OPT_STRING, 4042 .help = "read error action", 4043 },{ 4044 .name = "werror", 4045 .type = QEMU_OPT_STRING, 4046 .help = "write error action", 4047 },{ 4048 .name = BDRV_OPT_READ_ONLY, 4049 .type = QEMU_OPT_BOOL, 4050 .help = "open drive file as read-only", 4051 }, 4052 4053 THROTTLE_OPTS, 4054 4055 { 4056 .name = "throttling.group", 4057 .type = QEMU_OPT_STRING, 4058 .help = "name of the block throttling group", 4059 },{ 4060 .name = "copy-on-read", 4061 .type = QEMU_OPT_BOOL, 4062 .help = "copy read data from backing file into image file", 4063 },{ 4064 .name = "detect-zeroes", 4065 .type = QEMU_OPT_STRING, 4066 .help = "try to optimize zero writes (off, on, unmap)", 4067 },{ 4068 .name = "stats-account-invalid", 4069 .type = QEMU_OPT_BOOL, 4070 .help = "whether to account for invalid I/O operations " 4071 "in the statistics", 4072 },{ 4073 .name = "stats-account-failed", 4074 .type = QEMU_OPT_BOOL, 4075 .help = "whether to account for failed I/O operations " 4076 "in the statistics", 4077 }, 4078 { /* end of list */ } 4079 }, 4080 }; 4081 4082 QemuOptsList qemu_drive_opts = { 4083 .name = "drive", 4084 .head = QTAILQ_HEAD_INITIALIZER(qemu_drive_opts.head), 4085 .desc = { 4086 /* 4087 * no elements => accept any params 4088 * validation will happen later 4089 */ 4090 { /* end of list */ } 4091 }, 4092 }; 4093