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