From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44096) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrTcx-0002P4-6V for qemu-devel@nongnu.org; Mon, 02 Jun 2014 10:54:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WrTcr-0007A7-6m for qemu-devel@nongnu.org; Mon, 02 Jun 2014 10:54:19 -0400 Received: from lputeaux-656-01-25-125.w80-12.abo.wanadoo.fr ([80.12.84.125]:38622 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WrTcq-0007A1-Th for qemu-devel@nongnu.org; Mon, 02 Jun 2014 10:54:13 -0400 Date: Mon, 2 Jun 2014 16:54:12 +0200 From: =?iso-8859-1?Q?Beno=EEt?= Canet Message-ID: <20140602145411.GB8181@irqsave.net> References: <1401473631-10724-1-git-send-email-armbru@redhat.com> <1401473631-10724-3-git-send-email-armbru@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1401473631-10724-3-git-send-email-armbru@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH v3 02/10] block: New bdrv_nb_sectors() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Markus Armbruster Cc: kwolf@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com, mreitz@redhat.com The Friday 30 May 2014 =E0 20:13:43 (+0200), Markus Armbruster wrote : > A call to retrieve the image size converts between bytes and sectors > several times: >=20 > * BlockDriver method bdrv_getlength() returns bytes. >=20 > * refresh_total_sectors() converts to sectors, rounding up, and stores > in total_sectors. >=20 > * bdrv_getlength() converts total_sectors back to bytes (now rounded > up to a multiple of the sector size). >=20 > * Callers wanting sectors rather bytes convert it right back. > Example: bdrv_get_geometry(). >=20 > bdrv_nb_sectors() provides a way to omit the last two conversions. > It's exactly bdrv_getlength() with the conversion to bytes omitted. > It's functionally like bdrv_get_geometry() without its odd error > handling. >=20 > Reimplement bdrv_getlength() and bdrv_get_geometry() on top of > bdrv_nb_sectors(). >=20 > The next patches will convert some users of bdrv_getlength() to > bdrv_nb_sectors(). >=20 > Signed-off-by: Markus Armbruster > Reviewed-by: Eric Blake > --- > block.c | 29 +++++++++++++++++++---------- > include/block/block.h | 1 + > 2 files changed, 20 insertions(+), 10 deletions(-) >=20 > diff --git a/block.c b/block.c > index a517d72..14dc5fe 100644 > --- a/block.c > +++ b/block.c > @@ -693,6 +693,7 @@ static int find_image_format(BlockDriverState *bs, = const char *filename, > =20 > /** > * Set the current 'total_sectors' value > + * Return 0 on success, -errno on error. > */ > static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) > { > @@ -3551,11 +3552,12 @@ int64_t bdrv_get_allocated_file_size(BlockDrive= rState *bs) > } > =20 > /** > - * Length of a file in bytes. Return < 0 if error or unknown. > + * Return number of sectors on success, -errno on error. > */ > -int64_t bdrv_getlength(BlockDriverState *bs) > +int64_t bdrv_nb_sectors(BlockDriverState *bs) > { > BlockDriver *drv =3D bs->drv; > + > if (!drv) > return -ENOMEDIUM; > =20 > @@ -3565,19 +3567,26 @@ int64_t bdrv_getlength(BlockDriverState *bs) > return ret; > } > } > - return bs->total_sectors * BDRV_SECTOR_SIZE; > + return bs->total_sectors; > +} > + > +/** > + * Return length in bytes on success, -errno on error. > + * The length is always a multiple of BDRV_SECTOR_SIZE. > + */ > +int64_t bdrv_getlength(BlockDriverState *bs) > +{ > + int64_t ret =3D bdrv_nb_sectors(bs); > + > + return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE; > } > =20 > /* return 0 as number of sectors if no device present or error */ > void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) > { > - int64_t length; > - length =3D bdrv_getlength(bs); > - if (length < 0) > - length =3D 0; > - else > - length =3D length >> BDRV_SECTOR_BITS; > - *nb_sectors_ptr =3D length; > + int64_t nb_sectors =3D bdrv_nb_sectors(bs); > + > + *nb_sectors_ptr =3D nb_sectors < 0 ? 0 : nb_sectors; > } > =20 > void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_e= rror, > diff --git a/include/block/block.h b/include/block/block.h > index faee3aa..4a1e875 100644 > --- a/include/block/block.h > +++ b/include/block/block.h > @@ -279,6 +279,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriv= erState *bs, > const char *backing_file); > int bdrv_get_backing_file_depth(BlockDriverState *bs); > int bdrv_truncate(BlockDriverState *bs, int64_t offset); > +int64_t bdrv_nb_sectors(BlockDriverState *bs); > int64_t bdrv_getlength(BlockDriverState *bs); > int64_t bdrv_get_allocated_file_size(BlockDriverState *bs); > void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)= ; > --=20 > 1.9.3 >=20 >=20 Reviewed-by: Benoit Canet