Skip to contentSkip to conversation

vpc: Implement bdrv_co_get_status()

PATCH In review Kevin Wolf 2 participants 4 messages Feb 11, 2015 – Feb 12, 2015 Block layer

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 vpc: Implement bdrv_co_get_status()

This implements bdrv_co_get_status() for VHD images. This can significantly speed up qemu-img convert operation because only with this function implemented sparseness can be considered. (Before, converting a 1 TB empty image took several minutes for me, now it's instantaneous.)

---
 block/vpc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
block/vpc.c +52 -2
index 46803b1..379ff4a 100644
@@ -597,6 +597,55 @@ static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num,
     return ret;
 }
 
+static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs,
+        int64_t sector_num, int nb_sectors, int *pnum)
+{
+    BDRVVPCState *s = bs->opaque;
+    VHDFooter *footer =  (VHDFooter *) s->footer_buf;
+    int64_t start, offset;
+    bool allocated;
+    int n;
+
+    if (be32_to_cpu(footer->type) == VHD_FIXED) {
+        *pnum = nb_sectors;
+        return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
+               (sector_num << BDRV_SECTOR_BITS);
+    }
+
+    offset = get_sector_offset(bs, sector_num, 0);
+
+    *pnum = 1;
+    start = offset;
+    allocated = (offset != -1);
+    nb_sectors--;
+
+    while (nb_sectors > 0) {
+        offset = get_sector_offset(bs, sector_num, 0);
+
+        if ((offset != -1) != allocated) {
+            break;
+        }
+
+        if (allocated && offset != start + (*pnum * 512)) {
+            break;
+        }
+
+        /* All sectors in a block are contiguous (without using the bitmap) */
+        n = ROUND_UP(sector_num + 1, s->block_size / 512) - sector_num;
+        n = MIN(n, nb_sectors);
+
+        *pnum += n;
+        sector_num += n;
+        nb_sectors -= n;
+    }
+
+    if (allocated) {
+        return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    } else {
+        return 0;
+    }
+}
+
 /*
  * Calculates the number of cylinders, heads and sectors per cylinder
  * based on a given number of sectors. This is the algorithm described
@@ -907,8 +956,9 @@ static BlockDriver bdrv_vpc = {
     .bdrv_reopen_prepare    = vpc_reopen_prepare,
     .bdrv_create            = vpc_create,
 
-    .bdrv_read              = vpc_co_read,
-    .bdrv_write             = vpc_co_write,
+    .bdrv_read                  = vpc_co_read,
+    .bdrv_write                 = vpc_co_write,
+    .bdrv_co_get_block_status   = vpc_co_get_block_status,
 
     .bdrv_get_info          = vpc_get_info,
 
Signature · 1 line
1.8.3.1
author
#1 lore raw

Am 11.02.2015 um 17:23 hat Kevin Wolf geschrieben:

Quoted 1 line

This implements bdrv_co_get_status() for VHD images. This can

s/bdrv_co_get_status/bdrv_co_get_block_status/, obviously (in the subject as well).

Quoted 5 lines

significantly speed up qemu-img convert operation because only with this function implemented sparseness can be considered. (Before, converting a 1 TB empty image took several minutes for me, now it's instantaneous.)

Kevin

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

This implements bdrv_co_get_status() for VHD images. This can significantly speed up qemu-img convert operation because only with this function implemented sparseness can be considered. (Before, converting a 1 TB empty image took several minutes for me, now it's instantaneous.)

---
  block/vpc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 52 insertions(+), 2 deletions(-)
block/vpc.c +0 -0
index 46803b1..379ff4a 100644

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 604

Quoted 8 lines
block/vpc.c +5 -0
@@ -597,6 +597,55 @@ static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num,
      return ret;
  }
 
+static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs,
+        int64_t sector_num, int nb_sectors, int *pnum)
+{
+    BDRVVPCState *s = bs->opaque;
+    VHDFooter *footer =  (VHDFooter *) s->footer_buf;

Double space before the left parenthesis.

Quoted 9 lines
block/vpc.c +9 -0
+    int64_t start, offset;
+    bool allocated;
+    int n;
+
+    if (be32_to_cpu(footer->type) == VHD_FIXED) {
+        *pnum = nb_sectors;
+        return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
+               (sector_num << BDRV_SECTOR_BITS);
+    }

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 623

Quoted 10 lines
block/vpc.c +10 -0
+
+    offset = get_sector_offset(bs, sector_num, 0);
+
+    *pnum = 1;
+    start = offset;
+    allocated = (offset != -1);
+    nb_sectors--;
+
+    while (nb_sectors > 0) {
+        offset = get_sector_offset(bs, sector_num, 0);

During the first iteration, this will is exactly the function call as the one before this loop.

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 629

Quoted 6 lines
block/vpc.c +6 -0
+
+        if ((offset != -1) != allocated) {
+            break;
+        }
+
+        if (allocated && offset != start + (*pnum * 512)) {

Therefore, if allocated is true, this check will always fail because offset == start.

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 635

Quoted 6 lines
block/vpc.c +6 -0
+            break;
+        }
+
+        /* All sectors in a block are contiguous (without using the bitmap) */
+        n = ROUND_UP(sector_num + 1, s->block_size / 512) - sector_num;
+        n = MIN(n, nb_sectors);

I think you can make use of this assumption even before calling get_sector_offset() in the first iteration.

Also, 512 is shorter than BDRV_SECTOR_SIZE and it's nice to get it on a single line, but I think not introducing another literal 512 in the code would be nicer; the same applies above in "*pnum * 512".

Max

Quoted 28 lines
block/vpc.c +16 -2
+
+        *pnum += n;
+        sector_num += n;
+        nb_sectors -= n;
+    }
+
+    if (allocated) {
+        return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | start;
+    } else {
+        return 0;
+    }
+}
+
  /*
   * Calculates the number of cylinders, heads and sectors per cylinder
   * based on a given number of sectors. This is the algorithm described
@@ -907,8 +956,9 @@ static BlockDriver bdrv_vpc = {
      .bdrv_reopen_prepare    = vpc_reopen_prepare,
      .bdrv_create            = vpc_create,
 
-    .bdrv_read              = vpc_co_read,
-    .bdrv_write             = vpc_co_write,
+    .bdrv_read                  = vpc_co_read,
+    .bdrv_write                 = vpc_co_write,
+    .bdrv_co_get_block_status   = vpc_co_get_block_status,
 
      .bdrv_get_info          = vpc_get_info,
 
author
#3 lore raw

Am 11.02.2015 um 23:51 hat Max Reitz geschrieben:

Quoted 11 lines · Kevin Wolf
Quoted 10 lines

This implements bdrv_co_get_status() for VHD images. This can significantly speed up qemu-img convert operation because only with this function implemented sparseness can be considered. (Before, converting a 1 TB empty image took several minutes for me, now it's instantaneous.)

---
 block/vpc.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
Quoted 14 lines

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 603

Quoted 12 lines
block/vpc.c +5 -0
index 46803b1..379ff4a 100644
@@ -597,6 +597,55 @@ static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num,
     return ret;
 }
+static int64_t coroutine_fn vpc_co_get_block_status(BlockDriverState *bs,
+        int64_t sector_num, int nb_sectors, int *pnum)
+{
+    BDRVVPCState *s = bs->opaque;
+    VHDFooter *footer =  (VHDFooter *) s->footer_buf;

Double space before the left parenthesis.

Fun. As it happens, I copied this line from elsewhere...

Quoted 23 lines
Quoted 9 lines
block/vpc.c +9 -0
+    int64_t start, offset;
+    bool allocated;
+    int n;
+
+    if (be32_to_cpu(footer->type) == VHD_FIXED) {
+        *pnum = nb_sectors;
+        return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
+               (sector_num << BDRV_SECTOR_BITS);
+    }

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 623

Quoted 10 lines
block/vpc.c +10 -0
+
+    offset = get_sector_offset(bs, sector_num, 0);
+
+    *pnum = 1;
+    start = offset;
+    allocated = (offset != -1);
+    nb_sectors--;
+
+    while (nb_sectors > 0) {
+        offset = get_sector_offset(bs, sector_num, 0);

During the first iteration, this will is exactly the function call as the one before this loop.

Quoted 9 lines

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 629

Quoted 6 lines
block/vpc.c +6 -0
+
+        if ((offset != -1) != allocated) {
+            break;
+        }
+
+        if (allocated && offset != start + (*pnum * 512)) {

Therefore, if allocated is true, this check will always fail because offset == start.

Whoops, thanks for catching that. I'll fix it, even though it doesn't seem to have any practicel consequences. VHD is a format braindead enough that you never get two contiguous blocks anyway. There's always a bitmap in between.

Quoted 9 lines

block/vpc.c static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num, · line 635

Quoted 6 lines
block/vpc.c +6 -0
+            break;
+        }
+
+        /* All sectors in a block are contiguous (without using the bitmap) */
+        n = ROUND_UP(sector_num + 1, s->block_size / 512) - sector_num;
+        n = MIN(n, nb_sectors);

I think you can make use of this assumption even before calling get_sector_offset() in the first iteration.

Hm... Maybe I need to make it a do-while loop to get rid of some duplication with the first iteration.

Quoted 3 lines

Also, 512 is shorter than BDRV_SECTOR_SIZE and it's nice to get it on a single line, but I think not introducing another literal 512 in the code would be nicer; the same applies above in "*pnum * 512".

Fair enough.

Kevin