958e1b
From 47601d6fd4a620799984340c1fd44ff472f9d824 Mon Sep 17 00:00:00 2001
958e1b
Message-Id: <47601d6fd4a620799984340c1fd44ff472f9d824.1418766606.git.jen@redhat.com>
958e1b
In-Reply-To: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
958e1b
References: <6f81b4847eb68ebdf54a8f1a771e19d112d74152.1418766606.git.jen@redhat.com>
958e1b
From: Fam Zheng <famz@redhat.com>
958e1b
Date: Thu, 4 Dec 2014 00:05:18 -0600
958e1b
Subject: [CHANGE 24/31] block: New bdrv_nb_sectors()
958e1b
To: rhvirt-patches@redhat.com,
958e1b
    jen@redhat.com
958e1b
958e1b
RH-Author: Fam Zheng <famz@redhat.com>
958e1b
Message-id: <1417651524-18041-25-git-send-email-famz@redhat.com>
958e1b
Patchwork-id: 62697
958e1b
O-Subject: [RHEL-7.1 qemu-kvm PATCH v5 24/30] block: New bdrv_nb_sectors()
958e1b
Bugzilla: 1002493
958e1b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
958e1b
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
958e1b
RH-Acked-by: Max Reitz <mreitz@redhat.com>
958e1b
958e1b
From: Markus Armbruster <armbru@redhat.com>
958e1b
958e1b
A call to retrieve the image size converts between bytes and sectors
958e1b
several times:
958e1b
958e1b
* BlockDriver method bdrv_getlength() returns bytes.
958e1b
958e1b
* refresh_total_sectors() converts to sectors, rounding up, and stores
958e1b
  in total_sectors.
958e1b
958e1b
* bdrv_getlength() converts total_sectors back to bytes (now rounded
958e1b
  up to a multiple of the sector size).
958e1b
958e1b
* Callers wanting sectors rather bytes convert it right back.
958e1b
  Example: bdrv_get_geometry().
958e1b
958e1b
bdrv_nb_sectors() provides a way to omit the last two conversions.
958e1b
It's exactly bdrv_getlength() with the conversion to bytes omitted.
958e1b
It's functionally like bdrv_get_geometry() without its odd error
958e1b
handling.
958e1b
958e1b
Reimplement bdrv_getlength() and bdrv_get_geometry() on top of
958e1b
bdrv_nb_sectors().
958e1b
958e1b
The next patches will convert some users of bdrv_getlength() to
958e1b
bdrv_nb_sectors().
958e1b
958e1b
Signed-off-by: Markus Armbruster <armbru@redhat.com>
958e1b
Reviewed-by: Eric Blake <eblake@redhat.com>
958e1b
Reviewed-by: Benoit Canet <benoit@irqsave.net>
958e1b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
958e1b
(cherry picked from commit 65a9bb25d6b7a4bb41edb102fa0363d81800b417)
958e1b
Signed-off-by: Fam Zheng <famz@redhat.com>
958e1b
Signed-off-by: Jeff E. Nelson <jen@redhat.com>
958e1b
---
958e1b
 block.c               | 29 +++++++++++++++++++----------
958e1b
 include/block/block.h |  1 +
958e1b
 2 files changed, 20 insertions(+), 10 deletions(-)
958e1b
958e1b
diff --git a/block.c b/block.c
958e1b
index d7b6376..21418a6 100644
958e1b
--- a/block.c
958e1b
+++ b/block.c
958e1b
@@ -647,6 +647,7 @@ static int find_image_format(BlockDriverState *bs, const char *filename,
958e1b
 
958e1b
 /**
958e1b
  * Set the current 'total_sectors' value
958e1b
+ * Return 0 on success, -errno on error.
958e1b
  */
958e1b
 static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
958e1b
 {
958e1b
@@ -3255,11 +3256,12 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
958e1b
 }
958e1b
 
958e1b
 /**
958e1b
- * Length of a file in bytes. Return < 0 if error or unknown.
958e1b
+ * Return number of sectors on success, -errno on error.
958e1b
  */
958e1b
-int64_t bdrv_getlength(BlockDriverState *bs)
958e1b
+int64_t bdrv_nb_sectors(BlockDriverState *bs)
958e1b
 {
958e1b
     BlockDriver *drv = bs->drv;
958e1b
+
958e1b
     if (!drv)
958e1b
         return -ENOMEDIUM;
958e1b
 
958e1b
@@ -3269,19 +3271,26 @@ int64_t bdrv_getlength(BlockDriverState *bs)
958e1b
             return ret;
958e1b
         }
958e1b
     }
958e1b
-    return bs->total_sectors * BDRV_SECTOR_SIZE;
958e1b
+    return bs->total_sectors;
958e1b
+}
958e1b
+
958e1b
+/**
958e1b
+ * Return length in bytes on success, -errno on error.
958e1b
+ * The length is always a multiple of BDRV_SECTOR_SIZE.
958e1b
+ */
958e1b
+int64_t bdrv_getlength(BlockDriverState *bs)
958e1b
+{
958e1b
+    int64_t ret = bdrv_nb_sectors(bs);
958e1b
+
958e1b
+    return ret < 0 ? ret : ret * BDRV_SECTOR_SIZE;
958e1b
 }
958e1b
 
958e1b
 /* return 0 as number of sectors if no device present or error */
958e1b
 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
958e1b
 {
958e1b
-    int64_t length;
958e1b
-    length = bdrv_getlength(bs);
958e1b
-    if (length < 0)
958e1b
-        length = 0;
958e1b
-    else
958e1b
-        length = length >> BDRV_SECTOR_BITS;
958e1b
-    *nb_sectors_ptr = length;
958e1b
+    int64_t nb_sectors = bdrv_nb_sectors(bs);
958e1b
+
958e1b
+    *nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
958e1b
 }
958e1b
 
958e1b
 /* throttling disk io limits */
958e1b
diff --git a/include/block/block.h b/include/block/block.h
958e1b
index b06a9dc..c79a1e1 100644
958e1b
--- a/include/block/block.h
958e1b
+++ b/include/block/block.h
958e1b
@@ -250,6 +250,7 @@ BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
958e1b
     const char *backing_file);
958e1b
 int bdrv_get_backing_file_depth(BlockDriverState *bs);
958e1b
 int bdrv_truncate(BlockDriverState *bs, int64_t offset);
958e1b
+int64_t bdrv_nb_sectors(BlockDriverState *bs);
958e1b
 int64_t bdrv_getlength(BlockDriverState *bs);
958e1b
 int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
958e1b
 void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
958e1b
-- 
958e1b
2.1.0
958e1b