Patchwork [2/2] vmdk: Read footer for streamOptimized images

login
register
mail settings
Submitter Kevin Wolf
Date Aug. 16, 2012, 8:54 a.m.
Message ID <1345107271-10154-3-git-send-email-kwolf@redhat.com>
Download mbox | patch
Permalink /patch/177921/
State New
Headers show

Comments

Kevin Wolf - Aug. 16, 2012, 8:54 a.m.
The footer takes precedence over the header when it exists. It contains
the real grain directory offset that is missing in the header. Without
this patch, streamOptimized images with a footer cannot be read.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/vmdk.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
Stefan Hajnoczi - Aug. 16, 2012, 9:17 a.m.
On Thu, Aug 16, 2012 at 9:54 AM, Kevin Wolf <kwolf@redhat.com> wrote:
> The footer takes precedence over the header when it exists. It contains
> the real grain directory offset that is missing in the header. Without
> this patch, streamOptimized images with a footer cannot be read.
>
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/vmdk.c |   16 ++++++++++++++++
>  1 files changed, 16 insertions(+), 0 deletions(-)
>
> diff --git a/block/vmdk.c b/block/vmdk.c
> index 9648398..c243a96 100644
> --- a/block/vmdk.c
> +++ b/block/vmdk.c
> @@ -35,6 +35,7 @@
>  #define VMDK4_FLAG_RGD (1 << 1)
>  #define VMDK4_FLAG_COMPRESS (1 << 16)
>  #define VMDK4_FLAG_MARKER (1 << 17)
> +#define VMDK4_GD_AT_END 0xffffffffffffffffULL
>
>  typedef struct {
>      uint32_t version;
> @@ -451,6 +452,21 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
>      if (header.capacity == 0 && header.desc_offset) {
>          return vmdk_open_desc_file(bs, flags, header.desc_offset << 9);
>      }
> +
> +    if (header.gd_offset == VMDK4_GD_AT_END) {
> +        /*
> +         * The footer takes precedence over the header, so read it in. The
> +         * footer starts at offset -1024 from the end: One sector for the
> +         * footer, and another one for the end-of-stream marker.
> +         */
> +        ret = bdrv_pread(file,
> +            bs->file->total_sectors * 512 - 1024 + sizeof(magic),
> +            &header, sizeof(header));
> +        if (ret < 0) {
> +            return ret;
> +        }
> +    }
> +
>      l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gte)
>                          * le64_to_cpu(header.granularity);
>      if (l1_entry_sectors == 0) {

I think we should check the magic number or marker before trusting the
contents of the footer.

Stefan

Patch

diff --git a/block/vmdk.c b/block/vmdk.c
index 9648398..c243a96 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -35,6 +35,7 @@ 
 #define VMDK4_FLAG_RGD (1 << 1)
 #define VMDK4_FLAG_COMPRESS (1 << 16)
 #define VMDK4_FLAG_MARKER (1 << 17)
+#define VMDK4_GD_AT_END 0xffffffffffffffffULL
 
 typedef struct {
     uint32_t version;
@@ -451,6 +452,21 @@  static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (header.capacity == 0 && header.desc_offset) {
         return vmdk_open_desc_file(bs, flags, header.desc_offset << 9);
     }
+
+    if (header.gd_offset == VMDK4_GD_AT_END) {
+        /*
+         * The footer takes precedence over the header, so read it in. The
+         * footer starts at offset -1024 from the end: One sector for the
+         * footer, and another one for the end-of-stream marker.
+         */
+        ret = bdrv_pread(file,
+            bs->file->total_sectors * 512 - 1024 + sizeof(magic),
+            &header, sizeof(header));
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
     l1_entry_sectors = le32_to_cpu(header.num_gtes_per_gte)
                         * le64_to_cpu(header.granularity);
     if (l1_entry_sectors == 0) {