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