From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56696) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOrdP-0007ju-HY for qemu-devel@nongnu.org; Tue, 02 Sep 2014 13:12:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XOrdF-0001Ni-I5 for qemu-devel@nongnu.org; Tue, 02 Sep 2014 13:12:47 -0400 Received: from e06smtp16.uk.ibm.com ([195.75.94.112]:35615) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XOrdF-0001La-84 for qemu-devel@nongnu.org; Tue, 02 Sep 2014 13:12:37 -0400 Received: from /spool/local by e06smtp16.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 2 Sep 2014 18:12:36 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (d06relay10.portsmouth.uk.ibm.com [9.149.109.195]) by d06dlp02.portsmouth.uk.ibm.com (Postfix) with ESMTP id CE0B02190046 for ; Tue, 2 Sep 2014 18:12:13 +0100 (BST) Received: from d06av07.portsmouth.uk.ibm.com (d06av07.portsmouth.uk.ibm.com [9.149.37.248]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id s82HCWsd26280062 for ; Tue, 2 Sep 2014 17:12:32 GMT Received: from d06av07.portsmouth.uk.ibm.com (localhost [127.0.0.1]) by d06av07.portsmouth.uk.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id s82HCW49008006 for ; Tue, 2 Sep 2014 13:12:32 -0400 Date: Tue, 2 Sep 2014 19:12:27 +0200 From: Greg Kurz Message-ID: <20140902191227.7f07b4f0@bahia.local> In-Reply-To: <1409568798-2292-1-git-send-email-junmuzi@gmail.com> References: <1409568798-2292-1-git-send-email-junmuzi@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2] qcow2: add update refcount table realization for update_refcount List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Jun Li Cc: kwolf@redhat.com, juli@redhat.com, famz@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com On Mon, 1 Sep 2014 18:52:48 +0800 Jun Li wrote: > When every item of refcount block is NULL, free refcount block and reset the > corresponding item of refcount table with NULL. > > Signed-off-by: Jun Li > --- > > The v2 do following change to modify some potential issue. > > +------- Here should start from "0". > | > for (k = 0; k < refcount_block_entries; k++) { > if (refcount_block[k] != cpu_to_be16(0)) { > ... | | > } | | > } | +---- Using "0" is more safe. > | > +-------- This should be "k" not "++k". > --- > block/qcow2-refcount.c | 31 +++++++++++++++++++++++++++++++ > 1 file changed, 31 insertions(+) > > diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c > index 43665b8..63f36e6 100644 > --- a/block/qcow2-refcount.c > +++ b/block/qcow2-refcount.c > @@ -586,6 +586,37 @@ 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); > } > + > + /* When refcount block is NULL, update refcount table */ > + if (block_index == 0) { > + int k = block_index; Hi, k = 0 is also done in the for block below... > + int refcount_block_entries = s->cluster_size / sizeof(uint16_t); It's better for maintainance to count elements in an array this way: int refcount_block_entries = s->cluster_size / sizeof(refcount_block[0]); > + for (k = 0; k < refcount_block_entries; k++) { > + if (refcount_block[k] != cpu_to_be16(0)) { > + break; > + } > + } > + > + if (k == refcount_block_entries) { > + qemu_vfree(refcount_block); > + /* update refcount table */ > + unsigned int refcount_table_index; > + uint64_t data64 = cpu_to_be64(0); > + refcount_table_index = cluster_index >> (s->cluster_bits - > + REFCOUNT_SHIFT); > + ret = bdrv_pwrite_sync(bs->file, > + s->refcount_table_offset + > + refcount_table_index * > + sizeof(uint64_t), > + &data64, sizeof(data64)); > + if (ret < 0) { > + goto fail; > + } > + > + s->refcount_table[refcount_table_index] = data64; > + > + } > + } > } > > ret = 0; Cheers. -- Gregory Kurz kurzgreg@fr.ibm.com gkurz@linux.vnet.ibm.com Software Engineer @ IBM/Meiosys http://www.ibm.com Tel +33 (0)562 165 496 "Anarchy is about taking complete responsibility for yourself." Alan Moore.