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