From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, benoit@irqsave.net, stefanha@redhat.com,
mreitz@redhat.com
Subject: [Qemu-devel] [PATCH v4 02/10] block: New bdrv_nb_sectors()
Date: Wed, 4 Jun 2014 13:51:43 +0200 [thread overview]
Message-ID: <1401882711-30508-3-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1401882711-30508-1-git-send-email-armbru@redhat.com>
A call to retrieve the image size converts between bytes and sectors
several times:
* BlockDriver method bdrv_getlength() returns bytes.
* refresh_total_sectors() converts to sectors, rounding up, and stores
in total_sectors.
* bdrv_getlength() converts total_sectors back to bytes (now rounded
up to a multiple of the sector size).
* Callers wanting sectors rather bytes convert it right back.
Example: bdrv_get_geometry().
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.
Reimplement bdrv_getlength() and bdrv_get_geometry() on top of
bdrv_nb_sectors().
The next patches will convert some users of bdrv_getlength() to
bdrv_nb_sectors().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Benoit Canet <benoit@irqsave.net>
---
block.c | 29 +++++++++++++++++++----------
include/block/block.h | 1 +
2 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/block.c b/block.c
index 310ea89..0db7d39 100644
--- a/block.c
+++ b/block.c
@@ -693,6 +693,7 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
/**
* Set the current 'total_sectors' value
+ * Return 0 on success, -errno on error.
*/
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
{
@@ -3552,11 +3553,12 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
}
/**
- * 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 = bs->drv;
+
if (!drv)
return -ENOMEDIUM;
@@ -3566,19 +3568,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 = bdrv_nb_sectors(bs);
+
+ return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
}
/* 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 = bdrv_getlength(bs);
- if (length < 0)
- length = 0;
- else
- length = length >> BDRV_SECTOR_BITS;
- *nb_sectors_ptr = length;
+ int64_t nb_sectors = bdrv_nb_sectors(bs);
+
+ *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
}
void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
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(BlockDriverState *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);
--
1.9.3
next prev parent reply other threads:[~2014-06-04 11:52 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-04 11:51 [Qemu-devel] [PATCH v4 00/10] Clean up around bdrv_getlength() Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 01/10] raw-posix: Fix raw_getlength() to always return -errno on error Markus Armbruster
2014-06-04 11:51 ` Markus Armbruster [this message]
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 03/10] block: Use bdrv_nb_sectors() in bdrv_make_zero() Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 04/10] block: Use bdrv_nb_sectors() in bdrv_aligned_preadv() Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 05/10] block: Use bdrv_nb_sectors() in bdrv_co_get_block_status() Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 06/10] block: Use bdrv_nb_sectors() in img_convert() Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 07/10] block: Use bdrv_nb_sectors() where sectors, not bytes are wanted Markus Armbruster
2014-06-04 12:26 ` Benoît Canet
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 08/10] block: Drop superfluous aligning of bdrv_getlength()'s value Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 09/10] qemu-img: Make img_convert() get image size just once per image Markus Armbruster
2014-06-04 11:51 ` [Qemu-devel] [PATCH v4 10/10] block: Avoid bdrv_get_geometry() where errors should be detected Markus Armbruster
2014-06-04 12:24 ` Benoît Canet
2014-06-04 13:20 ` Markus Armbruster
2014-06-04 13:28 ` Benoît Canet
2014-06-04 13:30 ` Benoît Canet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1401882711-30508-3-git-send-email-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=benoit@irqsave.net \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).