Patchwork [V5,05/13] block: add image info query function bdrv_query_image_info()

login
register
mail settings
Submitter Wayne Xia
Date Jan. 24, 2013, 2:57 a.m.
Message ID <1358996283-32441-6-git-send-email-xiawenc@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/215107/
State New
Headers show

Comments

Wayne Xia - Jan. 24, 2013, 2:57 a.m.
This patch add function bdrv_query_image_info(), which will return
image info in qmp object format. The implementation code are mostly
copied from qemu-img.c, but use block layer function to get snapshot
info.
  A check with bdrv_can_read_snapshot(), was done before collecting
snapshot info.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block.c               |   73 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |    1 +
 2 files changed, 74 insertions(+), 0 deletions(-)
Kevin Wolf - Jan. 29, 2013, 12:55 p.m.
Am 24.01.2013 03:57, schrieb Wenchao Xia:
>   This patch add function bdrv_query_image_info(), which will return
> image info in qmp object format. The implementation code are mostly
> copied from qemu-img.c, but use block layer function to get snapshot
> info.

Don't copy code, reuse it.

Can you move the existing qemu-img code to block.c and make qemu-img use
it from there?

>   A check with bdrv_can_read_snapshot(), was done before collecting
> snapshot info.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> ---
>  block.c               |   73 +++++++++++++++++++++++++++++++++++++++++++++++++
>  include/block/block.h |    1 +
>  2 files changed, 74 insertions(+), 0 deletions(-)
> 
> diff --git a/block.c b/block.c
> index 7cdb6c6..14bf653 100644
> --- a/block.c
> +++ b/block.c
> @@ -2902,6 +2902,79 @@ SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
>      return head;
>  }
>  
> +/* collect all internal snapshot info in a image for ImageInfo */
> +static void collect_snapshots_info(BlockDriverState *bs,
> +                                   ImageInfo *info,
> +                                   Error **errp)
> +{
> +    SnapshotInfoList *info_list;
> +
> +    if (!bdrv_can_read_snapshot(bs)) {
> +        return;
> +    }
> +    info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp);
> +    if (info_list != NULL) {
> +        info->has_snapshots = true;
> +        info->snapshots = info_list;
> +    }
> +}
> +
> +static void collect_image_info(BlockDriverState *bs,
> +                               ImageInfo *info)
> +{
> +    uint64_t total_sectors;
> +    char backing_filename[1024];
> +    char backing_filename2[1024];
> +    BlockDriverInfo bdi;
> +    const char *filename;
> +
> +    filename = bdrv_get_filename(bs);
> +    bdrv_get_geometry(bs, &total_sectors);
> +
> +    info->filename        = g_strdup(filename);
> +    info->format          = g_strdup(bdrv_get_format_name(bs));
> +    info->virtual_size    = total_sectors * 512;
> +    info->actual_size     = bdrv_get_allocated_file_size(bs);
> +    info->has_actual_size = info->actual_size >= 0;
> +    if (bdrv_is_encrypted(bs)) {
> +        info->encrypted = true;
> +        info->has_encrypted = true;
> +    }
> +    if (bdrv_get_info(bs, &bdi) >= 0) {
> +        if (bdi.cluster_size != 0) {
> +            info->cluster_size = bdi.cluster_size;
> +            info->has_cluster_size = true;
> +        }
> +        info->dirty_flag = bdi.is_dirty;
> +        info->has_dirty_flag = true;
> +    }
> +    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));

No need to copy this, you can directly access bs->backing_file.

Kevin
Wayne Xia - Jan. 31, 2013, 9:06 a.m.
于 2013-1-29 20:55, Kevin Wolf 写道:
> Am 24.01.2013 03:57, schrieb Wenchao Xia:
>>    This patch add function bdrv_query_image_info(), which will return
>> image info in qmp object format. The implementation code are mostly
>> copied from qemu-img.c, but use block layer function to get snapshot
>> info.
>
> Don't copy code, reuse it.
>
> Can you move the existing qemu-img code to block.c and make qemu-img use
> it from there?
>
   A bit hard, original code are adjusted to form two API:image_info and
snapshot info. Those code can be moved to block.c in one patch and
in following patch modified, if you like.

>>    A check with bdrv_can_read_snapshot(), was done before collecting
>> snapshot info.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> Reviewed-by: Eric Blake <eblake@redhat.com>
>> ---
>>   block.c               |   73 +++++++++++++++++++++++++++++++++++++++++++++++++
>>   include/block/block.h |    1 +
>>   2 files changed, 74 insertions(+), 0 deletions(-)
>>
>> diff --git a/block.c b/block.c
>> index 7cdb6c6..14bf653 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -2902,6 +2902,79 @@ SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
>>       return head;
>>   }
>>
>> +/* collect all internal snapshot info in a image for ImageInfo */
>> +static void collect_snapshots_info(BlockDriverState *bs,
>> +                                   ImageInfo *info,
>> +                                   Error **errp)
>> +{
>> +    SnapshotInfoList *info_list;
>> +
>> +    if (!bdrv_can_read_snapshot(bs)) {
>> +        return;
>> +    }
>> +    info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp);
>> +    if (info_list != NULL) {
>> +        info->has_snapshots = true;
>> +        info->snapshots = info_list;
>> +    }
>> +}
>> +
>> +static void collect_image_info(BlockDriverState *bs,
>> +                               ImageInfo *info)
>> +{
>> +    uint64_t total_sectors;
>> +    char backing_filename[1024];
>> +    char backing_filename2[1024];
>> +    BlockDriverInfo bdi;
>> +    const char *filename;
>> +
>> +    filename = bdrv_get_filename(bs);
>> +    bdrv_get_geometry(bs, &total_sectors);
>> +
>> +    info->filename        = g_strdup(filename);
>> +    info->format          = g_strdup(bdrv_get_format_name(bs));
>> +    info->virtual_size    = total_sectors * 512;
>> +    info->actual_size     = bdrv_get_allocated_file_size(bs);
>> +    info->has_actual_size = info->actual_size >= 0;
>> +    if (bdrv_is_encrypted(bs)) {
>> +        info->encrypted = true;
>> +        info->has_encrypted = true;
>> +    }
>> +    if (bdrv_get_info(bs, &bdi) >= 0) {
>> +        if (bdi.cluster_size != 0) {
>> +            info->cluster_size = bdi.cluster_size;
>> +            info->has_cluster_size = true;
>> +        }
>> +        info->dirty_flag = bdi.is_dirty;
>> +        info->has_dirty_flag = true;
>> +    }
>> +    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
>
> No need to copy this, you can directly access bs->backing_file.
>
   OK.

> Kevin
>

Patch

diff --git a/block.c b/block.c
index 7cdb6c6..14bf653 100644
--- a/block.c
+++ b/block.c
@@ -2902,6 +2902,79 @@  SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
     return head;
 }
 
+/* collect all internal snapshot info in a image for ImageInfo */
+static void collect_snapshots_info(BlockDriverState *bs,
+                                   ImageInfo *info,
+                                   Error **errp)
+{
+    SnapshotInfoList *info_list;
+
+    if (!bdrv_can_read_snapshot(bs)) {
+        return;
+    }
+    info_list = bdrv_query_snapshot_infolist(bs, NULL, NULL, errp);
+    if (info_list != NULL) {
+        info->has_snapshots = true;
+        info->snapshots = info_list;
+    }
+}
+
+static void collect_image_info(BlockDriverState *bs,
+                               ImageInfo *info)
+{
+    uint64_t total_sectors;
+    char backing_filename[1024];
+    char backing_filename2[1024];
+    BlockDriverInfo bdi;
+    const char *filename;
+
+    filename = bdrv_get_filename(bs);
+    bdrv_get_geometry(bs, &total_sectors);
+
+    info->filename        = g_strdup(filename);
+    info->format          = g_strdup(bdrv_get_format_name(bs));
+    info->virtual_size    = total_sectors * 512;
+    info->actual_size     = bdrv_get_allocated_file_size(bs);
+    info->has_actual_size = info->actual_size >= 0;
+    if (bdrv_is_encrypted(bs)) {
+        info->encrypted = true;
+        info->has_encrypted = true;
+    }
+    if (bdrv_get_info(bs, &bdi) >= 0) {
+        if (bdi.cluster_size != 0) {
+            info->cluster_size = bdi.cluster_size;
+            info->has_cluster_size = true;
+        }
+        info->dirty_flag = bdi.is_dirty;
+        info->has_dirty_flag = true;
+    }
+    bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
+    if (backing_filename[0] != '\0') {
+        info->backing_filename = g_strdup(backing_filename);
+        info->has_backing_filename = true;
+        bdrv_get_full_backing_filename(bs, backing_filename2,
+                                       sizeof(backing_filename2));
+
+        if (strcmp(backing_filename, backing_filename2) != 0) {
+            info->full_backing_filename = g_strdup(backing_filename2);
+            info->has_full_backing_filename = true;
+        }
+
+        if (bs->backing_format[0]) {
+            info->backing_filename_format = g_strdup(bs->backing_format);
+            info->has_backing_filename_format = true;
+        }
+    }
+}
+
+ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp)
+{
+    ImageInfo *info = g_new0(ImageInfo, 1);
+    collect_image_info(bs, info);
+    collect_snapshots_info(bs, info, errp);
+    return info;
+}
+
 BlockInfo *bdrv_query_info(BlockDriverState *bs)
 {
     BlockInfo *info = g_malloc0(sizeof(*info));
diff --git a/include/block/block.h b/include/block/block.h
index ce18d4b..b6f80c3 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -323,6 +323,7 @@  SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
                                                SnapshotFilterFunc filter,
                                                void *opaque,
                                                Error **errp);
+ImageInfo *bdrv_query_image_info(BlockDriverState *bs, Error **errp);
 BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_read_snapshot(BlockDriverState *bs);