From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ky89s-0005AP-Pn for qemu-devel@nongnu.org; Thu, 06 Nov 2008 11:56:36 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ky89r-0005A1-Vg for qemu-devel@nongnu.org; Thu, 06 Nov 2008 11:56:36 -0500 Received: from [199.232.76.173] (port=41034 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ky89r-00059y-Qb for qemu-devel@nongnu.org; Thu, 06 Nov 2008 11:56:35 -0500 Received: from ecfrec.frec.bull.fr ([129.183.4.8]:40432) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Ky89r-0000CU-HO for qemu-devel@nongnu.org; Thu, 06 Nov 2008 11:56:35 -0500 Received: from localhost (localhost [127.0.0.1]) by ecfrec.frec.bull.fr (Postfix) with ESMTP id DB22A1A18C5 for ; Thu, 6 Nov 2008 17:55:56 +0100 (CET) Received: from ecfrec.frec.bull.fr ([127.0.0.1]) by localhost (ecfrec.frec.bull.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 01282-10 for ; Thu, 6 Nov 2008 17:55:53 +0100 (CET) Received: from cyclope.frec.bull.fr (cyclope.frec.bull.fr [129.183.4.9]) by ecfrec.frec.bull.fr (Postfix) with ESMTP id A5D401A18B9 for ; Thu, 6 Nov 2008 17:55:53 +0100 (CET) From: Laurent Vivier References: <20081106165212.380421945@bull.net> Content-Type: text/plain; charset=utf-8 Date: Thu, 06 Nov 2008 17:55:57 +0100 Message-Id: <1225990557.6576.11.camel@frecb07144> Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount(). Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Laurent Vivier pi=C3=A8ce jointe document texte brut (0001-Clean-up-update_cluster_refcount.patch) Move some parts to alloc_refcount_block() and block cache checking to load_refcount_block(). Signed-off-by: Laurent Vivier --- block-qcow2.c | 71 +++++++++++++++++++++++++++++++++++----------------= ------- 1 file changed, 43 insertions(+), 28 deletions(-) Index: qemu/block-qcow2.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- qemu.orig/block-qcow2.c 2008-11-06 16:34:46.000000000 +0100 +++ qemu/block-qcow2.c 2008-11-06 16:40:27.000000000 +0100 @@ -2169,6 +2169,10 @@ static int load_refcount_block(BlockDriv { BDRVQcowState *s =3D bs->opaque; int ret; + + if (refcount_block_offset =3D=3D s->refcount_block_cache_offset) + return 0; + ret =3D bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_c= ache, s->cluster_size); if (ret !=3D s->cluster_size) @@ -2189,11 +2193,8 @@ static int get_refcount(BlockDriverState refcount_block_offset =3D s->refcount_table[refcount_table_index]; if (!refcount_block_offset) return 0; - if (refcount_block_offset !=3D s->refcount_block_cache_offset) { - /* better than nothing: return allocated if read error */ - if (load_refcount_block(bs, refcount_block_offset) < 0) - return 1; - } + if (load_refcount_block(bs, refcount_block_offset) < 0) + return 1; block_index =3D cluster_index & ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); return be16_to_cpu(s->refcount_block_cache[block_index]); @@ -2227,6 +2228,38 @@ static int64_t alloc_clusters_noref(Bloc } } =20 +/* create a new refcount block */ + +static int64_t alloc_refcount_block(BlockDriverState *bs, int refcount_t= able_index) +{ + BDRVQcowState *s =3D bs->opaque; + int64_t offset; + int ret; + uint64_t data64; + + /* Note: we cannot update the refcount now to avoid recursion */ + + offset =3D alloc_clusters_noref(bs, s->cluster_size); + memset(s->refcount_block_cache, 0, s->cluster_size); + + ret =3D bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->clust= er_size); + if (ret !=3D s->cluster_size) + return -1; + + s->refcount_table[refcount_table_index] =3D offset; + data64 =3D cpu_to_be64(offset); + ret =3D bdrv_pwrite(s->hd, s->refcount_table_offset + + refcount_table_index * sizeof(uint64_t), + &data64, sizeof(data64)); + if (ret !=3D sizeof(data64)) + return -1; + + s->refcount_block_cache_offset =3D offset; + update_refcount(bs, offset, s->cluster_size, 1); + + return offset; +} + static int64_t alloc_clusters(BlockDriverState *bs, int64_t size) { int64_t offset; @@ -2359,9 +2392,8 @@ static int update_cluster_refcount(Block int addend) { BDRVQcowState *s =3D bs->opaque; - int64_t offset, refcount_block_offset; + int64_t refcount_block_offset; int ret, refcount_table_index, block_index, refcount; - uint64_t data64; =20 refcount_table_index =3D cluster_index >> (s->cluster_bits - REFCOUN= T_SHIFT); if (refcount_table_index >=3D s->refcount_table_size) { @@ -2375,29 +2407,12 @@ static int update_cluster_refcount(Block if (!refcount_block_offset) { if (addend < 0) return -EINVAL; - /* create a new refcount block */ - /* Note: we cannot update the refcount now to avoid recursion */ - offset =3D alloc_clusters_noref(bs, s->cluster_size); - memset(s->refcount_block_cache, 0, s->cluster_size); - ret =3D bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->c= luster_size); - if (ret !=3D s->cluster_size) - return -EINVAL; - s->refcount_table[refcount_table_index] =3D offset; - data64 =3D cpu_to_be64(offset); - ret =3D bdrv_pwrite(s->hd, s->refcount_table_offset + - refcount_table_index * sizeof(uint64_t), - &data64, sizeof(data64)); - if (ret !=3D sizeof(data64)) + refcount_block_offset =3D alloc_refcount_block(bs, refcount_tabl= e_index); + if (refcount_block_offset < 0) return -EINVAL; - - refcount_block_offset =3D offset; - s->refcount_block_cache_offset =3D offset; - update_refcount(bs, offset, s->cluster_size, 1); } else { - if (refcount_block_offset !=3D s->refcount_block_cache_offset) { - if (load_refcount_block(bs, refcount_block_offset) < 0) - return -EIO; - } + if (load_refcount_block(bs, refcount_block_offset) < 0) + return -EIO; } /* we can update the count and save it */ block_index =3D cluster_index & --=20