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 "block/qapi.h" 27 #include "block/block_int.h" 28 #include "block/throttle-groups.h" 29 #include "block/write-threshold.h" 30 #include "qmp-commands.h" 31 #include "qapi-visit.h" 32 #include "qapi/qobject-output-visitor.h" 33 #include "qapi/qmp/types.h" 34 #include "sysemu/block-backend.h" 35 #include "qemu/cutils.h" 36 37 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, 38 BlockDriverState *bs, Error **errp) 39 { 40 ImageInfo **p_image_info; 41 BlockDriverState *bs0; 42 BlockDeviceInfo *info; 43 44 if (!bs->drv) { 45 error_setg(errp, "Block device %s is ejected", bs->node_name); 46 return NULL; 47 } 48 49 info = g_malloc0(sizeof(*info)); 50 info->file = g_strdup(bs->filename); 51 info->ro = bs->read_only; 52 info->drv = g_strdup(bs->drv->format_name); 53 info->encrypted = bs->encrypted; 54 info->encryption_key_missing = false; 55 56 info->cache = g_new(BlockdevCacheInfo, 1); 57 *info->cache = (BlockdevCacheInfo) { 58 .writeback = blk ? blk_enable_write_cache(blk) : true, 59 .direct = !!(bs->open_flags & BDRV_O_NOCACHE), 60 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH), 61 }; 62 63 if (bs->node_name[0]) { 64 info->has_node_name = true; 65 info->node_name = g_strdup(bs->node_name); 66 } 67 68 if (bs->backing_file[0]) { 69 info->has_backing_file = true; 70 info->backing_file = g_strdup(bs->backing_file); 71 } 72 73 info->detect_zeroes = bs->detect_zeroes; 74 75 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) { 76 ThrottleConfig cfg; 77 BlockBackendPublic *blkp = blk_get_public(blk); 78 79 throttle_group_get_config(&blkp->throttle_group_member, &cfg); 80 81 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; 82 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; 83 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; 84 85 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; 86 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; 87 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; 88 89 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 90 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 91 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 92 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 93 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 94 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 95 96 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 97 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 98 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 99 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 100 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 101 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 102 103 info->has_bps_max_length = info->has_bps_max; 104 info->bps_max_length = 105 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length; 106 info->has_bps_rd_max_length = info->has_bps_rd_max; 107 info->bps_rd_max_length = 108 cfg.buckets[THROTTLE_BPS_READ].burst_length; 109 info->has_bps_wr_max_length = info->has_bps_wr_max; 110 info->bps_wr_max_length = 111 cfg.buckets[THROTTLE_BPS_WRITE].burst_length; 112 113 info->has_iops_max_length = info->has_iops_max; 114 info->iops_max_length = 115 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length; 116 info->has_iops_rd_max_length = info->has_iops_rd_max; 117 info->iops_rd_max_length = 118 cfg.buckets[THROTTLE_OPS_READ].burst_length; 119 info->has_iops_wr_max_length = info->has_iops_wr_max; 120 info->iops_wr_max_length = 121 cfg.buckets[THROTTLE_OPS_WRITE].burst_length; 122 123 info->has_iops_size = cfg.op_size; 124 info->iops_size = cfg.op_size; 125 126 info->has_group = true; 127 info->group = 128 g_strdup(throttle_group_get_name(&blkp->throttle_group_member)); 129 } 130 131 info->write_threshold = bdrv_write_threshold_get(bs); 132 133 bs0 = bs; 134 p_image_info = &info->image; 135 info->backing_file_depth = 0; 136 while (1) { 137 Error *local_err = NULL; 138 bdrv_query_image_info(bs0, p_image_info, &local_err); 139 if (local_err) { 140 error_propagate(errp, local_err); 141 qapi_free_BlockDeviceInfo(info); 142 return NULL; 143 } 144 145 if (bs0->drv && bs0->backing) { 146 info->backing_file_depth++; 147 bs0 = bs0->backing->bs; 148 (*p_image_info)->has_backing_image = true; 149 p_image_info = &((*p_image_info)->backing_image); 150 } else { 151 break; 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 while (blk && bs0->drv && bs0->implicit) { 157 bs0 = backing_bs(bs0); 158 assert(bs0); 159 } 160 } 161 162 return info; 163 } 164 165 /* 166 * Returns 0 on success, with *p_list either set to describe snapshot 167 * information, or NULL because there are no snapshots. Returns -errno on 168 * error, with *p_list untouched. 169 */ 170 int bdrv_query_snapshot_info_list(BlockDriverState *bs, 171 SnapshotInfoList **p_list, 172 Error **errp) 173 { 174 int i, sn_count; 175 QEMUSnapshotInfo *sn_tab = NULL; 176 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL; 177 SnapshotInfo *info; 178 179 sn_count = bdrv_snapshot_list(bs, &sn_tab); 180 if (sn_count < 0) { 181 const char *dev = bdrv_get_device_name(bs); 182 switch (sn_count) { 183 case -ENOMEDIUM: 184 error_setg(errp, "Device '%s' is not inserted", dev); 185 break; 186 case -ENOTSUP: 187 error_setg(errp, 188 "Device '%s' does not support internal snapshots", 189 dev); 190 break; 191 default: 192 error_setg_errno(errp, -sn_count, 193 "Can't list snapshots of device '%s'", dev); 194 break; 195 } 196 return sn_count; 197 } 198 199 for (i = 0; i < sn_count; i++) { 200 info = g_new0(SnapshotInfo, 1); 201 info->id = g_strdup(sn_tab[i].id_str); 202 info->name = g_strdup(sn_tab[i].name); 203 info->vm_state_size = sn_tab[i].vm_state_size; 204 info->date_sec = sn_tab[i].date_sec; 205 info->date_nsec = sn_tab[i].date_nsec; 206 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; 207 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; 208 209 info_list = g_new0(SnapshotInfoList, 1); 210 info_list->value = info; 211 212 /* XXX: waiting for the qapi to support qemu-queue.h types */ 213 if (!cur_item) { 214 head = cur_item = info_list; 215 } else { 216 cur_item->next = info_list; 217 cur_item = info_list; 218 } 219 220 } 221 222 g_free(sn_tab); 223 *p_list = head; 224 return 0; 225 } 226 227 /** 228 * bdrv_query_image_info: 229 * @bs: block device to examine 230 * @p_info: location to store image information 231 * @errp: location to store error information 232 * 233 * Store "flat" image information in @p_info. 234 * 235 * "Flat" means it does *not* query backing image information, 236 * i.e. (*pinfo)->has_backing_image will be set to false and 237 * (*pinfo)->backing_image to NULL even when the image does in fact have 238 * a backing image. 239 * 240 * @p_info will be set only on success. On error, store error in @errp. 241 */ 242 void bdrv_query_image_info(BlockDriverState *bs, 243 ImageInfo **p_info, 244 Error **errp) 245 { 246 int64_t size; 247 const char *backing_filename; 248 BlockDriverInfo bdi; 249 int ret; 250 Error *err = NULL; 251 ImageInfo *info; 252 253 aio_context_acquire(bdrv_get_aio_context(bs)); 254 255 size = bdrv_getlength(bs); 256 if (size < 0) { 257 error_setg_errno(errp, -size, "Can't get image size '%s'", 258 bs->exact_filename); 259 goto out; 260 } 261 262 info = g_new0(ImageInfo, 1); 263 info->filename = g_strdup(bs->filename); 264 info->format = g_strdup(bdrv_get_format_name(bs)); 265 info->virtual_size = size; 266 info->actual_size = bdrv_get_allocated_file_size(bs); 267 info->has_actual_size = info->actual_size >= 0; 268 if (bdrv_is_encrypted(bs)) { 269 info->encrypted = true; 270 info->has_encrypted = true; 271 } 272 if (bdrv_get_info(bs, &bdi) >= 0) { 273 if (bdi.cluster_size != 0) { 274 info->cluster_size = bdi.cluster_size; 275 info->has_cluster_size = true; 276 } 277 info->dirty_flag = bdi.is_dirty; 278 info->has_dirty_flag = true; 279 } 280 info->format_specific = bdrv_get_specific_info(bs); 281 info->has_format_specific = info->format_specific != NULL; 282 283 backing_filename = bs->backing_file; 284 if (backing_filename[0] != '\0') { 285 char *backing_filename2 = g_malloc0(PATH_MAX); 286 info->backing_filename = g_strdup(backing_filename); 287 info->has_backing_filename = true; 288 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err); 289 if (err) { 290 /* Can't reconstruct the full backing filename, so we must omit 291 * this field and apply a Best Effort to this query. */ 292 g_free(backing_filename2); 293 backing_filename2 = NULL; 294 error_free(err); 295 err = NULL; 296 } 297 298 /* Always report the full_backing_filename if present, even if it's the 299 * same as backing_filename. That they are same is useful info. */ 300 if (backing_filename2) { 301 info->full_backing_filename = g_strdup(backing_filename2); 302 info->has_full_backing_filename = true; 303 } 304 305 if (bs->backing_format[0]) { 306 info->backing_filename_format = g_strdup(bs->backing_format); 307 info->has_backing_filename_format = true; 308 } 309 g_free(backing_filename2); 310 } 311 312 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); 313 switch (ret) { 314 case 0: 315 if (info->snapshots) { 316 info->has_snapshots = true; 317 } 318 break; 319 /* recoverable error */ 320 case -ENOMEDIUM: 321 case -ENOTSUP: 322 error_free(err); 323 break; 324 default: 325 error_propagate(errp, err); 326 qapi_free_ImageInfo(info); 327 goto out; 328 } 329 330 *p_info = info; 331 332 out: 333 aio_context_release(bdrv_get_aio_context(bs)); 334 } 335 336 /* @p_info will be set only on success. */ 337 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, 338 Error **errp) 339 { 340 BlockInfo *info = g_malloc0(sizeof(*info)); 341 BlockDriverState *bs = blk_bs(blk); 342 char *qdev; 343 344 /* Skip automatically inserted nodes that the user isn't aware of */ 345 while (bs && bs->drv && bs->implicit) { 346 bs = backing_bs(bs); 347 } 348 349 info->device = g_strdup(blk_name(blk)); 350 info->type = g_strdup("unknown"); 351 info->locked = blk_dev_is_medium_locked(blk); 352 info->removable = blk_dev_has_removable_media(blk); 353 354 qdev = blk_get_attached_dev_id(blk); 355 if (qdev && *qdev) { 356 info->has_qdev = true; 357 info->qdev = qdev; 358 } else { 359 g_free(qdev); 360 } 361 362 if (blk_dev_has_tray(blk)) { 363 info->has_tray_open = true; 364 info->tray_open = blk_dev_is_tray_open(blk); 365 } 366 367 if (blk_iostatus_is_enabled(blk)) { 368 info->has_io_status = true; 369 info->io_status = blk_iostatus(blk); 370 } 371 372 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) { 373 info->has_dirty_bitmaps = true; 374 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); 375 } 376 377 if (bs && bs->drv) { 378 info->has_inserted = true; 379 info->inserted = bdrv_block_device_info(blk, bs, errp); 380 if (info->inserted == NULL) { 381 goto err; 382 } 383 } 384 385 *p_info = info; 386 return; 387 388 err: 389 qapi_free_BlockInfo(info); 390 } 391 392 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) 393 { 394 BlockAcctStats *stats = blk_get_stats(blk); 395 BlockAcctTimedStats *ts = NULL; 396 397 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; 398 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; 399 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; 400 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; 401 402 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; 403 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; 404 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; 405 406 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; 407 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; 408 ds->invalid_flush_operations = 409 stats->invalid_ops[BLOCK_ACCT_FLUSH]; 410 411 ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; 412 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; 413 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; 414 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; 415 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; 416 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; 417 418 ds->has_idle_time_ns = stats->last_access_time_ns > 0; 419 if (ds->has_idle_time_ns) { 420 ds->idle_time_ns = block_acct_idle_time_ns(stats); 421 } 422 423 ds->account_invalid = stats->account_invalid; 424 ds->account_failed = stats->account_failed; 425 426 while ((ts = block_acct_interval_next(stats, ts))) { 427 BlockDeviceTimedStatsList *timed_stats = 428 g_malloc0(sizeof(*timed_stats)); 429 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); 430 timed_stats->next = ds->timed_stats; 431 timed_stats->value = dev_stats; 432 ds->timed_stats = timed_stats; 433 434 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; 435 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; 436 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; 437 438 dev_stats->interval_length = ts->interval_length; 439 440 dev_stats->min_rd_latency_ns = timed_average_min(rd); 441 dev_stats->max_rd_latency_ns = timed_average_max(rd); 442 dev_stats->avg_rd_latency_ns = timed_average_avg(rd); 443 444 dev_stats->min_wr_latency_ns = timed_average_min(wr); 445 dev_stats->max_wr_latency_ns = timed_average_max(wr); 446 dev_stats->avg_wr_latency_ns = timed_average_avg(wr); 447 448 dev_stats->min_flush_latency_ns = timed_average_min(fl); 449 dev_stats->max_flush_latency_ns = timed_average_max(fl); 450 dev_stats->avg_flush_latency_ns = timed_average_avg(fl); 451 452 dev_stats->avg_rd_queue_depth = 453 block_acct_queue_depth(ts, BLOCK_ACCT_READ); 454 dev_stats->avg_wr_queue_depth = 455 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); 456 } 457 } 458 459 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs, 460 bool blk_level) 461 { 462 BlockStats *s = NULL; 463 464 s = g_malloc0(sizeof(*s)); 465 s->stats = g_malloc0(sizeof(*s->stats)); 466 467 if (!bs) { 468 return s; 469 } 470 471 /* Skip automatically inserted nodes that the user isn't aware of in 472 * a BlockBackend-level command. Stay at the exact node for a node-level 473 * command. */ 474 while (blk_level && bs->drv && bs->implicit) { 475 bs = backing_bs(bs); 476 assert(bs); 477 } 478 479 if (bdrv_get_node_name(bs)[0]) { 480 s->has_node_name = true; 481 s->node_name = g_strdup(bdrv_get_node_name(bs)); 482 } 483 484 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset); 485 486 if (bs->file) { 487 s->has_parent = true; 488 s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level); 489 } 490 491 if (blk_level && bs->backing) { 492 s->has_backing = true; 493 s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level); 494 } 495 496 return s; 497 } 498 499 BlockInfoList *qmp_query_block(Error **errp) 500 { 501 BlockInfoList *head = NULL, **p_next = &head; 502 BlockBackend *blk; 503 Error *local_err = NULL; 504 505 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 506 BlockInfoList *info; 507 508 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 509 continue; 510 } 511 512 info = g_malloc0(sizeof(*info)); 513 bdrv_query_info(blk, &info->value, &local_err); 514 if (local_err) { 515 error_propagate(errp, local_err); 516 g_free(info); 517 qapi_free_BlockInfoList(head); 518 return NULL; 519 } 520 521 *p_next = info; 522 p_next = &info->next; 523 } 524 525 return head; 526 } 527 528 BlockStatsList *qmp_query_blockstats(bool has_query_nodes, 529 bool query_nodes, 530 Error **errp) 531 { 532 BlockStatsList *head = NULL, **p_next = &head; 533 BlockBackend *blk; 534 BlockDriverState *bs; 535 536 /* Just to be safe if query_nodes is not always initialized */ 537 if (has_query_nodes && query_nodes) { 538 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) { 539 BlockStatsList *info = g_malloc0(sizeof(*info)); 540 AioContext *ctx = bdrv_get_aio_context(bs); 541 542 aio_context_acquire(ctx); 543 info->value = bdrv_query_bds_stats(bs, false); 544 aio_context_release(ctx); 545 546 *p_next = info; 547 p_next = &info->next; 548 } 549 } else { 550 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 551 BlockStatsList *info = g_malloc0(sizeof(*info)); 552 AioContext *ctx = blk_get_aio_context(blk); 553 BlockStats *s; 554 555 aio_context_acquire(ctx); 556 s = bdrv_query_bds_stats(blk_bs(blk), true); 557 s->has_device = true; 558 s->device = g_strdup(blk_name(blk)); 559 bdrv_query_blk_stats(s->stats, blk); 560 aio_context_release(ctx); 561 562 info->value = s; 563 *p_next = info; 564 p_next = &info->next; 565 } 566 } 567 568 return head; 569 } 570 571 #define NB_SUFFIXES 4 572 573 static char *get_human_readable_size(char *buf, int buf_size, int64_t size) 574 { 575 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'}; 576 int64_t base; 577 int i; 578 579 if (size <= 999) { 580 snprintf(buf, buf_size, "%" PRId64, size); 581 } else { 582 base = 1024; 583 for (i = 0; i < NB_SUFFIXES; i++) { 584 if (size < (10 * base)) { 585 snprintf(buf, buf_size, "%0.1f%c", 586 (double)size / base, 587 suffixes[i]); 588 break; 589 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 590 snprintf(buf, buf_size, "%" PRId64 "%c", 591 ((size + (base >> 1)) / base), 592 suffixes[i]); 593 break; 594 } 595 base = base * 1024; 596 } 597 } 598 return buf; 599 } 600 601 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f, 602 QEMUSnapshotInfo *sn) 603 { 604 char buf1[128], date_buf[128], clock_buf[128]; 605 struct tm tm; 606 time_t ti; 607 int64_t secs; 608 609 if (!sn) { 610 func_fprintf(f, 611 "%-10s%-20s%7s%20s%15s", 612 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 613 } else { 614 ti = sn->date_sec; 615 localtime_r(&ti, &tm); 616 strftime(date_buf, sizeof(date_buf), 617 "%Y-%m-%d %H:%M:%S", &tm); 618 secs = sn->vm_clock_nsec / 1000000000; 619 snprintf(clock_buf, sizeof(clock_buf), 620 "%02d:%02d:%02d.%03d", 621 (int)(secs / 3600), 622 (int)((secs / 60) % 60), 623 (int)(secs % 60), 624 (int)((sn->vm_clock_nsec / 1000000) % 1000)); 625 func_fprintf(f, 626 "%-10s%-20s%7s%20s%15s", 627 sn->id_str, sn->name, 628 get_human_readable_size(buf1, sizeof(buf1), 629 sn->vm_state_size), 630 date_buf, 631 clock_buf); 632 } 633 } 634 635 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, 636 QDict *dict); 637 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, 638 QList *list); 639 640 static void dump_qobject(fprintf_function func_fprintf, void *f, 641 int comp_indent, QObject *obj) 642 { 643 switch (qobject_type(obj)) { 644 case QTYPE_QNUM: { 645 QNum *value = qobject_to_qnum(obj); 646 char *tmp = qnum_to_string(value); 647 func_fprintf(f, "%s", tmp); 648 g_free(tmp); 649 break; 650 } 651 case QTYPE_QSTRING: { 652 QString *value = qobject_to_qstring(obj); 653 func_fprintf(f, "%s", qstring_get_str(value)); 654 break; 655 } 656 case QTYPE_QDICT: { 657 QDict *value = qobject_to_qdict(obj); 658 dump_qdict(func_fprintf, f, comp_indent, value); 659 break; 660 } 661 case QTYPE_QLIST: { 662 QList *value = qobject_to_qlist(obj); 663 dump_qlist(func_fprintf, f, comp_indent, value); 664 break; 665 } 666 case QTYPE_QBOOL: { 667 QBool *value = qobject_to_qbool(obj); 668 func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false"); 669 break; 670 } 671 default: 672 abort(); 673 } 674 } 675 676 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, 677 QList *list) 678 { 679 const QListEntry *entry; 680 int i = 0; 681 682 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { 683 QType type = qobject_type(entry->value); 684 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 685 func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, 686 composite ? '\n' : ' '); 687 dump_qobject(func_fprintf, f, indentation + 1, entry->value); 688 if (!composite) { 689 func_fprintf(f, "\n"); 690 } 691 } 692 } 693 694 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, 695 QDict *dict) 696 { 697 const QDictEntry *entry; 698 699 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { 700 QType type = qobject_type(entry->value); 701 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 702 char *key = g_malloc(strlen(entry->key) + 1); 703 int i; 704 705 /* replace dashes with spaces in key (variable) names */ 706 for (i = 0; entry->key[i]; i++) { 707 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; 708 } 709 key[i] = 0; 710 func_fprintf(f, "%*s%s:%c", indentation * 4, "", key, 711 composite ? '\n' : ' '); 712 dump_qobject(func_fprintf, f, indentation + 1, entry->value); 713 if (!composite) { 714 func_fprintf(f, "\n"); 715 } 716 g_free(key); 717 } 718 } 719 720 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, 721 ImageInfoSpecific *info_spec) 722 { 723 QObject *obj, *data; 724 Visitor *v = qobject_output_visitor_new(&obj); 725 726 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); 727 visit_complete(v, &obj); 728 data = qdict_get(qobject_to_qdict(obj), "data"); 729 dump_qobject(func_fprintf, f, 1, data); 730 qobject_decref(obj); 731 visit_free(v); 732 } 733 734 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f, 735 ImageInfo *info) 736 { 737 char size_buf[128], dsize_buf[128]; 738 if (!info->has_actual_size) { 739 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); 740 } else { 741 get_human_readable_size(dsize_buf, sizeof(dsize_buf), 742 info->actual_size); 743 } 744 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); 745 func_fprintf(f, 746 "image: %s\n" 747 "file format: %s\n" 748 "virtual size: %s (%" PRId64 " bytes)\n" 749 "disk size: %s\n", 750 info->filename, info->format, size_buf, 751 info->virtual_size, 752 dsize_buf); 753 754 if (info->has_encrypted && info->encrypted) { 755 func_fprintf(f, "encrypted: yes\n"); 756 } 757 758 if (info->has_cluster_size) { 759 func_fprintf(f, "cluster_size: %" PRId64 "\n", 760 info->cluster_size); 761 } 762 763 if (info->has_dirty_flag && info->dirty_flag) { 764 func_fprintf(f, "cleanly shut down: no\n"); 765 } 766 767 if (info->has_backing_filename) { 768 func_fprintf(f, "backing file: %s", info->backing_filename); 769 if (!info->has_full_backing_filename) { 770 func_fprintf(f, " (cannot determine actual path)"); 771 } else if (strcmp(info->backing_filename, 772 info->full_backing_filename) != 0) { 773 func_fprintf(f, " (actual path: %s)", info->full_backing_filename); 774 } 775 func_fprintf(f, "\n"); 776 if (info->has_backing_filename_format) { 777 func_fprintf(f, "backing file format: %s\n", 778 info->backing_filename_format); 779 } 780 } 781 782 if (info->has_snapshots) { 783 SnapshotInfoList *elem; 784 785 func_fprintf(f, "Snapshot list:\n"); 786 bdrv_snapshot_dump(func_fprintf, f, NULL); 787 func_fprintf(f, "\n"); 788 789 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but 790 * we convert to the block layer's native QEMUSnapshotInfo for now. 791 */ 792 for (elem = info->snapshots; elem; elem = elem->next) { 793 QEMUSnapshotInfo sn = { 794 .vm_state_size = elem->value->vm_state_size, 795 .date_sec = elem->value->date_sec, 796 .date_nsec = elem->value->date_nsec, 797 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + 798 elem->value->vm_clock_nsec, 799 }; 800 801 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); 802 pstrcpy(sn.name, sizeof(sn.name), elem->value->name); 803 bdrv_snapshot_dump(func_fprintf, f, &sn); 804 func_fprintf(f, "\n"); 805 } 806 } 807 808 if (info->has_format_specific) { 809 func_fprintf(f, "Format specific information:\n"); 810 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific); 811 } 812 } 813