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