218e99
From 57c326f0b2220c9bbe1968f3cc251ae2f99db8ae Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Wed, 6 Nov 2013 16:53:40 +0100
218e99
Subject: [PATCH 83/87] vmdk: convert error code to use errp
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383756824-6921-18-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55572
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH v2 17/21] vmdk: convert error code to use errp
218e99
Bugzilla: 980771
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
218e99
218e99
From: Fam Zheng <famz@redhat.com>
218e99
218e99
BZ: 980771
218e99
218e99
Convert "fprintf(stderr,..." and standardize error messages:
218e99
218e99
Remove a few local_error's and use errp.
218e99
218e99
Remove "VMDK:" or "Vmdk:" prefixes in error message and fix to upper
218e99
case.
218e99
218e99
Signed-off-by: Fam Zheng <famz@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit 4823970bcb882cd5b7e9c9a21fa6573190035050)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
218e99
Conflicts:
218e99
	block/vmdk.c
218e99
218e99
Conflicts because the series "Implement reference count for
218e99
BlockDriverState" has not yet been backported which "replaces"
218e99
bdrv_delete by bdrv_unref.
218e99
---
218e99
 block/vmdk.c               | 116 ++++++++++++++++++++++++---------------------
218e99
 tests/qemu-iotests/059.out |   6 +--
218e99
 2 files changed, 63 insertions(+), 59 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block/vmdk.c               |  116 +++++++++++++++++++++++---------------------
218e99
 tests/qemu-iotests/059.out |    6 +--
218e99
 2 files changed, 63 insertions(+), 59 deletions(-)
218e99
218e99
diff --git a/block/vmdk.c b/block/vmdk.c
218e99
index bd1dd62..96f9415 100644
218e99
--- a/block/vmdk.c
218e99
+++ b/block/vmdk.c
218e99
@@ -331,8 +331,7 @@ static int vmdk_reopen_prepare(BDRVReopenState *state,
218e99
     assert(state->bs != NULL);
218e99
 
218e99
     if (queue == NULL) {
218e99
-        error_set(errp, ERROR_CLASS_GENERIC_ERROR,
218e99
-                 "No reopen queue for VMDK extents");
218e99
+        error_setg(errp, "No reopen queue for VMDK extents");
218e99
         goto exit;
218e99
     }
218e99
 
218e99
@@ -391,22 +390,23 @@ static int vmdk_add_extent(BlockDriverState *bs,
218e99
                            int64_t l1_offset, int64_t l1_backup_offset,
218e99
                            uint32_t l1_size,
218e99
                            int l2_size, uint64_t cluster_sectors,
218e99
-                           VmdkExtent **new_extent)
218e99
+                           VmdkExtent **new_extent,
218e99
+                           Error **errp)
218e99
 {
218e99
     VmdkExtent *extent;
218e99
     BDRVVmdkState *s = bs->opaque;
218e99
 
218e99
     if (cluster_sectors > 0x200000) {
218e99
         /* 0x200000 * 512Bytes = 1GB for one cluster is unrealistic */
218e99
-        error_report("invalid granularity, image may be corrupt");
218e99
-        return -EINVAL;
218e99
+        error_setg(errp, "Invalid granularity, image may be corrupt");
218e99
+        return -EFBIG;
218e99
     }
218e99
     if (l1_size > 512 * 1024 * 1024) {
218e99
         /* Although with big capacity and small l1_entry_sectors, we can get a
218e99
          * big l1_size, we don't want unbounded value to allocate the table.
218e99
          * Limit it to 512M, which is 16PB for default cluster and L2 table
218e99
          * size */
218e99
-        error_report("L1 size too big");
218e99
+        error_setg(errp, "L1 size too big");
218e99
         return -EFBIG;
218e99
     }
218e99
 
218e99
@@ -438,7 +438,8 @@ static int vmdk_add_extent(BlockDriverState *bs,
218e99
     return 0;
218e99
 }
218e99
 
218e99
-static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
218e99
+static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent,
218e99
+                            Error **errp)
218e99
 {
218e99
     int ret;
218e99
     int l1_size, i;
218e99
@@ -447,10 +448,13 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
218e99
     l1_size = extent->l1_size * sizeof(uint32_t);
218e99
     extent->l1_table = g_malloc(l1_size);
218e99
     ret = bdrv_pread(extent->file,
218e99
-                    extent->l1_table_offset,
218e99
-                    extent->l1_table,
218e99
-                    l1_size);
218e99
+                     extent->l1_table_offset,
218e99
+                     extent->l1_table,
218e99
+                     l1_size);
218e99
     if (ret < 0) {
218e99
+        error_setg_errno(errp, -ret,
218e99
+                         "Could not read l1 table from extent '%s'",
218e99
+                         extent->file->filename);
218e99
         goto fail_l1;
218e99
     }
218e99
     for (i = 0; i < extent->l1_size; i++) {
218e99
@@ -460,10 +464,13 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
218e99
     if (extent->l1_backup_table_offset) {
218e99
         extent->l1_backup_table = g_malloc(l1_size);
218e99
         ret = bdrv_pread(extent->file,
218e99
-                        extent->l1_backup_table_offset,
218e99
-                        extent->l1_backup_table,
218e99
-                        l1_size);
218e99
+                         extent->l1_backup_table_offset,
218e99
+                         extent->l1_backup_table,
218e99
+                         l1_size);
218e99
         if (ret < 0) {
218e99
+            error_setg_errno(errp, -ret,
218e99
+                             "Could not read l1 backup table from extent '%s'",
218e99
+                             extent->file->filename);
218e99
             goto fail_l1b;
218e99
         }
218e99
         for (i = 0; i < extent->l1_size; i++) {
218e99
@@ -483,7 +490,7 @@ static int vmdk_init_tables(BlockDriverState *bs, VmdkExtent *extent)
218e99
 
218e99
 static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
218e99
                                  BlockDriverState *file,
218e99
-                                 int flags)
218e99
+                                 int flags, Error **errp)
218e99
 {
218e99
     int ret;
218e99
     uint32_t magic;
218e99
@@ -492,6 +499,9 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
218e99
 
218e99
     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
218e99
     if (ret < 0) {
218e99
+        error_setg_errno(errp, -ret,
218e99
+                         "Could not read header from file '%s'",
218e99
+                         file->filename);
218e99
         return ret;
218e99
     }
218e99
     ret = vmdk_add_extent(bs, file, false,
218e99
@@ -501,11 +511,12 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
218e99
                           le32_to_cpu(header.l1dir_size),
218e99
                           4096,
218e99
                           le32_to_cpu(header.granularity),
218e99
-                          &extent);
218e99
+                          &extent,
218e99
+                          errp);
218e99
     if (ret < 0) {
218e99
         return ret;
218e99
     }
218e99
-    ret = vmdk_init_tables(bs, extent);
218e99
+    ret = vmdk_init_tables(bs, extent, errp);
218e99
     if (ret) {
218e99
         /* free extent allocated by vmdk_add_extent */
218e99
         vmdk_free_last_extent(bs);
218e99
@@ -514,11 +525,11 @@ static int vmdk_open_vmfs_sparse(BlockDriverState *bs,
218e99
 }
218e99
 
218e99
 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
218e99
-                               uint64_t desc_offset);
218e99
+                               uint64_t desc_offset, Error **errp);
218e99
 
218e99
 static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
                            BlockDriverState *file,
218e99
-                           int flags)
218e99
+                           int flags, Error **errp)
218e99
 {
218e99
     int ret;
218e99
     uint32_t magic;
218e99
@@ -529,12 +540,14 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
 
218e99
     ret = bdrv_pread(file, sizeof(magic), &header, sizeof(header));
218e99
     if (ret < 0) {
218e99
-        return ret;
218e99
+        error_setg_errno(errp, -ret,
218e99
+                         "Could not read header from file '%s'",
218e99
+                         file->filename);
218e99
     }
218e99
     if (header.capacity == 0) {
218e99
         uint64_t desc_offset = le64_to_cpu(header.desc_offset);
218e99
         if (desc_offset) {
218e99
-            return vmdk_open_desc_file(bs, flags, desc_offset << 9);
218e99
+            return vmdk_open_desc_file(bs, flags, desc_offset << 9, errp);
218e99
         }
218e99
     }
218e99
 
218e99
@@ -616,7 +629,8 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
                           l1_size,
218e99
                           le32_to_cpu(header.num_gtes_per_gt),
218e99
                           le64_to_cpu(header.granularity),
218e99
-                          &extent);
218e99
+                          &extent,
218e99
+                          errp);
218e99
     if (ret < 0) {
218e99
         return ret;
218e99
     }
218e99
@@ -625,7 +639,7 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
218e99
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
218e99
     extent->version = le32_to_cpu(header.version);
218e99
     extent->has_zero_grain = le32_to_cpu(header.flags) & VMDK4_FLAG_ZERO_GRAIN;
218e99
-    ret = vmdk_init_tables(bs, extent);
218e99
+    ret = vmdk_init_tables(bs, extent, errp);
218e99
     if (ret) {
218e99
         /* free extent allocated by vmdk_add_extent */
218e99
         vmdk_free_last_extent(bs);
218e99
@@ -663,7 +677,7 @@ static int vmdk_parse_description(const char *desc, const char *opt_name,
218e99
 /* Open an extent file and append to bs array */
218e99
 static int vmdk_open_sparse(BlockDriverState *bs,
218e99
                             BlockDriverState *file,
218e99
-                            int flags)
218e99
+                            int flags, Error **errp)
218e99
 {
218e99
     uint32_t magic;
218e99
 
218e99
@@ -674,10 +688,10 @@ static int vmdk_open_sparse(BlockDriverState *bs,
218e99
     magic = be32_to_cpu(magic);
218e99
     switch (magic) {
218e99
         case VMDK3_MAGIC:
218e99
-            return vmdk_open_vmfs_sparse(bs, file, flags);
218e99
+            return vmdk_open_vmfs_sparse(bs, file, flags, errp);
218e99
             break;
218e99
         case VMDK4_MAGIC:
218e99
-            return vmdk_open_vmdk4(bs, file, flags);
218e99
+            return vmdk_open_vmdk4(bs, file, flags, errp);
218e99
             break;
218e99
         default:
218e99
             return -EMEDIUMTYPE;
218e99
@@ -686,7 +700,7 @@ static int vmdk_open_sparse(BlockDriverState *bs,
218e99
 }
218e99
 
218e99
 static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
-        const char *desc_file_path)
218e99
+                              const char *desc_file_path, Error **errp)
218e99
 {
218e99
     int ret;
218e99
     char access[11];
218e99
@@ -697,7 +711,6 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
     int64_t flat_offset;
218e99
     char extent_path[PATH_MAX];
218e99
     BlockDriverState *extent_file;
218e99
-    Error *local_err = NULL;
218e99
 
218e99
     while (*p) {
218e99
         /* parse extent line:
218e99
@@ -712,9 +725,11 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
             goto next_line;
218e99
         } else if (!strcmp(type, "FLAT")) {
218e99
             if (ret != 5 || flat_offset < 0) {
218e99
+                error_setg(errp, "Invalid extent lines: \n%s", p);
218e99
                 return -EINVAL;
218e99
             }
218e99
         } else if (ret != 4) {
218e99
+            error_setg(errp, "Invalid extent lines: \n%s", p);
218e99
             return -EINVAL;
218e99
         }
218e99
 
218e99
@@ -728,10 +743,8 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
         path_combine(extent_path, sizeof(extent_path),
218e99
                 desc_file_path, fname);
218e99
         ret = bdrv_file_open(&extent_file, extent_path, NULL, bs->open_flags,
218e99
-                             &local_err);
218e99
+                             errp);
218e99
         if (ret) {
218e99
-            qerror_report_err(local_err);
218e99
-            error_free(local_err);
218e99
             return ret;
218e99
         }
218e99
 
218e99
@@ -741,21 +754,20 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
218e99
             VmdkExtent *extent;
218e99
 
218e99
             ret = vmdk_add_extent(bs, extent_file, true, sectors,
218e99
-                            0, 0, 0, 0, 0, &extent);
218e99
+                            0, 0, 0, 0, 0, &extent, errp);
218e99
             if (ret < 0) {
218e99
                 return ret;
218e99
             }
218e99
             extent->flat_start_offset = flat_offset << 9;
218e99
         } else if (!strcmp(type, "SPARSE") || !strcmp(type, "VMFSSPARSE")) {
218e99
             /* SPARSE extent and VMFSSPARSE extent are both "COWD" sparse file*/
218e99
-            ret = vmdk_open_sparse(bs, extent_file, bs->open_flags);
218e99
+            ret = vmdk_open_sparse(bs, extent_file, bs->open_flags, errp);
218e99
             if (ret) {
218e99
                 bdrv_delete(extent_file);
218e99
                 return ret;
218e99
             }
218e99
         } else {
218e99
-            fprintf(stderr,
218e99
-                "VMDK: Not supported extent type \"%s\""".\n", type);
218e99
+            error_setg(errp, "Unsupported extent type '%s'", type);
218e99
             return -ENOTSUP;
218e99
         }
218e99
 next_line:
218e99
@@ -769,7 +781,7 @@ next_line:
218e99
 }
218e99
 
218e99
 static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
218e99
-                               uint64_t desc_offset)
218e99
+                               uint64_t desc_offset, Error **errp)
218e99
 {
218e99
     int ret;
218e99
     char *buf = NULL;
218e99
@@ -798,13 +810,12 @@ static int vmdk_open_desc_file(BlockDriverState *bs, int flags,
218e99
         strcmp(ct, "vmfsSparse") &&
218e99
         strcmp(ct, "twoGbMaxExtentSparse") &&
218e99
         strcmp(ct, "twoGbMaxExtentFlat")) {
218e99
-        fprintf(stderr,
218e99
-                "VMDK: Not supported image type \"%s\""".\n", ct);
218e99
+        error_setg(errp, "Unsupported image type '%s'", ct);
218e99
         ret = -ENOTSUP;
218e99
         goto exit;
218e99
     }
218e99
     s->desc_offset = 0;
218e99
-    ret = vmdk_parse_extents(buf, bs, bs->file->filename);
218e99
+    ret = vmdk_parse_extents(buf, bs, bs->file->filename, errp);
218e99
 exit:
218e99
     g_free(buf);
218e99
     return ret;
218e99
@@ -816,10 +827,10 @@ static int vmdk_open(BlockDriverState *bs, QDict *options, int flags,
218e99
     int ret;
218e99
     BDRVVmdkState *s = bs->opaque;
218e99
 
218e99
-    if (vmdk_open_sparse(bs, bs->file, flags) == 0) {
218e99
+    if (vmdk_open_sparse(bs, bs->file, flags, errp) == 0) {
218e99
         s->desc_offset = 0x200;
218e99
     } else {
218e99
-        ret = vmdk_open_desc_file(bs, flags, 0);
218e99
+        ret = vmdk_open_desc_file(bs, flags, 0, errp);
218e99
         if (ret) {
218e99
             goto fail;
218e99
         }
218e99
@@ -1286,8 +1297,7 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
218e99
     VmdkMetaData m_data;
218e99
 
218e99
     if (sector_num > bs->total_sectors) {
218e99
-        fprintf(stderr,
218e99
-                "(VMDK) Wrong offset: sector_num=0x%" PRIx64
218e99
+        error_report("Wrong offset: sector_num=0x%" PRIx64
218e99
                 " total_sectors=0x%" PRIx64 "\n",
218e99
                 sector_num, bs->total_sectors);
218e99
         return -EIO;
218e99
@@ -1307,9 +1317,8 @@ static int vmdk_write(BlockDriverState *bs, int64_t sector_num,
218e99
         if (extent->compressed) {
218e99
             if (ret == VMDK_OK) {
218e99
                 /* Refuse write to allocated cluster for streamOptimized */
218e99
-                fprintf(stderr,
218e99
-                        "VMDK: can't write to allocated cluster"
218e99
-                        " for streamOptimized\n");
218e99
+                error_report("Could not write to allocated cluster"
218e99
+                              " for streamOptimized");
218e99
                 return -EIO;
218e99
             } else {
218e99
                 /* allocate */
218e99
@@ -1517,12 +1526,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
218e99
 }
218e99
 
218e99
 static int filename_decompose(const char *filename, char *path, char *prefix,
218e99
-        char *postfix, size_t buf_len)
218e99
+                              char *postfix, size_t buf_len, Error **errp)
218e99
 {
218e99
     const char *p, *q;
218e99
 
218e99
     if (filename == NULL || !strlen(filename)) {
218e99
-        fprintf(stderr, "Vmdk: no filename provided.\n");
218e99
+        error_setg(errp, "No filename provided");
218e99
         return VMDK_ERROR;
218e99
     }
218e99
     p = strrchr(filename, '/');
218e99
@@ -1595,9 +1604,8 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
         "ddb.geometry.heads = \"%d\"\n"
218e99
         "ddb.geometry.sectors = \"63\"\n"
218e99
         "ddb.adapterType = \"%s\"\n";
218e99
-    Error *local_err = NULL;
218e99
 
218e99
-    if (filename_decompose(filename, path, prefix, postfix, PATH_MAX)) {
218e99
+    if (filename_decompose(filename, path, prefix, postfix, PATH_MAX, errp)) {
218e99
         return -EINVAL;
218e99
     }
218e99
     /* Read out options */
218e99
@@ -1623,7 +1631,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
                strcmp(adapter_type, "buslogic") &&
218e99
                strcmp(adapter_type, "lsilogic") &&
218e99
                strcmp(adapter_type, "legacyESX")) {
218e99
-        fprintf(stderr, "VMDK: Unknown adapter type: '%s'.\n", adapter_type);
218e99
+        error_setg(errp, "Unknown adapter type: '%s'", adapter_type);
218e99
         return -EINVAL;
218e99
     }
218e99
     if (strcmp(adapter_type, "ide") != 0) {
218e99
@@ -1639,7 +1647,7 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
                strcmp(fmt, "twoGbMaxExtentSparse") &&
218e99
                strcmp(fmt, "twoGbMaxExtentFlat") &&
218e99
                strcmp(fmt, "streamOptimized")) {
218e99
-        fprintf(stderr, "VMDK: Unknown subformat: %s\n", fmt);
218e99
+        error_setg(errp, "Unknown subformat: '%s'", fmt);
218e99
         return -EINVAL;
218e99
     }
218e99
     split = !(strcmp(fmt, "twoGbMaxExtentFlat") &&
218e99
@@ -1653,15 +1661,13 @@ static int vmdk_create(const char *filename, QEMUOptionParameter *options,
218e99
         desc_extent_line = "RW %lld SPARSE \"%s\"\n";
218e99
     }
218e99
     if (flat && backing_file) {
218e99
-        /* not supporting backing file for flat image */
218e99
+        error_setg(errp, "Flat image can't have backing file");
218e99
         return -ENOTSUP;
218e99
     }
218e99
     if (backing_file) {
218e99
         BlockDriverState *bs = bdrv_new("");
218e99
-        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, &local_err);
218e99
+        ret = bdrv_open(bs, backing_file, NULL, 0, NULL, errp);
218e99
         if (ret != 0) {
218e99
-            qerror_report_err(local_err);
218e99
-            error_free(local_err);
218e99
             bdrv_delete(bs);
218e99
             return ret;
218e99
         }
218e99
diff --git a/tests/qemu-iotests/059.out b/tests/qemu-iotests/059.out
218e99
index 9159dbe..dc4f024 100644
218e99
--- a/tests/qemu-iotests/059.out
218e99
+++ b/tests/qemu-iotests/059.out
218e99
@@ -2,8 +2,7 @@ QA output created by 059
218e99
 === Testing invalid granularity ===
218e99
 
218e99
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
218e99
-invalid granularity, image may be corrupt
218e99
-qemu-io: can't open device TEST_DIR/t.vmdk: Could not open 'TEST_DIR/t.vmdk': Wrong medium type
218e99
+qemu-io: can't open device TEST_DIR/t.vmdk: Invalid granularity, image may be corrupt
218e99
 no file open, try 'help open'
218e99
 === Testing too big L2 table size ===
218e99
 
218e99
@@ -14,8 +13,7 @@ no file open, try 'help open'
218e99
 === Testing too big L1 table size ===
218e99
 
218e99
 Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864
218e99
-L1 size too big
218e99
-qemu-io: can't open device TEST_DIR/t.vmdk: Could not open 'TEST_DIR/t.vmdk': Wrong medium type
218e99
+qemu-io: can't open device TEST_DIR/t.vmdk: L1 size too big
218e99
 no file open, try 'help open'
218e99
 === Testing monolithicFlat creation and opening ===
218e99
 
218e99
-- 
218e99
1.7.1
218e99