qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 20/22] qcow: Return real error code in qcow_open
Date: Thu, 26 Jan 2012 17:38:14 +0100	[thread overview]
Message-ID: <1327595896-19623-21-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1327595896-19623-1-git-send-email-kwolf@redhat.com>

From: Li Zhi Hui <zhihuili@linux.vnet.ibm.com>

Signed-off-by: Li Zhi Hui <zhihuili@linux.vnet.ibm.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow.c |   56 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/block/qcow.c b/block/qcow.c
index b16955d..e0d0b88 100644
--- a/block/qcow.c
+++ b/block/qcow.c
@@ -95,11 +95,13 @@ static int qcow_probe(const uint8_t *buf, int buf_size, const char *filename)
 static int qcow_open(BlockDriverState *bs, int flags)
 {
     BDRVQcowState *s = bs->opaque;
-    int len, i, shift;
+    int len, i, shift, ret;
     QCowHeader header;
 
-    if (bdrv_pread(bs->file, 0, &header, sizeof(header)) != sizeof(header))
+    ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
+    if (ret < 0) {
         goto fail;
+    }
     be32_to_cpus(&header.magic);
     be32_to_cpus(&header.version);
     be64_to_cpus(&header.backing_file_offset);
@@ -109,15 +111,31 @@ static int qcow_open(BlockDriverState *bs, int flags)
     be32_to_cpus(&header.crypt_method);
     be64_to_cpus(&header.l1_table_offset);
 
-    if (header.magic != QCOW_MAGIC || header.version != QCOW_VERSION)
+    if (header.magic != QCOW_MAGIC) {
+        ret = -EINVAL;
         goto fail;
-    if (header.size <= 1 || header.cluster_bits < 9)
+    }
+    if (header.version != QCOW_VERSION) {
+        char version[64];
+        snprintf(version, sizeof(version), "QCOW version %d", header.version);
+        qerror_report(QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
+            bs->device_name, "qcow", version);
+        ret = -ENOTSUP;
         goto fail;
-    if (header.crypt_method > QCOW_CRYPT_AES)
+    }
+
+    if (header.size <= 1 || header.cluster_bits < 9) {
+        ret = -EINVAL;
         goto fail;
+    }
+    if (header.crypt_method > QCOW_CRYPT_AES) {
+        ret = -EINVAL;
+        goto fail;
+    }
     s->crypt_method_header = header.crypt_method;
-    if (s->crypt_method_header)
+    if (s->crypt_method_header) {
         bs->encrypted = 1;
+    }
     s->cluster_bits = header.cluster_bits;
     s->cluster_size = 1 << s->cluster_bits;
     s->cluster_sectors = 1 << (s->cluster_bits - 9);
@@ -132,33 +150,33 @@ static int qcow_open(BlockDriverState *bs, int flags)
 
     s->l1_table_offset = header.l1_table_offset;
     s->l1_table = g_malloc(s->l1_size * sizeof(uint64_t));
-    if (!s->l1_table)
-        goto fail;
-    if (bdrv_pread(bs->file, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) !=
-        s->l1_size * sizeof(uint64_t))
+
+    ret = bdrv_pread(bs->file, s->l1_table_offset, s->l1_table,
+               s->l1_size * sizeof(uint64_t));
+    if (ret < 0) {
         goto fail;
+    }
+
     for(i = 0;i < s->l1_size; i++) {
         be64_to_cpus(&s->l1_table[i]);
     }
     /* alloc L2 cache */
     s->l2_cache = g_malloc(s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
-    if (!s->l2_cache)
-        goto fail;
     s->cluster_cache = g_malloc(s->cluster_size);
-    if (!s->cluster_cache)
-        goto fail;
     s->cluster_data = g_malloc(s->cluster_size);
-    if (!s->cluster_data)
-        goto fail;
     s->cluster_cache_offset = -1;
 
     /* read the backing file name */
     if (header.backing_file_offset != 0) {
         len = header.backing_file_size;
-        if (len > 1023)
+        if (len > 1023) {
             len = 1023;
-        if (bdrv_pread(bs->file, header.backing_file_offset, bs->backing_file, len) != len)
+        }
+        ret = bdrv_pread(bs->file, header.backing_file_offset,
+                   bs->backing_file, len);
+        if (ret < 0) {
             goto fail;
+        }
         bs->backing_file[len] = '\0';
     }
 
@@ -176,7 +194,7 @@ static int qcow_open(BlockDriverState *bs, int flags)
     g_free(s->l2_cache);
     g_free(s->cluster_cache);
     g_free(s->cluster_data);
-    return -1;
+    return ret;
 }
 
 static int qcow_set_key(BlockDriverState *bs, const char *key)
-- 
1.7.6.5

  parent reply	other threads:[~2012-01-26 16:35 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26 16:37 [Qemu-devel] [PULL 00/22] Block patches Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 01/22] rbd: wire up snapshot removal and rollback functionality Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 02/22] block: replace unchecked strdup/malloc/calloc with glib Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 03/22] coroutine: add co_sleep_ns() coroutine sleep function Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 04/22] block: check bdrv_in_use() before blockdev operations Kevin Wolf
2012-01-26 16:37 ` [Qemu-devel] [PATCH 05/22] block: make copy-on-read a per-request flag Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 06/22] block: add BlockJob interface for long-running operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 07/22] block: add image streaming block job Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 08/22] block: rate-limit streaming operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 09/22] qmp: add block_stream command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 10/22] qmp: add block_job_set_speed command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 11/22] qmp: add block_job_cancel command Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 12/22] qmp: add query-block-jobs Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 13/22] blockdev: make image streaming safe across hotplug Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 14/22] block: add bdrv_find_backing_image Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 15/22] add QERR_BASE_NOT_FOUND Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 16/22] block: add support for partial streaming Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 17/22] docs: describe live block operations Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 18/22] virtio-blk: add virtio_blk_handle_read trace event Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 19/22] block/vdi: Zero unused parts when allocating a new block (fix #919242) Kevin Wolf
2012-01-26 16:38 ` Kevin Wolf [this message]
2012-01-26 16:38 ` [Qemu-devel] [PATCH 21/22] qcow: Use bdrv functions to replace file operation Kevin Wolf
2012-01-26 16:38 ` [Qemu-devel] [PATCH 22/22] scsi: Guard against buflen exceeding req->cmd.xfer in scsi_disk_emulate_command Kevin Wolf
2012-01-27 17:33 ` [Qemu-devel] [PULL 00/22] Block patches Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1327595896-19623-21-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).