From patchwork Wed Jan 22 06:57:17 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 313137 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id C6A082C031A for ; Wed, 22 Jan 2014 18:00:47 +1100 (EST) Received: from localhost ([::1]:33932 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5rnp-0002tP-FN for incoming@patchwork.ozlabs.org; Wed, 22 Jan 2014 02:00:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52322) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5rn2-0002q1-Cy for qemu-devel@nongnu.org; Wed, 22 Jan 2014 02:00:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W5rmv-0001XP-LY for qemu-devel@nongnu.org; Wed, 22 Jan 2014 01:59:56 -0500 Received: from [222.73.24.84] (port=29979 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W5rmv-0001WK-A4 for qemu-devel@nongnu.org; Wed, 22 Jan 2014 01:59:49 -0500 X-IronPort-AV: E=Sophos;i="4.95,698,1384272000"; d="scan'208";a="9443141" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 22 Jan 2014 14:55:52 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id s0M6xOJt020658; Wed, 22 Jan 2014 14:59:30 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com ([10.167.226.102]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2014012214580129-1275357 ; Wed, 22 Jan 2014 14:58:01 +0800 From: Hu Tao To: qemu-devel@nongnu.org Date: Wed, 22 Jan 2014 14:57:17 +0800 Message-Id: <5582b551964ef5d2d4988277707010dd8c774676.1390373621.git.hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.8.5.2.229.g4448466 In-Reply-To: References: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/01/22 14:58:01, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2014/01/22 14:58:07, Serialize complete at 2014/01/22 14:58:07 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: Kevin Wolf , Max Reitz Subject: [Qemu-devel] [PATCH v3 3/4] qcow2: check for NULL l2meta 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 In case of do preallocating metadata with a large cluster size, qcow2_alloc_cluster_offset() can allocate nothing and returns a NULL l2meta. This patch checks for it and link2 l2 with only valid l2meta. Replace 9 and 512 with BDRV_SECTOR_BITS, BDRV_SECTOR_SIZE respectively while at the function. Reviewed-by: Max Reitz Signed-off-by: Hu Tao --- block/qcow2.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index a0596ec..e4e06f9 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1395,22 +1395,24 @@ static int preallocate(BlockDriverState *bs) int ret; QCowL2Meta *meta; - nb_sectors = bdrv_getlength(bs) >> 9; + nb_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; offset = 0; while (nb_sectors) { - num = MIN(nb_sectors, INT_MAX >> 9); + num = MIN(nb_sectors, INT_MAX >> BDRV_SECTOR_BITS); ret = qcow2_alloc_cluster_offset(bs, offset, &num, &host_offset, &meta); if (ret < 0) { return ret; } - ret = qcow2_alloc_cluster_link_l2(bs, meta); - if (ret < 0) { - qcow2_free_any_clusters(bs, meta->alloc_offset, meta->nb_clusters, - QCOW2_DISCARD_NEVER); - return ret; + if (meta) { + ret = qcow2_alloc_cluster_link_l2(bs, meta); + if (ret < 0) { + qcow2_free_any_clusters(bs, meta->alloc_offset, + meta->nb_clusters, QCOW2_DISCARD_NEVER); + return ret; + } } /* There are no dependent requests, but we need to remove our request @@ -1422,7 +1424,7 @@ static int preallocate(BlockDriverState *bs) /* TODO Preallocate data if requested */ nb_sectors -= num; - offset += num << 9; + offset += num << BDRV_SECTOR_BITS; } /* @@ -1431,9 +1433,10 @@ static int preallocate(BlockDriverState *bs) * EOF). Extend the image to the last allocated sector. */ if (host_offset != 0) { - uint8_t buf[512]; - memset(buf, 0, 512); - ret = bdrv_write(bs->file, (host_offset >> 9) + num - 1, buf, 1); + uint8_t buf[BDRV_SECTOR_SIZE]; + memset(buf, 0, BDRV_SECTOR_SIZE); + ret = bdrv_write(bs->file, (host_offset >> BDRV_SECTOR_BITS) + num - 1, + buf, 1); if (ret < 0) { return ret; }