xref: /openbmc/qemu/block/vhdx.c (revision eab15862)
1 /*
2  * Block driver for Hyper-V VHDX Images
3  *
4  * Copyright (c) 2013 Red Hat, Inc.,
5  *
6  * Authors:
7  *  Jeff Cody <jcody@redhat.com>
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 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "block/block_int.h"
21 #include "sysemu/block-backend.h"
22 #include "qemu/module.h"
23 #include "qemu/option.h"
24 #include "qemu/crc32c.h"
25 #include "qemu/bswap.h"
26 #include "block/vhdx.h"
27 #include "migration/blocker.h"
28 #include "qemu/uuid.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qobject-input-visitor.h"
31 #include "qapi/qapi-visit-block-core.h"
32 
33 /* Options for VHDX creation */
34 
35 #define VHDX_BLOCK_OPT_LOG_SIZE   "log_size"
36 #define VHDX_BLOCK_OPT_BLOCK_SIZE "block_size"
37 #define VHDX_BLOCK_OPT_ZERO "block_state_zero"
38 
39 typedef enum VHDXImageType {
40     VHDX_TYPE_DYNAMIC = 0,
41     VHDX_TYPE_FIXED,
42     VHDX_TYPE_DIFFERENCING,   /* Currently unsupported */
43 } VHDXImageType;
44 
45 static QemuOptsList vhdx_create_opts;
46 
47 /* Several metadata and region table data entries are identified by
48  * guids in  a MS-specific GUID format. */
49 
50 
51 /* ------- Known Region Table GUIDs ---------------------- */
52 static const MSGUID bat_guid =      { .data1 = 0x2dc27766,
53                                       .data2 = 0xf623,
54                                       .data3 = 0x4200,
55                                       .data4 = { 0x9d, 0x64, 0x11, 0x5e,
56                                                  0x9b, 0xfd, 0x4a, 0x08} };
57 
58 static const MSGUID metadata_guid = { .data1 = 0x8b7ca206,
59                                       .data2 = 0x4790,
60                                       .data3 = 0x4b9a,
61                                       .data4 = { 0xb8, 0xfe, 0x57, 0x5f,
62                                                  0x05, 0x0f, 0x88, 0x6e} };
63 
64 
65 
66 /* ------- Known Metadata Entry GUIDs ---------------------- */
67 static const MSGUID file_param_guid =   { .data1 = 0xcaa16737,
68                                           .data2 = 0xfa36,
69                                           .data3 = 0x4d43,
70                                           .data4 = { 0xb3, 0xb6, 0x33, 0xf0,
71                                                      0xaa, 0x44, 0xe7, 0x6b} };
72 
73 static const MSGUID virtual_size_guid = { .data1 = 0x2FA54224,
74                                           .data2 = 0xcd1b,
75                                           .data3 = 0x4876,
76                                           .data4 = { 0xb2, 0x11, 0x5d, 0xbe,
77                                                      0xd8, 0x3b, 0xf4, 0xb8} };
78 
79 static const MSGUID page83_guid =       { .data1 = 0xbeca12ab,
80                                           .data2 = 0xb2e6,
81                                           .data3 = 0x4523,
82                                           .data4 = { 0x93, 0xef, 0xc3, 0x09,
83                                                      0xe0, 0x00, 0xc7, 0x46} };
84 
85 
86 static const MSGUID phys_sector_guid =  { .data1 = 0xcda348c7,
87                                           .data2 = 0x445d,
88                                           .data3 = 0x4471,
89                                           .data4 = { 0x9c, 0xc9, 0xe9, 0x88,
90                                                      0x52, 0x51, 0xc5, 0x56} };
91 
92 static const MSGUID parent_locator_guid = { .data1 = 0xa8d35f2d,
93                                             .data2 = 0xb30b,
94                                             .data3 = 0x454d,
95                                             .data4 = { 0xab, 0xf7, 0xd3,
96                                                        0xd8, 0x48, 0x34,
97                                                        0xab, 0x0c} };
98 
99 static const MSGUID logical_sector_guid = { .data1 = 0x8141bf1d,
100                                             .data2 = 0xa96f,
101                                             .data3 = 0x4709,
102                                             .data4 = { 0xba, 0x47, 0xf2,
103                                                        0x33, 0xa8, 0xfa,
104                                                        0xab, 0x5f} };
105 
106 /* Each parent type must have a valid GUID; this is for parent images
107  * of type 'VHDX'.  If we were to allow e.g. a QCOW2 parent, we would
108  * need to make up our own QCOW2 GUID type */
109 static const MSGUID parent_vhdx_guid __attribute__((unused))
110                                      = { .data1 = 0xb04aefb7,
111                                          .data2 = 0xd19e,
112                                          .data3 = 0x4a81,
113                                          .data4 = { 0xb7, 0x89, 0x25, 0xb8,
114                                                     0xe9, 0x44, 0x59, 0x13} };
115 
116 
117 #define META_FILE_PARAMETER_PRESENT      0x01
118 #define META_VIRTUAL_DISK_SIZE_PRESENT   0x02
119 #define META_PAGE_83_PRESENT             0x04
120 #define META_LOGICAL_SECTOR_SIZE_PRESENT 0x08
121 #define META_PHYS_SECTOR_SIZE_PRESENT    0x10
122 #define META_PARENT_LOCATOR_PRESENT      0x20
123 
124 #define META_ALL_PRESENT    \
125     (META_FILE_PARAMETER_PRESENT | META_VIRTUAL_DISK_SIZE_PRESENT | \
126      META_PAGE_83_PRESENT | META_LOGICAL_SECTOR_SIZE_PRESENT | \
127      META_PHYS_SECTOR_SIZE_PRESENT)
128 
129 
130 typedef struct VHDXSectorInfo {
131     uint32_t bat_idx;       /* BAT entry index */
132     uint32_t sectors_avail; /* sectors available in payload block */
133     uint32_t bytes_left;    /* bytes left in the block after data to r/w */
134     uint32_t bytes_avail;   /* bytes available in payload block */
135     uint64_t file_offset;   /* absolute offset in bytes, in file */
136     uint64_t block_offset;  /* block offset, in bytes */
137 } VHDXSectorInfo;
138 
139 /* Calculates new checksum.
140  *
141  * Zero is substituted during crc calculation for the original crc field
142  * crc_offset: byte offset in buf of the buffer crc
143  * buf: buffer pointer
144  * size: size of buffer (must be > crc_offset+4)
145  *
146  * Note: The buffer should have all multi-byte data in little-endian format,
147  *       and the resulting checksum is in little endian format.
148  */
149 uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset)
150 {
151     uint32_t crc;
152 
153     assert(buf != NULL);
154     assert(size > (crc_offset + sizeof(crc)));
155 
156     memset(buf + crc_offset, 0, sizeof(crc));
157     crc =  crc32c(0xffffffff, buf, size);
158     cpu_to_le32s(&crc);
159     memcpy(buf + crc_offset, &crc, sizeof(crc));
160 
161     return crc;
162 }
163 
164 uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
165                             int crc_offset)
166 {
167     uint32_t crc_new;
168     uint32_t crc_orig;
169     assert(buf != NULL);
170 
171     if (crc_offset > 0) {
172         memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
173         memset(buf + crc_offset, 0, sizeof(crc_orig));
174     }
175 
176     crc_new = crc32c(crc, buf, size);
177     if (crc_offset > 0) {
178         memcpy(buf + crc_offset, &crc_orig, sizeof(crc_orig));
179     }
180 
181     return crc_new;
182 }
183 
184 /* Validates the checksum of the buffer, with an in-place CRC.
185  *
186  * Zero is substituted during crc calculation for the original crc field,
187  * and the crc field is restored afterwards.  But the buffer will be modifed
188  * during the calculation, so this may not be not suitable for multi-threaded
189  * use.
190  *
191  * crc_offset: byte offset in buf of the buffer crc
192  * buf: buffer pointer
193  * size: size of buffer (must be > crc_offset+4)
194  *
195  * returns true if checksum is valid, false otherwise
196  */
197 bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset)
198 {
199     uint32_t crc_orig;
200     uint32_t crc;
201 
202     assert(buf != NULL);
203     assert(size > (crc_offset + 4));
204 
205     memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
206     crc_orig = le32_to_cpu(crc_orig);
207 
208     crc = vhdx_checksum_calc(0xffffffff, buf, size, crc_offset);
209 
210     return crc == crc_orig;
211 }
212 
213 
214 /*
215  * This generates a UUID that is compliant with the MS GUIDs used
216  * in the VHDX spec (and elsewhere).
217  */
218 void vhdx_guid_generate(MSGUID *guid)
219 {
220     QemuUUID uuid;
221     assert(guid != NULL);
222 
223     qemu_uuid_generate(&uuid);
224     memcpy(guid, &uuid, sizeof(MSGUID));
225 }
226 
227 /* Check for region overlaps inside the VHDX image */
228 static int vhdx_region_check(BDRVVHDXState *s, uint64_t start, uint64_t length)
229 {
230     int ret = 0;
231     uint64_t end;
232     VHDXRegionEntry *r;
233 
234     end = start + length;
235     QLIST_FOREACH(r, &s->regions, entries) {
236         if (!((start >= r->end) || (end <= r->start))) {
237             ret = -EINVAL;
238             goto exit;
239         }
240     }
241 
242 exit:
243     return ret;
244 }
245 
246 /* Register a region for future checks */
247 static void vhdx_region_register(BDRVVHDXState *s,
248                                  uint64_t start, uint64_t length)
249 {
250     VHDXRegionEntry *r;
251 
252     r = g_malloc0(sizeof(*r));
253 
254     r->start = start;
255     r->end = start + length;
256 
257     QLIST_INSERT_HEAD(&s->regions, r, entries);
258 }
259 
260 /* Free all registered regions */
261 static void vhdx_region_unregister_all(BDRVVHDXState *s)
262 {
263     VHDXRegionEntry *r, *r_next;
264 
265     QLIST_FOREACH_SAFE(r, &s->regions, entries, r_next) {
266         QLIST_REMOVE(r, entries);
267         g_free(r);
268     }
269 }
270 
271 static void vhdx_set_shift_bits(BDRVVHDXState *s)
272 {
273     s->logical_sector_size_bits = ctz32(s->logical_sector_size);
274     s->sectors_per_block_bits =   ctz32(s->sectors_per_block);
275     s->chunk_ratio_bits =         ctz64(s->chunk_ratio);
276     s->block_size_bits =          ctz32(s->block_size);
277 }
278 
279 /*
280  * Per the MS VHDX Specification, for every VHDX file:
281  *      - The header section is fixed size - 1 MB
282  *      - The header section is always the first "object"
283  *      - The first 64KB of the header is the File Identifier
284  *      - The first uint64 (8 bytes) is the VHDX Signature ("vhdxfile")
285  *      - The following 512 bytes constitute a UTF-16 string identifiying the
286  *        software that created the file, and is optional and diagnostic only.
287  *
288  *  Therefore, we probe by looking for the vhdxfile signature "vhdxfile"
289  */
290 static int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename)
291 {
292     if (buf_size >= 8 && !memcmp(buf, "vhdxfile", 8)) {
293         return 100;
294     }
295     return 0;
296 }
297 
298 /*
299  * Writes the header to the specified offset.
300  *
301  * This will optionally read in buffer data from disk (otherwise zero-fill),
302  * and then update the header checksum.  Header is converted to proper
303  * endianness before being written to the specified file offset
304  */
305 static int vhdx_write_header(BdrvChild *file, VHDXHeader *hdr,
306                              uint64_t offset, bool read)
307 {
308     BlockDriverState *bs_file = file->bs;
309     uint8_t *buffer = NULL;
310     int ret;
311     VHDXHeader *header_le;
312 
313     assert(bs_file != NULL);
314     assert(hdr != NULL);
315 
316     /* the header checksum is not over just the packed size of VHDXHeader,
317      * but rather over the entire 'reserved' range for the header, which is
318      * 4KB (VHDX_HEADER_SIZE). */
319 
320     buffer = qemu_blockalign(bs_file, VHDX_HEADER_SIZE);
321     if (read) {
322         /* if true, we can't assume the extra reserved bytes are 0 */
323         ret = bdrv_pread(file, offset, buffer, VHDX_HEADER_SIZE);
324         if (ret < 0) {
325             goto exit;
326         }
327     } else {
328         memset(buffer, 0, VHDX_HEADER_SIZE);
329     }
330 
331     /* overwrite the actual VHDXHeader portion */
332     header_le = (VHDXHeader *)buffer;
333     memcpy(header_le, hdr, sizeof(VHDXHeader));
334     vhdx_header_le_export(hdr, header_le);
335     vhdx_update_checksum(buffer, VHDX_HEADER_SIZE,
336                          offsetof(VHDXHeader, checksum));
337     ret = bdrv_pwrite_sync(file, offset, header_le, sizeof(VHDXHeader));
338 
339 exit:
340     qemu_vfree(buffer);
341     return ret;
342 }
343 
344 /* Update the VHDX headers
345  *
346  * This follows the VHDX spec procedures for header updates.
347  *
348  *  - non-current header is updated with largest sequence number
349  */
350 static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
351                               bool generate_data_write_guid, MSGUID *log_guid)
352 {
353     int ret = 0;
354     int hdr_idx = 0;
355     uint64_t header_offset = VHDX_HEADER1_OFFSET;
356 
357     VHDXHeader *active_header;
358     VHDXHeader *inactive_header;
359 
360     /* operate on the non-current header */
361     if (s->curr_header == 0) {
362         hdr_idx = 1;
363         header_offset = VHDX_HEADER2_OFFSET;
364     }
365 
366     active_header   = s->headers[s->curr_header];
367     inactive_header = s->headers[hdr_idx];
368 
369     inactive_header->sequence_number = active_header->sequence_number + 1;
370 
371     /* a new file guid must be generated before any file write, including
372      * headers */
373     inactive_header->file_write_guid = s->session_guid;
374 
375     /* a new data guid only needs to be generated before any guest-visible
376      * writes (i.e. something observable via virtual disk read) */
377     if (generate_data_write_guid) {
378         vhdx_guid_generate(&inactive_header->data_write_guid);
379     }
380 
381     /* update the log guid if present */
382     if (log_guid) {
383         inactive_header->log_guid = *log_guid;
384     }
385 
386     ret = vhdx_write_header(bs->file, inactive_header, header_offset, true);
387     if (ret < 0) {
388         goto exit;
389     }
390     s->curr_header = hdr_idx;
391 
392 exit:
393     return ret;
394 }
395 
396 /*
397  * The VHDX spec calls for header updates to be performed twice, so that both
398  * the current and non-current header have valid info
399  */
400 int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s,
401                         bool generate_data_write_guid, MSGUID *log_guid)
402 {
403     int ret;
404 
405     ret = vhdx_update_header(bs, s, generate_data_write_guid, log_guid);
406     if (ret < 0) {
407         return ret;
408     }
409     ret = vhdx_update_header(bs, s, generate_data_write_guid, log_guid);
410     return ret;
411 }
412 
413 /* opens the specified header block from the VHDX file header section */
414 static void vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s,
415                               Error **errp)
416 {
417     int ret;
418     VHDXHeader *header1;
419     VHDXHeader *header2;
420     bool h1_valid = false;
421     bool h2_valid = false;
422     uint64_t h1_seq = 0;
423     uint64_t h2_seq = 0;
424     uint8_t *buffer;
425 
426     /* header1 & header2 are freed in vhdx_close() */
427     header1 = qemu_blockalign(bs, sizeof(VHDXHeader));
428     header2 = qemu_blockalign(bs, sizeof(VHDXHeader));
429 
430     buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE);
431 
432     s->headers[0] = header1;
433     s->headers[1] = header2;
434 
435     /* We have to read the whole VHDX_HEADER_SIZE instead of
436      * sizeof(VHDXHeader), because the checksum is over the whole
437      * region */
438     ret = bdrv_pread(bs->file, VHDX_HEADER1_OFFSET, buffer,
439                      VHDX_HEADER_SIZE);
440     if (ret < 0) {
441         goto fail;
442     }
443     /* copy over just the relevant portion that we need */
444     memcpy(header1, buffer, sizeof(VHDXHeader));
445 
446     if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) {
447         vhdx_header_le_import(header1);
448         if (header1->signature == VHDX_HEADER_SIGNATURE &&
449             header1->version == 1) {
450             h1_seq = header1->sequence_number;
451             h1_valid = true;
452         }
453     }
454 
455     ret = bdrv_pread(bs->file, VHDX_HEADER2_OFFSET, buffer,
456                      VHDX_HEADER_SIZE);
457     if (ret < 0) {
458         goto fail;
459     }
460     /* copy over just the relevant portion that we need */
461     memcpy(header2, buffer, sizeof(VHDXHeader));
462 
463     if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4)) {
464         vhdx_header_le_import(header2);
465         if (header2->signature == VHDX_HEADER_SIGNATURE &&
466             header2->version == 1) {
467             h2_seq = header2->sequence_number;
468             h2_valid = true;
469         }
470     }
471 
472     /* If there is only 1 valid header (or no valid headers), we
473      * don't care what the sequence numbers are */
474     if (h1_valid && !h2_valid) {
475         s->curr_header = 0;
476     } else if (!h1_valid && h2_valid) {
477         s->curr_header = 1;
478     } else if (!h1_valid && !h2_valid) {
479         goto fail;
480     } else {
481         /* If both headers are valid, then we choose the active one by the
482          * highest sequence number.  If the sequence numbers are equal, that is
483          * invalid */
484         if (h1_seq > h2_seq) {
485             s->curr_header = 0;
486         } else if (h2_seq > h1_seq) {
487             s->curr_header = 1;
488         } else {
489             /* The Microsoft Disk2VHD tool will create 2 identical
490              * headers, with identical sequence numbers.  If the headers are
491              * identical, don't consider the file corrupt */
492             if (!memcmp(header1, header2, sizeof(VHDXHeader))) {
493                 s->curr_header = 0;
494             } else {
495                 goto fail;
496             }
497         }
498     }
499 
500     vhdx_region_register(s, s->headers[s->curr_header]->log_offset,
501                             s->headers[s->curr_header]->log_length);
502     goto exit;
503 
504 fail:
505     error_setg_errno(errp, -ret, "No valid VHDX header found");
506     qemu_vfree(header1);
507     qemu_vfree(header2);
508     s->headers[0] = NULL;
509     s->headers[1] = NULL;
510 exit:
511     qemu_vfree(buffer);
512 }
513 
514 
515 static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
516 {
517     int ret = 0;
518     uint8_t *buffer;
519     int offset = 0;
520     VHDXRegionTableEntry rt_entry;
521     uint32_t i;
522     bool bat_rt_found = false;
523     bool metadata_rt_found = false;
524 
525     /* We have to read the whole 64KB block, because the crc32 is over the
526      * whole block */
527     buffer = qemu_blockalign(bs, VHDX_HEADER_BLOCK_SIZE);
528 
529     ret = bdrv_pread(bs->file, VHDX_REGION_TABLE_OFFSET, buffer,
530                      VHDX_HEADER_BLOCK_SIZE);
531     if (ret < 0) {
532         goto fail;
533     }
534     memcpy(&s->rt, buffer, sizeof(s->rt));
535     offset += sizeof(s->rt);
536 
537     if (!vhdx_checksum_is_valid(buffer, VHDX_HEADER_BLOCK_SIZE, 4)) {
538         ret = -EINVAL;
539         goto fail;
540     }
541 
542     vhdx_region_header_le_import(&s->rt);
543 
544     if (s->rt.signature != VHDX_REGION_SIGNATURE) {
545         ret = -EINVAL;
546         goto fail;
547     }
548 
549 
550     /* Per spec, maximum region table entry count is 2047 */
551     if (s->rt.entry_count > 2047) {
552         ret = -EINVAL;
553         goto fail;
554     }
555 
556     for (i = 0; i < s->rt.entry_count; i++) {
557         memcpy(&rt_entry, buffer + offset, sizeof(rt_entry));
558         offset += sizeof(rt_entry);
559 
560         vhdx_region_entry_le_import(&rt_entry);
561 
562         /* check for region overlap between these entries, and any
563          * other memory regions in the file */
564         ret = vhdx_region_check(s, rt_entry.file_offset, rt_entry.length);
565         if (ret < 0) {
566             goto fail;
567         }
568 
569         vhdx_region_register(s, rt_entry.file_offset, rt_entry.length);
570 
571         /* see if we recognize the entry */
572         if (guid_eq(rt_entry.guid, bat_guid)) {
573             /* must be unique; if we have already found it this is invalid */
574             if (bat_rt_found) {
575                 ret = -EINVAL;
576                 goto fail;
577             }
578             bat_rt_found = true;
579             s->bat_rt = rt_entry;
580             continue;
581         }
582 
583         if (guid_eq(rt_entry.guid, metadata_guid)) {
584             /* must be unique; if we have already found it this is invalid */
585             if (metadata_rt_found) {
586                 ret = -EINVAL;
587                 goto fail;
588             }
589             metadata_rt_found = true;
590             s->metadata_rt = rt_entry;
591             continue;
592         }
593 
594         if (rt_entry.data_bits & VHDX_REGION_ENTRY_REQUIRED) {
595             /* cannot read vhdx file - required region table entry that
596              * we do not understand.  per spec, we must fail to open */
597             ret = -ENOTSUP;
598             goto fail;
599         }
600     }
601 
602     if (!bat_rt_found || !metadata_rt_found) {
603         ret = -EINVAL;
604         goto fail;
605     }
606 
607     ret = 0;
608 
609 fail:
610     qemu_vfree(buffer);
611     return ret;
612 }
613 
614 
615 
616 /* Metadata initial parser
617  *
618  * This loads all the metadata entry fields.  This may cause additional
619  * fields to be processed (e.g. parent locator, etc..).
620  *
621  * There are 5 Metadata items that are always required:
622  *      - File Parameters (block size, has a parent)
623  *      - Virtual Disk Size (size, in bytes, of the virtual drive)
624  *      - Page 83 Data (scsi page 83 guid)
625  *      - Logical Sector Size (logical sector size in bytes, either 512 or
626  *                             4096.  We only support 512 currently)
627  *      - Physical Sector Size (512 or 4096)
628  *
629  * Also, if the File Parameters indicate this is a differencing file,
630  * we must also look for the Parent Locator metadata item.
631  */
632 static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s)
633 {
634     int ret = 0;
635     uint8_t *buffer;
636     int offset = 0;
637     uint32_t i = 0;
638     VHDXMetadataTableEntry md_entry;
639 
640     buffer = qemu_blockalign(bs, VHDX_METADATA_TABLE_MAX_SIZE);
641 
642     ret = bdrv_pread(bs->file, s->metadata_rt.file_offset, buffer,
643                      VHDX_METADATA_TABLE_MAX_SIZE);
644     if (ret < 0) {
645         goto exit;
646     }
647     memcpy(&s->metadata_hdr, buffer, sizeof(s->metadata_hdr));
648     offset += sizeof(s->metadata_hdr);
649 
650     vhdx_metadata_header_le_import(&s->metadata_hdr);
651 
652     if (s->metadata_hdr.signature != VHDX_METADATA_SIGNATURE) {
653         ret = -EINVAL;
654         goto exit;
655     }
656 
657     s->metadata_entries.present = 0;
658 
659     if ((s->metadata_hdr.entry_count * sizeof(md_entry)) >
660         (VHDX_METADATA_TABLE_MAX_SIZE - offset)) {
661         ret = -EINVAL;
662         goto exit;
663     }
664 
665     for (i = 0; i < s->metadata_hdr.entry_count; i++) {
666         memcpy(&md_entry, buffer + offset, sizeof(md_entry));
667         offset += sizeof(md_entry);
668 
669         vhdx_metadata_entry_le_import(&md_entry);
670 
671         if (guid_eq(md_entry.item_id, file_param_guid)) {
672             if (s->metadata_entries.present & META_FILE_PARAMETER_PRESENT) {
673                 ret = -EINVAL;
674                 goto exit;
675             }
676             s->metadata_entries.file_parameters_entry = md_entry;
677             s->metadata_entries.present |= META_FILE_PARAMETER_PRESENT;
678             continue;
679         }
680 
681         if (guid_eq(md_entry.item_id, virtual_size_guid)) {
682             if (s->metadata_entries.present & META_VIRTUAL_DISK_SIZE_PRESENT) {
683                 ret = -EINVAL;
684                 goto exit;
685             }
686             s->metadata_entries.virtual_disk_size_entry = md_entry;
687             s->metadata_entries.present |= META_VIRTUAL_DISK_SIZE_PRESENT;
688             continue;
689         }
690 
691         if (guid_eq(md_entry.item_id, page83_guid)) {
692             if (s->metadata_entries.present & META_PAGE_83_PRESENT) {
693                 ret = -EINVAL;
694                 goto exit;
695             }
696             s->metadata_entries.page83_data_entry = md_entry;
697             s->metadata_entries.present |= META_PAGE_83_PRESENT;
698             continue;
699         }
700 
701         if (guid_eq(md_entry.item_id, logical_sector_guid)) {
702             if (s->metadata_entries.present &
703                 META_LOGICAL_SECTOR_SIZE_PRESENT) {
704                 ret = -EINVAL;
705                 goto exit;
706             }
707             s->metadata_entries.logical_sector_size_entry = md_entry;
708             s->metadata_entries.present |= META_LOGICAL_SECTOR_SIZE_PRESENT;
709             continue;
710         }
711 
712         if (guid_eq(md_entry.item_id, phys_sector_guid)) {
713             if (s->metadata_entries.present & META_PHYS_SECTOR_SIZE_PRESENT) {
714                 ret = -EINVAL;
715                 goto exit;
716             }
717             s->metadata_entries.phys_sector_size_entry = md_entry;
718             s->metadata_entries.present |= META_PHYS_SECTOR_SIZE_PRESENT;
719             continue;
720         }
721 
722         if (guid_eq(md_entry.item_id, parent_locator_guid)) {
723             if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
724                 ret = -EINVAL;
725                 goto exit;
726             }
727             s->metadata_entries.parent_locator_entry = md_entry;
728             s->metadata_entries.present |= META_PARENT_LOCATOR_PRESENT;
729             continue;
730         }
731 
732         if (md_entry.data_bits & VHDX_META_FLAGS_IS_REQUIRED) {
733             /* cannot read vhdx file - required region table entry that
734              * we do not understand.  per spec, we must fail to open */
735             ret = -ENOTSUP;
736             goto exit;
737         }
738     }
739 
740     if (s->metadata_entries.present != META_ALL_PRESENT) {
741         ret = -ENOTSUP;
742         goto exit;
743     }
744 
745     ret = bdrv_pread(bs->file,
746                      s->metadata_entries.file_parameters_entry.offset
747                                          + s->metadata_rt.file_offset,
748                      &s->params,
749                      sizeof(s->params));
750 
751     if (ret < 0) {
752         goto exit;
753     }
754 
755     le32_to_cpus(&s->params.block_size);
756     le32_to_cpus(&s->params.data_bits);
757 
758 
759     /* We now have the file parameters, so we can tell if this is a
760      * differencing file (i.e.. has_parent), is dynamic or fixed
761      * sized (leave_blocks_allocated), and the block size */
762 
763     /* The parent locator required iff the file parameters has_parent set */
764     if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
765         if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
766             /* TODO: parse  parent locator fields */
767             ret = -ENOTSUP; /* temp, until differencing files are supported */
768             goto exit;
769         } else {
770             /* if has_parent is set, but there is not parent locator present,
771              * then that is an invalid combination */
772             ret = -EINVAL;
773             goto exit;
774         }
775     }
776 
777     /* determine virtual disk size, logical sector size,
778      * and phys sector size */
779 
780     ret = bdrv_pread(bs->file,
781                      s->metadata_entries.virtual_disk_size_entry.offset
782                                            + s->metadata_rt.file_offset,
783                      &s->virtual_disk_size,
784                      sizeof(uint64_t));
785     if (ret < 0) {
786         goto exit;
787     }
788     ret = bdrv_pread(bs->file,
789                      s->metadata_entries.logical_sector_size_entry.offset
790                                              + s->metadata_rt.file_offset,
791                      &s->logical_sector_size,
792                      sizeof(uint32_t));
793     if (ret < 0) {
794         goto exit;
795     }
796     ret = bdrv_pread(bs->file,
797                      s->metadata_entries.phys_sector_size_entry.offset
798                                           + s->metadata_rt.file_offset,
799                      &s->physical_sector_size,
800                      sizeof(uint32_t));
801     if (ret < 0) {
802         goto exit;
803     }
804 
805     le64_to_cpus(&s->virtual_disk_size);
806     le32_to_cpus(&s->logical_sector_size);
807     le32_to_cpus(&s->physical_sector_size);
808 
809     if (s->params.block_size < VHDX_BLOCK_SIZE_MIN ||
810         s->params.block_size > VHDX_BLOCK_SIZE_MAX) {
811         ret = -EINVAL;
812         goto exit;
813     }
814 
815     /* only 2 supported sector sizes */
816     if (s->logical_sector_size != 512 && s->logical_sector_size != 4096) {
817         ret = -EINVAL;
818         goto exit;
819     }
820 
821     /* Both block_size and sector_size are guaranteed powers of 2, below.
822        Due to range checks above, s->sectors_per_block can never be < 256 */
823     s->sectors_per_block = s->params.block_size / s->logical_sector_size;
824     s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) *
825                      (uint64_t)s->logical_sector_size /
826                      (uint64_t)s->params.block_size;
827 
828     /* These values are ones we will want to use for division / multiplication
829      * later on, and they are all guaranteed (per the spec) to be powers of 2,
830      * so we can take advantage of that for shift operations during
831      * reads/writes */
832     if (s->logical_sector_size & (s->logical_sector_size - 1)) {
833         ret = -EINVAL;
834         goto exit;
835     }
836     if (s->sectors_per_block & (s->sectors_per_block - 1)) {
837         ret = -EINVAL;
838         goto exit;
839     }
840     if (s->chunk_ratio & (s->chunk_ratio - 1)) {
841         ret = -EINVAL;
842         goto exit;
843     }
844     s->block_size = s->params.block_size;
845     if (s->block_size & (s->block_size - 1)) {
846         ret = -EINVAL;
847         goto exit;
848     }
849 
850     vhdx_set_shift_bits(s);
851 
852     ret = 0;
853 
854 exit:
855     qemu_vfree(buffer);
856     return ret;
857 }
858 
859 /*
860  * Calculate the number of BAT entries, including sector
861  * bitmap entries.
862  */
863 static void vhdx_calc_bat_entries(BDRVVHDXState *s)
864 {
865     uint32_t data_blocks_cnt, bitmap_blocks_cnt;
866 
867     data_blocks_cnt = DIV_ROUND_UP(s->virtual_disk_size, s->block_size);
868     bitmap_blocks_cnt = DIV_ROUND_UP(data_blocks_cnt, s->chunk_ratio);
869 
870     if (s->parent_entries) {
871         s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1);
872     } else {
873         s->bat_entries = data_blocks_cnt +
874                          ((data_blocks_cnt - 1) >> s->chunk_ratio_bits);
875     }
876 
877 }
878 
879 static void vhdx_close(BlockDriverState *bs)
880 {
881     BDRVVHDXState *s = bs->opaque;
882     qemu_vfree(s->headers[0]);
883     s->headers[0] = NULL;
884     qemu_vfree(s->headers[1]);
885     s->headers[1] = NULL;
886     qemu_vfree(s->bat);
887     s->bat = NULL;
888     qemu_vfree(s->parent_entries);
889     s->parent_entries = NULL;
890     migrate_del_blocker(s->migration_blocker);
891     error_free(s->migration_blocker);
892     qemu_vfree(s->log.hdr);
893     s->log.hdr = NULL;
894     vhdx_region_unregister_all(s);
895 }
896 
897 static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
898                      Error **errp)
899 {
900     BDRVVHDXState *s = bs->opaque;
901     int ret = 0;
902     uint32_t i;
903     uint64_t signature;
904     Error *local_err = NULL;
905 
906     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
907                                false, errp);
908     if (!bs->file) {
909         return -EINVAL;
910     }
911 
912     s->bat = NULL;
913     s->first_visible_write = true;
914 
915     qemu_co_mutex_init(&s->lock);
916     QLIST_INIT(&s->regions);
917 
918     /* validate the file signature */
919     ret = bdrv_pread(bs->file, 0, &signature, sizeof(uint64_t));
920     if (ret < 0) {
921         goto fail;
922     }
923     if (memcmp(&signature, "vhdxfile", 8)) {
924         ret = -EINVAL;
925         goto fail;
926     }
927 
928     /* This is used for any header updates, for the file_write_guid.
929      * The spec dictates that a new value should be used for the first
930      * header update */
931     vhdx_guid_generate(&s->session_guid);
932 
933     vhdx_parse_header(bs, s, &local_err);
934     if (local_err != NULL) {
935         error_propagate(errp, local_err);
936         ret = -EINVAL;
937         goto fail;
938     }
939 
940     ret = vhdx_parse_log(bs, s, &s->log_replayed_on_open, errp);
941     if (ret < 0) {
942         goto fail;
943     }
944 
945     ret = vhdx_open_region_tables(bs, s);
946     if (ret < 0) {
947         goto fail;
948     }
949 
950     ret = vhdx_parse_metadata(bs, s);
951     if (ret < 0) {
952         goto fail;
953     }
954 
955     s->block_size = s->params.block_size;
956 
957     /* the VHDX spec dictates that virtual_disk_size is always a multiple of
958      * logical_sector_size */
959     bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits;
960 
961     vhdx_calc_bat_entries(s);
962 
963     s->bat_offset = s->bat_rt.file_offset;
964 
965     if (s->bat_entries > s->bat_rt.length / sizeof(VHDXBatEntry)) {
966         /* BAT allocation is not large enough for all entries */
967         ret = -EINVAL;
968         goto fail;
969     }
970 
971     /* s->bat is freed in vhdx_close() */
972     s->bat = qemu_try_blockalign(bs->file->bs, s->bat_rt.length);
973     if (s->bat == NULL) {
974         ret = -ENOMEM;
975         goto fail;
976     }
977 
978     ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length);
979     if (ret < 0) {
980         goto fail;
981     }
982 
983     uint64_t payblocks = s->chunk_ratio;
984     /* endian convert, and verify populated BAT field file offsets against
985      * region table and log entries */
986     for (i = 0; i < s->bat_entries; i++) {
987         le64_to_cpus(&s->bat[i]);
988         if (payblocks--) {
989             /* payload bat entries */
990             if ((s->bat[i] & VHDX_BAT_STATE_BIT_MASK) ==
991                     PAYLOAD_BLOCK_FULLY_PRESENT) {
992                 ret = vhdx_region_check(s, s->bat[i] & VHDX_BAT_FILE_OFF_MASK,
993                                         s->block_size);
994                 if (ret < 0) {
995                     goto fail;
996                 }
997             }
998         } else {
999             payblocks = s->chunk_ratio;
1000             /* Once differencing files are supported, verify sector bitmap
1001              * blocks here */
1002         }
1003     }
1004 
1005     /* Disable migration when VHDX images are used */
1006     error_setg(&s->migration_blocker, "The vhdx format used by node '%s' "
1007                "does not support live migration",
1008                bdrv_get_device_or_node_name(bs));
1009     ret = migrate_add_blocker(s->migration_blocker, &local_err);
1010     if (local_err) {
1011         error_propagate(errp, local_err);
1012         error_free(s->migration_blocker);
1013         goto fail;
1014     }
1015 
1016     /* TODO: differencing files */
1017 
1018     return 0;
1019 fail:
1020     vhdx_close(bs);
1021     return ret;
1022 }
1023 
1024 static int vhdx_reopen_prepare(BDRVReopenState *state,
1025                                BlockReopenQueue *queue, Error **errp)
1026 {
1027     return 0;
1028 }
1029 
1030 
1031 /*
1032  * Perform sector to block offset translations, to get various
1033  * sector and file offsets into the image.  See VHDXSectorInfo
1034  */
1035 static void vhdx_block_translate(BDRVVHDXState *s, int64_t sector_num,
1036                                  int nb_sectors, VHDXSectorInfo *sinfo)
1037 {
1038     uint32_t block_offset;
1039 
1040     sinfo->bat_idx = sector_num >> s->sectors_per_block_bits;
1041     /* effectively a modulo - this gives us the offset into the block
1042      * (in sector sizes) for our sector number */
1043     block_offset = sector_num - (sinfo->bat_idx << s->sectors_per_block_bits);
1044     /* the chunk ratio gives us the interleaving of the sector
1045      * bitmaps, so we need to advance our page block index by the
1046      * sector bitmaps entry number */
1047     sinfo->bat_idx += sinfo->bat_idx >> s->chunk_ratio_bits;
1048 
1049     /* the number of sectors we can read/write in this cycle */
1050     sinfo->sectors_avail = s->sectors_per_block - block_offset;
1051 
1052     sinfo->bytes_left = sinfo->sectors_avail << s->logical_sector_size_bits;
1053 
1054     if (sinfo->sectors_avail > nb_sectors) {
1055         sinfo->sectors_avail = nb_sectors;
1056     }
1057 
1058     sinfo->bytes_avail = sinfo->sectors_avail << s->logical_sector_size_bits;
1059 
1060     sinfo->file_offset = s->bat[sinfo->bat_idx] & VHDX_BAT_FILE_OFF_MASK;
1061 
1062     sinfo->block_offset = block_offset << s->logical_sector_size_bits;
1063 
1064     /* The file offset must be past the header section, so must be > 0 */
1065     if (sinfo->file_offset == 0) {
1066         return;
1067     }
1068 
1069     /* block offset is the offset in vhdx logical sectors, in
1070      * the payload data block. Convert that to a byte offset
1071      * in the block, and add in the payload data block offset
1072      * in the file, in bytes, to get the final read address */
1073 
1074     sinfo->file_offset += sinfo->block_offset;
1075 }
1076 
1077 
1078 static int vhdx_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1079 {
1080     BDRVVHDXState *s = bs->opaque;
1081 
1082     bdi->cluster_size = s->block_size;
1083 
1084     bdi->unallocated_blocks_are_zero =
1085         (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) == 0;
1086 
1087     return 0;
1088 }
1089 
1090 
1091 static coroutine_fn int vhdx_co_readv(BlockDriverState *bs, int64_t sector_num,
1092                                       int nb_sectors, QEMUIOVector *qiov)
1093 {
1094     BDRVVHDXState *s = bs->opaque;
1095     int ret = 0;
1096     VHDXSectorInfo sinfo;
1097     uint64_t bytes_done = 0;
1098     QEMUIOVector hd_qiov;
1099 
1100     qemu_iovec_init(&hd_qiov, qiov->niov);
1101 
1102     qemu_co_mutex_lock(&s->lock);
1103 
1104     while (nb_sectors > 0) {
1105         /* We are a differencing file, so we need to inspect the sector bitmap
1106          * to see if we have the data or not */
1107         if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
1108             /* not supported yet */
1109             ret = -ENOTSUP;
1110             goto exit;
1111         } else {
1112             vhdx_block_translate(s, sector_num, nb_sectors, &sinfo);
1113 
1114             qemu_iovec_reset(&hd_qiov);
1115             qemu_iovec_concat(&hd_qiov, qiov,  bytes_done, sinfo.bytes_avail);
1116 
1117             /* check the payload block state */
1118             switch (s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK) {
1119             case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */
1120             case PAYLOAD_BLOCK_UNDEFINED:
1121             case PAYLOAD_BLOCK_UNMAPPED:
1122             case PAYLOAD_BLOCK_UNMAPPED_v095:
1123             case PAYLOAD_BLOCK_ZERO:
1124                 /* return zero */
1125                 qemu_iovec_memset(&hd_qiov, 0, 0, sinfo.bytes_avail);
1126                 break;
1127             case PAYLOAD_BLOCK_FULLY_PRESENT:
1128                 qemu_co_mutex_unlock(&s->lock);
1129                 ret = bdrv_co_readv(bs->file,
1130                                     sinfo.file_offset >> BDRV_SECTOR_BITS,
1131                                     sinfo.sectors_avail, &hd_qiov);
1132                 qemu_co_mutex_lock(&s->lock);
1133                 if (ret < 0) {
1134                     goto exit;
1135                 }
1136                 break;
1137             case PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1138                 /* we don't yet support difference files, fall through
1139                  * to error */
1140             default:
1141                 ret = -EIO;
1142                 goto exit;
1143                 break;
1144             }
1145             nb_sectors -= sinfo.sectors_avail;
1146             sector_num += sinfo.sectors_avail;
1147             bytes_done += sinfo.bytes_avail;
1148         }
1149     }
1150     ret = 0;
1151 exit:
1152     qemu_co_mutex_unlock(&s->lock);
1153     qemu_iovec_destroy(&hd_qiov);
1154     return ret;
1155 }
1156 
1157 /*
1158  * Allocate a new payload block at the end of the file.
1159  *
1160  * Allocation will happen at 1MB alignment inside the file
1161  *
1162  * Returns the file offset start of the new payload block
1163  */
1164 static int vhdx_allocate_block(BlockDriverState *bs, BDRVVHDXState *s,
1165                                     uint64_t *new_offset)
1166 {
1167     int64_t current_len;
1168 
1169     current_len = bdrv_getlength(bs->file->bs);
1170     if (current_len < 0) {
1171         return current_len;
1172     }
1173 
1174     *new_offset = current_len;
1175 
1176     /* per the spec, the address for a block is in units of 1MB */
1177     *new_offset = ROUND_UP(*new_offset, 1024 * 1024);
1178     if (*new_offset > INT64_MAX) {
1179         return -EINVAL;
1180     }
1181 
1182     return bdrv_truncate(bs->file, *new_offset + s->block_size,
1183                          PREALLOC_MODE_OFF, NULL);
1184 }
1185 
1186 /*
1187  * Update the BAT table entry with the new file offset, and the new entry
1188  * state */
1189 static void vhdx_update_bat_table_entry(BlockDriverState *bs, BDRVVHDXState *s,
1190                                        VHDXSectorInfo *sinfo,
1191                                        uint64_t *bat_entry_le,
1192                                        uint64_t *bat_offset, int state)
1193 {
1194     /* The BAT entry is a uint64, with 44 bits for the file offset in units of
1195      * 1MB, and 3 bits for the block state. */
1196     if ((state == PAYLOAD_BLOCK_ZERO)        ||
1197         (state == PAYLOAD_BLOCK_UNDEFINED)   ||
1198         (state == PAYLOAD_BLOCK_NOT_PRESENT) ||
1199         (state == PAYLOAD_BLOCK_UNMAPPED)) {
1200         s->bat[sinfo->bat_idx]  = 0;  /* For PAYLOAD_BLOCK_ZERO, the
1201                                          FileOffsetMB field is denoted as
1202                                          'reserved' in the v1.0 spec.  If it is
1203                                          non-zero, MS Hyper-V will fail to read
1204                                          the disk image */
1205     } else {
1206         s->bat[sinfo->bat_idx]  = sinfo->file_offset;
1207     }
1208 
1209     s->bat[sinfo->bat_idx] |= state & VHDX_BAT_STATE_BIT_MASK;
1210 
1211     *bat_entry_le = cpu_to_le64(s->bat[sinfo->bat_idx]);
1212     *bat_offset = s->bat_offset + sinfo->bat_idx * sizeof(VHDXBatEntry);
1213 
1214 }
1215 
1216 /* Per the spec, on the first write of guest-visible data to the file the
1217  * data write guid must be updated in the header */
1218 int vhdx_user_visible_write(BlockDriverState *bs, BDRVVHDXState *s)
1219 {
1220     int ret = 0;
1221     if (s->first_visible_write) {
1222         s->first_visible_write = false;
1223         ret = vhdx_update_headers(bs, s, true, NULL);
1224     }
1225     return ret;
1226 }
1227 
1228 static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,
1229                                       int nb_sectors, QEMUIOVector *qiov)
1230 {
1231     int ret = -ENOTSUP;
1232     BDRVVHDXState *s = bs->opaque;
1233     VHDXSectorInfo sinfo;
1234     uint64_t bytes_done = 0;
1235     uint64_t bat_entry = 0;
1236     uint64_t bat_entry_offset = 0;
1237     QEMUIOVector hd_qiov;
1238     struct iovec iov1 = { 0 };
1239     struct iovec iov2 = { 0 };
1240     int sectors_to_write;
1241     int bat_state;
1242     uint64_t bat_prior_offset = 0;
1243     bool bat_update = false;
1244 
1245     qemu_iovec_init(&hd_qiov, qiov->niov);
1246 
1247     qemu_co_mutex_lock(&s->lock);
1248 
1249     ret = vhdx_user_visible_write(bs, s);
1250     if (ret < 0) {
1251         goto exit;
1252     }
1253 
1254     while (nb_sectors > 0) {
1255         bool use_zero_buffers = false;
1256         bat_update = false;
1257         if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
1258             /* not supported yet */
1259             ret = -ENOTSUP;
1260             goto exit;
1261         } else {
1262             vhdx_block_translate(s, sector_num, nb_sectors, &sinfo);
1263             sectors_to_write = sinfo.sectors_avail;
1264 
1265             qemu_iovec_reset(&hd_qiov);
1266             /* check the payload block state */
1267             bat_state = s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK;
1268             switch (bat_state) {
1269             case PAYLOAD_BLOCK_ZERO:
1270                 /* in this case, we need to preserve zero writes for
1271                  * data that is not part of this write, so we must pad
1272                  * the rest of the buffer to zeroes */
1273 
1274                 /* if we are on a posix system with ftruncate() that extends
1275                  * a file, then it is zero-filled for us.  On Win32, the raw
1276                  * layer uses SetFilePointer and SetFileEnd, which does not
1277                  * zero fill AFAIK */
1278 
1279                 /* Queue another write of zero buffers if the underlying file
1280                  * does not zero-fill on file extension */
1281 
1282                 if (bdrv_has_zero_init(bs->file->bs) == 0) {
1283                     use_zero_buffers = true;
1284 
1285                     /* zero fill the front, if any */
1286                     if (sinfo.block_offset) {
1287                         iov1.iov_len = sinfo.block_offset;
1288                         iov1.iov_base = qemu_blockalign(bs, iov1.iov_len);
1289                         memset(iov1.iov_base, 0, iov1.iov_len);
1290                         qemu_iovec_concat_iov(&hd_qiov, &iov1, 1, 0,
1291                                               iov1.iov_len);
1292                         sectors_to_write += iov1.iov_len >> BDRV_SECTOR_BITS;
1293                     }
1294 
1295                     /* our actual data */
1296                     qemu_iovec_concat(&hd_qiov, qiov,  bytes_done,
1297                                       sinfo.bytes_avail);
1298 
1299                     /* zero fill the back, if any */
1300                     if ((sinfo.bytes_avail - sinfo.block_offset) <
1301                          s->block_size) {
1302                         iov2.iov_len = s->block_size -
1303                                       (sinfo.bytes_avail + sinfo.block_offset);
1304                         iov2.iov_base = qemu_blockalign(bs, iov2.iov_len);
1305                         memset(iov2.iov_base, 0, iov2.iov_len);
1306                         qemu_iovec_concat_iov(&hd_qiov, &iov2, 1, 0,
1307                                               iov2.iov_len);
1308                         sectors_to_write += iov2.iov_len >> BDRV_SECTOR_BITS;
1309                     }
1310                 }
1311                 /* fall through */
1312             case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */
1313             case PAYLOAD_BLOCK_UNMAPPED:
1314             case PAYLOAD_BLOCK_UNMAPPED_v095:
1315             case PAYLOAD_BLOCK_UNDEFINED:
1316                 bat_prior_offset = sinfo.file_offset;
1317                 ret = vhdx_allocate_block(bs, s, &sinfo.file_offset);
1318                 if (ret < 0) {
1319                     goto exit;
1320                 }
1321                 /* once we support differencing files, this may also be
1322                  * partially present */
1323                 /* update block state to the newly specified state */
1324                 vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry,
1325                                             &bat_entry_offset,
1326                                             PAYLOAD_BLOCK_FULLY_PRESENT);
1327                 bat_update = true;
1328                 /* since we just allocated a block, file_offset is the
1329                  * beginning of the payload block. It needs to be the
1330                  * write address, which includes the offset into the block */
1331                 if (!use_zero_buffers) {
1332                     sinfo.file_offset += sinfo.block_offset;
1333                 }
1334                 /* fall through */
1335             case PAYLOAD_BLOCK_FULLY_PRESENT:
1336                 /* if the file offset address is in the header zone,
1337                  * there is a problem */
1338                 if (sinfo.file_offset < (1024 * 1024)) {
1339                     ret = -EFAULT;
1340                     goto error_bat_restore;
1341                 }
1342 
1343                 if (!use_zero_buffers) {
1344                     qemu_iovec_concat(&hd_qiov, qiov,  bytes_done,
1345                                       sinfo.bytes_avail);
1346                 }
1347                 /* block exists, so we can just overwrite it */
1348                 qemu_co_mutex_unlock(&s->lock);
1349                 ret = bdrv_co_writev(bs->file,
1350                                     sinfo.file_offset >> BDRV_SECTOR_BITS,
1351                                     sectors_to_write, &hd_qiov);
1352                 qemu_co_mutex_lock(&s->lock);
1353                 if (ret < 0) {
1354                     goto error_bat_restore;
1355                 }
1356                 break;
1357             case PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1358                 /* we don't yet support difference files, fall through
1359                  * to error */
1360             default:
1361                 ret = -EIO;
1362                 goto exit;
1363                 break;
1364             }
1365 
1366             if (bat_update) {
1367                 /* this will update the BAT entry into the log journal, and
1368                  * then flush the log journal out to disk */
1369                 ret =  vhdx_log_write_and_flush(bs, s, &bat_entry,
1370                                                 sizeof(VHDXBatEntry),
1371                                                 bat_entry_offset);
1372                 if (ret < 0) {
1373                     goto exit;
1374                 }
1375             }
1376 
1377             nb_sectors -= sinfo.sectors_avail;
1378             sector_num += sinfo.sectors_avail;
1379             bytes_done += sinfo.bytes_avail;
1380 
1381         }
1382     }
1383 
1384     goto exit;
1385 
1386 error_bat_restore:
1387     if (bat_update) {
1388         /* keep metadata in sync, and restore the bat entry state
1389          * if error. */
1390         sinfo.file_offset = bat_prior_offset;
1391         vhdx_update_bat_table_entry(bs, s, &sinfo, &bat_entry,
1392                                     &bat_entry_offset, bat_state);
1393     }
1394 exit:
1395     qemu_vfree(iov1.iov_base);
1396     qemu_vfree(iov2.iov_base);
1397     qemu_co_mutex_unlock(&s->lock);
1398     qemu_iovec_destroy(&hd_qiov);
1399     return ret;
1400 }
1401 
1402 
1403 
1404 /*
1405  * Create VHDX Headers
1406  *
1407  * There are 2 headers, and the highest sequence number will represent
1408  * the active header
1409  */
1410 static int vhdx_create_new_headers(BlockBackend *blk, uint64_t image_size,
1411                                    uint32_t log_size)
1412 {
1413     BlockDriverState *bs = blk_bs(blk);
1414     BdrvChild *child;
1415     int ret = 0;
1416     VHDXHeader *hdr = NULL;
1417 
1418     hdr = g_new0(VHDXHeader, 1);
1419 
1420     hdr->signature       = VHDX_HEADER_SIGNATURE;
1421     hdr->sequence_number = g_random_int();
1422     hdr->log_version     = 0;
1423     hdr->version         = 1;
1424     hdr->log_length      = log_size;
1425     hdr->log_offset      = VHDX_HEADER_SECTION_END;
1426     vhdx_guid_generate(&hdr->file_write_guid);
1427     vhdx_guid_generate(&hdr->data_write_guid);
1428 
1429     /* XXX Ugly way to get blk->root, but that's a feature, not a bug. This
1430      * hack makes it obvious that vhdx_write_header() bypasses the BlockBackend
1431      * here, which it really shouldn't be doing. */
1432     child = QLIST_FIRST(&bs->parents);
1433     assert(!QLIST_NEXT(child, next_parent));
1434 
1435     ret = vhdx_write_header(child, hdr, VHDX_HEADER1_OFFSET, false);
1436     if (ret < 0) {
1437         goto exit;
1438     }
1439     hdr->sequence_number++;
1440     ret = vhdx_write_header(child, hdr, VHDX_HEADER2_OFFSET, false);
1441     if (ret < 0) {
1442         goto exit;
1443     }
1444 
1445 exit:
1446     g_free(hdr);
1447     return ret;
1448 }
1449 
1450 #define VHDX_METADATA_ENTRY_BUFFER_SIZE \
1451                                     (sizeof(VHDXFileParameters)               +\
1452                                      sizeof(VHDXVirtualDiskSize)              +\
1453                                      sizeof(VHDXPage83Data)                   +\
1454                                      sizeof(VHDXVirtualDiskLogicalSectorSize) +\
1455                                      sizeof(VHDXVirtualDiskPhysicalSectorSize))
1456 
1457 /*
1458  * Create the Metadata entries.
1459  *
1460  * For more details on the entries, see section 3.5 (pg 29) in the
1461  * VHDX 1.00 specification.
1462  *
1463  * We support 5 metadata entries (all required by spec):
1464  *          File Parameters,
1465  *          Virtual Disk Size,
1466  *          Page 83 Data,
1467  *          Logical Sector Size,
1468  *          Physical Sector Size
1469  *
1470  * The first 64KB of the Metadata section is reserved for the metadata
1471  * header and entries; beyond that, the metadata items themselves reside.
1472  */
1473 static int vhdx_create_new_metadata(BlockBackend *blk,
1474                                     uint64_t image_size,
1475                                     uint32_t block_size,
1476                                     uint32_t sector_size,
1477                                     uint64_t metadata_offset,
1478                                     VHDXImageType type)
1479 {
1480     int ret = 0;
1481     uint32_t offset = 0;
1482     void *buffer = NULL;
1483     void *entry_buffer;
1484     VHDXMetadataTableHeader *md_table;
1485     VHDXMetadataTableEntry  *md_table_entry;
1486 
1487     /* Metadata entries */
1488     VHDXFileParameters     *mt_file_params;
1489     VHDXVirtualDiskSize    *mt_virtual_size;
1490     VHDXPage83Data         *mt_page83;
1491     VHDXVirtualDiskLogicalSectorSize  *mt_log_sector_size;
1492     VHDXVirtualDiskPhysicalSectorSize *mt_phys_sector_size;
1493 
1494     entry_buffer = g_malloc0(VHDX_METADATA_ENTRY_BUFFER_SIZE);
1495 
1496     mt_file_params = entry_buffer;
1497     offset += sizeof(VHDXFileParameters);
1498     mt_virtual_size = entry_buffer + offset;
1499     offset += sizeof(VHDXVirtualDiskSize);
1500     mt_page83 = entry_buffer + offset;
1501     offset += sizeof(VHDXPage83Data);
1502     mt_log_sector_size = entry_buffer + offset;
1503     offset += sizeof(VHDXVirtualDiskLogicalSectorSize);
1504     mt_phys_sector_size = entry_buffer + offset;
1505 
1506     mt_file_params->block_size = cpu_to_le32(block_size);
1507     if (type == VHDX_TYPE_FIXED) {
1508         mt_file_params->data_bits |= VHDX_PARAMS_LEAVE_BLOCKS_ALLOCED;
1509         cpu_to_le32s(&mt_file_params->data_bits);
1510     }
1511 
1512     vhdx_guid_generate(&mt_page83->page_83_data);
1513     cpu_to_leguids(&mt_page83->page_83_data);
1514     mt_virtual_size->virtual_disk_size        = cpu_to_le64(image_size);
1515     mt_log_sector_size->logical_sector_size   = cpu_to_le32(sector_size);
1516     mt_phys_sector_size->physical_sector_size = cpu_to_le32(sector_size);
1517 
1518     buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE);
1519     md_table = buffer;
1520 
1521     md_table->signature   = VHDX_METADATA_SIGNATURE;
1522     md_table->entry_count = 5;
1523     vhdx_metadata_header_le_export(md_table);
1524 
1525 
1526     /* This will reference beyond the reserved table portion */
1527     offset = 64 * KiB;
1528 
1529     md_table_entry = buffer + sizeof(VHDXMetadataTableHeader);
1530 
1531     md_table_entry[0].item_id = file_param_guid;
1532     md_table_entry[0].offset  = offset;
1533     md_table_entry[0].length  = sizeof(VHDXFileParameters);
1534     md_table_entry[0].data_bits |= VHDX_META_FLAGS_IS_REQUIRED;
1535     offset += md_table_entry[0].length;
1536     vhdx_metadata_entry_le_export(&md_table_entry[0]);
1537 
1538     md_table_entry[1].item_id = virtual_size_guid;
1539     md_table_entry[1].offset  = offset;
1540     md_table_entry[1].length  = sizeof(VHDXVirtualDiskSize);
1541     md_table_entry[1].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1542                                    VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1543     offset += md_table_entry[1].length;
1544     vhdx_metadata_entry_le_export(&md_table_entry[1]);
1545 
1546     md_table_entry[2].item_id = page83_guid;
1547     md_table_entry[2].offset  = offset;
1548     md_table_entry[2].length  = sizeof(VHDXPage83Data);
1549     md_table_entry[2].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1550                                    VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1551     offset += md_table_entry[2].length;
1552     vhdx_metadata_entry_le_export(&md_table_entry[2]);
1553 
1554     md_table_entry[3].item_id = logical_sector_guid;
1555     md_table_entry[3].offset  = offset;
1556     md_table_entry[3].length  = sizeof(VHDXVirtualDiskLogicalSectorSize);
1557     md_table_entry[3].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1558                                    VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1559     offset += md_table_entry[3].length;
1560     vhdx_metadata_entry_le_export(&md_table_entry[3]);
1561 
1562     md_table_entry[4].item_id = phys_sector_guid;
1563     md_table_entry[4].offset  = offset;
1564     md_table_entry[4].length  = sizeof(VHDXVirtualDiskPhysicalSectorSize);
1565     md_table_entry[4].data_bits |= VHDX_META_FLAGS_IS_REQUIRED |
1566                                    VHDX_META_FLAGS_IS_VIRTUAL_DISK;
1567     vhdx_metadata_entry_le_export(&md_table_entry[4]);
1568 
1569     ret = blk_pwrite(blk, metadata_offset, buffer, VHDX_HEADER_BLOCK_SIZE, 0);
1570     if (ret < 0) {
1571         goto exit;
1572     }
1573 
1574     ret = blk_pwrite(blk, metadata_offset + (64 * KiB), entry_buffer,
1575                      VHDX_METADATA_ENTRY_BUFFER_SIZE, 0);
1576     if (ret < 0) {
1577         goto exit;
1578     }
1579 
1580 
1581 exit:
1582     g_free(buffer);
1583     g_free(entry_buffer);
1584     return ret;
1585 }
1586 
1587 /* This create the actual BAT itself.  We currently only support
1588  * 'Dynamic' and 'Fixed' image types.
1589  *
1590  *  Dynamic images: default state of the BAT is all zeroes.
1591  *
1592  *  Fixed images: default state of the BAT is fully populated, with
1593  *                file offsets and state PAYLOAD_BLOCK_FULLY_PRESENT.
1594  */
1595 static int vhdx_create_bat(BlockBackend *blk, BDRVVHDXState *s,
1596                            uint64_t image_size, VHDXImageType type,
1597                            bool use_zero_blocks, uint64_t file_offset,
1598                            uint32_t length, Error **errp)
1599 {
1600     int ret = 0;
1601     uint64_t data_file_offset;
1602     uint64_t total_sectors = 0;
1603     uint64_t sector_num = 0;
1604     uint64_t unused;
1605     int block_state;
1606     VHDXSectorInfo sinfo;
1607 
1608     assert(s->bat == NULL);
1609 
1610     /* this gives a data start after BAT/bitmap entries, and well
1611      * past any metadata entries (with a 4 MB buffer for future
1612      * expansion */
1613     data_file_offset = file_offset + length + 5 * MiB;
1614     total_sectors = image_size >> s->logical_sector_size_bits;
1615 
1616     if (type == VHDX_TYPE_DYNAMIC) {
1617         /* All zeroes, so we can just extend the file - the end of the BAT
1618          * is the furthest thing we have written yet */
1619         ret = blk_truncate(blk, data_file_offset, PREALLOC_MODE_OFF, errp);
1620         if (ret < 0) {
1621             goto exit;
1622         }
1623     } else if (type == VHDX_TYPE_FIXED) {
1624         ret = blk_truncate(blk, data_file_offset + image_size,
1625                            PREALLOC_MODE_OFF, errp);
1626         if (ret < 0) {
1627             goto exit;
1628         }
1629     } else {
1630         error_setg(errp, "Unsupported image type");
1631         ret = -ENOTSUP;
1632         goto exit;
1633     }
1634 
1635     if (type == VHDX_TYPE_FIXED ||
1636                 use_zero_blocks ||
1637                 bdrv_has_zero_init(blk_bs(blk)) == 0) {
1638         /* for a fixed file, the default BAT entry is not zero */
1639         s->bat = g_try_malloc0(length);
1640         if (length && s->bat == NULL) {
1641             error_setg(errp, "Failed to allocate memory for the BAT");
1642             ret = -ENOMEM;
1643             goto exit;
1644         }
1645         block_state = type == VHDX_TYPE_FIXED ? PAYLOAD_BLOCK_FULLY_PRESENT :
1646                                                 PAYLOAD_BLOCK_NOT_PRESENT;
1647         block_state = use_zero_blocks ? PAYLOAD_BLOCK_ZERO : block_state;
1648         /* fill the BAT by emulating sector writes of sectors_per_block size */
1649         while (sector_num < total_sectors) {
1650             vhdx_block_translate(s, sector_num, s->sectors_per_block, &sinfo);
1651             sinfo.file_offset = data_file_offset +
1652                                 (sector_num << s->logical_sector_size_bits);
1653             sinfo.file_offset = ROUND_UP(sinfo.file_offset, MiB);
1654             vhdx_update_bat_table_entry(blk_bs(blk), s, &sinfo, &unused, &unused,
1655                                         block_state);
1656             cpu_to_le64s(&s->bat[sinfo.bat_idx]);
1657             sector_num += s->sectors_per_block;
1658         }
1659         ret = blk_pwrite(blk, file_offset, s->bat, length, 0);
1660         if (ret < 0) {
1661             error_setg_errno(errp, -ret, "Failed to write the BAT");
1662             goto exit;
1663         }
1664     }
1665 
1666 
1667 
1668 exit:
1669     g_free(s->bat);
1670     return ret;
1671 }
1672 
1673 /* Creates the region table header, and region table entries.
1674  * There are 2 supported region table entries: BAT, and Metadata/
1675  *
1676  * As the calculations for the BAT region table are also needed
1677  * to create the BAT itself, we will also cause the BAT to be
1678  * created.
1679  */
1680 static int vhdx_create_new_region_table(BlockBackend *blk,
1681                                         uint64_t image_size,
1682                                         uint32_t block_size,
1683                                         uint32_t sector_size,
1684                                         uint32_t log_size,
1685                                         bool use_zero_blocks,
1686                                         VHDXImageType type,
1687                                         uint64_t *metadata_offset,
1688                                         Error **errp)
1689 {
1690     int ret = 0;
1691     uint32_t offset = 0;
1692     void *buffer = NULL;
1693     uint64_t bat_file_offset;
1694     uint32_t bat_length;
1695     BDRVVHDXState *s = NULL;
1696     VHDXRegionTableHeader *region_table;
1697     VHDXRegionTableEntry *rt_bat;
1698     VHDXRegionTableEntry *rt_metadata;
1699 
1700     assert(metadata_offset != NULL);
1701 
1702     /* Populate enough of the BDRVVHDXState to be able to use the
1703      * pre-existing BAT calculation, translation, and update functions */
1704     s = g_new0(BDRVVHDXState, 1);
1705 
1706     s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) *
1707                      (uint64_t) sector_size / (uint64_t) block_size;
1708 
1709     s->sectors_per_block = block_size / sector_size;
1710     s->virtual_disk_size = image_size;
1711     s->block_size = block_size;
1712     s->logical_sector_size = sector_size;
1713 
1714     vhdx_set_shift_bits(s);
1715 
1716     vhdx_calc_bat_entries(s);
1717 
1718     /* At this point the VHDX state is populated enough for creation */
1719 
1720     /* a single buffer is used so we can calculate the checksum over the
1721      * entire 64KB block */
1722     buffer = g_malloc0(VHDX_HEADER_BLOCK_SIZE);
1723     region_table = buffer;
1724     offset += sizeof(VHDXRegionTableHeader);
1725     rt_bat = buffer + offset;
1726     offset += sizeof(VHDXRegionTableEntry);
1727     rt_metadata  = buffer + offset;
1728 
1729     region_table->signature = VHDX_REGION_SIGNATURE;
1730     region_table->entry_count = 2;   /* BAT and Metadata */
1731 
1732     rt_bat->guid        = bat_guid;
1733     rt_bat->length      = ROUND_UP(s->bat_entries * sizeof(VHDXBatEntry), MiB);
1734     rt_bat->file_offset = ROUND_UP(VHDX_HEADER_SECTION_END + log_size, MiB);
1735     s->bat_offset = rt_bat->file_offset;
1736 
1737     rt_metadata->guid        = metadata_guid;
1738     rt_metadata->file_offset = ROUND_UP(rt_bat->file_offset + rt_bat->length,
1739                                         MiB);
1740     rt_metadata->length      = 1 * MiB; /* min size, and more than enough */
1741     *metadata_offset = rt_metadata->file_offset;
1742 
1743     bat_file_offset = rt_bat->file_offset;
1744     bat_length = rt_bat->length;
1745 
1746     vhdx_region_header_le_export(region_table);
1747     vhdx_region_entry_le_export(rt_bat);
1748     vhdx_region_entry_le_export(rt_metadata);
1749 
1750     vhdx_update_checksum(buffer, VHDX_HEADER_BLOCK_SIZE,
1751                          offsetof(VHDXRegionTableHeader, checksum));
1752 
1753 
1754     /* The region table gives us the data we need to create the BAT,
1755      * so do that now */
1756     ret = vhdx_create_bat(blk, s, image_size, type, use_zero_blocks,
1757                           bat_file_offset, bat_length, errp);
1758     if (ret < 0) {
1759         goto exit;
1760     }
1761 
1762     /* Now write out the region headers to disk */
1763     ret = blk_pwrite(blk, VHDX_REGION_TABLE_OFFSET, buffer,
1764                      VHDX_HEADER_BLOCK_SIZE, 0);
1765     if (ret < 0) {
1766         error_setg_errno(errp, -ret, "Failed to write first region table");
1767         goto exit;
1768     }
1769 
1770     ret = blk_pwrite(blk, VHDX_REGION_TABLE2_OFFSET, buffer,
1771                      VHDX_HEADER_BLOCK_SIZE, 0);
1772     if (ret < 0) {
1773         error_setg_errno(errp, -ret, "Failed to write second region table");
1774         goto exit;
1775     }
1776 
1777 exit:
1778     g_free(s);
1779     g_free(buffer);
1780     return ret;
1781 }
1782 
1783 /* We need to create the following elements:
1784  *
1785  *    .-----------------------------------------------------------------.
1786  *    |   (A)    |   (B)    |    (C)    |     (D)       |     (E)       |
1787  *    |  File ID |  Header1 |  Header 2 |  Region Tbl 1 |  Region Tbl 2 |
1788  *    |          |          |           |               |               |
1789  *    .-----------------------------------------------------------------.
1790  *    0         64KB      128KB       192KB           256KB           320KB
1791  *
1792  *
1793  *    .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------.
1794  *    |     (F)     |     (G)       |    (H)    |                        |
1795  *    | Journal Log |  BAT / Bitmap |  Metadata |  .... data ......      |
1796  *    |             |               |           |                        |
1797  *    .---- ~ ----------- ~ ------------ ~ ---------------- ~ -----------.
1798  *   1MB
1799  */
1800 static int coroutine_fn vhdx_co_create(BlockdevCreateOptions *opts,
1801                                        Error **errp)
1802 {
1803     BlockdevCreateOptionsVhdx *vhdx_opts;
1804     BlockBackend *blk = NULL;
1805     BlockDriverState *bs = NULL;
1806 
1807     int ret = 0;
1808     uint64_t image_size;
1809     uint32_t log_size;
1810     uint32_t block_size;
1811     uint64_t signature;
1812     uint64_t metadata_offset;
1813     bool use_zero_blocks = false;
1814 
1815     gunichar2 *creator = NULL;
1816     glong creator_items;
1817     VHDXImageType image_type;
1818 
1819     assert(opts->driver == BLOCKDEV_DRIVER_VHDX);
1820     vhdx_opts = &opts->u.vhdx;
1821 
1822     /* Validate options and set default values */
1823     image_size = vhdx_opts->size;
1824     if (image_size > VHDX_MAX_IMAGE_SIZE) {
1825         error_setg(errp, "Image size too large; max of 64TB");
1826         return -EINVAL;
1827     }
1828 
1829     if (!vhdx_opts->has_log_size) {
1830         log_size = DEFAULT_LOG_SIZE;
1831     } else {
1832         if (vhdx_opts->log_size > UINT32_MAX) {
1833             error_setg(errp, "Log size must be smaller than 4 GB");
1834             return -EINVAL;
1835         }
1836         log_size = vhdx_opts->log_size;
1837     }
1838     if (log_size < MiB || (log_size % MiB) != 0) {
1839         error_setg(errp, "Log size must be a multiple of 1 MB");
1840         return -EINVAL;
1841     }
1842 
1843     if (!vhdx_opts->has_block_state_zero) {
1844         use_zero_blocks = true;
1845     } else {
1846         use_zero_blocks = vhdx_opts->block_state_zero;
1847     }
1848 
1849     if (!vhdx_opts->has_subformat) {
1850         vhdx_opts->subformat = BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC;
1851     }
1852 
1853     switch (vhdx_opts->subformat) {
1854     case BLOCKDEV_VHDX_SUBFORMAT_DYNAMIC:
1855         image_type = VHDX_TYPE_DYNAMIC;
1856         break;
1857     case BLOCKDEV_VHDX_SUBFORMAT_FIXED:
1858         image_type = VHDX_TYPE_FIXED;
1859         break;
1860     default:
1861         g_assert_not_reached();
1862     }
1863 
1864     /* These are pretty arbitrary, and mainly designed to keep the BAT
1865      * size reasonable to load into RAM */
1866     if (vhdx_opts->has_block_size) {
1867         block_size = vhdx_opts->block_size;
1868     } else {
1869         if (image_size > 32 * TiB) {
1870             block_size = 64 * MiB;
1871         } else if (image_size > (uint64_t) 100 * GiB) {
1872             block_size = 32 * MiB;
1873         } else if (image_size > 1 * GiB) {
1874             block_size = 16 * MiB;
1875         } else {
1876             block_size = 8 * MiB;
1877         }
1878     }
1879 
1880     if (block_size < MiB || (block_size % MiB) != 0) {
1881         error_setg(errp, "Block size must be a multiple of 1 MB");
1882         return -EINVAL;
1883     }
1884     if (!is_power_of_2(block_size)) {
1885         error_setg(errp, "Block size must be a power of two");
1886         return -EINVAL;
1887     }
1888     if (block_size > VHDX_BLOCK_SIZE_MAX) {
1889         error_setg(errp, "Block size must not exceed %d", VHDX_BLOCK_SIZE_MAX);
1890         return -EINVAL;
1891     }
1892 
1893     /* Create BlockBackend to write to the image */
1894     bs = bdrv_open_blockdev_ref(vhdx_opts->file, errp);
1895     if (bs == NULL) {
1896         return -EIO;
1897     }
1898 
1899     blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
1900     ret = blk_insert_bs(blk, bs, errp);
1901     if (ret < 0) {
1902         goto delete_and_exit;
1903     }
1904     blk_set_allow_write_beyond_eof(blk, true);
1905 
1906     /* Create (A) */
1907 
1908     /* The creator field is optional, but may be useful for
1909      * debugging / diagnostics */
1910     creator = g_utf8_to_utf16("QEMU v" QEMU_VERSION, -1, NULL,
1911                               &creator_items, NULL);
1912     signature = cpu_to_le64(VHDX_FILE_SIGNATURE);
1913     ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET, &signature, sizeof(signature),
1914                      0);
1915     if (ret < 0) {
1916         error_setg_errno(errp, -ret, "Failed to write file signature");
1917         goto delete_and_exit;
1918     }
1919     if (creator) {
1920         ret = blk_pwrite(blk, VHDX_FILE_ID_OFFSET + sizeof(signature),
1921                          creator, creator_items * sizeof(gunichar2), 0);
1922         if (ret < 0) {
1923             error_setg_errno(errp, -ret, "Failed to write creator field");
1924             goto delete_and_exit;
1925         }
1926     }
1927 
1928 
1929     /* Creates (B),(C) */
1930     ret = vhdx_create_new_headers(blk, image_size, log_size);
1931     if (ret < 0) {
1932         error_setg_errno(errp, -ret, "Failed to write image headers");
1933         goto delete_and_exit;
1934     }
1935 
1936     /* Creates (D),(E),(G) explicitly. (F) created as by-product */
1937     ret = vhdx_create_new_region_table(blk, image_size, block_size, 512,
1938                                        log_size, use_zero_blocks, image_type,
1939                                        &metadata_offset, errp);
1940     if (ret < 0) {
1941         goto delete_and_exit;
1942     }
1943 
1944     /* Creates (H) */
1945     ret = vhdx_create_new_metadata(blk, image_size, block_size, 512,
1946                                    metadata_offset, image_type);
1947     if (ret < 0) {
1948         error_setg_errno(errp, -ret, "Failed to initialize metadata");
1949         goto delete_and_exit;
1950     }
1951 
1952 
1953 delete_and_exit:
1954     blk_unref(blk);
1955     bdrv_unref(bs);
1956     g_free(creator);
1957     return ret;
1958 }
1959 
1960 static int coroutine_fn vhdx_co_create_opts(const char *filename,
1961                                             QemuOpts *opts,
1962                                             Error **errp)
1963 {
1964     BlockdevCreateOptions *create_options = NULL;
1965     QDict *qdict = NULL;
1966     QObject *qobj;
1967     Visitor *v;
1968     BlockDriverState *bs = NULL;
1969     Error *local_err = NULL;
1970     int ret;
1971 
1972     static const QDictRenames opt_renames[] = {
1973         { VHDX_BLOCK_OPT_LOG_SIZE,      "log-size" },
1974         { VHDX_BLOCK_OPT_BLOCK_SIZE,    "block-size" },
1975         { VHDX_BLOCK_OPT_ZERO,          "block-state-zero" },
1976         { NULL, NULL },
1977     };
1978 
1979     /* Parse options and convert legacy syntax */
1980     qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vhdx_create_opts, true);
1981 
1982     if (!qdict_rename_keys(qdict, opt_renames, errp)) {
1983         ret = -EINVAL;
1984         goto fail;
1985     }
1986 
1987     /* Create and open the file (protocol layer) */
1988     ret = bdrv_create_file(filename, opts, &local_err);
1989     if (ret < 0) {
1990         error_propagate(errp, local_err);
1991         goto fail;
1992     }
1993 
1994     bs = bdrv_open(filename, NULL, NULL,
1995                    BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
1996     if (bs == NULL) {
1997         ret = -EIO;
1998         goto fail;
1999     }
2000 
2001     /* Now get the QAPI type BlockdevCreateOptions */
2002     qdict_put_str(qdict, "driver", "vhdx");
2003     qdict_put_str(qdict, "file", bs->node_name);
2004 
2005     qobj = qdict_crumple(qdict, errp);
2006     QDECREF(qdict);
2007     qdict = qobject_to(QDict, qobj);
2008     if (qdict == NULL) {
2009         ret = -EINVAL;
2010         goto fail;
2011     }
2012 
2013     v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
2014     visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
2015     visit_free(v);
2016 
2017     if (local_err) {
2018         error_propagate(errp, local_err);
2019         ret = -EINVAL;
2020         goto fail;
2021     }
2022 
2023     /* Silently round up sizes:
2024      * The image size is rounded to 512 bytes. Make the block and log size
2025      * close to what was specified, but must be at least 1MB, and a multiple of
2026      * 1 MB. Also respect VHDX_BLOCK_SIZE_MAX for block sizes. block_size = 0
2027      * means auto, which is represented by a missing key in QAPI. */
2028     assert(create_options->driver == BLOCKDEV_DRIVER_VHDX);
2029     create_options->u.vhdx.size =
2030         ROUND_UP(create_options->u.vhdx.size, BDRV_SECTOR_SIZE);
2031 
2032     if (create_options->u.vhdx.has_log_size) {
2033         create_options->u.vhdx.log_size =
2034             ROUND_UP(create_options->u.vhdx.log_size, MiB);
2035     }
2036     if (create_options->u.vhdx.has_block_size) {
2037         create_options->u.vhdx.block_size =
2038             ROUND_UP(create_options->u.vhdx.block_size, MiB);
2039 
2040         if (create_options->u.vhdx.block_size == 0) {
2041             create_options->u.vhdx.has_block_size = false;
2042         }
2043         if (create_options->u.vhdx.block_size > VHDX_BLOCK_SIZE_MAX) {
2044             create_options->u.vhdx.block_size = VHDX_BLOCK_SIZE_MAX;
2045         }
2046     }
2047 
2048     /* Create the vhdx image (format layer) */
2049     ret = vhdx_co_create(create_options, errp);
2050 
2051 fail:
2052     QDECREF(qdict);
2053     bdrv_unref(bs);
2054     qapi_free_BlockdevCreateOptions(create_options);
2055     return ret;
2056 }
2057 
2058 /* If opened r/w, the VHDX driver will automatically replay the log,
2059  * if one is present, inside the vhdx_open() call.
2060  *
2061  * If qemu-img check -r all is called, the image is automatically opened
2062  * r/w and any log has already been replayed, so there is nothing (currently)
2063  * for us to do here
2064  */
2065 static int coroutine_fn vhdx_co_check(BlockDriverState *bs,
2066                                       BdrvCheckResult *result,
2067                                       BdrvCheckMode fix)
2068 {
2069     BDRVVHDXState *s = bs->opaque;
2070 
2071     if (s->log_replayed_on_open) {
2072         result->corruptions_fixed++;
2073     }
2074     return 0;
2075 }
2076 
2077 static QemuOptsList vhdx_create_opts = {
2078     .name = "vhdx-create-opts",
2079     .head = QTAILQ_HEAD_INITIALIZER(vhdx_create_opts.head),
2080     .desc = {
2081         {
2082            .name = BLOCK_OPT_SIZE,
2083            .type = QEMU_OPT_SIZE,
2084            .help = "Virtual disk size; max of 64TB."
2085        },
2086        {
2087            .name = VHDX_BLOCK_OPT_LOG_SIZE,
2088            .type = QEMU_OPT_SIZE,
2089            .def_value_str = stringify(DEFAULT_LOG_SIZE),
2090            .help = "Log size; min 1MB."
2091        },
2092        {
2093            .name = VHDX_BLOCK_OPT_BLOCK_SIZE,
2094            .type = QEMU_OPT_SIZE,
2095            .def_value_str = stringify(0),
2096            .help = "Block Size; min 1MB, max 256MB. " \
2097                    "0 means auto-calculate based on image size."
2098        },
2099        {
2100            .name = BLOCK_OPT_SUBFMT,
2101            .type = QEMU_OPT_STRING,
2102            .help = "VHDX format type, can be either 'dynamic' or 'fixed'. "\
2103                    "Default is 'dynamic'."
2104        },
2105        {
2106            .name = VHDX_BLOCK_OPT_ZERO,
2107            .type = QEMU_OPT_BOOL,
2108            .help = "Force use of payload blocks of type 'ZERO'. "\
2109                    "Non-standard, but default.  Do not set to 'off' when "\
2110                    "using 'qemu-img convert' with subformat=dynamic."
2111        },
2112        { NULL }
2113     }
2114 };
2115 
2116 static BlockDriver bdrv_vhdx = {
2117     .format_name            = "vhdx",
2118     .instance_size          = sizeof(BDRVVHDXState),
2119     .bdrv_probe             = vhdx_probe,
2120     .bdrv_open              = vhdx_open,
2121     .bdrv_close             = vhdx_close,
2122     .bdrv_reopen_prepare    = vhdx_reopen_prepare,
2123     .bdrv_child_perm        = bdrv_format_default_perms,
2124     .bdrv_co_readv          = vhdx_co_readv,
2125     .bdrv_co_writev         = vhdx_co_writev,
2126     .bdrv_co_create         = vhdx_co_create,
2127     .bdrv_co_create_opts    = vhdx_co_create_opts,
2128     .bdrv_get_info          = vhdx_get_info,
2129     .bdrv_co_check          = vhdx_co_check,
2130     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
2131 
2132     .create_opts            = &vhdx_create_opts,
2133 };
2134 
2135 static void bdrv_vhdx_init(void)
2136 {
2137     bdrv_register(&bdrv_vhdx);
2138 }
2139 
2140 block_init(bdrv_vhdx_init);
2141