From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38578) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xe6IZ-0007U2-Gx for qemu-devel@nongnu.org; Tue, 14 Oct 2014 13:54:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Xe6IQ-0008AA-FZ for qemu-devel@nongnu.org; Tue, 14 Oct 2014 13:54:15 -0400 Received: from mail-pd0-x22e.google.com ([2607:f8b0:400e:c02::22e]:50843) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xe6IQ-0008A2-84 for qemu-devel@nongnu.org; Tue, 14 Oct 2014 13:54:06 -0400 Received: by mail-pd0-f174.google.com with SMTP id y13so7932413pdi.33 for ; Tue, 14 Oct 2014 10:54:05 -0700 (PDT) From: Jun Li Date: Wed, 15 Oct 2014 01:53:36 +0800 Message-Id: <1413309216-18294-3-git-send-email-junmuzi@gmail.com> In-Reply-To: <1413309216-18294-1-git-send-email-junmuzi@gmail.com> References: <1413309216-18294-1-git-send-email-junmuzi@gmail.com> Subject: [Qemu-devel] [PATCH v4 2/2] qcow2: add update refcount table realization for update_refcount List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: kwolf@redhat.com, stefanha@redhat.com, famz@redhat.com, juli@redhat.com Cc: Jun Li , qemu-devel@nongnu.org When every item of refcount block is NULL, free refcount block and reset the corresponding item of refcount table with NULL. At the same time, put this cluster to s->free_cluster_index. Signed-off-by: Jun Li --- v4: Do not change anything for this commit id. v3: Add handle self-describing refcount blocks. --- block/qcow2-refcount.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 2bcaaf9..b603d4e 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -28,6 +28,7 @@ #include "qemu/range.h" static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size); +static int write_reftable_entry(BlockDriverState *bs, int rt_index); static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, int64_t offset, int64_t length, int addend, enum qcow2_discard_type type); @@ -599,6 +600,49 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs, if (refcount == 0 && s->discard_passthrough[type]) { update_refcount_discard(bs, cluster_offset, s->cluster_size); } + + unsigned int refcount_table_index; + refcount_table_index = cluster_index >> (s->cluster_bits - + REFCOUNT_SHIFT); + + uint64_t refcount_block_offset = + s->refcount_table[refcount_table_index] & REFT_OFFSET_MASK; + + int64_t self_block_index = refcount_block_offset >> s->cluster_bits; + int self_block_index2 = (refcount_block_offset >> s->cluster_bits) & + ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1); + + /* When refcount block is NULL, update refcount table */ + if (!be16_to_cpu(refcount_block[0]) || self_block_index2 == 0) { + int k; + int refcount_block_entries = s->cluster_size / + sizeof(refcount_block[0]); + for (k = 0; k < refcount_block_entries; k++) { + if (k == self_block_index2) { + k++; + } + + if (be16_to_cpu(refcount_block[k])) { + break; + } + } + + if (k == refcount_block_entries) { + if (self_block_index < s->free_cluster_index) { + s->free_cluster_index = self_block_index; + } + + /* update refcount table */ + s->refcount_table[refcount_table_index] = 0; + ret = write_reftable_entry(bs, refcount_table_index); + if (ret < 0) { + fprintf(stderr, "Could not update refcount table: %s\n", + strerror(-ret)); + goto fail; + } + + } + } } ret = 0; @@ -1184,7 +1228,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, case QCOW2_CLUSTER_NORMAL: { - uint64_t offset = l2_entry & L2E_OFFSET_MASK; + uint64_t offset = (uint64_t)(l2_entry & L2E_OFFSET_MASK); if (flags & CHECK_FRAG_INFO) { res->bfi.allocated_clusters++; -- 1.9.3