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