VHDX log replay and write support
v3 cover VHDX log replay and write support
This patch series contains the initial VHDX log parsing, replay, and write support.
=== v3 changes ===
Thank you Kevin & Stefan for the feedback; incoporated in v3:
Patch 1: --- nil ---
Patch 2: * use sizeof(crc) instead of 4
* remove outdated comment
* use sizeof(MSGUID) instead of 16
* direct assignment of guid structs rather than memcpy
* rename 'rw' to 'generate_data_write_guid'
* use offsetof() instead of 4
* comment typos
* add missing error checking
* MSGUID is now QEMU_PACKED
* configure enable for vhdx is now correct and not braindead
Patch 3: --- nil ---
Patch 4: * code style fixes
* removed unused struct (VHDXLogEntryInfo)
Patch 5: * more direct assignment of guid rather than memcpy
* order of operation in export/import the same now
* became less generous with newlines (bah-humbug!)
Patch 6: * more direct assignment of guid rather than memcpy
* add error check in vhdx_user_visible_write(), now returns int
Patch 7: * check error return now of vhdx_user_visible_write()
Patch 8: * check error return now of vhdx_user_visible_write()
* vhdx_log_write_sectors() uses bdrv_pwrite() vs bdrv_pwrite_sync()
* more direct assignment of guid rather than memcpy
* use offsetof() instead of 4
Patch 9: --- nil ---
=== end v3 changelog ===
v2 changes: Incorporated Fam's review feedback - Thank you Fam for the feedback
=== end v2 changelog ===
This will allow an existing log in a VHDX image to be replayed (e.g., a VHDX image from a Hyper-V host that crashed). In addition, metadata writes are enabled through the log. This allows write support to be enabled for VHDX, as the BAT can be updated safely via the log journal.
These exact patches are available from github, for testing:
https://github.com/codyprime/qemu-kvm-jtc/tree/vhdx-write-v2-upstream
The latest vhdx work (including anything beyond these patches, such as backing/parent file support) can be found at:
https://github.com/codyprime/qemu-kvm-jtc/tree/jtc-vhdx-latest
Jeff Cody (9):
block: vhdx - minor comments and typo correction.
block: vhdx - add header update capability.
block: vhdx code movement - VHDXMetadataEntries and BDRVVHDXState to
header.
block: vhdx - log support struct and defines
block: vhdx - break endian translation functions out
block: vhdx - update log guid in header, and first write tracker
block: vhdx - log parsing, replay, and flush support
block: vhdx - add log write support
block: vhdx write support
block/Makefile.objs | 2 +- block/vhdx-endian.c | 141 +++++++ block/vhdx-log.c | 1011 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/vhdx.c | 399 ++++++++++++++------ block/vhdx.h | 135 +++++-- configure | 24 ++ 6 files changed, 1585 insertions(+), 127 deletions(-) create mode 100644 block/vhdx-endian.c create mode 100644 block/vhdx-log.c
Signature · 1 line
1.8.1.4
PATCH v3 1/9 block: vhdx - minor comments and typo correction.
Just a couple of minor comments to help note where allocated buffers are freed, and a typo fix.
--- block/vhdx.c | 6 ++++-- block/vhdx.h | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-)
block/vhdx.c +4 -2
@@ -6,9 +6,9 @@* Authors: * Jeff Cody <[email protected]> * -* This is based on the "VHDX Format Specification v0.95", published 4/12/2012 +* This is based on the "VHDX Format Specification v1.00", published 8/25/2012 * by Microsoft: -* https://www.microsoft.com/en-us/download/details.aspx?id=29681 +* https://www.microsoft.com/en-us/download/details.aspx?id=34750 * * This work is licensed under the terms of the GNU LGPL, version 2 or later. * See the COPYING.LIB file in the top-level directory.
@@ -262,6 +262,7 @@ static int vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s)uint64_t h2_seq =0 ;uint8_t *buffer; +/* header1 & header2 are freed in vhdx_close() */ header1 =qemu_blockalign (bs,sizeof (VHDXHeader)); header2 =qemu_blockalign (bs,sizeof (VHDXHeader));
@@ -787,6 +788,7 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags) goto fail; } +/* s->bat is freed in vhdx_close() */ s->bat =qemu_blockalign (bs, s->bat_rt.length); ret =bdrv_pread (bs->file, s->bat_offset, s->bat, s->bat_rt.length);
block/vhdx.h +3 -3
@@ -6,9 +6,9 @@* Authors: * Jeff Cody <[email protected]> * -* This is based on the "VHDX Format Specification v0.95", published 4/12/2012 +* This is based on the "VHDX Format Specification v1.00", published 8/25/2012 * by Microsoft: -* https://www.microsoft.com/en-us/download/details.aspx?id=29681 +* https://www.microsoft.com/en-us/download/details.aspx?id=34750 * * This work is licensed under the terms of the GNU LGPL, version 2 or later. * See the COPYING.LIB file in the top-level directory.
@@ -116,7 +116,7 @@ typedef struct QEMU_PACKED VHDXHeader {valid. */ uint16_t log_version;/* version of the log format. Mustn't be zero, unless log_guid is also zero */ -uint16_t version;/* version of th evhdx file. Currently, +uint16_t version;/* version of the vhdx file. Currently, only supported version is "1" */ uint32_t log_length;/* length of the log. Must be multiple of 1MB */
Signature · 1 line
1.8.1.4
PATCH v3 2/9 block: vhdx - add header update capability.
In reply to Jeff CodyThis adds the ability to update the headers in a VHDX image, including generating a new MS-compatible GUID.
As VHDX depends on uuid.h, VHDX is now a configurable build option. If VHDX support is enabled, that will also enable uuid as well. The default is to have VHDX enabled.
To enable/disable VHDX: --enable-vhdx, --disable-vhdx
--- block/Makefile.objs | 2 +- block/vhdx.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++- block/vhdx.h | 14 ++++- configure | 24 ++++++++ 4 files changed, 196 insertions(+), 5 deletions(-)
block/Makefile.objs +1 -1
@@ -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 block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-obj-y += qed-check.o -block-obj-y += vhdx.o +block-obj-$(CONFIG_VHDX) += vhdx.o block-obj-y += parallels.o blkdebug.o blkverify.o block-obj-y += snapshot.o qapi.o block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
block/vhdx.c +159 -2
202 lines not on this page · raw message
block/vhdx.h +12 -2
@@ -67,7 +67,7 @@ typedef struct VHDXFileIdentifier {* Microsoft is not just 16 bytes though - it is a structure that is defined, * so we need to follow it here so that endianness does not trip us up */ -typedef struct MSGUID { +typedef struct QEMU_PACKED MSGUID {uint32_t data1;uint16_t data2;uint16_t data3;
@@ -309,17 +309,27 @@ typedef struct QEMU_PACKED VHDXParentLocatorEntry {/* ----- END VHDX SPECIFICATION STRUCTURES ---- */ +void vhdx_guid_generate (MSGUID *guid); + +uint32_t vhdx_update_checksum (uint8_t *buf,size_t size,int crc_offset);uint32_t vhdx_checksum_calc (uint32_t crc,uint8_t *buf,size_t size,int crc_offset);bool vhdx_checksum_is_valid (uint8_t *buf,size_t size,int crc_offset); -static void leguid_to_cpus (MSGUID *guid) +static inline void leguid_to_cpus (MSGUID *guid) {le32_to_cpus (&guid->data1);le16_to_cpus (&guid->data2);le16_to_cpus (&guid->data3); } +static inline void cpu_to_leguids (MSGUID *guid) +{ +cpu_to_le32s (&guid->data1); +cpu_to_le16s (&guid->data2); +cpu_to_le16s (&guid->data3); +} +#endif
configure +24 -0
@@ -243,6 +243,7 @@ gtk="" gtkabi="2.0" tpm="no" libssh2="" +vhdx="" # parse CC options first for opt do
@@ -945,6 +946,10 @@ for opt do ;; --enable-libssh2) libssh2="yes" ;; + --enable-vhdx) vhdx="yes" + ;; + --disable-vhdx) vhdx="no" + ;; *) echo "ERROR: unknown option $opt"; show_help="yes" ;; esac
@@ -1159,6 +1164,8 @@ echo " --gcov=GCOV use specified gcov [$gcov_tool]" echo " --enable-tpm enable TPM support" echo " --disable-libssh2 disable ssh block device support" echo " --enable-libssh2 enable ssh block device support" +echo " --disable-vhdx disables support for the Microsoft VHDX image format" +echo " --enable-vhdx enable support for the Microsoft VHDX image format" echo "" echo "NOTE: The object files are built at the place where configure is launched" exit 1
@@ -1970,6 +1977,18 @@ EOF fi fi +if test "$vhdx" = "yes" ; then + if test "$uuid" = "no" ; then + error_exit "uuid required for VHDX support" + fi +elif test "$vhdx" != "no" ; then + if test "$uuid" = "yes" ; then + vhdx=yes + else + vhdx=no + fi +fi + ########################################## # xfsctl() probe, used for raw-posix if test "$xfs" != "no" ; then
@@ -3613,6 +3632,7 @@ echo "TPM support $tpm" echo "libssh2 support $libssh2" echo "TPM passthrough $tpm_passthrough" echo "QOM debugging $qom_cast_debug" +echo "vhdx $vhdx" if test "$sdl_too_old" = "yes"; then echo "-> Your SDL version is too old - please upgrade to have SDL support"
@@ -4003,6 +4023,10 @@ if test "$virtio_blk_data_plane" = "yes" ; then echo 'CONFIG_VIRTIO_BLK_DATA_PLANE=$(CONFIG_VIRTIO)' >> $config_host_mak fi +if test "$vhdx" = "yes" ; then + echo "CONFIG_VHDX=y" >> $config_host_mak +fi + # USB host support case "$usb" in linux)
Signature · 1 line
1.8.1.4
PATCH v3 3/9 block: vhdx code movement - VHDXMetadataEntries and BDRVVHDXState to header.
In reply to Jeff CodyIn preparation for VHDX log support, move these structures to the header.
--- block/vhdx.c | 51 --------------------------------------------------- block/vhdx.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 51 deletions(-)
block/vhdx.c +0 -51
@@ -104,16 +104,6 @@ static const MSGUID parent_vhdx_guid = { .data1 = 0xb04aefb7, META_PAGE_83_PRESENT | META_LOGICAL_SECTOR_SIZE_PRESENT | \ META_PHYS_SECTOR_SIZE_PRESENT) -typedef struct VHDXMetadataEntries { -VHDXMetadataTableEntry file_parameters_entry; -VHDXMetadataTableEntry virtual_disk_size_entry; -VHDXMetadataTableEntry page83_data_entry; -VHDXMetadataTableEntry logical_sector_size_entry; -VHDXMetadataTableEntry phys_sector_size_entry; -VHDXMetadataTableEntry parent_locator_entry; -uint16_t present; -}VHDXMetadataEntries ; - typedef struct VHDXSectorInfo { uint32_t bat_idx; /* BAT entry index */
@@ -124,47 +114,6 @@ typedef struct VHDXSectorInfo {uint64_t block_offset;/* block offset, in bytes */ } VHDXSectorInfo; - - -typedef struct BDRVVHDXState { -CoMutex lock; - -int curr_header; -VHDXHeader *headers[2 ]; - -VHDXRegionTableHeader rt; -VHDXRegionTableEntry bat_rt;/* region table for the BAT */ -VHDXRegionTableEntry metadata_rt;/* region table for the metadata */ - -VHDXMetadataTableHeader metadata_hdr; -VHDXMetadataEntries metadata_entries; - -VHDXFileParameters params; -uint32_t block_size; -uint32_t block_size_bits; -uint32_t sectors_per_block; -uint32_t sectors_per_block_bits; - -uint64_t virtual_disk_size; -uint32_t logical_sector_size; -uint32_t physical_sector_size; - -uint64_t chunk_ratio; -uint32_t chunk_ratio_bits; -uint32_t logical_sector_size_bits; - -uint32_t bat_entries; -VHDXBatEntry *bat; -uint64_t bat_offset; - -MSGUID session_guid; - - -VHDXParentLocatorHeader parent_header; -VHDXParentLocatorEntry *parent_entries; - -}BDRVVHDXState ; -/* Calculates new checksum. * * Zero is substituted during crc calculation for the original crc field
block/vhdx.h +47 -0
53 lines not on this page · raw message
Signature · 1 line
1.8.1.4
PATCH v3 4/9 block: vhdx - log support struct and defines
In reply to Jeff CodyThis adds some magic number defines, and internal structure definitions for VHDX log replay support. The struct VHDXLogEntries does not reflect an on-disk data structure, and thus does not need to be packed.
Some minor code style fixes are applied as well.
--- block/vhdx.h | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-)
block/vhdx.h +30 -16
@@ -30,12 +30,12 @@* 0.........64KB...........128KB........192KB..........256KB................1MB */ -#define VHDX_HEADER_BLOCK_SIZE (64*1024) +#define VHDX_HEADER_BLOCK_SIZE (64 * 1024)#define VHDX_FILE_ID_OFFSET 0 -#define VHDX_HEADER1_OFFSET (VHDX_HEADER_BLOCK_SIZE*1) -#define VHDX_HEADER2_OFFSET (VHDX_HEADER_BLOCK_SIZE*2) -#define VHDX_REGION_TABLE_OFFSET (VHDX_HEADER_BLOCK_SIZE*3) +#define VHDX_HEADER1_OFFSET (VHDX_HEADER_BLOCK_SIZE * 1) +#define VHDX_HEADER2_OFFSET (VHDX_HEADER_BLOCK_SIZE * 2) +#define VHDX_REGION_TABLE_OFFSET (VHDX_HEADER_BLOCK_SIZE * 3)/*
@@ -77,10 +77,10 @@ typedef struct QEMU_PACKED MSGUID {#define guid_eq (a, b) \ (memcmp(&(a), &(b), sizeof(MSGUID)) == 0) -#define VHDX_HEADER_SIZE (4*1024)/* although the vhdx_header struct in disk -is only 582 bytes, for purposes of crc -the header is the first 4KB of the 64KB -block */ +#define VHDX_HEADER_SIZE (4 * 1024)/* although the vhdx_header struct in disk +is only 582 bytes, for purposes of crc +the header is the first 4KB of the 64KB +block */ /* The full header is 4KB, although the actual header data is much smaller. * But for the checksum calculation, it is over the entire 4KB structure,
@@ -92,7 +92,7 @@ typedef struct QEMU_PACKED VHDXHeader {VHDX file has 2 of these headers, and only the header with the highest sequence number is valid */ -MSGUID file_write_guid;/* 128 bit unique identifier. Must be +MSGUID file_write_guid;/* 128 bit unique identifier. Must be updated to new, unique value before the first modification is made to file */
@@ -151,7 +151,10 @@ typedef struct QEMU_PACKED VHDXRegionTableEntry {/* ---- LOG ENTRY STRUCTURES ---- */ +#define VHDX_LOG_MIN_SIZE (1024 * 1024) +#define VHDX_LOG_SECTOR_SIZE 4096#define VHDX_LOG_HDR_SIZE 64 +#define VHDX_LOG_SIGNATURE 0x65676f6c typedef struct QEMU_PACKED VHDXLogEntryHeader {uint32_t signature;/* "loge" in ASCII */ uint32_t checksum;/* CRC-32C hash of the 64KB table */
@@ -174,7 +177,8 @@ typedef struct QEMU_PACKED VHDXLogEntryHeader { } VHDXLogEntryHeader;#define VHDX_LOG_DESC_SIZE 32 - +#define VHDX_LOG_DESC_SIGNATURE 0x63736564 +#define VHDX_LOG_ZERO_SIGNATURE 0x6f72657a typedef struct QEMU_PACKED VHDXLogDescriptor {uint32_t signature;/* "zero" or "desc" in ASCII */ union {
@@ -194,6 +198,7 @@ typedef struct QEMU_PACKED VHDXLogDescriptor {vhdx_log_entry_header */ } VHDXLogDescriptor; +#define VHDX_LOG_DATA_SIGNATURE 0x61746164 typedef struct QEMU_PACKED VHDXLogDataSector {uint32_t data_signature;/* "data" in ASCII */ uint32_t sequence_high;/* 4 MSB of 8 byte sequence_number */
@@ -219,12 +224,12 @@ typedef struct QEMU_PACKED VHDXLogDataSector {#define SB_BLOCK_PRESENT 6/* per the spec */ -#define VHDX_MAX_SECTORS_PER_BLOCK (1<<23) +#define VHDX_MAX_SECTORS_PER_BLOCK (1 << 23)/* upper 44 bits are the file offset in 1MB units lower 3 bits are the state other bits are reserved */ #define VHDX_BAT_STATE_BIT_MASK 0x07 -#define VHDX_BAT_FILE_OFF_BITS (64-44) +#define VHDX_BAT_FILE_OFF_BITS (64 - 44)typedef uint64_t VHDXBatEntry ;/* ---- METADATA REGION STRUCTURES ---- */
@@ -252,8 +257,8 @@ typedef struct QEMU_PACKED VHDXMetadataTableEntry {metadata region */ /* note: if length = 0, so is offset */ uint32_t length;/* length of metadata. <= 1MB. */ -uint32_t data_bits;/* least-significant 3 bits are flags, the -rest are reserved (see above) */ +uint32_t data_bits;/* least-significant 3 bits are flags, +the rest are reserved (see above) */ uint32_t reserved2; } VHDXMetadataTableEntry;
@@ -265,8 +270,8 @@ typedef struct QEMU_PACKED VHDXMetadataTableEntry { typedef struct QEMU_PACKED VHDXFileParameters {uint32_t block_size;/* size of each payload block, always power of 2, <= 256MB and >= 1MB. */ -uint32_t data_bits;/* least-significant 2 bits are flags, the rest -are reserved (see above) */ +uint32_t data_bits;/* least-significant 2 bits are flags, +the rest are reserved (see above) */ } VHDXFileParameters; typedef struct QEMU_PACKED VHDXVirtualDiskSize {
@@ -318,6 +323,13 @@ typedef struct VHDXMetadataEntries {uint16_t present; } VHDXMetadataEntries; +typedef struct VHDXLogEntries { +uint64_t offset; +uint64_t length; +uint32_t head; +uint32_t tail; +}VHDXLogEntries ; + typedef struct BDRVVHDXState { CoMutex lock;
@@ -351,6 +363,8 @@ typedef struct BDRVVHDXState {MSGUID session_guid; +VHDXLogEntries log; +VHDXParentLocatorHeader parent_header;VHDXParentLocatorEntry *parent_entries;
Signature · 1 line
1.8.1.4
PATCH v3 5/9 block: vhdx - break endian translation functions out
In reply to Jeff CodyThis moves the endian translation functions out from the vhdx.c source, into a separate source file. In addition to the previously defined endian functions, new endian translation functions for log support are added as well.
--- block/Makefile.objs | 2 +- block/vhdx-endian.c | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++++ block/vhdx.c | 43 ---------------- block/vhdx.h | 8 +++ 4 files changed, 150 insertions(+), 44 deletions(-) create mode 100644 block/vhdx-endian.c
block/Makefile.objs +1 -1
@@ -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 block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-obj-y += qed-check.o -block-obj-$(CONFIG_VHDX) += vhdx.o +block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o block-obj-y += parallels.o blkdebug.o blkverify.o block-obj-y += snapshot.o qapi.o block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
block/vhdx-endian.c new file +141 -0
141 lines not on this page · raw message
block/vhdx.c +0 -43
49 lines not on this page · raw message
block/vhdx.h +8 -0
@@ -393,4 +393,12 @@ static inline void cpu_to_leguids(MSGUID *guid) cpu_to_le16s(&guid->data3); } +void vhdx_header_le_import (VHDXHeader *h); +void vhdx_header_le_export (VHDXHeader *orig_h,VHDXHeader *new_h); +void vhdx_log_desc_le_import (VHDXLogDescriptor *d); +void vhdx_log_desc_le_export (VHDXLogDescriptor *d); +void vhdx_log_data_le_export (VHDXLogDataSector *d); +void vhdx_log_entry_hdr_le_import (VHDXLogEntryHeader *hdr); +void vhdx_log_entry_hdr_le_export (VHDXLogEntryHeader *hdr); +#endif
Signature · 1 line
1.8.1.4
PATCH v3 6/9 block: vhdx - update log guid in header, and first write tracker
In reply to Jeff CodyAllow tracking of first file write in the VHDX image, as well as the ability to update the GUID in the header. This is in preparation for log support.
--- block/vhdx.c | 30 ++++++++++++++++++++++++------ block/vhdx.h | 6 ++++++ 2 files changed, 30 insertions(+), 6 deletions(-)
block/vhdx.c +24 -6
@@ -229,7 +229,7 @@ static int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename)* - non-current header is updated with largest sequence number */ static int vhdx_update_header (BlockDriverState *bs,BDRVVHDXState *s, -bool generate_data_write_guid) +bool generate_data_write_guid,MSGUID *log_guid) {int ret =0 ;int hdr_idx =0 ;
@@ -261,6 +261,11 @@ static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,vhdx_guid_generate (&inactive_header->data_write_guid); } +/* update the log guid if present */ +if (log_guid) { + inactive_header->log_guid = *log_guid; + } +/* the header checksum is not over just the packed size of VHDXHeader, * but rather over the entire 'reserved' range for the header, which is * 4KB (VHDX_HEADER_SIZE). */
@@ -293,16 +298,16 @@ exit:* The VHDX spec calls for header updates to be performed twice, so that both * the current and non-current header have valid info */ -static int vhdx_update_headers (BlockDriverState *bs,BDRVVHDXState *s, -bool generate_data_write_guid) +int vhdx_update_headers (BlockDriverState *bs,BDRVVHDXState *s, +bool generate_data_write_guid,MSGUID *log_guid) {int ret; - ret =vhdx_update_header (bs, s, generate_data_write_guid); + ret =vhdx_update_header (bs, s, generate_data_write_guid, log_guid);if (ret <0 ) {return ret; } - ret =vhdx_update_header (bs, s, generate_data_write_guid); + ret =vhdx_update_header (bs, s, generate_data_write_guid, log_guid);return ret; }
@@ -782,6 +787,7 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags) s->bat =NULL ; + s->first_visible_write = true;qemu_co_mutex_init (&s->lock);
@@ -862,7 +868,7 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags) }if (flags &BDRV_O_RDWR ) { - ret =vhdx_update_headers (bs, s, false); + ret =vhdx_update_headers (bs, s, false,NULL );if (ret <0 ) { goto fail; }
@@ -1002,6 +1008,18 @@ exit: +/* Per the spec, on the first write of guest-visible data to the file the +* data write guid must be updated in the header */ +int vhdx_user_visible_write (BlockDriverState *bs,BDRVVHDXState *s) +{ +int ret =0 ; +if (s->first_visible_write) { + s->first_visible_write = false; + ret =vhdx_update_headers (bs, s, true,NULL ); + } +return ret; +} + static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,int nb_sectors,QEMUIOVector *qiov) {
block/vhdx.h +6 -0
@@ -361,6 +361,7 @@ typedef struct BDRVVHDXState {VHDXBatEntry *bat;uint64_t bat_offset; +bool first_visible_write;MSGUID session_guid;VHDXLogEntries log;
@@ -372,6 +373,9 @@ typedef struct BDRVVHDXState {void vhdx_guid_generate (MSGUID *guid); +int vhdx_update_headers (BlockDriverState *bs,BDRVVHDXState *s,bool rw, +MSGUID *log_guid); +uint32_t vhdx_update_checksum (uint8_t *buf,size_t size,int crc_offset);uint32_t vhdx_checksum_calc (uint32_t crc,uint8_t *buf,size_t size,int crc_offset);
@@ -401,4 +405,6 @@ void vhdx_log_data_le_export(VHDXLogDataSector *d);void vhdx_log_entry_hdr_le_import (VHDXLogEntryHeader *hdr);void vhdx_log_entry_hdr_le_export (VHDXLogEntryHeader *hdr); +int vhdx_user_visible_write (BlockDriverState *bs,BDRVVHDXState *s); +#endif
Signature · 1 line
1.8.1.4
PATCH v3 7/9 block: vhdx - log parsing, replay, and flush support
In reply to Jeff CodyThis adds support for VHDX v0 logs, as specified in Microsoft's VHDX Specification Format v1.00:
https://www.microsoft.com/en-us/download/details.aspx?id=34750
The following support is added:
* Log parsing, and validation - validate that an existing log is correct.
* Log search - search through an existing log, to find any valid sequence of entries.
* Log replay and flush - replay an existing log, and flush/clear the log when complete.
The VHDX log is a circular buffer, with elements (sectors) of 4KB.
A log entry is a variably-length number of sectors, that is comprised of a header and 'descriptors', that describe each sector.
A log may contain multiple entries, know as a log sequence. In a log sequence, each log entry immediately follows the previous entry, with an incrementing sequence number. There can only ever be one active and valid sequence in the log.
Each log entry must match the file log GUID in order to be valid (along with other criteria). Once we have flushed all valid log entries, we marked the file log GUID to be zero, which indicates a buffer with no valid entries.
--- block/Makefile.objs | 2 +- block/vhdx-log.c | 734 ++++++++++++++++++++++++++++++++++++++++++++++++++++ block/vhdx.c | 44 +--- block/vhdx.h | 7 +- 4 files changed, 743 insertions(+), 44 deletions(-) create mode 100644 block/vhdx-log.c
block/Makefile.objs +1 -1
@@ -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 block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o block-obj-y += qed-check.o -block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o +block-obj-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o block-obj-y += parallels.o blkdebug.o blkverify.o block-obj-y += snapshot.o qapi.o block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
block/vhdx-log.c new file +734 -0
734 lines not on this page · raw message
block/vhdx.c +2 -42
62 lines not on this page · raw message
block/vhdx.h +6 -1
@@ -326,7 +326,11 @@ typedef struct VHDXMetadataEntries {typedef struct VHDXLogEntries {uint64_t offset;uint64_t length; -uint32_t head; +uint32_t write; +uint32_t read; +VHDXLogEntryHeader *hdr; +void *desc_buffer; +uint64_t sequence;uint32_t tail; }VHDXLogEntries ;
@@ -382,6 +386,7 @@ uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,bool vhdx_checksum_is_valid (uint8_t *buf,size_t size,int crc_offset); +int vhdx_parse_log (BlockDriverState *bs,BDRVVHDXState *s);static inline void leguid_to_cpus (MSGUID *guid) {
Signature · 1 line
1.8.1.4
PATCH v3 8/9 block: vhdx - add log write support
In reply to Jeff CodyThis adds support for writing to the VHDX log.
For spec details, see VHDX Specification Format v1.00:
https://www.microsoft.com/en-us/download/details.aspx?id=34750
There are a few limitations to this log support:
1.) There is no caching yet
2.) The log is flushed after each entry
The primary write interface, vhdx_log_write_and_flush(), performs a log write followed by an immediate flush of the log.
As each log entry sector is a minimum of 4KB, partial sector writes are filled in with data from the disk write destination.
If the current file log GUID is 0, a new GUID is generated and updated in the header.
--- block/vhdx-log.c | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ block/vhdx.h | 3 + 2 files changed, 280 insertions(+)
block/vhdx-log.c +277 -0
@@ -170,6 +170,55 @@ exit:return ret; } +/* Writes num_sectors to the log (all log sectors are 4096 bytes), +* from buffer 'buffer'. Upon return, *sectors_written will contain +* the number of sectors successfully written. +* +* It is assumed that 'buffer' is at least 4096*num_sectors large. +* +* 0 is returned on success, -errno otherwise */ +static int vhdx_log_write_sectors (BlockDriverState *bs,VHDXLogEntries *log, +uint32_t *sectors_written,void *buffer, +uint32_t num_sectors) +{ +int ret =0 ; +uint64_t offset; +uint32_t write; +void *buffer_tmp; +BDRVVHDXState *s = bs->opaque; + + ret =vhdx_user_visible_write (bs, s); +if (ret <0 ) { + goto exit; + } + + write = log->write; + + buffer_tmp = buffer; +while (num_sectors) { + + offset = log->offset + write; + write =vhdx_log_inc_idx (write, log->length); +if (write == log->read) { +/* full */ +break ; + } + ret =bdrv_pwrite (bs->file, offset, buffer_tmp,VHDX_LOG_SECTOR_SIZE ); +if (ret <0 ) { + goto exit; + } + buffer_tmp +=VHDX_LOG_SECTOR_SIZE ; + + log->write = write; + *sectors_written = *sectors_written +1 ; + num_sectors--; + } + +exit: +return ret; +} + +/* Validates a log entry header */ static bool vhdx_log_hdr_is_valid (VHDXLogEntries *log,VHDXLogEntryHeader *hdr,BDRVVHDXState *s)
@@ -732,3 +781,231 @@ exit:return ret; } + + +static void vhdx_log_raw_to_le_sector (VHDXLogDescriptor *desc, +VHDXLogDataSector *sector,void *data, +uint64_t seq) +{ +/* 8 + 4084 + 4 = 4096, 1 log sector */ +memcpy (&desc->leading_bytes, data,8 ); + data +=8 ; +cpu_to_le64s (&desc->leading_bytes); +memcpy (sector->data, data,4084 ); + data +=4084 ; +memcpy (&desc->trailing_bytes, data,4 ); +cpu_to_le32s (&desc->trailing_bytes); + data +=4 ; + + sector->sequence_high = (uint32_t ) (seq >>32 ); + sector->sequence_low = (uint32_t ) (seq &0xffffffff ); + sector->data_signature =VHDX_LOG_DATA_SIGNATURE ; + +vhdx_log_desc_le_export (desc); +vhdx_log_data_le_export (sector); +} + + +static int vhdx_log_write (BlockDriverState *bs,BDRVVHDXState *s, +void *data,uint32_t length,uint64_t offset) +{ +int ret =0 ; +void *buffer =NULL ; +void *merged_sector =NULL ; +void *data_tmp, *sector_write; +unsigned int i; +int sector_offset; +uint32_t desc_sectors, sectors, total_length; +uint32_t sectors_written =0 ; +uint32_t aligned_length; +uint32_t leading_length =0 ; +uint32_t trailing_length =0 ; +uint32_t partial_sectors =0 ; +uint32_t bytes_written =0 ; +uint64_t file_offset; +VHDXHeader *header; +VHDXLogEntryHeader new_hdr; +VHDXLogDescriptor *new_desc =NULL ; +VHDXLogDataSector *data_sector =NULL ; +MSGUID new_guid = {0 }; + + header = s->headers[s->curr_header]; + +/* need to have offset read data, and be on 4096 byte boundary */ + +if (length > header->log_length) { +/* no log present. we could create a log here instead of failing */ + ret = -EINVAL ; + goto exit; + } + +if (vhdx_log_guid_is_zero (&header->log_guid)) { +vhdx_guid_generate (&new_guid); +vhdx_update_headers (bs, s, false, &new_guid); + }else { +/* currently, we require that the log be flushed after +* every write. */ + ret = -ENOTSUP ; + } + +/* 0 is an invalid sequence number, but may also represent the first +* log write (or a wrapped seq) */ +if (s->log.sequence ==0 ) { + s->log.sequence =1 ; + } + + sector_offset = offset %VHDX_LOG_SECTOR_SIZE ; + file_offset = (offset /VHDX_LOG_SECTOR_SIZE ) *VHDX_LOG_SECTOR_SIZE ; + + aligned_length = length; + +/* add in the unaligned head and tail bytes */ +if (sector_offset) { + leading_length = (VHDX_LOG_SECTOR_SIZE - sector_offset); + leading_length = leading_length > length ? length : leading_length; + aligned_length -= leading_length; + partial_sectors++; + } + + sectors = aligned_length /VHDX_LOG_SECTOR_SIZE ; + trailing_length = aligned_length - (sectors *VHDX_LOG_SECTOR_SIZE ); +if (trailing_length) { + partial_sectors++; + } + + sectors += partial_sectors; + +/* sectors is now how many sectors the data itself takes, not +* including the header and descriptor metadata */ + + new_hdr = (VHDXLogEntryHeader ) { + .signature =VHDX_LOG_SIGNATURE , + .tail = s->log.tail, + .sequence_number = s->log.sequence, + .descriptor_count = sectors, + .reserved =0 , + .flushed_file_offset =bdrv_getlength (bs->file), + .last_file_offset =bdrv_getlength (bs->file), + }; + + new_hdr.log_guid = header->log_guid; + + desc_sectors =vhdx_compute_desc_sectors (new_hdr.descriptor_count); + + total_length = (desc_sectors + sectors) *VHDX_LOG_SECTOR_SIZE ; + new_hdr.entry_length = total_length; + +vhdx_log_entry_hdr_le_export (&new_hdr); + + buffer =qemu_blockalign (bs, total_length); +memcpy (buffer, &new_hdr,sizeof (new_hdr)); + + new_desc = (VHDXLogDescriptor *) (buffer +sizeof (new_hdr)); + data_sector = buffer + (desc_sectors *VHDX_LOG_SECTOR_SIZE ); + data_tmp = data; + +/* All log sectors are 4KB, so for any partial sectors we must +* merge the data with preexisting data from the final file +* destination */ + merged_sector =qemu_blockalign (bs,VHDX_LOG_SECTOR_SIZE ); + +for (i =0 ; i < sectors; i++) { + new_desc->signature =VHDX_LOG_DESC_SIGNATURE ; + new_desc->sequence_number = s->log.sequence; + new_desc->file_offset = file_offset; + +if (i ==0 && leading_length) { +/* partial sector at the front of the buffer */ + ret =bdrv_pread (bs->file, file_offset, merged_sector, +VHDX_LOG_SECTOR_SIZE ); +if (ret <0 ) { + goto exit; + } +memcpy (merged_sector + sector_offset, data_tmp, leading_length); + bytes_written = leading_length; + sector_write = merged_sector; + }else if (i == sectors -1 && trailing_length) { +/* partial sector at the end of the buffer */ + ret =bdrv_pread (bs->file, + file_offset, + merged_sector + trailing_length, +VHDX_LOG_SECTOR_SIZE - trailing_length); +if (ret <0 ) { + goto exit; + } +memcpy (merged_sector, data_tmp, trailing_length); + bytes_written = trailing_length; + sector_write = merged_sector; + }else { + bytes_written =VHDX_LOG_SECTOR_SIZE ; + sector_write = data_tmp; + } + +/* populate the raw sector data into the proper structures, +* as well as update the descriptor, and convert to proper +* endianness */ +vhdx_log_raw_to_le_sector (new_desc, data_sector, sector_write, + s->log.sequence); + + data_tmp += bytes_written; + data_sector++; + new_desc++; + file_offset +=VHDX_LOG_SECTOR_SIZE ; + } + +/* checksum covers entire entry, from the log header through the +* last data sector */ +vhdx_update_checksum (buffer, total_length, + offsetof(VHDXLogEntryHeader , checksum)); +cpu_to_le32s ((uint32_t *)(buffer +4 )); + +/* now write to the log */ +vhdx_log_write_sectors (bs, &s->log, §ors_written, buffer, + desc_sectors + sectors); +if (ret <0 ) { + goto exit; + } + +if (sectors_written != desc_sectors + sectors) { +/* instead of failing, we could flush the log here */ + ret = -EINVAL ; + goto exit; + } + + s->log.sequence++; +/* write new tail */ + s->log.tail = s->log.write; + +exit: +qemu_vfree (buffer); +qemu_vfree (merged_sector); +return ret; +} + +/* Perform a log write, and then immediately flush the entire log */ +int vhdx_log_write_and_flush (BlockDriverState *bs,BDRVVHDXState *s, +void *data,uint32_t length,uint64_t offset) +{ +int ret =0 ; +VHDXLogSequence logs = { .valid = true, + .count =1 , + .hdr = {0 } }; + + + ret =vhdx_log_write (bs, s, data, length, offset); +if (ret <0 ) { + goto exit; + } + logs.log = s->log; + + ret =vhdx_log_flush (bs, s, &logs); +if (ret <0 ) { + goto exit; + } + + s->log = logs.log; + +exit: +return ret; +} +
block/vhdx.h +3 -0
@@ -388,6 +388,9 @@ bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);int vhdx_parse_log (BlockDriverState *bs,BDRVVHDXState *s); +int vhdx_log_write_and_flush (BlockDriverState *bs,BDRVVHDXState *s, +void *data,uint32_t length,uint64_t offset); +static inline void leguid_to_cpus (MSGUID *guid) {le32_to_cpus (&guid->data1);
Signature · 1 line
1.8.1.4
PATCH v3 9/9 block: vhdx write support
In reply to Jeff CodyThis adds support for writing to VHDX image files, using coroutines. Writes into the BAT table goes through the VHDX log. Currently, BAT table writes occur when expanding a dynamic VHDX file, and allocating a new BAT entry.
--- block/vhdx.c | 150 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-)
block/vhdx.c +148 -2
@@ -833,7 +833,7 @@ static int vhdx_open(BlockDriverState *bs, QDict *options, int flags) } } -/* TODO: differencing files, write */ +/* TODO: differencing files */ return 0 ; fail:
@@ -965,7 +965,43 @@ exit:return ret; } +/* +* Allocate a new payload block at the end of the file. +* +* Allocation will happen at 1MB alignment inside the file +* +* Returns the file offset start of the new payload block +*/ +static int vhdx_allocate_block (BlockDriverState *bs,BDRVVHDXState *s, +uint64_t *new_offset) +{ + *new_offset =bdrv_getlength (bs->file); + +/* per the spec, the address for a block is in units of 1MB */ + *new_offset =ROUND_UP (*new_offset,1024 *1024 ); + +return bdrv_truncate (bs->file, *new_offset + s->block_size); +} + +/* +* Update the BAT tablet entry with the new file offset, and the new entry +* state */ +static void vhdx_update_bat_table_entry (BlockDriverState *bs,BDRVVHDXState *s, +VHDXSectorInfo *sinfo, +uint64_t *bat_entry, +uint64_t *bat_offset,int state) +{ +/* The BAT entry is a uint64, with 44 bits for the file offset in units of +* 1MB, and 3 bits for the block state. */ + s->bat[sinfo->bat_idx] = ((sinfo->file_offset>>20 ) << +VHDX_BAT_FILE_OFF_BITS ); + s->bat[sinfo->bat_idx] |= state &VHDX_BAT_STATE_BIT_MASK ; + + *bat_entry =cpu_to_le64 (s->bat[sinfo->bat_idx]); + *bat_offset = s->bat_offset + sinfo->bat_idx *sizeof (VHDXBatEntry); + +}/* Per the spec, on the first write of guest-visible data to the file the * data write guid must be updated in the header */
@@ -982,7 +1018,117 @@ int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s) static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,int nb_sectors,QEMUIOVector *qiov) { -return -ENOTSUP ; +int ret = -ENOTSUP ; +BDRVVHDXState *s = bs->opaque; +VHDXSectorInfo sinfo; +uint64_t bytes_done =0 ; +uint64_t bat_entry =0 ; +uint64_t bat_entry_offset =0 ; +bool bat_update; +QEMUIOVector hd_qiov; + +qemu_iovec_init (&hd_qiov, qiov->niov); + +qemu_co_mutex_lock (&s->lock); + + ret =vhdx_user_visible_write (bs, s); +if (ret <0 ) { + goto exit; + } + +while (nb_sectors >0 ) { +if (s->params.data_bits &VHDX_PARAMS_HAS_PARENT ) { +/* not supported yet */ + ret = -ENOTSUP ; + goto exit; + }else { + bat_update = false; +vhdx_block_translate (s, sector_num, nb_sectors, &sinfo); + +qemu_iovec_reset (&hd_qiov); +qemu_iovec_concat (&hd_qiov, qiov, bytes_done, sinfo.bytes_avail); +/* check the payload block state */ +switch (s->bat[sinfo.bat_idx] &VHDX_BAT_STATE_BIT_MASK ) { +case PAYLOAD_BLOCK_ZERO : +/* in this case, we need to preserve zero writes for +* data that is not part of this write, so we must pad +* the rest of the buffer to zeroes */ + +/* if we are on a posix system with ftruncate() that extends +* a file, then it is zero-filled for us. On Win32, the raw +* layer uses SetFilePointer and SetFileEnd, which does not +* zero fill AFAIK */ + +/* TODO: queue another write of zero buffers if the host OS does +* not zero-fill on file extension */ + +/* fall through */ +case PAYLOAD_BLOCK_NOT_PRESENT :/* fall through */ +case PAYLOAD_BLOCK_UNMAPPED :/* fall through */ +case PAYLOAD_BLOCK_UNDEFINED :/* fall through */ + ret =vhdx_allocate_block (bs, s, &sinfo.file_offset); +if (ret <0 ) { + goto exit; + } +/* once we support differencing files, this may also be +* partially present */ +/* update block state to the newly specified state */ +vhdx_update_bat_table_entry (bs, s, &sinfo, &bat_entry, + &bat_entry_offset, +PAYLOAD_BLOCK_FULL_PRESENT ); + bat_update = true; +/* since we just allocated a block, file_offset is the +* beginning of the payload block. It needs to be the +* write address, which includes the offset into the block */ + sinfo.file_offset += sinfo.block_offset; +/* fall through */ +case PAYLOAD_BLOCK_FULL_PRESENT : +/* if the file offset address is in the header zone, +* there is a problem */ +if (sinfo.file_offset < (1024 *1024 )) { + ret = -EFAULT ; + goto exit; + } +/* block exists, so we can just overwrite it */ +qemu_co_mutex_unlock (&s->lock); + ret =bdrv_co_writev (bs->file, + sinfo.file_offset>>BDRV_SECTOR_BITS , + sinfo.sectors_avail, &hd_qiov); +qemu_co_mutex_lock (&s->lock); +if (ret <0 ) { + goto exit; + } +break ; +case PAYLOAD_BLOCK_PARTIALLY_PRESENT : +/* we don't yet support difference files, fall through +* to error */ +default : + ret = -EIO ; + goto exit; +break ; + } + +if (bat_update) { +/* this will update the BAT entry into the log journal, and +* then flush the log journal out to disk */ + ret =vhdx_log_write_and_flush (bs, s, &bat_entry, +sizeof (VHDXBatEntry), + bat_entry_offset); +if (ret <0 ) { + goto exit; + } + } + + nb_sectors -= sinfo.sectors_avail; + sector_num += sinfo.sectors_avail; + bytes_done += sinfo.bytes_avail; + + } + } + +exit: +qemu_co_mutex_unlock (&s->lock); +return ret; }
Signature · 1 line
1.8.1.4
Quoted 47 lines · Jeff Cody
This patch series contains the initial VHDX log parsing, replay, and write support.
=== v3 changes ===
Thank you Kevin & Stefan for the feedback; incoporated in v3:
Patch 1: --- nil ---
Patch 2: * use sizeof(crc) instead of 4
* remove outdated comment
* use sizeof(MSGUID) instead of 16
* direct assignment of guid structs rather than memcpy
* rename 'rw' to 'generate_data_write_guid'
* useoffsetof()instead of 4
* comment typos
* add missing error checking
* MSGUID is nowQEMU_PACKED
* configure enable for vhdx is now correct and not braindeadPatch 3: --- nil ---
Patch 4: * code style fixes
* removed unused struct (VHDXLogEntryInfo)Patch 5: * more direct assignment of guid rather than memcpy
* order of operation in export/import the same now
* became less generous with newlines (bah-humbug!)Patch 6: * more direct assignment of guid rather than memcpy
* add error check invhdx_user_visible_write(), now returns intPatch 7: * check error return now of
vhdx_user_visible_write()Patch 8: * check error return now of
vhdx_user_visible_write()
*vhdx_log_write_sectors()usesbdrv_pwrite()vsbdrv_pwrite_sync()
* more direct assignment of guid rather than memcpy
* useoffsetof()instead of 4Patch 9: --- nil ---
=== end v3 changelog ===
v2 changes: Incorporated Fam's review feedback - Thank you Fam for the feedback
=== end v2 changelog ===
Quoted 7 lines
This will allow an existing log in a VHDX image to be replayed (e.g., a VHDX image from a Hyper-V host that crashed). In addition, metadata writes are enabled through the log. This allows write support to be enabled for VHDX, as the BAT can be updated safely via the log journal.
These exact patches are available from github, for testing:
https://github.com/codyprime/qemu-kvm-jtc/tree/vhdx-write-v2-upstream
I meant to mention, that the v3 patches are available from github, for testing:
https://github.com/codyprime/qemu-kvm-jtc/tree/vhdx-write-v3-upstream
Quoted 30 lines
The latest vhdx work (including anything beyond these patches, such as backing/parent file support) can be found at:
https://github.com/codyprime/qemu-kvm-jtc/tree/jtc-vhdx-latestJeff Cody (9):
block: vhdx - minor comments and typo correction.
block: vhdx - add header update capability.
block: vhdx code movement - VHDXMetadataEntries and BDRVVHDXState to
header.
block: vhdx - log support struct and defines
block: vhdx - break endian translation functions out
block: vhdx - update log guid in header, and first write tracker
block: vhdx - log parsing, replay, and flush support
block: vhdx - add log write support
block: vhdx write supportblock/Makefile.objs | 2 +- block/vhdx-endian.c | 141 +++++++ block/vhdx-log.c | 1011 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/vhdx.c | 399 ++++++++++++++------ block/vhdx.h | 135 +++++-- configure | 24 ++ 6 files changed, 1585 insertions(+), 127 deletions(-) create mode 100644 block/vhdx-endian.c create mode 100644 block/vhdx-log.cSignature · 1 line
1.8.1.4