From patchwork Thu Dec 15 10:14:00 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhi Hui Li X-Patchwork-Id: 131554 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E49731007D4 for ; Thu, 15 Dec 2011 21:14:31 +1100 (EST) Received: from localhost ([::1]:55235 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rb8KU-0007Q1-R8 for incoming@patchwork.ozlabs.org; Thu, 15 Dec 2011 05:14:22 -0500 Received: from eggs.gnu.org ([140.186.70.92]:51935) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rb8KP-0007Pw-RF for qemu-devel@nongnu.org; Thu, 15 Dec 2011 05:14:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Rb8KH-0003o8-NR for qemu-devel@nongnu.org; Thu, 15 Dec 2011 05:14:17 -0500 Received: from e28smtp04.in.ibm.com ([122.248.162.4]:35878) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Rb8KH-0003nh-3C for qemu-devel@nongnu.org; Thu, 15 Dec 2011 05:14:09 -0500 Received: from /spool/local by e28smtp04.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 15 Dec 2011 15:44:05 +0530 Received: from d28relay01.in.ibm.com (9.184.220.58) by e28smtp04.in.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 15 Dec 2011 15:44:03 +0530 Received: from d28av05.in.ibm.com (d28av05.in.ibm.com [9.184.220.67]) by d28relay01.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id pBFAE2Xt4755678 for ; Thu, 15 Dec 2011 15:44:03 +0530 Received: from d28av05.in.ibm.com (loopback [127.0.0.1]) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id pBFAE28A017020 for ; Thu, 15 Dec 2011 21:14:02 +1100 Received: from mm.cn.ibm.com (mm.cn.ibm.com [9.115.116.136] (may be forged)) by d28av05.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id pBFAE1sP016991; Thu, 15 Dec 2011 21:14:01 +1100 From: Li Zhi Hui To: qemu-devel@nongnu.org Date: Thu, 15 Dec 2011 18:14:00 +0800 Message-Id: <1323944040-7096-1-git-send-email-zhihuili@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.1 x-cbid: 11121510-5564-0000-0000-0000008461FA X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.4 Cc: kwolf@redhat.com, Li Zhi Hui , stefanha@linux.vnet.ibm.com Subject: [Qemu-devel] [PATCH] block/qcow.c : return real error code in qcow_open X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Li Zhi Hui --- 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)