2 * Block driver for Hyper-V VHDX Images
4 * Copyright (c) 2013 Red Hat, Inc.,
7 * Jeff Cody <jcody@redhat.com>
9 * This is based on the "VHDX Format Specification v1.00", published 8/25/2012
11 * https://www.microsoft.com/en-us/download/details.aspx?id=34750
13 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14 * See the COPYING.LIB file in the top-level directory.
20 #include "qemu/units.h"
22 #define DEFAULT_LOG_SIZE 1048576 /* 1MiB */
23 /* Structures and fields present in the VHDX file */
25 /* The header section has the following blocks,
28 * _____________________________________________________________________________
29 * | File Id. | Header 1 | Header 2 | Region Table | Reserved (768KB) |
30 * |----------|---------------|------------|--------------|--------------------|
32 * 0.........64KB...........128KB........192KB..........256KB................1MB
35 #define VHDX_HEADER_BLOCK_SIZE (64 * 1024)
37 #define VHDX_FILE_ID_OFFSET 0
38 #define VHDX_HEADER1_OFFSET (VHDX_HEADER_BLOCK_SIZE * 1)
39 #define VHDX_HEADER2_OFFSET (VHDX_HEADER_BLOCK_SIZE * 2)
40 #define VHDX_REGION_TABLE_OFFSET (VHDX_HEADER_BLOCK_SIZE * 3)
41 #define VHDX_REGION_TABLE2_OFFSET (VHDX_HEADER_BLOCK_SIZE * 4)
43 #define VHDX_HEADER_SECTION_END (1 * MiB)
45 * A note on the use of MS-GUID fields. For more details on the GUID,
46 * please see: https://en.wikipedia.org/wiki/Globally_unique_identifier.
48 * The VHDX specification only states that these are MS GUIDs, and which
49 * bytes are data1-data4. It makes no mention of what algorithm should be used
50 * to generate the GUID, nor what standard. However, looking at the specified
51 * known GUID fields, it appears the GUIDs are:
52 * Standard/DCE GUID type (noted by 10b in the MSB of byte 0 of .data4)
53 * Random algorithm (noted by 0x4XXX for .data3)
56 /* ---- HEADER SECTION STRUCTURES ---- */
58 /* These structures are ones that are defined in the VHDX specification
61 #define VHDX_FILE_SIGNATURE 0x656C696678646876ULL /* "vhdxfile" in ASCII */
62 typedef struct VHDXFileIdentifier
{
63 uint64_t signature
; /* "vhdxfile" in ASCII */
64 uint16_t creator
[256]; /* optional; utf-16 string to identify
65 the vhdx file creator. Diagnostic
70 /* the guid is a 16 byte unique ID - the definition for this used by
71 * Microsoft is not just 16 bytes though - it is a structure that is defined,
72 * so we need to follow it here so that endianness does not trip us up */
74 typedef struct QEMU_PACKED MSGUID
{
81 #define guid_eq(a, b) \
82 (memcmp(&(a), &(b), sizeof(MSGUID)) == 0)
84 #define VHDX_HEADER_SIZE (4 * 1024) /* although the vhdx_header struct in disk
85 is only 582 bytes, for purposes of crc
86 the header is the first 4KB of the 64KB
89 /* The full header is 4KB, although the actual header data is much smaller.
90 * But for the checksum calculation, it is over the entire 4KB structure,
91 * not just the defined portion of it */
92 #define VHDX_HEADER_SIGNATURE 0x64616568
93 typedef struct QEMU_PACKED VHDXHeader
{
94 uint32_t signature
; /* "head" in ASCII */
95 uint32_t checksum
; /* CRC-32C hash of the whole header */
96 uint64_t sequence_number
; /* Seq number of this header. Each
97 VHDX file has 2 of these headers,
98 and only the header with the highest
99 sequence number is valid */
100 MSGUID file_write_guid
; /* 128 bit unique identifier. Must be
101 updated to new, unique value before
102 the first modification is made to
104 MSGUID data_write_guid
; /* 128 bit unique identifier. Must be
105 updated to new, unique value before
106 the first modification is made to
107 visible data. Visbile data is
109 - system & user metadata
112 - any change that will
113 cause the virtual disk
114 sector read to differ
116 This does not need to change if
117 blocks are re-arranged */
118 MSGUID log_guid
; /* 128 bit unique identifier. If zero,
119 there is no valid log. If non-zero,
120 log entries with this guid are
122 uint16_t log_version
; /* version of the log format. Must be
124 uint16_t version
; /* version of the vhdx file. Currently,
125 only supported version is "1" */
126 uint32_t log_length
; /* length of the log. Must be multiple
128 uint64_t log_offset
; /* byte offset in the file of the log.
129 Must also be a multiple of 1MB */
132 /* Header for the region table block */
133 #define VHDX_REGION_SIGNATURE 0x69676572 /* "regi" in ASCII */
134 typedef struct QEMU_PACKED VHDXRegionTableHeader
{
135 uint32_t signature
; /* "regi" in ASCII */
136 uint32_t checksum
; /* CRC-32C hash of the 64KB table */
137 uint32_t entry_count
; /* number of valid entries */
139 } VHDXRegionTableHeader
;
141 /* Individual region table entry. There may be a maximum of 2047 of these
143 * There are two known region table properties. Both are required.
144 * BAT (block allocation table): 2DC27766F62342009D64115E9BFD4A08
145 * Metadata: 8B7CA20647904B9AB8FE575F050F886E
147 #define VHDX_REGION_ENTRY_REQUIRED 0x01 /* if set, parser must understand
148 this entry in order to open
150 typedef struct QEMU_PACKED VHDXRegionTableEntry
{
151 MSGUID guid
; /* 128-bit unique identifier */
152 uint64_t file_offset
; /* offset of the object in the file.
153 Must be multiple of 1MB */
154 uint32_t length
; /* length, in bytes, of the object */
156 } VHDXRegionTableEntry
;
159 /* ---- LOG ENTRY STRUCTURES ---- */
160 #define VHDX_LOG_MIN_SIZE (1024 * 1024)
161 #define VHDX_LOG_SECTOR_SIZE 4096
162 #define VHDX_LOG_HDR_SIZE 64
163 #define VHDX_LOG_SIGNATURE 0x65676f6c
164 typedef struct QEMU_PACKED VHDXLogEntryHeader
{
165 uint32_t signature
; /* "loge" in ASCII */
166 uint32_t checksum
; /* CRC-32C hash of the 64KB table */
167 uint32_t entry_length
; /* length in bytes, multiple of 1MB */
168 uint32_t tail
; /* byte offset of first log entry of a
169 seq, where this entry is the last
171 uint64_t sequence_number
; /* incremented with each log entry.
173 uint32_t descriptor_count
; /* number of descriptors in this log
174 entry, must be >= 0 */
176 MSGUID log_guid
; /* value of the log_guid from
177 vhdx_header. If not found in
178 vhdx_header, it is invalid */
179 uint64_t flushed_file_offset
; /* see spec for full details - this
180 should be vhdx file size in bytes */
181 uint64_t last_file_offset
; /* size in bytes that all allocated
182 file structures fit into */
183 } VHDXLogEntryHeader
;
185 #define VHDX_LOG_DESC_SIZE 32
186 #define VHDX_LOG_DESC_SIGNATURE 0x63736564
187 #define VHDX_LOG_ZERO_SIGNATURE 0x6f72657a
188 typedef struct QEMU_PACKED VHDXLogDescriptor
{
189 uint32_t signature
; /* "zero" or "desc" in ASCII */
191 uint32_t reserved
; /* zero desc */
192 uint32_t trailing_bytes
; /* data desc: bytes 4092-4096 of the
196 uint64_t zero_length
; /* zero desc: length of the section to
198 uint64_t leading_bytes
; /* data desc: bytes 0-7 of the data
201 uint64_t file_offset
; /* file offset to write zeros - multiple
203 uint64_t sequence_number
; /* must match same field in
204 vhdx_log_entry_header */
207 #define VHDX_LOG_DATA_SIGNATURE 0x61746164
208 typedef struct QEMU_PACKED VHDXLogDataSector
{
209 uint32_t data_signature
; /* "data" in ASCII */
210 uint32_t sequence_high
; /* 4 MSB of 8 byte sequence_number */
211 uint8_t data
[4084]; /* raw data, bytes 8-4091 (inclusive).
212 see the data descriptor field for the
213 other mising bytes */
214 uint32_t sequence_low
; /* 4 LSB of 8 byte sequence_number */
219 /* block states - different state values depending on whether it is a
220 * payload block, or a sector block. */
222 #define PAYLOAD_BLOCK_NOT_PRESENT 0
223 #define PAYLOAD_BLOCK_UNDEFINED 1
224 #define PAYLOAD_BLOCK_ZERO 2
225 #define PAYLOAD_BLOCK_UNMAPPED 3
226 #define PAYLOAD_BLOCK_UNMAPPED_v095 5
227 #define PAYLOAD_BLOCK_FULLY_PRESENT 6
228 #define PAYLOAD_BLOCK_PARTIALLY_PRESENT 7
230 #define SB_BLOCK_NOT_PRESENT 0
231 #define SB_BLOCK_PRESENT 6
234 #define VHDX_MAX_SECTORS_PER_BLOCK (1 << 23)
236 /* upper 44 bits are the file offset in 1MB units lower 3 bits are the state
237 other bits are reserved */
238 #define VHDX_BAT_STATE_BIT_MASK 0x07
239 #define VHDX_BAT_FILE_OFF_MASK 0xFFFFFFFFFFF00000ULL /* upper 44 bits */
240 typedef uint64_t VHDXBatEntry
;
242 /* ---- METADATA REGION STRUCTURES ---- */
244 #define VHDX_METADATA_ENTRY_SIZE 32
245 #define VHDX_METADATA_MAX_ENTRIES 2047 /* not including the header */
246 #define VHDX_METADATA_TABLE_MAX_SIZE \
247 (VHDX_METADATA_ENTRY_SIZE * (VHDX_METADATA_MAX_ENTRIES+1))
248 #define VHDX_METADATA_SIGNATURE 0x617461646174656DULL /* "metadata" in ASCII */
249 typedef struct QEMU_PACKED VHDXMetadataTableHeader
{
250 uint64_t signature
; /* "metadata" in ASCII */
252 uint16_t entry_count
; /* number table entries. <= 2047 */
253 uint32_t reserved2
[5];
254 } VHDXMetadataTableHeader
;
256 #define VHDX_META_FLAGS_IS_USER 0x01 /* max 1024 entries */
257 #define VHDX_META_FLAGS_IS_VIRTUAL_DISK 0x02 /* virtual disk metadata if set,
258 otherwise file metdata */
259 #define VHDX_META_FLAGS_IS_REQUIRED 0x04 /* parse must understand this
260 entry to open the file */
261 typedef struct QEMU_PACKED VHDXMetadataTableEntry
{
262 MSGUID item_id
; /* 128-bit identifier for metadata */
263 uint32_t offset
; /* byte offset of the metadata. At
264 least 64kB. Relative to start of
266 /* note: if length = 0, so is offset */
267 uint32_t length
; /* length of metadata. <= 1MB. */
268 uint32_t data_bits
; /* least-significant 3 bits are flags,
269 the rest are reserved (see above) */
271 } VHDXMetadataTableEntry
;
273 #define VHDX_PARAMS_LEAVE_BLOCKS_ALLOCED 0x01 /* Do not change any blocks to
274 be BLOCK_NOT_PRESENT.
275 If set indicates a fixed
277 #define VHDX_PARAMS_HAS_PARENT 0x02 /* has parent / backing file */
278 #define VHDX_BLOCK_SIZE_MIN (1 * MiB)
279 #define VHDX_BLOCK_SIZE_MAX (256 * MiB)
280 typedef struct QEMU_PACKED VHDXFileParameters
{
281 uint32_t block_size
; /* size of each payload block, always
282 power of 2, <= 256MB and >= 1MB. */
283 uint32_t data_bits
; /* least-significant 2 bits are flags,
284 the rest are reserved (see above) */
285 } VHDXFileParameters
;
287 #define VHDX_MAX_IMAGE_SIZE ((uint64_t) 64 * TiB)
288 typedef struct QEMU_PACKED VHDXVirtualDiskSize
{
289 uint64_t virtual_disk_size
; /* Size of the virtual disk, in bytes.
290 Must be multiple of the sector size,
292 } VHDXVirtualDiskSize
;
294 typedef struct QEMU_PACKED VHDXPage83Data
{
295 MSGUID page_83_data
; /* unique id for scsi devices that
299 typedef struct QEMU_PACKED VHDXVirtualDiskLogicalSectorSize
{
300 uint32_t logical_sector_size
; /* virtual disk sector size (in bytes).
301 Can only be 512 or 4096 bytes */
302 } VHDXVirtualDiskLogicalSectorSize
;
304 typedef struct QEMU_PACKED VHDXVirtualDiskPhysicalSectorSize
{
305 uint32_t physical_sector_size
; /* physical sector size (in bytes).
306 Can only be 512 or 4096 bytes */
307 } VHDXVirtualDiskPhysicalSectorSize
;
309 typedef struct QEMU_PACKED VHDXParentLocatorHeader
{
310 MSGUID locator_type
; /* type of the parent virtual disk. */
312 uint16_t key_value_count
; /* number of key/value pairs for this
314 } VHDXParentLocatorHeader
;
316 /* key and value strings are UNICODE strings, UTF-16 LE encoding, no NULs */
317 typedef struct QEMU_PACKED VHDXParentLocatorEntry
{
318 uint32_t key_offset
; /* offset in metadata for key, > 0 */
319 uint32_t value_offset
; /* offset in metadata for value, >0 */
320 uint16_t key_length
; /* length of entry key, > 0 */
321 uint16_t value_length
; /* length of entry value, > 0 */
322 } VHDXParentLocatorEntry
;
325 /* ----- END VHDX SPECIFICATION STRUCTURES ---- */
327 typedef struct VHDXMetadataEntries
{
328 VHDXMetadataTableEntry file_parameters_entry
;
329 VHDXMetadataTableEntry virtual_disk_size_entry
;
330 VHDXMetadataTableEntry page83_data_entry
;
331 VHDXMetadataTableEntry logical_sector_size_entry
;
332 VHDXMetadataTableEntry phys_sector_size_entry
;
333 VHDXMetadataTableEntry parent_locator_entry
;
335 } VHDXMetadataEntries
;
337 typedef struct VHDXLogEntries
{
342 VHDXLogEntryHeader
*hdr
;
348 typedef struct VHDXRegionEntry
{
351 QLIST_ENTRY(VHDXRegionEntry
) entries
;
354 typedef struct BDRVVHDXState
{
358 VHDXHeader
*headers
[2];
360 VHDXRegionTableHeader rt
;
361 VHDXRegionTableEntry bat_rt
; /* region table for the BAT */
362 VHDXRegionTableEntry metadata_rt
; /* region table for the metadata */
364 VHDXMetadataTableHeader metadata_hdr
;
365 VHDXMetadataEntries metadata_entries
;
367 VHDXFileParameters params
;
369 uint32_t block_size_bits
;
370 uint32_t sectors_per_block
;
371 uint32_t sectors_per_block_bits
;
373 uint64_t virtual_disk_size
;
374 uint32_t logical_sector_size
;
375 uint32_t physical_sector_size
;
377 uint64_t chunk_ratio
;
378 uint32_t chunk_ratio_bits
;
379 uint32_t logical_sector_size_bits
;
381 uint32_t bat_entries
;
385 bool first_visible_write
;
390 VHDXParentLocatorHeader parent_header
;
391 VHDXParentLocatorEntry
*parent_entries
;
393 Error
*migration_blocker
;
395 bool log_replayed_on_open
;
397 QLIST_HEAD(, VHDXRegionEntry
) regions
;
400 void vhdx_guid_generate(MSGUID
*guid
);
402 int vhdx_update_headers(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool rw
,
405 uint32_t vhdx_update_checksum(uint8_t *buf
, size_t size
, int crc_offset
);
406 uint32_t vhdx_checksum_calc(uint32_t crc
, uint8_t *buf
, size_t size
,
409 bool vhdx_checksum_is_valid(uint8_t *buf
, size_t size
, int crc_offset
);
411 int vhdx_parse_log(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool *flushed
,
414 int vhdx_log_write_and_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
415 void *data
, uint32_t length
, uint64_t offset
);
417 static inline void leguid_to_cpus(MSGUID
*guid
)
419 guid
->data1
= le32_to_cpu(guid
->data1
);
420 guid
->data2
= le16_to_cpu(guid
->data2
);
421 guid
->data3
= le16_to_cpu(guid
->data3
);
424 static inline void cpu_to_leguids(MSGUID
*guid
)
426 guid
->data1
= cpu_to_le32(guid
->data1
);
427 guid
->data2
= cpu_to_le16(guid
->data2
);
428 guid
->data3
= cpu_to_le16(guid
->data3
);
431 void vhdx_header_le_import(VHDXHeader
*h
);
432 void vhdx_header_le_export(VHDXHeader
*orig_h
, VHDXHeader
*new_h
);
433 void vhdx_log_desc_le_import(VHDXLogDescriptor
*d
);
434 void vhdx_log_desc_le_export(VHDXLogDescriptor
*d
);
435 void vhdx_log_data_le_import(VHDXLogDataSector
*d
);
436 void vhdx_log_data_le_export(VHDXLogDataSector
*d
);
437 void vhdx_log_entry_hdr_le_import(VHDXLogEntryHeader
*hdr
);
438 void vhdx_log_entry_hdr_le_export(VHDXLogEntryHeader
*hdr
);
439 void vhdx_region_header_le_import(VHDXRegionTableHeader
*hdr
);
440 void vhdx_region_header_le_export(VHDXRegionTableHeader
*hdr
);
441 void vhdx_region_entry_le_import(VHDXRegionTableEntry
*e
);
442 void vhdx_region_entry_le_export(VHDXRegionTableEntry
*e
);
443 void vhdx_metadata_header_le_import(VHDXMetadataTableHeader
*hdr
);
444 void vhdx_metadata_header_le_export(VHDXMetadataTableHeader
*hdr
);
445 void vhdx_metadata_entry_le_import(VHDXMetadataTableEntry
*e
);
446 void vhdx_metadata_entry_le_export(VHDXMetadataTableEntry
*e
);
447 int vhdx_user_visible_write(BlockDriverState
*bs
, BDRVVHDXState
*s
);