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