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