cryptospore / rpms / qemu-kvm

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