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 "block/qapi.h" 26 #include "block/block_int.h" 27 #include "qmp-commands.h" 28 #include "qapi-visit.h" 29 #include "qapi/qmp-output-visitor.h" 30 #include "qapi/qmp/types.h" 31 #include "sysemu/block-backend.h" 32 33 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs) 34 { 35 BlockDeviceInfo *info = g_malloc0(sizeof(*info)); 36 37 info->file = g_strdup(bs->filename); 38 info->ro = bs->read_only; 39 info->drv = g_strdup(bs->drv->format_name); 40 info->encrypted = bs->encrypted; 41 info->encryption_key_missing = bdrv_key_required(bs); 42 43 if (bs->node_name[0]) { 44 info->has_node_name = true; 45 info->node_name = g_strdup(bs->node_name); 46 } 47 48 if (bs->backing_file[0]) { 49 info->has_backing_file = true; 50 info->backing_file = g_strdup(bs->backing_file); 51 } 52 53 info->backing_file_depth = bdrv_get_backing_file_depth(bs); 54 info->detect_zeroes = bs->detect_zeroes; 55 56 if (bs->io_limits_enabled) { 57 ThrottleConfig cfg; 58 throttle_get_config(&bs->throttle_state, &cfg); 59 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; 60 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; 61 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; 62 63 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; 64 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; 65 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; 66 67 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 68 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 69 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 70 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 71 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 72 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 73 74 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 75 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 76 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 77 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 78 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 79 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 80 81 info->has_iops_size = cfg.op_size; 82 info->iops_size = cfg.op_size; 83 } 84 85 return info; 86 } 87 88 /* 89 * Returns 0 on success, with *p_list either set to describe snapshot 90 * information, or NULL because there are no snapshots. Returns -errno on 91 * error, with *p_list untouched. 92 */ 93 int bdrv_query_snapshot_info_list(BlockDriverState *bs, 94 SnapshotInfoList **p_list, 95 Error **errp) 96 { 97 int i, sn_count; 98 QEMUSnapshotInfo *sn_tab = NULL; 99 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL; 100 SnapshotInfo *info; 101 102 sn_count = bdrv_snapshot_list(bs, &sn_tab); 103 if (sn_count < 0) { 104 const char *dev = bdrv_get_device_name(bs); 105 switch (sn_count) { 106 case -ENOMEDIUM: 107 error_setg(errp, "Device '%s' is not inserted", dev); 108 break; 109 case -ENOTSUP: 110 error_setg(errp, 111 "Device '%s' does not support internal snapshots", 112 dev); 113 break; 114 default: 115 error_setg_errno(errp, -sn_count, 116 "Can't list snapshots of device '%s'", dev); 117 break; 118 } 119 return sn_count; 120 } 121 122 for (i = 0; i < sn_count; i++) { 123 info = g_new0(SnapshotInfo, 1); 124 info->id = g_strdup(sn_tab[i].id_str); 125 info->name = g_strdup(sn_tab[i].name); 126 info->vm_state_size = sn_tab[i].vm_state_size; 127 info->date_sec = sn_tab[i].date_sec; 128 info->date_nsec = sn_tab[i].date_nsec; 129 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; 130 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; 131 132 info_list = g_new0(SnapshotInfoList, 1); 133 info_list->value = info; 134 135 /* XXX: waiting for the qapi to support qemu-queue.h types */ 136 if (!cur_item) { 137 head = cur_item = info_list; 138 } else { 139 cur_item->next = info_list; 140 cur_item = info_list; 141 } 142 143 } 144 145 g_free(sn_tab); 146 *p_list = head; 147 return 0; 148 } 149 150 /** 151 * bdrv_query_image_info: 152 * @bs: block device to examine 153 * @p_info: location to store image information 154 * @errp: location to store error information 155 * 156 * Store "flat" image information in @p_info. 157 * 158 * "Flat" means it does *not* query backing image information, 159 * i.e. (*pinfo)->has_backing_image will be set to false and 160 * (*pinfo)->backing_image to NULL even when the image does in fact have 161 * a backing image. 162 * 163 * @p_info will be set only on success. On error, store error in @errp. 164 */ 165 void bdrv_query_image_info(BlockDriverState *bs, 166 ImageInfo **p_info, 167 Error **errp) 168 { 169 int64_t size; 170 const char *backing_filename; 171 char backing_filename2[1024]; 172 BlockDriverInfo bdi; 173 int ret; 174 Error *err = NULL; 175 ImageInfo *info; 176 177 size = bdrv_getlength(bs); 178 if (size < 0) { 179 error_setg_errno(errp, -size, "Can't get size of device '%s'", 180 bdrv_get_device_name(bs)); 181 return; 182 } 183 184 info = g_new0(ImageInfo, 1); 185 info->filename = g_strdup(bs->filename); 186 info->format = g_strdup(bdrv_get_format_name(bs)); 187 info->virtual_size = size; 188 info->actual_size = bdrv_get_allocated_file_size(bs); 189 info->has_actual_size = info->actual_size >= 0; 190 if (bdrv_is_encrypted(bs)) { 191 info->encrypted = true; 192 info->has_encrypted = true; 193 } 194 if (bdrv_get_info(bs, &bdi) >= 0) { 195 if (bdi.cluster_size != 0) { 196 info->cluster_size = bdi.cluster_size; 197 info->has_cluster_size = true; 198 } 199 info->dirty_flag = bdi.is_dirty; 200 info->has_dirty_flag = true; 201 } 202 info->format_specific = bdrv_get_specific_info(bs); 203 info->has_format_specific = info->format_specific != NULL; 204 205 backing_filename = bs->backing_file; 206 if (backing_filename[0] != '\0') { 207 info->backing_filename = g_strdup(backing_filename); 208 info->has_backing_filename = true; 209 bdrv_get_full_backing_filename(bs, backing_filename2, 210 sizeof(backing_filename2)); 211 212 if (strcmp(backing_filename, backing_filename2) != 0) { 213 info->full_backing_filename = 214 g_strdup(backing_filename2); 215 info->has_full_backing_filename = true; 216 } 217 218 if (bs->backing_format[0]) { 219 info->backing_filename_format = g_strdup(bs->backing_format); 220 info->has_backing_filename_format = true; 221 } 222 } 223 224 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); 225 switch (ret) { 226 case 0: 227 if (info->snapshots) { 228 info->has_snapshots = true; 229 } 230 break; 231 /* recoverable error */ 232 case -ENOMEDIUM: 233 case -ENOTSUP: 234 error_free(err); 235 break; 236 default: 237 error_propagate(errp, err); 238 qapi_free_ImageInfo(info); 239 return; 240 } 241 242 *p_info = info; 243 } 244 245 /* @p_info will be set only on success. */ 246 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, 247 Error **errp) 248 { 249 BlockInfo *info = g_malloc0(sizeof(*info)); 250 BlockDriverState *bs = blk_bs(blk); 251 BlockDriverState *bs0; 252 ImageInfo **p_image_info; 253 Error *local_err = NULL; 254 info->device = g_strdup(blk_name(blk)); 255 info->type = g_strdup("unknown"); 256 info->locked = blk_dev_is_medium_locked(blk); 257 info->removable = blk_dev_has_removable_media(blk); 258 259 if (blk_dev_has_removable_media(blk)) { 260 info->has_tray_open = true; 261 info->tray_open = blk_dev_is_tray_open(blk); 262 } 263 264 if (bdrv_iostatus_is_enabled(bs)) { 265 info->has_io_status = true; 266 info->io_status = bs->iostatus; 267 } 268 269 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { 270 info->has_dirty_bitmaps = true; 271 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); 272 } 273 274 if (bs->drv) { 275 info->has_inserted = true; 276 info->inserted = bdrv_block_device_info(bs); 277 278 bs0 = bs; 279 p_image_info = &info->inserted->image; 280 while (1) { 281 bdrv_query_image_info(bs0, p_image_info, &local_err); 282 if (local_err) { 283 error_propagate(errp, local_err); 284 goto err; 285 } 286 if (bs0->drv && bs0->backing_hd) { 287 bs0 = bs0->backing_hd; 288 (*p_image_info)->has_backing_image = true; 289 p_image_info = &((*p_image_info)->backing_image); 290 } else { 291 break; 292 } 293 } 294 } 295 296 *p_info = info; 297 return; 298 299 err: 300 qapi_free_BlockInfo(info); 301 } 302 303 static BlockStats *bdrv_query_stats(const BlockDriverState *bs, 304 bool query_backing) 305 { 306 BlockStats *s; 307 308 s = g_malloc0(sizeof(*s)); 309 310 if (bdrv_get_device_name(bs)[0]) { 311 s->has_device = true; 312 s->device = g_strdup(bdrv_get_device_name(bs)); 313 } 314 315 if (bdrv_get_node_name(bs)[0]) { 316 s->has_node_name = true; 317 s->node_name = g_strdup(bdrv_get_node_name(bs)); 318 } 319 320 s->stats = g_malloc0(sizeof(*s->stats)); 321 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ]; 322 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE]; 323 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ]; 324 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE]; 325 s->stats->wr_highest_offset = 326 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE; 327 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH]; 328 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE]; 329 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ]; 330 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH]; 331 332 if (bs->file) { 333 s->has_parent = true; 334 s->parent = bdrv_query_stats(bs->file, query_backing); 335 } 336 337 if (query_backing && bs->backing_hd) { 338 s->has_backing = true; 339 s->backing = bdrv_query_stats(bs->backing_hd, query_backing); 340 } 341 342 return s; 343 } 344 345 BlockInfoList *qmp_query_block(Error **errp) 346 { 347 BlockInfoList *head = NULL, **p_next = &head; 348 BlockBackend *blk; 349 Error *local_err = NULL; 350 351 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { 352 BlockInfoList *info = g_malloc0(sizeof(*info)); 353 bdrv_query_info(blk, &info->value, &local_err); 354 if (local_err) { 355 error_propagate(errp, local_err); 356 goto err; 357 } 358 359 *p_next = info; 360 p_next = &info->next; 361 } 362 363 return head; 364 365 err: 366 qapi_free_BlockInfoList(head); 367 return NULL; 368 } 369 370 BlockStatsList *qmp_query_blockstats(bool has_query_nodes, 371 bool query_nodes, 372 Error **errp) 373 { 374 BlockStatsList *head = NULL, **p_next = &head; 375 BlockDriverState *bs = NULL; 376 377 /* Just to be safe if query_nodes is not always initialized */ 378 query_nodes = has_query_nodes && query_nodes; 379 380 while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) { 381 BlockStatsList *info = g_malloc0(sizeof(*info)); 382 AioContext *ctx = bdrv_get_aio_context(bs); 383 384 aio_context_acquire(ctx); 385 info->value = bdrv_query_stats(bs, !query_nodes); 386 aio_context_release(ctx); 387 388 *p_next = info; 389 p_next = &info->next; 390 } 391 392 return head; 393 } 394 395 #define NB_SUFFIXES 4 396 397 static char *get_human_readable_size(char *buf, int buf_size, int64_t size) 398 { 399 static const char suffixes[NB_SUFFIXES] = "KMGT"; 400 int64_t base; 401 int i; 402 403 if (size <= 999) { 404 snprintf(buf, buf_size, "%" PRId64, size); 405 } else { 406 base = 1024; 407 for (i = 0; i < NB_SUFFIXES; i++) { 408 if (size < (10 * base)) { 409 snprintf(buf, buf_size, "%0.1f%c", 410 (double)size / base, 411 suffixes[i]); 412 break; 413 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 414 snprintf(buf, buf_size, "%" PRId64 "%c", 415 ((size + (base >> 1)) / base), 416 suffixes[i]); 417 break; 418 } 419 base = base * 1024; 420 } 421 } 422 return buf; 423 } 424 425 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f, 426 QEMUSnapshotInfo *sn) 427 { 428 char buf1[128], date_buf[128], clock_buf[128]; 429 struct tm tm; 430 time_t ti; 431 int64_t secs; 432 433 if (!sn) { 434 func_fprintf(f, 435 "%-10s%-20s%7s%20s%15s", 436 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 437 } else { 438 ti = sn->date_sec; 439 localtime_r(&ti, &tm); 440 strftime(date_buf, sizeof(date_buf), 441 "%Y-%m-%d %H:%M:%S", &tm); 442 secs = sn->vm_clock_nsec / 1000000000; 443 snprintf(clock_buf, sizeof(clock_buf), 444 "%02d:%02d:%02d.%03d", 445 (int)(secs / 3600), 446 (int)((secs / 60) % 60), 447 (int)(secs % 60), 448 (int)((sn->vm_clock_nsec / 1000000) % 1000)); 449 func_fprintf(f, 450 "%-10s%-20s%7s%20s%15s", 451 sn->id_str, sn->name, 452 get_human_readable_size(buf1, sizeof(buf1), 453 sn->vm_state_size), 454 date_buf, 455 clock_buf); 456 } 457 } 458 459 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, 460 QDict *dict); 461 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, 462 QList *list); 463 464 static void dump_qobject(fprintf_function func_fprintf, void *f, 465 int comp_indent, QObject *obj) 466 { 467 switch (qobject_type(obj)) { 468 case QTYPE_QINT: { 469 QInt *value = qobject_to_qint(obj); 470 func_fprintf(f, "%" PRId64, qint_get_int(value)); 471 break; 472 } 473 case QTYPE_QSTRING: { 474 QString *value = qobject_to_qstring(obj); 475 func_fprintf(f, "%s", qstring_get_str(value)); 476 break; 477 } 478 case QTYPE_QDICT: { 479 QDict *value = qobject_to_qdict(obj); 480 dump_qdict(func_fprintf, f, comp_indent, value); 481 break; 482 } 483 case QTYPE_QLIST: { 484 QList *value = qobject_to_qlist(obj); 485 dump_qlist(func_fprintf, f, comp_indent, value); 486 break; 487 } 488 case QTYPE_QFLOAT: { 489 QFloat *value = qobject_to_qfloat(obj); 490 func_fprintf(f, "%g", qfloat_get_double(value)); 491 break; 492 } 493 case QTYPE_QBOOL: { 494 QBool *value = qobject_to_qbool(obj); 495 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false"); 496 break; 497 } 498 case QTYPE_QERROR: { 499 QString *value = qerror_human((QError *)obj); 500 func_fprintf(f, "%s", qstring_get_str(value)); 501 QDECREF(value); 502 break; 503 } 504 case QTYPE_NONE: 505 break; 506 case QTYPE_MAX: 507 default: 508 abort(); 509 } 510 } 511 512 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation, 513 QList *list) 514 { 515 const QListEntry *entry; 516 int i = 0; 517 518 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { 519 qtype_code type = qobject_type(entry->value); 520 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 521 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: "; 522 523 func_fprintf(f, format, indentation * 4, "", i); 524 dump_qobject(func_fprintf, f, indentation + 1, entry->value); 525 if (!composite) { 526 func_fprintf(f, "\n"); 527 } 528 } 529 } 530 531 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, 532 QDict *dict) 533 { 534 const QDictEntry *entry; 535 536 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { 537 qtype_code type = qobject_type(entry->value); 538 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 539 const char *format = composite ? "%*s%s:\n" : "%*s%s: "; 540 char key[strlen(entry->key) + 1]; 541 int i; 542 543 /* replace dashes with spaces in key (variable) names */ 544 for (i = 0; entry->key[i]; i++) { 545 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; 546 } 547 key[i] = 0; 548 549 func_fprintf(f, format, indentation * 4, "", key); 550 dump_qobject(func_fprintf, f, indentation + 1, entry->value); 551 if (!composite) { 552 func_fprintf(f, "\n"); 553 } 554 } 555 } 556 557 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, 558 ImageInfoSpecific *info_spec) 559 { 560 QmpOutputVisitor *ov = qmp_output_visitor_new(); 561 QObject *obj, *data; 562 563 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL, 564 &error_abort); 565 obj = qmp_output_get_qobject(ov); 566 assert(qobject_type(obj) == QTYPE_QDICT); 567 data = qdict_get(qobject_to_qdict(obj), "data"); 568 dump_qobject(func_fprintf, f, 1, data); 569 qmp_output_visitor_cleanup(ov); 570 } 571 572 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f, 573 ImageInfo *info) 574 { 575 char size_buf[128], dsize_buf[128]; 576 if (!info->has_actual_size) { 577 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); 578 } else { 579 get_human_readable_size(dsize_buf, sizeof(dsize_buf), 580 info->actual_size); 581 } 582 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); 583 func_fprintf(f, 584 "image: %s\n" 585 "file format: %s\n" 586 "virtual size: %s (%" PRId64 " bytes)\n" 587 "disk size: %s\n", 588 info->filename, info->format, size_buf, 589 info->virtual_size, 590 dsize_buf); 591 592 if (info->has_encrypted && info->encrypted) { 593 func_fprintf(f, "encrypted: yes\n"); 594 } 595 596 if (info->has_cluster_size) { 597 func_fprintf(f, "cluster_size: %" PRId64 "\n", 598 info->cluster_size); 599 } 600 601 if (info->has_dirty_flag && info->dirty_flag) { 602 func_fprintf(f, "cleanly shut down: no\n"); 603 } 604 605 if (info->has_backing_filename) { 606 func_fprintf(f, "backing file: %s", info->backing_filename); 607 if (info->has_full_backing_filename) { 608 func_fprintf(f, " (actual path: %s)", info->full_backing_filename); 609 } 610 func_fprintf(f, "\n"); 611 if (info->has_backing_filename_format) { 612 func_fprintf(f, "backing file format: %s\n", 613 info->backing_filename_format); 614 } 615 } 616 617 if (info->has_snapshots) { 618 SnapshotInfoList *elem; 619 620 func_fprintf(f, "Snapshot list:\n"); 621 bdrv_snapshot_dump(func_fprintf, f, NULL); 622 func_fprintf(f, "\n"); 623 624 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but 625 * we convert to the block layer's native QEMUSnapshotInfo for now. 626 */ 627 for (elem = info->snapshots; elem; elem = elem->next) { 628 QEMUSnapshotInfo sn = { 629 .vm_state_size = elem->value->vm_state_size, 630 .date_sec = elem->value->date_sec, 631 .date_nsec = elem->value->date_nsec, 632 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + 633 elem->value->vm_clock_nsec, 634 }; 635 636 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); 637 pstrcpy(sn.name, sizeof(sn.name), elem->value->name); 638 bdrv_snapshot_dump(func_fprintf, f, &sn); 639 func_fprintf(f, "\n"); 640 } 641 } 642 643 if (info->has_format_specific) { 644 func_fprintf(f, "Format specific information:\n"); 645 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific); 646 } 647 } 648