Skip to contentSkip to conversation

block: Fix bdrv_is_allocated() for short backing files

PATCH Superseded Kevin Wolf 3 participants 4 messages May 6, 2014 – May 7, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 block: Fix bdrv_is_allocated() for short backing files

bdrv_is_allocated() shouldn't return true for sectors that are unallocated, but after the end of a short backing file, even though such sectors are (correctly) marked as containing zeros.

---
 block.c               |  8 +++++---
 include/block/block.h | 11 +++++++----
 2 files changed, 12 insertions(+), 7 deletions(-)
block.c +5 -3
index c90c71a..d3a9906 100644
@@ -3883,6 +3883,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
                                      *pnum, pnum);
     }
 
+    if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
+        ret |= BDRV_BLOCK_ALLOCATED;
+    }
+
     if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
         if (bdrv_unallocated_blocks_are_zero(bs)) {
             ret |= BDRV_BLOCK_ZERO;
@@ -3959,9 +3963,7 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
     if (ret < 0) {
         return ret;
     }
-    return
-        (ret & BDRV_BLOCK_DATA) ||
-        ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
+    return (ret & BDRV_BLOCK_ALLOCATED);
 }
 
 /*
include/block/block.h +7 -4
index 2fda81c..ad4c7e8 100644
@@ -116,6 +116,8 @@ typedef enum {
 /* BDRV_BLOCK_DATA: data is read from bs->file or another file
  * BDRV_BLOCK_ZERO: sectors read as zero
  * BDRV_BLOCK_OFFSET_VALID: sector stored in bs->file as raw data
+ * BDRV_BLOCK_ALLOCATED: the content of the block is determined by this
+ *                       layer (as opposed to the backing file)
  * BDRV_BLOCK_RAW: used internally to indicate that the request
  *                 was answered by the raw driver and that one
  *                 should look in bs->file directly.
@@ -137,10 +139,11 @@ typedef enum {
  *  f    t        f       not allocated or unknown offset, read as zero
  *  f    f        f       not allocated or unknown offset, read from backing_hd
  */
-#define BDRV_BLOCK_DATA         1
-#define BDRV_BLOCK_ZERO         2
-#define BDRV_BLOCK_OFFSET_VALID 4
-#define BDRV_BLOCK_RAW          8
+#define BDRV_BLOCK_DATA         0x01
+#define BDRV_BLOCK_ZERO         0x02
+#define BDRV_BLOCK_OFFSET_VALID 0x04
+#define BDRV_BLOCK_RAW          0x08
+#define BDRV_BLOCK_ALLOCATED    0x10
 #define BDRV_BLOCK_OFFSET_MASK  BDRV_SECTOR_MASK
 
 typedef enum {
Signature · 1 line
1.8.3.1
#1 lore raw

Il 06/05/2014 15:30, Kevin Wolf ha scritto:

Quoted 5 lines

bdrv_is_allocated() shouldn't return true for sectors that are unallocated, but after the end of a short backing file, even though such sectors are (correctly) marked as containing zeros.

Nice. :)

Paolo

#2 lore raw
In reply to Kevin Wolf
Quoted 15 lines · Kevin Wolf

bdrv_is_allocated() shouldn't return true for sectors that are unallocated, but after the end of a short backing file, even though such sectors are (correctly) marked as containing zeros.

---
  block.c               |  8 +++++---
  include/block/block.h | 11 +++++++----
  2 files changed, 12 insertions(+), 7 deletions(-)
block.c +0 -0
index c90c71a..d3a9906 100644

block.c static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, · line 3889

Quoted 7 lines
block.c +4 -0
@@ -3883,6 +3883,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
                                       *pnum, pnum);
      }
 
+    if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
+        ret |= BDRV_BLOCK_ALLOCATED;
+    }
+

Shouldn't BDRV_BLOCK_ALLOCATED be set in the !bs->drv->bdrv_co_get_block_status case as well?

Quoted 19 lines
block.c +1 -3
      if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
          if (bdrv_unallocated_blocks_are_zero(bs)) {
              ret |= BDRV_BLOCK_ZERO;
@@ -3959,9 +3963,7 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
      if (ret < 0) {
          return ret;
      }
-    return
-        (ret & BDRV_BLOCK_DATA) ||
-        ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
+    return (ret & BDRV_BLOCK_ALLOCATED);
  }
 
  /*
include/block/block.h +0 -0
index 2fda81c..ad4c7e8 100644

include/block/block.h typedef enum { · line 120

Quoted 5 lines
include/block/block.h +2 -0
@@ -116,6 +116,8 @@ typedef enum {
  /* BDRV_BLOCK_DATA: data is read from bs->file or another file
   * BDRV_BLOCK_ZERO: sectors read as zero
   * BDRV_BLOCK_OFFSET_VALID: sector stored in bs->file as raw data
+ * BDRV_BLOCK_ALLOCATED: the content of the block is determined by this
+ *                       layer (as opposed to the backing file)

I guess this is above BDRV_BLOCK_RAW (albeit having a greater value) because it is not only used internally? (to pick up on the topic of OCD :-P)

Max

Quoted 19 lines
include/block/block.h +5 -4
   * BDRV_BLOCK_RAW: used internally to indicate that the request
   *                 was answered by the raw driver and that one
   *                 should look in bs->file directly.
@@ -137,10 +139,11 @@ typedef enum {
   *  f    t        f       not allocated or unknown offset, read as zero
   *  f    f        f       not allocated or unknown offset, read from backing_hd
   */
-#define BDRV_BLOCK_DATA         1
-#define BDRV_BLOCK_ZERO         2
-#define BDRV_BLOCK_OFFSET_VALID 4
-#define BDRV_BLOCK_RAW          8
+#define BDRV_BLOCK_DATA         0x01
+#define BDRV_BLOCK_ZERO         0x02
+#define BDRV_BLOCK_OFFSET_VALID 0x04
+#define BDRV_BLOCK_RAW          0x08
+#define BDRV_BLOCK_ALLOCATED    0x10
  #define BDRV_BLOCK_OFFSET_MASK  BDRV_SECTOR_MASK
 
  typedef enum {
author
#3 lore raw

Am 06.05.2014 um 21:53 hat Max Reitz geschrieben:

Quoted 11 lines · Kevin Wolf
Quoted 10 lines

bdrv_is_allocated() shouldn't return true for sectors that are unallocated, but after the end of a short backing file, even though such sectors are (correctly) marked as containing zeros.

---
 block.c               |  8 +++++---
 include/block/block.h | 11 +++++++----
 2 files changed, 12 insertions(+), 7 deletions(-)
Quoted 14 lines

block.c static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, · line 3888

Quoted 11 lines
block.c +4 -0
index c90c71a..d3a9906 100644
@@ -3883,6 +3883,10 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
                                      *pnum, pnum);
     }
+    if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) {
+        ret |= BDRV_BLOCK_ALLOCATED;
+    }
+

Shouldn't BDRV_BLOCK_ALLOCATED be set in the
!bs->drv->bdrv_co_get_block_status case as well?

It should. Thanks, I'll send a v2.

Quoted 19 lines
Quoted 19 lines
block.c +1 -3
     if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) {
         if (bdrv_unallocated_blocks_are_zero(bs)) {
             ret |= BDRV_BLOCK_ZERO;
@@ -3959,9 +3963,7 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
     if (ret < 0) {
         return ret;
     }
-    return
-        (ret & BDRV_BLOCK_DATA) ||
-        ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs));
+    return (ret & BDRV_BLOCK_ALLOCATED);
 }
 /*
include/block/block.h +0 -0
index 2fda81c..ad4c7e8 100644
@@ -116,6 +116,8 @@ typedef enum {
 /* BDRV_BLOCK_DATA: data is read from bs->file or another file
Quoted 8 lines

include/block/block.h typedef enum { · line 120

Quoted 4 lines
include/block/block.h +2 -0
  * BDRV_BLOCK_ZERO: sectors read as zero
  * BDRV_BLOCK_OFFSET_VALID: sector stored in bs->file as raw data
+ * BDRV_BLOCK_ALLOCATED: the content of the block is determined by this
+ *                       layer (as opposed to the backing file)

I guess this is above BDRV_BLOCK_RAW (albeit having a greater value) because it is not only used internally? (to pick up on the topic of OCD :-P)

Because it felt right. This may or may not be equivalent.

Kevin