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