qxl: refactor rounding up to a nearest power of 2
[qemu/cris-port.git] / block / qapi.c
blob1808e67336af161b868896d2d99f31badce8fa37
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)
36 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
38 info->file = g_strdup(bs->filename);
39 info->ro = bs->read_only;
40 info->drv = g_strdup(bs->drv->format_name);
41 info->encrypted = bs->encrypted;
42 info->encryption_key_missing = bdrv_key_required(bs);
44 info->cache = g_new(BlockdevCacheInfo, 1);
45 *info->cache = (BlockdevCacheInfo) {
46 .writeback = bdrv_enable_write_cache(bs),
47 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
48 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
51 if (bs->node_name[0]) {
52 info->has_node_name = true;
53 info->node_name = g_strdup(bs->node_name);
56 if (bs->backing_file[0]) {
57 info->has_backing_file = true;
58 info->backing_file = g_strdup(bs->backing_file);
61 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
62 info->detect_zeroes = bs->detect_zeroes;
64 if (bs->io_limits_enabled) {
65 ThrottleConfig cfg;
66 throttle_get_config(&bs->throttle_state, &cfg);
67 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
68 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
69 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
71 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
72 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
73 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
75 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
76 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
77 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
78 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
79 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
80 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
82 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
83 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
84 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
85 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
86 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
87 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
89 info->has_iops_size = cfg.op_size;
90 info->iops_size = cfg.op_size;
93 info->write_threshold = bdrv_write_threshold_get(bs);
95 return info;
99 * Returns 0 on success, with *p_list either set to describe snapshot
100 * information, or NULL because there are no snapshots. Returns -errno on
101 * error, with *p_list untouched.
103 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
104 SnapshotInfoList **p_list,
105 Error **errp)
107 int i, sn_count;
108 QEMUSnapshotInfo *sn_tab = NULL;
109 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
110 SnapshotInfo *info;
112 sn_count = bdrv_snapshot_list(bs, &sn_tab);
113 if (sn_count < 0) {
114 const char *dev = bdrv_get_device_name(bs);
115 switch (sn_count) {
116 case -ENOMEDIUM:
117 error_setg(errp, "Device '%s' is not inserted", dev);
118 break;
119 case -ENOTSUP:
120 error_setg(errp,
121 "Device '%s' does not support internal snapshots",
122 dev);
123 break;
124 default:
125 error_setg_errno(errp, -sn_count,
126 "Can't list snapshots of device '%s'", dev);
127 break;
129 return sn_count;
132 for (i = 0; i < sn_count; i++) {
133 info = g_new0(SnapshotInfo, 1);
134 info->id = g_strdup(sn_tab[i].id_str);
135 info->name = g_strdup(sn_tab[i].name);
136 info->vm_state_size = sn_tab[i].vm_state_size;
137 info->date_sec = sn_tab[i].date_sec;
138 info->date_nsec = sn_tab[i].date_nsec;
139 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
140 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
142 info_list = g_new0(SnapshotInfoList, 1);
143 info_list->value = info;
145 /* XXX: waiting for the qapi to support qemu-queue.h types */
146 if (!cur_item) {
147 head = cur_item = info_list;
148 } else {
149 cur_item->next = info_list;
150 cur_item = info_list;
155 g_free(sn_tab);
156 *p_list = head;
157 return 0;
161 * bdrv_query_image_info:
162 * @bs: block device to examine
163 * @p_info: location to store image information
164 * @errp: location to store error information
166 * Store "flat" image information in @p_info.
168 * "Flat" means it does *not* query backing image information,
169 * i.e. (*pinfo)->has_backing_image will be set to false and
170 * (*pinfo)->backing_image to NULL even when the image does in fact have
171 * a backing image.
173 * @p_info will be set only on success. On error, store error in @errp.
175 void bdrv_query_image_info(BlockDriverState *bs,
176 ImageInfo **p_info,
177 Error **errp)
179 int64_t size;
180 const char *backing_filename;
181 BlockDriverInfo bdi;
182 int ret;
183 Error *err = NULL;
184 ImageInfo *info;
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;
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;
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;
208 info->dirty_flag = bdi.is_dirty;
209 info->has_dirty_flag = true;
211 info->format_specific = bdrv_get_specific_info(bs);
212 info->has_format_specific = info->format_specific != NULL;
214 backing_filename = bs->backing_file;
215 if (backing_filename[0] != '\0') {
216 char *backing_filename2 = g_malloc0(PATH_MAX);
217 info->backing_filename = g_strdup(backing_filename);
218 info->has_backing_filename = true;
219 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
220 if (err) {
221 error_propagate(errp, err);
222 qapi_free_ImageInfo(info);
223 g_free(backing_filename2);
224 return;
227 if (strcmp(backing_filename, backing_filename2) != 0) {
228 info->full_backing_filename =
229 g_strdup(backing_filename2);
230 info->has_full_backing_filename = true;
233 if (bs->backing_format[0]) {
234 info->backing_filename_format = g_strdup(bs->backing_format);
235 info->has_backing_filename_format = true;
237 g_free(backing_filename2);
240 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
241 switch (ret) {
242 case 0:
243 if (info->snapshots) {
244 info->has_snapshots = true;
246 break;
247 /* recoverable error */
248 case -ENOMEDIUM:
249 case -ENOTSUP:
250 error_free(err);
251 break;
252 default:
253 error_propagate(errp, err);
254 qapi_free_ImageInfo(info);
255 return;
258 *p_info = info;
261 /* @p_info will be set only on success. */
262 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
263 Error **errp)
265 BlockInfo *info = g_malloc0(sizeof(*info));
266 BlockDriverState *bs = blk_bs(blk);
267 BlockDriverState *bs0;
268 ImageInfo **p_image_info;
269 Error *local_err = NULL;
270 info->device = g_strdup(blk_name(blk));
271 info->type = g_strdup("unknown");
272 info->locked = blk_dev_is_medium_locked(blk);
273 info->removable = blk_dev_has_removable_media(blk);
275 if (blk_dev_has_removable_media(blk)) {
276 info->has_tray_open = true;
277 info->tray_open = blk_dev_is_tray_open(blk);
280 if (bdrv_iostatus_is_enabled(bs)) {
281 info->has_io_status = true;
282 info->io_status = bs->iostatus;
285 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
286 info->has_dirty_bitmaps = true;
287 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
290 if (bs->drv) {
291 info->has_inserted = true;
292 info->inserted = bdrv_block_device_info(bs);
294 bs0 = bs;
295 p_image_info = &info->inserted->image;
296 while (1) {
297 bdrv_query_image_info(bs0, p_image_info, &local_err);
298 if (local_err) {
299 error_propagate(errp, local_err);
300 goto err;
302 if (bs0->drv && bs0->backing_hd) {
303 bs0 = bs0->backing_hd;
304 (*p_image_info)->has_backing_image = true;
305 p_image_info = &((*p_image_info)->backing_image);
306 } else {
307 break;
312 *p_info = info;
313 return;
315 err:
316 qapi_free_BlockInfo(info);
319 static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
320 bool query_backing)
322 BlockStats *s;
324 s = g_malloc0(sizeof(*s));
326 if (bdrv_get_device_name(bs)[0]) {
327 s->has_device = true;
328 s->device = g_strdup(bdrv_get_device_name(bs));
331 if (bdrv_get_node_name(bs)[0]) {
332 s->has_node_name = true;
333 s->node_name = g_strdup(bdrv_get_node_name(bs));
336 s->stats = g_malloc0(sizeof(*s->stats));
337 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ];
338 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE];
339 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ];
340 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE];
341 s->stats->rd_merged = bs->stats.merged[BLOCK_ACCT_READ];
342 s->stats->wr_merged = bs->stats.merged[BLOCK_ACCT_WRITE];
343 s->stats->wr_highest_offset =
344 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE;
345 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH];
346 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE];
347 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ];
348 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH];
350 if (bs->file) {
351 s->has_parent = true;
352 s->parent = bdrv_query_stats(bs->file, query_backing);
355 if (query_backing && bs->backing_hd) {
356 s->has_backing = true;
357 s->backing = bdrv_query_stats(bs->backing_hd, query_backing);
360 return s;
363 BlockInfoList *qmp_query_block(Error **errp)
365 BlockInfoList *head = NULL, **p_next = &head;
366 BlockBackend *blk;
367 Error *local_err = NULL;
369 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
370 BlockInfoList *info = g_malloc0(sizeof(*info));
371 bdrv_query_info(blk, &info->value, &local_err);
372 if (local_err) {
373 error_propagate(errp, local_err);
374 goto err;
377 *p_next = info;
378 p_next = &info->next;
381 return head;
383 err:
384 qapi_free_BlockInfoList(head);
385 return NULL;
388 BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
389 bool query_nodes,
390 Error **errp)
392 BlockStatsList *head = NULL, **p_next = &head;
393 BlockDriverState *bs = NULL;
395 /* Just to be safe if query_nodes is not always initialized */
396 query_nodes = has_query_nodes && query_nodes;
398 while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
399 BlockStatsList *info = g_malloc0(sizeof(*info));
400 AioContext *ctx = bdrv_get_aio_context(bs);
402 aio_context_acquire(ctx);
403 info->value = bdrv_query_stats(bs, !query_nodes);
404 aio_context_release(ctx);
406 *p_next = info;
407 p_next = &info->next;
410 return head;
413 #define NB_SUFFIXES 4
415 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
417 static const char suffixes[NB_SUFFIXES] = "KMGT";
418 int64_t base;
419 int i;
421 if (size <= 999) {
422 snprintf(buf, buf_size, "%" PRId64, size);
423 } else {
424 base = 1024;
425 for (i = 0; i < NB_SUFFIXES; i++) {
426 if (size < (10 * base)) {
427 snprintf(buf, buf_size, "%0.1f%c",
428 (double)size / base,
429 suffixes[i]);
430 break;
431 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
432 snprintf(buf, buf_size, "%" PRId64 "%c",
433 ((size + (base >> 1)) / base),
434 suffixes[i]);
435 break;
437 base = base * 1024;
440 return buf;
443 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
444 QEMUSnapshotInfo *sn)
446 char buf1[128], date_buf[128], clock_buf[128];
447 struct tm tm;
448 time_t ti;
449 int64_t secs;
451 if (!sn) {
452 func_fprintf(f,
453 "%-10s%-20s%7s%20s%15s",
454 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
455 } else {
456 ti = sn->date_sec;
457 localtime_r(&ti, &tm);
458 strftime(date_buf, sizeof(date_buf),
459 "%Y-%m-%d %H:%M:%S", &tm);
460 secs = sn->vm_clock_nsec / 1000000000;
461 snprintf(clock_buf, sizeof(clock_buf),
462 "%02d:%02d:%02d.%03d",
463 (int)(secs / 3600),
464 (int)((secs / 60) % 60),
465 (int)(secs % 60),
466 (int)((sn->vm_clock_nsec / 1000000) % 1000));
467 func_fprintf(f,
468 "%-10s%-20s%7s%20s%15s",
469 sn->id_str, sn->name,
470 get_human_readable_size(buf1, sizeof(buf1),
471 sn->vm_state_size),
472 date_buf,
473 clock_buf);
477 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
478 QDict *dict);
479 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
480 QList *list);
482 static void dump_qobject(fprintf_function func_fprintf, void *f,
483 int comp_indent, QObject *obj)
485 switch (qobject_type(obj)) {
486 case QTYPE_QINT: {
487 QInt *value = qobject_to_qint(obj);
488 func_fprintf(f, "%" PRId64, qint_get_int(value));
489 break;
491 case QTYPE_QSTRING: {
492 QString *value = qobject_to_qstring(obj);
493 func_fprintf(f, "%s", qstring_get_str(value));
494 break;
496 case QTYPE_QDICT: {
497 QDict *value = qobject_to_qdict(obj);
498 dump_qdict(func_fprintf, f, comp_indent, value);
499 break;
501 case QTYPE_QLIST: {
502 QList *value = qobject_to_qlist(obj);
503 dump_qlist(func_fprintf, f, comp_indent, value);
504 break;
506 case QTYPE_QFLOAT: {
507 QFloat *value = qobject_to_qfloat(obj);
508 func_fprintf(f, "%g", qfloat_get_double(value));
509 break;
511 case QTYPE_QBOOL: {
512 QBool *value = qobject_to_qbool(obj);
513 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
514 break;
516 case QTYPE_QERROR: {
517 QString *value = qerror_human((QError *)obj);
518 func_fprintf(f, "%s", qstring_get_str(value));
519 QDECREF(value);
520 break;
522 case QTYPE_NONE:
523 break;
524 case QTYPE_MAX:
525 default:
526 abort();
530 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
531 QList *list)
533 const QListEntry *entry;
534 int i = 0;
536 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
537 qtype_code type = qobject_type(entry->value);
538 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
539 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
541 func_fprintf(f, format, indentation * 4, "", i);
542 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
543 if (!composite) {
544 func_fprintf(f, "\n");
549 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
550 QDict *dict)
552 const QDictEntry *entry;
554 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
555 qtype_code type = qobject_type(entry->value);
556 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
557 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
558 char key[strlen(entry->key) + 1];
559 int i;
561 /* replace dashes with spaces in key (variable) names */
562 for (i = 0; entry->key[i]; i++) {
563 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
565 key[i] = 0;
567 func_fprintf(f, format, indentation * 4, "", key);
568 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
569 if (!composite) {
570 func_fprintf(f, "\n");
575 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
576 ImageInfoSpecific *info_spec)
578 QmpOutputVisitor *ov = qmp_output_visitor_new();
579 QObject *obj, *data;
581 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
582 &error_abort);
583 obj = qmp_output_get_qobject(ov);
584 assert(qobject_type(obj) == QTYPE_QDICT);
585 data = qdict_get(qobject_to_qdict(obj), "data");
586 dump_qobject(func_fprintf, f, 1, data);
587 qmp_output_visitor_cleanup(ov);
590 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
591 ImageInfo *info)
593 char size_buf[128], dsize_buf[128];
594 if (!info->has_actual_size) {
595 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
596 } else {
597 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
598 info->actual_size);
600 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
601 func_fprintf(f,
602 "image: %s\n"
603 "file format: %s\n"
604 "virtual size: %s (%" PRId64 " bytes)\n"
605 "disk size: %s\n",
606 info->filename, info->format, size_buf,
607 info->virtual_size,
608 dsize_buf);
610 if (info->has_encrypted && info->encrypted) {
611 func_fprintf(f, "encrypted: yes\n");
614 if (info->has_cluster_size) {
615 func_fprintf(f, "cluster_size: %" PRId64 "\n",
616 info->cluster_size);
619 if (info->has_dirty_flag && info->dirty_flag) {
620 func_fprintf(f, "cleanly shut down: no\n");
623 if (info->has_backing_filename) {
624 func_fprintf(f, "backing file: %s", info->backing_filename);
625 if (info->has_full_backing_filename) {
626 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
628 func_fprintf(f, "\n");
629 if (info->has_backing_filename_format) {
630 func_fprintf(f, "backing file format: %s\n",
631 info->backing_filename_format);
635 if (info->has_snapshots) {
636 SnapshotInfoList *elem;
638 func_fprintf(f, "Snapshot list:\n");
639 bdrv_snapshot_dump(func_fprintf, f, NULL);
640 func_fprintf(f, "\n");
642 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
643 * we convert to the block layer's native QEMUSnapshotInfo for now.
645 for (elem = info->snapshots; elem; elem = elem->next) {
646 QEMUSnapshotInfo sn = {
647 .vm_state_size = elem->value->vm_state_size,
648 .date_sec = elem->value->date_sec,
649 .date_nsec = elem->value->date_nsec,
650 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
651 elem->value->vm_clock_nsec,
654 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
655 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
656 bdrv_snapshot_dump(func_fprintf, f, &sn);
657 func_fprintf(f, "\n");
661 if (info->has_format_specific) {
662 func_fprintf(f, "Format specific information:\n");
663 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);