Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KSWns-0004Fl-Vp for qemu-devel@nongnu.org; Mon, 11 Aug 2008 08:47:17 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KSWns-0004FY-5s for qemu-devel@nongnu.org; Mon, 11 Aug 2008 08:47:16 -0400 Received: from [199.232.76.173] (port=47753 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KSWnr-0004FT-PA for qemu-devel@nongnu.org; Mon, 11 Aug 2008 08:47:15 -0400 Received: from ecfrec.frec.bull.fr ([129.183.4.8]:44099) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KSWnr-0002du-0v for qemu-devel@nongnu.org; Mon, 11 Aug 2008 08:47:15 -0400 Subject: Re: [Qemu-devel] [patch 4/5][v2] Aggregate same type clusters. From: Laurent Vivier In-Reply-To: <48A02C42.1040804@suse.de> References: <20080729141352.573798859@bull.net> <20080729141447.627696154@bull.net> <48A02C42.1040804@suse.de> Content-Type: text/plain; charset=utf-8 Date: Mon, 11 Aug 2008 14:39:05 +0200 Message-Id: <1218458345.3871.12.camel@frecb07144> Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Kevin Wolf Cc: qemu-devel@nongnu.org Hi Kevin, BTW, I'm currently rewriting this patch... Le lundi 11 ao=C3=BBt 2008 =C3=A0 14:10 +0200, Kevin Wolf a =C3=A9crit : > Laurent Vivier schrieb: > > Modify get_cluster_offset(), alloc_cluster_offset() and free_used_clu= sters() > > to specify how many clusters we want. > >=20 > > Signed-off-by: Laurent Vivier > > --- > > block-qcow2.c | 212 ++++++++++++++++++++++++++++++++++++++++++-----= ----------- > > 1 file changed, 154 insertions(+), 58 deletions(-) > >=20 > > 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-07-29 15:22:26.000000000 +0200 > > +++ qemu/block-qcow2.c 2008-07-29 15:22:28.000000000 +0200 > > @@ -575,32 +575,76 @@ static int l2_allocate(BlockDriverState=20 > > return 1; > > } > > =20 > > -static uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t of= fset) > > +static uint64_t get_cluster_offset(BlockDriverState *bs, > > + uint64_t offset, int *num) >=20 > I think you start to know what kind of comments I'll provide. So yes, > here's another one of them: While it's intuitive what value I should > pass for num, it's cleary not what the function will return in it. Or > even what the function is doing at all. >=20 > This is how I understand it: The returned num is the number of > contiguous clusters that can be read with a single read operation, i.e. > they are all sparse, come from a backing file or are physically > contiguous in the image file. Yes > Add a comment which says this and I'll be happy. OK (I'll cut&paste the lines above ;-) ) > > { > > BDRVQcowState *s =3D bs->opaque; > > int l1_index, l2_index, ret; > > - uint64_t l2_offset, *l2_table, cluster_offset; > > + uint64_t l2_offset, *l2_table, cluster_offset, next; > > + int l1_bits; > > + int index_in_cluster, nb_available, nb_needed; > > =20 > > - l1_index =3D offset >> (s->l2_bits + s->cluster_bits); > > + index_in_cluster =3D (offset >> 9) & (s->cluster_sectors - 1); > > + nb_needed =3D *num + index_in_cluster; > > + > > + l1_bits =3D s->l2_bits + s->cluster_bits; > > + > > + nb_available =3D (1 << l1_bits) - (offset & ((1 << l1_bits) - 1)= ); > > + nb_available =3D (nb_available >> 9) + index_in_cluster; >=20 > This could use a comment that nb_available is the remaining sectors in > the L2 table (is it?) and that it is used in the following two > conditions (the goto makes this non-obvious - at first, I thought that > this value wouldn't be used at all) nb_available =3D (1 << l1_bits) - (offset & ((1 << l1_bits) - 1)); is the number of bytes given by one l1 entry nb_available =3D (nb_available >> 9) + index_in_cluster; is the number of sectors from the first sector we want to the last sector of the same l1 entry. I do that because I don't want to manage the case where we run across a l1 cache entry boundary. I guess you want comments ? > > + > > + cluster_offset =3D 0; > > + > > + l1_index =3D offset >> l1_bits; > > if (l1_index >=3D s->l1_size) > > - return 0; > > + goto out; > > =20 > > if (!s->l1_table[l1_index]) > > - return 0; > > + goto out; > > =20 > > ret =3D l2_load(bs, l1_index, &l2_table, &l2_offset); > > if (ret =3D=3D 0) > > - return 0; > > + goto out; >=20 > ret =3D=3D 0 means that loading the L2 table failed. This is a real err= or, > right? Isn't return 0 the right thing to do then? Yes... :-P > > =20 > > l2_index =3D (offset >> s->cluster_bits) & (s->l2_size - 1); > > cluster_offset =3D be64_to_cpu(l2_table[l2_index]); > > + nb_available =3D s->cluster_sectors; > > + l2_index++; > > + > > + if (!cluster_offset) { > > + > > + /* how many empty clusters ? */ > > + > > + while (nb_available < nb_needed && !l2_table[l2_index]) { > > + l2_index++; > > + nb_available +=3D s->cluster_sectors; > > + } > > + > > + } else { > > =20 > > - return cluster_offset & ~QCOW_OFLAG_COPIED; > > + /* how many allocated clusters ? */ > > + > > + cluster_offset &=3D ~QCOW_OFLAG_COPIED; > > + while (nb_available < nb_needed) { > > + next =3D be64_to_cpu(l2_table[l2_index]) & ~QCOW_OFLAG_CO= PIED; > > + if (next !=3D cluster_offset + (nb_available << 9)) > > + break; > > + l2_index++; > > + nb_available +=3D s->cluster_sectors; > > + } > > + } > > + > > +out: > > + if (nb_available > nb_needed) > > + nb_available =3D nb_needed; > > + > > + *num =3D nb_available - index_in_cluster; > > + > > + return cluster_offset; > > } > > =20 > > static uint64_t free_used_clusters(BlockDriverState *bs, uint64_t of= fset, > > uint64_t **l2_table, uint64_t *l2_offset, > > - int *l2_index) > > + int *l2_index, int *nb_clusters) >=20 > You would save some ifs if you didn't allow nb_cluster to be NULL. > Passing a local variable containing 1 should do the very same thing and > seems to be less error prone. Otherwise, put a note here which says wha= t > passing NULL means. >=20 To follow your previous comments (patch 3), free_used_clusters() has been removed from this patch... > > { > > BDRVQcowState *s =3D bs->opaque; > > int l1_index, ret; > > @@ -629,21 +673,63 @@ static uint64_t free_used_clusters(Block > > *l2_index =3D (offset >> s->cluster_bits) & (s->l2_size - 1); > > cluster_offset =3D be64_to_cpu((*l2_table)[*l2_index]); > > =20 > > - if (cluster_offset & QCOW_OFLAG_COPIED) > > + if (nb_clusters && *nb_clusters > s->l2_size - (*l2_index)) > > + *nb_clusters =3D s->l2_size - (*l2_index); > > + > > + if (!cluster_offset) { > > + if (nb_clusters) { > > + int i =3D 1; > > + while (i < *nb_clusters && (*l2_table)[(*l2_index) + i] = =3D=3D 0) { > > + i++; > > + } > > + *nb_clusters =3D i; > > + } > > + return 0; > > + } > > + > > + if (cluster_offset & QCOW_OFLAG_COPIED) { > > + if (nb_clusters) { > > + int i =3D 1; > > + uint64_t current; > > + while (i < *nb_clusters) { > > + current =3D be64_to_cpu((*l2_table)[(*l2_index) + i]= ); > > + if (cluster_offset + (i << s->cluster_bits) !=3D cur= rent) > > + break; > > + i++; > > + } > > + *nb_clusters =3D i; > > + } > > return cluster_offset; > > + } > > =20 > > - if (cluster_offset) { > > - /* free the cluster */ > > - if (cluster_offset & QCOW_OFLAG_COMPRESSED) { > > - int nb_csectors; > > - nb_csectors =3D ((cluster_offset >> s->csize_shift) & > > - s->csize_mask) + 1; > > - free_clusters(bs, (cluster_offset & s->cluster_offset_ma= sk) & ~511, > > - nb_csectors * 512); > > - } else { > > - free_clusters(bs, cluster_offset, s->cluster_size); > > + /* free the cluster */ > > + > > + if (cluster_offset & QCOW_OFLAG_COMPRESSED) { > > + int nb_csectors; > > + nb_csectors =3D ((cluster_offset >> s->csize_shift) & s->csi= ze_mask) + 1; > > + free_clusters(bs, (cluster_offset & s->cluster_offset_mask) = & ~511, > > + nb_csectors * 512); > > + if (nb_clusters) > > + *nb_clusters =3D 1; > > + return 0; > > + } > > + > > + if (nb_clusters) { > > + int i =3D 1; > > + uint64_t current; > > + while (i < *nb_clusters) { > > + current =3D be64_to_cpu((*l2_table)[(*l2_index) + i]); > > + if (cluster_offset + (i << s->cluster_bits) !=3D current= ) > > + break; > > + i++; > > } > > + *nb_clusters =3D i; > > + free_clusters(bs, cluster_offset, i << s->cluster_bits); > > + return 0; > > } > > + > > + free_clusters(bs, cluster_offset, s->cluster_size); > > + > > return 0; > > } > > =20 > > @@ -657,7 +743,8 @@ static uint64_t alloc_compressed_cluster > > int nb_csectors; > > =20 > > cluster_offset =3D free_used_clusters(bs, offset, > > - &l2_table, &l2_offset, &l2_i= ndex); > > + &l2_table, &l2_offset, &l2_i= ndex, > > + NULL); > > if (cluster_offset & QCOW_OFLAG_COPIED) > > return cluster_offset & ~QCOW_OFLAG_COPIED; > > =20 > > @@ -683,63 +770,80 @@ static uint64_t alloc_compressed_cluster > > =20 > > static uint64_t alloc_cluster_offset(BlockDriverState *bs, > > uint64_t offset, > > - int n_start, int n_end) > > + int n_start, int n_end, > > + int *num) >=20 > The interface between get_cluster_offset and alloc_cluster_offset is > inconsistent. In the former function, the value passed in num is used t= o > determine the number of clusters to get. In the latter, num is an outpu= t > parameter whose value isn't used. This is confusing. Yes, I know, I have to think about this. > > { > > BDRVQcowState *s =3D bs->opaque; > > int l2_index, ret; > > uint64_t l2_offset, *l2_table, cluster_offset; > > + int nb_available, nb_clusters, i; > > + uint64_t start_sect; > > =20 > > + nb_clusters =3D ((n_end << 9) + s->cluster_size - 1) >> > > + s->cluster_bits; > > =20 > > cluster_offset =3D free_used_clusters(bs, offset, > > - &l2_table, &l2_offset, &l2_i= ndex); > > - if (cluster_offset & QCOW_OFLAG_COPIED) > > - return cluster_offset & ~QCOW_OFLAG_COPIED; > > + &l2_table, &l2_offset, &l2_i= ndex, > > + &nb_clusters); > > + nb_available =3D nb_clusters << (s->cluster_bits - 9); > > + if (nb_available > n_end) > > + nb_available =3D n_end; > > + > > + if (cluster_offset & QCOW_OFLAG_COPIED) { > > + cluster_offset &=3D ~QCOW_OFLAG_COPIED; > > + goto out; > > + } > > =20 > > - /* allocate a new cluster */ > > + /* allocate new clusters */ > > =20 > > - cluster_offset =3D alloc_clusters(bs, s->cluster_size); > > + cluster_offset =3D alloc_clusters(bs, nb_clusters * s->cluster_s= ize); > > =20 > > /* we must initialize the cluster content which won't be > > written */ > > =20 > > - if ((n_end - n_start) < s->cluster_sectors) { > > - uint64_t start_sect; > > - > > - start_sect =3D (offset & ~(s->cluster_size - 1)) >> 9; > > + start_sect =3D (offset & ~(s->cluster_size - 1)) >> 9; > > + if (n_start) { > > ret =3D copy_sectors(bs, start_sect, cluster_offset, 0, n_st= art); > > if (ret < 0) > > return 0; > > - ret =3D copy_sectors(bs, start_sect, > > - cluster_offset, n_end, s->cluster_sectors= ); > > + } > > + > > + if (nb_available & (s->cluster_sectors - 1)) { > > + uint64_t end =3D nb_available & ~(uint64_t)(s->cluster_secto= rs - 1); > > + ret =3D copy_sectors(bs, start_sect + end, > > + cluster_offset + (end << 9), > > + nb_available - end, > > + s->cluster_sectors); > > if (ret < 0) > > return 0; > > } > > =20 > > /* update L2 table */ > > =20 > > - l2_table[l2_index] =3D cpu_to_be64(cluster_offset | QCOW_OFLAG_C= OPIED); > > + for (i =3D 0; i < nb_clusters; i++) > > + l2_table[l2_index + i] =3D cpu_to_be64((cluster_offset + > > + (i << s->cluster_bits))= | > > + QCOW_OFLAG_COPIED); > > + > > if (bdrv_pwrite(s->hd, > > l2_offset + l2_index * sizeof(uint64_t), > > l2_table + l2_index, > > - sizeof(uint64_t)) !=3D sizeof(uint64_t)) > > + nb_clusters * sizeof(uint64_t)) !=3D > > + nb_clusters * sizeof(uint64_t)) > > return 0; > > =20 > > +out: > > + *num =3D nb_available - n_start; > > return cluster_offset; > > } > > =20 > > static int qcow_is_allocated(BlockDriverState *bs, int64_t sector_nu= m, > > int nb_sectors, int *pnum) > > { > > - BDRVQcowState *s =3D bs->opaque; > > - int index_in_cluster, n; > > uint64_t cluster_offset; > > =20 > > - cluster_offset =3D get_cluster_offset(bs, sector_num << 9); > > - index_in_cluster =3D sector_num & (s->cluster_sectors - 1); > > - n =3D s->cluster_sectors - index_in_cluster; > > - if (n > nb_sectors) > > - n =3D nb_sectors; > > - *pnum =3D n; > > + cluster_offset =3D get_cluster_offset(bs, sector_num << 9, pnum)= ; > > + > > return (cluster_offset !=3D 0); > > } > > =20 > > @@ -816,11 +920,9 @@ static int qcow_read(BlockDriverState *b > > uint64_t cluster_offset; > > =20 > > while (nb_sectors > 0) { > > - cluster_offset =3D get_cluster_offset(bs, sector_num << 9); > > + n =3D nb_sectors; > > + cluster_offset =3D get_cluster_offset(bs, sector_num << 9, &= n); > > index_in_cluster =3D sector_num & (s->cluster_sectors - 1); > > - n =3D s->cluster_sectors - index_in_cluster; > > - if (n > nb_sectors) > > - n =3D nb_sectors; > > if (!cluster_offset) { > > if (bs->backing_hd) { > > /* read from the base image */ > > @@ -862,12 +964,10 @@ static int qcow_write(BlockDriverState * > > =20 > > while (nb_sectors > 0) { > > index_in_cluster =3D sector_num & (s->cluster_sectors - 1); > > - n =3D s->cluster_sectors - index_in_cluster; > > - if (n > nb_sectors) > > - n =3D nb_sectors; > > cluster_offset =3D alloc_cluster_offset(bs, sector_num << 9, > > index_in_cluster, > > - index_in_cluster + n); > > + index_in_cluster + nb_= sectors, > > + &n); > > if (!cluster_offset) > > return -1; > > if (s->crypt_method) { > > @@ -940,11 +1040,9 @@ static void qcow_aio_read_cb(void *opaqu > > } > > =20 > > /* prepare next AIO request */ > > - acb->cluster_offset =3D get_cluster_offset(bs, acb->sector_num <= < 9); > > + acb->n =3D acb->nb_sectors; > > + acb->cluster_offset =3D get_cluster_offset(bs, acb->sector_num <= < 9, &acb->n); > > index_in_cluster =3D acb->sector_num & (s->cluster_sectors - 1); > > - acb->n =3D s->cluster_sectors - index_in_cluster; > > - if (acb->n > acb->nb_sectors) > > - acb->n =3D acb->nb_sectors; > > =20 > > if (!acb->cluster_offset) { > > if (bs->backing_hd) { > > @@ -1046,12 +1144,10 @@ static void qcow_aio_write_cb(void *opaq > > } > > =20 > > index_in_cluster =3D acb->sector_num & (s->cluster_sectors - 1); > > - acb->n =3D s->cluster_sectors - index_in_cluster; > > - if (acb->n > acb->nb_sectors) > > - acb->n =3D acb->nb_sectors; > > cluster_offset =3D alloc_cluster_offset(bs, acb->sector_num << 9= , > > index_in_cluster, > > - index_in_cluster + acb->n)= ; > > + index_in_cluster + acb->nb= _sectors, > > + &acb->n); > > if (!cluster_offset || (cluster_offset & 511) !=3D 0) { > > ret =3D -EIO; > > goto fail; >=20 > In the writing functions, you can't just assign a big n, because > s->cluster_data will be too small when processing encrypted data. As yo= u > said you fixed a segfault, I think you know this one already. yes, this is the cause of the segfault. Regards, Laurent --=20 ----------------- Laurent.Vivier@bull.net ------------------ "La perfection est atteinte non quand il ne reste rien =C3=A0 ajouter mais quand il ne reste rien =C3=A0 enlever." Saint Exup=C3=A9ry