Patchwork [08/11] block: update error reporting for bdrv_snapshot_create() and related functions

login
register
mail settings
Submitter Pavel Hrdina
Date April 16, 2013, 4:05 p.m.
Message ID <acd6e17b7efc83e76a24f14ba8cfe8a55c370224.1366127809.git.phrdina@redhat.com>
Download mbox | patch
Permalink /patch/237026/
State New
Headers show

Comments

Pavel Hrdina - April 16, 2013, 4:05 p.m.
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
---
 block.c                   | 23 ++++++++++++++---------
 block/qcow2-snapshot.c    | 18 ++++++++++++------
 block/qcow2.h             |  4 +++-
 block/rbd.c               | 20 +++++++++++---------
 block/sheepdog.c          | 22 +++++++++++-----------
 include/block/block.h     |  5 +++--
 include/block/block_int.h |  5 +++--
 qemu-img.c                |  8 +++-----
 savevm.c                  |  9 +++++----
 9 files changed, 65 insertions(+), 49 deletions(-)
Eric Blake - April 16, 2013, 11:54 p.m.
On 04/16/2013 10:05 AM, Pavel Hrdina wrote:
> Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
> ---
>  block.c                   | 23 ++++++++++++++---------
>  block/qcow2-snapshot.c    | 18 ++++++++++++------
>  block/qcow2.h             |  4 +++-
>  block/rbd.c               | 20 +++++++++++---------
>  block/sheepdog.c          | 22 +++++++++++-----------
>  include/block/block.h     |  5 +++--
>  include/block/block_int.h |  5 +++--
>  qemu-img.c                |  8 +++-----
>  savevm.c                  |  9 +++++----
>  9 files changed, 65 insertions(+), 49 deletions(-)
> 

> @@ -855,20 +857,20 @@ static int qemu_rbd_snap_create(BlockDriverState *bs,
>       */
>      if (sn_info->id_str[0] != '\0' &&
>          strcmp(sn_info->id_str, sn_info->name) != 0) {
> -        return -EINVAL;
> +        error_setg(errp, "rbd: ID and name have to be equal");
> +        return;

Who generates sn_info->id_str in the first place?  Didn't you just say
that the QMP interface does NOT expose user control over id_str, and
that it gets populated as a side effect of creation?  That says that
this error condition is probably dead code, if the QMP command never
attempts to generate id_str itself.

At any rate, the conversion to errp looks reasonable.

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

Patch

diff --git a/block.c b/block.c
index a760a2f..7cf24f5 100644
--- a/block.c
+++ b/block.c
@@ -3352,17 +3352,22 @@  BlockDriverState *bdrv_snapshots(void)
     return NULL;
 }
 
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info)
+void bdrv_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp)
 {
     BlockDriver *drv = bs->drv;
-    if (!drv)
-        return -ENOMEDIUM;
-    if (drv->bdrv_snapshot_create)
-        return drv->bdrv_snapshot_create(bs, sn_info);
-    if (bs->file)
-        return bdrv_snapshot_create(bs->file, sn_info);
-    return -ENOTSUP;
+
+    if (!drv) {
+        error_setg(errp, "device '%s' has no medium", bdrv_get_device_name(bs));
+    } else if (drv->bdrv_snapshot_create) {
+        drv->bdrv_snapshot_create(bs, sn_info, errp);
+    } else if (bs->file) {
+        bdrv_snapshot_create(bs->file, sn_info, errp);
+    } else {
+        error_setg(errp, "snapshots are not supported on device '%s'",
+                           bdrv_get_device_name(bs));
+    }
 }
 
 void bdrv_snapshot_goto(BlockDriverState *bs,
diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
index 4a69b88..bb6a35d 100644
--- a/block/qcow2-snapshot.c
+++ b/block/qcow2-snapshot.c
@@ -315,7 +315,9 @@  static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
 }
 
 /* if no id is provided, a new one is constructed */
-int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
+void qcow2_snapshot_create(BlockDriverState *bs,
+                           QEMUSnapshotInfo *sn_info,
+                           Error **errp)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *new_snapshot_list = NULL;
@@ -334,7 +336,8 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
     /* Check that the ID is unique */
     if (find_snapshot_by_id(bs, sn_info->id_str) >= 0) {
-        return -EEXIST;
+        error_setg(errp, "qcow2: parameter 'id' has to be unique ID");
+        return;
     }
 
     /* Populate sn with passed data */
@@ -350,7 +353,8 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     /* Allocate the L1 table of the snapshot and copy the current one there. */
     l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
     if (l1_table_offset < 0) {
-        ret = l1_table_offset;
+        error_setg_errno(errp, -l1_table_offset,
+                         "qcow2: failed to allocate L1 table");
         goto fail;
     }
 
@@ -365,6 +369,7 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     ret = bdrv_pwrite(bs->file, sn->l1_table_offset, l1_table,
                       s->l1_size * sizeof(uint64_t));
     if (ret < 0) {
+        error_setg_errno(errp, -ret, "qcow2: failed to save L1 table");
         goto fail;
     }
 
@@ -378,6 +383,8 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
      */
     ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
     if (ret < 0) {
+        error_setg_errno(errp, -ret,
+                         "qcow2: failed to update snapshot refcount");
         goto fail;
     }
 
@@ -395,6 +402,7 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     if (ret < 0) {
         g_free(s->snapshots);
         s->snapshots = old_snapshot_list;
+        error_setg_errno(errp, -ret, "qcow2: failed to write new snapshot");
         goto fail;
     }
 
@@ -406,14 +414,12 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
       qcow2_check_refcounts(bs, &result, 0);
     }
 #endif
-    return 0;
+    return;
 
 fail:
     g_free(sn->id_str);
     g_free(sn->name);
     g_free(l1_table);
-
-    return ret;
 }
 
 /* copy the snapshot 'snapshot_name' into the current disk image */
diff --git a/block/qcow2.h b/block/qcow2.h
index ae62953..c194cff 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -382,7 +382,9 @@  int qcow2_discard_clusters(BlockDriverState *bs, uint64_t offset,
 int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
 
 /* qcow2-snapshot.c functions */
-int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
+void qcow2_snapshot_create(BlockDriverState *bs,
+                           QEMUSnapshotInfo *sn_info,
+                           Error **errp);
 void qcow2_snapshot_goto(BlockDriverState *bs,
                          const char *snapshot_id,
                          Error **errp);
diff --git a/block/rbd.c b/block/rbd.c
index 9259621..1f073fe 100644
--- a/block/rbd.c
+++ b/block/rbd.c
@@ -839,14 +839,16 @@  static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
     return 0;
 }
 
-static int qemu_rbd_snap_create(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info)
+static void qemu_rbd_snap_create(BlockDriverState *bs,
+                                 QEMUSnapshotInfo *sn_info,
+                                 Error **errp)
 {
     BDRVRBDState *s = bs->opaque;
     int r;
 
     if (sn_info->name[0] == '\0') {
-        return -EINVAL; /* we need a name for rbd snapshots */
+        error_setg(errp, "rbd: parameter 'name' cannot be empty");
+        return;
     }
 
     /*
@@ -855,20 +857,20 @@  static int qemu_rbd_snap_create(BlockDriverState *bs,
      */
     if (sn_info->id_str[0] != '\0' &&
         strcmp(sn_info->id_str, sn_info->name) != 0) {
-        return -EINVAL;
+        error_setg(errp, "rbd: ID and name have to be equal");
+        return;
     }
 
     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
-        return -ERANGE;
+        error_setg(errp, "rbd: parameter 'name' has to be shorter than %zd "
+                   "chars", sizeof(sn_info->id_str));
+        return;
     }
 
     r = rbd_snap_create(s->image, sn_info->name);
     if (r < 0) {
-        error_report("failed to create snap: %s", strerror(-r));
-        return r;
+        error_setg_errno(errp, -r, "rbd: failed to create snapshot");
     }
-
-    return 0;
 }
 
 static void qemu_rbd_snap_remove(BlockDriverState *bs,
diff --git a/block/sheepdog.c b/block/sheepdog.c
index 3d44575..996441d 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -1767,7 +1767,9 @@  static int coroutine_fn sd_co_flush_to_disk(BlockDriverState *bs)
     return acb->ret;
 }
 
-static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
+static void sd_snapshot_create(BlockDriverState *bs,
+                               QEMUSnapshotInfo *sn_info,
+                               Error **errp)
 {
     BDRVSheepdogState *s = bs->opaque;
     int ret, fd;
@@ -1780,10 +1782,10 @@  static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
             s->name, sn_info->vm_state_size, s->is_snapshot);
 
     if (s->is_snapshot) {
-        error_report("You can't create a snapshot of a snapshot VDI, "
-                     "%s (%" PRIu32 ").", s->name, s->inode.vdi_id);
-
-        return -EINVAL;
+        error_setg(errp, "sd: you can't create a snapshot '%s' of a snapshot "
+                "VDI %s (%" PRIu32 ")", sn_info->name, s->name,
+                s->inode.vdi_id);
+        return;
     }
 
     dprintf("%s %s\n", sn_info->name, sn_info->id_str);
@@ -1800,22 +1802,21 @@  static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     /* refresh inode. */
     fd = connect_to_sdog(s);
     if (fd < 0) {
-        ret = fd;
+        error_setg_errno(errp, -fd, "sd: failed to connect to sdog");
         goto cleanup;
     }
 
     ret = write_object(fd, (char *)&s->inode, vid_to_vdi_oid(s->inode.vdi_id),
                        s->inode.nr_copies, datalen, 0, false, s->cache_flags);
     if (ret < 0) {
-        error_report("failed to write snapshot's inode.");
+        error_setg_errno(errp, -ret, "sd: failed to write snapshot's inode");
         goto cleanup;
     }
 
     ret = do_sd_create(s, s->name, s->inode.vdi_size, s->inode.vdi_id, &new_vid,
                        1);
     if (ret < 0) {
-        error_report("failed to create inode for snapshot. %s",
-                     strerror(errno));
+        error_setg_errno(errp, -ret, "sd: failed to create inode for snapshot");
         goto cleanup;
     }
 
@@ -1825,7 +1826,7 @@  static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
                       s->inode.nr_copies, datalen, 0, s->cache_flags);
 
     if (ret < 0) {
-        error_report("failed to read new inode info. %s", strerror(errno));
+        error_setg_errno(errp, -ret, "sd: failed to read new inode info");
         goto cleanup;
     }
 
@@ -1835,7 +1836,6 @@  static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
 cleanup:
     closesocket(fd);
-    return ret;
 }
 
 static void sd_snapshot_goto(BlockDriverState *bs,
diff --git a/include/block/block.h b/include/block/block.h
index 33d498b..fb5a864 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -333,8 +333,9 @@  BlockStats *bdrv_query_stats(const BlockDriverState *bs);
 int bdrv_can_snapshot(BlockDriverState *bs);
 int bdrv_is_snapshot(BlockDriverState *bs);
 BlockDriverState *bdrv_snapshots(void);
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info);
+void bdrv_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp);
 void bdrv_snapshot_goto(BlockDriverState *bs,
                         const char *snapshot_id,
                         Error **errp);
diff --git a/include/block/block_int.h b/include/block/block_int.h
index c56d923..9903874 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -153,8 +153,9 @@  struct BlockDriver {
     int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
                                  const uint8_t *buf, int nb_sectors);
 
-    int (*bdrv_snapshot_create)(BlockDriverState *bs,
-                                QEMUSnapshotInfo *sn_info);
+    void (*bdrv_snapshot_create)(BlockDriverState *bs,
+                                 QEMUSnapshotInfo *sn_info,
+                                 Error **errp);
     void (*bdrv_snapshot_goto)(BlockDriverState *bs,
                                const char *snapshot_id,
                                Error **errp);
diff --git a/qemu-img.c b/qemu-img.c
index 06cd8af..2e420f3 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2024,11 +2024,9 @@  static int img_snapshot(int argc, char **argv)
         sn.date_sec = tv.tv_sec;
         sn.date_nsec = tv.tv_usec * 1000;
 
-        ret = bdrv_snapshot_create(bs, &sn);
-        if (ret) {
-            error_report("Could not create snapshot '%s': %d (%s)",
-                snapshot_name, ret, strerror(-ret));
-        }
+        bdrv_snapshot_create(bs, &sn, &local_err);
+        ret = qemu_img_handle_error("qemu-img snapshot create failed",
+                                    local_err);
         break;
 
     case SNAPSHOT_APPLY:
diff --git a/savevm.c b/savevm.c
index 1258f5f..a8e7d60 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2283,6 +2283,7 @@  void do_savevm(Monitor *mon, const QDict *qdict)
     qemu_timeval tv;
     struct tm tm;
     const char *name = qdict_get_try_str(qdict, "name");
+    Error *local_err = NULL;
 
     /* Verify if there is a device that doesn't support snapshots and is writable */
     bs = NULL;
@@ -2355,10 +2356,10 @@  void do_savevm(Monitor *mon, const QDict *qdict)
         if (bdrv_can_snapshot(bs1)) {
             /* Write VM state size only to the image that contains the state */
             sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
-            ret = bdrv_snapshot_create(bs1, sn);
-            if (ret < 0) {
-                monitor_printf(mon, "Error while creating snapshot on '%s'\n",
-                               bdrv_get_device_name(bs1));
+            bdrv_snapshot_create(bs1, sn, &local_err);
+            if (error_is_set(&local_err)) {
+                monitor_printf(mon, "'%s'\n", error_get_pretty(local_err));
+                error_free(local_err);
             }
         }
     }