Patchwork [V9,1/8] snapshot: add parameter *errp in snapshot create

login
register
mail settings
Submitter Wayne Xia
Date Jan. 5, 2014, 7:43 p.m.
Message ID <1388950991-30105-2-git-send-email-xiawenc@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/307088/
State New
Headers show

Comments

Wayne Xia - Jan. 5, 2014, 7:43 p.m.
The return value is only used for error report before this patch,
so change the function protype to return void.

Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
---
 block/qcow2-snapshot.c    |   30 +++++++++++++++++++++++++-----
 block/qcow2.h             |    4 +++-
 block/rbd.c               |   19 ++++++++++---------
 block/sheepdog.c          |   28 ++++++++++++++++++----------
 block/snapshot.c          |   19 +++++++++++++------
 blockdev.c                |   10 ++++------
 include/block/block_int.h |    5 +++--
 include/block/snapshot.h  |    5 +++--
 qemu-img.c                |   10 ++++++----
 savevm.c                  |   12 ++++++++----
 10 files changed, 93 insertions(+), 49 deletions(-)
Max Reitz - Jan. 11, 2014, 10:47 p.m.
On 05.01.2014 20:43, Wenchao Xia wrote:
> The return value is only used for error report before this patch,
> so change the function protype to return void.
>
> Signed-off-by: Wenchao Xia <xiawenc@linux.vnet.ibm.com>
> ---
>   block/qcow2-snapshot.c    |   30 +++++++++++++++++++++++++-----
>   block/qcow2.h             |    4 +++-
>   block/rbd.c               |   19 ++++++++++---------
>   block/sheepdog.c          |   28 ++++++++++++++++++----------
>   block/snapshot.c          |   19 +++++++++++++------
>   blockdev.c                |   10 ++++------
>   include/block/block_int.h |    5 +++--
>   include/block/snapshot.h  |    5 +++--
>   qemu-img.c                |   10 ++++++----
>   savevm.c                  |   12 ++++++++----
>   10 files changed, 93 insertions(+), 49 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>

Patch

diff --git a/block/qcow2-snapshot.c b/block/qcow2-snapshot.c
index ad8bf3d..8cac42d 100644
--- a/block/qcow2-snapshot.c
+++ b/block/qcow2-snapshot.c
@@ -347,7 +347,9 @@  static int find_snapshot_by_id_or_name(BlockDriverState *bs,
 }
 
 /* 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;
@@ -366,7 +368,8 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
     /* Check that the ID is unique */
     if (find_snapshot_by_id_and_name(bs, sn_info->id_str, NULL) >= 0) {
-        return -EEXIST;
+        error_setg(errp, "Snapshot with id %s already exists", sn_info->id_str);
+        return;
     }
 
     /* Populate sn with passed data */
@@ -382,7 +385,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,
+                         "Failed in allocation of snapshot L1 table");
         goto fail;
     }
 
@@ -397,12 +401,22 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     ret = qcow2_pre_write_overlap_check(bs, 0, sn->l1_table_offset,
                                         s->l1_size * sizeof(uint64_t));
     if (ret < 0) {
+        error_setg_errno(errp, -ret,
+                         "Failed in overlap check for snapshot L1 table at %"
+                         PRIu64 " with size %" PRIu64,
+                         sn->l1_table_offset,
+                         (uint64_t)(s->l1_size * sizeof(uint64_t)));
         goto fail;
     }
 
     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,
+                         "Failed in update of snapshot L1 table at %"
+                         PRIu64 " with size %" PRIu64,
+                         sn->l1_table_offset,
+                         (uint64_t)(s->l1_size * sizeof(uint64_t)));
         goto fail;
     }
 
@@ -416,6 +430,10 @@  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,
+                         "Failed in update of refcount for snapshot at %"
+                         PRIu64 " with size %d",
+                         s->l1_table_offset, s->l1_size);
         goto fail;
     }
 
@@ -431,6 +449,8 @@  int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
     ret = qcow2_write_snapshots(bs);
     if (ret < 0) {
+        /* Following line will be replaced with more detailed error later */
+        error_setg(errp, "Failed in write of snapshot");
         g_free(s->snapshots);
         s->snapshots = old_snapshot_list;
         s->nb_snapshots--;
@@ -452,14 +472,14 @@  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;
+    return;
 }
 
 /* copy the snapshot 'snapshot_name' into the current disk image */
diff --git a/block/qcow2.h b/block/qcow2.h
index 303eb26..c56a5b6 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -481,7 +481,9 @@  int qcow2_zero_clusters(BlockDriverState *bs, uint64_t offset, int nb_sectors);
 int qcow2_expand_zero_clusters(BlockDriverState *bs);
 
 /* qcow2-snapshot.c functions */
-int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
+void qcow2_snapshot_create(BlockDriverState *bs,
+                           QEMUSnapshotInfo *sn_info,
+                           Error **errp);
 int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
 int qcow2_snapshot_delete(BlockDriverState *bs,
                           const char *snapshot_id,
diff --git a/block/rbd.c b/block/rbd.c
index 4a1ea5b..5740f5e 100644
--- a/block/rbd.c
+++ b/block/rbd.c
@@ -860,14 +860,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 snapshot requires a valid name");
+        return; /* we need a name for rbd snapshots */
     }
 
     /*
@@ -876,20 +878,19 @@  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 snapshot id should be empty or equal to name");
+        return;
     }
 
     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
-        return -ERANGE;
+        error_setg(errp, "rbd snapshot name is too long");
+        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, "Failed to create snapshot");
     }
-
-    return 0;
 }
 
 static int qemu_rbd_snap_remove(BlockDriverState *bs,
diff --git a/block/sheepdog.c b/block/sheepdog.c
index d1c812d..7001900 100644
--- a/block/sheepdog.c
+++ b/block/sheepdog.c
@@ -2116,7 +2116,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;
@@ -2129,10 +2131,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);
+        error_setg(errp, "You can't create a snapshot of a snapshot VDI, "
+                   "%s (%" PRIu32 ")", s->name, s->inode.vdi_id);
 
-        return -EINVAL;
+        return;
     }
 
     DPRINTF("%s %s\n", sn_info->name, sn_info->id_str);
@@ -2149,21 +2151,24 @@  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, "Failed to connect");
         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, "Failed to write snapshot's inode");
         goto cleanup;
     }
 
     ret = do_sd_create(s, &new_vid, 1);
     if (ret < 0) {
-        error_report("failed to create inode for snapshot. %s",
-                     strerror(errno));
+        error_setg(errp,
+                   "Failed to create inode for snapshot: %d (%s), "
+                   "errno %d (%s)",
+                   ret, strerror(-ret),
+                   errno, strerror(errno));
         goto cleanup;
     }
 
@@ -2173,7 +2178,10 @@  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(errp, "Failed to read new inode info: %d (%s), "
+                   "errno %d (%s)",
+                   ret, strerror(-ret),
+                   errno, strerror(errno));
         goto cleanup;
     }
 
@@ -2183,7 +2191,7 @@  static int sd_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 
 cleanup:
     closesocket(fd);
-    return ret;
+    return;
 }
 
 /*
diff --git a/block/snapshot.c b/block/snapshot.c
index 9047f8d..b2a49e7 100644
--- a/block/snapshot.c
+++ b/block/snapshot.c
@@ -156,20 +156,27 @@  int bdrv_can_snapshot(BlockDriverState *bs)
     return 1;
 }
 
-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;
+        error_set(errp, QERR_DEVICE_HAS_NO_MEDIUM, bdrv_get_device_name(bs));
+        return;
     }
     if (drv->bdrv_snapshot_create) {
-        return drv->bdrv_snapshot_create(bs, sn_info);
+        drv->bdrv_snapshot_create(bs, sn_info, errp);
+        return;
     }
     if (bs->file) {
-        return bdrv_snapshot_create(bs->file, sn_info);
+        bdrv_snapshot_create(bs->file, sn_info, errp);
+        return;
     }
-    return -ENOTSUP;
+
+    error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
+              drv->format_name, bdrv_get_device_name(bs),
+              "internal snapshot creation");
 }
 
 int bdrv_snapshot_goto(BlockDriverState *bs,
diff --git a/blockdev.c b/blockdev.c
index 6a85961..2e45898 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1080,7 +1080,7 @@  static void internal_snapshot_prepare(BlkTransactionState *common,
     qemu_timeval tv;
     BlockdevSnapshotInternal *internal;
     InternalSnapshotState *state;
-    int ret1;
+    Error *local_err = NULL;
 
     g_assert(common->action->kind ==
              TRANSACTION_ACTION_KIND_BLOCKDEV_SNAPSHOT_INTERNAL_SYNC);
@@ -1138,11 +1138,9 @@  static void internal_snapshot_prepare(BlkTransactionState *common,
     sn->date_nsec = tv.tv_usec * 1000;
     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 
-    ret1 = bdrv_snapshot_create(bs, sn);
-    if (ret1 < 0) {
-        error_setg_errno(errp, -ret1,
-                         "Failed to create snapshot '%s' on device '%s'",
-                         name, device);
+    bdrv_snapshot_create(bs, sn, &local_err);
+    if (error_is_set(&local_err)) {
+        error_propagate(errp, local_err);
         return;
     }
 
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 8b132d7..28b7a14 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -165,8 +165,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);
     int (*bdrv_snapshot_goto)(BlockDriverState *bs,
                               const char *snapshot_id);
     int (*bdrv_snapshot_delete)(BlockDriverState *bs,
diff --git a/include/block/snapshot.h b/include/block/snapshot.h
index 770d9bb..5f286da 100644
--- a/include/block/snapshot.h
+++ b/include/block/snapshot.h
@@ -55,8 +55,9 @@  bool bdrv_snapshot_find_by_id_and_name(BlockDriverState *bs,
                                        QEMUSnapshotInfo *sn_info,
                                        Error **errp);
 int bdrv_can_snapshot(BlockDriverState *bs);
-int bdrv_snapshot_create(BlockDriverState *bs,
-                         QEMUSnapshotInfo *sn_info);
+void bdrv_snapshot_create(BlockDriverState *bs,
+                          QEMUSnapshotInfo *sn_info,
+                          Error **errp);
 int bdrv_snapshot_goto(BlockDriverState *bs,
                        const char *snapshot_id);
 int bdrv_snapshot_delete(BlockDriverState *bs,
diff --git a/qemu-img.c b/qemu-img.c
index a4b3931..97dec9a 100644
--- a/qemu-img.c
+++ b/qemu-img.c
@@ -2167,10 +2167,12 @@  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, &err);
+        if (error_is_set(&err)) {
+            error_report("Could not create snapshot '%s': %s",
+                         snapshot_name, error_get_pretty(err));
+            error_free(err);
+            ret = 1;
         }
         break;
 
diff --git a/savevm.c b/savevm.c
index 3f912dd..993be2e 100644
--- a/savevm.c
+++ b/savevm.c
@@ -2366,6 +2366,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 *err = NULL;
 
     /* Verify if there is a device that doesn't support snapshots and is writable */
     bs = NULL;
@@ -2439,10 +2440,13 @@  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, &err);
+            if (error_is_set(&err)) {
+                monitor_printf(mon,
+                               "Error while creating snapshot on '%s': %s\n",
+                               bdrv_get_device_name(bs1),
+                               error_get_pretty(err));
+                error_free(err);
             }
         }
     }