Patchwork [v2,05/17] block: add error parameter to bdrv_snapshot_list() and related functions

login
register
mail settings
Submitter Pavel Hrdina
Date Dec. 13, 2012, 3:40 p.m.
Message ID <3862c38764846da34728349cf0b5c3fec31a8660.1355404685.git.phrdina@redhat.com>
Download mbox | patch
Permalink /patch/206163/
State New
Headers show

Comments

Pavel Hrdina - Dec. 13, 2012, 3:40 p.m.
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 block.c                | 26 ++++++++++++++++++--------
 block.h                |  3 ++-
 block/qcow2-snapshot.c |  5 ++++-
 block/qcow2.h          |  4 +++-
 block/rbd.c            |  4 +++-
 block/sheepdog.c       |  9 +++++++--
 block_int.h            |  3 ++-
 qemu-img.c             |  8 ++++++--
 savevm.c               |  4 ++--
 9 files changed, 47 insertions(+), 19 deletions(-)

Patch

diff --git a/block.c b/block.c
index 1ec4d24..2983dda 100644
--- a/block.c
+++ b/block.c
@@ -3152,16 +3152,26 @@  int bdrv_snapshot_delete(BlockDriverState *bs,
 }
 
 int bdrv_snapshot_list(BlockDriverState *bs,
-                       QEMUSnapshotInfo **psn_info)
+                       QEMUSnapshotInfo **psn_info,
+                       Error **errp)
 {
     BlockDriver *drv = bs->drv;
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_list)
-        return drv->bdrv_snapshot_list(bs, psn_info);
-    if (bs->file)
-        return bdrv_snapshot_list(bs->file, psn_info);
-    return -ENOTSUP;
+    int ret;
+
+    if (!drv) {
+        error_setg(errp, "Device '%s' has no medium.",
+                   bdrv_get_device_name(bs));
+        ret = -ENOMEDIUM;
+    } else if (drv->bdrv_snapshot_list) {
+        ret = drv->bdrv_snapshot_list(bs, psn_info, errp);
+    } else if (bs->file) {
+        ret = bdrv_snapshot_list(bs->file, psn_info, errp);
+    } else {
+        error_setg(errp, "Not supported.");
+        ret = -ENOTSUP;
+    }
+
+    return ret;
 }
 
 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
diff --git a/block.h b/block.h
index b6b25a7..ac28fff 100644
--- a/block.h
+++ b/block.h
@@ -330,7 +330,8 @@  int bdrv_snapshot_delete(BlockDriverState *bs,
                          const char *snapshot_id,
                          Error **errp);
 int bdrv_snapshot_list(BlockDriverState *bs,
-                       QEMUSnapshotInfo **psn_info);
+                       QEMUSnapshotInfo **psn_info,
+                       Error **errp);
 int bdrv_snapshot_load_tmp(BlockDriverState *bs,
                            const char *snapshot_name);
 char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
index 0bd0d1f..c834605 100644
--- a/block/qcow2-snapshot.c
+++ b/block/qcow2-snapshot.c
@@ -614,7 +614,9 @@  int qcow2_snapshot_delete(BlockDriverState *bs,
     return 0;
 }
 
-int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
+int qcow2_snapshot_list(BlockDriverState *bs,
+                        QEMUSnapshotInfo **psn_tab,
+                        Error **errp)
 {
     BDRVQcowState *s = bs->opaque;
     QEMUSnapshotInfo *sn_tab, *sn_info;
@@ -623,6 +625,7 @@  int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
 
     if (!s->nb_snapshots) {
         *psn_tab = NULL;
+        error_setg(errp, "No snapshot list find.");
         return s->nb_snapshots;
     }
 
diff --git a/block/qcow2.h b/block/qcow2.h
index 8dfdbb7..2fe5687 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -317,7 +317,9 @@  int qcow2_snapshot_goto(BlockDriverState *bs,
 int qcow2_snapshot_delete(BlockDriverState *bs,
                           const char *snapshot_id,
                           Error **errp);
-int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
+int qcow2_snapshot_list(BlockDriverState *bs,
+                        QEMUSnapshotInfo **psn_tab,
+                        Error **errp);
 int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
 
 void qcow2_free_snapshots(BlockDriverState *bs);
diff --git a/block/rbd.c b/block/rbd.c
index f8fe938..d05e9d1 100644
--- a/block/rbd.c
+++ b/block/rbd.c
@@ -876,7 +876,8 @@  static int qemu_rbd_snap_rollback(BlockDriverState *bs,
 }
 
 static int qemu_rbd_snap_list(BlockDriverState *bs,
-                              QEMUSnapshotInfo **psn_tab)
+                              QEMUSnapshotInfo **psn_tab,
+                              Error **errp)
 {
     BDRVRBDState *s = bs->opaque;
     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
@@ -893,6 +894,7 @@  static int qemu_rbd_snap_list(BlockDriverState *bs,
     } while (snap_count == -ERANGE);
 
     if (snap_count <= 0) {
+        error_setg(errp, "No snapshot list find.");
         goto done;
     }
 
diff --git a/block/sheepdog.c b/block/sheepdog.c
index c7f8d9e..c2cd3ad 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1887,7 +1887,9 @@  static int sd_snapshot_delete(BlockDriverState *bs,
     return 0;
 }
 
-static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
+static int sd_snapshot_list(BlockDriverState *bs,
+                            QEMUSnapshotInfo **psn_tab,
+                            Error **errp)
 {
     BDRVSheepdogState *s = bs->opaque;
     SheepdogReq req;
@@ -1905,6 +1907,7 @@  static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
 
     fd = connect_to_sdog(s->addr, s->port);
     if (fd < 0) {
+        error_setg(errp, "Failed to connect to sdog.");
         ret = fd;
         goto out;
     }
@@ -1921,6 +1924,7 @@  static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
 
     closesocket(fd);
     if (ret) {
+        error_setg(errp, "No snapshot list find.");
         goto out;
     }
 
@@ -1932,7 +1936,7 @@  static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
 
     fd = connect_to_sdog(s->addr, s->port);
     if (fd < 0) {
-        error_report("failed to connect");
+        error_setg(errp, "Failed to connect to sdog.");
         ret = fd;
         goto out;
     }
@@ -1973,6 +1977,7 @@  out:
     g_free(vdi_inuse);
 
     if (ret < 0) {
+        error_setg(errp, "%s", strerror(errno));
         return ret;
     }
 
diff --git a/block_int.h b/block_int.h
index 779e3cb..cc60f51 100644
--- a/block_int.h
+++ b/block_int.h
@@ -156,7 +156,8 @@  struct BlockDriver {
                                 const char *snapshot_id,
                                 Error **errp);
     int (*bdrv_snapshot_list)(BlockDriverState *bs,
-                              QEMUSnapshotInfo **psn_info);
+                              QEMUSnapshotInfo **psn_info,
+                              Error **errp);
     int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
                                   const char *snapshot_name);
     int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
diff --git a/qemu-img.c b/qemu-img.c
index fd9b525..d1690d4 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -1099,8 +1099,10 @@  static void dump_snapshots(BlockDriverState *bs)
     QEMUSnapshotInfo *sn_tab, *sn;
     int nb_sns, i;
     char buf[256];
+    Error *err = NULL;
 
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs, &sn_tab, &err);
+    handle_error(&err);
     if (nb_sns <= 0)
         return;
     printf("Snapshot list:\n");
@@ -1134,7 +1136,9 @@  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);
+    Error *err = NULL;
+    sn_count = bdrv_snapshot_list(bs, &sn_tab, &err);
+    handle_error(&err);
 
     for (i = 0; i < sn_count; i++) {
         info->has_snapshots = true;
diff --git a/savevm.c b/savevm.c
index 2aaa08b..e57c108 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2068,7 +2068,7 @@  static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
     int nb_sns, i, ret;
 
     ret = -ENOENT;
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs, &sn_tab, NULL);
     if (nb_sns < 0)
         return ret;
     for(i = 0; i < nb_sns; i++) {
@@ -2373,7 +2373,7 @@  void do_info_snapshots(Monitor *mon)
         return;
     }
 
-    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
+    nb_sns = bdrv_snapshot_list(bs, &sn_tab, NULL);
     if (nb_sns < 0) {
         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
         return;