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