Patchwork [PULL,24/42] block: do not use ->total_sectors in bdrv_co_is_allocated

login
register
mail settings
Submitter Stefan Hajnoczi
Date Sept. 6, 2013, 3:38 p.m.
Message ID <1378481953-23099-25-git-send-email-stefanha@redhat.com>
Download mbox | patch
Permalink /patch/273284/
State New
Headers show

Comments

Stefan Hajnoczi - Sept. 6, 2013, 3:38 p.m.
From: Paolo Bonzini <pbonzini@redhat.com>

This is more robust when the device has removable media.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

Patch

diff --git a/block.c b/block.c
index f27bab6..4a10fb1 100644
--- a/block.c
+++ b/block.c
@@ -3065,9 +3065,15 @@  static int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs,
                                              int64_t sector_num,
                                              int nb_sectors, int *pnum)
 {
+    int64_t length;
     int64_t n;
 
-    if (sector_num >= bs->total_sectors) {
+    length = bdrv_getlength(bs);
+    if (length < 0) {
+        return length;
+    }
+
+    if (sector_num >= (length >> BDRV_SECTOR_BITS)) {
         *pnum = 0;
         return 0;
     }