218e99
From 9bba35d273d094e206e29f5a328f21834a838471 Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Thu, 7 Nov 2013 12:59:07 +0100
218e99
Subject: [PATCH 68/87] block: move qmp and info dump related code to block/qapi.c
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383756824-6921-3-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55557
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 02/21] block: move qmp and info dump related code to block/qapi.c
218e99
Bugzilla: 980771
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
218e99
218e99
From: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
218e99
BZ: 980771
218e99
218e99
This patch is a pure code move patch, except following modification:
218e99
1 get_human_readable_size() is changed to static function.
218e99
2 dump_human_image_info() is renamed to bdrv_image_info_dump().
218e99
3 in qmp_query_block() and qmp_query_blockstats, use bdrv_next(bs)
218e99
instead of direct traverse of global array 'bdrv_states'.
218e99
4 collect_snapshots() and collect_image_info() are renamed, unused parameter
218e99
*fmt in collect_image_info() is removed.
218e99
5 code style fix.
218e99
218e99
To avoid conflict and tip better, macro in header file is BLOCK_QAPI_H
218e99
instead of QAPI_H. Now block.h and snapshot.h are at the same level in
218e99
include path, block_int.h and qapi.h will both include them.
218e99
218e99
Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
218e99
Reviewed-by: Eric Blake <eblake@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit f364ec65b56b69c55b674cb6560aa1fbbea9e013)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block.c                   |  185 -----------------------
218e99
 block/Makefile.objs       |    2 +-
218e99
 block/qapi.c              |  360 +++++++++++++++++++++++++++++++++++++++++++++
218e99
 include/block/block.h     |    9 -
218e99
 include/block/block_int.h |    1 +
218e99
 include/block/qapi.h      |   41 +++++
218e99
 qemu-img.c                |  156 +-------------------
218e99
 savevm.c                  |    1 +
218e99
 8 files changed, 408 insertions(+), 347 deletions(-)
218e99
 create mode 100644 block/qapi.c
218e99
 create mode 100644 include/block/qapi.h
218e99
218e99
diff --git a/block.c b/block.c
218e99
index dccdb2c..e4885da 100644
218e99
--- a/block.c
218e99
+++ b/block.c
218e99
@@ -3266,128 +3266,6 @@ int bdrv_is_allocated_above(BlockDriverState *top,
218e99
     return 0;
218e99
 }
218e99
 
218e99
-BlockInfo *bdrv_query_info(BlockDriverState *bs)
218e99
-{
218e99
-    BlockInfo *info = g_malloc0(sizeof(*info));
218e99
-    info->device = g_strdup(bs->device_name);
218e99
-    info->type = g_strdup("unknown");
218e99
-    info->locked = bdrv_dev_is_medium_locked(bs);
218e99
-    info->removable = bdrv_dev_has_removable_media(bs);
218e99
-
218e99
-    if (bdrv_dev_has_removable_media(bs)) {
218e99
-        info->has_tray_open = true;
218e99
-        info->tray_open = bdrv_dev_is_tray_open(bs);
218e99
-    }
218e99
-
218e99
-    if (bdrv_iostatus_is_enabled(bs)) {
218e99
-        info->has_io_status = true;
218e99
-        info->io_status = bs->iostatus;
218e99
-    }
218e99
-
218e99
-    if (bs->dirty_bitmap) {
218e99
-        info->has_dirty = true;
218e99
-        info->dirty = g_malloc0(sizeof(*info->dirty));
218e99
-        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
218e99
-        info->dirty->granularity =
218e99
-            ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
218e99
-    }
218e99
-
218e99
-    if (bs->drv) {
218e99
-        info->has_inserted = true;
218e99
-        info->inserted = g_malloc0(sizeof(*info->inserted));
218e99
-        info->inserted->file = g_strdup(bs->filename);
218e99
-        info->inserted->ro = bs->read_only;
218e99
-        info->inserted->drv = g_strdup(bs->drv->format_name);
218e99
-        info->inserted->encrypted = bs->encrypted;
218e99
-        info->inserted->encryption_key_missing = bdrv_key_required(bs);
218e99
-
218e99
-        if (bs->backing_file[0]) {
218e99
-            info->inserted->has_backing_file = true;
218e99
-            info->inserted->backing_file = g_strdup(bs->backing_file);
218e99
-        }
218e99
-
218e99
-        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
218e99
-
218e99
-        if (bs->io_limits_enabled) {
218e99
-            info->inserted->bps =
218e99
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
218e99
-            info->inserted->bps_rd =
218e99
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
218e99
-            info->inserted->bps_wr =
218e99
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
218e99
-            info->inserted->iops =
218e99
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
218e99
-            info->inserted->iops_rd =
218e99
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
218e99
-            info->inserted->iops_wr =
218e99
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
218e99
-        }
218e99
-    }
218e99
-    return info;
218e99
-}
218e99
-
218e99
-BlockInfoList *qmp_query_block(Error **errp)
218e99
-{
218e99
-    BlockInfoList *head = NULL, **p_next = &head;
218e99
-    BlockDriverState *bs;
218e99
-
218e99
-    QTAILQ_FOREACH(bs, &bdrv_states, list) {
218e99
-        BlockInfoList *info = g_malloc0(sizeof(*info));
218e99
-        info->value = bdrv_query_info(bs);
218e99
-
218e99
-        *p_next = info;
218e99
-        p_next = &info->next;
218e99
-    }
218e99
-
218e99
-    return head;
218e99
-}
218e99
-
218e99
-BlockStats *bdrv_query_stats(const BlockDriverState *bs)
218e99
-{
218e99
-    BlockStats *s;
218e99
-
218e99
-    s = g_malloc0(sizeof(*s));
218e99
-
218e99
-    if (bs->device_name[0]) {
218e99
-        s->has_device = true;
218e99
-        s->device = g_strdup(bs->device_name);
218e99
-    }
218e99
-
218e99
-    s->stats = g_malloc0(sizeof(*s->stats));
218e99
-    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
218e99
-    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
218e99
-    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
218e99
-    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
218e99
-    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
218e99
-    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
218e99
-    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
218e99
-    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
218e99
-    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
218e99
-
218e99
-    if (bs->file) {
218e99
-        s->has_parent = true;
218e99
-        s->parent = bdrv_query_stats(bs->file);
218e99
-    }
218e99
-
218e99
-    return s;
218e99
-}
218e99
-
218e99
-BlockStatsList *qmp_query_blockstats(Error **errp)
218e99
-{
218e99
-    BlockStatsList *head = NULL, **p_next = &head;
218e99
-    BlockDriverState *bs;
218e99
-
218e99
-    QTAILQ_FOREACH(bs, &bdrv_states, list) {
218e99
-        BlockStatsList *info = g_malloc0(sizeof(*info));
218e99
-        info->value = bdrv_query_stats(bs);
218e99
-
218e99
-        *p_next = info;
218e99
-        p_next = &info->next;
218e99
-    }
218e99
-
218e99
-    return head;
218e99
-}
218e99
-
218e99
 const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
218e99
 {
218e99
     if (bs->backing_hd && bs->backing_hd->encrypted)
218e99
@@ -3623,69 +3501,6 @@ BlockDriverState *bdrv_find_base(BlockDriverState *bs)
218e99
     return curr_bs;
218e99
 }
218e99
 
218e99
-#define NB_SUFFIXES 4
218e99
-
218e99
-char *get_human_readable_size(char *buf, int buf_size, int64_t size)
218e99
-{
218e99
-    static const char suffixes[NB_SUFFIXES] = "KMGT";
218e99
-    int64_t base;
218e99
-    int i;
218e99
-
218e99
-    if (size <= 999) {
218e99
-        snprintf(buf, buf_size, "%" PRId64, size);
218e99
-    } else {
218e99
-        base = 1024;
218e99
-        for(i = 0; i < NB_SUFFIXES; i++) {
218e99
-            if (size < (10 * base)) {
218e99
-                snprintf(buf, buf_size, "%0.1f%c",
218e99
-                         (double)size / base,
218e99
-                         suffixes[i]);
218e99
-                break;
218e99
-            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
218e99
-                snprintf(buf, buf_size, "%" PRId64 "%c",
218e99
-                         ((size + (base >> 1)) / base),
218e99
-                         suffixes[i]);
218e99
-                break;
218e99
-            }
218e99
-            base = base * 1024;
218e99
-        }
218e99
-    }
218e99
-    return buf;
218e99
-}
218e99
-
218e99
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
218e99
-{
218e99
-    char buf1[128], date_buf[128], clock_buf[128];
218e99
-    struct tm tm;
218e99
-    time_t ti;
218e99
-    int64_t secs;
218e99
-
218e99
-    if (!sn) {
218e99
-        snprintf(buf, buf_size,
218e99
-                 "%-10s%-20s%7s%20s%15s",
218e99
-                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
218e99
-    } else {
218e99
-        ti = sn->date_sec;
218e99
-        localtime_r(&ti, &tm;;
218e99
-        strftime(date_buf, sizeof(date_buf),
218e99
-                 "%Y-%m-%d %H:%M:%S", &tm;;
218e99
-        secs = sn->vm_clock_nsec / 1000000000;
218e99
-        snprintf(clock_buf, sizeof(clock_buf),
218e99
-                 "%02d:%02d:%02d.%03d",
218e99
-                 (int)(secs / 3600),
218e99
-                 (int)((secs / 60) % 60),
218e99
-                 (int)(secs % 60),
218e99
-                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
218e99
-        snprintf(buf, buf_size,
218e99
-                 "%-10s%-20s%7s%20s%15s",
218e99
-                 sn->id_str, sn->name,
218e99
-                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
218e99
-                 date_buf,
218e99
-                 clock_buf);
218e99
-    }
218e99
-    return buf;
218e99
-}
218e99
-
218e99
 /**************************************************************/
218e99
 /* async I/Os */
218e99
 
218e99
diff --git a/block/Makefile.objs b/block/Makefile.objs
218e99
index 162f270..3db1839 100644
218e99
--- a/block/Makefile.objs
218e99
+++ b/block/Makefile.objs
218e99
@@ -4,7 +4,7 @@ block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
218e99
 block-obj-y += qed-check.o
218e99
 block-obj-y += vhdx.o
218e99
 block-obj-y += parallels.o blkdebug.o blkverify.o
218e99
-block-obj-y += snapshot.o
218e99
+block-obj-y += snapshot.o qapi.o
218e99
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
218e99
 block-obj-$(CONFIG_POSIX) += raw-posix.o
218e99
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
218e99
diff --git a/block/qapi.c b/block/qapi.c
218e99
new file mode 100644
218e99
index 0000000..155e77e
218e99
--- /dev/null
218e99
+++ b/block/qapi.c
218e99
@@ -0,0 +1,360 @@
218e99
+/*
218e99
+ * Block layer qmp and info dump related functions
218e99
+ *
218e99
+ * Copyright (c) 2003-2008 Fabrice Bellard
218e99
+ *
218e99
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
+ * of this software and associated documentation files (the "Software"), to deal
218e99
+ * in the Software without restriction, including without limitation the rights
218e99
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
+ * copies of the Software, and to permit persons to whom the Software is
218e99
+ * furnished to do so, subject to the following conditions:
218e99
+ *
218e99
+ * The above copyright notice and this permission notice shall be included in
218e99
+ * all copies or substantial portions of the Software.
218e99
+ *
218e99
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
+ * THE SOFTWARE.
218e99
+ */
218e99
+
218e99
+#include "block/qapi.h"
218e99
+#include "block/block_int.h"
218e99
+#include "qmp-commands.h"
218e99
+
218e99
+void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info)
218e99
+{
218e99
+    int i, sn_count;
218e99
+    QEMUSnapshotInfo *sn_tab = NULL;
218e99
+    SnapshotInfoList *info_list, *cur_item = NULL;
218e99
+    sn_count = bdrv_snapshot_list(bs, &sn_tab);
218e99
+
218e99
+    for (i = 0; i < sn_count; i++) {
218e99
+        info->has_snapshots = true;
218e99
+        info_list = g_new0(SnapshotInfoList, 1);
218e99
+
218e99
+        info_list->value                = g_new0(SnapshotInfo, 1);
218e99
+        info_list->value->id            = g_strdup(sn_tab[i].id_str);
218e99
+        info_list->value->name          = g_strdup(sn_tab[i].name);
218e99
+        info_list->value->vm_state_size = sn_tab[i].vm_state_size;
218e99
+        info_list->value->date_sec      = sn_tab[i].date_sec;
218e99
+        info_list->value->date_nsec     = sn_tab[i].date_nsec;
218e99
+        info_list->value->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
218e99
+        info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
218e99
+
218e99
+        /* XXX: waiting for the qapi to support qemu-queue.h types */
218e99
+        if (!cur_item) {
218e99
+            info->snapshots = cur_item = info_list;
218e99
+        } else {
218e99
+            cur_item->next = info_list;
218e99
+            cur_item = info_list;
218e99
+        }
218e99
+
218e99
+    }
218e99
+
218e99
+    g_free(sn_tab);
218e99
+}
218e99
+
218e99
+void bdrv_collect_image_info(BlockDriverState *bs,
218e99
+                             ImageInfo *info,
218e99
+                             const char *filename)
218e99
+{
218e99
+    uint64_t total_sectors;
218e99
+    char backing_filename[1024];
218e99
+    char backing_filename2[1024];
218e99
+    BlockDriverInfo bdi;
218e99
+
218e99
+    bdrv_get_geometry(bs, &total_sectors);
218e99
+
218e99
+    info->filename        = g_strdup(filename);
218e99
+    info->format          = g_strdup(bdrv_get_format_name(bs));
218e99
+    info->virtual_size    = total_sectors * 512;
218e99
+    info->actual_size     = bdrv_get_allocated_file_size(bs);
218e99
+    info->has_actual_size = info->actual_size >= 0;
218e99
+    if (bdrv_is_encrypted(bs)) {
218e99
+        info->encrypted = true;
218e99
+        info->has_encrypted = true;
218e99
+    }
218e99
+    if (bdrv_get_info(bs, &bdi) >= 0) {
218e99
+        if (bdi.cluster_size != 0) {
218e99
+            info->cluster_size = bdi.cluster_size;
218e99
+            info->has_cluster_size = true;
218e99
+        }
218e99
+        info->dirty_flag = bdi.is_dirty;
218e99
+        info->has_dirty_flag = true;
218e99
+    }
218e99
+    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
218e99
+    if (backing_filename[0] != '\0') {
218e99
+        info->backing_filename = g_strdup(backing_filename);
218e99
+        info->has_backing_filename = true;
218e99
+        bdrv_get_full_backing_filename(bs, backing_filename2,
218e99
+                                       sizeof(backing_filename2));
218e99
+
218e99
+        if (strcmp(backing_filename, backing_filename2) != 0) {
218e99
+            info->full_backing_filename =
218e99
+                        g_strdup(backing_filename2);
218e99
+            info->has_full_backing_filename = true;
218e99
+        }
218e99
+
218e99
+        if (bs->backing_format[0]) {
218e99
+            info->backing_filename_format = g_strdup(bs->backing_format);
218e99
+            info->has_backing_filename_format = true;
218e99
+        }
218e99
+    }
218e99
+}
218e99
+
218e99
+BlockInfo *bdrv_query_info(BlockDriverState *bs)
218e99
+{
218e99
+    BlockInfo *info = g_malloc0(sizeof(*info));
218e99
+    info->device = g_strdup(bs->device_name);
218e99
+    info->type = g_strdup("unknown");
218e99
+    info->locked = bdrv_dev_is_medium_locked(bs);
218e99
+    info->removable = bdrv_dev_has_removable_media(bs);
218e99
+
218e99
+    if (bdrv_dev_has_removable_media(bs)) {
218e99
+        info->has_tray_open = true;
218e99
+        info->tray_open = bdrv_dev_is_tray_open(bs);
218e99
+    }
218e99
+
218e99
+    if (bdrv_iostatus_is_enabled(bs)) {
218e99
+        info->has_io_status = true;
218e99
+        info->io_status = bs->iostatus;
218e99
+    }
218e99
+
218e99
+    if (bs->dirty_bitmap) {
218e99
+        info->has_dirty = true;
218e99
+        info->dirty = g_malloc0(sizeof(*info->dirty));
218e99
+        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
218e99
+        info->dirty->granularity =
218e99
+         ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
218e99
+    }
218e99
+
218e99
+    if (bs->drv) {
218e99
+        info->has_inserted = true;
218e99
+        info->inserted = g_malloc0(sizeof(*info->inserted));
218e99
+        info->inserted->file = g_strdup(bs->filename);
218e99
+        info->inserted->ro = bs->read_only;
218e99
+        info->inserted->drv = g_strdup(bs->drv->format_name);
218e99
+        info->inserted->encrypted = bs->encrypted;
218e99
+        info->inserted->encryption_key_missing = bdrv_key_required(bs);
218e99
+
218e99
+        if (bs->backing_file[0]) {
218e99
+            info->inserted->has_backing_file = true;
218e99
+            info->inserted->backing_file = g_strdup(bs->backing_file);
218e99
+        }
218e99
+
218e99
+        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
218e99
+
218e99
+        if (bs->io_limits_enabled) {
218e99
+            info->inserted->bps =
218e99
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
218e99
+            info->inserted->bps_rd =
218e99
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
218e99
+            info->inserted->bps_wr =
218e99
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
218e99
+            info->inserted->iops =
218e99
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
218e99
+            info->inserted->iops_rd =
218e99
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
218e99
+            info->inserted->iops_wr =
218e99
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
218e99
+        }
218e99
+    }
218e99
+    return info;
218e99
+}
218e99
+
218e99
+BlockStats *bdrv_query_stats(const BlockDriverState *bs)
218e99
+{
218e99
+    BlockStats *s;
218e99
+
218e99
+    s = g_malloc0(sizeof(*s));
218e99
+
218e99
+    if (bs->device_name[0]) {
218e99
+        s->has_device = true;
218e99
+        s->device = g_strdup(bs->device_name);
218e99
+    }
218e99
+
218e99
+    s->stats = g_malloc0(sizeof(*s->stats));
218e99
+    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
218e99
+    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
218e99
+    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
218e99
+    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
218e99
+    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
218e99
+    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
218e99
+    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
218e99
+    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
218e99
+    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
218e99
+
218e99
+    if (bs->file) {
218e99
+        s->has_parent = true;
218e99
+        s->parent = bdrv_query_stats(bs->file);
218e99
+    }
218e99
+
218e99
+    return s;
218e99
+}
218e99
+
218e99
+BlockInfoList *qmp_query_block(Error **errp)
218e99
+{
218e99
+    BlockInfoList *head = NULL, **p_next = &head;
218e99
+    BlockDriverState *bs = NULL;
218e99
+
218e99
+     while ((bs = bdrv_next(bs))) {
218e99
+        BlockInfoList *info = g_malloc0(sizeof(*info));
218e99
+        info->value = bdrv_query_info(bs);
218e99
+
218e99
+        *p_next = info;
218e99
+        p_next = &info->next;
218e99
+    }
218e99
+
218e99
+    return head;
218e99
+}
218e99
+
218e99
+BlockStatsList *qmp_query_blockstats(Error **errp)
218e99
+{
218e99
+    BlockStatsList *head = NULL, **p_next = &head;
218e99
+    BlockDriverState *bs = NULL;
218e99
+
218e99
+     while ((bs = bdrv_next(bs))) {
218e99
+        BlockStatsList *info = g_malloc0(sizeof(*info));
218e99
+        info->value = bdrv_query_stats(bs);
218e99
+
218e99
+        *p_next = info;
218e99
+        p_next = &info->next;
218e99
+    }
218e99
+
218e99
+    return head;
218e99
+}
218e99
+
218e99
+#define NB_SUFFIXES 4
218e99
+
218e99
+static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
218e99
+{
218e99
+    static const char suffixes[NB_SUFFIXES] = "KMGT";
218e99
+    int64_t base;
218e99
+    int i;
218e99
+
218e99
+    if (size <= 999) {
218e99
+        snprintf(buf, buf_size, "%" PRId64, size);
218e99
+    } else {
218e99
+        base = 1024;
218e99
+        for (i = 0; i < NB_SUFFIXES; i++) {
218e99
+            if (size < (10 * base)) {
218e99
+                snprintf(buf, buf_size, "%0.1f%c",
218e99
+                         (double)size / base,
218e99
+                         suffixes[i]);
218e99
+                break;
218e99
+            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
218e99
+                snprintf(buf, buf_size, "%" PRId64 "%c",
218e99
+                         ((size + (base >> 1)) / base),
218e99
+                         suffixes[i]);
218e99
+                break;
218e99
+            }
218e99
+            base = base * 1024;
218e99
+        }
218e99
+    }
218e99
+    return buf;
218e99
+}
218e99
+
218e99
+char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
218e99
+{
218e99
+    char buf1[128], date_buf[128], clock_buf[128];
218e99
+    struct tm tm;
218e99
+    time_t ti;
218e99
+    int64_t secs;
218e99
+
218e99
+    if (!sn) {
218e99
+        snprintf(buf, buf_size,
218e99
+                 "%-10s%-20s%7s%20s%15s",
218e99
+                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
218e99
+    } else {
218e99
+        ti = sn->date_sec;
218e99
+        localtime_r(&ti, &tm;;
218e99
+        strftime(date_buf, sizeof(date_buf),
218e99
+                 "%Y-%m-%d %H:%M:%S", &tm;;
218e99
+        secs = sn->vm_clock_nsec / 1000000000;
218e99
+        snprintf(clock_buf, sizeof(clock_buf),
218e99
+                 "%02d:%02d:%02d.%03d",
218e99
+                 (int)(secs / 3600),
218e99
+                 (int)((secs / 60) % 60),
218e99
+                 (int)(secs % 60),
218e99
+                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
218e99
+        snprintf(buf, buf_size,
218e99
+                 "%-10s%-20s%7s%20s%15s",
218e99
+                 sn->id_str, sn->name,
218e99
+                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
218e99
+                 date_buf,
218e99
+                 clock_buf);
218e99
+    }
218e99
+    return buf;
218e99
+}
218e99
+
218e99
+void bdrv_image_info_dump(ImageInfo *info)
218e99
+{
218e99
+    char size_buf[128], dsize_buf[128];
218e99
+    if (!info->has_actual_size) {
218e99
+        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
218e99
+    } else {
218e99
+        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
218e99
+                                info->actual_size);
218e99
+    }
218e99
+    get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
218e99
+    printf("image: %s\n"
218e99
+           "file format: %s\n"
218e99
+           "virtual size: %s (%" PRId64 " bytes)\n"
218e99
+           "disk size: %s\n",
218e99
+           info->filename, info->format, size_buf,
218e99
+           info->virtual_size,
218e99
+           dsize_buf);
218e99
+
218e99
+    if (info->has_encrypted && info->encrypted) {
218e99
+        printf("encrypted: yes\n");
218e99
+    }
218e99
+
218e99
+    if (info->has_cluster_size) {
218e99
+        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
218e99
+    }
218e99
+
218e99
+    if (info->has_dirty_flag && info->dirty_flag) {
218e99
+        printf("cleanly shut down: no\n");
218e99
+    }
218e99
+
218e99
+    if (info->has_backing_filename) {
218e99
+        printf("backing file: %s", info->backing_filename);
218e99
+        if (info->has_full_backing_filename) {
218e99
+            printf(" (actual path: %s)", info->full_backing_filename);
218e99
+        }
218e99
+        putchar('\n');
218e99
+        if (info->has_backing_filename_format) {
218e99
+            printf("backing file format: %s\n", info->backing_filename_format);
218e99
+        }
218e99
+    }
218e99
+
218e99
+    if (info->has_snapshots) {
218e99
+        SnapshotInfoList *elem;
218e99
+        char buf[256];
218e99
+
218e99
+        printf("Snapshot list:\n");
218e99
+        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
218e99
+
218e99
+        /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
218e99
+         * we convert to the block layer's native QEMUSnapshotInfo for now.
218e99
+         */
218e99
+        for (elem = info->snapshots; elem; elem = elem->next) {
218e99
+            QEMUSnapshotInfo sn = {
218e99
+                .vm_state_size = elem->value->vm_state_size,
218e99
+                .date_sec = elem->value->date_sec,
218e99
+                .date_nsec = elem->value->date_nsec,
218e99
+                .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
218e99
+                                 elem->value->vm_clock_nsec,
218e99
+            };
218e99
+
218e99
+            pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
218e99
+            pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
218e99
+            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
218e99
+        }
218e99
+    }
218e99
+}
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index fac1282..7695cae 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -7,11 +7,6 @@
218e99
 #include "block/coroutine.h"
218e99
 #include "qapi/qmp/qobject.h"
218e99
 #include "qapi-types.h"
218e99
-/*
218e99
- * snapshot.h is needed since bdrv_snapshot_dump(), it can be removed when the
218e99
- * function is moved to other file.
218e99
- */
218e99
-#include "block/snapshot.h"
218e99
 
218e99
 /* block.c */
218e99
 typedef struct BlockDriver BlockDriver;
218e99
@@ -353,12 +348,8 @@ void bdrv_get_backing_filename(BlockDriverState *bs,
218e99
                                char *filename, int filename_size);
218e99
 void bdrv_get_full_backing_filename(BlockDriverState *bs,
218e99
                                     char *dest, size_t sz);
218e99
-BlockInfo *bdrv_query_info(BlockDriverState *s);
218e99
-BlockStats *bdrv_query_stats(const BlockDriverState *bs);
218e99
 int bdrv_is_snapshot(BlockDriverState *bs);
218e99
-char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
218e99
 
218e99
-char *get_human_readable_size(char *buf, int buf_size, int64_t size);
218e99
 int path_is_absolute(const char *path);
218e99
 void path_combine(char *dest, int dest_size,
218e99
                   const char *base_path,
218e99
diff --git a/include/block/block_int.h b/include/block/block_int.h
218e99
index 696bbb9..9348238 100644
218e99
--- a/include/block/block_int.h
218e99
+++ b/include/block/block_int.h
218e99
@@ -33,6 +33,7 @@
218e99
 #include "qapi/qmp/qerror.h"
218e99
 #include "monitor/monitor.h"
218e99
 #include "qemu/hbitmap.h"
218e99
+#include "block/snapshot.h"
218e99
 
218e99
 #define BLOCK_FLAG_ENCRYPT          1
218e99
 #define BLOCK_FLAG_COMPAT6          4
218e99
diff --git a/include/block/qapi.h b/include/block/qapi.h
218e99
new file mode 100644
218e99
index 0000000..55d1848
218e99
--- /dev/null
218e99
+++ b/include/block/qapi.h
218e99
@@ -0,0 +1,41 @@
218e99
+/*
218e99
+ * Block layer qmp and info dump related functions
218e99
+ *
218e99
+ * Copyright (c) 2003-2008 Fabrice Bellard
218e99
+ *
218e99
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
218e99
+ * of this software and associated documentation files (the "Software"), to deal
218e99
+ * in the Software without restriction, including without limitation the rights
218e99
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
218e99
+ * copies of the Software, and to permit persons to whom the Software is
218e99
+ * furnished to do so, subject to the following conditions:
218e99
+ *
218e99
+ * The above copyright notice and this permission notice shall be included in
218e99
+ * all copies or substantial portions of the Software.
218e99
+ *
218e99
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
218e99
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218e99
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
218e99
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
218e99
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218e99
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
218e99
+ * THE SOFTWARE.
218e99
+ */
218e99
+
218e99
+#ifndef BLOCK_QAPI_H
218e99
+#define BLOCK_QAPI_H
218e99
+
218e99
+#include "qapi-types.h"
218e99
+#include "block/block.h"
218e99
+#include "block/snapshot.h"
218e99
+
218e99
+void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info);
218e99
+void bdrv_collect_image_info(BlockDriverState *bs,
218e99
+                             ImageInfo *info,
218e99
+                             const char *filename);
218e99
+BlockInfo *bdrv_query_info(BlockDriverState *s);
218e99
+BlockStats *bdrv_query_stats(const BlockDriverState *bs);
218e99
+
218e99
+char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
218e99
+void bdrv_image_info_dump(ImageInfo *info);
218e99
+#endif
218e99
diff --git a/qemu-img.c b/qemu-img.c
218e99
index 0f869f3..0bc084d 100644
218e99
--- a/qemu-img.c
218e99
+++ b/qemu-img.c
218e99
@@ -30,6 +30,7 @@
218e99
 #include "qemu/osdep.h"
218e99
 #include "sysemu/sysemu.h"
218e99
 #include "block/block_int.h"
218e99
+#include "block/qapi.h"
218e99
 #include <getopt.h>
218e99
 #include <stdio.h>
218e99
 #include <stdarg.h>
218e99
@@ -1605,39 +1606,6 @@ static void dump_json_image_info_list(ImageInfoList *list)
218e99
     QDECREF(str);
218e99
 }
218e99
 
218e99
-static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
218e99
-{
218e99
-    int i, sn_count;
218e99
-    QEMUSnapshotInfo *sn_tab = NULL;
218e99
-    SnapshotInfoList *info_list, *cur_item = NULL;
218e99
-    sn_count = bdrv_snapshot_list(bs, &sn_tab);
218e99
-
218e99
-    for (i = 0; i < sn_count; i++) {
218e99
-        info->has_snapshots = true;
218e99
-        info_list = g_new0(SnapshotInfoList, 1);
218e99
-
218e99
-        info_list->value                = g_new0(SnapshotInfo, 1);
218e99
-        info_list->value->id            = g_strdup(sn_tab[i].id_str);
218e99
-        info_list->value->name          = g_strdup(sn_tab[i].name);
218e99
-        info_list->value->vm_state_size = sn_tab[i].vm_state_size;
218e99
-        info_list->value->date_sec      = sn_tab[i].date_sec;
218e99
-        info_list->value->date_nsec     = sn_tab[i].date_nsec;
218e99
-        info_list->value->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
218e99
-        info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
218e99
-
218e99
-        /* XXX: waiting for the qapi to support qemu-queue.h types */
218e99
-        if (!cur_item) {
218e99
-            info->snapshots = cur_item = info_list;
218e99
-        } else {
218e99
-            cur_item->next = info_list;
218e99
-            cur_item = info_list;
218e99
-        }
218e99
-
218e99
-    }
218e99
-
218e99
-    g_free(sn_tab);
218e99
-}
218e99
-
218e99
 static void dump_json_image_info(ImageInfo *info)
218e99
 {
218e99
     Error *errp = NULL;
218e99
@@ -1655,122 +1623,6 @@ static void dump_json_image_info(ImageInfo *info)
218e99
     QDECREF(str);
218e99
 }
218e99
 
218e99
-static void collect_image_info(BlockDriverState *bs,
218e99
-                   ImageInfo *info,
218e99
-                   const char *filename,
218e99
-                   const char *fmt)
218e99
-{
218e99
-    uint64_t total_sectors;
218e99
-    char backing_filename[1024];
218e99
-    char backing_filename2[1024];
218e99
-    BlockDriverInfo bdi;
218e99
-
218e99
-    bdrv_get_geometry(bs, &total_sectors);
218e99
-
218e99
-    info->filename        = g_strdup(filename);
218e99
-    info->format          = g_strdup(bdrv_get_format_name(bs));
218e99
-    info->virtual_size    = total_sectors * 512;
218e99
-    info->actual_size     = bdrv_get_allocated_file_size(bs);
218e99
-    info->has_actual_size = info->actual_size >= 0;
218e99
-    if (bdrv_is_encrypted(bs)) {
218e99
-        info->encrypted = true;
218e99
-        info->has_encrypted = true;
218e99
-    }
218e99
-    if (bdrv_get_info(bs, &bdi) >= 0) {
218e99
-        if (bdi.cluster_size != 0) {
218e99
-            info->cluster_size = bdi.cluster_size;
218e99
-            info->has_cluster_size = true;
218e99
-        }
218e99
-        info->dirty_flag = bdi.is_dirty;
218e99
-        info->has_dirty_flag = true;
218e99
-    }
218e99
-    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
218e99
-    if (backing_filename[0] != '\0') {
218e99
-        info->backing_filename = g_strdup(backing_filename);
218e99
-        info->has_backing_filename = true;
218e99
-        bdrv_get_full_backing_filename(bs, backing_filename2,
218e99
-                                       sizeof(backing_filename2));
218e99
-
218e99
-        if (strcmp(backing_filename, backing_filename2) != 0) {
218e99
-            info->full_backing_filename =
218e99
-                        g_strdup(backing_filename2);
218e99
-            info->has_full_backing_filename = true;
218e99
-        }
218e99
-
218e99
-        if (bs->backing_format[0]) {
218e99
-            info->backing_filename_format = g_strdup(bs->backing_format);
218e99
-            info->has_backing_filename_format = true;
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
-static void dump_human_image_info(ImageInfo *info)
218e99
-{
218e99
-    char size_buf[128], dsize_buf[128];
218e99
-    if (!info->has_actual_size) {
218e99
-        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
218e99
-    } else {
218e99
-        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
218e99
-                                info->actual_size);
218e99
-    }
218e99
-    get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
218e99
-    printf("image: %s\n"
218e99
-           "file format: %s\n"
218e99
-           "virtual size: %s (%" PRId64 " bytes)\n"
218e99
-           "disk size: %s\n",
218e99
-           info->filename, info->format, size_buf,
218e99
-           info->virtual_size,
218e99
-           dsize_buf);
218e99
-
218e99
-    if (info->has_encrypted && info->encrypted) {
218e99
-        printf("encrypted: yes\n");
218e99
-    }
218e99
-
218e99
-    if (info->has_cluster_size) {
218e99
-        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
218e99
-    }
218e99
-
218e99
-    if (info->has_dirty_flag && info->dirty_flag) {
218e99
-        printf("cleanly shut down: no\n");
218e99
-    }
218e99
-
218e99
-    if (info->has_backing_filename) {
218e99
-        printf("backing file: %s", info->backing_filename);
218e99
-        if (info->has_full_backing_filename) {
218e99
-            printf(" (actual path: %s)", info->full_backing_filename);
218e99
-        }
218e99
-        putchar('\n');
218e99
-        if (info->has_backing_filename_format) {
218e99
-            printf("backing file format: %s\n", info->backing_filename_format);
218e99
-        }
218e99
-    }
218e99
-
218e99
-    if (info->has_snapshots) {
218e99
-        SnapshotInfoList *elem;
218e99
-        char buf[256];
218e99
-
218e99
-        printf("Snapshot list:\n");
218e99
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
218e99
-
218e99
-        /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
218e99
-         * we convert to the block layer's native QEMUSnapshotInfo for now.
218e99
-         */
218e99
-        for (elem = info->snapshots; elem; elem = elem->next) {
218e99
-            QEMUSnapshotInfo sn = {
218e99
-                .vm_state_size = elem->value->vm_state_size,
218e99
-                .date_sec = elem->value->date_sec,
218e99
-                .date_nsec = elem->value->date_nsec,
218e99
-                .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
218e99
-                                 elem->value->vm_clock_nsec,
218e99
-            };
218e99
-
218e99
-            pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
218e99
-            pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
218e99
-            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
218e99
-        }
218e99
-    }
218e99
-}
218e99
-
218e99
 static void dump_human_image_info_list(ImageInfoList *list)
218e99
 {
218e99
     ImageInfoList *elem;
218e99
@@ -1782,7 +1634,7 @@ static void dump_human_image_info_list(ImageInfoList *list)
218e99
         }
218e99
         delim = true;
218e99
 
218e99
-        dump_human_image_info(elem->value);
218e99
+        bdrv_image_info_dump(elem->value);
218e99
     }
218e99
 }
218e99
 
218e99
@@ -1832,8 +1684,8 @@ static ImageInfoList *collect_image_info_list(const char *filename,
218e99
         }
218e99
 
218e99
         info = g_new0(ImageInfo, 1);
218e99
-        collect_image_info(bs, info, filename, fmt);
218e99
-        collect_snapshots(bs, info);
218e99
+        bdrv_collect_image_info(bs, info, filename);
218e99
+        bdrv_collect_snapshots(bs, info);
218e99
 
218e99
         elem = g_new0(ImageInfoList, 1);
218e99
         elem->value = info;
218e99
diff --git a/savevm.c b/savevm.c
218e99
index 005bf35..f126bff 100644
218e99
--- a/savevm.c
218e99
+++ b/savevm.c
218e99
@@ -41,6 +41,7 @@
218e99
 #include "qemu/bitops.h"
218e99
 #include "qemu/iov.h"
218e99
 #include "block/snapshot.h"
218e99
+#include "block/qapi.h"
218e99
 
218e99
 #define SELF_ANNOUNCE_ROUNDS 5
218e99
 
218e99
-- 
218e99
1.7.1
218e99