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