usb: usb-hid QOMify
[qemu/ar7.git] / block / qapi.c
blob063dd1bc1f3f9167167e0114d79bf0e0d90fab43
1 /*
2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "block/qapi.h"
26 #include "block/block_int.h"
27 #include "block/write-threshold.h"
28 #include "qmp-commands.h"
29 #include "qapi-visit.h"
30 #include "qapi/qmp-output-visitor.h"
31 #include "qapi/qmp/types.h"
32 #include "sysemu/block-backend.h"
34 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
36 ImageInfo **p_image_info;
37 BlockDriverState *bs0;
38 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
40 info->file = g_strdup(bs->filename);
41 info->ro = bs->read_only;
42 info->drv = g_strdup(bs->drv->format_name);
43 info->encrypted = bs->encrypted;
44 info->encryption_key_missing = bdrv_key_required(bs);
46 info->cache = g_new(BlockdevCacheInfo, 1);
47 *info->cache = (BlockdevCacheInfo) {
48 .writeback = bdrv_enable_write_cache(bs),
49 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
50 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
53 if (bs->node_name[0]) {
54 info->has_node_name = true;
55 info->node_name = g_strdup(bs->node_name);
58 if (bs->backing_file[0]) {
59 info->has_backing_file = true;
60 info->backing_file = g_strdup(bs->backing_file);
63 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
64 info->detect_zeroes = bs->detect_zeroes;
66 if (bs->io_limits_enabled) {
67 ThrottleConfig cfg;
68 throttle_get_config(&bs->throttle_state, &cfg);
69 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
70 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
71 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
73 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
74 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
75 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
77 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
78 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
79 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
80 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
81 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
82 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
84 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
85 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
86 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
87 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
88 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
89 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
91 info->has_iops_size = cfg.op_size;
92 info->iops_size = cfg.op_size;
95 info->write_threshold = bdrv_write_threshold_get(bs);
97 bs0 = bs;
98 p_image_info = &info->image;
99 while (1) {
100 Error *local_err = NULL;
101 bdrv_query_image_info(bs0, p_image_info, &local_err);
102 if (local_err) {
103 error_propagate(errp, local_err);
104 qapi_free_BlockDeviceInfo(info);
105 return NULL;
107 if (bs0->drv && bs0->backing_hd) {
108 bs0 = bs0->backing_hd;
109 (*p_image_info)->has_backing_image = true;
110 p_image_info = &((*p_image_info)->backing_image);
111 } else {
112 break;
116 return info;
120 * Returns 0 on success, with *p_list either set to describe snapshot
121 * information, or NULL because there are no snapshots. Returns -errno on
122 * error, with *p_list untouched.
124 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
125 SnapshotInfoList **p_list,
126 Error **errp)
128 int i, sn_count;
129 QEMUSnapshotInfo *sn_tab = NULL;
130 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
131 SnapshotInfo *info;
133 sn_count = bdrv_snapshot_list(bs, &sn_tab);
134 if (sn_count < 0) {
135 const char *dev = bdrv_get_device_name(bs);
136 switch (sn_count) {
137 case -ENOMEDIUM:
138 error_setg(errp, "Device '%s' is not inserted", dev);
139 break;
140 case -ENOTSUP:
141 error_setg(errp,
142 "Device '%s' does not support internal snapshots",
143 dev);
144 break;
145 default:
146 error_setg_errno(errp, -sn_count,
147 "Can't list snapshots of device '%s'", dev);
148 break;
150 return sn_count;
153 for (i = 0; i < sn_count; i++) {
154 info = g_new0(SnapshotInfo, 1);
155 info->id = g_strdup(sn_tab[i].id_str);
156 info->name = g_strdup(sn_tab[i].name);
157 info->vm_state_size = sn_tab[i].vm_state_size;
158 info->date_sec = sn_tab[i].date_sec;
159 info->date_nsec = sn_tab[i].date_nsec;
160 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
161 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
163 info_list = g_new0(SnapshotInfoList, 1);
164 info_list->value = info;
166 /* XXX: waiting for the qapi to support qemu-queue.h types */
167 if (!cur_item) {
168 head = cur_item = info_list;
169 } else {
170 cur_item->next = info_list;
171 cur_item = info_list;
176 g_free(sn_tab);
177 *p_list = head;
178 return 0;
182 * bdrv_query_image_info:
183 * @bs: block device to examine
184 * @p_info: location to store image information
185 * @errp: location to store error information
187 * Store "flat" image information in @p_info.
189 * "Flat" means it does *not* query backing image information,
190 * i.e. (*pinfo)->has_backing_image will be set to false and
191 * (*pinfo)->backing_image to NULL even when the image does in fact have
192 * a backing image.
194 * @p_info will be set only on success. On error, store error in @errp.
196 void bdrv_query_image_info(BlockDriverState *bs,
197 ImageInfo **p_info,
198 Error **errp)
200 int64_t size;
201 const char *backing_filename;
202 BlockDriverInfo bdi;
203 int ret;
204 Error *err = NULL;
205 ImageInfo *info;
207 size = bdrv_getlength(bs);
208 if (size < 0) {
209 error_setg_errno(errp, -size, "Can't get size of device '%s'",
210 bdrv_get_device_name(bs));
211 return;
214 info = g_new0(ImageInfo, 1);
215 info->filename = g_strdup(bs->filename);
216 info->format = g_strdup(bdrv_get_format_name(bs));
217 info->virtual_size = size;
218 info->actual_size = bdrv_get_allocated_file_size(bs);
219 info->has_actual_size = info->actual_size >= 0;
220 if (bdrv_is_encrypted(bs)) {
221 info->encrypted = true;
222 info->has_encrypted = true;
224 if (bdrv_get_info(bs, &bdi) >= 0) {
225 if (bdi.cluster_size != 0) {
226 info->cluster_size = bdi.cluster_size;
227 info->has_cluster_size = true;
229 info->dirty_flag = bdi.is_dirty;
230 info->has_dirty_flag = true;
232 info->format_specific = bdrv_get_specific_info(bs);
233 info->has_format_specific = info->format_specific != NULL;
235 backing_filename = bs->backing_file;
236 if (backing_filename[0] != '\0') {
237 char *backing_filename2 = g_malloc0(PATH_MAX);
238 info->backing_filename = g_strdup(backing_filename);
239 info->has_backing_filename = true;
240 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
241 if (err) {
242 error_propagate(errp, err);
243 qapi_free_ImageInfo(info);
244 g_free(backing_filename2);
245 return;
248 if (strcmp(backing_filename, backing_filename2) != 0) {
249 info->full_backing_filename =
250 g_strdup(backing_filename2);
251 info->has_full_backing_filename = true;
254 if (bs->backing_format[0]) {
255 info->backing_filename_format = g_strdup(bs->backing_format);
256 info->has_backing_filename_format = true;
258 g_free(backing_filename2);
261 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
262 switch (ret) {
263 case 0:
264 if (info->snapshots) {
265 info->has_snapshots = true;
267 break;
268 /* recoverable error */
269 case -ENOMEDIUM:
270 case -ENOTSUP:
271 error_free(err);
272 break;
273 default:
274 error_propagate(errp, err);
275 qapi_free_ImageInfo(info);
276 return;
279 *p_info = info;
282 /* @p_info will be set only on success. */
283 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
284 Error **errp)
286 BlockInfo *info = g_malloc0(sizeof(*info));
287 BlockDriverState *bs = blk_bs(blk);
288 info->device = g_strdup(blk_name(blk));
289 info->type = g_strdup("unknown");
290 info->locked = blk_dev_is_medium_locked(blk);
291 info->removable = blk_dev_has_removable_media(blk);
293 if (blk_dev_has_removable_media(blk)) {
294 info->has_tray_open = true;
295 info->tray_open = blk_dev_is_tray_open(blk);
298 if (bdrv_iostatus_is_enabled(bs)) {
299 info->has_io_status = true;
300 info->io_status = bs->iostatus;
303 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
304 info->has_dirty_bitmaps = true;
305 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
308 if (bs->drv) {
309 info->has_inserted = true;
310 info->inserted = bdrv_block_device_info(bs, errp);
311 if (info->inserted == NULL) {
312 goto err;
316 *p_info = info;
317 return;
319 err:
320 qapi_free_BlockInfo(info);
323 static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
324 bool query_backing)
326 BlockStats *s;
328 s = g_malloc0(sizeof(*s));
330 if (bdrv_get_device_name(bs)[0]) {
331 s->has_device = true;
332 s->device = g_strdup(bdrv_get_device_name(bs));
335 if (bdrv_get_node_name(bs)[0]) {
336 s->has_node_name = true;
337 s->node_name = g_strdup(bdrv_get_node_name(bs));
340 s->stats = g_malloc0(sizeof(*s->stats));
341 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ];
342 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE];
343 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ];
344 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE];
345 s->stats->rd_merged = bs->stats.merged[BLOCK_ACCT_READ];
346 s->stats->wr_merged = bs->stats.merged[BLOCK_ACCT_WRITE];
347 s->stats->wr_highest_offset =
348 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE;
349 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH];
350 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE];
351 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ];
352 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH];
354 if (bs->file) {
355 s->has_parent = true;
356 s->parent = bdrv_query_stats(bs->file, query_backing);
359 if (query_backing && bs->backing_hd) {
360 s->has_backing = true;
361 s->backing = bdrv_query_stats(bs->backing_hd, query_backing);
364 return s;
367 BlockInfoList *qmp_query_block(Error **errp)
369 BlockInfoList *head = NULL, **p_next = &head;
370 BlockBackend *blk;
371 Error *local_err = NULL;
373 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
374 BlockInfoList *info = g_malloc0(sizeof(*info));
375 bdrv_query_info(blk, &info->value, &local_err);
376 if (local_err) {
377 error_propagate(errp, local_err);
378 goto err;
381 *p_next = info;
382 p_next = &info->next;
385 return head;
387 err:
388 qapi_free_BlockInfoList(head);
389 return NULL;
392 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
393 bool query_nodes,
394 Error **errp)
396 BlockStatsList *head = NULL, **p_next = &head;
397 BlockDriverState *bs = NULL;
399 /* Just to be safe if query_nodes is not always initialized */
400 query_nodes = has_query_nodes && query_nodes;
402 while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
403 BlockStatsList *info = g_malloc0(sizeof(*info));
404 AioContext *ctx = bdrv_get_aio_context(bs);
406 aio_context_acquire(ctx);
407 info->value = bdrv_query_stats(bs, !query_nodes);
408 aio_context_release(ctx);
410 *p_next = info;
411 p_next = &info->next;
414 return head;
417 #define NB_SUFFIXES 4
419 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
421 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
422 int64_t base;
423 int i;
425 if (size <= 999) {
426 snprintf(buf, buf_size, "%" PRId64, size);
427 } else {
428 base = 1024;
429 for (i = 0; i < NB_SUFFIXES; i++) {
430 if (size < (10 * base)) {
431 snprintf(buf, buf_size, "%0.1f%c",
432 (double)size / base,
433 suffixes[i]);
434 break;
435 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
436 snprintf(buf, buf_size, "%" PRId64 "%c",
437 ((size + (base >> 1)) / base),
438 suffixes[i]);
439 break;
441 base = base * 1024;
444 return buf;
447 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
448 QEMUSnapshotInfo *sn)
450 char buf1[128], date_buf[128], clock_buf[128];
451 struct tm tm;
452 time_t ti;
453 int64_t secs;
455 if (!sn) {
456 func_fprintf(f,
457 "%-10s%-20s%7s%20s%15s",
458 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
459 } else {
460 ti = sn->date_sec;
461 localtime_r(&ti, &tm);
462 strftime(date_buf, sizeof(date_buf),
463 "%Y-%m-%d %H:%M:%S", &tm);
464 secs = sn->vm_clock_nsec / 1000000000;
465 snprintf(clock_buf, sizeof(clock_buf),
466 "%02d:%02d:%02d.%03d",
467 (int)(secs / 3600),
468 (int)((secs / 60) % 60),
469 (int)(secs % 60),
470 (int)((sn->vm_clock_nsec / 1000000) % 1000));
471 func_fprintf(f,
472 "%-10s%-20s%7s%20s%15s",
473 sn->id_str, sn->name,
474 get_human_readable_size(buf1, sizeof(buf1),
475 sn->vm_state_size),
476 date_buf,
477 clock_buf);
481 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
482 QDict *dict);
483 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
484 QList *list);
486 static void dump_qobject(fprintf_function func_fprintf, void *f,
487 int comp_indent, QObject *obj)
489 switch (qobject_type(obj)) {
490 case QTYPE_QINT: {
491 QInt *value = qobject_to_qint(obj);
492 func_fprintf(f, "%" PRId64, qint_get_int(value));
493 break;
495 case QTYPE_QSTRING: {
496 QString *value = qobject_to_qstring(obj);
497 func_fprintf(f, "%s", qstring_get_str(value));
498 break;
500 case QTYPE_QDICT: {
501 QDict *value = qobject_to_qdict(obj);
502 dump_qdict(func_fprintf, f, comp_indent, value);
503 break;
505 case QTYPE_QLIST: {
506 QList *value = qobject_to_qlist(obj);
507 dump_qlist(func_fprintf, f, comp_indent, value);
508 break;
510 case QTYPE_QFLOAT: {
511 QFloat *value = qobject_to_qfloat(obj);
512 func_fprintf(f, "%g", qfloat_get_double(value));
513 break;
515 case QTYPE_QBOOL: {
516 QBool *value = qobject_to_qbool(obj);
517 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
518 break;
520 case QTYPE_QERROR: {
521 QString *value = qerror_human((QError *)obj);
522 func_fprintf(f, "%s", qstring_get_str(value));
523 QDECREF(value);
524 break;
526 case QTYPE_NONE:
527 break;
528 case QTYPE_MAX:
529 default:
530 abort();
534 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
535 QList *list)
537 const QListEntry *entry;
538 int i = 0;
540 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
541 qtype_code type = qobject_type(entry->value);
542 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
543 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
545 func_fprintf(f, format, indentation * 4, "", i);
546 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
547 if (!composite) {
548 func_fprintf(f, "\n");
553 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
554 QDict *dict)
556 const QDictEntry *entry;
558 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
559 qtype_code type = qobject_type(entry->value);
560 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
561 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
562 char key[strlen(entry->key) + 1];
563 int i;
565 /* replace dashes with spaces in key (variable) names */
566 for (i = 0; entry->key[i]; i++) {
567 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
569 key[i] = 0;
571 func_fprintf(f, format, indentation * 4, "", key);
572 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
573 if (!composite) {
574 func_fprintf(f, "\n");
579 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
580 ImageInfoSpecific *info_spec)
582 QmpOutputVisitor *ov = qmp_output_visitor_new();
583 QObject *obj, *data;
585 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
586 &error_abort);
587 obj = qmp_output_get_qobject(ov);
588 assert(qobject_type(obj) == QTYPE_QDICT);
589 data = qdict_get(qobject_to_qdict(obj), "data");
590 dump_qobject(func_fprintf, f, 1, data);
591 qmp_output_visitor_cleanup(ov);
594 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
595 ImageInfo *info)
597 char size_buf[128], dsize_buf[128];
598 if (!info->has_actual_size) {
599 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
600 } else {
601 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
602 info->actual_size);
604 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
605 func_fprintf(f,
606 "image: %s\n"
607 "file format: %s\n"
608 "virtual size: %s (%" PRId64 " bytes)\n"
609 "disk size: %s\n",
610 info->filename, info->format, size_buf,
611 info->virtual_size,
612 dsize_buf);
614 if (info->has_encrypted && info->encrypted) {
615 func_fprintf(f, "encrypted: yes\n");
618 if (info->has_cluster_size) {
619 func_fprintf(f, "cluster_size: %" PRId64 "\n",
620 info->cluster_size);
623 if (info->has_dirty_flag && info->dirty_flag) {
624 func_fprintf(f, "cleanly shut down: no\n");
627 if (info->has_backing_filename) {
628 func_fprintf(f, "backing file: %s", info->backing_filename);
629 if (info->has_full_backing_filename) {
630 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
632 func_fprintf(f, "\n");
633 if (info->has_backing_filename_format) {
634 func_fprintf(f, "backing file format: %s\n",
635 info->backing_filename_format);
639 if (info->has_snapshots) {
640 SnapshotInfoList *elem;
642 func_fprintf(f, "Snapshot list:\n");
643 bdrv_snapshot_dump(func_fprintf, f, NULL);
644 func_fprintf(f, "\n");
646 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
647 * we convert to the block layer's native QEMUSnapshotInfo for now.
649 for (elem = info->snapshots; elem; elem = elem->next) {
650 QEMUSnapshotInfo sn = {
651 .vm_state_size = elem->value->vm_state_size,
652 .date_sec = elem->value->date_sec,
653 .date_nsec = elem->value->date_nsec,
654 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
655 elem->value->vm_clock_nsec,
658 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
659 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
660 bdrv_snapshot_dump(func_fprintf, f, &sn);
661 func_fprintf(f, "\n");
665 if (info->has_format_specific) {
666 func_fprintf(f, "Format specific information:\n");
667 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);