Skip to contentSkip to conversation

Fix conversion from ISO to VMDK streamOptimized

PATCH Superseded Fam Zheng 2 participants 12 messages Nov 13, 2013 – Nov 14, 2013 Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

cover Fix conversion from ISO to VMDK streamOptimized

Previouly, "qemu-img convert" from ISO to VMDK with subformat=streamOptimized fails:

    $ ./qemu-img convert -O vmdk -o subformat=streamOptimized foo.iso bar.vmdk
    VMDK: can't write to allocated cluster for streamOptimized
    qemu-img: error while writing sector 64: Input/output error

Because current code in qemu-img.c uses the normal convert loop, rather than the compressed == true loop, which write by cluster size. In VMDK streamOptimized, writes must be in cluster unit, because overlapped write to an allocated cluster is not supported.

This series adds an is_compressed field in BlockDriverInfo, and use compressed convertion loop if the block driver set this field to true.

Implement .bdrv_get_info and .bdrv_write_compressed in VMDK driver to fit into this framework.

Fam Zheng (5):
qemu-img: Convert by cluster size if target is compressed
vmdk: Implement .bdrv_write_compressed
block: Change BlockDriverInfo.cluster_size to 64 bits
vmdk: Implement .bdrv_get_info()
mirror: Check for bdrv_get_info result

 block/mirror.c             |  4 ++--
 block/vmdk.c               | 33 +++++++++++++++++++++++++++++++++
 include/block/block.h      |  3 ++-
 qemu-img.c                 |  8 ++++++--
 tests/qemu-iotests/059.out |  1 +
 5 files changed, 44 insertions(+), 5 deletions(-)
Signature · 1 line
1.8.4.2
author
#1 lore raw

PATCH 1/5 qemu-img: Convert by cluster size if target is compressed

If target block driver forces compression, qemu-img convert needs to write by cluster size as well as "-c" option.

Particularly, this applies for converting to VMDK streamOptimized format.

---
 include/block/block.h | 1 +
 qemu-img.c            | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
include/block/block.h +1 -0
index 3560deb..169c092 100644
@@ -18,6 +18,7 @@ typedef struct BlockDriverInfo {
     /* offset at which the VM state can be saved (0 if not possible) */
     int64_t vm_state_offset;
     bool is_dirty;
+    bool is_compressed;
 } BlockDriverInfo;
 
 typedef struct BlockFragInfo {
qemu-img.c +4 -1
index bf3fb4f..09ed9b2 100644
@@ -1383,8 +1383,11 @@ static int img_convert(int argc, char **argv)
         }
     }
 
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret == 0) {
+        compress = compress || bdi.is_compressed;
+    }
     if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
         if (ret < 0) {
             error_report("could not get block driver info");
             goto out;
Signature · 1 line
1.8.4.2
#2 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-img: Convert by cluster size if target is compressed

Quoted 31 lines · Fam Zheng

If target block driver forces compression, qemu-img convert needs to write by cluster size as well as "-c" option.

Particularly, this applies for converting to VMDK streamOptimized format.

---
 include/block/block.h | 1 +
 qemu-img.c            | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
include/block/block.h +1 -0
index 3560deb..169c092 100644
@@ -18,6 +18,7 @@ typedef struct BlockDriverInfo {
     /* offset at which the VM state can be saved (0 if not possible) */
     int64_t vm_state_offset;
     bool is_dirty;
+    bool is_compressed;
 } BlockDriverInfo;
 
 typedef struct BlockFragInfo {
qemu-img.c +0 -0
index bf3fb4f..09ed9b2 100644
@@ -1383,8 +1383,11 @@ static int img_convert(int argc, char **argv)
         }
     }

qemu-img.c static int img_convert(int argc, char **argv) · line 1393

Quoted 10 lines
qemu-img.c +4 -1
 
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret == 0) {
+        compress = compress || bdi.is_compressed;
+    }
     if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
         if (ret < 0) {
             error_report("could not get block driver info");
             goto out;

You need to move the error checking up as well, above the 'if (compress)'

Jeff

author
#3 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-img: Convert by cluster size if target is compressed

Quoted 33 lines · Jeff Cody
Quoted 32 lines · Fam Zheng

If target block driver forces compression, qemu-img convert needs to write by cluster size as well as "-c" option.

Particularly, this applies for converting to VMDK streamOptimized format.

---
  include/block/block.h | 1 +
  qemu-img.c            | 5 ++++-
  2 files changed, 5 insertions(+), 1 deletion(-)
include/block/block.h +1 -0
index 3560deb..169c092 100644
@@ -18,6 +18,7 @@ typedef struct BlockDriverInfo {
      /* offset at which the VM state can be saved (0 if not possible) */
      int64_t vm_state_offset;
      bool is_dirty;
+    bool is_compressed;
  } BlockDriverInfo;
 
  typedef struct BlockFragInfo {
qemu-img.c +0 -0
index bf3fb4f..09ed9b2 100644
@@ -1383,8 +1383,11 @@ static int img_convert(int argc, char **argv)
          }
      }
 
Quoted 13 lines

qemu-img.c static int img_convert(int argc, char **argv) · line 1393

Quoted 9 lines
qemu-img.c +4 -1
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret == 0) {
+        compress = compress || bdi.is_compressed;
+    }
      if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
          if (ret < 0) {
              error_report("could not get block driver info");
              goto out;

You need to move the error checking up as well, above the 'if (compress)'

Moving it up forces bdrv_get_info to succeed for both compress case and non compress case, which is too strict and fails when target driver doesn't support it.

I see it's just odd to assign ret outside if block and check it in only one branch, but I don't have a better way to do this here.

Fam

#4 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-img: Convert by cluster size if target is compressed

Quoted 48 lines · Fam Zheng
Quoted 33 lines · Jeff Cody
Quoted 32 lines · Fam Zheng

If target block driver forces compression, qemu-img convert needs to write by cluster size as well as "-c" option.

Particularly, this applies for converting to VMDK streamOptimized format.

---
 include/block/block.h | 1 +
 qemu-img.c            | 5 ++++-
 2 files changed, 5 insertions(+), 1 deletion(-)
include/block/block.h +1 -0
index 3560deb..169c092 100644
@@ -18,6 +18,7 @@ typedef struct BlockDriverInfo {
     /* offset at which the VM state can be saved (0 if not possible) */
     int64_t vm_state_offset;
     bool is_dirty;
+    bool is_compressed;
 } BlockDriverInfo;
 
 typedef struct BlockFragInfo {
qemu-img.c +0 -0
index bf3fb4f..09ed9b2 100644
@@ -1383,8 +1383,11 @@ static int img_convert(int argc, char **argv)
         }
     }
 
Quoted 13 lines

qemu-img.c static int img_convert(int argc, char **argv) · line 1393

Quoted 9 lines
qemu-img.c +4 -1
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret == 0) {
+        compress = compress || bdi.is_compressed;
+    }
     if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
         if (ret < 0) {
             error_report("could not get block driver info");
             goto out;

You need to move the error checking up as well, above the 'if (compress)'

Quoted 3 lines

Moving it up forces bdrv_get_info to succeed for both compress case and non compress case, which is too strict and fails when target driver doesn't support it.

bdrv_get_info() will return -ENOTSUP if it is not supported. If we do call down to the driver layer bdrv_get_info() and get an error, I think we should pass it up. So just ignore -ENOTSUP if we don't care about the case when bdrv_get_info() is not supported by a target.

Quoted 6 lines

I see it's just odd to assign ret outside if block and check it in only one branch, but I don't have a better way to do this here.

Fam

author
#5 lore raw

Re: [Qemu-devel] [PATCH 1/5] qemu-img: Convert by cluster size if target is compressed

Quoted 49 lines · Jeff Cody
Quoted 48 lines · Fam Zheng
Quoted 46 lines · Jeff Cody
Quoted 31 lines · Fam Zheng

If target block driver forces compression, qemu-img convert needs to write by cluster size as well as "-c" option.

Particularly, this applies for converting to VMDK streamOptimized format.

---
  include/block/block.h | 1 +
  qemu-img.c            | 5 ++++-
  2 files changed, 5 insertions(+), 1 deletion(-)
include/block/block.h +1 -0
index 3560deb..169c092 100644
@@ -18,6 +18,7 @@ typedef struct BlockDriverInfo {
      /* offset at which the VM state can be saved (0 if not possible) */
      int64_t vm_state_offset;
      bool is_dirty;
+    bool is_compressed;
  } BlockDriverInfo;
 
  typedef struct BlockFragInfo {
qemu-img.c +0 -0
index bf3fb4f..09ed9b2 100644
@@ -1383,8 +1383,11 @@ static int img_convert(int argc, char **argv)
          }
      }

qemu-img.c static int img_convert(int argc, char **argv) · line 1393

Quoted 10 lines
qemu-img.c +4 -1
 
+    ret = bdrv_get_info(out_bs, &bdi);
+    if (ret == 0) {
+        compress = compress || bdi.is_compressed;
+    }
      if (compress) {
-        ret = bdrv_get_info(out_bs, &bdi);
          if (ret < 0) {
              error_report("could not get block driver info");
              goto out;

You need to move the error checking up as well, above the 'if (compress)'

Quoted 9 lines
Quoted 3 lines

Moving it up forces bdrv_get_info to succeed for both compress case and non compress case, which is too strict and fails when target driver doesn't support it.

bdrv_get_info() will return -ENOTSUP if it is not supported. If we do call down to the driver layer bdrv_get_info() and get an error, I think we should pass it up. So just ignore -ENOTSUP if we don't care about the case when bdrv_get_info() is not supported by a target.

We care for -ENOTSUP as well as other err code in the compressed case, and don't care the call to bdrv_get_info in the other case. I get your point, I'll add "else if (ret != -ENOTSUP) {...}" below the call, and keep the check of ret in "if (compressed)" unchanged.

Fam

author
#6 lore raw

PATCH 2/5 vmdk: Implement .bdrv_write_compressed

In reply to Fam Zheng

Add a wrapper function to support "compressed" path in qemu-img convert. Only support streamOptimized subformat case for now (num_extents == 1 and extent compression is true).

---
 block/vmdk.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
block/vmdk.c +14 -0
index a7ebd0f..f439206 100644
@@ -1417,6 +1417,19 @@ static coroutine_fn int vmdk_co_write(BlockDriverState *bs, int64_t sector_num,
     return ret;
 }
 
+static int vmdk_write_compressed(BlockDriverState *bs,
+                                 int64_t sector_num,
+                                 const uint8_t *buf,
+                                 int nb_sectors)
+{
+    BDRVVmdkState *s = bs->opaque;
+    if (s->num_extents == 1 && s->extents[0].compressed) {
+        return vmdk_write(bs, sector_num, buf, nb_sectors, false, false);
+    } else {
+        return -ENOTSUP;
+    }
+}
+
 static int coroutine_fn vmdk_co_write_zeroes(BlockDriverState *bs,
                                              int64_t sector_num,
                                              int nb_sectors)
@@ -1937,6 +1950,7 @@ static BlockDriver bdrv_vmdk = {
     .bdrv_reopen_prepare          = vmdk_reopen_prepare,
     .bdrv_read                    = vmdk_co_read,
     .bdrv_write                   = vmdk_co_write,
+    .bdrv_write_compressed        = vmdk_write_compressed,
     .bdrv_co_write_zeroes         = vmdk_co_write_zeroes,
     .bdrv_close                   = vmdk_close,
     .bdrv_create                  = vmdk_create,
Signature · 1 line
1.8.4.2
author
#7 lore raw

PATCH 3/5 block: Change BlockDriverInfo.cluster_size to 64 bits

In reply to Fam Zheng

VMDK could have big cluster_size for monolithicFlat. It implements .bdrv_get_info now, a 32 bit field is likely to overflow.

---
 include/block/block.h | 2 +-
 qemu-img.c            | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
include/block/block.h +1 -1
index 169c092..7059bb4 100644
@@ -14,7 +14,7 @@ typedef struct BlockJob BlockJob;
 
 typedef struct BlockDriverInfo {
     /* in bytes, 0 if irrelevant */
-    int cluster_size;
+    int64_t cluster_size;
     /* offset at which the VM state can be saved (0 if not possible) */
     int64_t vm_state_offset;
     bool is_dirty;
qemu-img.c +2 -1
index 09ed9b2..6ca19ff 100644
@@ -1121,8 +1121,9 @@ out3:
 
 static int img_convert(int argc, char **argv)
 {
-    int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size,
+    int c, ret = 0, n, n1, bs_n, bs_i, compress,
         cluster_sectors, skip_create;
+    int64_t cluster_size;
     int progress = 0, flags;
     const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
     BlockDriver *drv, *proto_drv;
Signature · 1 line
1.8.4.2
author
#8 lore raw

PATCH 4/5 vmdk: Implement .bdrv_get_info()

In reply to Fam Zheng

This will return cluster_size and is_compressed to caller, if all the extents has the same value (or there's only one extent). Otherwise return -ENOTSUP.

---
 block/vmdk.c               | 19 +++++++++++++++++++
 tests/qemu-iotests/059.out |  1 +
 2 files changed, 20 insertions(+)
block/vmdk.c +19 -0
index f439206..07f77d8 100644
@@ -1905,6 +1905,24 @@ static ImageInfoSpecific *vmdk_get_specific_info(BlockDriverState *bs)
     return spec_info;
 }
 
+static int vmdk_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
+{
+    int i;
+    BDRVVmdkState *s = bs->opaque;
+    assert(s->num_extents);
+    bdi->is_compressed = s->extents[0].compressed;
+    bdi->cluster_size = s->extents[0].cluster_sectors << BDRV_SECTOR_BITS;
+    /* See if we have multiple extents but they have different cases */
+    for (i = 1; i < s->num_extents; i++) {
+        if (bdi->is_compressed != s->extents[i].compressed ||
+            bdi->cluster_size !=
+                s->extents[i].cluster_sectors << BDRV_SECTOR_BITS) {
+            return -ENOTSUP;
+        }
+    }
+    return 0;
+}
+
 static QEMUOptionParameter vmdk_create_options[] = {
     {
         .name = BLOCK_OPT_SIZE,
@@ -1959,6 +1977,7 @@ static BlockDriver bdrv_vmdk = {
     .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size,
     .bdrv_has_zero_init           = vmdk_has_zero_init,
     .bdrv_get_specific_info       = vmdk_get_specific_info,
+    .bdrv_get_info                = vmdk_get_info,
 
     .create_options               = vmdk_create_options,
 };
tests/qemu-iotests/059.out +1 -0
index 2ded8a9..8394d14 100644
@@ -21,6 +21,7 @@ Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=2147483648
 image: TEST_DIR/t.IMGFMT
 file format: IMGFMT
 virtual size: 2.0G (2147483648 bytes)
+cluster_size: 2147483648
 
 === Testing monolithicFlat with zeroed_grain ===
 qemu-img: TEST_DIR/t.IMGFMT: Flat image can't enable zeroed grain
Signature · 1 line
1.8.4.2
author
#9 lore raw

PATCH 5/5 mirror: Check for bdrv_get_info result

In reply to Fam Zheng

bdrv_get_info could fail. Add check before using the returned value.

---
 block/mirror.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/mirror.c +2 -2
index 7b95acf..c0c321b 100644
@@ -320,8 +320,8 @@ static void coroutine_fn mirror_run(void *opaque)
     bdrv_get_backing_filename(s->target, backing_filename,
                               sizeof(backing_filename));
     if (backing_filename[0] && !s->target->backing_hd) {
-        bdrv_get_info(s->target, &bdi);
-        if (s->granularity < bdi.cluster_size) {
+        ret = bdrv_get_info(s->target, &bdi);
+        if (ret == 0 && s->granularity < bdi.cluster_size) {
             s->buf_size = MAX(s->buf_size, bdi.cluster_size);
             s->cow_bitmap = bitmap_new(length);
         }
Signature · 1 line
1.8.4.2
#10 lore raw

Re: [Qemu-devel] [PATCH 5/5] mirror: Check for bdrv_get_info result

Quoted 12 lines · Fam Zheng

bdrv_get_info could fail. Add check before using the returned value.

---
 block/mirror.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/mirror.c +0 -0
index 7b95acf..c0c321b 100644

block/mirror.c static void coroutine_fn mirror_run(void *opaque) · line 324

Quoted 7 lines
block/mirror.c +2 -2
@@ -320,8 +320,8 @@ static void coroutine_fn mirror_run(void *opaque)
     bdrv_get_backing_filename(s->target, backing_filename,
                               sizeof(backing_filename));
     if (backing_filename[0] && !s->target->backing_hd) {
-        bdrv_get_info(s->target, &bdi);
-        if (s->granularity < bdi.cluster_size) {
+        ret = bdrv_get_info(s->target, &bdi);
+        if (ret == 0 && s->granularity < bdi.cluster_size) {

For ret < 0, I think we should exit on error (at least in cases where ret < 0 && ret != -ENOTSUP).

Quoted 3 lines
block/mirror.c +0 -0
             s->buf_size = MAX(s->buf_size, bdi.cluster_size);
             s->cow_bitmap = bitmap_new(length);
         }
author
#11 lore raw

Re: [Qemu-devel] [PATCH 5/5] mirror: Check for bdrv_get_info result

Quoted 8 lines · Jeff Cody
Quoted 7 lines · Fam Zheng

bdrv_get_info could fail. Add check before using the returned value.

---
  block/mirror.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 16 lines

block/mirror.c static void coroutine_fn mirror_run(void *opaque) · line 324

Quoted 12 lines
block/mirror.c +2 -2
index 7b95acf..c0c321b 100644
@@ -320,8 +320,8 @@ static void coroutine_fn mirror_run(void *opaque)
      bdrv_get_backing_filename(s->target, backing_filename,
                                sizeof(backing_filename));
      if (backing_filename[0] && !s->target->backing_hd) {
-        bdrv_get_info(s->target, &bdi);
-        if (s->granularity < bdi.cluster_size) {
+        ret = bdrv_get_info(s->target, &bdi);
+        if (ret == 0 && s->granularity < bdi.cluster_size) {

For ret < 0, I think we should exit on error (at least in cases where ret < 0 && ret != -ENOTSUP).

We only need the cluster_size here, which is optional to set up operation granularity. Exiting the block job because bdrv_get_info fails (which is not a critical error) is worse in fault tolerance sense, so I'd like to keep this way.

Paolo, any ideas?

Fam