]> Git Repo - qemu.git/blob - block/vhdx.h
block/vhdx: Remove redundant IEC binary prefixes definition
[qemu.git] / block / vhdx.h
1 /*
2  * Block driver for Hyper-V VHDX Images
3  *
4  * Copyright (c) 2013 Red Hat, Inc.,
5  *
6  * Authors:
7  *  Jeff Cody <[email protected]>
8  *
9  *  This is based on the "VHDX Format Specification v1.00", published 8/25/2012
10  *  by Microsoft:
11  *      https://www.microsoft.com/en-us/download/details.aspx?id=34750
12  *
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.
15  *
16  */
17
18 #ifndef BLOCK_VHDX_H
19 #define BLOCK_VHDX_H
20 #include "qemu/units.h"
21
22 #define DEFAULT_LOG_SIZE 1048576 /* 1MiB */
23 /* Structures and fields present in the VHDX file */
24
25 /* The header section has the following blocks,
26  * each block is 64KB:
27  *
28  * _____________________________________________________________________________
29  * | File Id. |   Header 1    | Header 2   | Region Table |  Reserved (768KB)  |
30  * |----------|---------------|------------|--------------|--------------------|
31  * |          |               |            |              |                    |
32  * 0.........64KB...........128KB........192KB..........256KB................1MB
33  */
34
35 #define VHDX_HEADER_BLOCK_SIZE      (64 * 1024)
36
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)
42
43 #define VHDX_HEADER_SECTION_END     (1 * MiB)
44 /*
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.
47  *
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)
54  */
55
56 /* ---- HEADER SECTION STRUCTURES ---- */
57
58 /* These structures are ones that are defined in the VHDX specification
59  * document */
60
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
66                                            only */
67 } VHDXFileIdentifier;
68
69
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 */
73
74 typedef struct QEMU_PACKED MSGUID {
75     uint32_t  data1;
76     uint16_t  data2;
77     uint16_t  data3;
78     uint8_t   data4[8];
79 } MSGUID;
80
81 #define guid_eq(a, b) \
82     (memcmp(&(a), &(b), sizeof(MSGUID)) == 0)
83
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
87                                          block */
88
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
103                                            file */
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
108                                            defined as:
109                                                     - system & user metadata
110                                                     - raw block data
111                                                     - disk size
112                                                     - any change that will
113                                                       cause the virtual disk
114                                                       sector read to differ
115
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
121                                            valid. */
122     uint16_t    log_version;            /* version of the log format. Must be
123                                            set to zero */
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
127                                            of 1MB */
128     uint64_t    log_offset;             /* byte offset in the file of the log.
129                                            Must also be a multiple of 1MB */
130 } VHDXHeader;
131
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 */
138     uint32_t    reserved;
139 } VHDXRegionTableHeader;
140
141 /* Individual region table entry.  There may be a maximum of 2047 of these
142  *
143  *  There are two known region table properties.  Both are required.
144  *  BAT (block allocation table):  2DC27766F62342009D64115E9BFD4A08
145  *  Metadata:                      8B7CA20647904B9AB8FE575F050F886E
146  */
147 #define VHDX_REGION_ENTRY_REQUIRED  0x01    /* if set, parser must understand
148                                                this entry in order to open
149                                                file */
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 */
155     uint32_t    data_bits;
156 } VHDXRegionTableEntry;
157
158
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
170                                            entry */
171     uint64_t    sequence_number;        /* incremented with each log entry.
172                                            May not be zero. */
173     uint32_t    descriptor_count;       /* number of descriptors in this log
174                                            entry, must be >= 0 */
175     uint32_t    reserved;
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;
184
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 */
190     union  {
191         uint32_t    reserved;           /* zero desc */
192         uint32_t    trailing_bytes;     /* data desc: bytes 4092-4096 of the
193                                            data sector */
194     };
195     union {
196         uint64_t    zero_length;        /* zero desc: length of the section to
197                                            zero */
198         uint64_t    leading_bytes;      /* data desc: bytes 0-7 of the data
199                                            sector */
200     };
201     uint64_t    file_offset;            /* file offset to write zeros - multiple
202                                            of 4kB */
203     uint64_t    sequence_number;        /* must match same field in
204                                            vhdx_log_entry_header */
205 } VHDXLogDescriptor;
206
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 */
215 } VHDXLogDataSector;
216
217
218
219 /* block states - different state values depending on whether it is a
220  * payload block, or a sector block. */
221
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
229
230 #define SB_BLOCK_NOT_PRESENT    0
231 #define SB_BLOCK_PRESENT        6
232
233 /* per the spec */
234 #define VHDX_MAX_SECTORS_PER_BLOCK  (1 << 23)
235
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;
241
242 /* ---- METADATA REGION STRUCTURES ---- */
243
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 */
251     uint16_t    reserved;
252     uint16_t    entry_count;            /* number table entries. <= 2047 */
253     uint32_t    reserved2[5];
254 } VHDXMetadataTableHeader;
255
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
265                                            metadata region */
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) */
270     uint32_t    reserved2;
271 } VHDXMetadataTableEntry;
272
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
276                                                    size VHDX file */
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;
286
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,
291                                            max of 64TB */
292 } VHDXVirtualDiskSize;
293
294 typedef struct QEMU_PACKED VHDXPage83Data {
295     MSGUID      page_83_data;           /* unique id for scsi devices that
296                                            support page 0x83 */
297 } VHDXPage83Data;
298
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;
303
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;
308
309 typedef struct QEMU_PACKED VHDXParentLocatorHeader {
310     MSGUID      locator_type;           /* type of the parent virtual disk. */
311     uint16_t    reserved;
312     uint16_t    key_value_count;        /* number of key/value pairs for this
313                                            locator */
314 } VHDXParentLocatorHeader;
315
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;
323
324
325 /* ----- END VHDX SPECIFICATION STRUCTURES ---- */
326
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;
334     uint16_t present;
335 } VHDXMetadataEntries;
336
337 typedef struct VHDXLogEntries {
338     uint64_t offset;
339     uint64_t length;
340     uint32_t write;
341     uint32_t read;
342     VHDXLogEntryHeader *hdr;
343     void *desc_buffer;
344     uint64_t sequence;
345     uint32_t tail;
346 } VHDXLogEntries;
347
348 typedef struct VHDXRegionEntry {
349     uint64_t start;
350     uint64_t end;
351     QLIST_ENTRY(VHDXRegionEntry) entries;
352 } VHDXRegionEntry;
353
354 typedef struct BDRVVHDXState {
355     CoMutex lock;
356
357     int curr_header;
358     VHDXHeader *headers[2];
359
360     VHDXRegionTableHeader rt;
361     VHDXRegionTableEntry bat_rt;         /* region table for the BAT */
362     VHDXRegionTableEntry metadata_rt;    /* region table for the metadata */
363
364     VHDXMetadataTableHeader metadata_hdr;
365     VHDXMetadataEntries metadata_entries;
366
367     VHDXFileParameters params;
368     uint32_t block_size;
369     uint32_t block_size_bits;
370     uint32_t sectors_per_block;
371     uint32_t sectors_per_block_bits;
372
373     uint64_t virtual_disk_size;
374     uint32_t logical_sector_size;
375     uint32_t physical_sector_size;
376
377     uint64_t chunk_ratio;
378     uint32_t chunk_ratio_bits;
379     uint32_t logical_sector_size_bits;
380
381     uint32_t bat_entries;
382     VHDXBatEntry *bat;
383     uint64_t bat_offset;
384
385     bool first_visible_write;
386     MSGUID session_guid;
387
388     VHDXLogEntries log;
389
390     VHDXParentLocatorHeader parent_header;
391     VHDXParentLocatorEntry *parent_entries;
392
393     Error *migration_blocker;
394
395     bool log_replayed_on_open;
396
397     QLIST_HEAD(, VHDXRegionEntry) regions;
398 } BDRVVHDXState;
399
400 void vhdx_guid_generate(MSGUID *guid);
401
402 int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, bool rw,
403                         MSGUID *log_guid);
404
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,
407                             int crc_offset);
408
409 bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset);
410
411 int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed,
412                    Error **errp);
413
414 int vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s,
415                              void *data, uint32_t length, uint64_t offset);
416
417 static inline void leguid_to_cpus(MSGUID *guid)
418 {
419     guid->data1 = le32_to_cpu(guid->data1);
420     guid->data2 = le16_to_cpu(guid->data2);
421     guid->data3 = le16_to_cpu(guid->data3);
422 }
423
424 static inline void cpu_to_leguids(MSGUID *guid)
425 {
426     guid->data1 = cpu_to_le32(guid->data1);
427     guid->data2 = cpu_to_le16(guid->data2);
428     guid->data3 = cpu_to_le16(guid->data3);
429 }
430
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);
448
449 #endif
This page took 0.04804 seconds and 4 git commands to generate.