9ae3a8
From 8f07e694eb932e0f3d50ad1083155b314e0162ca Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Mon, 4 Nov 2013 22:32:12 +0100
9ae3a8
Subject: [PATCH 19/87] block: Error parameter for create functions
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1383604354-12743-22-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 55321
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 21/43] block: Error parameter for create functions
9ae3a8
Bugzilla: 1026524
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1026524
9ae3a8
9ae3a8
Add an Error ** parameter to bdrv_create and its associated functions to
9ae3a8
allow more specific error messages.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
(cherry picked from commit cc84d90ff54c025190dbe49ec5fea1268217c5f2)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block/raw.c
9ae3a8
	block/raw_bsd.c
9ae3a8
9ae3a8
Conflicts because raw_bsd.c does not exist downstream; instead, there is
9ae3a8
raw.c.
9ae3a8
---
9ae3a8
 block.c               | 80 +++++++++++++++++++++++++++++++++++----------------
9ae3a8
 block/cow.c           |  4 ++-
9ae3a8
 block/qcow.c          |  4 ++-
9ae3a8
 block/qcow2.c         |  2 +-
9ae3a8
 block/qed.c           |  4 ++-
9ae3a8
 block/raw.c           | 10 ++++++-
9ae3a8
 block/vvfat.c         |  4 ++-
9ae3a8
 include/block/block.h |  5 ++--
9ae3a8
 qemu-img.c            | 16 ++++-------
9ae3a8
 9 files changed, 86 insertions(+), 43 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block.c               |   80 ++++++++++++++++++++++++++++++++++--------------
9ae3a8
 block/cow.c           |    4 ++-
9ae3a8
 block/qcow.c          |    4 ++-
9ae3a8
 block/qcow2.c         |    2 +-
9ae3a8
 block/qed.c           |    4 ++-
9ae3a8
 block/raw.c           |   10 +++++-
9ae3a8
 block/vvfat.c         |    4 ++-
9ae3a8
 include/block/block.h |    5 ++-
9ae3a8
 qemu-img.c            |   16 +++-------
9ae3a8
 9 files changed, 86 insertions(+), 43 deletions(-)
9ae3a8
9ae3a8
diff --git a/block.c b/block.c
9ae3a8
index cb99faf..c2b6930 100644
9ae3a8
--- a/block.c
9ae3a8
+++ b/block.c
9ae3a8
@@ -366,18 +366,26 @@ typedef struct CreateCo {
9ae3a8
     char *filename;
9ae3a8
     QEMUOptionParameter *options;
9ae3a8
     int ret;
9ae3a8
+    Error *err;
9ae3a8
 } CreateCo;
9ae3a8
 
9ae3a8
 static void coroutine_fn bdrv_create_co_entry(void *opaque)
9ae3a8
 {
9ae3a8
+    Error *local_err = NULL;
9ae3a8
+    int ret;
9ae3a8
+
9ae3a8
     CreateCo *cco = opaque;
9ae3a8
     assert(cco->drv);
9ae3a8
 
9ae3a8
-    cco->ret = cco->drv->bdrv_create(cco->filename, cco->options, NULL);
9ae3a8
+    ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err);
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        error_propagate(&cco->err, local_err);
9ae3a8
+    }
9ae3a8
+    cco->ret = ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
 int bdrv_create(BlockDriver *drv, const char* filename,
9ae3a8
-    QEMUOptionParameter *options)
9ae3a8
+    QEMUOptionParameter *options, Error **errp)
9ae3a8
 {
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
@@ -387,9 +395,11 @@ int bdrv_create(BlockDriver *drv, const char* filename,
9ae3a8
         .filename = g_strdup(filename),
9ae3a8
         .options = options,
9ae3a8
         .ret = NOT_DONE,
9ae3a8
+        .err = NULL,
9ae3a8
     };
9ae3a8
 
9ae3a8
     if (!drv->bdrv_create) {
9ae3a8
+        error_setg(errp, "Driver '%s' does not support image creation", drv->format_name);
9ae3a8
         ret = -ENOTSUP;
9ae3a8
         goto out;
9ae3a8
     }
9ae3a8
@@ -406,22 +416,37 @@ int bdrv_create(BlockDriver *drv, const char* filename,
9ae3a8
     }
9ae3a8
 
9ae3a8
     ret = cco.ret;
9ae3a8
+    if (ret < 0) {
9ae3a8
+        if (error_is_set(&cco.err)) {
9ae3a8
+            error_propagate(errp, cco.err);
9ae3a8
+        } else {
9ae3a8
+            error_setg_errno(errp, -ret, "Could not create image");
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
 
9ae3a8
 out:
9ae3a8
     g_free(cco.filename);
9ae3a8
     return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
-int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
9ae3a8
+int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
9ae3a8
+                     Error **errp)
9ae3a8
 {
9ae3a8
     BlockDriver *drv;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
+    int ret;
9ae3a8
 
9ae3a8
     drv = bdrv_find_protocol(filename, true);
9ae3a8
     if (drv == NULL) {
9ae3a8
+        error_setg(errp, "Could not find protocol for file '%s'", filename);
9ae3a8
         return -ENOENT;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    return bdrv_create(drv, filename, options);
9ae3a8
+    ret = bdrv_create(drv, filename, options, &local_err);
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        error_propagate(errp, local_err);
9ae3a8
+    }
9ae3a8
+    return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
 /*
9ae3a8
@@ -1055,11 +1080,14 @@ int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
9ae3a8
                 drv->format_name);
9ae3a8
         }
9ae3a8
 
9ae3a8
-        ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options);
9ae3a8
+        ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err);
9ae3a8
         free_option_parameters(create_options);
9ae3a8
         if (ret < 0) {
9ae3a8
             error_setg_errno(errp, -ret, "Could not create temporary overlay "
9ae3a8
-                             "'%s'", tmp_filename);
9ae3a8
+                             "'%s': %s", tmp_filename,
9ae3a8
+                             error_get_pretty(local_err));
9ae3a8
+            error_free(local_err);
9ae3a8
+            local_err = NULL;
9ae3a8
             goto fail;
9ae3a8
         }
9ae3a8
 
9ae3a8
@@ -4755,6 +4783,7 @@ void bdrv_img_create(const char *filename, const char *fmt,
9ae3a8
     BlockDriverState *bs = NULL;
9ae3a8
     BlockDriver *drv, *proto_drv;
9ae3a8
     BlockDriver *backing_drv = NULL;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
     int ret = 0;
9ae3a8
 
9ae3a8
     /* Find driver and parse its options */
9ae3a8
@@ -4841,10 +4870,13 @@ void bdrv_img_create(const char *filename, const char *fmt,
9ae3a8
             bs = bdrv_new("");
9ae3a8
 
9ae3a8
             ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
9ae3a8
-                            backing_drv, NULL);
9ae3a8
+                            backing_drv, &local_err);
9ae3a8
             if (ret < 0) {
9ae3a8
-                error_setg_errno(errp, -ret, "Could not open '%s'",
9ae3a8
-                                 backing_file->value.s);
9ae3a8
+                error_setg_errno(errp, -ret, "Could not open '%s': %s",
9ae3a8
+                                 backing_file->value.s,
9ae3a8
+                                 error_get_pretty(local_err));
9ae3a8
+                error_free(local_err);
9ae3a8
+                local_err = NULL;
9ae3a8
                 goto out;
9ae3a8
             }
9ae3a8
             bdrv_get_geometry(bs, &size);
9ae3a8
@@ -4863,22 +4895,19 @@ void bdrv_img_create(const char *filename, const char *fmt,
9ae3a8
         print_option_parameters(param);
9ae3a8
         puts("");
9ae3a8
     }
9ae3a8
-    ret = bdrv_create(drv, filename, param);
9ae3a8
-    if (ret < 0) {
9ae3a8
-        if (ret == -ENOTSUP) {
9ae3a8
-            error_setg(errp,"Formatting or formatting option not supported for "
9ae3a8
-                            "file format '%s'", fmt);
9ae3a8
-        } else if (ret == -EFBIG) {
9ae3a8
-            const char *cluster_size_hint = "";
9ae3a8
-            if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
9ae3a8
-                cluster_size_hint = " (try using a larger cluster size)";
9ae3a8
-            }
9ae3a8
-            error_setg(errp, "The image size is too large for file format '%s'%s",
9ae3a8
-                       fmt, cluster_size_hint);
9ae3a8
-        } else {
9ae3a8
-            error_setg(errp, "%s: error while creating %s: %s", filename, fmt,
9ae3a8
-                       strerror(-ret));
9ae3a8
+    ret = bdrv_create(drv, filename, param, &local_err);
9ae3a8
+    if (ret == -EFBIG) {
9ae3a8
+        /* This is generally a better message than whatever the driver would
9ae3a8
+         * deliver (especially because of the cluster_size_hint), since that
9ae3a8
+         * is most probably not much different from "image too large". */
9ae3a8
+        const char *cluster_size_hint = "";
9ae3a8
+        if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
9ae3a8
+            cluster_size_hint = " (try using a larger cluster size)";
9ae3a8
         }
9ae3a8
+        error_setg(errp, "The image size is too large for file format '%s'"
9ae3a8
+                   "%s", fmt, cluster_size_hint);
9ae3a8
+        error_free(local_err);
9ae3a8
+        local_err = NULL;
9ae3a8
     }
9ae3a8
 
9ae3a8
 out:
9ae3a8
@@ -4888,6 +4917,9 @@ out:
9ae3a8
     if (bs) {
9ae3a8
         bdrv_delete(bs);
9ae3a8
     }
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        error_propagate(errp, local_err);
9ae3a8
+    }
9ae3a8
 }
9ae3a8
 
9ae3a8
 AioContext *bdrv_get_aio_context(BlockDriverState *bs)
9ae3a8
diff --git a/block/cow.c b/block/cow.c
9ae3a8
index 8e00f8f..cd78129 100644
9ae3a8
--- a/block/cow.c
9ae3a8
+++ b/block/cow.c
9ae3a8
@@ -316,8 +316,10 @@ static int cow_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
         options++;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    ret = bdrv_create_file(filename, options);
9ae3a8
+    ret = bdrv_create_file(filename, options, &local_err);
9ae3a8
     if (ret < 0) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/block/qcow.c b/block/qcow.c
9ae3a8
index 41a578c..6d029cc 100644
9ae3a8
--- a/block/qcow.c
9ae3a8
+++ b/block/qcow.c
9ae3a8
@@ -684,8 +684,10 @@ static int qcow_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
         options++;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    ret = bdrv_create_file(filename, options);
9ae3a8
+    ret = bdrv_create_file(filename, options, &local_err);
9ae3a8
     if (ret < 0) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index a2fca7a..027d210 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -1364,7 +1364,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
     uint8_t* refcount_table;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
-    ret = bdrv_create_file(filename, options);
9ae3a8
+    ret = bdrv_create_file(filename, options, NULL);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
diff --git a/block/qed.c b/block/qed.c
9ae3a8
index fa35fe2..7b13bb5 100644
9ae3a8
--- a/block/qed.c
9ae3a8
+++ b/block/qed.c
9ae3a8
@@ -555,8 +555,10 @@ static int qed_create(const char *filename, uint32_t cluster_size,
9ae3a8
     int ret = 0;
9ae3a8
     BlockDriverState *bs = NULL;
9ae3a8
 
9ae3a8
-    ret = bdrv_create_file(filename, NULL);
9ae3a8
+    ret = bdrv_create_file(filename, NULL, &local_err);
9ae3a8
     if (ret < 0) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/block/raw.c b/block/raw.c
9ae3a8
index f92c04e..50073b6 100644
9ae3a8
--- a/block/raw.c
9ae3a8
+++ b/block/raw.c
9ae3a8
@@ -108,7 +108,15 @@ static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
9ae3a8
 static int raw_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
                       Error **errp)
9ae3a8
 {
9ae3a8
-    return bdrv_create_file(filename, options);
9ae3a8
+    Error *local_err = NULL;
9ae3a8
+    int ret;
9ae3a8
+
9ae3a8
+    ret = bdrv_create_file(filename, options, &local_err);
9ae3a8
+    if (error_is_set(&local_err)) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
+    }
9ae3a8
+    return ret;
9ae3a8
 }
9ae3a8
 
9ae3a8
 static QEMUOptionParameter raw_create_options[] = {
9ae3a8
diff --git a/block/vvfat.c b/block/vvfat.c
9ae3a8
index a385b6f..cb45687 100644
9ae3a8
--- a/block/vvfat.c
9ae3a8
+++ b/block/vvfat.c
9ae3a8
@@ -2928,8 +2928,10 @@ static int enable_write_target(BDRVVVFATState *s)
9ae3a8
     set_option_parameter_int(options, BLOCK_OPT_SIZE, s->sector_count * 512);
9ae3a8
     set_option_parameter(options, BLOCK_OPT_BACKING_FILE, "fat:");
9ae3a8
 
9ae3a8
-    ret = bdrv_create(bdrv_qcow, s->qcow_filename, options);
9ae3a8
+    ret = bdrv_create(bdrv_qcow, s->qcow_filename, options, &local_err);
9ae3a8
     if (ret < 0) {
9ae3a8
+        qerror_report_err(local_err);
9ae3a8
+        error_free(local_err);
9ae3a8
         goto err;
9ae3a8
     }
9ae3a8
 
9ae3a8
diff --git a/include/block/block.h b/include/block/block.h
9ae3a8
index 168d8a8..fac1282 100644
9ae3a8
--- a/include/block/block.h
9ae3a8
+++ b/include/block/block.h
9ae3a8
@@ -152,8 +152,9 @@ BlockDriver *bdrv_find_format(const char *format_name);
9ae3a8
 BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
9ae3a8
                                           bool readonly);
9ae3a8
 int bdrv_create(BlockDriver *drv, const char* filename,
9ae3a8
-    QEMUOptionParameter *options);
9ae3a8
-int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
9ae3a8
+    QEMUOptionParameter *options, Error **errp);
9ae3a8
+int bdrv_create_file(const char* filename, QEMUOptionParameter *options,
9ae3a8
+                     Error **errp);
9ae3a8
 BlockDriverState *bdrv_new(const char *device_name);
9ae3a8
 void bdrv_make_anon(BlockDriverState *bs);
9ae3a8
 void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old);
9ae3a8
diff --git a/qemu-img.c b/qemu-img.c
9ae3a8
index d7d1244..9fda8cf 100644
9ae3a8
--- a/qemu-img.c
9ae3a8
+++ b/qemu-img.c
9ae3a8
@@ -1136,6 +1136,7 @@ static int img_convert(int argc, char **argv)
9ae3a8
     float local_progress = 0;
9ae3a8
     int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
9ae3a8
     bool quiet = false;
9ae3a8
+    Error *local_err = NULL;
9ae3a8
 
9ae3a8
     fmt = NULL;
9ae3a8
     out_fmt = "raw";
9ae3a8
@@ -1338,18 +1339,11 @@ static int img_convert(int argc, char **argv)
9ae3a8
 
9ae3a8
     if (!skip_create) {
9ae3a8
         /* Create the new image */
9ae3a8
-        ret = bdrv_create(drv, out_filename, param);
9ae3a8
+        ret = bdrv_create(drv, out_filename, param, &local_err);
9ae3a8
         if (ret < 0) {
9ae3a8
-            if (ret == -ENOTSUP) {
9ae3a8
-                error_report("Formatting not supported for file format '%s'",
9ae3a8
-                             out_fmt);
9ae3a8
-            } else if (ret == -EFBIG) {
9ae3a8
-                error_report("The image size is too large for file format '%s'",
9ae3a8
-                             out_fmt);
9ae3a8
-            } else {
9ae3a8
-                error_report("%s: error while converting %s: %s",
9ae3a8
-                             out_filename, out_fmt, strerror(-ret));
9ae3a8
-            }
9ae3a8
+            error_report("%s: error while converting %s: %s",
9ae3a8
+                         out_filename, out_fmt, error_get_pretty(local_err));
9ae3a8
+            error_free(local_err);
9ae3a8
             goto out;
9ae3a8
         }
9ae3a8
     }
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8