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