Patchwork [V4,04/13] block: add snapshot info query function bdrv_query_snapshot_infolist()

login
register
mail settings
Submitter Wayne Xia
Date Jan. 17, 2013, 7:40 a.m.
Message ID <1358408410-22187-5-git-send-email-xiawenc@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/213175/
State New
Headers show

Comments

Wayne Xia - Jan. 17, 2013, 7:40 a.m.
This patch add function bdrv_query_snapshot_infolist(), which will
return snapshot info of an image in qmp object format. The implementation
code are mostly copied from qemu-img.c with modification to fit more
for qmp based block layer API.
  To help filter out snapshot info not needed, a call back function is
added in bdrv_query_snapshot_infolist().
  bdrv_can_read_snapshot() should be called before call this function,
to avoid got *errp set unexpectly.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block.c               |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |    7 +++++++
 2 files changed, 53 insertions(+), 0 deletions(-)
Eric Blake - Jan. 17, 2013, 9:53 p.m.
On 01/17/2013 12:40 AM, Wenchao Xia wrote:
>   This patch add function bdrv_query_snapshot_infolist(), which will
> return snapshot info of an image in qmp object format. The implementation
> code are mostly copied from qemu-img.c with modification to fit more
> for qmp based block layer API.
>   To help filter out snapshot info not needed, a call back function is
> added in bdrv_query_snapshot_infolist().
>   bdrv_can_read_snapshot() should be called before call this function,
> to avoid got *errp set unexpectly.

s/unexpectly/unexpectedly/

> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> ---
>  block.c               |   46 ++++++++++++++++++++++++++++++++++++++++++++++
>  include/block/block.h |    7 +++++++
>  2 files changed, 53 insertions(+), 0 deletions(-)
> 

> +SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
> +                                               SnapshotFilterFunc filter,
> +                                               void *opaque,
> +                                               Error **errp)
> +{
> +    int i, sn_count;
> +    QEMUSnapshotInfo *sn_tab = NULL;
> +    SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
> +
> +    sn_count = bdrv_snapshot_list(bs, &sn_tab);
> +    if (sn_count < 0) {
> +        /* Fix me:  set errp in bdrv_snapshot_list() instead of here */

You didn't remove this fixme anywhere in the series; it would be better
to respin things to avoid a net gain of fixmes.

> +        error_setg(errp, "bdrv_snapshot_list: error %d\n", sn_count);
> +        return NULL;
> +    }
Wayne Xia - Jan. 18, 2013, 1:57 a.m.
于 2013-1-18 5:53, Eric Blake 写道:
> On 01/17/2013 12:40 AM, Wenchao Xia wrote:
>>    This patch add function bdrv_query_snapshot_infolist(), which will
>> return snapshot info of an image in qmp object format. The implementation
>> code are mostly copied from qemu-img.c with modification to fit more
>> for qmp based block layer API.
>>    To help filter out snapshot info not needed, a call back function is
>> added in bdrv_query_snapshot_infolist().
>>    bdrv_can_read_snapshot() should be called before call this function,
>> to avoid got *errp set unexpectly.
>
> s/unexpectly/unexpectedly/
>
  sorry for it.

>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> Reviewed-by: Eric Blake <eblake@redhat.com>
>> ---
>>   block.c               |   46 ++++++++++++++++++++++++++++++++++++++++++++++
>>   include/block/block.h |    7 +++++++
>>   2 files changed, 53 insertions(+), 0 deletions(-)
>>
>
>> +SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
>> +                                               SnapshotFilterFunc filter,
>> +                                               void *opaque,
>> +                                               Error **errp)
>> +{
>> +    int i, sn_count;
>> +    QEMUSnapshotInfo *sn_tab = NULL;
>> +    SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
>> +
>> +    sn_count = bdrv_snapshot_list(bs, &sn_tab);
>> +    if (sn_count < 0) {
>> +        /* Fix me:  set errp in bdrv_snapshot_list() instead of here */
>
> You didn't remove this fixme anywhere in the series; it would be better
> to respin things to avoid a net gain of fixmes.
>
   yep, but solving that may need more work focusing on the function
enhancement and caller behavior which already exist, not quite related
to this serial. This serial is a bit long already, I'd like a
standalone patch for it later.

>> +        error_setg(errp, "bdrv_snapshot_list: error %d\n", sn_count);
>> +        return NULL;
>> +    }
>

Patch

diff --git a/block.c b/block.c
index 934bb3f..fba1be4 100644
--- a/block.c
+++ b/block.c
@@ -2842,6 +2842,52 @@  int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
     return 0;
 }
 
+SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
+                                               SnapshotFilterFunc filter,
+                                               void *opaque,
+                                               Error **errp)
+{
+    int i, sn_count;
+    QEMUSnapshotInfo *sn_tab = NULL;
+    SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
+
+    sn_count = bdrv_snapshot_list(bs, &sn_tab);
+    if (sn_count < 0) {
+        /* Fix me:  set errp in bdrv_snapshot_list() instead of here */
+        error_setg(errp, "bdrv_snapshot_list: error %d\n", sn_count);
+        return NULL;
+    }
+
+    for (i = 0; i < sn_count; i++) {
+        if (filter && filter(&sn_tab[i], opaque) != 0) {
+            continue;
+        }
+
+        info_list = g_new0(SnapshotInfoList, 1);
+
+        info_list->value                = g_new0(SnapshotInfo, 1);
+        info_list->value->id            = g_strdup(sn_tab[i].id_str);
+        info_list->value->name          = g_strdup(sn_tab[i].name);
+        info_list->value->vm_state_size = sn_tab[i].vm_state_size;
+        info_list->value->date_sec      = sn_tab[i].date_sec;
+        info_list->value->date_nsec     = sn_tab[i].date_nsec;
+        info_list->value->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
+        info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
+
+        /* XXX: waiting for the qapi to support qemu-queue.h types */
+        if (!cur_item) {
+            head = cur_item = info_list;
+        } else {
+            cur_item->next = info_list;
+            cur_item = info_list;
+        }
+
+    }
+
+    g_free(sn_tab);
+    return head;
+}
+
 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 b4c1612..ce18d4b 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -316,6 +316,13 @@  void bdrv_get_backing_filename(BlockDriverState *bs,
                                char *filename, int filename_size);
 void bdrv_get_full_backing_filename(BlockDriverState *bs,
                                     char *dest, size_t sz);
+
+typedef int (*SnapshotFilterFunc)(const QEMUSnapshotInfo *sn, void *opaque);
+/* assume bs is already opened, use qapi_free_* to free returned value. */
+SnapshotInfoList *bdrv_query_snapshot_infolist(BlockDriverState *bs,
+                                               SnapshotFilterFunc filter,
+                                               void *opaque,
+                                               Error **errp);
 BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_read_snapshot(BlockDriverState *bs);