From patchwork Sat Dec 15 14:09:33 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Weil X-Patchwork-Id: 206622 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id C45152C00D8 for ; Sun, 16 Dec 2012 01:30:26 +1100 (EST) Received: from localhost ([::1]:57402 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TjsRc-0000uV-LR for incoming@patchwork.ozlabs.org; Sat, 15 Dec 2012 09:10:24 -0500 Received: from eggs.gnu.org ([208.118.235.92]:59440) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TjsRE-0000Gy-TT for qemu-devel@nongnu.org; Sat, 15 Dec 2012 09:10:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TjsRC-0003QV-1l for qemu-devel@nongnu.org; Sat, 15 Dec 2012 09:10:00 -0500 Received: from v220110690675601.yourvserver.net ([78.47.199.172]:44121) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TjsRB-0003QQ-Nf for qemu-devel@nongnu.org; Sat, 15 Dec 2012 09:09:57 -0500 Received: from localhost (v220110690675601.yourvserver.net.local [127.0.0.1]) by v220110690675601.yourvserver.net (Postfix) with ESMTP id E06C7728004A; Sat, 15 Dec 2012 15:09:56 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at weilnetz.de Received: from v220110690675601.yourvserver.net ([127.0.0.1]) by localhost (v220110690675601.yourvserver.net [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eeYMSYrOJSs5; Sat, 15 Dec 2012 15:09:56 +0100 (CET) Received: by v220110690675601.yourvserver.net (Postfix, from userid 1000) id 4D813728004B; Sat, 15 Dec 2012 15:09:56 +0100 (CET) From: Stefan Weil To: Stefan Hajnoczi , Kevin Wolf Date: Sat, 15 Dec 2012 15:09:33 +0100 Message-Id: <1355580573-19323-5-git-send-email-sw@weilnetz.de> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1355580573-19323-1-git-send-email-sw@weilnetz.de> References: <1355580573-19323-1-git-send-email-sw@weilnetz.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 78.47.199.172 Cc: Stefan Weil , qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 4/4] block/vdi: Improved return values from vdi_open and other small fixes 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 vdi_open returned -1 in case of any error, but it should return an error code (negative value of errno or BDRV_WRONG_FORMAT). vdi_open did not check for a bad signature. This check was only in vdi_probe. The signature is a 32 bit value and needs up to 8 hex digits for printing. Signed-off-by: Stefan Weil --- block/vdi.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/block/vdi.c b/block/vdi.c index c8330b7..7c7699f 100644 --- a/block/vdi.c +++ b/block/vdi.c @@ -246,7 +246,7 @@ static void vdi_header_print(VdiHeader *header) { char uuid[37]; logout("text %s", header->text); - logout("signature 0x%04x\n", header->signature); + logout("signature 0x%08x\n", header->signature); logout("header size 0x%04x\n", header->header_size); logout("image type 0x%04x\n", header->image_type); logout("image flags 0x%04x\n", header->image_flags); @@ -369,10 +369,12 @@ static int vdi_open(BlockDriverState *bs, int flags) BDRVVdiState *s = bs->opaque; VdiHeader header; size_t bmap_size; + int ret; logout("\n"); - if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) { + ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1); + if (ret < 0) { goto fail; } @@ -390,33 +392,45 @@ static int vdi_open(BlockDriverState *bs, int flags) header.disk_size &= ~(SECTOR_SIZE - 1); } - if (header.version != VDI_VERSION_1_1) { + if (header.signature != VDI_SIGNATURE) { + logout("bad vdi signature %08x\n", header.signature); + ret = BDRV_WRONG_FORMAT; + goto fail; + } else if (header.version != VDI_VERSION_1_1) { logout("unsupported version %u.%u\n", header.version >> 16, header.version & 0xffff); + ret = -ENOTSUP; goto fail; } else if (header.offset_bmap % SECTOR_SIZE != 0) { /* We only support block maps which start on a sector boundary. */ logout("unsupported block map offset 0x%x B\n", header.offset_bmap); + ret = -ENOTSUP; goto fail; } else if (header.offset_data % SECTOR_SIZE != 0) { /* We only support data blocks which start on a sector boundary. */ logout("unsupported data offset 0x%x B\n", header.offset_data); + ret = -ENOTSUP; goto fail; } else if (header.sector_size != SECTOR_SIZE) { logout("unsupported sector size %u B\n", header.sector_size); + ret = -ENOTSUP; goto fail; } else if (header.block_size != 1 * MiB) { logout("unsupported block size %u B\n", header.block_size); + ret = -ENOTSUP; goto fail; } else if (header.disk_size > (uint64_t)header.blocks_in_image * header.block_size) { logout("unsupported disk size %" PRIu64 " B\n", header.disk_size); + ret = -ENOTSUP; goto fail; } else if (!uuid_is_null(header.uuid_link)) { logout("link uuid != 0, unsupported\n"); + ret = -ENOTSUP; goto fail; } else if (!uuid_is_null(header.uuid_parent)) { logout("parent uuid != 0, unsupported\n"); + ret = -ENOTSUP; goto fail; } @@ -432,7 +446,8 @@ static int vdi_open(BlockDriverState *bs, int flags) if (bmap_size > 0) { s->bmap = g_malloc(bmap_size * SECTOR_SIZE); } - if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) { + ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size); + if (ret < 0) { goto fail_free_bmap; } @@ -448,7 +463,7 @@ static int vdi_open(BlockDriverState *bs, int flags) g_free(s->bmap); fail: - return -1; + return ret; } static int vdi_reopen_prepare(BDRVReopenState *state,