All of lore.kernel.org
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info
@ 2013-03-22 14:18 Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 01/17] block: move bdrv_snapshot_find() to block/snapshot.c Wenchao Xia
                   ` (17 more replies)
  0 siblings, 18 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  In the use of snapshot a way to retrieve related info at runtime is needed,
so this serial of patches will merge some code for qemu and qemu-img, and add
or enchance following interfaces for qemu:

1) qmp: query-block, show info for a block device include image info
Example:
-> { "execute": "query-block" }
<- {
      "return":[
         {
            "io-status": "ok",
            "device":"ide0-hd0",
            "locked":false,
            "removable":false,
            "inserted":{
               "ro":false,
               "drv":"qcow2",
               "encrypted":false,
               "file":"disks/test.img",
               "backing_file_depth":1,
               "bps":1000000,
               "bps_rd":0,
               "bps_wr":0,
               "iops":1000000,
               "iops_rd":0,
               "iops_wr":0,
               "image":{
                  "filename":"disks/test.img",
                  "format":"qcow2",
                  "virtual-size":2048000,
                  "backing_file":"base.img",
                  "full-backing-filename":"disks/base.img",
                  "backing-filename-format:"qcow2",
                  "snapshots":[
                     {
                        "id": "1",
                        "name": "snapshot1",
                        "vm-state-size": 0,
                        "date-sec": 10000200,
                        "date-nsec": 12,
                        "vm-clock-sec": 206,
                        "vm-clock-nsec": 30
                     }
                  ],
                  "backing-image":{
                      "filename":"disks/base.img",
                      "format":"qcow2",
                      "virtual-size":2048000
                  }
               }
            },
            "type":"unknown"
         },
         {
            "io-status": "ok",
            "device":"ide1-cd0",
            "locked":false,
            "removable":true,
            "type":"unknown"
         },
         {
            "device":"floppy0",
            "locked":false,
            "removable":true,
            "type":"unknown"
         },
         {
            "device":"sd0",
            "locked":false,
            "removable":true,
            "type":"unknown"
         }
      ]
   }

2) qmp: query-snapshots, show avaiable vm snapshot info which can be used
in loadvm.
Example:
-> { "execute": "query-snapshots" }
<- {
      "return":[
         {
            "id": "1",
            "name": "snapshot1",
            "vm-state-size": 0,
            "date-sec": 10000200,
            "date-nsec": 12,
            "vm-clock-sec": 206,
            "vm-clock-nsec": 30
         }
      ]
    }

3) hmp: info block, show what got in qmp query-block in monitor

  These patches follows the rule that use qmp to retieve information,
hmp layer just does a translation from qmp object it got.
  To make code graceful, snapshot and image info retrieving code in
qemu and qemu-img are merged into block layer, and some function name was
adjusted to make it tips better. For the part touch by the serial, it works as:

   qemu          qemu-img

dump_monitor    dump_stdout
     |--------------| 
            |
       block/qapi.c

v2:
  Rename and adjusted qmp interface according to comments from Eric.
  Spelling fix.
  Information retrieving function in block layer goes to seperated patch.
  Free qmp object after usage in hmp.
  Added counterpart in qmp-commands.hx.
  Better tips in qmp-schema.json.

v3:
  Spelling fix in commit message, patch 03/11.
  Spelling fix in code, patch 06/11.
  Add comments that vm-state-size is in bytes, and change size of it in
example to a reasonable number, patch 08/11.

v4:
  02/13: in bdrv_get_filename(), add const to parameter *bs.
  03/13: new added, in which the function correct the behavior in info
retrieving.
  04/13: in bdrv_query_snapshot_infolist(), remove NULL check before call
err_setg(), added TODO comments that let block layer function set error instead
of this layer to tip better for errors, Split out patch about image info to
patch 05/13.
  05/13: new splitted, and it checks *bs by calling bdrv_can_read_snapshot()
before collect internal snasphot info to avoid *err is set unexpectly now.
  06/13: check if error happens after calling bdrv_query_image_info().
  08/13: rename info to image in DeviceImageInfo and make it optional,
when device is not inserted it will be empty, added error handling code
when met error in calling block layer API.
  09/13: distinguish *id and *name in bdrv_find_snapshots(), caller
can choose what to search with. id_wellformed() should be called in
new snapshot creation interface above this function in the future.
  10/13: now this interface have addtional parameter *device, which
enable showing internal snapshots on a single device. Also use
bdrv_can_read_snapshot() instead of bdrv_can_snapshot() now.
  11/13: this function goes to hmp.c so hmp_handler_error is not exported
any more, split out patch that switch snapshot info function to patch 12/13.
  12/13: new splitted.
  13/13: use qmp API instead of directly calling block layer API, now
all hmp function have correspond qmp funtion in this serial.

v5:
  04/13: spelling fix in commit message, better comments and better
tips of error, after calling bdrv_snapshot_list().

v6:
  ==Address Kevin's comments==:
  04/14: seperate patch for code moving,
  05/14: use strerror() for error tipping after call of bdrv_snapshot_list(),
use "switch" instead of "if else" for error checking, replace
info_list->value->* by info->*.
  06/14: access bs->backing_file directly instead of calling of
bdrv_get_backing_filename in collect_image_info. Function switch patch
of last version is merged into this patch.
  08/14: API change, add optional parameter of "device" and "backing",
which allow showing all image info in the backing file chain of block
device, add related implemention.
  09/14: seperate patch for code moving.
  10/14: seperate patch for function change, return what got from
bdrv_snapshot_list() instead of -ENOENT.

  ==Address Eric's comments==:
  02/14: return bool instead of int for this new function.
  03/14: return bool instead of int for old function bdrv_can_snapshot().

V7:
  ==Address Eric's comments==:
  6/14: discard of static function bdrv_get_filename().
  8/14: better doc and comments, trailing '.' is removed.
  11/14: QMP API change: removed parameter *device, it only returns VM snapshot
now. Block device's internal snapshot info can be accessed by query-images.
  12/14: related caller code change for query-snapshots. Seperate internal
static function for VM snapshot info.
  14/14: HMP API change: added support of query internal snapshots of one image
in the backing file chain of a block device. use query-images instead of
query-snapshots. Seperate internal static mirror function for block snapshot
info.

v8:
  General change:
    discard bdrv_can_read_snapshot() for that return value can tip error. add
block/snapshot.c and block/qapi.c and function are moved there. Patches 17-20
are for clean and moved to tail.

  Address Markus's comments:
  3/20: function was moved to block/snapshot.c instead of block.c.
  7/20: better commit message. Remove comments for the return value of
bdrv_snapshot_list(), remove period suffix in error message, better error
message, remove the filter callback of it. remove comments for *bs must be
open. It return negative value for error check.
  8/20: add a flag parameter to filter out inconsistent vm snapshots instead
of a call back function.
  9/20: function reorganized to return negative error value, and qmp object
will be got only on success.
  10/20: better documents and remove suffix period.
  11/20: better documents and remove suffix period, explain optional member
in qmp-commands.hx.

  Address Stefan's comments:
  6/20: add prefix 'bdrv_' to the moved functions.
  7/20: remove the filter call back function. remove bdrv_can_reand_snapshot()
call in qemu-img. It return negative value for error check.
  8/20: add a flag parameter to filter out inconsistent vm snapshots instead
of a call back function.
  9/20: small trivial static function was removed.

  Address Eric's comments:
  6/20: add prefix 'bdrv_' to the moved functions. Added message in commit
about the patch's purpose.

  Other:
  14/20 - 16/20: new interface info image which show all image info
including internal snapshots for each device, share the dump code
with qemu-img.
  18/20: new clean up patch for block/snapshot.c.
  19/20: new clean up patch for block/qapi.c.

V9:
  General change:
  Drop 4 cleaning patches in the tail in laster version, to make the serial
shorter, which can be submitted later.

  Address Eric's comments:
  1/14: Merge the patch with file adding patch, add copyright note in the
header file, fix the argument number issue in last version.
  3/14: Better commit message.
  4/14: Merge the patch with file adding patch, add copyright note in the
header file.
  6/14: Better document and comments, rename snapshot_filter_vm() to
snapshot_valid_for_vm() and return bool now.
  7/14: Better commit message.
  8/14: Remove the free of *list on fail calling
bdrv_query_snapshot_info_list(), better commit message and documents.
  9/14: Better documents.

V10:
  General change:
  use original license in new files. Use query-block to show the info
instead of new API query-images, related hmp change in patches 16/17, 17/17.

  Address Eric's comments:
  6/17: check 'bs1 != bs' first as optimization.
  8/17, 10/17, 11/17: new patches recusively show image info in the backing
chain in command query-block.

Wenchao Xia (17):
  1 block: move bdrv_snapshot_find() to block/snapshot.c
  2 block: distinguish id and name in bdrv_find_snapshot()
  3 qemu-img: remove unused parameter in collect_image_info()
  4 block: move collect_snapshots() and collect_image_info() to block/qapi.c
  5 block: add snapshot info query function bdrv_query_snapshot_info_list()
  6 block: add check for VM snapshot in bdrv_query_snapshot_info_list()
  7 block: add image info query function bdrv_query_image_info()
  8 block: move qmp_query_block() and bdrv_query_info() to block/qapi.c
  9 qmp: add interface query-snapshots
  10 qmp: add recursive member in ImageInfo
  11 qmp: add ImageInfo in BlockDeviceInfo used by query-block
  12 hmp: add function hmp_info_snapshots()
  13 hmp: switch snapshot info function to qmp based one
  14 block: move dump_human_image_info() to block/qapi.c
  15 block: dump to buffer for bdrv_image_info_dump()
  16 hmp: show ImageInfo in 'info block'
  17 hmp: add parameter device and -b for info block

 block.c                  |   76 --------
 block/Makefile.objs      |    1 +
 block/qapi.c             |  423 ++++++++++++++++++++++++++++++++++++++++++++++
 block/snapshot.c         |   78 +++++++++
 hmp.c                    |   68 ++++++++
 hmp.h                    |    1 +
 include/block/block.h    |    1 -
 include/block/qapi.h     |   42 +++++
 include/block/snapshot.h |   37 ++++
 monitor.c                |    9 +-
 qapi-schema.json         |   24 +++-
 qemu-img.c               |  162 +-----------------
 qmp-commands.hx          |  122 +++++++++++++-
 savevm.c                 |   95 +----------
 14 files changed, 812 insertions(+), 327 deletions(-)
 create mode 100644 block/qapi.c
 create mode 100644 block/snapshot.c
 create mode 100644 include/block/qapi.h
 create mode 100644 include/block/snapshot.h

^ permalink raw reply	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 01/17] block: move bdrv_snapshot_find() to block/snapshot.c
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
@ 2013-03-22 14:18 ` Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 02/17] block: distinguish id and name in bdrv_find_snapshot() Wenchao Xia
                   ` (16 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch adds block/snapshot.c and then moves the function
there. It also fixes small code style errors reported by check script.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/Makefile.objs      |    1 +
 block/snapshot.c         |   48 ++++++++++++++++++++++++++++++++++++++++++++++
 include/block/snapshot.h |   37 +++++++++++++++++++++++++++++++++++
 savevm.c                 |   23 +---------------------
 4 files changed, 87 insertions(+), 22 deletions(-)
 create mode 100644 block/snapshot.c
 create mode 100644 include/block/snapshot.h

diff --git a/block/Makefile.objs b/block/Makefile.objs
index c067f38..60a4cd2 100644
--- a/block/Makefile.objs
+++ b/block/Makefile.objs
@@ -3,6 +3,7 @@ block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-c
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += parallels.o blkdebug.o blkverify.o
+block-obj-y += snapshot.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
diff --git a/block/snapshot.c b/block/snapshot.c
new file mode 100644
index 0000000..c47a899
--- /dev/null
+++ b/block/snapshot.c
@@ -0,0 +1,48 @@
+/*
+ * Block layer snapshot related functions
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "block/snapshot.h"
+
+int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
+                       const char *name)
+{
+    QEMUSnapshotInfo *sn_tab, *sn;
+    int nb_sns, i, ret;
+
+    ret = -ENOENT;
+    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    if (nb_sns < 0) {
+        return ret;
+    }
+    for (i = 0; i < nb_sns; i++) {
+        sn = &sn_tab[i];
+        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
+            *sn_info = *sn;
+            ret = 0;
+            break;
+        }
+    }
+    g_free(sn_tab);
+    return ret;
+}
diff --git a/include/block/snapshot.h b/include/block/snapshot.h
new file mode 100644
index 0000000..4ad070c
--- /dev/null
+++ b/include/block/snapshot.h
@@ -0,0 +1,37 @@
+/*
+ * Block layer snapshot related functions
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef SNAPSHOT_H
+#define SNAPSHOT_H
+
+#include "qemu-common.h"
+/*
+ * block.h is needed for QEMUSnapshotInfo, it can be removed when define is
+ * moved here.
+ */
+#include "block.h"
+
+int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
+                       const char *name);
+#endif
diff --git a/savevm.c b/savevm.c
index 35c8d1e..82cb0e7 100644
--- a/savevm.c
+++ b/savevm.c
@@ -39,6 +39,7 @@
 #include "qmp-commands.h"
 #include "trace.h"
 #include "qemu/bitops.h"
+#include "block/snapshot.h"
 
 #define SELF_ANNOUNCE_ROUNDS 5
 
@@ -2085,28 +2086,6 @@ out:
     return ret;
 }
 
-static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
-                              const char *name)
-{
-    QEMUSnapshotInfo *sn_tab, *sn;
-    int nb_sns, i, ret;
-
-    ret = -ENOENT;
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
-    if (nb_sns < 0)
-        return ret;
-    for(i = 0; i < nb_sns; i++) {
-        sn = &sn_tab[i];
-        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
-            *sn_info = *sn;
-            ret = 0;
-            break;
-        }
-    }
-    g_free(sn_tab);
-    return ret;
-}
-
 /*
  * Deletes snapshots of a given name in all opened images.
  */
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 02/17] block: distinguish id and name in bdrv_find_snapshot()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 01/17] block: move bdrv_snapshot_find() to block/snapshot.c Wenchao Xia
@ 2013-03-22 14:18 ` Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 03/17] qemu-img: remove unused parameter in collect_image_info() Wenchao Xia
                   ` (15 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  To make it clear about id and name in searching, the API is changed
a bit to distinguish them, and caller can choose to search by id or name.
Searching will be done with higher priority of id. This function also
returns negative value from bdrv_snapshot_list() instead of -ENOENT on
error now.

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

diff --git a/block/snapshot.c b/block/snapshot.c
index c47a899..7b2026c 100644
--- a/block/snapshot.c
+++ b/block/snapshot.c
@@ -24,8 +24,20 @@
 
 #include "block/snapshot.h"
 
+/*
+ * Try find an internal snapshot with @id or @name, @id have higher priority
+ * in searching.
+ *
+ * @bs: block device to search on, must not be NULL.
+ * @sn_info: snapshot information to be filled in, must not be NULL.
+ * @id: snapshot id to search with, can be NULL.
+ * @name: snapshot name to search with, can be NULL.
+ *
+ * returns 0 and @sn_info is filled with related information if found,
+ * otherwise it returns negative value.
+ */
 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
-                       const char *name)
+                       const char *id, const char *name)
 {
     QEMUSnapshotInfo *sn_tab, *sn;
     int nb_sns, i, ret;
@@ -33,16 +45,34 @@ int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
     ret = -ENOENT;
     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
     if (nb_sns < 0) {
-        return ret;
+        return nb_sns;
     }
-    for (i = 0; i < nb_sns; i++) {
-        sn = &sn_tab[i];
-        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
-            *sn_info = *sn;
-            ret = 0;
-            break;
+
+    /* search by id */
+    if (id) {
+        for (i = 0; i < nb_sns; i++) {
+            sn = &sn_tab[i];
+            if (!strcmp(sn->id_str, id)) {
+                *sn_info = *sn;
+                ret = 0;
+                goto out;
+            }
         }
     }
+
+    /* search by name */
+    if (name) {
+        for (i = 0; i < nb_sns; i++) {
+            sn = &sn_tab[i];
+            if (!strcmp(sn->name, name)) {
+                *sn_info = *sn;
+                ret = 0;
+                goto out;
+            }
+        }
+    }
+
+ out:
     g_free(sn_tab);
     return ret;
 }
diff --git a/include/block/snapshot.h b/include/block/snapshot.h
index 4ad070c..a047a8e 100644
--- a/include/block/snapshot.h
+++ b/include/block/snapshot.h
@@ -33,5 +33,5 @@
 #include "block.h"
 
 int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
-                       const char *name);
+                       const char *id, const char *name);
 #endif
diff --git a/savevm.c b/savevm.c
index 82cb0e7..04fcd10 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2098,7 +2098,7 @@ static int del_existing_snapshots(Monitor *mon, const char *name)
     bs = NULL;
     while ((bs = bdrv_next(bs))) {
         if (bdrv_can_snapshot(bs) &&
-            bdrv_snapshot_find(bs, snapshot, name) >= 0)
+            bdrv_snapshot_find(bs, snapshot, name, name) >= 0)
         {
             ret = bdrv_snapshot_delete(bs, name);
             if (ret < 0) {
@@ -2158,7 +2158,7 @@ void do_savevm(Monitor *mon, const QDict *qdict)
     sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
 
     if (name) {
-        ret = bdrv_snapshot_find(bs, old_sn, name);
+        ret = bdrv_snapshot_find(bs, old_sn, name, name);
         if (ret >= 0) {
             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
@@ -2249,7 +2249,7 @@ int load_vmstate(const char *name)
     }
 
     /* Don't even try to load empty VM states */
-    ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
+    ret = bdrv_snapshot_find(bs_vm_state, &sn, name, name);
     if (ret < 0) {
         return ret;
     } else if (sn.vm_state_size == 0) {
@@ -2273,7 +2273,7 @@ int load_vmstate(const char *name)
             return -ENOTSUP;
         }
 
-        ret = bdrv_snapshot_find(bs, &sn, name);
+        ret = bdrv_snapshot_find(bs, &sn, name, name);
         if (ret < 0) {
             error_report("Device '%s' does not have the requested snapshot '%s'",
                            bdrv_get_device_name(bs), name);
@@ -2379,7 +2379,7 @@ void do_info_snapshots(Monitor *mon, const QDict *qdict)
 
         while ((bs1 = bdrv_next(bs1))) {
             if (bdrv_can_snapshot(bs1) && bs1 != bs) {
-                ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
+                ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str, NULL);
                 if (ret < 0) {
                     available = 0;
                     break;
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 03/17] qemu-img: remove unused parameter in collect_image_info()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 01/17] block: move bdrv_snapshot_find() to block/snapshot.c Wenchao Xia
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 02/17] block: distinguish id and name in bdrv_find_snapshot() Wenchao Xia
@ 2013-03-22 14:18 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 04/17] block: move collect_snapshots() and collect_image_info() to block/qapi.c Wenchao Xia
                   ` (14 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:18 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  Parameter *fmt was not used, so remove it.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 qemu-img.c |    5 ++---
 1 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/qemu-img.c b/qemu-img.c
index 31627b0..937ec01 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1640,8 +1640,7 @@ static void dump_json_image_info(ImageInfo *info)
 
 static void collect_image_info(BlockDriverState *bs,
                    ImageInfo *info,
-                   const char *filename,
-                   const char *fmt)
+                   const char *filename)
 {
     uint64_t total_sectors;
     char backing_filename[1024];
@@ -1815,7 +1814,7 @@ static ImageInfoList *collect_image_info_list(const char *filename,
         }
 
         info = g_new0(ImageInfo, 1);
-        collect_image_info(bs, info, filename, fmt);
+        collect_image_info(bs, info, filename);
         collect_snapshots(bs, info);
 
         elem = g_new0(ImageInfoList, 1);
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 04/17] block: move collect_snapshots() and collect_image_info() to block/qapi.c
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (2 preceding siblings ...)
  2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 03/17] qemu-img: remove unused parameter in collect_image_info() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list() Wenchao Xia
                   ` (13 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch adds block/qapi.c and moves the functions there. To avoid
conflict and tip better, macro in header file is BLOCK_QAPI_H instead
of QAPI_H. The moving is for making review easier, those functions
will be modified and renamed later.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/Makefile.objs  |    2 +-
 block/qapi.c         |  107 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/qapi.h |   35 ++++++++++++++++
 qemu-img.c           |   86 +--------------------------------------
 4 files changed, 146 insertions(+), 84 deletions(-)
 create mode 100644 block/qapi.c
 create mode 100644 include/block/qapi.h

diff --git a/block/Makefile.objs b/block/Makefile.objs
index 60a4cd2..fc27bed 100644
--- a/block/Makefile.objs
+++ b/block/Makefile.objs
@@ -3,7 +3,7 @@ block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-c
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += parallels.o blkdebug.o blkverify.o
-block-obj-y += snapshot.o
+block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
diff --git a/block/qapi.c b/block/qapi.c
new file mode 100644
index 0000000..e2b1b6b
--- /dev/null
+++ b/block/qapi.c
@@ -0,0 +1,107 @@
+/*
+ * Block layer qmp related functions
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "block/qapi.h"
+#include "block/block_int.h"
+
+void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info)
+{
+    int i, sn_count;
+    QEMUSnapshotInfo *sn_tab = NULL;
+    SnapshotInfoList *info_list, *cur_item = NULL;
+    sn_count = bdrv_snapshot_list(bs, &sn_tab);
+
+    for (i = 0; i < sn_count; i++) {
+        info->has_snapshots = true;
+        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) {
+            info->snapshots = cur_item = info_list;
+        } else {
+            cur_item->next = info_list;
+            cur_item = info_list;
+        }
+
+    }
+
+    g_free(sn_tab);
+}
+
+void bdrv_collect_image_info(BlockDriverState *bs,
+                             ImageInfo *info,
+                             const char *filename)
+{
+    uint64_t total_sectors;
+    char backing_filename[1024];
+    char backing_filename2[1024];
+    BlockDriverInfo bdi;
+
+    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;
+        }
+    }
+}
diff --git a/include/block/qapi.h b/include/block/qapi.h
new file mode 100644
index 0000000..4586578
--- /dev/null
+++ b/include/block/qapi.h
@@ -0,0 +1,35 @@
+/*
+ * Block layer qmp related functions
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef BLOCK_QAPI_H
+#define BLOCK_QAPI_H
+
+#include "qapi-types.h"
+#include "block/block.h"
+
+void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info);
+void bdrv_collect_image_info(BlockDriverState *bs,
+                             ImageInfo *info,
+                             const char *filename);
+#endif
diff --git a/qemu-img.c b/qemu-img.c
index 937ec01..a020ccc 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -30,6 +30,7 @@
 #include "qemu/osdep.h"
 #include "sysemu/sysemu.h"
 #include "block/block_int.h"
+#include "block/qapi.h"
 #include <getopt.h>
 #include <stdio.h>
 #include <stdarg.h>
@@ -1588,39 +1589,6 @@ static void dump_json_image_info_list(ImageInfoList *list)
     QDECREF(str);
 }
 
-static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
-{
-    int i, sn_count;
-    QEMUSnapshotInfo *sn_tab = NULL;
-    SnapshotInfoList *info_list, *cur_item = NULL;
-    sn_count = bdrv_snapshot_list(bs, &sn_tab);
-
-    for (i = 0; i < sn_count; i++) {
-        info->has_snapshots = true;
-        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) {
-            info->snapshots = cur_item = info_list;
-        } else {
-            cur_item->next = info_list;
-            cur_item = info_list;
-        }
-
-    }
-
-    g_free(sn_tab);
-}
-
 static void dump_json_image_info(ImageInfo *info)
 {
     Error *errp = NULL;
@@ -1638,54 +1606,6 @@ static void dump_json_image_info(ImageInfo *info)
     QDECREF(str);
 }
 
-static void collect_image_info(BlockDriverState *bs,
-                   ImageInfo *info,
-                   const char *filename)
-{
-    uint64_t total_sectors;
-    char backing_filename[1024];
-    char backing_filename2[1024];
-    BlockDriverInfo bdi;
-
-    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;
-        }
-    }
-}
-
 static void dump_human_image_info(ImageInfo *info)
 {
     char size_buf[128], dsize_buf[128];
@@ -1814,8 +1734,8 @@ static ImageInfoList *collect_image_info_list(const char *filename,
         }
 
         info = g_new0(ImageInfo, 1);
-        collect_image_info(bs, info, filename);
-        collect_snapshots(bs, info);
+        bdrv_collect_image_info(bs, info, filename);
+        bdrv_collect_snapshots(bs, info);
 
         elem = g_new0(ImageInfoList, 1);
         elem->value = info;
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (3 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 04/17] block: move collect_snapshots() and collect_image_info() to block/qapi.c Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-27 21:31   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 06/17] block: add check for VM snapshot in bdrv_query_snapshot_info_list() Wenchao Xia
                   ` (12 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch adds function bdrv_query_snapshot_info_list(), which will
retrieve snapshot info of an image in qmp object format. The implementation
is based on the code moved from qemu-img.c with modification to fit more
for qmp based block layer API.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/qapi.c         |   52 +++++++++++++++++++++++++++++++++++++------------
 include/block/qapi.h |    4 ++-
 qemu-img.c           |    4 ++-
 3 files changed, 45 insertions(+), 15 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index e2b1b6b..119f1f9 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -25,29 +25,53 @@
 #include "block/qapi.h"
 #include "block/block_int.h"
 
-void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info)
+/* return 0 on success, and @p_list will be set only on success. */
+int bdrv_query_snapshot_info_list(BlockDriverState *bs,
+                                  SnapshotInfoList **p_list,
+                                  Error **errp)
 {
     int i, sn_count;
     QEMUSnapshotInfo *sn_tab = NULL;
-    SnapshotInfoList *info_list, *cur_item = NULL;
+    SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
+    SnapshotInfo *info;
+
     sn_count = bdrv_snapshot_list(bs, &sn_tab);
+    if (sn_count < 0) {
+        const char *dev = bdrv_get_device_name(bs);
+        switch (sn_count) {
+        case -ENOMEDIUM:
+            error_setg(errp, "Device '%s' is not inserted", dev);
+            break;
+        case -ENOTSUP:
+            error_setg(errp,
+                       "Device '%s' does not support internal snapshots",
+                       dev);
+            break;
+        default:
+            error_setg(errp, "Can't list snapshots of device '%s': %s",
+                       dev, strerror(-sn_count));
+            break;
+        }
+        return sn_count;
+    }
 
     for (i = 0; i < sn_count; i++) {
-        info->has_snapshots = true;
-        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;
+        info = g_new0(SnapshotInfo, 1);
+        info->id            = g_strdup(sn_tab[i].id_str);
+        info->name          = g_strdup(sn_tab[i].name);
+        info->vm_state_size = sn_tab[i].vm_state_size;
+        info->date_sec      = sn_tab[i].date_sec;
+        info->date_nsec     = sn_tab[i].date_nsec;
+        info->vm_clock_sec  = sn_tab[i].vm_clock_nsec / 1000000000;
+        info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
+
+        info_list = g_new0(SnapshotInfoList, 1);
+        info_list->value = info;
 
         /* XXX: waiting for the qapi to support qemu-queue.h types */
         if (!cur_item) {
-            info->snapshots = cur_item = info_list;
+            head = cur_item = info_list;
         } else {
             cur_item->next = info_list;
             cur_item = info_list;
@@ -56,6 +80,8 @@ void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info)
     }
 
     g_free(sn_tab);
+    *p_list = head;
+    return 0;
 }
 
 void bdrv_collect_image_info(BlockDriverState *bs,
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 4586578..91dc41b 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -28,7 +28,9 @@
 #include "qapi-types.h"
 #include "block/block.h"
 
-void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info);
+int bdrv_query_snapshot_info_list(BlockDriverState *bs,
+                                  SnapshotInfoList **p_list,
+                                  Error **errp);
 void bdrv_collect_image_info(BlockDriverState *bs,
                              ImageInfo *info,
                              const char *filename);
diff --git a/qemu-img.c b/qemu-img.c
index a020ccc..9fb33e1 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1735,7 +1735,9 @@ static ImageInfoList *collect_image_info_list(const char *filename,
 
         info = g_new0(ImageInfo, 1);
         bdrv_collect_image_info(bs, info, filename);
-        bdrv_collect_snapshots(bs, info);
+        if (!bdrv_query_snapshot_info_list(bs, &info->snapshots, NULL)) {
+            info->has_snapshots = true;
+        }
 
         elem = g_new0(ImageInfoList, 1);
         elem->value = info;
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 06/17] block: add check for VM snapshot in bdrv_query_snapshot_info_list()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (4 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 07/17] block: add image info query function bdrv_query_image_info() Wenchao Xia
                   ` (11 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch adds a parameter to tell whether return valid snapshots
for whole VM only.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/qapi.c         |   42 ++++++++++++++++++++++++++++++++++++++++--
 include/block/qapi.h |    1 +
 qemu-img.c           |    3 ++-
 3 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 119f1f9..007e691 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -23,11 +23,47 @@
  */
 
 #include "block/qapi.h"
+#include "block/snapshot.h"
 #include "block/block_int.h"
 
-/* return 0 on success, and @p_list will be set only on success. */
+/*
+ * check whether the snapshot is valid for whole vm.
+ *
+ * @sn: snapshot info to be checked.
+ * @bs: where @sn was found.
+ *
+ * return true if the snapshot is consistent for the VM.
+ */
+static bool snapshot_valid_for_vm(const QEMUSnapshotInfo *sn,
+                                  BlockDriverState *bs)
+{
+    BlockDriverState *bs1 = NULL;
+    QEMUSnapshotInfo s, *sn_info = &s;
+    int ret;
+
+    /* Check logic is connected with load_vmstate():
+       Only check the devices that can snapshot, other devices that can't
+       take snapshot, for example, readonly ones, will be ignored in
+       load_vmstate(). */
+    while ((bs1 = bdrv_next(bs1))) {
+        if (bs1 != bs && bdrv_can_snapshot(bs1)) {
+            ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str, NULL);
+            if (ret < 0) {
+                return false;
+            }
+        }
+    }
+    return true;
+}
+
+/*
+ * return 0 on success, and @p_list will be set only on success. If
+ * @vm_snapshot is true, limit the results to snapshots valid for the
+ * whole VM.
+ */
 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
                                   SnapshotInfoList **p_list,
+                                  bool vm_snapshot,
                                   Error **errp)
 {
     int i, sn_count;
@@ -56,7 +92,9 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
     }
 
     for (i = 0; i < sn_count; i++) {
-
+        if (vm_snapshot && !snapshot_valid_for_vm(&sn_tab[i], bs)) {
+            continue;
+        }
         info = g_new0(SnapshotInfo, 1);
         info->id            = g_strdup(sn_tab[i].id_str);
         info->name          = g_strdup(sn_tab[i].name);
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 91dc41b..fe66053 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -30,6 +30,7 @@
 
 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
                                   SnapshotInfoList **p_list,
+                                  bool vm_snapshot,
                                   Error **errp);
 void bdrv_collect_image_info(BlockDriverState *bs,
                              ImageInfo *info,
diff --git a/qemu-img.c b/qemu-img.c
index 9fb33e1..98ae4b1 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1735,7 +1735,8 @@ static ImageInfoList *collect_image_info_list(const char *filename,
 
         info = g_new0(ImageInfo, 1);
         bdrv_collect_image_info(bs, info, filename);
-        if (!bdrv_query_snapshot_info_list(bs, &info->snapshots, NULL)) {
+        if (!bdrv_query_snapshot_info_list(bs, &info->snapshots,
+                                          false, NULL)) {
             info->has_snapshots = true;
         }
 
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 07/17] block: add image info query function bdrv_query_image_info()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (5 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 06/17] block: add check for VM snapshot in bdrv_query_snapshot_info_list() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c Wenchao Xia
                   ` (10 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch adds function bdrv_query_image_info(), which will
retrieve image info in qmp object format. The implementation is
based on the code moved from qemu-img.c, but uses block layer
function to get snapshot info.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/qapi.c         |   39 ++++++++++++++++++++++++++++++++-------
 include/block/qapi.h |    6 +++---
 qemu-img.c           |    7 ++-----
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 007e691..4cf33ab 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -122,18 +122,22 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
     return 0;
 }
 
-void bdrv_collect_image_info(BlockDriverState *bs,
-                             ImageInfo *info,
-                             const char *filename)
+/* return 0 on success, and @p_info will be set only on success. */
+int bdrv_query_image_info(BlockDriverState *bs,
+                          ImageInfo **p_info,
+                          Error **errp)
 {
     uint64_t total_sectors;
-    char backing_filename[1024];
+    const char *backing_filename;
     char backing_filename2[1024];
     BlockDriverInfo bdi;
+    int ret;
+    Error *err = NULL;
+    ImageInfo *info = g_new0(ImageInfo, 1);
 
     bdrv_get_geometry(bs, &total_sectors);
 
-    info->filename        = g_strdup(filename);
+    info->filename        = g_strdup(bs->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);
@@ -150,8 +154,8 @@ void bdrv_collect_image_info(BlockDriverState *bs,
         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') {
+    backing_filename = bs->backing_file;
+    if (backing_filename && backing_filename[0] != '\0') {
         info->backing_filename = g_strdup(backing_filename);
         info->has_backing_filename = true;
         bdrv_get_full_backing_filename(bs, backing_filename2,
@@ -168,4 +172,25 @@ void bdrv_collect_image_info(BlockDriverState *bs,
             info->has_backing_filename_format = true;
         }
     }
+
+    ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, false, &err);
+    switch (ret) {
+    case 0:
+        info->has_snapshots = true;
+        break;
+    /* recoverable error */
+    case -ENOMEDIUM:
+        error_free(err);
+        break;
+    case -ENOTSUP:
+        error_free(err);
+        break;
+    default:
+        error_propagate(errp, err);
+        qapi_free_ImageInfo(info);
+        return ret;
+    }
+
+    *p_info = info;
+    return 0;
 }
diff --git a/include/block/qapi.h b/include/block/qapi.h
index fe66053..2c62fdf 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -32,7 +32,7 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
                                   SnapshotInfoList **p_list,
                                   bool vm_snapshot,
                                   Error **errp);
-void bdrv_collect_image_info(BlockDriverState *bs,
-                             ImageInfo *info,
-                             const char *filename);
+int bdrv_query_image_info(BlockDriverState *bs,
+                          ImageInfo **p_info,
+                          Error **errp);
 #endif
diff --git a/qemu-img.c b/qemu-img.c
index 98ae4b1..1dd0a60 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1733,11 +1733,8 @@ static ImageInfoList *collect_image_info_list(const char *filename,
             goto err;
         }
 
-        info = g_new0(ImageInfo, 1);
-        bdrv_collect_image_info(bs, info, filename);
-        if (!bdrv_query_snapshot_info_list(bs, &info->snapshots,
-                                          false, NULL)) {
-            info->has_snapshots = true;
+        if (bdrv_query_image_info(bs, &info, NULL)) {
+            goto err;
         }
 
         elem = g_new0(ImageInfoList, 1);
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (6 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 07/17] block: add image info query function bdrv_query_image_info() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-29 20:10   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 09/17] qmp: add interface query-snapshots Wenchao Xia
                   ` (9 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This is a code move patch, except in qmp_query_block bdrv_next(bs)
is used instead of direct traverse of global array 'bdrv_states'.
  This patch also fix code style error reported by check script.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block.c               |   76 ------------------------------------------------
 block/qapi.c          |   77 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h |    1 -
 include/block/qapi.h  |    1 +
 4 files changed, 78 insertions(+), 77 deletions(-)

diff --git a/block.c b/block.c
index 0a062c9..c811350 100644
--- a/block.c
+++ b/block.c
@@ -2916,82 +2916,6 @@ int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
     return data.ret;
 }
 
-BlockInfo *bdrv_query_info(BlockDriverState *bs)
-{
-    BlockInfo *info = g_malloc0(sizeof(*info));
-    info->device = g_strdup(bs->device_name);
-    info->type = g_strdup("unknown");
-    info->locked = bdrv_dev_is_medium_locked(bs);
-    info->removable = bdrv_dev_has_removable_media(bs);
-
-    if (bdrv_dev_has_removable_media(bs)) {
-        info->has_tray_open = true;
-        info->tray_open = bdrv_dev_is_tray_open(bs);
-    }
-
-    if (bdrv_iostatus_is_enabled(bs)) {
-        info->has_io_status = true;
-        info->io_status = bs->iostatus;
-    }
-
-    if (bs->dirty_bitmap) {
-        info->has_dirty = true;
-        info->dirty = g_malloc0(sizeof(*info->dirty));
-        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
-        info->dirty->granularity =
-            ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
-    }
-
-    if (bs->drv) {
-        info->has_inserted = true;
-        info->inserted = g_malloc0(sizeof(*info->inserted));
-        info->inserted->file = g_strdup(bs->filename);
-        info->inserted->ro = bs->read_only;
-        info->inserted->drv = g_strdup(bs->drv->format_name);
-        info->inserted->encrypted = bs->encrypted;
-        info->inserted->encryption_key_missing = bdrv_key_required(bs);
-
-        if (bs->backing_file[0]) {
-            info->inserted->has_backing_file = true;
-            info->inserted->backing_file = g_strdup(bs->backing_file);
-        }
-
-        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
-
-        if (bs->io_limits_enabled) {
-            info->inserted->bps =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
-            info->inserted->bps_rd =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
-            info->inserted->bps_wr =
-                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
-            info->inserted->iops =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
-            info->inserted->iops_rd =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
-            info->inserted->iops_wr =
-                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
-        }
-    }
-    return info;
-}
-
-BlockInfoList *qmp_query_block(Error **errp)
-{
-    BlockInfoList *head = NULL, **p_next = &head;
-    BlockDriverState *bs;
-
-    QTAILQ_FOREACH(bs, &bdrv_states, list) {
-        BlockInfoList *info = g_malloc0(sizeof(*info));
-        info->value = bdrv_query_info(bs);
-
-        *p_next = info;
-        p_next = &info->next;
-    }
-
-    return head;
-}
-
 BlockStats *bdrv_query_stats(const BlockDriverState *bs)
 {
     BlockStats *s;
diff --git a/block/qapi.c b/block/qapi.c
index 4cf33ab..85df0b8 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -25,6 +25,7 @@
 #include "block/qapi.h"
 #include "block/snapshot.h"
 #include "block/block_int.h"
+#include "qmp-commands.h"
 
 /*
  * check whether the snapshot is valid for whole vm.
@@ -194,3 +195,79 @@ int bdrv_query_image_info(BlockDriverState *bs,
     *p_info = info;
     return 0;
 }
+
+BlockInfo *bdrv_query_info(BlockDriverState *bs)
+{
+    BlockInfo *info = g_malloc0(sizeof(*info));
+    info->device = g_strdup(bs->device_name);
+    info->type = g_strdup("unknown");
+    info->locked = bdrv_dev_is_medium_locked(bs);
+    info->removable = bdrv_dev_has_removable_media(bs);
+
+    if (bdrv_dev_has_removable_media(bs)) {
+        info->has_tray_open = true;
+        info->tray_open = bdrv_dev_is_tray_open(bs);
+    }
+
+    if (bdrv_iostatus_is_enabled(bs)) {
+        info->has_io_status = true;
+        info->io_status = bs->iostatus;
+    }
+
+    if (bs->dirty_bitmap) {
+        info->has_dirty = true;
+        info->dirty = g_malloc0(sizeof(*info->dirty));
+        info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
+        info->dirty->granularity =
+         ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
+    }
+
+    if (bs->drv) {
+        info->has_inserted = true;
+        info->inserted = g_malloc0(sizeof(*info->inserted));
+        info->inserted->file = g_strdup(bs->filename);
+        info->inserted->ro = bs->read_only;
+        info->inserted->drv = g_strdup(bs->drv->format_name);
+        info->inserted->encrypted = bs->encrypted;
+        info->inserted->encryption_key_missing = bdrv_key_required(bs);
+
+        if (bs->backing_file[0]) {
+            info->inserted->has_backing_file = true;
+            info->inserted->backing_file = g_strdup(bs->backing_file);
+        }
+
+        info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
+
+        if (bs->io_limits_enabled) {
+            info->inserted->bps =
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
+            info->inserted->bps_rd =
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
+            info->inserted->bps_wr =
+                           bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
+            info->inserted->iops =
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
+            info->inserted->iops_rd =
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
+            info->inserted->iops_wr =
+                           bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
+        }
+    }
+    return info;
+}
+
+BlockInfoList *qmp_query_block(Error **errp)
+{
+    BlockInfoList *head = NULL, **p_next = &head;
+    BlockDriverState *bs = NULL;
+
+    while ((bs = bdrv_next(bs))) {
+        BlockInfoList *info = g_malloc0(sizeof(*info));
+        info->value = bdrv_query_info(bs);
+
+        *p_next = info;
+        p_next = &info->next;
+    }
+
+    return head;
+}
diff --git a/include/block/block.h b/include/block/block.h
index d4f34d6..6b3d43a 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -325,7 +325,6 @@ 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);
-BlockInfo *bdrv_query_info(BlockDriverState *s);
 BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_snapshot(BlockDriverState *bs);
 int bdrv_is_snapshot(BlockDriverState *bs);
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 2c62fdf..0039a70 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -35,4 +35,5 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
 int bdrv_query_image_info(BlockDriverState *bs,
                           ImageInfo **p_info,
                           Error **errp);
+BlockInfo *bdrv_query_info(BlockDriverState *bs);
 #endif
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 09/17] qmp: add interface query-snapshots
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (7 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo Wenchao Xia
                   ` (8 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This interface returns info of valid internal snapshots for whole vm.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/qapi.c     |   17 ++++++++++++++++
 qapi-schema.json |   14 +++++++++++++
 qmp-commands.hx  |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 85df0b8..ea1ca7f 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -256,6 +256,23 @@ BlockInfo *bdrv_query_info(BlockDriverState *bs)
     return info;
 }
 
+SnapshotInfoList *qmp_query_snapshots(Error **errp)
+{
+    BlockDriverState *bs;
+    SnapshotInfoList *list = NULL;
+
+    /* internal snapshot for whole vm */
+    bs = bdrv_snapshots();
+    if (!bs) {
+        error_setg(errp, "No available block device supports snapshots\n");
+        return NULL;
+    }
+
+    bdrv_query_snapshot_info_list(bs, &list, true, errp);
+
+    return list;
+}
+
 BlockInfoList *qmp_query_block(Error **errp)
 {
     BlockInfoList *head = NULL, **p_next = &head;
diff --git a/qapi-schema.json b/qapi-schema.json
index fdaa9da..712838c 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -839,6 +839,20 @@
 { 'command': 'query-block', 'returns': ['BlockInfo'] }
 
 ##
+# @query-snapshots:
+#
+# Get a list of internal snapshots for the whole virtual machine. Only valid
+# internal snapshots will be returned, inconsistent ones will be ignored
+#
+# Returns: a list of @SnapshotInfo describing all consistent virtual machine
+#          snapshots
+#
+# Since: 1.5
+##
+{ 'command': 'query-snapshots',
+  'returns': ['SnapshotInfo'] }
+
+##
 # @BlockDeviceStats:
 #
 # Statistics of a virtual block device or a block backing device.
diff --git a/qmp-commands.hx b/qmp-commands.hx
index b370060..a4cd229 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -1743,6 +1743,61 @@ EQMP
     },
 
 SQMP
+query-snapshots
+---------------
+
+Show the internal consistent snapshot information
+
+Each snapshot is represented by a json-object. The returned value
+is a json-array of all snapshots
+
+Each json-object contain the following:
+
+- "id": unique snapshot id (json-string)
+- "name": internal snapshot name (json-string)
+- "vm-state-size": size of the VM state in bytes (json-int)
+- "date-sec": UTC date of the snapshot in seconds (json-int)
+- "date-nsec": fractional part in nanoseconds to be used with
+               date-sec(json-int)
+- "vm-clock-sec": VM clock relative to boot in seconds (json-int)
+- "vm-clock-nsec": fractional part in nanoseconds to be used with
+                   vm-clock-sec (json-int)
+
+Example:
+
+-> { "execute": "query-snapshots" }
+<- {
+      "return":[
+         {
+            "id": "1",
+            "name": "snapshot1",
+            "vm-state-size": 0,
+            "date-sec": 10000200,
+            "date-nsec": 12,
+            "vm-clock-sec": 206,
+            "vm-clock-nsec": 30
+         },
+         {
+            "id": "2",
+            "name": "snapshot2",
+            "vm-state-size": 34000000,
+            "date-sec": 13000200,
+            "date-nsec": 32,
+            "vm-clock-sec": 406,
+            "vm-clock-nsec": 31
+         }
+      ]
+   }
+
+EQMP
+
+    {
+        .name       = "query-snapshots",
+        .args_type  = "",
+        .mhandler.cmd_new = qmp_marshal_input_query_snapshots,
+    },
+
+SQMP
 query-blockstats
 ----------------
 
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (8 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 09/17] qmp: add interface query-snapshots Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-29 22:54   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block Wenchao Xia
                   ` (7 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  New member *backing-image is added to reflect the backing chain
status.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block/qapi.c     |    6 +++++-
 qapi-schema.json |    5 ++++-
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index ea1ca7f..df73f5b 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -123,7 +123,11 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
     return 0;
 }
 
-/* return 0 on success, and @p_info will be set only on success. */
+/*
+ * return 0 on success, and @p_info will be set only on success,
+ * (*pinfo)->has_backing_image will be false and (*pinfo)->backing_image will
+ * be NULL.
+ */
 int bdrv_query_image_info(BlockDriverState *bs,
                           ImageInfo **p_info,
                           Error **errp)
diff --git a/qapi-schema.json b/qapi-schema.json
index 712838c..b927c97 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -233,6 +233,8 @@
 #
 # @snapshots: #optional list of VM snapshots
 #
+# @backing-image: #optional info of the backing image (since 1.5)
+#
 # Since: 1.3
 #
 ##
@@ -242,7 +244,8 @@
            '*actual-size': 'int', 'virtual-size': 'int',
            '*cluster-size': 'int', '*encrypted': 'bool',
            '*backing-filename': 'str', '*full-backing-filename': 'str',
-           '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'] } }
+           '*backing-filename-format': 'str', '*snapshots': ['SnapshotInfo'],
+           '*backing-image': 'ImageInfo' } }
 
 ##
 # @ImageCheck:
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (9 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-28  9:54   ` Kevin Wolf
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots() Wenchao Xia
                   ` (6 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  Now image info will be retrieved as an embbed json object inside
BlockDeviceInfo, backing chain info and all related internal snapshot
info can be got in the enhanced recursive structure of ImageInfo.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block/qapi.c         |   39 ++++++++++++++++++++++++++--
 include/block/qapi.h |    4 ++-
 qapi-schema.json     |    5 +++-
 qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 109 insertions(+), 6 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index df73f5b..9051947 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
     return 0;
 }
 
-BlockInfo *bdrv_query_info(BlockDriverState *bs)
+/* return 0 on success, and @p_info will be set only on success. */
+int bdrv_query_info(BlockDriverState *bs,
+                    BlockInfo **p_info,
+                    Error **errp)
 {
     BlockInfo *info = g_malloc0(sizeof(*info));
+    BlockDriverState *bs0;
+    ImageInfo **p_image_info;
+    int ret = 0;
     info->device = g_strdup(bs->device_name);
     info->type = g_strdup("unknown");
     info->locked = bdrv_dev_is_medium_locked(bs);
@@ -256,8 +262,29 @@ BlockInfo *bdrv_query_info(BlockDriverState *bs)
             info->inserted->iops_wr =
                            bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
         }
+
+        bs0 = bs;
+        p_image_info = &info->inserted->image;
+        while (1) {
+            if (bdrv_query_image_info(bs0, p_image_info, errp)) {
+                goto err;
+            }
+            if (bs0->drv && bs0->backing_hd) {
+                bs0 = bs0->backing_hd;
+                (*p_image_info)->has_backing_image = true;
+                p_image_info = &((*p_image_info)->backing_image);
+            } else {
+                break;
+            }
+        }
     }
-    return info;
+
+    *p_info = info;
+    return 0;
+
+ err:
+    qapi_free_BlockInfo(info);
+    return ret;
 }
 
 SnapshotInfoList *qmp_query_snapshots(Error **errp)
@@ -284,11 +311,17 @@ BlockInfoList *qmp_query_block(Error **errp)
 
     while ((bs = bdrv_next(bs))) {
         BlockInfoList *info = g_malloc0(sizeof(*info));
-        info->value = bdrv_query_info(bs);
+        if (bdrv_query_info(bs, &info->value, errp)) {
+            goto err;
+        }
 
         *p_next = info;
         p_next = &info->next;
     }
 
     return head;
+
+ err:
+    qapi_free_BlockInfoList(head);
+    return NULL;
 }
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 0039a70..49f9a17 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -35,5 +35,7 @@ int bdrv_query_snapshot_info_list(BlockDriverState *bs,
 int bdrv_query_image_info(BlockDriverState *bs,
                           ImageInfo **p_info,
                           Error **errp);
-BlockInfo *bdrv_query_info(BlockDriverState *bs);
+int bdrv_query_info(BlockDriverState *bs,
+                    BlockInfo **p_info,
+                    Error **errp);
 #endif
diff --git a/qapi-schema.json b/qapi-schema.json
index b927c97..9a9e673 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -754,6 +754,8 @@
 #
 # @iops_wr: write I/O operations per second is specified
 #
+# @image: the info of image used (since: 1.5)
+#
 # Since: 0.14.0
 #
 # Notes: This interface is only found in @BlockInfo.
@@ -763,7 +765,8 @@
             '*backing_file': 'str', 'backing_file_depth': 'int',
             'encrypted': 'bool', 'encryption_key_missing': 'bool',
             'bps': 'int', 'bps_rd': 'int', 'bps_wr': 'int',
-            'iops': 'int', 'iops_rd': 'int', 'iops_wr': 'int'} }
+            'iops': 'int', 'iops_rd': 'int', 'iops_wr': 'int',
+            'image': 'ImageInfo' } }
 
 ##
 # @BlockDeviceIoStatus:
diff --git a/qmp-commands.hx b/qmp-commands.hx
index a4cd229..f977c6f 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -1681,6 +1681,47 @@ Each json-object contain the following:
          - "iops": limit total I/O operations per second (json-int)
          - "iops_rd": limit read operations per second (json-int)
          - "iops_wr": limit write operations per second (json-int)
+         - "image": the detail of the image, it is a json-object containing
+            the following:
+             - "filename": image file name (json-string)
+             - "format": image format (json-string)
+             - "virtual-size": image capacity in bytes (json-int)
+             - "dirty-flag": true if image is not cleanly closed, not present
+                             means clean (json-bool, optional)
+             - "actual-size": actual size on disk in bytes of the image, not
+                              present when image does not support thin
+                              provision (json-int, optional)
+             - "cluster-size": size of a cluster in bytes, not present if image
+                               format does not support it (json-int, optional)
+             - "encrypted": true if the image is encrypted, not present means
+                            false or the image format does not support
+                            encryption (json-bool, optional)
+             - "backing_file": backing file name, not present means no backing
+                               file is used or the image format does not
+                               support backing file chain
+                               (json-string, optional)
+             - "full-backing-filename": full path of the backing file, not
+                                        present if it equals backing_file or no
+                                        backing file is used
+                                        (json-string, optional)
+             - "backing-filename-format": the format of the backing file, not
+                                          present means unknown or no backing
+                                          file (json-string, optional)
+             - "snapshots": the internal snapshot info, it is an optional list
+                of json-object containing the following:
+                 - "id": unique snapshot id (json-string)
+                 - "name": snapshot name (json-string)
+                 - "vm-state-size": size of the VM state in bytes (json-int)
+                 - "date-sec": UTC date of the snapshot in seconds (json-int)
+                 - "date-nsec": fractional part in nanoseconds to be used with
+                                date-sec(json-int)
+                 - "vm-clock-sec": VM clock relative to boot in seconds
+                                   (json-int)
+                 - "vm-clock-nsec": fractional part in nanoseconds to be used
+                                    with vm-clock-sec (json-int)
+             - "backing-image": the detail of the backing image, it is an
+                                optional json-object only present when a
+                                backing image present for this image
 
 - "io-status": I/O operation status, only present if the device supports it
                and the VM is configured to stop on errors. It's always reset
@@ -1702,13 +1743,37 @@ Example:
                "drv":"qcow2",
                "encrypted":false,
                "file":"disks/test.img",
-               "backing_file_depth":0,
+               "backing_file_depth":1,
                "bps":1000000,
                "bps_rd":0,
                "bps_wr":0,
                "iops":1000000,
                "iops_rd":0,
                "iops_wr":0,
+               "image":{
+                  "filename":"disks/test.img",
+                  "format":"qcow2",
+                  "virtual-size":2048000,
+                  "backing_file":"base.img",
+                  "full-backing-filename":"disks/base.img",
+                  "backing-filename-format:"qcow2",
+                  "snapshots":[
+                     {
+                        "id": "1",
+                        "name": "snapshot1",
+                        "vm-state-size": 0,
+                        "date-sec": 10000200,
+                        "date-nsec": 12,
+                        "vm-clock-sec": 206,
+                        "vm-clock-nsec": 30
+                     }
+                  ],
+                  "backing-image":{
+                      "filename":"disks/base.img",
+                      "format":"qcow2",
+                      "virtual-size":2048000
+                  }
+               }
             },
             "type":"unknown"
          },
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (10 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-29 23:04   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one Wenchao Xia
                   ` (5 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This function will simply call qmp interface qmp_query_snapshots()
added in last commit and then dump information in monitor console.
  To get snapshot info, Now qemu and qemu-img both call block layer
function bdrv_query_snapshot_info_list() in their calling path, and
then they just translate the qmp object got to strings in stdout or
monitor console.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 hmp.c |   42 ++++++++++++++++++++++++++++++++++++++++++
 hmp.h |    1 +
 2 files changed, 43 insertions(+), 0 deletions(-)

diff --git a/hmp.c b/hmp.c
index b0a861c..c475d65 100644
--- a/hmp.c
+++ b/hmp.c
@@ -651,6 +651,48 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
     qapi_free_TPMInfoList(info_list);
 }
 
+/* assume list is valid */
+static void monitor_dump_snapshotinfolist(Monitor *mon, SnapshotInfoList *list)
+{
+    SnapshotInfoList *elem;
+    char buf[256];
+
+    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+
+    for (elem = list; elem; elem = elem->next) {
+        QEMUSnapshotInfo sn = {
+            .vm_state_size = elem->value->vm_state_size,
+            .date_sec = elem->value->date_sec,
+            .date_nsec = elem->value->date_nsec,
+            .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
+                             elem->value->vm_clock_nsec,
+        };
+        pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
+        pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
+        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+    }
+}
+
+void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
+{
+    Error *err = NULL;
+    SnapshotInfoList *list;
+
+    list = qmp_query_snapshots(&err);
+    if (error_is_set(&err)) {
+        hmp_handle_error(mon, &err);
+        return;
+    }
+
+    if (list == NULL) {
+        monitor_printf(mon, "There is no suitable snapshot available\n");
+        return;
+    }
+
+    monitor_dump_snapshotinfolist(mon, list);
+    qapi_free_SnapshotInfoList(list);
+}
+
 void hmp_quit(Monitor *mon, const QDict *qdict)
 {
     monitor_suspend(mon);
diff --git a/hmp.h b/hmp.h
index 95fe76e..106d8d5 100644
--- a/hmp.h
+++ b/hmp.h
@@ -37,6 +37,7 @@ void hmp_info_balloon(Monitor *mon, const QDict *qdict);
 void hmp_info_pci(Monitor *mon, const QDict *qdict);
 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict);
 void hmp_info_tpm(Monitor *mon, const QDict *qdict);
+void hmp_info_snapshots(Monitor *mon, const QDict *qdict);
 void hmp_quit(Monitor *mon, const QDict *qdict);
 void hmp_stop(Monitor *mon, const QDict *qdict);
 void hmp_system_reset(Monitor *mon, const QDict *qdict);
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (11 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-04-01 15:56   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 14/17] block: move dump_human_image_info() to block/qapi.c Wenchao Xia
                   ` (4 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This patch using new added function in last commit which retrieve
info from qmp for snapshot info.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 monitor.c |    2 +-
 savevm.c  |   64 -------------------------------------------------------------
 2 files changed, 1 insertions(+), 65 deletions(-)

diff --git a/monitor.c b/monitor.c
index 680d344..e927c71 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2611,7 +2611,7 @@ static mon_cmd_t info_cmds[] = {
         .args_type  = "",
         .params     = "",
         .help       = "show the currently saved VM snapshots",
-        .mhandler.cmd = do_info_snapshots,
+        .mhandler.cmd = hmp_info_snapshots,
     },
     {
         .name       = "status",
diff --git a/savevm.c b/savevm.c
index 04fcd10..30a8e5a 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2344,70 +2344,6 @@ void do_delvm(Monitor *mon, const QDict *qdict)
     }
 }
 
-void do_info_snapshots(Monitor *mon, const QDict *qdict)
-{
-    BlockDriverState *bs, *bs1;
-    QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
-    int nb_sns, i, ret, available;
-    int total;
-    int *available_snapshots;
-    char buf[256];
-
-    bs = bdrv_snapshots();
-    if (!bs) {
-        monitor_printf(mon, "No available block device supports snapshots\n");
-        return;
-    }
-
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
-    if (nb_sns < 0) {
-        monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
-        return;
-    }
-
-    if (nb_sns == 0) {
-        monitor_printf(mon, "There is no snapshot available.\n");
-        return;
-    }
-
-    available_snapshots = g_malloc0(sizeof(int) * nb_sns);
-    total = 0;
-    for (i = 0; i < nb_sns; i++) {
-        sn = &sn_tab[i];
-        available = 1;
-        bs1 = NULL;
-
-        while ((bs1 = bdrv_next(bs1))) {
-            if (bdrv_can_snapshot(bs1) && bs1 != bs) {
-                ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str, NULL);
-                if (ret < 0) {
-                    available = 0;
-                    break;
-                }
-            }
-        }
-
-        if (available) {
-            available_snapshots[total] = i;
-            total++;
-        }
-    }
-
-    if (total > 0) {
-        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-        for (i = 0; i < total; i++) {
-            sn = &sn_tab[available_snapshots[i]];
-            monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
-        }
-    } else {
-        monitor_printf(mon, "There is no suitable snapshot available\n");
-    }
-
-    g_free(sn_tab);
-    g_free(available_snapshots);
-
-}
-
 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
 {
     qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 14/17] block: move dump_human_image_info() to block/qapi.c
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (12 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump() Wenchao Xia
                   ` (3 subsequent siblings)
  17 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This function is needed later in hmp command, it is also renamed to
bdrv_image_info_dump().

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
---
 block/qapi.c         |   67 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/block/qapi.h |    1 +
 qemu-img.c           |   69 +-------------------------------------------------
 3 files changed, 69 insertions(+), 68 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 9051947..477659a 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -325,3 +325,70 @@ BlockInfoList *qmp_query_block(Error **errp)
     qapi_free_BlockInfoList(head);
     return NULL;
 }
+
+void bdrv_image_info_dump(ImageInfo *info)
+{
+    char size_buf[128], dsize_buf[128];
+    if (!info->has_actual_size) {
+        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
+    } else {
+        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
+                                info->actual_size);
+    }
+    get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
+    printf("image: %s\n"
+           "file format: %s\n"
+           "virtual size: %s (%" PRId64 " bytes)\n"
+           "disk size: %s\n",
+           info->filename, info->format, size_buf,
+           info->virtual_size,
+           dsize_buf);
+
+    if (info->has_encrypted && info->encrypted) {
+        printf("encrypted: yes\n");
+    }
+
+    if (info->has_cluster_size) {
+        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
+    }
+
+    if (info->has_dirty_flag && info->dirty_flag) {
+        printf("cleanly shut down: no\n");
+    }
+
+    if (info->has_backing_filename) {
+        printf("backing file: %s", info->backing_filename);
+        if (info->has_full_backing_filename) {
+            printf(" (actual path: %s)", info->full_backing_filename);
+        }
+        putchar('\n');
+        if (info->has_backing_filename_format) {
+            printf("backing file format: %s\n", info->backing_filename_format);
+        }
+    }
+
+    if (info->has_snapshots) {
+        SnapshotInfoList *elem;
+        char buf[256];
+
+        printf("Snapshot list:\n");
+        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+
+        /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
+         * we convert to the block layer's native QEMUSnapshotInfo for now.
+         */
+        for (elem = info->snapshots; elem; elem = elem->next) {
+            QEMUSnapshotInfo sn = {
+                .vm_state_size = elem->value->vm_state_size,
+                .date_sec = elem->value->date_sec,
+                .date_nsec = elem->value->date_nsec,
+                .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
+                                 elem->value->vm_clock_nsec,
+            };
+
+            pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
+            pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
+            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+        }
+    }
+}
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 49f9a17..5b2762c 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -38,4 +38,5 @@ int bdrv_query_image_info(BlockDriverState *bs,
 int bdrv_query_info(BlockDriverState *bs,
                     BlockInfo **p_info,
                     Error **errp);
+void bdrv_image_info_dump(ImageInfo *info);
 #endif
diff --git a/qemu-img.c b/qemu-img.c
index 1dd0a60..5b229a9 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1606,73 +1606,6 @@ static void dump_json_image_info(ImageInfo *info)
     QDECREF(str);
 }
 
-static void dump_human_image_info(ImageInfo *info)
-{
-    char size_buf[128], dsize_buf[128];
-    if (!info->has_actual_size) {
-        snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
-    } else {
-        get_human_readable_size(dsize_buf, sizeof(dsize_buf),
-                                info->actual_size);
-    }
-    get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
-    printf("image: %s\n"
-           "file format: %s\n"
-           "virtual size: %s (%" PRId64 " bytes)\n"
-           "disk size: %s\n",
-           info->filename, info->format, size_buf,
-           info->virtual_size,
-           dsize_buf);
-
-    if (info->has_encrypted && info->encrypted) {
-        printf("encrypted: yes\n");
-    }
-
-    if (info->has_cluster_size) {
-        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
-    }
-
-    if (info->has_dirty_flag && info->dirty_flag) {
-        printf("cleanly shut down: no\n");
-    }
-
-    if (info->has_backing_filename) {
-        printf("backing file: %s", info->backing_filename);
-        if (info->has_full_backing_filename) {
-            printf(" (actual path: %s)", info->full_backing_filename);
-        }
-        putchar('\n');
-        if (info->has_backing_filename_format) {
-            printf("backing file format: %s\n", info->backing_filename_format);
-        }
-    }
-
-    if (info->has_snapshots) {
-        SnapshotInfoList *elem;
-        char buf[256];
-
-        printf("Snapshot list:\n");
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
-
-        /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
-         * we convert to the block layer's native QEMUSnapshotInfo for now.
-         */
-        for (elem = info->snapshots; elem; elem = elem->next) {
-            QEMUSnapshotInfo sn = {
-                .vm_state_size = elem->value->vm_state_size,
-                .date_sec = elem->value->date_sec,
-                .date_nsec = elem->value->date_nsec,
-                .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
-                                 elem->value->vm_clock_nsec,
-            };
-
-            pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
-            pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
-            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
-        }
-    }
-}
-
 static void dump_human_image_info_list(ImageInfoList *list)
 {
     ImageInfoList *elem;
@@ -1684,7 +1617,7 @@ static void dump_human_image_info_list(ImageInfoList *list)
         }
         delim = true;
 
-        dump_human_image_info(elem->value);
+        bdrv_image_info_dump(elem->value);
     }
 }
 
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump()
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (13 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 14/17] block: move dump_human_image_info() to block/qapi.c Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-04-01 19:17   ` Eric Blake
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block' Wenchao Xia
                   ` (2 subsequent siblings)
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  This allow hmp use this function, just like qemu-img.
It also returns a pointer now to make it easy to use.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block/qapi.c         |   67 +++++++++++++++++++++++++++++++++++--------------
 include/block/qapi.h |    2 +-
 qemu-img.c           |    6 +++-
 3 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/block/qapi.c b/block/qapi.c
index 477659a..c7932eb 100644
--- a/block/qapi.c
+++ b/block/qapi.c
@@ -326,9 +326,30 @@ BlockInfoList *qmp_query_block(Error **errp)
     return NULL;
 }
 
-void bdrv_image_info_dump(ImageInfo *info)
+GCC_FMT_ATTR(3, 4)
+static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)
+{
+    int l;
+    if (*p_size < 1) {
+        return;
+    }
+
+    va_list ap;
+    va_start(ap, fmt);
+    l = vsnprintf(*p_buf, *p_size, fmt, ap);
+    va_end(ap);
+    if (l > 0) {
+        *p_buf += l;
+        *p_size -= l;
+    }
+}
+
+/* Use buf instead of asprintf to reduce memory fragility. */
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info)
 {
     char size_buf[128], dsize_buf[128];
+    char *buf1 = buf;
+
     if (!info->has_actual_size) {
         snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
     } else {
@@ -336,43 +357,49 @@ void bdrv_image_info_dump(ImageInfo *info)
                                 info->actual_size);
     }
     get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
-    printf("image: %s\n"
-           "file format: %s\n"
-           "virtual size: %s (%" PRId64 " bytes)\n"
-           "disk size: %s\n",
-           info->filename, info->format, size_buf,
-           info->virtual_size,
-           dsize_buf);
+    snprintf_tail(&buf1, &buf_size,
+                  "image: %s\n"
+                  "file format: %s\n"
+                  "virtual size: %s (%" PRId64 " bytes)\n"
+                  "disk size: %s\n",
+                  info->filename, info->format, size_buf,
+                  info->virtual_size,
+                  dsize_buf);
 
     if (info->has_encrypted && info->encrypted) {
-        printf("encrypted: yes\n");
+        snprintf_tail(&buf1, &buf_size, "encrypted: yes\n");
     }
 
     if (info->has_cluster_size) {
-        printf("cluster_size: %" PRId64 "\n", info->cluster_size);
+        snprintf_tail(&buf1, &buf_size, "cluster_size: %" PRId64 "\n",
+                 info->cluster_size);
     }
 
     if (info->has_dirty_flag && info->dirty_flag) {
-        printf("cleanly shut down: no\n");
+        snprintf_tail(&buf1, &buf_size, "cleanly shut down: no\n");
     }
 
     if (info->has_backing_filename) {
-        printf("backing file: %s", info->backing_filename);
+        snprintf_tail(&buf1, &buf_size, "backing file: %s",
+                      info->backing_filename);
         if (info->has_full_backing_filename) {
-            printf(" (actual path: %s)", info->full_backing_filename);
+            snprintf_tail(&buf1, &buf_size, " (actual path: %s)",
+                          info->full_backing_filename);
         }
-        putchar('\n');
+        snprintf_tail(&buf1, &buf_size, "\n");
         if (info->has_backing_filename_format) {
-            printf("backing file format: %s\n", info->backing_filename_format);
+            snprintf_tail(&buf1, &buf_size, "backing file format: %s\n",
+                          info->backing_filename_format);
         }
     }
 
     if (info->has_snapshots) {
         SnapshotInfoList *elem;
-        char buf[256];
+        char buf_sn[256];
 
-        printf("Snapshot list:\n");
-        printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
+        snprintf_tail(&buf1, &buf_size, "Snapshot list:\n");
+        snprintf_tail(&buf1, &buf_size, "%s\n",
+                      bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), NULL));
 
         /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
          * we convert to the block layer's native QEMUSnapshotInfo for now.
@@ -388,7 +415,9 @@ void bdrv_image_info_dump(ImageInfo *info)
 
             pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
             pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
-            printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
+            snprintf_tail(&buf1, &buf_size, "%s\n",
+                          bdrv_snapshot_dump(buf_sn, sizeof(buf_sn), &sn));
         }
     }
+    return buf;
 }
diff --git a/include/block/qapi.h b/include/block/qapi.h
index 5b2762c..4c4677c 100644
--- a/include/block/qapi.h
+++ b/include/block/qapi.h
@@ -38,5 +38,5 @@ int bdrv_query_image_info(BlockDriverState *bs,
 int bdrv_query_info(BlockDriverState *bs,
                     BlockInfo **p_info,
                     Error **errp);
-void bdrv_image_info_dump(ImageInfo *info);
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info);
 #endif
diff --git a/qemu-img.c b/qemu-img.c
index 5b229a9..d3fce63 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1606,10 +1606,12 @@ static void dump_json_image_info(ImageInfo *info)
     QDECREF(str);
 }
 
+#define IMAGE_INFO_BUF_SIZE (2048)
 static void dump_human_image_info_list(ImageInfoList *list)
 {
     ImageInfoList *elem;
     bool delim = false;
+    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);
 
     for (elem = list; elem; elem = elem->next) {
         if (delim) {
@@ -1617,8 +1619,10 @@ static void dump_human_image_info_list(ImageInfoList *list)
         }
         delim = true;
 
-        bdrv_image_info_dump(elem->value);
+        printf("%s",
+               bdrv_image_info_dump(buf, IMAGE_INFO_BUF_SIZE, elem->value));
     }
+    g_free(buf);
 }
 
 static gboolean str_equal_func(gconstpointer a, gconstpointer b)
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block'
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (14 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump() Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-28 11:06   ` Kevin Wolf
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block Wenchao Xia
  2013-03-28 11:10 ` [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Kevin Wolf
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  Now human monitor can show image details include internal
snapshot info for every block device.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 hmp.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/hmp.c b/hmp.c
index c475d65..49f851b 100644
--- a/hmp.c
+++ b/hmp.c
@@ -22,6 +22,7 @@
 #include "qemu/sockets.h"
 #include "monitor/monitor.h"
 #include "ui/console.h"
+#include "block/qapi.h"
 
 static void hmp_handle_error(Monitor *mon, Error **errp)
 {
@@ -272,9 +273,12 @@ void hmp_info_cpus(Monitor *mon, const QDict *qdict)
     qapi_free_CpuInfoList(cpu_list);
 }
 
+#define IMAGE_INFO_BUF_SIZE (2048)
 void hmp_info_block(Monitor *mon, const QDict *qdict)
 {
     BlockInfoList *block_list, *info;
+    ImageInfo *image_info;
+    char *buf = NULL;
 
     block_list = qmp_query_block(NULL);
 
@@ -316,6 +320,22 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)
                             info->value->inserted->iops,
                             info->value->inserted->iops_rd,
                             info->value->inserted->iops_wr);
+
+            if (!buf) {
+                buf = g_malloc0(IMAGE_INFO_BUF_SIZE);
+            }
+            monitor_printf(mon, " images:\n");
+            image_info = info->value->inserted->image;
+            while (1) {
+                bdrv_image_info_dump(buf, IMAGE_INFO_BUF_SIZE, image_info);
+                monitor_printf(mon, "%s", buf);
+                if (image_info->has_backing_image) {
+                    image_info = image_info->backing_image;
+                } else {
+                    break;
+                }
+            }
+
         } else {
             monitor_printf(mon, " [not inserted]");
         }
@@ -323,6 +343,7 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)
         monitor_printf(mon, "\n");
     }
 
+    g_free(buf);
     qapi_free_BlockInfoList(block_list);
 }
 
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (15 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block' Wenchao Xia
@ 2013-03-22 14:19 ` Wenchao Xia
  2013-03-28 11:09   ` Kevin Wolf
  2013-03-28 11:10 ` [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Kevin Wolf
  17 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-22 14:19 UTC (permalink / raw)
  To: qemu-devel
  Cc: kwolf, aliguori, stefanha, armbru, lcapitulino, pbonzini, Wenchao Xia

  With these parameters, user can choose the information to be showed,
to avoid message flood in the montior.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 hmp.c     |    7 ++++++-
 monitor.c |    7 ++++---
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/hmp.c b/hmp.c
index 49f851b..3a2ba54 100644
--- a/hmp.c
+++ b/hmp.c
@@ -279,10 +279,15 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)
     BlockInfoList *block_list, *info;
     ImageInfo *image_info;
     char *buf = NULL;
+    const char *device = qdict_get_try_str(qdict, "device");
+    int backing = qdict_get_try_bool(qdict, "backing", 0);
 
     block_list = qmp_query_block(NULL);
 
     for (info = block_list; info; info = info->next) {
+        if (device && strcmp(device, info->value->device)) {
+            continue;
+        }
         monitor_printf(mon, "%s: removable=%d",
                        info->value->device, info->value->removable);
 
@@ -329,7 +334,7 @@ void hmp_info_block(Monitor *mon, const QDict *qdict)
             while (1) {
                 bdrv_image_info_dump(buf, IMAGE_INFO_BUF_SIZE, image_info);
                 monitor_printf(mon, "%s", buf);
-                if (image_info->has_backing_image) {
+                if (backing && image_info->has_backing_image) {
                     image_info = image_info->backing_image;
                 } else {
                     break;
diff --git a/monitor.c b/monitor.c
index e927c71..055252e 100644
--- a/monitor.c
+++ b/monitor.c
@@ -2455,9 +2455,10 @@ static mon_cmd_t info_cmds[] = {
     },
     {
         .name       = "block",
-        .args_type  = "",
-        .params     = "",
-        .help       = "show the block devices",
+        .args_type  = "backing:-b,device:B?",
+        .params     = "[-b] [device]",
+        .help       = "show info of one block device or all block devices "
+                      "[and info of backing images with -b option",
         .mhandler.cmd = hmp_info_block,
     },
     {
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list()
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list() Wenchao Xia
@ 2013-03-27 21:31   ` Eric Blake
  0 siblings, 0 replies; 38+ messages in thread
From: Eric Blake @ 2013-03-27 21:31 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 1151 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This patch adds function bdrv_query_snapshot_info_list(), which will
> retrieve snapshot info of an image in qmp object format. The implementation
> is based on the code moved from qemu-img.c with modification to fit more
> for qmp based block layer API.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> Reviewed-by: Eric Blake <eblake@redhat.com>
> ---
>  block/qapi.c         |   52 +++++++++++++++++++++++++++++++++++++------------
>  include/block/qapi.h |    4 ++-
>  qemu-img.c           |    4 ++-
>  3 files changed, 45 insertions(+), 15 deletions(-)

> +        default:
> +            error_setg(errp, "Can't list snapshots of device '%s': %s",
> +                       dev, strerror(-sn_count));

This works (hence I didn't notice it before), but you might want to use:

error_setg_errno(errp, -sn_count,
                 "Can't list snapshots of device '%s'", dev);

Either way, you can keep my

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block Wenchao Xia
@ 2013-03-28  9:54   ` Kevin Wolf
  2013-03-29  2:35     ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Kevin Wolf @ 2013-03-28  9:54 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>   Now image info will be retrieved as an embbed json object inside
> BlockDeviceInfo, backing chain info and all related internal snapshot
> info can be got in the enhanced recursive structure of ImageInfo.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  block/qapi.c         |   39 ++++++++++++++++++++++++++--
>  include/block/qapi.h |    4 ++-
>  qapi-schema.json     |    5 +++-
>  qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
>  4 files changed, 109 insertions(+), 6 deletions(-)
> 
> diff --git a/block/qapi.c b/block/qapi.c
> index df73f5b..9051947 100644
> --- a/block/qapi.c
> +++ b/block/qapi.c
> @@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
>      return 0;
>  }
>  
> -BlockInfo *bdrv_query_info(BlockDriverState *bs)
> +/* return 0 on success, and @p_info will be set only on success. */
> +int bdrv_query_info(BlockDriverState *bs,
> +                    BlockInfo **p_info,
> +                    Error **errp)
>  {
>      BlockInfo *info = g_malloc0(sizeof(*info));
> +    BlockDriverState *bs0;
> +    ImageInfo **p_image_info;
> +    int ret = 0;

ret is never changed, so this function always returns 0. I would suggest
to drop ret and make the function return type void.

>      info->device = g_strdup(bs->device_name);
>      info->type = g_strdup("unknown");
>      info->locked = bdrv_dev_is_medium_locked(bs);
> @@ -256,8 +262,29 @@ BlockInfo *bdrv_query_info(BlockDriverState *bs)
>              info->inserted->iops_wr =
>                             bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
>          }
> +
> +        bs0 = bs;
> +        p_image_info = &info->inserted->image;
> +        while (1) {
> +            if (bdrv_query_image_info(bs0, p_image_info, errp)) {
> +                goto err;
> +            }
> +            if (bs0->drv && bs0->backing_hd) {
> +                bs0 = bs0->backing_hd;
> +                (*p_image_info)->has_backing_image = true;
> +                p_image_info = &((*p_image_info)->backing_image);
> +            } else {
> +                break;
> +            }
> +        }
>      }
> -    return info;
> +
> +    *p_info = info;
> +    return 0;
> +
> + err:
> +    qapi_free_BlockInfo(info);
> +    return ret;
>  }
>  
>  SnapshotInfoList *qmp_query_snapshots(Error **errp)
> @@ -284,11 +311,17 @@ BlockInfoList *qmp_query_block(Error **errp)
>  
>      while ((bs = bdrv_next(bs))) {
>          BlockInfoList *info = g_malloc0(sizeof(*info));
> -        info->value = bdrv_query_info(bs);
> +        if (bdrv_query_info(bs, &info->value, errp)) {
> +            goto err;
> +        }

Consequently, you've got the error handling wrong here, the if condition
is never true. It should look more or less like this (the syntax details
may be wrong):

Error *local_err;
bdrv_query_info(bs, &info->value, &local_err);
if (error_is_set(local_err)) {
    error_propagate(err, local_err);
    goto err;
}

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block'
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block' Wenchao Xia
@ 2013-03-28 11:06   ` Kevin Wolf
  0 siblings, 0 replies; 38+ messages in thread
From: Kevin Wolf @ 2013-03-28 11:06 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>   Now human monitor can show image details include internal
> snapshot info for every block device.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>

I don't think we should do that unconditionally. 'info block' should be
a short summary of all block devices in use by the VM and this would
make it way too long.

We could either introduce an 'info block -v' to display everything, or
only do it for a single BlockDriverState with something like
'info block <blockdev-name>'. I'm not sure how easy those would be to
implement, though.

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block Wenchao Xia
@ 2013-03-28 11:09   ` Kevin Wolf
  2013-03-29  2:48     ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Kevin Wolf @ 2013-03-28 11:09 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>   With these parameters, user can choose the information to be showed,
> to avoid message flood in the montior.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>

Aha, so here you actually introduce the 'device' parameter. If you can
have this patch first, and only then patch 16, then limiting the new
output to the form with a device specified should be trivial.

> --- a/monitor.c
> +++ b/monitor.c
> @@ -2455,9 +2455,10 @@ static mon_cmd_t info_cmds[] = {
>      },
>      {
>          .name       = "block",
> -        .args_type  = "",
> -        .params     = "",
> -        .help       = "show the block devices",
> +        .args_type  = "backing:-b,device:B?",
> +        .params     = "[-b] [device]",
> +        .help       = "show info of one block device or all block devices "
> +                      "[and info of backing images with -b option",

That '[' doesn't look intentional?

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info
  2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
                   ` (16 preceding siblings ...)
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block Wenchao Xia
@ 2013-03-28 11:10 ` Kevin Wolf
  17 siblings, 0 replies; 38+ messages in thread
From: Kevin Wolf @ 2013-03-28 11:10 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 22.03.2013 um 15:18 hat Wenchao Xia geschrieben:
>   In the use of snapshot a way to retrieve related info at runtime is needed,
> so this serial of patches will merge some code for qemu and qemu-img, and add
> or enchance following interfaces for qemu:
> [...]

I had comments on patches 11, 16 and 17. The rest is:

Reviewed-by: Kevin Wolf <kwolf@redhat.com>

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-03-28  9:54   ` Kevin Wolf
@ 2013-03-29  2:35     ` Wenchao Xia
  2013-04-02  8:09       ` Kevin Wolf
  0 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-29  2:35 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: aliguori, stefanha, qemu-devel, lcapitulino, pbonzini, armbru

于 2013-3-28 17:54, Kevin Wolf 写道:
> Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>>    Now image info will be retrieved as an embbed json object inside
>> BlockDeviceInfo, backing chain info and all related internal snapshot
>> info can be got in the enhanced recursive structure of ImageInfo.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>>   block/qapi.c         |   39 ++++++++++++++++++++++++++--
>>   include/block/qapi.h |    4 ++-
>>   qapi-schema.json     |    5 +++-
>>   qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
>>   4 files changed, 109 insertions(+), 6 deletions(-)
>>
>> diff --git a/block/qapi.c b/block/qapi.c
>> index df73f5b..9051947 100644
>> --- a/block/qapi.c
>> +++ b/block/qapi.c
>> @@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
>>       return 0;
>>   }
>>
>> -BlockInfo *bdrv_query_info(BlockDriverState *bs)
>> +/* return 0 on success, and @p_info will be set only on success. */
>> +int bdrv_query_info(BlockDriverState *bs,
>> +                    BlockInfo **p_info,
>> +                    Error **errp)
>>   {
>>       BlockInfo *info = g_malloc0(sizeof(*info));
>> +    BlockDriverState *bs0;
>> +    ImageInfo **p_image_info;
>> +    int ret = 0;
>
> ret is never changed, so this function always returns 0. I would suggest
> to drop ret and make the function return type void.
>
   My bad, I forgot to set its value, the interface is intend to return
negative error number and errp both on fail.

>>       info->device = g_strdup(bs->device_name);
>>       info->type = g_strdup("unknown");
>>       info->locked = bdrv_dev_is_medium_locked(bs);
>> @@ -256,8 +262,29 @@ BlockInfo *bdrv_query_info(BlockDriverState *bs)
>>               info->inserted->iops_wr =
>>                              bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
>>           }
>> +
>> +        bs0 = bs;
>> +        p_image_info = &info->inserted->image;
>> +        while (1) {
>> +            if (bdrv_query_image_info(bs0, p_image_info, errp)) {
>> +                goto err;
>> +            }
   Sorry ret is missing here, it should be:
             ret = bdrv_query_image_info(bs0, p_image_info, errp));
             if (ret) {
                 goto err;
             }
   I'll correct it.

>> +            if (bs0->drv && bs0->backing_hd) {
>> +                bs0 = bs0->backing_hd;
>> +                (*p_image_info)->has_backing_image = true;
>> +                p_image_info = &((*p_image_info)->backing_image);
>> +            } else {
>> +                break;
>> +            }
>> +        }
>>       }
>> -    return info;
>> +
>> +    *p_info = info;
>> +    return 0;
>> +
>> + err:
>> +    qapi_free_BlockInfo(info);
>> +    return ret;
>>   }
>>
>>   SnapshotInfoList *qmp_query_snapshots(Error **errp)
>> @@ -284,11 +311,17 @@ BlockInfoList *qmp_query_block(Error **errp)
>>
>>       while ((bs = bdrv_next(bs))) {
>>           BlockInfoList *info = g_malloc0(sizeof(*info));
>> -        info->value = bdrv_query_info(bs);
>> +        if (bdrv_query_info(bs, &info->value, errp)) {
>> +            goto err;
>> +        }
>
> Consequently, you've got the error handling wrong here, the if condition
> is never true. It should look more or less like this (the syntax details
> may be wrong):
>
> Error *local_err;
> bdrv_query_info(bs, &info->value, &local_err);
> if (error_is_set(local_err)) {
>      error_propagate(err, local_err);
>      goto err;
> }
>
> Kevin
>


-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block
  2013-03-28 11:09   ` Kevin Wolf
@ 2013-03-29  2:48     ` Wenchao Xia
  2013-04-02  7:46       ` Kevin Wolf
  0 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-29  2:48 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

于 2013-3-28 19:09, Kevin Wolf 写道:
> Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>>    With these parameters, user can choose the information to be showed,
>> to avoid message flood in the montior.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>
> Aha, so here you actually introduce the 'device' parameter. If you can
> have this patch first, and only then patch 16, then limiting the new
> output to the form with a device specified should be trivial.
>
   It is a bit indirect that "info block" and "info block <device>"
show different on single device, so I introduced additional parameter
'-b' to filter out info. With more thinking, I think it should be
{
         .name       = "block",
         .help       = "show the block devices",
         .args_type  = "verbose:-v,device:B?",
         .params     = "[-v] [device]",
         .help       = "show info of one block device or all block devices "
                       "and detail of images with -v option",
}
   Then by default "info block" so brief summary as old time. Since the
"-v" parameter is filtering something out not present on patch 15,
so I can't move this patch forward, hope you are OK with it.

>> --- a/monitor.c
>> +++ b/monitor.c
>> @@ -2455,9 +2455,10 @@ static mon_cmd_t info_cmds[] = {
>>       },
>>       {
>>           .name       = "block",
>> -        .args_type  = "",
>> -        .params     = "",
>> -        .help       = "show the block devices",
>> +        .args_type  = "backing:-b,device:B?",
>> +        .params     = "[-b] [device]",
>> +        .help       = "show info of one block device or all block devices "
>> +                      "[and info of backing images with -b option",
>
> That '[' doesn't look intentional?
   my hand shaking... will correct it.

>
> Kevin
>


-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c Wenchao Xia
@ 2013-03-29 20:10   ` Eric Blake
  2013-03-30 12:32     ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Eric Blake @ 2013-03-29 20:10 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 1403 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This is a code move patch, except in qmp_query_block bdrv_next(bs)
> is used instead of direct traverse of global array 'bdrv_states'.

Mixing code motion and a code change isn't always the best, but at least
you were honest about it.  I don't know how easy it would be to split
this into two patches for straight code motion.

>   This patch also fix code style error reported by check script.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  block.c               |   76 ------------------------------------------------
>  block/qapi.c          |   77 +++++++++++++++++++++++++++++++++++++++++++++++++
>  include/block/block.h |    1 -
>  include/block/qapi.h  |    1 +
>  4 files changed, 78 insertions(+), 77 deletions(-)

At any rate,

Reviewed-by: Eric Blake <eblake@redhat.com>

> -BlockInfoList *qmp_query_block(Error **errp)
> -{
> -    BlockInfoList *head = NULL, **p_next = &head;
> -    BlockDriverState *bs;
> -
> -    QTAILQ_FOREACH(bs, &bdrv_states, list) {

> +BlockInfoList *qmp_query_block(Error **errp)
> +{
> +    BlockInfoList *head = NULL, **p_next = &head;
> +    BlockDriverState *bs = NULL;
> +
> +    while ((bs = bdrv_next(bs))) {

This is the tweak you made.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo Wenchao Xia
@ 2013-03-29 22:54   ` Eric Blake
  0 siblings, 0 replies; 38+ messages in thread
From: Eric Blake @ 2013-03-29 22:54 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 474 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   New member *backing-image is added to reflect the backing chain
> status.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  block/qapi.c     |    6 +++++-
>  qapi-schema.json |    5 ++++-
>  2 files changed, 9 insertions(+), 2 deletions(-)

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots()
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots() Wenchao Xia
@ 2013-03-29 23:04   ` Eric Blake
  2013-03-30 12:38     ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Eric Blake @ 2013-03-29 23:04 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 1408 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This function will simply call qmp interface qmp_query_snapshots()
> added in last commit and then dump information in monitor console.
>   To get snapshot info, Now qemu and qemu-img both call block layer
> function bdrv_query_snapshot_info_list() in their calling path, and
> then they just translate the qmp object got to strings in stdout or
> monitor console.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  hmp.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>  hmp.h |    1 +
>  2 files changed, 43 insertions(+), 0 deletions(-)
> 
> diff --git a/hmp.c b/hmp.c
> index b0a861c..c475d65 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -651,6 +651,48 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
>      qapi_free_TPMInfoList(info_list);
>  }
>  
> +/* assume list is valid */
> +static void monitor_dump_snapshotinfolist(Monitor *mon, SnapshotInfoList *list)
> +{
> +    SnapshotInfoList *elem;
> +    char buf[256];
> +
> +    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));

Are you sure that won't ever be truncated?  I'm pretty sure that I could
come up with a scenario where I cause bdrv_snapshot_dump() to want to
output more than 256 bytes of details.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c
  2013-03-29 20:10   ` Eric Blake
@ 2013-03-30 12:32     ` Wenchao Xia
  0 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-03-30 12:32 UTC (permalink / raw)
  To: Eric Blake
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

于 2013-3-30 4:10, Eric Blake 写道:
> On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>>    This is a code move patch, except in qmp_query_block bdrv_next(bs)
>> is used instead of direct traverse of global array 'bdrv_states'.
>
> Mixing code motion and a code change isn't always the best, but at least
> you were honest about it.  I don't know how easy it would be to split
> this into two patches for straight code motion.
>
   It can't be split since build fail with original code, bdrv_states
is a static global variable in block.c.

>>    This patch also fix code style error reported by check script.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>>   block.c               |   76 ------------------------------------------------
>>   block/qapi.c          |   77 +++++++++++++++++++++++++++++++++++++++++++++++++
>>   include/block/block.h |    1 -
>>   include/block/qapi.h  |    1 +
>>   4 files changed, 78 insertions(+), 77 deletions(-)
>
> At any rate,
>
> Reviewed-by: Eric Blake <eblake@redhat.com>
>
>> -BlockInfoList *qmp_query_block(Error **errp)
>> -{
>> -    BlockInfoList *head = NULL, **p_next = &head;
>> -    BlockDriverState *bs;
>> -
>> -    QTAILQ_FOREACH(bs, &bdrv_states, list) {
>
>> +BlockInfoList *qmp_query_block(Error **errp)
>> +{
>> +    BlockInfoList *head = NULL, **p_next = &head;
>> +    BlockDriverState *bs = NULL;
>> +
>> +    while ((bs = bdrv_next(bs))) {
>
> This is the tweak you made.
>


-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots()
  2013-03-29 23:04   ` Eric Blake
@ 2013-03-30 12:38     ` Wenchao Xia
  2013-04-02  7:39       ` Kevin Wolf
  0 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-03-30 12:38 UTC (permalink / raw)
  To: Eric Blake
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

于 2013-3-30 7:04, Eric Blake 写道:
> On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>>    This function will simply call qmp interface qmp_query_snapshots()
>> added in last commit and then dump information in monitor console.
>>    To get snapshot info, Now qemu and qemu-img both call block layer
>> function bdrv_query_snapshot_info_list() in their calling path, and
>> then they just translate the qmp object got to strings in stdout or
>> monitor console.
>>
>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>> ---
>>   hmp.c |   42 ++++++++++++++++++++++++++++++++++++++++++
>>   hmp.h |    1 +
>>   2 files changed, 43 insertions(+), 0 deletions(-)
>>
>> diff --git a/hmp.c b/hmp.c
>> index b0a861c..c475d65 100644
>> --- a/hmp.c
>> +++ b/hmp.c
>> @@ -651,6 +651,48 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
>>       qapi_free_TPMInfoList(info_list);
>>   }
>>
>> +/* assume list is valid */
>> +static void monitor_dump_snapshotinfolist(Monitor *mon, SnapshotInfoList *list)
>> +{
>> +    SnapshotInfoList *elem;
>> +    char buf[256];
>> +
>> +    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
>
> Are you sure that won't ever be truncated?  I'm pretty sure that I could
> come up with a scenario where I cause bdrv_snapshot_dump() to want to
> output more than 256 bytes of details.
>
   I hope not use dynamic buff for strings, how about check it as
following:

char buf[254] = 0;
monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
if (buf[254]) {
      monitor_printf(mon, "'string truncated'");
}
-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one Wenchao Xia
@ 2013-04-01 15:56   ` Eric Blake
  0 siblings, 0 replies; 38+ messages in thread
From: Eric Blake @ 2013-04-01 15:56 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 742 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This patch using new added function in last commit which retrieve
> info from qmp for snapshot info.
> 
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>  monitor.c |    2 +-
>  savevm.c  |   64 -------------------------------------------------------------
>  2 files changed, 1 insertions(+), 65 deletions(-)

If I were maintainer, I would be okay with squashing this with the
previous one.  But since I'm not, you may want to get a second opinion.
 At any rate, I can also live with it being separate, so:

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump()
  2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump() Wenchao Xia
@ 2013-04-01 19:17   ` Eric Blake
  2013-04-02  2:42     ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Eric Blake @ 2013-04-01 19:17 UTC (permalink / raw)
  To: Wenchao Xia
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

[-- Attachment #1: Type: text/plain, Size: 1148 bytes --]

On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>   This allow hmp use this function, just like qemu-img.
> It also returns a pointer now to make it easy to use.
> 

>  
> -void bdrv_image_info_dump(ImageInfo *info)
> +GCC_FMT_ATTR(3, 4)
> +static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)

Yuck.  I'm too worried that you are likely to cause truncation when you
exceed the bounds of the fixed-width buffer.  And you can't argue that
this is here to avoid malloc pressure, since...

>  
> +#define IMAGE_INFO_BUF_SIZE (2048)
>  static void dump_human_image_info_list(ImageInfoList *list)
>  {
>      ImageInfoList *elem;
>      bool delim = false;
> +    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);

...you are doing a malloc for the original buffer in the first place.
I'd much rather see use of g_string_append_printf or some similar glib
interface that manages a dynamically-sized output buffer to begin with,
than to attempt to force the output to fit in a fixed-width malloc'd buffer.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 621 bytes --]

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump()
  2013-04-01 19:17   ` Eric Blake
@ 2013-04-02  2:42     ` Wenchao Xia
  0 siblings, 0 replies; 38+ messages in thread
From: Wenchao Xia @ 2013-04-02  2:42 UTC (permalink / raw)
  To: Eric Blake
  Cc: kwolf, aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

于 2013-4-2 3:17, Eric Blake 写道:
> On 03/22/2013 08:19 AM, Wenchao Xia wrote:
>>    This allow hmp use this function, just like qemu-img.
>> It also returns a pointer now to make it easy to use.
>>
>
>>
>> -void bdrv_image_info_dump(ImageInfo *info)
>> +GCC_FMT_ATTR(3, 4)
>> +static void snprintf_tail(char **p_buf, int *p_size, const char *fmt, ...)
>
> Yuck.  I'm too worried that you are likely to cause truncation when you
> exceed the bounds of the fixed-width buffer.  And you can't argue that
> this is here to avoid malloc pressure, since...
>
>>
>> +#define IMAGE_INFO_BUF_SIZE (2048)
>>   static void dump_human_image_info_list(ImageInfoList *list)
>>   {
>>       ImageInfoList *elem;
>>       bool delim = false;
>> +    char *buf = g_malloc0(IMAGE_INFO_BUF_SIZE);
>
> ...you are doing a malloc for the original buffer in the first place.
> I'd much rather see use of g_string_append_printf or some similar glib
> interface that manages a dynamically-sized output buffer to begin with,
> than to attempt to force the output to fit in a fixed-width malloc'd buffer.
>
   I have considered it before, but here g_malloc0 always allocate
a fixed half page size which brings less fragment than dynamic
allocation according to string length, just as my comments in code:

+/* Use buf instead of asprintf to reduce memory fragility. */
+char *bdrv_image_info_dump(char *buf, int buf_size, ImageInfo *info)

  Personally I'd like to avoid asprintf since there would be many
times of reallocation for a single info dumping, not worthy I think,
and there will not be much trouble if string truncate is tipped.


-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots()
  2013-03-30 12:38     ` Wenchao Xia
@ 2013-04-02  7:39       ` Kevin Wolf
  0 siblings, 0 replies; 38+ messages in thread
From: Kevin Wolf @ 2013-04-02  7:39 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 30.03.2013 um 13:38 hat Wenchao Xia geschrieben:
> 于 2013-3-30 7:04, Eric Blake 写道:
> >On 03/22/2013 08:19 AM, Wenchao Xia wrote:
> >>   This function will simply call qmp interface qmp_query_snapshots()
> >>added in last commit and then dump information in monitor console.
> >>   To get snapshot info, Now qemu and qemu-img both call block layer
> >>function bdrv_query_snapshot_info_list() in their calling path, and
> >>then they just translate the qmp object got to strings in stdout or
> >>monitor console.
> >>
> >>Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> >>---
> >>  hmp.c |   42 ++++++++++++++++++++++++++++++++++++++++++
> >>  hmp.h |    1 +
> >>  2 files changed, 43 insertions(+), 0 deletions(-)
> >>
> >>diff --git a/hmp.c b/hmp.c
> >>index b0a861c..c475d65 100644
> >>--- a/hmp.c
> >>+++ b/hmp.c
> >>@@ -651,6 +651,48 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict)
> >>      qapi_free_TPMInfoList(info_list);
> >>  }
> >>
> >>+/* assume list is valid */
> >>+static void monitor_dump_snapshotinfolist(Monitor *mon, SnapshotInfoList *list)
> >>+{
> >>+    SnapshotInfoList *elem;
> >>+    char buf[256];
> >>+
> >>+    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
> >
> >Are you sure that won't ever be truncated?  I'm pretty sure that I could
> >come up with a scenario where I cause bdrv_snapshot_dump() to want to
> >output more than 256 bytes of details.
> >
>   I hope not use dynamic buff for strings, how about check it as
> following:
> 
> char buf[254] = 0;
> monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
> if (buf[254]) {
>      monitor_printf(mon, "'string truncated'");
> }

About as useless as the truncated string alone. That something is
missing will be noticed by the user; the fix isn't to tell him that the
output is broken, but to give the full output.

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block
  2013-03-29  2:48     ` Wenchao Xia
@ 2013-04-02  7:46       ` Kevin Wolf
  0 siblings, 0 replies; 38+ messages in thread
From: Kevin Wolf @ 2013-04-02  7:46 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 29.03.2013 um 03:48 hat Wenchao Xia geschrieben:
> 于 2013-3-28 19:09, Kevin Wolf 写道:
> >Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
> >>   With these parameters, user can choose the information to be showed,
> >>to avoid message flood in the montior.
> >>
> >>Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> >
> >Aha, so here you actually introduce the 'device' parameter. If you can
> >have this patch first, and only then patch 16, then limiting the new
> >output to the form with a device specified should be trivial.
> >
>   It is a bit indirect that "info block" and "info block <device>"
> show different on single device, so I introduced additional parameter
> '-b' to filter out info. With more thinking, I think it should be
> {
>         .name       = "block",
>         .help       = "show the block devices",
>         .args_type  = "verbose:-v,device:B?",
>         .params     = "[-v] [device]",
>         .help       = "show info of one block device or all block devices "
>                       "and detail of images with -v option",
> }
>   Then by default "info block" so brief summary as old time. Since the
> "-v" parameter is filtering something out not present on patch 15,
> so I can't move this patch forward, hope you are OK with it.

'info block [-v] [<device>]' is okay with me. I would prefer if you
could arrange the patches so that without -v you never get verbose
output in any intermediate patch, but otherwise I don't mind about
the order of patches.

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-03-29  2:35     ` Wenchao Xia
@ 2013-04-02  8:09       ` Kevin Wolf
  2013-04-02  8:54         ` Wenchao Xia
  0 siblings, 1 reply; 38+ messages in thread
From: Kevin Wolf @ 2013-04-02  8:09 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, lcapitulino, pbonzini, armbru

Am 29.03.2013 um 03:35 hat Wenchao Xia geschrieben:
> 于 2013-3-28 17:54, Kevin Wolf 写道:
> >Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
> >>   Now image info will be retrieved as an embbed json object inside
> >>BlockDeviceInfo, backing chain info and all related internal snapshot
> >>info can be got in the enhanced recursive structure of ImageInfo.
> >>
> >>Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> >>---
> >>  block/qapi.c         |   39 ++++++++++++++++++++++++++--
> >>  include/block/qapi.h |    4 ++-
> >>  qapi-schema.json     |    5 +++-
> >>  qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
> >>  4 files changed, 109 insertions(+), 6 deletions(-)
> >>
> >>diff --git a/block/qapi.c b/block/qapi.c
> >>index df73f5b..9051947 100644
> >>--- a/block/qapi.c
> >>+++ b/block/qapi.c
> >>@@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
> >>      return 0;
> >>  }
> >>
> >>-BlockInfo *bdrv_query_info(BlockDriverState *bs)
> >>+/* return 0 on success, and @p_info will be set only on success. */
> >>+int bdrv_query_info(BlockDriverState *bs,
> >>+                    BlockInfo **p_info,
> >>+                    Error **errp)
> >>  {
> >>      BlockInfo *info = g_malloc0(sizeof(*info));
> >>+    BlockDriverState *bs0;
> >>+    ImageInfo **p_image_info;
> >>+    int ret = 0;
> >
> >ret is never changed, so this function always returns 0. I would suggest
> >to drop ret and make the function return type void.
> >
>   My bad, I forgot to set its value, the interface is intend to return
> negative error number and errp both on fail.

Why do you need two separate error reporting mechanisms? Shouldn't only
errp be enough?

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-04-02  8:09       ` Kevin Wolf
@ 2013-04-02  8:54         ` Wenchao Xia
  2013-04-02  9:30           ` Kevin Wolf
  0 siblings, 1 reply; 38+ messages in thread
From: Wenchao Xia @ 2013-04-02  8:54 UTC (permalink / raw)
  To: Kevin Wolf; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

于 2013-4-2 16:09, Kevin Wolf 写道:
> Am 29.03.2013 um 03:35 hat Wenchao Xia geschrieben:
>> 于 2013-3-28 17:54, Kevin Wolf 写道:
>>> Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
>>>>    Now image info will be retrieved as an embbed json object inside
>>>> BlockDeviceInfo, backing chain info and all related internal snapshot
>>>> info can be got in the enhanced recursive structure of ImageInfo.
>>>>
>>>> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
>>>> ---
>>>>   block/qapi.c         |   39 ++++++++++++++++++++++++++--
>>>>   include/block/qapi.h |    4 ++-
>>>>   qapi-schema.json     |    5 +++-
>>>>   qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
>>>>   4 files changed, 109 insertions(+), 6 deletions(-)
>>>>
>>>> diff --git a/block/qapi.c b/block/qapi.c
>>>> index df73f5b..9051947 100644
>>>> --- a/block/qapi.c
>>>> +++ b/block/qapi.c
>>>> @@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
>>>>       return 0;
>>>>   }
>>>>
>>>> -BlockInfo *bdrv_query_info(BlockDriverState *bs)
>>>> +/* return 0 on success, and @p_info will be set only on success. */
>>>> +int bdrv_query_info(BlockDriverState *bs,
>>>> +                    BlockInfo **p_info,
>>>> +                    Error **errp)
>>>>   {
>>>>       BlockInfo *info = g_malloc0(sizeof(*info));
>>>> +    BlockDriverState *bs0;
>>>> +    ImageInfo **p_image_info;
>>>> +    int ret = 0;
>>>
>>> ret is never changed, so this function always returns 0. I would suggest
>>> to drop ret and make the function return type void.
>>>
>>    My bad, I forgot to set its value, the interface is intend to return
>> negative error number and errp both on fail.
>
> Why do you need two separate error reporting mechanisms? Shouldn't only
> errp be enough?
>
> Kevin
>
   Returned value can tell caller what error it is, like -ENOMEDIUM.
Although it is not used by caller now, but I feel better to have it
just like

+int bdrv_query_snapshot_info_list(BlockDriverState *bs,
+                                  SnapshotInfoList **p_list,
+                                  Error **errp)

in patch 5/17.

-- 
Best Regards

Wenchao Xia

^ permalink raw reply	[flat|nested] 38+ messages in thread

* Re: [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block
  2013-04-02  8:54         ` Wenchao Xia
@ 2013-04-02  9:30           ` Kevin Wolf
  0 siblings, 0 replies; 38+ messages in thread
From: Kevin Wolf @ 2013-04-02  9:30 UTC (permalink / raw)
  To: Wenchao Xia; +Cc: aliguori, stefanha, qemu-devel, armbru, pbonzini, lcapitulino

Am 02.04.2013 um 10:54 hat Wenchao Xia geschrieben:
> 于 2013-4-2 16:09, Kevin Wolf 写道:
> >Am 29.03.2013 um 03:35 hat Wenchao Xia geschrieben:
> >>于 2013-3-28 17:54, Kevin Wolf 写道:
> >>>Am 22.03.2013 um 15:19 hat Wenchao Xia geschrieben:
> >>>>   Now image info will be retrieved as an embbed json object inside
> >>>>BlockDeviceInfo, backing chain info and all related internal snapshot
> >>>>info can be got in the enhanced recursive structure of ImageInfo.
> >>>>
> >>>>Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> >>>>---
> >>>>  block/qapi.c         |   39 ++++++++++++++++++++++++++--
> >>>>  include/block/qapi.h |    4 ++-
> >>>>  qapi-schema.json     |    5 +++-
> >>>>  qmp-commands.hx      |   67 +++++++++++++++++++++++++++++++++++++++++++++++++-
> >>>>  4 files changed, 109 insertions(+), 6 deletions(-)
> >>>>
> >>>>diff --git a/block/qapi.c b/block/qapi.c
> >>>>index df73f5b..9051947 100644
> >>>>--- a/block/qapi.c
> >>>>+++ b/block/qapi.c
> >>>>@@ -200,9 +200,15 @@ int bdrv_query_image_info(BlockDriverState *bs,
> >>>>      return 0;
> >>>>  }
> >>>>
> >>>>-BlockInfo *bdrv_query_info(BlockDriverState *bs)
> >>>>+/* return 0 on success, and @p_info will be set only on success. */
> >>>>+int bdrv_query_info(BlockDriverState *bs,
> >>>>+                    BlockInfo **p_info,
> >>>>+                    Error **errp)
> >>>>  {
> >>>>      BlockInfo *info = g_malloc0(sizeof(*info));
> >>>>+    BlockDriverState *bs0;
> >>>>+    ImageInfo **p_image_info;
> >>>>+    int ret = 0;
> >>>
> >>>ret is never changed, so this function always returns 0. I would suggest
> >>>to drop ret and make the function return type void.
> >>>
> >>   My bad, I forgot to set its value, the interface is intend to return
> >>negative error number and errp both on fail.
> >
> >Why do you need two separate error reporting mechanisms? Shouldn't only
> >errp be enough?
> >
> >Kevin
> >
>   Returned value can tell caller what error it is, like -ENOMEDIUM.
> Although it is not used by caller now, but I feel better to have it

No, let's remove it if there is no user. We can always add it back if we
ever need it. I doubt that we will.

Kevin

^ permalink raw reply	[flat|nested] 38+ messages in thread

end of thread, other threads:[~2013-04-02  9:30 UTC | newest]

Thread overview: 38+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-22 14:18 [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Wenchao Xia
2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 01/17] block: move bdrv_snapshot_find() to block/snapshot.c Wenchao Xia
2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 02/17] block: distinguish id and name in bdrv_find_snapshot() Wenchao Xia
2013-03-22 14:18 ` [Qemu-devel] [PATCH V10 03/17] qemu-img: remove unused parameter in collect_image_info() Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 04/17] block: move collect_snapshots() and collect_image_info() to block/qapi.c Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 05/17] block: add snapshot info query function bdrv_query_snapshot_info_list() Wenchao Xia
2013-03-27 21:31   ` Eric Blake
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 06/17] block: add check for VM snapshot in bdrv_query_snapshot_info_list() Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 07/17] block: add image info query function bdrv_query_image_info() Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 08/17] block: move qmp_query_block() and bdrv_query_info() to block/qapi.c Wenchao Xia
2013-03-29 20:10   ` Eric Blake
2013-03-30 12:32     ` Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 09/17] qmp: add interface query-snapshots Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 10/17] qmp: add recursive member in ImageInfo Wenchao Xia
2013-03-29 22:54   ` Eric Blake
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 11/17] qmp: add ImageInfo in BlockDeviceInfo used by query-block Wenchao Xia
2013-03-28  9:54   ` Kevin Wolf
2013-03-29  2:35     ` Wenchao Xia
2013-04-02  8:09       ` Kevin Wolf
2013-04-02  8:54         ` Wenchao Xia
2013-04-02  9:30           ` Kevin Wolf
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 12/17] hmp: add function hmp_info_snapshots() Wenchao Xia
2013-03-29 23:04   ` Eric Blake
2013-03-30 12:38     ` Wenchao Xia
2013-04-02  7:39       ` Kevin Wolf
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 13/17] hmp: switch snapshot info function to qmp based one Wenchao Xia
2013-04-01 15:56   ` Eric Blake
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 14/17] block: move dump_human_image_info() to block/qapi.c Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 15/17] block: dump to buffer for bdrv_image_info_dump() Wenchao Xia
2013-04-01 19:17   ` Eric Blake
2013-04-02  2:42     ` Wenchao Xia
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 16/17] hmp: show ImageInfo in 'info block' Wenchao Xia
2013-03-28 11:06   ` Kevin Wolf
2013-03-22 14:19 ` [Qemu-devel] [PATCH V10 17/17] hmp: add parameter device and -b for info block Wenchao Xia
2013-03-28 11:09   ` Kevin Wolf
2013-03-29  2:48     ` Wenchao Xia
2013-04-02  7:46       ` Kevin Wolf
2013-03-28 11:10 ` [Qemu-devel] [PATCH V10 00/17] qmp/hmp interfaces for internal snapshot info Kevin Wolf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.