1 /* 2 * Block layer qmp and info dump related functions 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include "qemu/cutils.h" 27 #include "block/qapi.h" 28 #include "block/block_int.h" 29 #include "block/dirty-bitmap.h" 30 #include "block/throttle-groups.h" 31 #include "block/write-threshold.h" 32 #include "qapi/error.h" 33 #include "qapi/qapi-commands-block-core.h" 34 #include "qapi/qobject-output-visitor.h" 35 #include "qapi/qapi-visit-block-core.h" 36 #include "qapi/qmp/qbool.h" 37 #include "qapi/qmp/qdict.h" 38 #include "qapi/qmp/qlist.h" 39 #include "qapi/qmp/qnum.h" 40 #include "qapi/qmp/qstring.h" 41 #include "qemu/qemu-print.h" 42 #include "sysemu/block-backend.h" 43 #include "qemu/cutils.h" 44 45 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, 46 BlockDriverState *bs, 47 bool flat, 48 Error **errp) 49 { 50 ImageInfo **p_image_info; 51 ImageInfo *backing_info; 52 BlockDriverState *bs0, *backing; 53 BlockDeviceInfo *info; 54 ERRP_GUARD(); 55 56 if (!bs->drv) { 57 error_setg(errp, "Block device %s is ejected", bs->node_name); 58 return NULL; 59 } 60 61 bdrv_refresh_filename(bs); 62 63 info = g_malloc0(sizeof(*info)); 64 info->file = g_strdup(bs->filename); 65 info->ro = bdrv_is_read_only(bs); 66 info->drv = g_strdup(bs->drv->format_name); 67 info->encrypted = bs->encrypted; 68 69 info->cache = g_new(BlockdevCacheInfo, 1); 70 *info->cache = (BlockdevCacheInfo) { 71 .writeback = blk ? blk_enable_write_cache(blk) : true, 72 .direct = !!(bs->open_flags & BDRV_O_NOCACHE), 73 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH), 74 }; 75 76 if (bs->node_name[0]) { 77 info->node_name = g_strdup(bs->node_name); 78 } 79 80 backing = bdrv_cow_bs(bs); 81 if (backing) { 82 info->backing_file = g_strdup(backing->filename); 83 } 84 85 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { 86 info->has_dirty_bitmaps = true; 87 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); 88 } 89 90 info->detect_zeroes = bs->detect_zeroes; 91 92 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) { 93 ThrottleConfig cfg; 94 BlockBackendPublic *blkp = blk_get_public(blk); 95 96 throttle_group_get_config(&blkp->throttle_group_member, &cfg); 97 98 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; 99 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; 100 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; 101 102 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; 103 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; 104 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; 105 106 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 107 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 108 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 109 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 110 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 111 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 112 113 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 114 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 115 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 116 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 117 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 118 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 119 120 info->has_bps_max_length = info->has_bps_max; 121 info->bps_max_length = 122 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length; 123 info->has_bps_rd_max_length = info->has_bps_rd_max; 124 info->bps_rd_max_length = 125 cfg.buckets[THROTTLE_BPS_READ].burst_length; 126 info->has_bps_wr_max_length = info->has_bps_wr_max; 127 info->bps_wr_max_length = 128 cfg.buckets[THROTTLE_BPS_WRITE].burst_length; 129 130 info->has_iops_max_length = info->has_iops_max; 131 info->iops_max_length = 132 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length; 133 info->has_iops_rd_max_length = info->has_iops_rd_max; 134 info->iops_rd_max_length = 135 cfg.buckets[THROTTLE_OPS_READ].burst_length; 136 info->has_iops_wr_max_length = info->has_iops_wr_max; 137 info->iops_wr_max_length = 138 cfg.buckets[THROTTLE_OPS_WRITE].burst_length; 139 140 info->has_iops_size = cfg.op_size; 141 info->iops_size = cfg.op_size; 142 143 info->group = 144 g_strdup(throttle_group_get_name(&blkp->throttle_group_member)); 145 } 146 147 info->write_threshold = bdrv_write_threshold_get(bs); 148 149 bs0 = bs; 150 p_image_info = &info->image; 151 info->backing_file_depth = 0; 152 153 /* 154 * Skip automatically inserted nodes that the user isn't aware of for 155 * query-block (blk != NULL), but not for query-named-block-nodes 156 */ 157 bdrv_query_image_info(bs0, p_image_info, flat, blk != NULL, errp); 158 if (*errp) { 159 qapi_free_BlockDeviceInfo(info); 160 return NULL; 161 } 162 163 backing_info = info->image->backing_image; 164 while (backing_info) { 165 info->backing_file_depth++; 166 backing_info = backing_info->backing_image; 167 } 168 169 return info; 170 } 171 172 /* 173 * Returns 0 on success, with *p_list either set to describe snapshot 174 * information, or NULL because there are no snapshots. Returns -errno on 175 * error, with *p_list untouched. 176 */ 177 int bdrv_query_snapshot_info_list(BlockDriverState *bs, 178 SnapshotInfoList **p_list, 179 Error **errp) 180 { 181 int i, sn_count; 182 QEMUSnapshotInfo *sn_tab = NULL; 183 SnapshotInfoList *head = NULL, **tail = &head; 184 SnapshotInfo *info; 185 186 sn_count = bdrv_snapshot_list(bs, &sn_tab); 187 if (sn_count < 0) { 188 const char *dev = bdrv_get_device_name(bs); 189 switch (sn_count) { 190 case -ENOMEDIUM: 191 error_setg(errp, "Device '%s' is not inserted", dev); 192 break; 193 case -ENOTSUP: 194 error_setg(errp, 195 "Device '%s' does not support internal snapshots", 196 dev); 197 break; 198 default: 199 error_setg_errno(errp, -sn_count, 200 "Can't list snapshots of device '%s'", dev); 201 break; 202 } 203 return sn_count; 204 } 205 206 for (i = 0; i < sn_count; i++) { 207 info = g_new0(SnapshotInfo, 1); 208 info->id = g_strdup(sn_tab[i].id_str); 209 info->name = g_strdup(sn_tab[i].name); 210 info->vm_state_size = sn_tab[i].vm_state_size; 211 info->date_sec = sn_tab[i].date_sec; 212 info->date_nsec = sn_tab[i].date_nsec; 213 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; 214 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; 215 info->icount = sn_tab[i].icount; 216 info->has_icount = sn_tab[i].icount != -1ULL; 217 218 QAPI_LIST_APPEND(tail, info); 219 } 220 221 g_free(sn_tab); 222 *p_list = head; 223 return 0; 224 } 225 226 /** 227 * Helper function for other query info functions. Store information about @bs 228 * in @info, setting @errp on error. 229 */ 230 static void bdrv_do_query_node_info(BlockDriverState *bs, 231 BlockNodeInfo *info, 232 Error **errp) 233 { 234 int64_t size; 235 const char *backing_filename; 236 BlockDriverInfo bdi; 237 int ret; 238 Error *err = NULL; 239 240 aio_context_acquire(bdrv_get_aio_context(bs)); 241 242 size = bdrv_getlength(bs); 243 if (size < 0) { 244 error_setg_errno(errp, -size, "Can't get image size '%s'", 245 bs->exact_filename); 246 goto out; 247 } 248 249 bdrv_refresh_filename(bs); 250 251 info->filename = g_strdup(bs->filename); 252 info->format = g_strdup(bdrv_get_format_name(bs)); 253 info->virtual_size = size; 254 info->actual_size = bdrv_get_allocated_file_size(bs); 255 info->has_actual_size = info->actual_size >= 0; 256 if (bs->encrypted) { 257 info->encrypted = true; 258 info->has_encrypted = true; 259 } 260 if (bdrv_get_info(bs, &bdi) >= 0) { 261 if (bdi.cluster_size != 0) { 262 info->cluster_size = bdi.cluster_size; 263 info->has_cluster_size = true; 264 } 265 info->dirty_flag = bdi.is_dirty; 266 info->has_dirty_flag = true; 267 } 268 info->format_specific = bdrv_get_specific_info(bs, &err); 269 if (err) { 270 error_propagate(errp, err); 271 goto out; 272 } 273 backing_filename = bs->backing_file; 274 if (backing_filename[0] != '\0') { 275 char *backing_filename2; 276 277 info->backing_filename = g_strdup(backing_filename); 278 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL); 279 280 /* Always report the full_backing_filename if present, even if it's the 281 * same as backing_filename. That they are same is useful info. */ 282 if (backing_filename2) { 283 info->full_backing_filename = g_strdup(backing_filename2); 284 } 285 286 if (bs->backing_format[0]) { 287 info->backing_filename_format = g_strdup(bs->backing_format); 288 } 289 g_free(backing_filename2); 290 } 291 292 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); 293 switch (ret) { 294 case 0: 295 if (info->snapshots) { 296 info->has_snapshots = true; 297 } 298 break; 299 /* recoverable error */ 300 case -ENOMEDIUM: 301 case -ENOTSUP: 302 error_free(err); 303 break; 304 default: 305 error_propagate(errp, err); 306 goto out; 307 } 308 309 out: 310 aio_context_release(bdrv_get_aio_context(bs)); 311 } 312 313 /** 314 * bdrv_query_block_node_info: 315 * @bs: block node to examine 316 * @p_info: location to store node information 317 * @errp: location to store error information 318 * 319 * Store image information about @bs in @p_info. 320 * 321 * @p_info will be set only on success. On error, store error in @errp. 322 */ 323 void bdrv_query_block_node_info(BlockDriverState *bs, 324 BlockNodeInfo **p_info, 325 Error **errp) 326 { 327 BlockNodeInfo *info; 328 ERRP_GUARD(); 329 330 info = g_new0(BlockNodeInfo, 1); 331 bdrv_do_query_node_info(bs, info, errp); 332 if (*errp) { 333 qapi_free_BlockNodeInfo(info); 334 return; 335 } 336 337 *p_info = info; 338 } 339 340 /** 341 * bdrv_query_image_info: 342 * @bs: block node to examine 343 * @p_info: location to store image information 344 * @flat: skip backing node information 345 * @skip_implicit_filters: skip implicit filters in the backing chain 346 * @errp: location to store error information 347 * 348 * Store image information in @p_info, potentially recursively covering the 349 * backing chain. 350 * 351 * If @flat is true, do not query backing image information, i.e. 352 * (*p_info)->has_backing_image will be set to false and 353 * (*p_info)->backing_image to NULL even when the image does in fact have a 354 * backing image. 355 * 356 * If @skip_implicit_filters is true, implicit filter nodes in the backing chain 357 * will be skipped when querying backing image information. 358 * (@skip_implicit_filters is ignored when @flat is true.) 359 * 360 * @p_info will be set only on success. On error, store error in @errp. 361 */ 362 void bdrv_query_image_info(BlockDriverState *bs, 363 ImageInfo **p_info, 364 bool flat, 365 bool skip_implicit_filters, 366 Error **errp) 367 { 368 ImageInfo *info; 369 ERRP_GUARD(); 370 371 info = g_new0(ImageInfo, 1); 372 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp); 373 if (*errp) { 374 goto fail; 375 } 376 377 if (!flat) { 378 BlockDriverState *backing; 379 380 /* 381 * Use any filtered child here (for backwards compatibility to when 382 * we always took bs->backing, which might be any filtered child). 383 */ 384 backing = bdrv_filter_or_cow_bs(bs); 385 if (skip_implicit_filters) { 386 backing = bdrv_skip_implicit_filters(backing); 387 } 388 389 if (backing) { 390 bdrv_query_image_info(backing, &info->backing_image, false, 391 skip_implicit_filters, errp); 392 if (*errp) { 393 goto fail; 394 } 395 } 396 } 397 398 *p_info = info; 399 return; 400 401 fail: 402 assert(*errp); 403 qapi_free_ImageInfo(info); 404 } 405 406 /* @p_info will be set only on success. */ 407 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, 408 Error **errp) 409 { 410 BlockInfo *info = g_malloc0(sizeof(*info)); 411 BlockDriverState *bs = blk_bs(blk); 412 char *qdev; 413 414 /* Skip automatically inserted nodes that the user isn't aware of */ 415 bs = bdrv_skip_implicit_filters(bs); 416 417 info->device = g_strdup(blk_name(blk)); 418 info->type = g_strdup("unknown"); 419 info->locked = blk_dev_is_medium_locked(blk); 420 info->removable = blk_dev_has_removable_media(blk); 421 422 qdev = blk_get_attached_dev_id(blk); 423 if (qdev && *qdev) { 424 info->qdev = qdev; 425 } else { 426 g_free(qdev); 427 } 428 429 if (blk_dev_has_tray(blk)) { 430 info->has_tray_open = true; 431 info->tray_open = blk_dev_is_tray_open(blk); 432 } 433 434 if (blk_iostatus_is_enabled(blk)) { 435 info->has_io_status = true; 436 info->io_status = blk_iostatus(blk); 437 } 438 439 if (bs && bs->drv) { 440 info->inserted = bdrv_block_device_info(blk, bs, false, errp); 441 if (info->inserted == NULL) { 442 goto err; 443 } 444 } 445 446 *p_info = info; 447 return; 448 449 err: 450 qapi_free_BlockInfo(info); 451 } 452 453 static uint64List *uint64_list(uint64_t *list, int size) 454 { 455 int i; 456 uint64List *out_list = NULL; 457 uint64List **tail = &out_list; 458 459 for (i = 0; i < size; i++) { 460 QAPI_LIST_APPEND(tail, list[i]); 461 } 462 463 return out_list; 464 } 465 466 static BlockLatencyHistogramInfo * 467 bdrv_latency_histogram_stats(BlockLatencyHistogram *hist) 468 { 469 BlockLatencyHistogramInfo *info; 470 471 if (!hist->bins) { 472 return NULL; 473 } 474 475 info = g_new0(BlockLatencyHistogramInfo, 1); 476 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1); 477 info->bins = uint64_list(hist->bins, hist->nbins); 478 return info; 479 } 480 481 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) 482 { 483 BlockAcctStats *stats = blk_get_stats(blk); 484 BlockAcctTimedStats *ts = NULL; 485 BlockLatencyHistogram *hgram; 486 487 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; 488 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; 489 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP]; 490 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; 491 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; 492 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP]; 493 494 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; 495 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; 496 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; 497 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP]; 498 499 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; 500 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; 501 ds->invalid_flush_operations = 502 stats->invalid_ops[BLOCK_ACCT_FLUSH]; 503 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP]; 504 505 ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; 506 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; 507 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP]; 508 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; 509 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; 510 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; 511 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; 512 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP]; 513 514 ds->has_idle_time_ns = stats->last_access_time_ns > 0; 515 if (ds->has_idle_time_ns) { 516 ds->idle_time_ns = block_acct_idle_time_ns(stats); 517 } 518 519 ds->account_invalid = stats->account_invalid; 520 ds->account_failed = stats->account_failed; 521 522 while ((ts = block_acct_interval_next(stats, ts))) { 523 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); 524 525 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; 526 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; 527 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; 528 529 dev_stats->interval_length = ts->interval_length; 530 531 dev_stats->min_rd_latency_ns = timed_average_min(rd); 532 dev_stats->max_rd_latency_ns = timed_average_max(rd); 533 dev_stats->avg_rd_latency_ns = timed_average_avg(rd); 534 535 dev_stats->min_wr_latency_ns = timed_average_min(wr); 536 dev_stats->max_wr_latency_ns = timed_average_max(wr); 537 dev_stats->avg_wr_latency_ns = timed_average_avg(wr); 538 539 dev_stats->min_flush_latency_ns = timed_average_min(fl); 540 dev_stats->max_flush_latency_ns = timed_average_max(fl); 541 dev_stats->avg_flush_latency_ns = timed_average_avg(fl); 542 543 dev_stats->avg_rd_queue_depth = 544 block_acct_queue_depth(ts, BLOCK_ACCT_READ); 545 dev_stats->avg_wr_queue_depth = 546 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); 547 548 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats); 549 } 550 551 hgram = stats->latency_histogram; 552 ds->rd_latency_histogram 553 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]); 554 ds->wr_latency_histogram 555 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]); 556 ds->flush_latency_histogram 557 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]); 558 } 559 560 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs, 561 bool blk_level) 562 { 563 BdrvChild *parent_child; 564 BlockDriverState *filter_or_cow_bs; 565 BlockStats *s = NULL; 566 567 s = g_malloc0(sizeof(*s)); 568 s->stats = g_malloc0(sizeof(*s->stats)); 569 570 if (!bs) { 571 return s; 572 } 573 574 /* Skip automatically inserted nodes that the user isn't aware of in 575 * a BlockBackend-level command. Stay at the exact node for a node-level 576 * command. */ 577 if (blk_level) { 578 bs = bdrv_skip_implicit_filters(bs); 579 } 580 581 if (bdrv_get_node_name(bs)[0]) { 582 s->node_name = g_strdup(bdrv_get_node_name(bs)); 583 } 584 585 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset); 586 587 s->driver_specific = bdrv_get_specific_stats(bs); 588 589 parent_child = bdrv_primary_child(bs); 590 if (!parent_child || 591 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED))) 592 { 593 BdrvChild *c; 594 595 /* 596 * Look for a unique data-storing child. We do not need to look for 597 * filtered children, as there would be only one and it would have been 598 * the primary child. 599 */ 600 parent_child = NULL; 601 QLIST_FOREACH(c, &bs->children, next) { 602 if (c->role & BDRV_CHILD_DATA) { 603 if (parent_child) { 604 /* 605 * There are multiple data-storing children and we cannot 606 * choose between them. 607 */ 608 parent_child = NULL; 609 break; 610 } 611 parent_child = c; 612 } 613 } 614 } 615 if (parent_child) { 616 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level); 617 } 618 619 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs); 620 if (blk_level && filter_or_cow_bs) { 621 /* 622 * Put any filtered or COW child here (for backwards 623 * compatibility to when we put bs0->backing here, which might 624 * be either) 625 */ 626 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level); 627 } 628 629 return s; 630 } 631 632 BlockInfoList *qmp_query_block(Error **errp) 633 { 634 BlockInfoList *head = NULL, **p_next = &head; 635 BlockBackend *blk; 636 Error *local_err = NULL; 637 638 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 639 BlockInfoList *info; 640 641 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 642 continue; 643 } 644 645 info = g_malloc0(sizeof(*info)); 646 bdrv_query_info(blk, &info->value, &local_err); 647 if (local_err) { 648 error_propagate(errp, local_err); 649 g_free(info); 650 qapi_free_BlockInfoList(head); 651 return NULL; 652 } 653 654 *p_next = info; 655 p_next = &info->next; 656 } 657 658 return head; 659 } 660 661 BlockStatsList *qmp_query_blockstats(bool has_query_nodes, 662 bool query_nodes, 663 Error **errp) 664 { 665 BlockStatsList *head = NULL, **tail = &head; 666 BlockBackend *blk; 667 BlockDriverState *bs; 668 669 /* Just to be safe if query_nodes is not always initialized */ 670 if (has_query_nodes && query_nodes) { 671 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) { 672 AioContext *ctx = bdrv_get_aio_context(bs); 673 674 aio_context_acquire(ctx); 675 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false)); 676 aio_context_release(ctx); 677 } 678 } else { 679 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 680 AioContext *ctx = blk_get_aio_context(blk); 681 BlockStats *s; 682 char *qdev; 683 684 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 685 continue; 686 } 687 688 aio_context_acquire(ctx); 689 s = bdrv_query_bds_stats(blk_bs(blk), true); 690 s->device = g_strdup(blk_name(blk)); 691 692 qdev = blk_get_attached_dev_id(blk); 693 if (qdev && *qdev) { 694 s->qdev = qdev; 695 } else { 696 g_free(qdev); 697 } 698 699 bdrv_query_blk_stats(s->stats, blk); 700 aio_context_release(ctx); 701 702 QAPI_LIST_APPEND(tail, s); 703 } 704 } 705 706 return head; 707 } 708 709 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn) 710 { 711 char clock_buf[128]; 712 char icount_buf[128] = {0}; 713 int64_t secs; 714 char *sizing = NULL; 715 716 if (!sn) { 717 qemu_printf("%-10s%-17s%8s%20s%13s%11s", 718 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT"); 719 } else { 720 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec); 721 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S"); 722 723 secs = sn->vm_clock_nsec / 1000000000; 724 snprintf(clock_buf, sizeof(clock_buf), 725 "%02d:%02d:%02d.%03d", 726 (int)(secs / 3600), 727 (int)((secs / 60) % 60), 728 (int)(secs % 60), 729 (int)((sn->vm_clock_nsec / 1000000) % 1000)); 730 sizing = size_to_str(sn->vm_state_size); 731 if (sn->icount != -1ULL) { 732 snprintf(icount_buf, sizeof(icount_buf), 733 "%"PRId64, sn->icount); 734 } 735 qemu_printf("%-9s %-16s %8s%20s%13s%11s", 736 sn->id_str, sn->name, 737 sizing, 738 date_buf, 739 clock_buf, 740 icount_buf); 741 } 742 g_free(sizing); 743 } 744 745 static void dump_qdict(int indentation, QDict *dict); 746 static void dump_qlist(int indentation, QList *list); 747 748 static void dump_qobject(int comp_indent, QObject *obj) 749 { 750 switch (qobject_type(obj)) { 751 case QTYPE_QNUM: { 752 QNum *value = qobject_to(QNum, obj); 753 char *tmp = qnum_to_string(value); 754 qemu_printf("%s", tmp); 755 g_free(tmp); 756 break; 757 } 758 case QTYPE_QSTRING: { 759 QString *value = qobject_to(QString, obj); 760 qemu_printf("%s", qstring_get_str(value)); 761 break; 762 } 763 case QTYPE_QDICT: { 764 QDict *value = qobject_to(QDict, obj); 765 dump_qdict(comp_indent, value); 766 break; 767 } 768 case QTYPE_QLIST: { 769 QList *value = qobject_to(QList, obj); 770 dump_qlist(comp_indent, value); 771 break; 772 } 773 case QTYPE_QBOOL: { 774 QBool *value = qobject_to(QBool, obj); 775 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false"); 776 break; 777 } 778 default: 779 abort(); 780 } 781 } 782 783 static void dump_qlist(int indentation, QList *list) 784 { 785 const QListEntry *entry; 786 int i = 0; 787 788 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { 789 QType type = qobject_type(entry->value); 790 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 791 qemu_printf("%*s[%i]:%c", indentation * 4, "", i, 792 composite ? '\n' : ' '); 793 dump_qobject(indentation + 1, entry->value); 794 if (!composite) { 795 qemu_printf("\n"); 796 } 797 } 798 } 799 800 static void dump_qdict(int indentation, QDict *dict) 801 { 802 const QDictEntry *entry; 803 804 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { 805 QType type = qobject_type(entry->value); 806 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 807 char *key = g_malloc(strlen(entry->key) + 1); 808 int i; 809 810 /* replace dashes with spaces in key (variable) names */ 811 for (i = 0; entry->key[i]; i++) { 812 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; 813 } 814 key[i] = 0; 815 qemu_printf("%*s%s:%c", indentation * 4, "", key, 816 composite ? '\n' : ' '); 817 dump_qobject(indentation + 1, entry->value); 818 if (!composite) { 819 qemu_printf("\n"); 820 } 821 g_free(key); 822 } 823 } 824 825 /* 826 * Return whether dumping the given QObject with dump_qobject() would 827 * yield an empty dump, i.e. not print anything. 828 */ 829 static bool qobject_is_empty_dump(const QObject *obj) 830 { 831 switch (qobject_type(obj)) { 832 case QTYPE_QNUM: 833 case QTYPE_QSTRING: 834 case QTYPE_QBOOL: 835 return false; 836 837 case QTYPE_QDICT: 838 return qdict_size(qobject_to(QDict, obj)) == 0; 839 840 case QTYPE_QLIST: 841 return qlist_empty(qobject_to(QList, obj)); 842 843 default: 844 abort(); 845 } 846 } 847 848 /** 849 * Dumps the given ImageInfoSpecific object in a human-readable form, 850 * prepending an optional prefix if the dump is not empty. 851 */ 852 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec, 853 const char *prefix) 854 { 855 QObject *obj, *data; 856 Visitor *v = qobject_output_visitor_new(&obj); 857 858 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); 859 visit_complete(v, &obj); 860 data = qdict_get(qobject_to(QDict, obj), "data"); 861 if (!qobject_is_empty_dump(data)) { 862 if (prefix) { 863 qemu_printf("%s", prefix); 864 } 865 dump_qobject(1, data); 866 } 867 qobject_unref(obj); 868 visit_free(v); 869 } 870 871 void bdrv_node_info_dump(BlockNodeInfo *info) 872 { 873 char *size_buf, *dsize_buf; 874 if (!info->has_actual_size) { 875 dsize_buf = g_strdup("unavailable"); 876 } else { 877 dsize_buf = size_to_str(info->actual_size); 878 } 879 size_buf = size_to_str(info->virtual_size); 880 qemu_printf("image: %s\n" 881 "file format: %s\n" 882 "virtual size: %s (%" PRId64 " bytes)\n" 883 "disk size: %s\n", 884 info->filename, info->format, size_buf, 885 info->virtual_size, 886 dsize_buf); 887 g_free(size_buf); 888 g_free(dsize_buf); 889 890 if (info->has_encrypted && info->encrypted) { 891 qemu_printf("encrypted: yes\n"); 892 } 893 894 if (info->has_cluster_size) { 895 qemu_printf("cluster_size: %" PRId64 "\n", 896 info->cluster_size); 897 } 898 899 if (info->has_dirty_flag && info->dirty_flag) { 900 qemu_printf("cleanly shut down: no\n"); 901 } 902 903 if (info->backing_filename) { 904 qemu_printf("backing file: %s", info->backing_filename); 905 if (!info->full_backing_filename) { 906 qemu_printf(" (cannot determine actual path)"); 907 } else if (strcmp(info->backing_filename, 908 info->full_backing_filename) != 0) { 909 qemu_printf(" (actual path: %s)", info->full_backing_filename); 910 } 911 qemu_printf("\n"); 912 if (info->backing_filename_format) { 913 qemu_printf("backing file format: %s\n", 914 info->backing_filename_format); 915 } 916 } 917 918 if (info->has_snapshots) { 919 SnapshotInfoList *elem; 920 921 qemu_printf("Snapshot list:\n"); 922 bdrv_snapshot_dump(NULL); 923 qemu_printf("\n"); 924 925 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but 926 * we convert to the block layer's native QEMUSnapshotInfo for now. 927 */ 928 for (elem = info->snapshots; elem; elem = elem->next) { 929 QEMUSnapshotInfo sn = { 930 .vm_state_size = elem->value->vm_state_size, 931 .date_sec = elem->value->date_sec, 932 .date_nsec = elem->value->date_nsec, 933 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + 934 elem->value->vm_clock_nsec, 935 .icount = elem->value->has_icount ? 936 elem->value->icount : -1ULL, 937 }; 938 939 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); 940 pstrcpy(sn.name, sizeof(sn.name), elem->value->name); 941 bdrv_snapshot_dump(&sn); 942 qemu_printf("\n"); 943 } 944 } 945 946 if (info->format_specific) { 947 bdrv_image_info_specific_dump(info->format_specific, 948 "Format specific information:\n"); 949 } 950 } 951