1 /* 2 * Blockdev HMP commands 3 * 4 * Authors: 5 * Anthony Liguori <aliguori@us.ibm.com> 6 * 7 * Copyright (c) 2003-2008 Fabrice Bellard 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. 10 * See the COPYING file in the top-level directory. 11 * Contributions after 2012-01-13 are licensed under the terms of the 12 * GNU GPL, version 2 or (at your option) any later version. 13 * 14 * This file incorporates work covered by the following copyright and 15 * permission notice: 16 * 17 * Copyright (c) 2003-2008 Fabrice Bellard 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining a copy 20 * of this software and associated documentation files (the "Software"), to deal 21 * in the Software without restriction, including without limitation the rights 22 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 * copies of the Software, and to permit persons to whom the Software is 24 * furnished to do so, subject to the following conditions: 25 * 26 * The above copyright notice and this permission notice shall be included in 27 * all copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 32 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 35 * THE SOFTWARE. 36 */ 37 38 #include "qemu/osdep.h" 39 #include "hw/boards.h" 40 #include "system/block-backend.h" 41 #include "system/blockdev.h" 42 #include "qapi/qapi-commands-block.h" 43 #include "qapi/qapi-commands-block-export.h" 44 #include "qobject/qdict.h" 45 #include "qapi/error.h" 46 #include "qapi/qmp/qerror.h" 47 #include "qemu/config-file.h" 48 #include "qemu/option.h" 49 #include "qemu/sockets.h" 50 #include "qemu/cutils.h" 51 #include "qemu/error-report.h" 52 #include "system/system.h" 53 #include "monitor/monitor.h" 54 #include "monitor/hmp.h" 55 #include "block/nbd.h" 56 #include "block/qapi.h" 57 #include "block/block_int.h" 58 #include "block/block-hmp-cmds.h" 59 #include "qemu-io.h" 60 61 static void hmp_drive_add_node(Monitor *mon, const char *optstr) 62 { 63 QemuOpts *opts; 64 QDict *qdict; 65 Error *local_err = NULL; 66 67 opts = qemu_opts_parse_noisily(&qemu_drive_opts, optstr, false); 68 if (!opts) { 69 return; 70 } 71 72 qdict = qemu_opts_to_qdict(opts, NULL); 73 74 if (!qdict_get_try_str(qdict, "node-name")) { 75 qobject_unref(qdict); 76 error_report("'node-name' needs to be specified"); 77 goto out; 78 } 79 80 BlockDriverState *bs = bds_tree_init(qdict, &local_err); 81 if (!bs) { 82 error_report_err(local_err); 83 goto out; 84 } 85 86 bdrv_set_monitor_owned(bs); 87 out: 88 qemu_opts_del(opts); 89 } 90 91 void hmp_drive_add(Monitor *mon, const QDict *qdict) 92 { 93 Error *err = NULL; 94 DriveInfo *dinfo; 95 QemuOpts *opts; 96 MachineClass *mc; 97 const char *optstr = qdict_get_str(qdict, "opts"); 98 bool node = qdict_get_try_bool(qdict, "node", false); 99 100 if (node) { 101 hmp_drive_add_node(mon, optstr); 102 return; 103 } 104 105 opts = qemu_opts_parse_noisily(qemu_find_opts("drive"), optstr, false); 106 if (!opts) 107 return; 108 109 mc = MACHINE_GET_CLASS(current_machine); 110 dinfo = drive_new(opts, mc->block_default_type, &err); 111 if (err) { 112 error_report_err(err); 113 qemu_opts_del(opts); 114 goto err; 115 } 116 117 if (!dinfo) { 118 return; 119 } 120 121 switch (dinfo->type) { 122 case IF_NONE: 123 monitor_printf(mon, "OK\n"); 124 break; 125 default: 126 monitor_printf(mon, "Can't hot-add drive to type %d\n", dinfo->type); 127 goto err; 128 } 129 return; 130 131 err: 132 if (dinfo) { 133 BlockBackend *blk = blk_by_legacy_dinfo(dinfo); 134 monitor_remove_blk(blk); 135 blk_unref(blk); 136 } 137 } 138 139 void hmp_drive_del(Monitor *mon, const QDict *qdict) 140 { 141 const char *id = qdict_get_str(qdict, "id"); 142 BlockBackend *blk; 143 BlockDriverState *bs; 144 Error *local_err = NULL; 145 146 GLOBAL_STATE_CODE(); 147 bdrv_graph_rdlock_main_loop(); 148 149 bs = bdrv_find_node(id); 150 if (bs) { 151 qmp_blockdev_del(id, &local_err); 152 if (local_err) { 153 error_report_err(local_err); 154 } 155 goto unlock; 156 } 157 158 blk = blk_by_name(id); 159 if (!blk) { 160 error_report("Device '%s' not found", id); 161 goto unlock; 162 } 163 164 if (!blk_legacy_dinfo(blk)) { 165 error_report("Deleting device added with blockdev-add" 166 " is not supported"); 167 goto unlock; 168 } 169 170 bs = blk_bs(blk); 171 if (bs) { 172 if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_DRIVE_DEL, &local_err)) { 173 error_report_err(local_err); 174 goto unlock; 175 } 176 177 bdrv_graph_rdunlock_main_loop(); 178 blk_remove_bs(blk); 179 bdrv_graph_rdlock_main_loop(); 180 } 181 182 /* Make the BlockBackend and the attached BlockDriverState anonymous */ 183 monitor_remove_blk(blk); 184 185 /* 186 * If this BlockBackend has a device attached to it, its refcount will be 187 * decremented when the device is removed; otherwise we have to do so here. 188 */ 189 if (blk_get_attached_dev(blk)) { 190 /* Further I/O must not pause the guest */ 191 blk_set_on_error(blk, BLOCKDEV_ON_ERROR_REPORT, 192 BLOCKDEV_ON_ERROR_REPORT); 193 } else { 194 blk_unref(blk); 195 } 196 197 unlock: 198 bdrv_graph_rdunlock_main_loop(); 199 } 200 201 void hmp_commit(Monitor *mon, const QDict *qdict) 202 { 203 const char *device = qdict_get_str(qdict, "device"); 204 BlockBackend *blk; 205 int ret; 206 207 GLOBAL_STATE_CODE(); 208 GRAPH_RDLOCK_GUARD_MAINLOOP(); 209 210 if (!strcmp(device, "all")) { 211 ret = blk_commit_all(); 212 } else { 213 BlockDriverState *bs; 214 215 blk = blk_by_name(device); 216 if (!blk) { 217 error_report("Device '%s' not found", device); 218 return; 219 } 220 221 bs = bdrv_skip_implicit_filters(blk_bs(blk)); 222 223 if (!blk_is_available(blk)) { 224 error_report("Device '%s' has no medium", device); 225 return; 226 } 227 228 ret = bdrv_commit(bs); 229 } 230 if (ret < 0) { 231 error_report("'commit' error for '%s': %s", device, strerror(-ret)); 232 } 233 } 234 235 void hmp_drive_mirror(Monitor *mon, const QDict *qdict) 236 { 237 const char *filename = qdict_get_str(qdict, "target"); 238 const char *format = qdict_get_try_str(qdict, "format"); 239 bool reuse = qdict_get_try_bool(qdict, "reuse", false); 240 bool full = qdict_get_try_bool(qdict, "full", false); 241 Error *err = NULL; 242 DriveMirror mirror = { 243 .device = (char *)qdict_get_str(qdict, "device"), 244 .target = (char *)filename, 245 .format = (char *)format, 246 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP, 247 .has_mode = true, 248 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS, 249 .unmap = true, 250 }; 251 252 if (!filename) { 253 error_setg(&err, QERR_MISSING_PARAMETER, "target"); 254 goto end; 255 } 256 qmp_drive_mirror(&mirror, &err); 257 end: 258 hmp_handle_error(mon, err); 259 } 260 261 void hmp_drive_backup(Monitor *mon, const QDict *qdict) 262 { 263 const char *device = qdict_get_str(qdict, "device"); 264 const char *filename = qdict_get_str(qdict, "target"); 265 const char *format = qdict_get_try_str(qdict, "format"); 266 bool reuse = qdict_get_try_bool(qdict, "reuse", false); 267 bool full = qdict_get_try_bool(qdict, "full", false); 268 bool compress = qdict_get_try_bool(qdict, "compress", false); 269 Error *err = NULL; 270 DriveBackup backup = { 271 .device = (char *)device, 272 .target = (char *)filename, 273 .format = (char *)format, 274 .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP, 275 .has_mode = true, 276 .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS, 277 .has_compress = !!compress, 278 .compress = compress, 279 }; 280 281 if (!filename) { 282 error_setg(&err, QERR_MISSING_PARAMETER, "target"); 283 goto end; 284 } 285 286 qmp_drive_backup(&backup, &err); 287 end: 288 hmp_handle_error(mon, err); 289 } 290 291 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict) 292 { 293 Error *error = NULL; 294 const char *device = qdict_get_str(qdict, "device"); 295 int64_t value = qdict_get_int(qdict, "speed"); 296 297 qmp_block_job_set_speed(device, value, &error); 298 299 hmp_handle_error(mon, error); 300 } 301 302 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict) 303 { 304 Error *error = NULL; 305 const char *device = qdict_get_str(qdict, "device"); 306 bool force = qdict_get_try_bool(qdict, "force", false); 307 308 qmp_block_job_cancel(device, true, force, &error); 309 310 hmp_handle_error(mon, error); 311 } 312 313 void hmp_block_job_pause(Monitor *mon, const QDict *qdict) 314 { 315 Error *error = NULL; 316 const char *device = qdict_get_str(qdict, "device"); 317 318 qmp_block_job_pause(device, &error); 319 320 hmp_handle_error(mon, error); 321 } 322 323 void hmp_block_job_resume(Monitor *mon, const QDict *qdict) 324 { 325 Error *error = NULL; 326 const char *device = qdict_get_str(qdict, "device"); 327 328 qmp_block_job_resume(device, &error); 329 330 hmp_handle_error(mon, error); 331 } 332 333 void hmp_block_job_complete(Monitor *mon, const QDict *qdict) 334 { 335 Error *error = NULL; 336 const char *device = qdict_get_str(qdict, "device"); 337 338 qmp_block_job_complete(device, &error); 339 340 hmp_handle_error(mon, error); 341 } 342 343 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict) 344 { 345 const char *device = qdict_get_str(qdict, "device"); 346 const char *filename = qdict_get_try_str(qdict, "snapshot-file"); 347 const char *format = qdict_get_try_str(qdict, "format"); 348 bool reuse = qdict_get_try_bool(qdict, "reuse", false); 349 enum NewImageMode mode; 350 Error *err = NULL; 351 352 if (!filename) { 353 /* 354 * In the future, if 'snapshot-file' is not specified, the snapshot 355 * will be taken internally. Today it's actually required. 356 */ 357 error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file"); 358 goto end; 359 } 360 361 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS; 362 qmp_blockdev_snapshot_sync(device, NULL, filename, NULL, format, 363 true, mode, &err); 364 end: 365 hmp_handle_error(mon, err); 366 } 367 368 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict) 369 { 370 const char *device = qdict_get_str(qdict, "device"); 371 const char *name = qdict_get_str(qdict, "name"); 372 Error *err = NULL; 373 374 qmp_blockdev_snapshot_internal_sync(device, name, &err); 375 hmp_handle_error(mon, err); 376 } 377 378 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict) 379 { 380 const char *device = qdict_get_str(qdict, "device"); 381 const char *name = qdict_get_str(qdict, "name"); 382 const char *id = qdict_get_try_str(qdict, "id"); 383 Error *err = NULL; 384 385 qmp_blockdev_snapshot_delete_internal_sync(device, id, name, &err); 386 hmp_handle_error(mon, err); 387 } 388 389 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict) 390 { 391 const char *uri = qdict_get_str(qdict, "uri"); 392 bool writable = qdict_get_try_bool(qdict, "writable", false); 393 bool all = qdict_get_try_bool(qdict, "all", false); 394 Error *local_err = NULL; 395 BlockInfoList *block_list, *info; 396 SocketAddress *addr; 397 NbdServerAddOptions export; 398 399 if (writable && !all) { 400 error_setg(&local_err, "-w only valid together with -a"); 401 goto exit; 402 } 403 404 /* First check if the address is valid and start the server. */ 405 addr = socket_parse(uri, &local_err); 406 if (local_err != NULL) { 407 goto exit; 408 } 409 410 nbd_server_start(addr, NBD_DEFAULT_HANDSHAKE_MAX_SECS, NULL, NULL, 411 NBD_DEFAULT_MAX_CONNECTIONS, &local_err); 412 qapi_free_SocketAddress(addr); 413 if (local_err != NULL) { 414 goto exit; 415 } 416 417 if (!all) { 418 return; 419 } 420 421 /* Then try adding all block devices. If one fails, close all and 422 * exit. 423 */ 424 block_list = qmp_query_block(NULL); 425 426 for (info = block_list; info; info = info->next) { 427 if (!info->value->inserted) { 428 continue; 429 } 430 431 export = (NbdServerAddOptions) { 432 .device = info->value->device, 433 .has_writable = true, 434 .writable = writable, 435 }; 436 437 qmp_nbd_server_add(&export, &local_err); 438 439 if (local_err != NULL) { 440 qmp_nbd_server_stop(NULL); 441 break; 442 } 443 } 444 445 qapi_free_BlockInfoList(block_list); 446 447 exit: 448 hmp_handle_error(mon, local_err); 449 } 450 451 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict) 452 { 453 const char *device = qdict_get_str(qdict, "device"); 454 const char *name = qdict_get_try_str(qdict, "name"); 455 bool writable = qdict_get_try_bool(qdict, "writable", false); 456 Error *local_err = NULL; 457 458 NbdServerAddOptions export = { 459 .device = (char *) device, 460 .name = (char *) name, 461 .has_writable = true, 462 .writable = writable, 463 }; 464 465 qmp_nbd_server_add(&export, &local_err); 466 hmp_handle_error(mon, local_err); 467 } 468 469 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict) 470 { 471 const char *name = qdict_get_str(qdict, "name"); 472 bool force = qdict_get_try_bool(qdict, "force", false); 473 Error *err = NULL; 474 475 /* Rely on BLOCK_EXPORT_REMOVE_MODE_SAFE being the default */ 476 qmp_nbd_server_remove(name, force, BLOCK_EXPORT_REMOVE_MODE_HARD, &err); 477 hmp_handle_error(mon, err); 478 } 479 480 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict) 481 { 482 Error *err = NULL; 483 484 qmp_nbd_server_stop(&err); 485 hmp_handle_error(mon, err); 486 } 487 488 void coroutine_fn hmp_block_resize(Monitor *mon, const QDict *qdict) 489 { 490 const char *device = qdict_get_str(qdict, "device"); 491 int64_t size = qdict_get_int(qdict, "size"); 492 Error *err = NULL; 493 494 qmp_block_resize(device, NULL, size, &err); 495 hmp_handle_error(mon, err); 496 } 497 498 void hmp_block_stream(Monitor *mon, const QDict *qdict) 499 { 500 Error *error = NULL; 501 const char *device = qdict_get_str(qdict, "device"); 502 const char *base = qdict_get_try_str(qdict, "base"); 503 int64_t speed = qdict_get_try_int(qdict, "speed", 0); 504 505 qmp_block_stream(device, device, base, NULL, NULL, false, false, NULL, 506 qdict_haskey(qdict, "speed"), speed, 507 true, BLOCKDEV_ON_ERROR_REPORT, NULL, 508 false, false, false, false, &error); 509 510 hmp_handle_error(mon, error); 511 } 512 513 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict) 514 { 515 Error *err = NULL; 516 char *device = (char *) qdict_get_str(qdict, "device"); 517 BlockIOThrottle throttle = { 518 .bps = qdict_get_int(qdict, "bps"), 519 .bps_rd = qdict_get_int(qdict, "bps_rd"), 520 .bps_wr = qdict_get_int(qdict, "bps_wr"), 521 .iops = qdict_get_int(qdict, "iops"), 522 .iops_rd = qdict_get_int(qdict, "iops_rd"), 523 .iops_wr = qdict_get_int(qdict, "iops_wr"), 524 }; 525 526 /* 527 * qmp_block_set_io_throttle has separate parameters for the 528 * (deprecated) block device name and the qdev ID but the HMP 529 * version has only one, so we must decide which one to pass. 530 */ 531 if (blk_by_name(device)) { 532 throttle.device = device; 533 } else { 534 throttle.id = device; 535 } 536 537 qmp_block_set_io_throttle(&throttle, &err); 538 hmp_handle_error(mon, err); 539 } 540 541 void hmp_eject(Monitor *mon, const QDict *qdict) 542 { 543 bool force = qdict_get_try_bool(qdict, "force", false); 544 const char *device = qdict_get_str(qdict, "device"); 545 Error *err = NULL; 546 547 qmp_eject(device, NULL, true, force, &err); 548 hmp_handle_error(mon, err); 549 } 550 551 void hmp_qemu_io(Monitor *mon, const QDict *qdict) 552 { 553 BlockBackend *blk = NULL; 554 BlockDriverState *bs = NULL; 555 BlockBackend *local_blk = NULL; 556 bool qdev = qdict_get_try_bool(qdict, "qdev", false); 557 const char *device = qdict_get_str(qdict, "device"); 558 const char *command = qdict_get_str(qdict, "command"); 559 Error *err = NULL; 560 int ret; 561 562 if (qdev) { 563 blk = blk_by_qdev_id(device, &err); 564 if (!blk) { 565 goto fail; 566 } 567 } else { 568 blk = blk_by_name(device); 569 if (!blk) { 570 bs = bdrv_lookup_bs(NULL, device, &err); 571 if (!bs) { 572 goto fail; 573 } 574 } 575 } 576 577 if (bs) { 578 blk = local_blk = blk_new(bdrv_get_aio_context(bs), 0, BLK_PERM_ALL); 579 ret = blk_insert_bs(blk, bs, &err); 580 if (ret < 0) { 581 goto fail; 582 } 583 } 584 585 /* 586 * Notably absent: Proper permission management. This is sad, but it seems 587 * almost impossible to achieve without changing the semantics and thereby 588 * limiting the use cases of the qemu-io HMP command. 589 * 590 * In an ideal world we would unconditionally create a new BlockBackend for 591 * qemuio_command(), but we have commands like 'reopen' and want them to 592 * take effect on the exact BlockBackend whose name the user passed instead 593 * of just on a temporary copy of it. 594 * 595 * Another problem is that deleting the temporary BlockBackend involves 596 * draining all requests on it first, but some qemu-iotests cases want to 597 * issue multiple aio_read/write requests and expect them to complete in 598 * the background while the monitor has already returned. 599 * 600 * This is also what prevents us from saving the original permissions and 601 * restoring them later: We can't revoke permissions until all requests 602 * have completed, and we don't know when that is nor can we really let 603 * anything else run before we have revoken them to avoid race conditions. 604 * 605 * What happens now is that command() in qemu-io-cmds.c can extend the 606 * permissions if necessary for the qemu-io command. And they simply stay 607 * extended, possibly resulting in a read-only guest device keeping write 608 * permissions. Ugly, but it appears to be the lesser evil. 609 */ 610 qemuio_command(blk, command); 611 612 fail: 613 blk_unref(local_blk); 614 hmp_handle_error(mon, err); 615 } 616 617 static void print_block_info(Monitor *mon, BlockInfo *info, 618 BlockDeviceInfo *inserted, bool verbose) 619 { 620 ImageInfo *image_info; 621 622 assert(!info || !info->inserted || info->inserted == inserted); 623 624 if (info && *info->device) { 625 monitor_puts(mon, info->device); 626 if (inserted && inserted->node_name) { 627 monitor_printf(mon, " (%s)", inserted->node_name); 628 } 629 } else { 630 assert(info || inserted); 631 monitor_puts(mon, 632 inserted && inserted->node_name ? inserted->node_name 633 : info && info->qdev ? info->qdev 634 : "<anonymous>"); 635 } 636 637 if (inserted) { 638 monitor_printf(mon, ": %s (%s%s%s%s)\n", 639 inserted->file, 640 inserted->drv, 641 inserted->ro ? ", read-only" : "", 642 inserted->encrypted ? ", encrypted" : "", 643 inserted->active ? "" : ", inactive"); 644 } else { 645 monitor_printf(mon, ": [not inserted]\n"); 646 } 647 648 if (info) { 649 if (info->qdev) { 650 monitor_printf(mon, " Attached to: %s\n", info->qdev); 651 } 652 if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) { 653 monitor_printf(mon, " I/O status: %s\n", 654 BlockDeviceIoStatus_str(info->io_status)); 655 } 656 657 if (info->removable) { 658 monitor_printf(mon, " Removable device: %slocked, tray %s\n", 659 info->locked ? "" : "not ", 660 info->tray_open ? "open" : "closed"); 661 } 662 } 663 664 665 if (!inserted) { 666 return; 667 } 668 669 monitor_printf(mon, " Cache mode: %s%s%s\n", 670 inserted->cache->writeback ? "writeback" : "writethrough", 671 inserted->cache->direct ? ", direct" : "", 672 inserted->cache->no_flush ? ", ignore flushes" : ""); 673 674 if (inserted->backing_file) { 675 monitor_printf(mon, 676 " Backing file: %s " 677 "(chain depth: %" PRId64 ")\n", 678 inserted->backing_file, 679 inserted->backing_file_depth); 680 } 681 682 if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) { 683 monitor_printf(mon, " Detect zeroes: %s\n", 684 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes)); 685 } 686 687 if (inserted->bps || inserted->bps_rd || inserted->bps_wr || 688 inserted->iops || inserted->iops_rd || inserted->iops_wr) 689 { 690 monitor_printf(mon, " I/O throttling: bps=%" PRId64 691 " bps_rd=%" PRId64 " bps_wr=%" PRId64 692 " bps_max=%" PRId64 693 " bps_rd_max=%" PRId64 694 " bps_wr_max=%" PRId64 695 " iops=%" PRId64 " iops_rd=%" PRId64 696 " iops_wr=%" PRId64 697 " iops_max=%" PRId64 698 " iops_rd_max=%" PRId64 699 " iops_wr_max=%" PRId64 700 " iops_size=%" PRId64 701 " group=%s\n", 702 inserted->bps, 703 inserted->bps_rd, 704 inserted->bps_wr, 705 inserted->bps_max, 706 inserted->bps_rd_max, 707 inserted->bps_wr_max, 708 inserted->iops, 709 inserted->iops_rd, 710 inserted->iops_wr, 711 inserted->iops_max, 712 inserted->iops_rd_max, 713 inserted->iops_wr_max, 714 inserted->iops_size, 715 inserted->group); 716 } 717 718 if (verbose) { 719 monitor_printf(mon, "\nImages:\n"); 720 image_info = inserted->image; 721 while (1) { 722 bdrv_node_info_dump(qapi_ImageInfo_base(image_info), 0, false); 723 if (image_info->backing_image) { 724 image_info = image_info->backing_image; 725 } else { 726 break; 727 } 728 } 729 } 730 } 731 732 void hmp_info_block(Monitor *mon, const QDict *qdict) 733 { 734 BlockInfoList *block_list, *info; 735 BlockDeviceInfoList *blockdev_list, *blockdev; 736 const char *device = qdict_get_try_str(qdict, "device"); 737 bool verbose = qdict_get_try_bool(qdict, "verbose", false); 738 bool nodes = qdict_get_try_bool(qdict, "nodes", false); 739 bool printed = false; 740 741 /* Print BlockBackend information */ 742 if (!nodes) { 743 block_list = qmp_query_block(NULL); 744 } else { 745 block_list = NULL; 746 } 747 748 for (info = block_list; info; info = info->next) { 749 if (device && strcmp(device, info->value->device)) { 750 continue; 751 } 752 753 if (info != block_list) { 754 monitor_printf(mon, "\n"); 755 } 756 757 print_block_info(mon, info->value, info->value->inserted, 758 verbose); 759 printed = true; 760 } 761 762 qapi_free_BlockInfoList(block_list); 763 764 if ((!device && !nodes) || printed) { 765 return; 766 } 767 768 /* Print node information */ 769 blockdev_list = qmp_query_named_block_nodes(false, false, NULL); 770 for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) { 771 assert(blockdev->value->node_name); 772 if (device && strcmp(device, blockdev->value->node_name)) { 773 continue; 774 } 775 776 if (blockdev != blockdev_list) { 777 monitor_printf(mon, "\n"); 778 } 779 780 print_block_info(mon, NULL, blockdev->value, verbose); 781 } 782 qapi_free_BlockDeviceInfoList(blockdev_list); 783 } 784 785 void hmp_info_blockstats(Monitor *mon, const QDict *qdict) 786 { 787 BlockStatsList *stats_list, *stats; 788 789 stats_list = qmp_query_blockstats(false, false, NULL); 790 791 for (stats = stats_list; stats; stats = stats->next) { 792 if (!stats->value->device) { 793 continue; 794 } 795 796 monitor_printf(mon, "%s:", stats->value->device); 797 monitor_printf(mon, " rd_bytes=%" PRId64 798 " wr_bytes=%" PRId64 799 " rd_operations=%" PRId64 800 " wr_operations=%" PRId64 801 " flush_operations=%" PRId64 802 " wr_total_time_ns=%" PRId64 803 " rd_total_time_ns=%" PRId64 804 " flush_total_time_ns=%" PRId64 805 " rd_merged=%" PRId64 806 " wr_merged=%" PRId64 807 " idle_time_ns=%" PRId64 808 "\n", 809 stats->value->stats->rd_bytes, 810 stats->value->stats->wr_bytes, 811 stats->value->stats->rd_operations, 812 stats->value->stats->wr_operations, 813 stats->value->stats->flush_operations, 814 stats->value->stats->wr_total_time_ns, 815 stats->value->stats->rd_total_time_ns, 816 stats->value->stats->flush_total_time_ns, 817 stats->value->stats->rd_merged, 818 stats->value->stats->wr_merged, 819 stats->value->stats->idle_time_ns); 820 } 821 822 qapi_free_BlockStatsList(stats_list); 823 } 824 825 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict) 826 { 827 BlockJobInfoList *list; 828 829 list = qmp_query_block_jobs(&error_abort); 830 831 if (!list) { 832 monitor_printf(mon, "No active jobs\n"); 833 return; 834 } 835 836 while (list) { 837 if (list->value->type == JOB_TYPE_STREAM) { 838 monitor_printf(mon, "Streaming device %s: Completed %" PRId64 839 " of %" PRId64 " bytes, speed limit %" PRId64 840 " bytes/s\n", 841 list->value->device, 842 list->value->offset, 843 list->value->len, 844 list->value->speed); 845 } else { 846 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64 847 " of %" PRId64 " bytes, speed limit %" PRId64 848 " bytes/s\n", 849 JobType_str(list->value->type), 850 list->value->device, 851 list->value->offset, 852 list->value->len, 853 list->value->speed); 854 } 855 list = list->next; 856 } 857 858 qapi_free_BlockJobInfoList(list); 859 } 860 861 void hmp_info_snapshots(Monitor *mon, const QDict *qdict) 862 { 863 BlockDriverState *bs, *bs1; 864 BdrvNextIterator it1; 865 QEMUSnapshotInfo *sn_tab, *sn; 866 bool no_snapshot = true; 867 int nb_sns, i; 868 int total; 869 int *global_snapshots; 870 871 typedef struct SnapshotEntry { 872 QEMUSnapshotInfo sn; 873 QTAILQ_ENTRY(SnapshotEntry) next; 874 } SnapshotEntry; 875 876 typedef struct ImageEntry { 877 const char *imagename; 878 QTAILQ_ENTRY(ImageEntry) next; 879 QTAILQ_HEAD(, SnapshotEntry) snapshots; 880 } ImageEntry; 881 882 QTAILQ_HEAD(, ImageEntry) image_list = 883 QTAILQ_HEAD_INITIALIZER(image_list); 884 885 ImageEntry *image_entry, *next_ie; 886 SnapshotEntry *snapshot_entry; 887 Error *err = NULL; 888 889 GRAPH_RDLOCK_GUARD_MAINLOOP(); 890 891 bs = bdrv_all_find_vmstate_bs(NULL, false, NULL, &err); 892 if (!bs) { 893 error_report_err(err); 894 return; 895 } 896 897 nb_sns = bdrv_snapshot_list(bs, &sn_tab); 898 899 if (nb_sns < 0) { 900 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); 901 return; 902 } 903 904 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) { 905 int bs1_nb_sns = 0; 906 ImageEntry *ie; 907 SnapshotEntry *se; 908 909 if (bdrv_can_snapshot(bs1)) { 910 sn = NULL; 911 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn); 912 if (bs1_nb_sns > 0) { 913 no_snapshot = false; 914 ie = g_new0(ImageEntry, 1); 915 ie->imagename = bdrv_get_device_name(bs1); 916 QTAILQ_INIT(&ie->snapshots); 917 QTAILQ_INSERT_TAIL(&image_list, ie, next); 918 for (i = 0; i < bs1_nb_sns; i++) { 919 se = g_new0(SnapshotEntry, 1); 920 se->sn = sn[i]; 921 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next); 922 } 923 } 924 g_free(sn); 925 } 926 } 927 928 if (no_snapshot) { 929 monitor_printf(mon, "There is no snapshot available.\n"); 930 return; 931 } 932 933 global_snapshots = g_new0(int, nb_sns); 934 total = 0; 935 for (i = 0; i < nb_sns; i++) { 936 SnapshotEntry *next_sn; 937 if (bdrv_all_has_snapshot(sn_tab[i].name, false, NULL, NULL) == 1) { 938 global_snapshots[total] = i; 939 total++; 940 QTAILQ_FOREACH(image_entry, &image_list, next) { 941 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, 942 next, next_sn) { 943 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) { 944 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry, 945 next); 946 g_free(snapshot_entry); 947 } 948 } 949 } 950 } 951 } 952 monitor_printf(mon, "List of snapshots present on all disks:\n"); 953 954 if (total > 0) { 955 bdrv_snapshot_dump(NULL); 956 monitor_printf(mon, "\n"); 957 for (i = 0; i < total; i++) { 958 sn = &sn_tab[global_snapshots[i]]; 959 /* 960 * The ID is not guaranteed to be the same on all images, so 961 * overwrite it. 962 */ 963 pstrcpy(sn->id_str, sizeof(sn->id_str), "--"); 964 bdrv_snapshot_dump(sn); 965 monitor_printf(mon, "\n"); 966 } 967 } else { 968 monitor_printf(mon, "None\n"); 969 } 970 971 QTAILQ_FOREACH(image_entry, &image_list, next) { 972 if (QTAILQ_EMPTY(&image_entry->snapshots)) { 973 continue; 974 } 975 monitor_printf(mon, 976 "\nList of partial (non-loadable) snapshots on '%s':\n", 977 image_entry->imagename); 978 bdrv_snapshot_dump(NULL); 979 monitor_printf(mon, "\n"); 980 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) { 981 bdrv_snapshot_dump(&snapshot_entry->sn); 982 monitor_printf(mon, "\n"); 983 } 984 } 985 986 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) { 987 SnapshotEntry *next_sn; 988 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next, 989 next_sn) { 990 g_free(snapshot_entry); 991 } 992 g_free(image_entry); 993 } 994 g_free(sn_tab); 995 g_free(global_snapshots); 996 } 997 998 void hmp_change_medium(Monitor *mon, const char *device, const char *target, 999 const char *arg, const char *read_only, bool force, 1000 Error **errp) 1001 { 1002 ERRP_GUARD(); 1003 BlockdevChangeReadOnlyMode read_only_mode = 0; 1004 1005 if (read_only) { 1006 read_only_mode = 1007 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup, 1008 read_only, 1009 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, errp); 1010 if (*errp) { 1011 return; 1012 } 1013 } 1014 1015 qmp_blockdev_change_medium(device, NULL, target, arg, true, force, 1016 !!read_only, read_only_mode, errp); 1017 } 1018