9ae3a8
From 47299b9e7ac4a8318e0cf3c88cc3f24cc214da1b Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Wed, 20 Nov 2013 19:43:51 +0100
9ae3a8
Subject: [PATCH 08/25] block: vhdx - add header update capability.
9ae3a8
9ae3a8
RH-Author: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Message-id: <42b5dd4b297781538d090b292503c89e2dcb9ed5.1384975172.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 55801
9ae3a8
O-Subject: [RHEL7 qemu-kvm PATCH 08/26] block: vhdx - add header update capability.
9ae3a8
Bugzilla: 879234
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
9ae3a8
This adds the ability to update the headers in a VHDX image, including
9ae3a8
generating a new MS-compatible GUID.
9ae3a8
9ae3a8
As VHDX depends on uuid.h, VHDX is now a configurable build option.  If
9ae3a8
VHDX support is enabled, that will also enable uuid as well.  The
9ae3a8
default is to have VHDX enabled.
9ae3a8
9ae3a8
To enable/disable VHDX:  --enable-vhdx, --disable-vhdx
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit 4f18b7824ab5eda9fe051f5b24e90e5f34d08a23)
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	configure
9ae3a8
9ae3a8
RHEL7 Notes: Conflict due to the changes for VHDX enable/disable in the
9ae3a8
             same area of the file as the RHEL7 live block differentiation.
9ae3a8
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
---
9ae3a8
 block/Makefile.objs |   2 +-
9ae3a8
 block/vhdx.c        | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++-
9ae3a8
 block/vhdx.h        |  14 ++++-
9ae3a8
 configure           |  26 ++++++++-
9ae3a8
 4 files changed, 197 insertions(+), 6 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/Makefile.objs |    2 +-
9ae3a8
 block/vhdx.c        |  161 ++++++++++++++++++++++++++++++++++++++++++++++++++-
9ae3a8
 block/vhdx.h        |   14 ++++-
9ae3a8
 configure           |   26 ++++++++-
9ae3a8
 4 files changed, 197 insertions(+), 6 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/Makefile.objs b/block/Makefile.objs
9ae3a8
index 3db1839..114f8a5 100644
9ae3a8
--- a/block/Makefile.objs
9ae3a8
+++ b/block/Makefile.objs
9ae3a8
@@ -2,7 +2,7 @@ block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat
9ae3a8
 block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
9ae3a8
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
9ae3a8
 block-obj-y += qed-check.o
9ae3a8
-block-obj-y += vhdx.o
9ae3a8
+block-obj-$(CONFIG_VHDX) += vhdx.o
9ae3a8
 block-obj-y += parallels.o blkdebug.o blkverify.o
9ae3a8
 block-obj-y += snapshot.o qapi.o
9ae3a8
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
9ae3a8
diff --git a/block/vhdx.c b/block/vhdx.c
9ae3a8
index 23be4e3..fba2aa9 100644
9ae3a8
--- a/block/vhdx.c
9ae3a8
+++ b/block/vhdx.c
9ae3a8
@@ -22,6 +22,7 @@
9ae3a8
 #include "block/vhdx.h"
9ae3a8
 #include "migration/migration.h"
9ae3a8
 
9ae3a8
+#include <uuid/uuid.h>
9ae3a8
 
9ae3a8
 /* Several metadata and region table data entries are identified by
9ae3a8
  * guids in  a MS-specific GUID format. */
9ae3a8
@@ -157,12 +158,41 @@ typedef struct BDRVVHDXState {
9ae3a8
     VHDXBatEntry *bat;
9ae3a8
     uint64_t bat_offset;
9ae3a8
 
9ae3a8
+    MSGUID session_guid;
9ae3a8
+
9ae3a8
+
9ae3a8
     VHDXParentLocatorHeader parent_header;
9ae3a8
     VHDXParentLocatorEntry *parent_entries;
9ae3a8
 
9ae3a8
     Error *migration_blocker;
9ae3a8
 } BDRVVHDXState;
9ae3a8
 
9ae3a8
+/* Calculates new checksum.
9ae3a8
+ *
9ae3a8
+ * Zero is substituted during crc calculation for the original crc field
9ae3a8
+ * crc_offset: byte offset in buf of the buffer crc
9ae3a8
+ * buf: buffer pointer
9ae3a8
+ * size: size of buffer (must be > crc_offset+4)
9ae3a8
+ *
9ae3a8
+ * Note: The resulting checksum is in the CPU endianness, not necessarily
9ae3a8
+ *       in the file format endianness (LE).  Any header export to disk should
9ae3a8
+ *       make sure that vhdx_header_le_export() is used to convert to the
9ae3a8
+ *       correct endianness
9ae3a8
+ */
9ae3a8
+uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset)
9ae3a8
+{
9ae3a8
+    uint32_t crc;
9ae3a8
+
9ae3a8
+    assert(buf != NULL);
9ae3a8
+    assert(size > (crc_offset + sizeof(crc)));
9ae3a8
+
9ae3a8
+    memset(buf + crc_offset, 0, sizeof(crc));
9ae3a8
+    crc =  crc32c(0xffffffff, buf, size);
9ae3a8
+    memcpy(buf + crc_offset, &crc, sizeof(crc));
9ae3a8
+
9ae3a8
+    return crc;
9ae3a8
+}
9ae3a8
+
9ae3a8
 uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
9ae3a8
                             int crc_offset)
9ae3a8
 {
9ae3a8
@@ -214,6 +244,19 @@ bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset)
9ae3a8
 
9ae3a8
 
9ae3a8
 /*
9ae3a8
+ * This generates a UUID that is compliant with the MS GUIDs used
9ae3a8
+ * in the VHDX spec (and elsewhere).
9ae3a8
+ */
9ae3a8
+void vhdx_guid_generate(MSGUID *guid)
9ae3a8
+{
9ae3a8
+    uuid_t uuid;
9ae3a8
+    assert(guid != NULL);
9ae3a8
+
9ae3a8
+    uuid_generate(uuid);
9ae3a8
+    memcpy(guid, uuid, sizeof(MSGUID));
9ae3a8
+}
9ae3a8
+
9ae3a8
+/*
9ae3a8
  * Per the MS VHDX Specification, for every VHDX file:
9ae3a8
  *      - The header section is fixed size - 1 MB
9ae3a8
  *      - The header section is always the first "object"
9ae3a8
@@ -251,6 +294,113 @@ static void vhdx_header_le_import(VHDXHeader *h)
9ae3a8
     le64_to_cpus(&h->log_offset);
9ae3a8
 }
9ae3a8
 
9ae3a8
+/* All VHDX structures on disk are little endian */
9ae3a8
+static void vhdx_header_le_export(VHDXHeader *orig_h, VHDXHeader *new_h)
9ae3a8
+{
9ae3a8
+    assert(orig_h != NULL);
9ae3a8
+    assert(new_h != NULL);
9ae3a8
+
9ae3a8
+    new_h->signature       = cpu_to_le32(orig_h->signature);
9ae3a8
+    new_h->checksum        = cpu_to_le32(orig_h->checksum);
9ae3a8
+    new_h->sequence_number = cpu_to_le64(orig_h->sequence_number);
9ae3a8
+
9ae3a8
+    new_h->file_write_guid = orig_h->file_write_guid;
9ae3a8
+    new_h->data_write_guid = orig_h->data_write_guid;
9ae3a8
+    new_h->log_guid        = orig_h->log_guid;
9ae3a8
+
9ae3a8
+    cpu_to_leguids(&new_h->file_write_guid);
9ae3a8
+    cpu_to_leguids(&new_h->data_write_guid);
9ae3a8
+    cpu_to_leguids(&new_h->log_guid);
9ae3a8
+
9ae3a8
+    new_h->log_version     = cpu_to_le16(orig_h->log_version);
9ae3a8
+    new_h->version         = cpu_to_le16(orig_h->version);
9ae3a8
+    new_h->log_length      = cpu_to_le32(orig_h->log_length);
9ae3a8
+    new_h->log_offset      = cpu_to_le64(orig_h->log_offset);
9ae3a8
+}
9ae3a8
+
9ae3a8
+/* Update the VHDX headers
9ae3a8
+ *
9ae3a8
+ * This follows the VHDX spec procedures for header updates.
9ae3a8
+ *
9ae3a8
+ *  - non-current header is updated with largest sequence number
9ae3a8
+ */
9ae3a8
+static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
+                              bool generate_data_write_guid)
9ae3a8
+{
9ae3a8
+    int ret = 0;
9ae3a8
+    int hdr_idx = 0;
9ae3a8
+    uint64_t header_offset = VHDX_HEADER1_OFFSET;
9ae3a8
+
9ae3a8
+    VHDXHeader *active_header;
9ae3a8
+    VHDXHeader *inactive_header;
9ae3a8
+    VHDXHeader header_le;
9ae3a8
+    uint8_t *buffer;
9ae3a8
+
9ae3a8
+    /* operate on the non-current header */
9ae3a8
+    if (s->curr_header == 0) {
9ae3a8
+        hdr_idx = 1;
9ae3a8
+        header_offset = VHDX_HEADER2_OFFSET;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    active_header   = s->headers[s->curr_header];
9ae3a8
+    inactive_header = s->headers[hdr_idx];
9ae3a8
+
9ae3a8
+    inactive_header->sequence_number = active_header->sequence_number + 1;
9ae3a8
+
9ae3a8
+    /* a new file guid must be generated before any file write, including
9ae3a8
+     * headers */
9ae3a8
+    inactive_header->file_write_guid = s->session_guid;
9ae3a8
+
9ae3a8
+    /* a new data guid only needs to be generated before any guest-visible
9ae3a8
+     * writes (i.e. something observable via virtual disk read) */
9ae3a8
+    if (generate_data_write_guid) {
9ae3a8
+        vhdx_guid_generate(&inactive_header->data_write_guid);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* the header checksum is not over just the packed size of VHDXHeader,
9ae3a8
+     * but rather over the entire 'reserved' range for the header, which is
9ae3a8
+     * 4KB (VHDX_HEADER_SIZE). */
9ae3a8
+
9ae3a8
+    buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE);
9ae3a8
+    /* we can't assume the extra reserved bytes are 0 */
9ae3a8
+    ret = bdrv_pread(bs->file, header_offset, buffer, VHDX_HEADER_SIZE);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+    /* overwrite the actual VHDXHeader portion */
9ae3a8
+    memcpy(buffer, inactive_header, sizeof(VHDXHeader));
9ae3a8
+    inactive_header->checksum =
9ae3a8
+                        vhdx_update_checksum(buffer, VHDX_HEADER_SIZE,
9ae3a8
+                                             offsetof(VHDXHeader, checksum));
9ae3a8
+    vhdx_header_le_export(inactive_header, &header_le);
9ae3a8
+    ret = bdrv_pwrite_sync(bs->file, header_offset, &header_le,
9ae3a8
+                           sizeof(VHDXHeader));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto exit;
9ae3a8
+    }
9ae3a8
+    s->curr_header = hdr_idx;
9ae3a8
+
9ae3a8
+exit:
9ae3a8
+    qemu_vfree(buffer);
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
+
9ae3a8
+/*
9ae3a8
+ * The VHDX spec calls for header updates to be performed twice, so that both
9ae3a8
+ * the current and non-current header have valid info
9ae3a8
+ */
9ae3a8
+static int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s,
9ae3a8
+                               bool generate_data_write_guid)
9ae3a8
+{
9ae3a8
+    int ret;
9ae3a8
+
9ae3a8
+    ret = vhdx_update_header(bs, s, generate_data_write_guid);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        return ret;
9ae3a8
+    }
9ae3a8
+    ret = vhdx_update_header(bs, s, generate_data_write_guid);
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
 
9ae3a8
 /* opens the specified header block from the VHDX file header section */
9ae3a8
 static int vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s)
9ae3a8
@@ -742,6 +892,11 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
 
9ae3a8
+    /* This is used for any header updates, for the file_write_guid.
9ae3a8
+     * The spec dictates that a new value should be used for the first
9ae3a8
+     * header update */
9ae3a8
+    vhdx_guid_generate(&s->session_guid);
9ae3a8
+
9ae3a8
     ret = vhdx_parse_header(bs, s);
9ae3a8
     if (ret) {
9ae3a8
         goto fail;
9ae3a8
@@ -804,8 +959,10 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (flags & BDRV_O_RDWR) {
9ae3a8
-        ret = -ENOTSUP;
9ae3a8
-        goto fail;
9ae3a8
+        ret = vhdx_update_headers(bs, s, false);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* TODO: differencing files, write */
9ae3a8
diff --git a/block/vhdx.h b/block/vhdx.h
9ae3a8
index 1dbb320..2223b15 100644
9ae3a8
--- a/block/vhdx.h
9ae3a8
+++ b/block/vhdx.h
9ae3a8
@@ -67,7 +67,7 @@ typedef struct VHDXFileIdentifier {
9ae3a8
  * Microsoft is not just 16 bytes though - it is a structure that is defined,
9ae3a8
  * so we need to follow it here so that endianness does not trip us up */
9ae3a8
 
9ae3a8
-typedef struct MSGUID {
9ae3a8
+typedef struct QEMU_PACKED MSGUID {
9ae3a8
     uint32_t  data1;
9ae3a8
     uint16_t  data2;
9ae3a8
     uint16_t  data3;
9ae3a8
@@ -309,17 +309,27 @@ typedef struct QEMU_PACKED VHDXParentLocatorEntry {
9ae3a8
 /* ----- END VHDX SPECIFICATION STRUCTURES ---- */
9ae3a8
 
9ae3a8
 
9ae3a8
+void vhdx_guid_generate(MSGUID *guid);
9ae3a8
+
9ae3a8
+uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset);
9ae3a8
 uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
9ae3a8
                             int crc_offset);
9ae3a8
 
9ae3a8
 bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
9ae3a8
 
9ae3a8
 
9ae3a8
-static void leguid_to_cpus(MSGUID *guid)
9ae3a8
+static inline void leguid_to_cpus(MSGUID *guid)
9ae3a8
 {
9ae3a8
     le32_to_cpus(&guid->data1);
9ae3a8
     le16_to_cpus(&guid->data2);
9ae3a8
     le16_to_cpus(&guid->data3);
9ae3a8
 }
9ae3a8
 
9ae3a8
+static inline void cpu_to_leguids(MSGUID *guid)
9ae3a8
+{
9ae3a8
+    cpu_to_le32s(&guid->data1);
9ae3a8
+    cpu_to_le16s(&guid->data2);
9ae3a8
+    cpu_to_le16s(&guid->data3);
9ae3a8
+}
9ae3a8
+
9ae3a8
 #endif
9ae3a8
diff --git a/configure b/configure
9ae3a8
index 33235c4..5161fec 100755
9ae3a8
--- a/configure
9ae3a8
+++ b/configure
9ae3a8
@@ -245,6 +245,7 @@ libssh2=""
9ae3a8
 live_block_ops="yes"
9ae3a8
 live_block_migration="no"
9ae3a8
 ceph_support="yes"
9ae3a8
+vhdx=""
9ae3a8
 
9ae3a8
 # parse CC options first
9ae3a8
 for opt do
9ae3a8
@@ -950,7 +951,11 @@ for opt do
9ae3a8
   ;;
9ae3a8
   --enable-ceph-support) ceph_support=="yes"
9ae3a8
   ;;
9ae3a8
-*) echo "ERROR: unknown option $opt"; show_help="yes"
9ae3a8
+  --enable-vhdx) vhdx="yes"
9ae3a8
+  ;;
9ae3a8
+  --disable-vhdx) vhdx="no"
9ae3a8
+  ;;
9ae3a8
+  *) echo "ERROR: unknown option $opt"; show_help="yes"
9ae3a8
   ;;
9ae3a8
   esac
9ae3a8
 done
9ae3a8
@@ -1227,6 +1232,8 @@ echo "  --disable-live-block-migration disable live block migration"
9ae3a8
 echo "  --enable-live-block-migration  enable live block migration"
9ae3a8
 echo "  --disable-ceph-support   disable support for rbd block driver support"
9ae3a8
 echo "  --enable-ceph-support    enable support for rbd block driver support"
9ae3a8
+echo "  --disable-vhdx           disables support for the Microsoft VHDX image format"
9ae3a8
+echo "  --enable-vhdx            enable support for the Microsoft VHDX image format"
9ae3a8
 echo ""
9ae3a8
 echo "NOTE: The object files are built at the place where configure is launched"
9ae3a8
 exit 1
9ae3a8
@@ -2027,6 +2034,18 @@ EOF
9ae3a8
   fi
9ae3a8
 fi
9ae3a8
 
9ae3a8
+if test "$vhdx" = "yes" ; then
9ae3a8
+    if test "$uuid" = "no" ; then
9ae3a8
+        error_exit "uuid required for VHDX support"
9ae3a8
+    fi
9ae3a8
+elif test "$vhdx" != "no" ; then
9ae3a8
+    if test "$uuid" = "yes" ; then
9ae3a8
+        vhdx=yes
9ae3a8
+    else
9ae3a8
+        vhdx=no
9ae3a8
+    fi
9ae3a8
+fi
9ae3a8
+
9ae3a8
 ##########################################
9ae3a8
 # xfsctl() probe, used for raw-posix
9ae3a8
 if test "$xfs" != "no" ; then
9ae3a8
@@ -3615,6 +3634,7 @@ echo "TPM passthrough   $tpm_passthrough"
9ae3a8
 echo "QOM debugging     $qom_cast_debug"
9ae3a8
 echo "Live block operations $live_block_ops"
9ae3a8
 echo "Live block migration $live_block_migration"
9ae3a8
+echo "vhdx              $vhdx"
9ae3a8
 
9ae3a8
 if test "$sdl_too_old" = "yes"; then
9ae3a8
 echo "-> Your SDL version is too old - please upgrade to have SDL support"
9ae3a8
@@ -4011,6 +4031,10 @@ if test "ceph_support" = "yes"; then
9ae3a8
   echo "CONFIG_CEPH_SUPPORT=y" >> $config_host_mak
9ae3a8
 fi
9ae3a8
 
9ae3a8
+if test "$vhdx" = "yes" ; then
9ae3a8
+  echo "CONFIG_VHDX=y" >> $config_host_mak
9ae3a8
+fi
9ae3a8
+
9ae3a8
 # USB host support
9ae3a8
 if test "$libusb" = "yes"; then
9ae3a8
   echo "HOST_USB=libusb legacy" >> $config_host_mak
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8