Skip to contentSkip to conversation

raw-posix: Fix raw_co_get_block_status()

PATCH In review Max Reitz 2 participants 5 messages Sep 22, 2014 – Oct 10, 2014 Block layer

lore
Share & tools
lore
author
#3 lore raw

PATCH 2/2 raw-posix: raw_co_get_block_status() return value

In reply to Max Reitz

Instead of generating the full return value thrice in try_fiemap(), try_seek_hole() and as a fall-back in raw_co_get_block_status() itself, generate the value only in raw_co_get_block_status().

---
 block/raw-posix.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
block/raw-posix.c +10 -10
index de4e3f3..7e1cc02 100644
@@ -1455,12 +1455,12 @@ out:
     return result;
 }
 
-static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
+static int try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
                           off_t *hole, int nb_sectors, int *pnum)
 {
 #ifdef CONFIG_FIEMAP
     BDRVRawState *s = bs->opaque;
-    int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    int ret = 0;
     struct {
         struct fiemap fm;
         struct fiemap_extent fe;
@@ -1501,7 +1501,7 @@ static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
 #endif
 }
 
-static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
+static int try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
                              off_t *hole, int *pnum)
 {
 #if defined SEEK_HOLE && defined SEEK_DATA
@@ -1526,7 +1526,7 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
         }
     }
 
-    return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    return 0;
 #else
     return -ENOTSUP;
 #endif
@@ -1552,7 +1552,7 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
                                                     int nb_sectors, int *pnum)
 {
     off_t start, data = 0, hole = 0;
-    int64_t ret;
+    int ret;
 
     ret = fd_open(bs);
     if (ret < 0) {
@@ -1572,21 +1572,21 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
             /* Assume everything is allocated. */
             data = 0;
             hole = start + nb_sectors * BDRV_SECTOR_SIZE;
-            ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+            ret = 0;
         }
     }
 
+    assert(ret >= 0);
+
     if (data <= start) {
         /* On a data extent, compute sectors to the end of the extent.  */
         *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
+        return ret | BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
     } else {
         /* On a hole, compute sectors to the beginning of the next extent.  */
         *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
-        ret &= ~BDRV_BLOCK_DATA;
-        ret |= BDRV_BLOCK_ZERO;
+        return ret | BDRV_BLOCK_ZERO | BDRV_BLOCK_OFFSET_VALID | start;
     }
-
-    return ret;
 }
 
 static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
Signature · 1 line
2.1.0
author
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] raw-posix: raw_co_get_block_status() return value

Quoted 15 lines · Max Reitz

Instead of generating the full return value thrice in try_fiemap(), try_seek_hole() and as a fall-back in raw_co_get_block_status() itself, generate the value only in raw_co_get_block_status().

---
  block/raw-posix.c | 20 ++++++++++----------
  1 file changed, 10 insertions(+), 10 deletions(-)
block/raw-posix.c +0 -0
index de4e3f3..7e1cc02 100644

block/raw-posix.c out: · line 1459

Quoted 6 lines
block/raw-posix.c +1 -1
@@ -1455,12 +1455,12 @@ out:
      return result;
  }
 
-static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
+static int try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
                            off_t *hole, int nb_sectors, int *pnum)

Self-NACK, I always love to see alignment issues only after I already sent the series...

Max

Quoted 62 lines
block/raw-posix.c +9 -9
  {
  #ifdef CONFIG_FIEMAP
      BDRVRawState *s = bs->opaque;
-    int64_t ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    int ret = 0;
      struct {
          struct fiemap fm;
          struct fiemap_extent fe;
@@ -1501,7 +1501,7 @@ static int64_t try_fiemap(BlockDriverState *bs, off_t start, off_t *data,
  #endif
  }
 
-static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
+static int try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
                               off_t *hole, int *pnum)
  {
  #if defined SEEK_HOLE && defined SEEK_DATA
@@ -1526,7 +1526,7 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
          }
      }
 
-    return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    return 0;
  #else
      return -ENOTSUP;
  #endif
@@ -1552,7 +1552,7 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
                                                      int nb_sectors, int *pnum)
  {
      off_t start, data = 0, hole = 0;
-    int64_t ret;
+    int ret;
 
      ret = fd_open(bs);
      if (ret < 0) {
@@ -1572,21 +1572,21 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
              /* Assume everything is allocated. */
              data = 0;
              hole = start + nb_sectors * BDRV_SECTOR_SIZE;
-            ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+            ret = 0;
          }
      }
 
+    assert(ret >= 0);
+
      if (data <= start) {
          /* On a data extent, compute sectors to the end of the extent.  */
          *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
+        return ret | BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
      } else {
          /* On a hole, compute sectors to the beginning of the next extent.  */
          *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
-        ret &= ~BDRV_BLOCK_DATA;
-        ret |= BDRV_BLOCK_ZERO;
+        return ret | BDRV_BLOCK_ZERO | BDRV_BLOCK_OFFSET_VALID | start;
      }
-
-    return ret;
  }
 
  static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,

Max Reitz

author
#0 lore raw

cover raw-posix: Fix raw_co_get_block_status()

raw_co_get_block_status() should return 0 and set *pnum to 0 after the EOF; currently it does this merely by accident, so implement it directly.

While doing that, centralize the generation of raw_co_get_block_status()'s return value along the way.

Max Reitz (2):
raw-posix: Fix raw_co_get_block_status() after EOF
raw-posix: raw_co_get_block_status() return value

 block/raw-posix.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
Signature · 1 line
2.1.0
author
#1 lore raw

PATCH 1/2 raw-posix: Fix raw_co_get_block_status() after EOF

As its comment states, raw_co_get_block_status() should unconditionally return 0 and set *pnum to 0 for after EOF.

An assertion after lseek(..., SEEK_HOLE) tried to catch this case by asserting that errno != -ENXIO (which would indicate a position after the EOF); but it should be errno != ENXIO instead. Fix this, too.

---
 block/raw-posix.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/raw-posix.c +6 -2
index a253697..de4e3f3 100644
@@ -1509,9 +1509,9 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
 
     *hole = lseek(s->fd, start, SEEK_HOLE);
     if (*hole == -1) {
-        /* -ENXIO indicates that sector_num was past the end of the file.
+        /* ENXIO indicates that sector_num was past the end of the file.
          * There is a virtual hole there.  */
-        assert(errno != -ENXIO);
+        assert(errno != ENXIO);
 
         return -errno;
     }
@@ -1560,6 +1560,10 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
     }
 
     start = sector_num * BDRV_SECTOR_SIZE;
+    if (start >= bdrv_getlength(bs)) {
+        *pnum = 0;
+        return 0;
+    }
 
     ret = try_fiemap(bs, start, &data, &hole, nb_sectors, pnum);
     if (ret < 0) {
Signature · 1 line
2.1.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] raw-posix: Fix raw_co_get_block_status() after EOF

Quoted 44 lines · Max Reitz

As its comment states, raw_co_get_block_status() should unconditionally return 0 and set *pnum to 0 for after EOF.

An assertion after lseek(..., SEEK_HOLE) tried to catch this case by asserting that errno != -ENXIO (which would indicate a position after the EOF); but it should be errno != ENXIO instead. Fix this, too.

---
 block/raw-posix.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
block/raw-posix.c +6 -2
index a253697..de4e3f3 100644
@@ -1509,9 +1509,9 @@ static int64_t try_seek_hole(BlockDriverState *bs, off_t start, off_t *data,
 
     *hole = lseek(s->fd, start, SEEK_HOLE);
     if (*hole == -1) {
-        /* -ENXIO indicates that sector_num was past the end of the file.
+        /* ENXIO indicates that sector_num was past the end of the file.
          * There is a virtual hole there.  */
-        assert(errno != -ENXIO);
+        assert(errno != ENXIO);
 
         return -errno;
     }
@@ -1560,6 +1560,10 @@ static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
     }
 
     start = sector_num * BDRV_SECTOR_SIZE;
+    if (start >= bdrv_getlength(bs)) {
+        *pnum = 0;
+        return 0;
+    }
 
     ret = try_fiemap(bs, start, &data, &hole, nb_sectors, pnum);
     if (ret < 0) {
Signature · 1 line
2.1.0