Skip to contentSkip to conversation

vmdk: improve streamOptimized vmdk support

PATCH Superseded Milos Vyletel 4 participants 21 messages Jul 7, 2014 – Aug 29, 2014 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vmdk: improve streamOptimized vmdk support

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
block/vmdk.c +281 -82
index 27a78da..f482225 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,24 +104,29 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
     uint32_t l1_entry_sectors;
 
     unsigned int l2_size;
+    uint32_t *l2_table;
     uint32_t *l2_cache;
     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -555,14 +575,51 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM)
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +650,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +667,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +715,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR)
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -1000,6 +1035,11 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed)
+        return VMDK_OK;
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1026,6 +1066,97 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                           VMDK4Footer *footer,
+                           VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, &gd_marker, sizeof(gd_marker));
+    if (ret < 0)
+        goto exit;
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0)
+        goto exit;
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0)
+        goto exit;
+
+    ret = 0;
+ exit:
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int ret;
+    VMDK4MetaMarker gtm;
+    uint64_t offset;
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    extent->l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1034,8 +1165,8 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
-    uint32_t min_count, *l2_table;
+    int min_index, i, j, ret;
+    uint32_t min_count;
     bool zeroed = false;
 
     if (m_data) {
@@ -1048,11 +1179,25 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
-    if (l1_index >= extent->l1_size) {
+    if (extent->compressed && l1_index &&
+            extent->l1_index != l1_index) {
+        ret = vmdk_write_grain_table(extent);
+        if (ret < 0)
+            return ret;
+    }
+
+    extent->l1_index = l1_index;
+    if (extent->l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
-    l2_offset = extent->l1_table[l1_index];
+ retry:
+    l2_offset = extent->l1_table[extent->l1_index];
+
     if (!l2_offset) {
+        if (extent->compressed) {
+            extent->l1_table[extent->l1_index] = bdrv_getlength(extent->file);
+            goto retry;
+        }
         return VMDK_UNALLOC;
     }
     for (i = 0; i < L2_CACHE_SIZE; i++) {
@@ -1063,7 +1208,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                     extent->l2_cache_counts[j] >>= 1;
                 }
             }
-            l2_table = extent->l2_cache + (i * extent->l2_size);
+            extent->l2_table = extent->l2_cache + (i * extent->l2_size);
             goto found;
         }
     }
@@ -1076,11 +1221,11 @@ static int get_cluster_offset(BlockDriverState *bs,
             min_index = i;
         }
     }
-    l2_table = extent->l2_cache + (min_index * extent->l2_size);
+    extent->l2_table = extent->l2_cache + (min_index * extent->l2_size);
     if (bdrv_pread(
                 extent->file,
                 (int64_t)l2_offset * 512,
-                l2_table,
+                extent->l2_table,
                 extent->l2_size * sizeof(uint32_t)
             ) != extent->l2_size * sizeof(uint32_t)) {
         return VMDK_ERROR;
@@ -1090,15 +1235,15 @@ static int get_cluster_offset(BlockDriverState *bs,
     extent->l2_cache_counts[min_index] = 1;
  found:
     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
-    *cluster_offset = le32_to_cpu(l2_table[l2_index]);
+    *cluster_offset = le32_to_cpu(extent->l2_table[l2_index]);
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
+        m_data->l1_index = extent->l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
-        m_data->l2_cache_entry = &l2_table[l2_index];
+        m_data->l2_cache_entry = &extent->l2_table[l2_index];
     }
     if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
         zeroed = true;
@@ -1119,7 +1264,7 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         *cluster_offset >>= 9;
-        l2_table[l2_index] = cpu_to_le32(*cluster_offset);
+        extent->l2_table[l2_index] = cpu_to_le32(*cluster_offset);
 
         /* First of all we write grain itself, to avoid race condition
          * that may to corrupt the image.
@@ -1210,6 +1355,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1227,6 +1373,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         data->size = buf_len;
         write_buf = (uint8_t *)data;
         write_len = buf_len + sizeof(VmdkGrainMarker);
+
     }
     ret = bdrv_pwrite(extent->file,
                         cluster_offset + offset_in_cluster,
@@ -1236,6 +1383,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1534,10 +1688,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1562,28 +1718,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1620,30 +1786,54 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1914,6 +2104,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#1 lore raw
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Quoted 586 lines
block/vmdk.c +281 -82
index 27a78da..f482225 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,24 +104,29 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
     uint32_t l1_entry_sectors;
 
     unsigned int l2_size;
+    uint32_t *l2_table;
     uint32_t *l2_cache;
     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -555,14 +575,51 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM)
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +650,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +667,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +715,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR)
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -1000,6 +1035,11 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed)
+        return VMDK_OK;
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1026,6 +1066,97 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                           VMDK4Footer *footer,
+                           VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, &gd_marker, sizeof(gd_marker));
+    if (ret < 0)
+        goto exit;
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0)
+        goto exit;
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0)
+        goto exit;
+
+    ret = 0;
+ exit:
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int ret;
+    VMDK4MetaMarker gtm;
+    uint64_t offset;
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    extent->l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1034,8 +1165,8 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
-    uint32_t min_count, *l2_table;
+    int min_index, i, j, ret;
+    uint32_t min_count;
     bool zeroed = false;
 
     if (m_data) {
@@ -1048,11 +1179,25 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
-    if (l1_index >= extent->l1_size) {
+    if (extent->compressed && l1_index &&
+            extent->l1_index != l1_index) {
+        ret = vmdk_write_grain_table(extent);
+        if (ret < 0)
+            return ret;
+    }
+
+    extent->l1_index = l1_index;
+    if (extent->l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
-    l2_offset = extent->l1_table[l1_index];
+ retry:
+    l2_offset = extent->l1_table[extent->l1_index];
+
     if (!l2_offset) {
+        if (extent->compressed) {
+            extent->l1_table[extent->l1_index] = bdrv_getlength(extent->file);
+            goto retry;
+        }
         return VMDK_UNALLOC;
     }
     for (i = 0; i < L2_CACHE_SIZE; i++) {
@@ -1063,7 +1208,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                     extent->l2_cache_counts[j] >>= 1;
                 }
             }
-            l2_table = extent->l2_cache + (i * extent->l2_size);
+            extent->l2_table = extent->l2_cache + (i * extent->l2_size);
             goto found;
         }
     }
@@ -1076,11 +1221,11 @@ static int get_cluster_offset(BlockDriverState *bs,
             min_index = i;
         }
     }
-    l2_table = extent->l2_cache + (min_index * extent->l2_size);
+    extent->l2_table = extent->l2_cache + (min_index * extent->l2_size);
     if (bdrv_pread(
                 extent->file,
                 (int64_t)l2_offset * 512,
-                l2_table,
+                extent->l2_table,
                 extent->l2_size * sizeof(uint32_t)
             ) != extent->l2_size * sizeof(uint32_t)) {
         return VMDK_ERROR;
@@ -1090,15 +1235,15 @@ static int get_cluster_offset(BlockDriverState *bs,
     extent->l2_cache_counts[min_index] = 1;
  found:
     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
-    *cluster_offset = le32_to_cpu(l2_table[l2_index]);
+    *cluster_offset = le32_to_cpu(extent->l2_table[l2_index]);
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
+        m_data->l1_index = extent->l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
-        m_data->l2_cache_entry = &l2_table[l2_index];
+        m_data->l2_cache_entry = &extent->l2_table[l2_index];
     }
     if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
         zeroed = true;
@@ -1119,7 +1264,7 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         *cluster_offset >>= 9;
-        l2_table[l2_index] = cpu_to_le32(*cluster_offset);
+        extent->l2_table[l2_index] = cpu_to_le32(*cluster_offset);
 
         /* First of all we write grain itself, to avoid race condition
          * that may to corrupt the image.
@@ -1210,6 +1355,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1227,6 +1373,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         data->size = buf_len;
         write_buf = (uint8_t *)data;
         write_len = buf_len + sizeof(VmdkGrainMarker);
+
     }
     ret = bdrv_pwrite(extent->file,
                         cluster_offset + offset_in_cluster,
@@ -1236,6 +1383,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1534,10 +1688,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1562,28 +1718,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1620,30 +1786,54 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1914,6 +2104,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#2 lore raw
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
Quoted 9 lines

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Images created/converted using QEMU were rejected by VMWare ESXi, vCloud, VirtualBox and Oracle OVM. I did not try anything else.

Generally speaking streamOptimized format is not followed by QEMU. Instead regular VMDK format + encryption is used. We can say this is bug fix since streamOptimzed format is already included but does not work well.

I'll check the style and fix the code. I'll postpone resubmit until further review is done since I'm sure there will be things that will need to be fixed.

Milos

Quoted 586 lines
Quoted 586 lines
block/vmdk.c +281 -83
index 27a78da..f482225 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,24 +104,29 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
     uint32_t l1_entry_sectors;
 
     unsigned int l2_size;
+    uint32_t *l2_table;
     uint32_t *l2_cache;
     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -555,14 +575,51 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM)
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +650,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +667,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +715,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR)
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -1000,6 +1035,11 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed)
+        return VMDK_OK;
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1026,6 +1066,97 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                           VMDK4Footer *footer,
+                           VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, &gd_marker, sizeof(gd_marker));
+    if (ret < 0)
+        goto exit;
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0)
+        goto exit;
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0)
+        goto exit;
+
+    ret = 0;
+ exit:
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int ret;
+    VMDK4MetaMarker gtm;
+    uint64_t offset;
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    extent->l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1034,8 +1165,8 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
-    uint32_t min_count, *l2_table;
+    int min_index, i, j, ret;
+    uint32_t min_count;
     bool zeroed = false;
 
     if (m_data) {
@@ -1048,11 +1179,25 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
-    if (l1_index >= extent->l1_size) {
+    if (extent->compressed && l1_index &&
+            extent->l1_index != l1_index) {
+        ret = vmdk_write_grain_table(extent);
+        if (ret < 0)
+            return ret;
+    }
+
+    extent->l1_index = l1_index;
+    if (extent->l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
-    l2_offset = extent->l1_table[l1_index];
+ retry:
+    l2_offset = extent->l1_table[extent->l1_index];
+
     if (!l2_offset) {
+        if (extent->compressed) {
+            extent->l1_table[extent->l1_index] = bdrv_getlength(extent->file);
+            goto retry;
+        }
         return VMDK_UNALLOC;
     }
     for (i = 0; i < L2_CACHE_SIZE; i++) {
@@ -1063,7 +1208,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                     extent->l2_cache_counts[j] >>= 1;
                 }
             }
-            l2_table = extent->l2_cache + (i * extent->l2_size);
+            extent->l2_table = extent->l2_cache + (i * extent->l2_size);
             goto found;
         }
     }
@@ -1076,11 +1221,11 @@ static int get_cluster_offset(BlockDriverState *bs,
             min_index = i;
         }
     }
-    l2_table = extent->l2_cache + (min_index * extent->l2_size);
+    extent->l2_table = extent->l2_cache + (min_index * extent->l2_size);
     if (bdrv_pread(
                 extent->file,
                 (int64_t)l2_offset * 512,
-                l2_table,
+                extent->l2_table,
                 extent->l2_size * sizeof(uint32_t)
             ) != extent->l2_size * sizeof(uint32_t)) {
         return VMDK_ERROR;
@@ -1090,15 +1235,15 @@ static int get_cluster_offset(BlockDriverState *bs,
     extent->l2_cache_counts[min_index] = 1;
  found:
     l2_index = ((offset >> 9) / extent->cluster_sectors) % extent->l2_size;
-    *cluster_offset = le32_to_cpu(l2_table[l2_index]);
+    *cluster_offset = le32_to_cpu(extent->l2_table[l2_index]);
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
+        m_data->l1_index = extent->l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
-        m_data->l2_cache_entry = &l2_table[l2_index];
+        m_data->l2_cache_entry = &extent->l2_table[l2_index];
     }
     if (extent->has_zero_grain && *cluster_offset == VMDK_GTE_ZEROED) {
         zeroed = true;
@@ -1119,7 +1264,7 @@ static int get_cluster_offset(BlockDriverState *bs,
         }
 
         *cluster_offset >>= 9;
-        l2_table[l2_index] = cpu_to_le32(*cluster_offset);
+        extent->l2_table[l2_index] = cpu_to_le32(*cluster_offset);
 
         /* First of all we write grain itself, to avoid race condition
          * that may to corrupt the image.
@@ -1210,6 +1355,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1227,6 +1373,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         data->size = buf_len;
         write_buf = (uint8_t *)data;
         write_len = buf_len + sizeof(VmdkGrainMarker);
+
     }
     ret = bdrv_pwrite(extent->file,
                         cluster_offset + offset_in_cluster,
@@ -1236,6 +1383,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1534,10 +1688,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1562,28 +1718,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1620,30 +1786,54 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1914,6 +2104,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
--

1.7.1

#3 lore raw
Quoted 42 lines · Milos Vyletel
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
Quoted 9 lines

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Images created/converted using QEMU were rejected by VMWare ESXi, vCloud, VirtualBox and Oracle OVM. I did not try anything else.

Quoted 8 lines

Generally speaking streamOptimized format is not followed by QEMU. Instead regular VMDK format + encryption is used. We can say this is bug fix since streamOptimzed format is already included but does not work well.

I'll check the style and fix the code. I'll postpone resubmit until further review is done since I'm sure there will be things that will need to be fixed.

What was the command-line you used?

qemu-img convert -f qcow2 -O vmdk -o subformat=streamOptimized input.qcow2 output.vmdk

Stefan

#4 lore raw
Quoted 52 lines · Stefan Hajnoczi
Quoted 42 lines · Milos Vyletel
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
Quoted 9 lines

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Images created/converted using QEMU were rejected by VMWare ESXi, vCloud, VirtualBox and Oracle OVM. I did not try anything else.

Quoted 8 lines

Generally speaking streamOptimized format is not followed by QEMU. Instead regular VMDK format + encryption is used. We can say this is bug fix since streamOptimzed format is already included but does not work well.

I'll check the style and fix the code. I'll postpone resubmit until further review is done since I'm sure there will be things that will need to be fixed.

Quoted 6 lines

What was the command-line you used?

qemu-img convert -f qcow2 -O vmdk -o subformat=streamOptimized input.qcow2 output.vmdk

Stefan

I've been using test image from
http://wiki.qemu.org/download/linux-0.2.img.bz2 and converting it like this:

qemu-img convert -O vmdk -o adapter_type=lsilogic,compat6,subformat=streamOptimized linux-0.2.img linux-0.2.vmdk

Milos

#5 lore raw
Quoted 60 lines · Milos Vyletel
Quoted 52 lines · Stefan Hajnoczi
Quoted 42 lines · Milos Vyletel
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
Quoted 9 lines

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Images created/converted using QEMU were rejected by VMWare ESXi, vCloud, VirtualBox and Oracle OVM. I did not try anything else.

Quoted 8 lines

Generally speaking streamOptimized format is not followed by QEMU. Instead regular VMDK format + encryption is used. We can say this is bug fix since streamOptimzed format is already included but does not work well.

I'll check the style and fix the code. I'll postpone resubmit until further review is done since I'm sure there will be things that will need to be fixed.

Quoted 6 lines

What was the command-line you used?

qemu-img convert -f qcow2 -O vmdk -o subformat=streamOptimized input.qcow2 output.vmdk

Stefan

Quoted 7 lines

I've been using test image from
http://wiki.qemu.org/download/linux-0.2.img.bz2 and converting it like this:

qemu-img convert -O vmdk -o adapter_type=lsilogic,compat6,subformat=streamOptimized linux-0.2.img linux-0.2.vmdk

Okay, great. I wanted to check that you used subformat=streamOptimized.

If possible, please be more specific in the commit description about what caused ESXi and other software to reject the image and what exactly this patch changes.

Stefan

#6 lore raw

I will update description in v2 with more info.

Quoted 76 lines · Stefan Hajnoczi
Quoted 60 lines · Milos Vyletel
Quoted 52 lines · Stefan Hajnoczi
Quoted 42 lines · Milos Vyletel
Quoted 28 lines · Stefan Hajnoczi
Quoted 22 lines · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

Quoted 4 lines
---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
Quoted 9 lines

What does this patch do beyond what QEMU already supports today?

Is there a particular application that rejected QEMU's streamOptimized images? Is this a bug fix?

Please use scripts/checkpatch.pl to check coding style.

Fam: Please review

Images created/converted using QEMU were rejected by VMWare ESXi, vCloud, VirtualBox and Oracle OVM. I did not try anything else.

Quoted 8 lines

Generally speaking streamOptimized format is not followed by QEMU. Instead regular VMDK format + encryption is used. We can say this is bug fix since streamOptimzed format is already included but does not work well.

I'll check the style and fix the code. I'll postpone resubmit until further review is done since I'm sure there will be things that will need to be fixed.

Quoted 6 lines

What was the command-line you used?

qemu-img convert -f qcow2 -O vmdk -o subformat=streamOptimized input.qcow2 output.vmdk

Stefan

Quoted 7 lines

I've been using test image from
http://wiki.qemu.org/download/linux-0.2.img.bz2 and converting it like this:

qemu-img convert -O vmdk -o adapter_type=lsilogic,compat6,subformat=streamOptimized linux-0.2.img linux-0.2.vmdk

Okay, great. I wanted to check that you used subformat=streamOptimized.

If possible, please be more specific in the commit description about what caused ESXi and other software to reject the image and what exactly this patch changes.

Stefan

#7 lore raw
In reply to Milos Vyletel
Quoted 1 line · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format.

s/that/from/

Quoted 4 lines

L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

s/differs/differ/

Quoted 50 lines

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
block/vmdk.c +15 -0
index 27a78da..f482225 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,24 +104,29 @@ typedef struct VmdkExtent {
     bool compressed;

block/vmdk.c typedef struct VmdkExtent { · line 114

Quoted 10 lines
block/vmdk.c +2 -0
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;

Could you track the allocation staus of grain table with l1_table entry value? For those with value 0, we allocate grain table in file, and update its l1 entry. That way the fields l1_index and l2_table are not necessary here.

Quoted 16 lines
block/vmdk.c +3 -0
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
     uint32_t l1_entry_sectors;
 
     unsigned int l2_size;
+    uint32_t *l2_table;
     uint32_t *l2_cache;
     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;

<snip>

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1071

Quoted 9 lines

typedef struct BDRVVmdkState {

block/vmdk.c +3 -0
@@ -1026,6 +1066,97 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                           VMDK4Footer *footer,
+                           VmdkExtent *extent)

Bad alignment.

Quoted 15 lines
block/vmdk.c +15 -0
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1096

Quoted 10 lines
block/vmdk.c +10 -0
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, &gd_marker, sizeof(gd_marker));
+    if (ret < 0)
+        goto exit;

Always add braces around if body. Again, scripts/checkpatch.pl can help check style issue.

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1101

Quoted 5 lines
block/vmdk.c +5 -0
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);

gd_buf is never freed.

Quoted 23 lines
block/vmdk.c +23 -0
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0)
+        goto exit;
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0)
+        goto exit;
+
+    ret = 0;
+ exit:
+    return ret;
+}

<snip>

Quoted 27 lines

static int get_cluster_offset(BlockDriverState *bs,
VmdkExtent *extent, VmdkMetaData *m_data,

block/vmdk.c +10 -3
@@ -1034,8 +1165,8 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
-    uint32_t min_count, *l2_table;
+    int min_index, i, j, ret;
+    uint32_t min_count;
     bool zeroed = false;
 
     if (m_data) {
@@ -1048,11 +1179,25 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
-    if (l1_index >= extent->l1_size) {
+    if (extent->compressed && l1_index &&
+            extent->l1_index != l1_index) {
+        ret = vmdk_write_grain_table(extent);
+        if (ret < 0)
+            return ret;
+    }
+
+    extent->l1_index = l1_index;

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1198

Quoted 10 lines
block/vmdk.c +6 -1
+    if (extent->l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
-    l2_offset = extent->l1_table[l1_index];
+ retry:
+    l2_offset = extent->l1_table[extent->l1_index];
+
     if (!l2_offset) {
+        if (extent->compressed) {
+            extent->l1_table[extent->l1_index] = bdrv_getlength(extent->file);

This control flow desn't make sense. We just write a new grain table at the end of file and update the l1_table entry.

Quoted 5 lines
block/vmdk.c +2 -0
+            goto retry;
+        }
         return VMDK_UNALLOC;
     }
     for (i = 0; i < L2_CACHE_SIZE; i++) {
#8 lore raw
Quoted 68 lines · Fam Zheng
Quoted 1 line · Milos Vyletel

VMDK's streamOptimized format is different that regular sparse format.

s/that/from/

Quoted 4 lines

L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differs.

s/differs/differ/

Quoted 56 lines

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

This patch improves streamOptimized support and adds possibility to create true streamOptimized images using qemu-img. Some of the changes are from VMDK specs, some of them from hexdump-ing images from VMWare and VirtualBox.

I have compared these images to the ones generated by VMWare and vbox and they are identical with the exception of DescriptorFile that has some differences but none that would change behavior(CID and some additional DDB entries differ) and streamOptimized image generated from raw image was succesfully imported (as OVA) into VMWare ESXi and Oracle OVM.

---
 block/vmdk.c |  363 +++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 281 insertions(+), 82 deletions(-)
block/vmdk.c +16 -0
index 27a78da..f482225 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,24 +104,29 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
Quoted 8 lines

block/vmdk.c typedef struct VmdkExtent { · line 114

Quoted 4 lines
block/vmdk.c +1 -0
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;

Could you track the allocation staus of grain table with l1_table entry value? For those with value 0, we allocate grain table in file, and update its l1 entry. That way the fields l1_index and l2_table are not necessary here.

I possibly could and I'm trying that now. I can use VmdkMetaData structure that already has these values and leave VmdkExtent as it is.

Quoted 127 lines
Quoted 17 lines
block/vmdk.c +3 -0
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
     uint32_t l1_entry_sectors;
 
     unsigned int l2_size;
+    uint32_t *l2_table;
     uint32_t *l2_cache;
     uint32_t l2_cache_offsets[L2_CACHE_SIZE];
     uint32_t l2_cache_counts[L2_CACHE_SIZE];
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;

<snip>

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1071

Quoted 9 lines

typedef struct BDRVVmdkState {

block/vmdk.c +3 -0
@@ -1026,6 +1066,97 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                           VMDK4Footer *footer,
+                           VmdkExtent *extent)

Bad alignment.

Quoted 15 lines
block/vmdk.c +15 -0
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1096

Quoted 10 lines
block/vmdk.c +10 -0
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, &gd_marker, sizeof(gd_marker));
+    if (ret < 0)
+        goto exit;

Always add braces around if body. Again, scripts/checkpatch.pl can help check style issue.

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1101

Quoted 5 lines
block/vmdk.c +5 -0
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);

gd_buf is never freed.

Quoted 23 lines
block/vmdk.c +23 -0
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0)
+        goto exit;
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0)
+        goto exit;
+
+    ret = 0;
+ exit:
+    return ret;
+}

<snip>

Quoted 32 lines

static int get_cluster_offset(BlockDriverState *bs,
VmdkExtent *extent, VmdkMetaData *m_data,

block/vmdk.c +12 -4
@@ -1034,8 +1165,8 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
-    uint32_t min_count, *l2_table;
+    int min_index, i, j, ret;
+    uint32_t min_count;
     bool zeroed = false;
 
     if (m_data) {
@@ -1048,11 +1179,25 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
-    if (l1_index >= extent->l1_size) {
+    if (extent->compressed && l1_index &&
+            extent->l1_index != l1_index) {
+        ret = vmdk_write_grain_table(extent);
+        if (ret < 0)
+            return ret;
+    }
+
+    extent->l1_index = l1_index;
+    if (extent->l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
-    l2_offset = extent->l1_table[l1_index];
+ retry:
Quoted 14 lines

block/vmdk.c static int get_cluster_offset(BlockDriverState *bs, · line 1198

Quoted 5 lines
block/vmdk.c +4 -0
+    l2_offset = extent->l1_table[extent->l1_index];
+
     if (!l2_offset) {
+        if (extent->compressed) {
+            extent->l1_table[extent->l1_index] = bdrv_getlength(extent->file);

This control flow desn't make sense. We just write a new grain table at the end of file and update the l1_table entry.

Quoted 5 lines
block/vmdk.c +2 -0
+            goto retry;
+        }
         return VMDK_UNALLOC;
     }
     for (i = 0; i < L2_CACHE_SIZE; i++) {

Thanks for the comments. I'm working on them and I hopefully have v2 ready tomorrow.

#9 lore raw

PATCH v2 1/1 vmdk: improve streamOptimized vmdk support

In reply to Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

 block/vmdk.c |  365 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 290 insertions(+), 75 deletions(-)
block/vmdk.c +290 -75
index 0517bba..ecaca8f 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -107,6 +124,8 @@ typedef struct VmdkExtent {
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -125,7 +144,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +573,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +647,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +664,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +712,21 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1031,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1047,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1063,113 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                             VMDK4Footer *footer,
+                             VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = 0;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i, ret;
+    uint64_t offset;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1178,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1192,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index]) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1254,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1208,6 +1369,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1234,6 +1396,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1701,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1731,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1799,55 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1912,6 +2118,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#10 lore raw
Quoted 6 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

Quoted 6 lines

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

Does putting a monolithicSparse into the OVA work in this case?

Quoted 69 lines

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

 block/vmdk.c |  365 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 290 insertions(+), 75 deletions(-)
block/vmdk.c +17 -0
index 0517bba..ecaca8f 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;

block/vmdk.c typedef struct VmdkExtent { · line 128

Quoted 5 lines
block/vmdk.c +2 -0
@@ -107,6 +124,8 @@ typedef struct VmdkExtent {
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;

This adds hunderds of bytes into the memory structure, but most are unnecessary. I'm dubious about that. Fields in the footer all have counterparts in header, which are as well fields of VmdkExtent. So please reuse them instead of introducing redundancy.

When we close the extent, we could reuse the same logic as in vmdk_create_extent, to generate footer dynamically, instead of writting what is read when closing. That way footer here is not needed.

Quoted 140 lines
block/vmdk.c +49 -41
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -125,7 +144,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +573,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +647,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +664,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +712,21 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }

block/vmdk.c static int vmdk_open_vmdk4(BlockDriverState *bs, · line 728

Quoted 10 lines
block/vmdk.c +5 -0
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);

Looks dangerous. What if has_footer == false, as for the imaged created from older implementation?

Quoted 360 lines
block/vmdk.c +217 -34
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1031,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1047,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1063,113 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                             VMDK4Footer *footer,
+                             VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = 0;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i, ret;
+    uint64_t offset;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1178,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1192,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index]) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1254,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1208,6 +1369,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1234,6 +1396,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1701,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1731,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1799,55 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1912,6 +2118,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#11 lore raw
Quoted 7 lines · Fam Zheng
Quoted 6 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

Quoted 8 lines
Quoted 6 lines

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

Does putting a monolithicSparse into the OVA work in this case?

It does not. I did not try to import it to OVM but ESXi server complained about the format of vmdk in OVA archive (don't have exact error message anymore). I did look in OVF specifications and it does not state that the vmdk must be streamOptimized but it looks like VMWare is enforcing it.

I can try to create OVA with monolithicSparse image and retry if you want to know exact error I got.

Quoted 81 lines
Quoted 70 lines

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

 block/vmdk.c |  365 ++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 290 insertions(+), 75 deletions(-)
block/vmdk.c +17 -0
index 0517bba..ecaca8f 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;

block/vmdk.c typedef struct VmdkExtent { · line 128

Quoted 5 lines
block/vmdk.c +2 -0
@@ -107,6 +124,8 @@ typedef struct VmdkExtent {
 
     int64_t cluster_sectors;
     char *type;
+
+    VMDK4Footer footer;

This adds hunderds of bytes into the memory structure, but most are unnecessary. I'm dubious about that. Fields in the footer all have counterparts in header, which are as well fields of VmdkExtent. So please reuse them instead of introducing redundancy.

Quoted 4 lines

When we close the extent, we could reuse the same logic as in vmdk_create_extent, to generate footer dynamically, instead of writting what is read when closing. That way footer here is not needed.

Good point. I'll remove footer from VmdkExtent structure.

Quoted 145 lines
Quoted 145 lines
block/vmdk.c +50 -41
 } VmdkExtent;
 
 typedef struct BDRVVmdkState {
@@ -125,7 +144,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +573,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +647,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +664,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +712,21 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+        extent->footer = footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
Quoted 9 lines

block/vmdk.c static int vmdk_open_vmdk4(BlockDriverState *bs, · line 728

Quoted 5 lines
block/vmdk.c +4 -0
         s->create_type = g_strdup("streamOptimized");
+
+        if (flags & BDRV_O_RDWR) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);

Looks dangerous. What if has_footer == false, as for the imaged created from older implementation?

Will check for has_footer.

Quoted 360 lines
Quoted 360 lines
block/vmdk.c +217 -35
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1031,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* do not update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1047,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1063,113 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs,
+                             VMDK4Footer *footer,
+                             VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL;
+    uint32_t grains, gd_sectors, gt_size, gt_count;
+    uint64_t offset;
+    VMDK4Header header;
+    VMDK4MetaMarker gd_marker;
+
+    header = footer->header;
+    offset = le64_to_cpu(header.gd_offset);
+
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
+    gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
+                           BDRV_SECTOR_SIZE);
+    gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    offset += sizeof(gd_marker) / BDRV_SECTOR_SIZE;
+
+    /* write grain directory */
+    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+    gd_buf = g_malloc0(gd_buf_size);
+    if (extent) {
+        /* copy over L1 table if we have it */
+        for (i = 0; i < gt_count; i++) {
+            gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+        }
+    }
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    /* save real gd_offset */
+    footer->header.gd_offset = cpu_to_le64(offset);
+    offset += gd_sectors;
+
+    /* write footer */
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = 0;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i, ret;
+    uint64_t offset;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    offset = le64_to_cpu(extent->footer.header.gd_offset) << 9;
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, offset, &gtm, sizeof(gtm)) < 0) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += sizeof(gtm);
+    extent->l1_table[extent->l1_index] = offset >> 9;
+    if (bdrv_pwrite(extent->file, offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        ret = -EIO;
+        goto out;
+    }
+
+    offset += extent->l2_size * sizeof(uint32_t);
+    extent->l1_table_offset = offset;
+    extent->footer.header.gd_offset = cpu_to_le64(offset >> 9);
+
+    ret = 0;
+ out:
+    return ret;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1178,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1192,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index]) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1254,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1208,6 +1369,7 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
     uLongf buf_len;
     const uint8_t *write_buf = buf;
     int write_len = nb_sectors * 512;
+    uint64_t gd_offset;
 
     if (extent->compressed) {
         if (!extent->has_marker) {
@@ -1234,6 +1396,13 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        gd_offset = bdrv_getlength(extent->file);
+        extent->l1_table_offset = gd_offset;
+        gd_offset /= BDRV_SECTOR_SIZE;
+        extent->footer.header.gd_offset = cpu_to_le64(gd_offset);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1701,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1731,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1799,55 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        /* footer marker */
+        footer.footer_marker.val = cpu_to_le64(1);
+        footer.footer_marker.type = cpu_to_le32(MARKER_FOOTER);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        /* header */
+        footer.magic = cpu_to_be32(VMDK4_MAGIC);
+        footer.header = header;
+
+        /* grain directory offset */
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
+
+        /* EOS marker */
+        footer.eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+        ret = vmdk_write_footer(bs, &footer, NULL);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1912,6 +2118,15 @@ exit:
 static void vmdk_close(BlockDriverState *bs)
 {
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_footer(extent->file, &extent->footer, extent);
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
--

1.7.1

#12 lore raw
Quoted 11 lines · Milos Vyletel
Quoted 1 line · Fam Zheng

Does putting a monolithicSparse into the OVA work in this case?

It does not. I did not try to import it to OVM but ESXi server complained about the format of vmdk in OVA archive (don't have exact error message anymore). I did look in OVF specifications and it does not state that the vmdk must be streamOptimized but it looks like VMWare is enforcing it.

I can try to create OVA with monolithicSparse image and retry if you want to know exact error I got.

I'm curious about the tool you're using to create OVA, because there are xml files in OVA/OVF that qemu has no knowledge about.

Is it some tool from VMware?

Fam

#13 lore raw
Quoted 13 lines · Fam Zheng
Quoted 11 lines · Milos Vyletel
Quoted 1 line · Fam Zheng

Does putting a monolithicSparse into the OVA work in this case?

It does not. I did not try to import it to OVM but ESXi server complained about the format of vmdk in OVA archive (don't have exact error message anymore). I did look in OVF specifications and it does not state that the vmdk must be streamOptimized but it looks like VMWare is enforcing it.

I can try to create OVA with monolithicSparse image and retry if you want to know exact error I got.

Quoted 6 lines

I'm curious about the tool you're using to create OVA, because there are xml files in OVA/OVF that qemu has no knowledge about.

Is it some tool from VMware?

Fam

Nope. It's just perl script that creates OVF, manifest and packs them together with vmdk. Qemu just does the conversion to vmdk in this case.

Milos

#14 lore raw

PATCH v2 1/1 vmdk: improve streamOptimized vmdk support

In reply to Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

v3 changes:
- removed footer from VmdkExtent structure
- split added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
block/vmdk.c +280 -75
index 0517bba..3ea1c31 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -125,7 +142,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +571,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +645,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +662,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +710,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (extent->has_footer && (flags & BDRV_O_RDWR)) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1028,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* nothing to update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1044,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1060,108 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs, VMDK4Footer *footer)
+{
+    int ret;
+    uint64_t offset;
+    uint32_t grains, gt_count, gd_sectors;
+
+    if (!footer) {
+        return -EINVAL;
+    }
+
+    grains = DIV_ROUND_UP(le64_to_cpu(footer->header.capacity),
+                          le64_to_cpu(footer->header.granularity));
+    gt_count = DIV_ROUND_UP(grains,
+                            le32_to_cpu(footer->header.num_gtes_per_gt));
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    offset = le64_to_cpu(footer->header.gd_offset) + gd_sectors;
+    footer->footer_marker.val = cpu_to_le64(1);
+    footer->footer_marker.type = cpu_to_le32(MARKER_FOOTER);
+    footer->magic = cpu_to_be32(VMDK4_MAGIC);
+    footer->eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        return ret;
+    }
+
+    return VMDK_OK;
+}
+
+static int vmdk_write_grain_directory(VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL, gd_sectors;
+    VMDK4MetaMarker gd_marker;
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_sectors = DIV_ROUND_UP(extent->l1_size * sizeof(uint32_t),
+                              BDRV_SECTOR_SIZE);
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    extent->l1_table_offset += sizeof(gd_marker);
+
+    /* write grain directory */
+    gd_buf_size = extent->l1_size * sizeof(uint32_t);
+    gd_buf = g_malloc0(gd_buf_size);
+    for (i = 0; i < extent->l1_size; i++) {
+        gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+    }
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = VMDK_OK;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        return -EINVAL;
+    }
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    &gtm, sizeof(gtm)) != sizeof(gtm)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += sizeof(gtm);
+
+    extent->l1_table[extent->l1_index] = extent->l1_table_offset >> 9;
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += extent->l2_size * sizeof(uint32_t);
+
+    return VMDK_OK;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1170,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1184,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index]) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1246,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1234,6 +1387,10 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        extent->l1_table_offset = bdrv_getlength(extent->file);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1689,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1719,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1787,44 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        footer.header = header;
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        ret = vmdk_write_footer(bs, &footer);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1911,7 +2094,29 @@ exit:
 
 static void vmdk_close(BlockDriverState *bs)
 {
+    int ret;
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+    VMDK4Footer footer;
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_grain_directory(extent);
+            if (extent->has_footer) {
+                memset(&footer, 0, sizeof(footer));
+                ret = bdrv_pread(extent->file, sizeof(uint32_t),
+                                 &footer.header, sizeof(footer.header));
+                if (ret < 0) {
+                    continue;
+                }
+                footer.header.gd_offset =
+                    cpu_to_le64(extent->l1_table_offset >> 9);
+                vmdk_write_footer(extent->file, &footer);
+            }
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#15 lore raw
Quoted 576 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

v3 changes:
- removed footer from VmdkExtent structure
- split added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
block/vmdk.c +280 -76
index 0517bba..3ea1c31 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -125,7 +142,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +571,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +645,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +662,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +710,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (extent->has_footer && (flags & BDRV_O_RDWR)) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1028,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* nothing to update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1044,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1060,108 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs, VMDK4Footer *footer)
+{
+    int ret;
+    uint64_t offset;
+    uint32_t grains, gt_count, gd_sectors;
+
+    if (!footer) {
+        return -EINVAL;
+    }
+
+    grains = DIV_ROUND_UP(le64_to_cpu(footer->header.capacity),
+                          le64_to_cpu(footer->header.granularity));
+    gt_count = DIV_ROUND_UP(grains,
+                            le32_to_cpu(footer->header.num_gtes_per_gt));
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    offset = le64_to_cpu(footer->header.gd_offset) + gd_sectors;
+    footer->footer_marker.val = cpu_to_le64(1);
+    footer->footer_marker.type = cpu_to_le32(MARKER_FOOTER);
+    footer->magic = cpu_to_be32(VMDK4_MAGIC);
+    footer->eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        return ret;
+    }
+
+    return VMDK_OK;
+}
+
+static int vmdk_write_grain_directory(VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL, gd_sectors;
+    VMDK4MetaMarker gd_marker;
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_sectors = DIV_ROUND_UP(extent->l1_size * sizeof(uint32_t),
+                              BDRV_SECTOR_SIZE);
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    extent->l1_table_offset += sizeof(gd_marker);
+
+    /* write grain directory */
+    gd_buf_size = extent->l1_size * sizeof(uint32_t);
+    gd_buf = g_malloc0(gd_buf_size);
+    for (i = 0; i < extent->l1_size; i++) {
+        gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+    }
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = VMDK_OK;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        return -EINVAL;
+    }
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    &gtm, sizeof(gtm)) != sizeof(gtm)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += sizeof(gtm);
+
+    extent->l1_table[extent->l1_index] = extent->l1_table_offset >> 9;
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += extent->l2_size * sizeof(uint32_t);
+
+    return VMDK_OK;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1170,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1184,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index]) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1246,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1234,6 +1387,10 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        extent->l1_table_offset = bdrv_getlength(extent->file);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1689,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1719,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1787,44 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        footer.header = header;
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        ret = vmdk_write_footer(bs, &footer);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1911,7 +2094,29 @@ exit:
 
 static void vmdk_close(BlockDriverState *bs)
 {
+    int ret;
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+    VMDK4Footer footer;
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_grain_directory(extent);
+            if (extent->has_footer) {
+                memset(&footer, 0, sizeof(footer));
+                ret = bdrv_pread(extent->file, sizeof(uint32_t),
+                                 &footer.header, sizeof(footer.header));
+                if (ret < 0) {
+                    continue;
+                }
+                footer.header.gd_offset =
+                    cpu_to_le64(extent->l1_table_offset >> 9);
+                vmdk_write_footer(extent->file, &footer);
+            }
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
--
Quoted 2 lines

1.7.1

Please ignore this patch. Found small bug and will resend with fix and correct subject this time.

Milos

#16 lore raw

PATCH v3 1/1 vmdk: improve streamOptimized vmdk support

In reply to Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

v3 changes:
- removed footer from VmdkExtent structure
- added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation
- fix reverse conversion from VMDK to other formats

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
block/vmdk.c +280 -75
index 0517bba..0a1a08c 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -125,7 +142,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +571,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +645,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +662,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +710,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (extent->has_footer && (flags & BDRV_O_RDWR)) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1028,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* nothing to update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1044,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1060,108 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs, VMDK4Footer *footer)
+{
+    int ret;
+    uint64_t offset;
+    uint32_t grains, gt_count, gd_sectors;
+
+    if (!footer) {
+        return -EINVAL;
+    }
+
+    grains = DIV_ROUND_UP(le64_to_cpu(footer->header.capacity),
+                          le64_to_cpu(footer->header.granularity));
+    gt_count = DIV_ROUND_UP(grains,
+                            le32_to_cpu(footer->header.num_gtes_per_gt));
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    offset = le64_to_cpu(footer->header.gd_offset) + gd_sectors;
+    footer->footer_marker.val = cpu_to_le64(1);
+    footer->footer_marker.type = cpu_to_le32(MARKER_FOOTER);
+    footer->magic = cpu_to_be32(VMDK4_MAGIC);
+    footer->eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        return ret;
+    }
+
+    return VMDK_OK;
+}
+
+static int vmdk_write_grain_directory(VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL, gd_sectors;
+    VMDK4MetaMarker gd_marker;
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_sectors = DIV_ROUND_UP(extent->l1_size * sizeof(uint32_t),
+                              BDRV_SECTOR_SIZE);
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    extent->l1_table_offset += sizeof(gd_marker);
+
+    /* write grain directory */
+    gd_buf_size = extent->l1_size * sizeof(uint32_t);
+    gd_buf = g_malloc0(gd_buf_size);
+    for (i = 0; i < extent->l1_size; i++) {
+        gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+    }
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = VMDK_OK;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);
+        }
+    }
+    if (!l2_table) {
+        return -EINVAL;
+    }
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    &gtm, sizeof(gtm)) != sizeof(gtm)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += sizeof(gtm);
+
+    extent->l1_table[extent->l1_index] = extent->l1_table_offset >> 9;
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        return -EIO;
+    }
+    extent->l1_table_offset += extent->l2_size * sizeof(uint32_t);
+
+    return VMDK_OK;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1170,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1184,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index] && allocate) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1246,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1234,6 +1387,10 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        extent->l1_table_offset = bdrv_getlength(extent->file);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1689,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1719,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1787,44 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        footer.header = header;
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        ret = vmdk_write_footer(bs, &footer);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1911,7 +2094,29 @@ exit:
 
 static void vmdk_close(BlockDriverState *bs)
 {
+    int ret;
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];
+    VMDK4Footer footer;
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_grain_directory(extent);
+            if (extent->has_footer) {
+                memset(&footer, 0, sizeof(footer));
+                ret = bdrv_pread(extent->file, sizeof(uint32_t),
+                                 &footer.header, sizeof(footer.header));
+                if (ret < 0) {
+                    continue;
+                }
+                footer.header.gd_offset =
+                    cpu_to_le64(extent->l1_table_offset >> 9);
+                vmdk_write_footer(extent->file, &footer);
+            }
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#17 lore raw
Quoted 36 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

Quoted 8 lines

v3 changes:
- removed footer from VmdkExtent structure
- added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation
- fix reverse conversion from VMDK to other formats

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)

In the future, please send revisions as top-level email threads. When people use threaded email clients your patch can be overlooked since it's a subthread of an old discussion, rather than a new thread in the inbox.

I skimmed the patch and am relying on Fam for a detailed review.

#18 lore raw
Quoted 46 lines · Stefan Hajnoczi
Quoted 36 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

Quoted 8 lines

v3 changes:
- removed footer from VmdkExtent structure
- added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation
- fix reverse conversion from VMDK to other formats

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
Quoted 6 lines

In the future, please send revisions as top-level email threads. When people use threaded email clients your patch can be overlooked since it's a subthread of an old discussion, rather than a new thread in the inbox.

I skimmed the patch and am relying on Fam for a detailed review.

Will do. Sorry about that. I just thought it's easier to have it all in one thread so lets say that in future you find this thread in archive and you'll have all patch versions in one place and don't have to look for other threads.

Milos

#19 lore raw
In reply to Milos Vyletel
Quoted 328 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

v3 changes:
- removed footer from VmdkExtent structure
- added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation
- fix reverse conversion from VMDK to other formats

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
block/vmdk.c +144 -42
index 0517bba..0a1a08c 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -125,7 +142,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +571,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +645,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +662,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +710,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (extent->has_footer && (flags & BDRV_O_RDWR)) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1028,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* nothing to update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1044,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1060,108 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs, VMDK4Footer *footer)
+{
+    int ret;
+    uint64_t offset;
+    uint32_t grains, gt_count, gd_sectors;
+
+    if (!footer) {
+        return -EINVAL;
+    }
+
+    grains = DIV_ROUND_UP(le64_to_cpu(footer->header.capacity),
+                          le64_to_cpu(footer->header.granularity));
+    gt_count = DIV_ROUND_UP(grains,
+                            le32_to_cpu(footer->header.num_gtes_per_gt));
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    offset = le64_to_cpu(footer->header.gd_offset) + gd_sectors;
+    footer->footer_marker.val = cpu_to_le64(1);
+    footer->footer_marker.type = cpu_to_le32(MARKER_FOOTER);
+    footer->magic = cpu_to_be32(VMDK4_MAGIC);
+    footer->eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        return ret;
+    }
+
+    return VMDK_OK;
+}
+
+static int vmdk_write_grain_directory(VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL, gd_sectors;
+    VMDK4MetaMarker gd_marker;
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_sectors = DIV_ROUND_UP(extent->l1_size * sizeof(uint32_t),
+                              BDRV_SECTOR_SIZE);
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    extent->l1_table_offset += sizeof(gd_marker);
+
+    /* write grain directory */
+    gd_buf_size = extent->l1_size * sizeof(uint32_t);
+    gd_buf = g_malloc0(gd_buf_size);
+    for (i = 0; i < extent->l1_size; i++) {
+        gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+    }
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = VMDK_OK;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1138

Quoted 10 lines
block/vmdk.c +10 -0
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);

break?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1149

Quoted 11 lines
block/vmdk.c +11 -0
+        }
+    }
+    if (!l2_table) {
+        return -EINVAL;
+    }
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    &gtm, sizeof(gtm)) != sizeof(gtm)) {

I feel that the variable extent->l1_table_offest is abused. Could you rebase to current master and use extent->next_cluster_sector to track the position for next metadata write?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1150

Quoted 1 line
block/vmdk.c +1 -0
+        return -EIO;

Why not return bdrv_pwrite's return value instead?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1158

Quoted 8 lines
block/vmdk.c +8 -0
+    }
+    extent->l1_table_offset += sizeof(gtm);
+
+    extent->l1_table[extent->l1_index] = extent->l1_table_offset >> 9;
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        return -EIO;

Ditto.

Quoted 195 lines
block/vmdk.c +86 -33
+    }
+    extent->l1_table_offset += extent->l2_size * sizeof(uint32_t);
+
+    return VMDK_OK;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1170,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1184,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index] && allocate) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1246,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1234,6 +1387,10 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        extent->l1_table_offset = bdrv_getlength(extent->file);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1689,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1719,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1787,44 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        footer.header = header;
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        ret = vmdk_write_footer(bs, &footer);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1911,7 +2094,29 @@ exit:
 
 static void vmdk_close(BlockDriverState *bs)
 {
+    int ret;
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];

block/vmdk.c exit: · line 2109

Quoted 10 lines
block/vmdk.c +10 -0
+    VMDK4Footer footer;
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_grain_directory(extent);
+            if (extent->has_footer) {
+                memset(&footer, 0, sizeof(footer));
+                ret = bdrv_pread(extent->file, sizeof(uint32_t),
+                                 &footer.header, sizeof(footer.header));

You read the header and write it to the end of file. Shouldn't this be reading from the footer at the end of the extent?

Fam

Quoted 16 lines
block/vmdk.c +10 -0
+                if (ret < 0) {
+                    continue;
+                }
+                footer.header.gd_offset =
+                    cpu_to_le64(extent->l1_table_offset >> 9);
+                vmdk_write_footer(extent->file, &footer);
+            }
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
Signature · 1 line
1.7.1
#20 lore raw
Quoted 582 lines · Fam Zheng
Quoted 328 lines · Milos Vyletel

VMDK's streamOptimized format is different from regular sparse format. L1(GD) and L2(GT) tables are not predefined but rather generated and written during image creation mainly because there is no way to tell how much space data will occupy once they are compressed. Also the location of header, L1 and L2 tables differ.

- L2 tables (grain tables) are written after all grains they point to
- L1 tables are written after all grains and L2 tables
- footer at the end is used instead of header in first sector

Images generated by qemu-img could not be imported (as part of OVA archive) to neither VMWare nor OVM because of errors.

- VMWare during OVA import:
Not a supported disk format (sparse VMDK too old)

- OVM's vbox-img during conversion:
vbox-img: error: Error while copying the image: VERR_EOF

This patch fixes streamOptimized support in qemu which was not fully compatible with VMDK specifications as defined in latest avaialble version at https://www.vmware.com/support/developer/vddk/vmdk_50_technote.pdf.

Qemu generated images are identical to the ones generated by VMWare and OVM (vbox-img) with the exception of DescriptorFile but that is expected (CID and some additional DDB entries differ). They were also succesfully imported to VMWare vCloud, ESXi and Oracle OVM.

---
v2 changes:
- updated commit message description with errors received
- style/grammar fixes (clean checkpatch pass)
- removed l2_table pointer from VmdkExtent struct
- fixed memory leak in vmdk_write_footer()

v3 changes:
- removed footer from VmdkExtent structure
- added vmdk_write_grain_directory function to separate GD and footer writes
- fix possible problems with opening of images created by older implementation
- fix reverse conversion from VMDK to other formats

 block/vmdk.c |  355 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 280 insertions(+), 75 deletions(-)
block/vmdk.c +144 -42
index 0517bba..0a1a08c 100644
@@ -81,6 +81,21 @@ typedef struct {
     uint16_t compressAlgorithm;
 } QEMU_PACKED VMDK4Header;
 
+typedef struct {
+    uint64_t val;
+    uint32_t size;
+    uint32_t type;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint64_t) - 2*sizeof(uint32_t)];
+} QEMU_PACKED VMDK4MetaMarker;
+
+typedef struct {
+    VMDK4MetaMarker footer_marker;
+    uint32_t magic;
+    VMDK4Header header;
+    uint8_t pad[BDRV_SECTOR_SIZE - sizeof(uint32_t) - sizeof(VMDK4Header)];
+    VMDK4MetaMarker eos_marker;
+} QEMU_PACKED VMDK4Footer;
+
 #define L2_CACHE_SIZE 16
 
 typedef struct VmdkExtent {
@@ -89,12 +104,14 @@ typedef struct VmdkExtent {
     bool compressed;
     bool has_marker;
     bool has_zero_grain;
+    bool has_footer;
     int version;
     int64_t sectors;
     int64_t end_sector;
     int64_t flat_start_offset;
     int64_t l1_table_offset;
     int64_t l1_backup_table_offset;
+    uint32_t l1_index;
     uint32_t *l1_table;
     uint32_t *l1_backup_table;
     unsigned int l1_size;
@@ -125,7 +142,6 @@ typedef struct BDRVVmdkState {
 
 typedef struct VmdkMetaData {
     uint32_t offset;
-    unsigned int l1_index;
     unsigned int l2_index;
     unsigned int l2_offset;
     int valid;
@@ -555,14 +571,50 @@ static char *vmdk_read_desc(BlockDriverState *file, uint64_t desc_offset,
     return buf;
 }
 
+static int vmdk_read_footer(BlockDriverState *bs,
+                            VMDK4Footer *footer)
+{
+    int ret;
+
+    /*
+    * footer starts 3 sectors from end
+    * - footer marker
+    * - footer
+    * - end-of-stream marker
+    */
+    ret = bdrv_pread(bs->file,
+        (bs->file->total_sectors - 3) * BDRV_SECTOR_SIZE,
+        footer, sizeof(*footer));
+    if (ret < 0) {
+        goto out;
+    }
+
+    /* Some sanity checks for the footer */
+    if (be32_to_cpu(footer->magic) != VMDK4_MAGIC ||
+        le32_to_cpu(footer->footer_marker.size) != 0  ||
+        le32_to_cpu(footer->footer_marker.type) != MARKER_FOOTER ||
+        le64_to_cpu(footer->eos_marker.val) != 0  ||
+        le32_to_cpu(footer->eos_marker.size) != 0  ||
+        le32_to_cpu(footer->eos_marker.type) != MARKER_END_OF_STREAM) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    ret = VMDK_OK;
+ out:
+    return ret;
+}
+
 static int vmdk_open_vmdk4(BlockDriverState *bs,
                            BlockDriverState *file,
                            int flags, Error **errp)
 {
     int ret;
+    bool has_footer = false;
     uint32_t magic;
     uint32_t l1_size, l1_entry_sectors;
     VMDK4Header header;
+    VMDK4Footer footer;
     VmdkExtent *extent;
     BDRVVmdkState *s = bs->opaque;
     int64_t l1_backup_offset = 0;
@@ -593,48 +645,13 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
 
     if (le64_to_cpu(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.
+         * The footer takes precedence over the header, so read it in.
          */
-        struct {
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED footer_marker;
-
-            uint32_t magic;
-            VMDK4Header header;
-            uint8_t pad[512 - 4 - sizeof(VMDK4Header)];
-
-            struct {
-                uint64_t val;
-                uint32_t size;
-                uint32_t type;
-                uint8_t pad[512 - 16];
-            } QEMU_PACKED eos_marker;
-        } QEMU_PACKED footer;
-
-        ret = bdrv_pread(file,
-            bs->file->total_sectors * 512 - 1536,
-            &footer, sizeof(footer));
+        ret = vmdk_read_footer(bs, &footer);
         if (ret < 0) {
             return ret;
         }
-
-        /* Some sanity checks for the footer */
-        if (be32_to_cpu(footer.magic) != VMDK4_MAGIC ||
-            le32_to_cpu(footer.footer_marker.size) != 0  ||
-            le32_to_cpu(footer.footer_marker.type) != MARKER_FOOTER ||
-            le64_to_cpu(footer.eos_marker.val) != 0  ||
-            le32_to_cpu(footer.eos_marker.size) != 0  ||
-            le32_to_cpu(footer.eos_marker.type) != MARKER_END_OF_STREAM)
-        {
-            return -EINVAL;
-        }
-
+        has_footer = true;
         header = footer.header;
     }
 
@@ -645,11 +662,15 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
         error_set(errp, QERR_UNKNOWN_BLOCK_FORMAT_FEATURE,
                   bs->device_name, "vmdk", buf);
         return -ENOTSUP;
-    } else if (le32_to_cpu(header.version) == 3 && (flags & BDRV_O_RDWR)) {
+    } else if (le32_to_cpu(header.version) == 3 &&
+               (flags & BDRV_O_RDWR) &&
+               !(le64_to_cpu(header.flags) & VMDK4_FLAG_COMPRESS)) {
         /* VMware KB 2064959 explains that version 3 added support for
          * persistent changed block tracking (CBT), and backup software can
          * read it as version=1 if it doesn't care about the changed area
-         * information. So we are safe to enable read only. */
+         * information. So we are safe to enable read only.
+         * Note that this does not apply to streamOptimized images which
+         * are written only once and are used as transport format */
         error_setg(errp, "VMDK version 3 must be read only");
         return -EINVAL;
     }
@@ -689,11 +710,20 @@ static int vmdk_open_vmdk4(BlockDriverState *bs,
     if (ret < 0) {
         return ret;
     }
+    if (has_footer) {
+        extent->has_footer = has_footer;
+    }
+
     extent->compressed =
         le16_to_cpu(header.compressAlgorithm) == VMDK4_COMPRESSION_DEFLATE;
     if (extent->compressed) {
         g_free(s->create_type);
         s->create_type = g_strdup("streamOptimized");
+
+        if (extent->has_footer && (flags & BDRV_O_RDWR)) {
+            bdrv_truncate(file,
+                    le64_to_cpu(header.grain_offset) * BDRV_SECTOR_SIZE);
+        }
     }
     extent->has_marker = le32_to_cpu(header.flags) & VMDK4_FLAG_MARKER;
     extent->version = le32_to_cpu(header.version);
@@ -998,6 +1028,12 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     uint32_t offset;
     QEMU_BUILD_BUG_ON(sizeof(offset) != sizeof(m_data->offset));
     offset = cpu_to_le32(m_data->offset);
+
+    /* nothing to update on streamOptimized */
+    if (extent->compressed) {
+        return VMDK_OK;
+    }
+
     /* update L2 table */
     if (bdrv_pwrite_sync(
                 extent->file,
@@ -1008,7 +1044,7 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     }
     /* update backup L2 table */
     if (extent->l1_backup_table_offset != 0) {
-        m_data->l2_offset = extent->l1_backup_table[m_data->l1_index];
+        m_data->l2_offset = extent->l1_backup_table[extent->l1_index];
         if (bdrv_pwrite_sync(
                     extent->file,
                     ((int64_t)m_data->l2_offset * 512)
@@ -1024,6 +1060,108 @@ static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data)
     return VMDK_OK;
 }
 
+static int vmdk_write_footer(BlockDriverState *bs, VMDK4Footer *footer)
+{
+    int ret;
+    uint64_t offset;
+    uint32_t grains, gt_count, gd_sectors;
+
+    if (!footer) {
+        return -EINVAL;
+    }
+
+    grains = DIV_ROUND_UP(le64_to_cpu(footer->header.capacity),
+                          le64_to_cpu(footer->header.granularity));
+    gt_count = DIV_ROUND_UP(grains,
+                            le32_to_cpu(footer->header.num_gtes_per_gt));
+    gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
+
+    offset = le64_to_cpu(footer->header.gd_offset) + gd_sectors;
+    footer->footer_marker.val = cpu_to_le64(1);
+    footer->footer_marker.type = cpu_to_le32(MARKER_FOOTER);
+    footer->magic = cpu_to_be32(VMDK4_MAGIC);
+    footer->eos_marker.type = cpu_to_le32(MARKER_END_OF_STREAM);
+
+    ret = bdrv_pwrite(bs, offset * BDRV_SECTOR_SIZE, footer, sizeof(*footer));
+    if (ret < 0) {
+        return ret;
+    }
+
+    return VMDK_OK;
+}
+
+static int vmdk_write_grain_directory(VmdkExtent *extent)
+{
+    int i, ret, gd_buf_size;
+    uint32_t *gd_buf = NULL, gd_sectors;
+    VMDK4MetaMarker gd_marker;
+
+    /* write grain directory marker */
+    memset(&gd_marker, 0, sizeof(gd_marker));
+    gd_sectors = DIV_ROUND_UP(extent->l1_size * sizeof(uint32_t),
+                              BDRV_SECTOR_SIZE);
+    gd_marker.val = cpu_to_le64(gd_sectors);
+    gd_marker.type = cpu_to_le32(MARKER_GRAIN_DIRECTORY);
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      &gd_marker, sizeof(gd_marker));
+    if (ret < 0) {
+        return ret;
+    }
+    extent->l1_table_offset += sizeof(gd_marker);
+
+    /* write grain directory */
+    gd_buf_size = extent->l1_size * sizeof(uint32_t);
+    gd_buf = g_malloc0(gd_buf_size);
+    for (i = 0; i < extent->l1_size; i++) {
+        gd_buf[i] = cpu_to_le32(extent->l1_table[i]);
+    }
+    ret = bdrv_pwrite(extent->file, extent->l1_table_offset,
+                      gd_buf, gd_buf_size);
+    if (ret < 0) {
+        goto exit;
+    }
+
+    ret = VMDK_OK;
+ exit:
+    g_free(gd_buf);
+    return ret;
+}

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1138

Quoted 10 lines
block/vmdk.c +10 -0
+
+static int vmdk_write_grain_table(VmdkExtent *extent)
+{
+    int i;
+    uint32_t *l2_table = NULL;
+    VMDK4MetaMarker gtm;
+
+    for (i = 0; i < L2_CACHE_SIZE; i++) {
+        if (extent->l1_table[extent->l1_index] == extent->l2_cache_offsets[i]) {
+            l2_table = extent->l2_cache + (i * extent->l2_size);

break?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1149

Quoted 11 lines
block/vmdk.c +11 -0
+        }
+    }
+    if (!l2_table) {
+        return -EINVAL;
+    }
+
+    memset(&gtm, 0, sizeof(gtm));
+    gtm.val = cpu_to_le32((extent->l2_size * sizeof(uint32_t)) >> 9);
+    gtm.type = cpu_to_le32(MARKER_GRAIN_TABLE);
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    &gtm, sizeof(gtm)) != sizeof(gtm)) {

I feel that the variable extent->l1_table_offest is abused. Could you rebase to current master and use extent->next_cluster_sector to track the position for next metadata write?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1150

Quoted 1 line
block/vmdk.c +1 -0
+        return -EIO;

Why not return bdrv_pwrite's return value instead?

block/vmdk.c static int vmdk_L2update(VmdkExtent *extent, VmdkMetaData *m_data) · line 1158

Quoted 8 lines
block/vmdk.c +8 -0
+    }
+    extent->l1_table_offset += sizeof(gtm);
+
+    extent->l1_table[extent->l1_index] = extent->l1_table_offset >> 9;
+    if (bdrv_pwrite(extent->file, extent->l1_table_offset,
+                    l2_table, extent->l2_size * sizeof(uint32_t)
+                   ) != extent->l2_size * sizeof(uint32_t)) {
+        return -EIO;

Ditto.

Quoted 195 lines
block/vmdk.c +86 -33
+    }
+    extent->l1_table_offset += extent->l2_size * sizeof(uint32_t);
+
+    return VMDK_OK;
+}
+
 static int get_cluster_offset(BlockDriverState *bs,
                                     VmdkExtent *extent,
                                     VmdkMetaData *m_data,
@@ -1032,7 +1170,7 @@ static int get_cluster_offset(BlockDriverState *bs,
                                     uint64_t *cluster_offset)
 {
     unsigned int l1_index, l2_offset, l2_index;
-    int min_index, i, j;
+    int min_index, i, j, ret;
     uint32_t min_count, *l2_table;
     bool zeroed = false;
 
@@ -1046,6 +1184,22 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     offset -= (extent->end_sector - extent->sectors) * SECTOR_SIZE;
     l1_index = (offset >> 9) / extent->l1_entry_sectors;
+    if (extent->compressed && !extent->l1_table[l1_index] && allocate) {
+        if (l1_index) {
+            /* grain (L2) tables follow compressed data so first L2 table will
+             * be written when we move to next index or when we close image.
+             * that is why we need to save l1_index in extent itself for easy
+             * access from both here and vmdk_close */
+            ret = vmdk_write_grain_table(extent);
+            if (ret < 0) {
+                return ret;
+            }
+        }
+        /* allocate new L2; set it to GD offset for now */
+        extent->l1_table[l1_index] = extent->l1_table_offset;
+    }
+    extent->l1_index = l1_index;
+
     if (l1_index >= extent->l1_size) {
         return VMDK_ERROR;
     }
@@ -1092,7 +1246,6 @@ static int get_cluster_offset(BlockDriverState *bs,
 
     if (m_data) {
         m_data->valid = 1;
-        m_data->l1_index = l1_index;
         m_data->l2_index = l2_index;
         m_data->offset = *cluster_offset;
         m_data->l2_offset = l2_offset;
@@ -1234,6 +1387,10 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
         ret = ret < 0 ? ret : -EIO;
         goto out;
     }
+    if (extent->compressed) {
+        /* update GD offset after each write */
+        extent->l1_table_offset = bdrv_getlength(extent->file);
+    }
     ret = 0;
  out:
     g_free(data);
@@ -1532,10 +1689,12 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     int ret, i;
     BlockDriverState *bs = NULL;
     VMDK4Header header;
+    VMDK4Footer footer;
     Error *local_err = NULL;
     uint32_t tmp, magic, grains, gd_sectors, gt_size, gt_count;
     uint32_t *gd_buf = NULL;
     int gd_buf_size;
+    uint64_t grain_offset, rgd_offset, gd_offset;
 
     ret = bdrv_create_file(filename, opts, &local_err);
     if (ret < 0) {
@@ -1560,28 +1719,38 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
     }
     magic = cpu_to_be32(VMDK4_MAGIC);
     memset(&header, 0, sizeof(header));
-    header.version = zeroed_grain ? 2 : 1;
-    header.flags = VMDK4_FLAG_RGD | VMDK4_FLAG_NL_DETECT
-                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER : 0)
+    memset(&footer, 0, sizeof(footer));
+
+    header.version = (compress ? 3 : zeroed_grain ? 2 : 1);
+    header.flags = VMDK4_FLAG_NL_DETECT
+                   | (compress ? VMDK4_FLAG_COMPRESS | VMDK4_FLAG_MARKER
+                               : VMDK4_FLAG_RGD)
                    | (zeroed_grain ? VMDK4_FLAG_ZERO_GRAIN : 0);
     header.compressAlgorithm = compress ? VMDK4_COMPRESSION_DEFLATE : 0;
     header.capacity = filesize / BDRV_SECTOR_SIZE;
     header.granularity = 128;
     header.num_gtes_per_gt = BDRV_SECTOR_SIZE;
 
-    grains = DIV_ROUND_UP(filesize / BDRV_SECTOR_SIZE, header.granularity);
+    grains = DIV_ROUND_UP(header.capacity, header.granularity);
     gt_size = DIV_ROUND_UP(header.num_gtes_per_gt * sizeof(uint32_t),
                            BDRV_SECTOR_SIZE);
     gt_count = DIV_ROUND_UP(grains, header.num_gtes_per_gt);
     gd_sectors = DIV_ROUND_UP(gt_count * sizeof(uint32_t), BDRV_SECTOR_SIZE);
 
     header.desc_offset = 1;
-    header.desc_size = 20;
-    header.rgd_offset = header.desc_offset + header.desc_size;
-    header.gd_offset = header.rgd_offset + gd_sectors + (gt_size * gt_count);
-    header.grain_offset =
+    header.desc_size = (compress ? 2 : 20);
+    rgd_offset = header.desc_offset + header.desc_size;
+    header.rgd_offset = ((header.flags & VMDK4_FLAG_RGD) ? rgd_offset : 0);
+    gd_offset = rgd_offset + gd_sectors + (gt_size * gt_count);
+    header.gd_offset = (compress ? VMDK4_GD_AT_END : gd_offset);
+    grain_offset =
         ROUND_UP(header.gd_offset + gd_sectors + (gt_size * gt_count),
                  header.granularity);
+    /* streamOptimized reserves first 128 sectors */
+    header.grain_offset = (compress ? header.granularity : grain_offset);
+    /* streamOptimzed's grain directory is at the end */
+    gd_offset = header.grain_offset + 1;
+
     /* swap endianness for all header fields */
     header.version = cpu_to_le32(header.version);
     header.flags = cpu_to_le32(header.flags);
@@ -1618,30 +1787,44 @@ static int vmdk_create_extent(const char *filename, int64_t filesize,
         goto exit;
     }
 
-    /* write grain directory */
-    gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
-    gd_buf = g_malloc0(gd_buf_size);
-    for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
-    }
+    if (compress) {
+        footer.header = header;
+        footer.header.gd_offset = cpu_to_le64(gd_offset);
 
-    /* write backup grain directory */
-    for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
-         i < gt_count; i++, tmp += gt_size) {
-        gd_buf[i] = cpu_to_le32(tmp);
-    }
-    ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
-                      gd_buf, gd_buf_size);
-    if (ret < 0) {
-        error_set(errp, QERR_IO_ERROR);
-        goto exit;
+        ret = vmdk_write_footer(bs, &footer);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
+    } else {
+        /* write redundant grain directory (if applicable) */
+        if (le64_to_cpu(header.rgd_offset)) {
+            gd_buf_size = gd_sectors * BDRV_SECTOR_SIZE;
+            gd_buf = g_malloc0(gd_buf_size);
+            for (i = 0, tmp = le64_to_cpu(header.rgd_offset) + gd_sectors;
+                 i < gt_count; i++, tmp += gt_size) {
+                gd_buf[i] = cpu_to_le32(tmp);
+            }
+            ret = bdrv_pwrite(bs, le64_to_cpu(header.rgd_offset) *
+                                  BDRV_SECTOR_SIZE,
+                              gd_buf, gd_buf_size);
+            if (ret < 0) {
+                error_set(errp, QERR_IO_ERROR);
+                goto exit;
+            }
+        }
+
+        /* write grain directory */
+        for (i = 0, tmp = le64_to_cpu(header.gd_offset) + gd_sectors;
+             i < gt_count; i++, tmp += gt_size) {
+            gd_buf[i] = cpu_to_le32(tmp);
+        }
+        ret = bdrv_pwrite(bs, le64_to_cpu(header.gd_offset) * BDRV_SECTOR_SIZE,
+                          gd_buf, gd_buf_size);
+        if (ret < 0) {
+            error_set(errp, QERR_IO_ERROR);
+            goto exit;
+        }
     }
 
     ret = 0;
@@ -1911,7 +2094,29 @@ exit:
 
 static void vmdk_close(BlockDriverState *bs)
 {
+    int ret;
     BDRVVmdkState *s = bs->opaque;
+    VmdkExtent *extent = &s->extents[0];

block/vmdk.c exit: · line 2109

Quoted 10 lines
block/vmdk.c +10 -0
+    VMDK4Footer footer;
+
+    if (extent->compressed) {
+        while (extent < &s->extents[s->num_extents]) {
+            vmdk_write_grain_table(extent);
+            vmdk_write_grain_directory(extent);
+            if (extent->has_footer) {
+                memset(&footer, 0, sizeof(footer));
+                ret = bdrv_pread(extent->file, sizeof(uint32_t),
+                                 &footer.header, sizeof(footer.header));

You read the header and write it to the end of file. Shouldn't this be reading from the footer at the end of the extent?

Quoted 2 lines

Fam

Sorry for taking so long to reply but I'm kind of busy at the moment (moving between continents) so I'll get back to you as soon as I can.

Milos

Quoted 16 lines
Quoted 16 lines
block/vmdk.c +10 -1
+                if (ret < 0) {
+                    continue;
+                }
+                footer.header.gd_offset =
+                    cpu_to_le64(extent->l1_table_offset >> 9);
+                vmdk_write_footer(extent->file, &footer);
+            }
+            extent++;
+        }
+    }
 
     vmdk_free_extents(bs);
     g_free(s->create_type);
--

1.7.1