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/throttle-groups.h" 30 #include "block/write-threshold.h" 31 #include "qapi/error.h" 32 #include "qapi/qapi-commands-block-core.h" 33 #include "qapi/qobject-output-visitor.h" 34 #include "qapi/qapi-visit-block-core.h" 35 #include "qapi/qmp/qbool.h" 36 #include "qapi/qmp/qdict.h" 37 #include "qapi/qmp/qlist.h" 38 #include "qapi/qmp/qnum.h" 39 #include "qapi/qmp/qstring.h" 40 #include "qemu/qemu-print.h" 41 #include "sysemu/block-backend.h" 42 #include "qemu/cutils.h" 43 44 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, 45 BlockDriverState *bs, 46 bool flat, 47 Error **errp) 48 { 49 ImageInfo **p_image_info; 50 BlockDriverState *bs0, *backing; 51 BlockDeviceInfo *info; 52 53 if (!bs->drv) { 54 error_setg(errp, "Block device %s is ejected", bs->node_name); 55 return NULL; 56 } 57 58 bdrv_refresh_filename(bs); 59 60 info = g_malloc0(sizeof(*info)); 61 info->file = g_strdup(bs->filename); 62 info->ro = bs->read_only; 63 info->drv = g_strdup(bs->drv->format_name); 64 info->encrypted = bs->encrypted; 65 info->encryption_key_missing = false; 66 67 info->cache = g_new(BlockdevCacheInfo, 1); 68 *info->cache = (BlockdevCacheInfo) { 69 .writeback = blk ? blk_enable_write_cache(blk) : true, 70 .direct = !!(bs->open_flags & BDRV_O_NOCACHE), 71 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH), 72 }; 73 74 if (bs->node_name[0]) { 75 info->has_node_name = true; 76 info->node_name = g_strdup(bs->node_name); 77 } 78 79 backing = bdrv_cow_bs(bs); 80 if (backing) { 81 info->has_backing_file = true; 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->has_group = true; 144 info->group = 145 g_strdup(throttle_group_get_name(&blkp->throttle_group_member)); 146 } 147 148 info->write_threshold = bdrv_write_threshold_get(bs); 149 150 bs0 = bs; 151 p_image_info = &info->image; 152 info->backing_file_depth = 0; 153 while (1) { 154 Error *local_err = NULL; 155 bdrv_query_image_info(bs0, p_image_info, &local_err); 156 if (local_err) { 157 error_propagate(errp, local_err); 158 qapi_free_BlockDeviceInfo(info); 159 return NULL; 160 } 161 162 /* stop gathering data for flat output */ 163 if (flat) { 164 break; 165 } 166 167 if (bs0->drv && bdrv_filter_or_cow_child(bs0)) { 168 /* 169 * Put any filtered child here (for backwards compatibility to when 170 * we put bs0->backing here, which might be any filtered child). 171 */ 172 info->backing_file_depth++; 173 bs0 = bdrv_filter_or_cow_bs(bs0); 174 (*p_image_info)->has_backing_image = true; 175 p_image_info = &((*p_image_info)->backing_image); 176 } else { 177 break; 178 } 179 180 /* Skip automatically inserted nodes that the user isn't aware of for 181 * query-block (blk != NULL), but not for query-named-block-nodes */ 182 if (blk) { 183 bs0 = bdrv_skip_implicit_filters(bs0); 184 } 185 } 186 187 return info; 188 } 189 190 /* 191 * Returns 0 on success, with *p_list either set to describe snapshot 192 * information, or NULL because there are no snapshots. Returns -errno on 193 * error, with *p_list untouched. 194 */ 195 int bdrv_query_snapshot_info_list(BlockDriverState *bs, 196 SnapshotInfoList **p_list, 197 Error **errp) 198 { 199 int i, sn_count; 200 QEMUSnapshotInfo *sn_tab = NULL; 201 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL; 202 SnapshotInfo *info; 203 204 sn_count = bdrv_snapshot_list(bs, &sn_tab); 205 if (sn_count < 0) { 206 const char *dev = bdrv_get_device_name(bs); 207 switch (sn_count) { 208 case -ENOMEDIUM: 209 error_setg(errp, "Device '%s' is not inserted", dev); 210 break; 211 case -ENOTSUP: 212 error_setg(errp, 213 "Device '%s' does not support internal snapshots", 214 dev); 215 break; 216 default: 217 error_setg_errno(errp, -sn_count, 218 "Can't list snapshots of device '%s'", dev); 219 break; 220 } 221 return sn_count; 222 } 223 224 for (i = 0; i < sn_count; i++) { 225 info = g_new0(SnapshotInfo, 1); 226 info->id = g_strdup(sn_tab[i].id_str); 227 info->name = g_strdup(sn_tab[i].name); 228 info->vm_state_size = sn_tab[i].vm_state_size; 229 info->date_sec = sn_tab[i].date_sec; 230 info->date_nsec = sn_tab[i].date_nsec; 231 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; 232 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; 233 234 info_list = g_new0(SnapshotInfoList, 1); 235 info_list->value = info; 236 237 /* XXX: waiting for the qapi to support qemu-queue.h types */ 238 if (!cur_item) { 239 head = cur_item = info_list; 240 } else { 241 cur_item->next = info_list; 242 cur_item = info_list; 243 } 244 245 } 246 247 g_free(sn_tab); 248 *p_list = head; 249 return 0; 250 } 251 252 /** 253 * bdrv_query_image_info: 254 * @bs: block device to examine 255 * @p_info: location to store image information 256 * @errp: location to store error information 257 * 258 * Store "flat" image information in @p_info. 259 * 260 * "Flat" means it does *not* query backing image information, 261 * i.e. (*pinfo)->has_backing_image will be set to false and 262 * (*pinfo)->backing_image to NULL even when the image does in fact have 263 * a backing image. 264 * 265 * @p_info will be set only on success. On error, store error in @errp. 266 */ 267 void bdrv_query_image_info(BlockDriverState *bs, 268 ImageInfo **p_info, 269 Error **errp) 270 { 271 int64_t size; 272 const char *backing_filename; 273 BlockDriverInfo bdi; 274 int ret; 275 Error *err = NULL; 276 ImageInfo *info; 277 278 aio_context_acquire(bdrv_get_aio_context(bs)); 279 280 size = bdrv_getlength(bs); 281 if (size < 0) { 282 error_setg_errno(errp, -size, "Can't get image size '%s'", 283 bs->exact_filename); 284 goto out; 285 } 286 287 bdrv_refresh_filename(bs); 288 289 info = g_new0(ImageInfo, 1); 290 info->filename = g_strdup(bs->filename); 291 info->format = g_strdup(bdrv_get_format_name(bs)); 292 info->virtual_size = size; 293 info->actual_size = bdrv_get_allocated_file_size(bs); 294 info->has_actual_size = info->actual_size >= 0; 295 if (bs->encrypted) { 296 info->encrypted = true; 297 info->has_encrypted = true; 298 } 299 if (bdrv_get_info(bs, &bdi) >= 0) { 300 if (bdi.cluster_size != 0) { 301 info->cluster_size = bdi.cluster_size; 302 info->has_cluster_size = true; 303 } 304 info->dirty_flag = bdi.is_dirty; 305 info->has_dirty_flag = true; 306 } 307 info->format_specific = bdrv_get_specific_info(bs, &err); 308 if (err) { 309 error_propagate(errp, err); 310 qapi_free_ImageInfo(info); 311 goto out; 312 } 313 info->has_format_specific = info->format_specific != NULL; 314 315 backing_filename = bs->backing_file; 316 if (backing_filename[0] != '\0') { 317 char *backing_filename2; 318 319 info->backing_filename = g_strdup(backing_filename); 320 info->has_backing_filename = true; 321 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL); 322 323 /* Always report the full_backing_filename if present, even if it's the 324 * same as backing_filename. That they are same is useful info. */ 325 if (backing_filename2) { 326 info->full_backing_filename = g_strdup(backing_filename2); 327 info->has_full_backing_filename = true; 328 } 329 330 if (bs->backing_format[0]) { 331 info->backing_filename_format = g_strdup(bs->backing_format); 332 info->has_backing_filename_format = true; 333 } 334 g_free(backing_filename2); 335 } 336 337 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); 338 switch (ret) { 339 case 0: 340 if (info->snapshots) { 341 info->has_snapshots = true; 342 } 343 break; 344 /* recoverable error */ 345 case -ENOMEDIUM: 346 case -ENOTSUP: 347 error_free(err); 348 break; 349 default: 350 error_propagate(errp, err); 351 qapi_free_ImageInfo(info); 352 goto out; 353 } 354 355 *p_info = info; 356 357 out: 358 aio_context_release(bdrv_get_aio_context(bs)); 359 } 360 361 /* @p_info will be set only on success. */ 362 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, 363 Error **errp) 364 { 365 BlockInfo *info = g_malloc0(sizeof(*info)); 366 BlockDriverState *bs = blk_bs(blk); 367 char *qdev; 368 369 /* Skip automatically inserted nodes that the user isn't aware of */ 370 bs = bdrv_skip_implicit_filters(bs); 371 372 info->device = g_strdup(blk_name(blk)); 373 info->type = g_strdup("unknown"); 374 info->locked = blk_dev_is_medium_locked(blk); 375 info->removable = blk_dev_has_removable_media(blk); 376 377 qdev = blk_get_attached_dev_id(blk); 378 if (qdev && *qdev) { 379 info->has_qdev = true; 380 info->qdev = qdev; 381 } else { 382 g_free(qdev); 383 } 384 385 if (blk_dev_has_tray(blk)) { 386 info->has_tray_open = true; 387 info->tray_open = blk_dev_is_tray_open(blk); 388 } 389 390 if (blk_iostatus_is_enabled(blk)) { 391 info->has_io_status = true; 392 info->io_status = blk_iostatus(blk); 393 } 394 395 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) { 396 info->has_dirty_bitmaps = true; 397 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); 398 } 399 400 if (bs && bs->drv) { 401 info->has_inserted = true; 402 info->inserted = bdrv_block_device_info(blk, bs, false, errp); 403 if (info->inserted == NULL) { 404 goto err; 405 } 406 } 407 408 *p_info = info; 409 return; 410 411 err: 412 qapi_free_BlockInfo(info); 413 } 414 415 static uint64List *uint64_list(uint64_t *list, int size) 416 { 417 int i; 418 uint64List *out_list = NULL; 419 uint64List **pout_list = &out_list; 420 421 for (i = 0; i < size; i++) { 422 uint64List *entry = g_new(uint64List, 1); 423 entry->value = list[i]; 424 *pout_list = entry; 425 pout_list = &entry->next; 426 } 427 428 *pout_list = NULL; 429 430 return out_list; 431 } 432 433 static void bdrv_latency_histogram_stats(BlockLatencyHistogram *hist, 434 bool *not_null, 435 BlockLatencyHistogramInfo **info) 436 { 437 *not_null = hist->bins != NULL; 438 if (*not_null) { 439 *info = g_new0(BlockLatencyHistogramInfo, 1); 440 441 (*info)->boundaries = uint64_list(hist->boundaries, hist->nbins - 1); 442 (*info)->bins = uint64_list(hist->bins, hist->nbins); 443 } 444 } 445 446 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) 447 { 448 BlockAcctStats *stats = blk_get_stats(blk); 449 BlockAcctTimedStats *ts = NULL; 450 451 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; 452 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; 453 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP]; 454 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; 455 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; 456 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP]; 457 458 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; 459 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; 460 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; 461 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP]; 462 463 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; 464 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; 465 ds->invalid_flush_operations = 466 stats->invalid_ops[BLOCK_ACCT_FLUSH]; 467 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP]; 468 469 ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; 470 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; 471 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP]; 472 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; 473 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; 474 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; 475 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; 476 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP]; 477 478 ds->has_idle_time_ns = stats->last_access_time_ns > 0; 479 if (ds->has_idle_time_ns) { 480 ds->idle_time_ns = block_acct_idle_time_ns(stats); 481 } 482 483 ds->account_invalid = stats->account_invalid; 484 ds->account_failed = stats->account_failed; 485 486 while ((ts = block_acct_interval_next(stats, ts))) { 487 BlockDeviceTimedStatsList *timed_stats = 488 g_malloc0(sizeof(*timed_stats)); 489 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); 490 timed_stats->next = ds->timed_stats; 491 timed_stats->value = dev_stats; 492 ds->timed_stats = timed_stats; 493 494 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; 495 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; 496 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; 497 498 dev_stats->interval_length = ts->interval_length; 499 500 dev_stats->min_rd_latency_ns = timed_average_min(rd); 501 dev_stats->max_rd_latency_ns = timed_average_max(rd); 502 dev_stats->avg_rd_latency_ns = timed_average_avg(rd); 503 504 dev_stats->min_wr_latency_ns = timed_average_min(wr); 505 dev_stats->max_wr_latency_ns = timed_average_max(wr); 506 dev_stats->avg_wr_latency_ns = timed_average_avg(wr); 507 508 dev_stats->min_flush_latency_ns = timed_average_min(fl); 509 dev_stats->max_flush_latency_ns = timed_average_max(fl); 510 dev_stats->avg_flush_latency_ns = timed_average_avg(fl); 511 512 dev_stats->avg_rd_queue_depth = 513 block_acct_queue_depth(ts, BLOCK_ACCT_READ); 514 dev_stats->avg_wr_queue_depth = 515 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); 516 } 517 518 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_READ], 519 &ds->has_rd_latency_histogram, 520 &ds->rd_latency_histogram); 521 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_WRITE], 522 &ds->has_wr_latency_histogram, 523 &ds->wr_latency_histogram); 524 bdrv_latency_histogram_stats(&stats->latency_histogram[BLOCK_ACCT_FLUSH], 525 &ds->has_flush_latency_histogram, 526 &ds->flush_latency_histogram); 527 } 528 529 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs, 530 bool blk_level) 531 { 532 BdrvChild *parent_child; 533 BlockDriverState *filter_or_cow_bs; 534 BlockStats *s = NULL; 535 536 s = g_malloc0(sizeof(*s)); 537 s->stats = g_malloc0(sizeof(*s->stats)); 538 539 if (!bs) { 540 return s; 541 } 542 543 /* Skip automatically inserted nodes that the user isn't aware of in 544 * a BlockBackend-level command. Stay at the exact node for a node-level 545 * command. */ 546 if (blk_level) { 547 bs = bdrv_skip_implicit_filters(bs); 548 } 549 550 if (bdrv_get_node_name(bs)[0]) { 551 s->has_node_name = true; 552 s->node_name = g_strdup(bdrv_get_node_name(bs)); 553 } 554 555 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset); 556 557 s->driver_specific = bdrv_get_specific_stats(bs); 558 if (s->driver_specific) { 559 s->has_driver_specific = true; 560 } 561 562 parent_child = bdrv_primary_child(bs); 563 if (!parent_child || 564 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED))) 565 { 566 BdrvChild *c; 567 568 /* 569 * Look for a unique data-storing child. We do not need to look for 570 * filtered children, as there would be only one and it would have been 571 * the primary child. 572 */ 573 parent_child = NULL; 574 QLIST_FOREACH(c, &bs->children, next) { 575 if (c->role & BDRV_CHILD_DATA) { 576 if (parent_child) { 577 /* 578 * There are multiple data-storing children and we cannot 579 * choose between them. 580 */ 581 parent_child = NULL; 582 break; 583 } 584 parent_child = c; 585 } 586 } 587 } 588 if (parent_child) { 589 s->has_parent = true; 590 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level); 591 } 592 593 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs); 594 if (blk_level && filter_or_cow_bs) { 595 /* 596 * Put any filtered or COW child here (for backwards 597 * compatibility to when we put bs0->backing here, which might 598 * be either) 599 */ 600 s->has_backing = true; 601 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level); 602 } 603 604 return s; 605 } 606 607 BlockInfoList *qmp_query_block(Error **errp) 608 { 609 BlockInfoList *head = NULL, **p_next = &head; 610 BlockBackend *blk; 611 Error *local_err = NULL; 612 613 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 614 BlockInfoList *info; 615 616 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 617 continue; 618 } 619 620 info = g_malloc0(sizeof(*info)); 621 bdrv_query_info(blk, &info->value, &local_err); 622 if (local_err) { 623 error_propagate(errp, local_err); 624 g_free(info); 625 qapi_free_BlockInfoList(head); 626 return NULL; 627 } 628 629 *p_next = info; 630 p_next = &info->next; 631 } 632 633 return head; 634 } 635 636 BlockStatsList *qmp_query_blockstats(bool has_query_nodes, 637 bool query_nodes, 638 Error **errp) 639 { 640 BlockStatsList *head = NULL, **p_next = &head; 641 BlockBackend *blk; 642 BlockDriverState *bs; 643 644 /* Just to be safe if query_nodes is not always initialized */ 645 if (has_query_nodes && query_nodes) { 646 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) { 647 BlockStatsList *info = g_malloc0(sizeof(*info)); 648 AioContext *ctx = bdrv_get_aio_context(bs); 649 650 aio_context_acquire(ctx); 651 info->value = bdrv_query_bds_stats(bs, false); 652 aio_context_release(ctx); 653 654 *p_next = info; 655 p_next = &info->next; 656 } 657 } else { 658 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 659 BlockStatsList *info; 660 AioContext *ctx = blk_get_aio_context(blk); 661 BlockStats *s; 662 char *qdev; 663 664 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 665 continue; 666 } 667 668 aio_context_acquire(ctx); 669 s = bdrv_query_bds_stats(blk_bs(blk), true); 670 s->has_device = true; 671 s->device = g_strdup(blk_name(blk)); 672 673 qdev = blk_get_attached_dev_id(blk); 674 if (qdev && *qdev) { 675 s->has_qdev = true; 676 s->qdev = qdev; 677 } else { 678 g_free(qdev); 679 } 680 681 bdrv_query_blk_stats(s->stats, blk); 682 aio_context_release(ctx); 683 684 info = g_malloc0(sizeof(*info)); 685 info->value = s; 686 *p_next = info; 687 p_next = &info->next; 688 } 689 } 690 691 return head; 692 } 693 694 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn) 695 { 696 char date_buf[128], clock_buf[128]; 697 struct tm tm; 698 time_t ti; 699 int64_t secs; 700 char *sizing = NULL; 701 702 if (!sn) { 703 qemu_printf("%-10s%-20s%11s%20s%15s", 704 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 705 } else { 706 ti = sn->date_sec; 707 localtime_r(&ti, &tm); 708 strftime(date_buf, sizeof(date_buf), 709 "%Y-%m-%d %H:%M:%S", &tm); 710 secs = sn->vm_clock_nsec / 1000000000; 711 snprintf(clock_buf, sizeof(clock_buf), 712 "%02d:%02d:%02d.%03d", 713 (int)(secs / 3600), 714 (int)((secs / 60) % 60), 715 (int)(secs % 60), 716 (int)((sn->vm_clock_nsec / 1000000) % 1000)); 717 sizing = size_to_str(sn->vm_state_size); 718 qemu_printf("%-10s%-20s%11s%20s%15s", 719 sn->id_str, sn->name, 720 sizing, 721 date_buf, 722 clock_buf); 723 } 724 g_free(sizing); 725 } 726 727 static void dump_qdict(int indentation, QDict *dict); 728 static void dump_qlist(int indentation, QList *list); 729 730 static void dump_qobject(int comp_indent, QObject *obj) 731 { 732 switch (qobject_type(obj)) { 733 case QTYPE_QNUM: { 734 QNum *value = qobject_to(QNum, obj); 735 char *tmp = qnum_to_string(value); 736 qemu_printf("%s", tmp); 737 g_free(tmp); 738 break; 739 } 740 case QTYPE_QSTRING: { 741 QString *value = qobject_to(QString, obj); 742 qemu_printf("%s", qstring_get_str(value)); 743 break; 744 } 745 case QTYPE_QDICT: { 746 QDict *value = qobject_to(QDict, obj); 747 dump_qdict(comp_indent, value); 748 break; 749 } 750 case QTYPE_QLIST: { 751 QList *value = qobject_to(QList, obj); 752 dump_qlist(comp_indent, value); 753 break; 754 } 755 case QTYPE_QBOOL: { 756 QBool *value = qobject_to(QBool, obj); 757 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false"); 758 break; 759 } 760 default: 761 abort(); 762 } 763 } 764 765 static void dump_qlist(int indentation, QList *list) 766 { 767 const QListEntry *entry; 768 int i = 0; 769 770 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { 771 QType type = qobject_type(entry->value); 772 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 773 qemu_printf("%*s[%i]:%c", indentation * 4, "", i, 774 composite ? '\n' : ' '); 775 dump_qobject(indentation + 1, entry->value); 776 if (!composite) { 777 qemu_printf("\n"); 778 } 779 } 780 } 781 782 static void dump_qdict(int indentation, QDict *dict) 783 { 784 const QDictEntry *entry; 785 786 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { 787 QType type = qobject_type(entry->value); 788 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 789 char *key = g_malloc(strlen(entry->key) + 1); 790 int i; 791 792 /* replace dashes with spaces in key (variable) names */ 793 for (i = 0; entry->key[i]; i++) { 794 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; 795 } 796 key[i] = 0; 797 qemu_printf("%*s%s:%c", indentation * 4, "", key, 798 composite ? '\n' : ' '); 799 dump_qobject(indentation + 1, entry->value); 800 if (!composite) { 801 qemu_printf("\n"); 802 } 803 g_free(key); 804 } 805 } 806 807 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec) 808 { 809 QObject *obj, *data; 810 Visitor *v = qobject_output_visitor_new(&obj); 811 812 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); 813 visit_complete(v, &obj); 814 data = qdict_get(qobject_to(QDict, obj), "data"); 815 dump_qobject(1, data); 816 qobject_unref(obj); 817 visit_free(v); 818 } 819 820 void bdrv_image_info_dump(ImageInfo *info) 821 { 822 char *size_buf, *dsize_buf; 823 if (!info->has_actual_size) { 824 dsize_buf = g_strdup("unavailable"); 825 } else { 826 dsize_buf = size_to_str(info->actual_size); 827 } 828 size_buf = size_to_str(info->virtual_size); 829 qemu_printf("image: %s\n" 830 "file format: %s\n" 831 "virtual size: %s (%" PRId64 " bytes)\n" 832 "disk size: %s\n", 833 info->filename, info->format, size_buf, 834 info->virtual_size, 835 dsize_buf); 836 g_free(size_buf); 837 g_free(dsize_buf); 838 839 if (info->has_encrypted && info->encrypted) { 840 qemu_printf("encrypted: yes\n"); 841 } 842 843 if (info->has_cluster_size) { 844 qemu_printf("cluster_size: %" PRId64 "\n", 845 info->cluster_size); 846 } 847 848 if (info->has_dirty_flag && info->dirty_flag) { 849 qemu_printf("cleanly shut down: no\n"); 850 } 851 852 if (info->has_backing_filename) { 853 qemu_printf("backing file: %s", info->backing_filename); 854 if (!info->has_full_backing_filename) { 855 qemu_printf(" (cannot determine actual path)"); 856 } else if (strcmp(info->backing_filename, 857 info->full_backing_filename) != 0) { 858 qemu_printf(" (actual path: %s)", info->full_backing_filename); 859 } 860 qemu_printf("\n"); 861 if (info->has_backing_filename_format) { 862 qemu_printf("backing file format: %s\n", 863 info->backing_filename_format); 864 } 865 } 866 867 if (info->has_snapshots) { 868 SnapshotInfoList *elem; 869 870 qemu_printf("Snapshot list:\n"); 871 bdrv_snapshot_dump(NULL); 872 qemu_printf("\n"); 873 874 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but 875 * we convert to the block layer's native QEMUSnapshotInfo for now. 876 */ 877 for (elem = info->snapshots; elem; elem = elem->next) { 878 QEMUSnapshotInfo sn = { 879 .vm_state_size = elem->value->vm_state_size, 880 .date_sec = elem->value->date_sec, 881 .date_nsec = elem->value->date_nsec, 882 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + 883 elem->value->vm_clock_nsec, 884 }; 885 886 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); 887 pstrcpy(sn.name, sizeof(sn.name), elem->value->name); 888 bdrv_snapshot_dump(&sn); 889 qemu_printf("\n"); 890 } 891 } 892 893 if (info->has_format_specific) { 894 qemu_printf("Format specific information:\n"); 895 bdrv_image_info_specific_dump(info->format_specific); 896 } 897 } 898