]> git.proxmox.com Git - qemu.git/blame - block/vhdx.c
block: vhdx code movement - VHDXMetadataEntries and BDRVVHDXState to header.
[qemu.git] / block / vhdx.c
CommitLineData
e8d4e5ff
JC
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 *
6e9d290b 9 * This is based on the "VHDX Format Specification v1.00", published 8/25/2012
e8d4e5ff 10 * by Microsoft:
6e9d290b 11 * https://www.microsoft.com/en-us/download/details.aspx?id=34750
e8d4e5ff
JC
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"
5641bf40 23#include "migration/migration.h"
e8d4e5ff 24
4f18b782 25#include <uuid/uuid.h>
e8d4e5ff
JC
26
27/* Several metadata and region table data entries are identified by
28 * guids in a MS-specific GUID format. */
29
30
31/* ------- Known Region Table GUIDs ---------------------- */
32static const MSGUID bat_guid = { .data1 = 0x2dc27766,
33 .data2 = 0xf623,
34 .data3 = 0x4200,
35 .data4 = { 0x9d, 0x64, 0x11, 0x5e,
36 0x9b, 0xfd, 0x4a, 0x08} };
37
38static const MSGUID metadata_guid = { .data1 = 0x8b7ca206,
39 .data2 = 0x4790,
40 .data3 = 0x4b9a,
41 .data4 = { 0xb8, 0xfe, 0x57, 0x5f,
42 0x05, 0x0f, 0x88, 0x6e} };
43
44
45
46/* ------- Known Metadata Entry GUIDs ---------------------- */
47static const MSGUID file_param_guid = { .data1 = 0xcaa16737,
48 .data2 = 0xfa36,
49 .data3 = 0x4d43,
50 .data4 = { 0xb3, 0xb6, 0x33, 0xf0,
51 0xaa, 0x44, 0xe7, 0x6b} };
52
53static const MSGUID virtual_size_guid = { .data1 = 0x2FA54224,
54 .data2 = 0xcd1b,
55 .data3 = 0x4876,
56 .data4 = { 0xb2, 0x11, 0x5d, 0xbe,
57 0xd8, 0x3b, 0xf4, 0xb8} };
58
59static const MSGUID page83_guid = { .data1 = 0xbeca12ab,
60 .data2 = 0xb2e6,
61 .data3 = 0x4523,
62 .data4 = { 0x93, 0xef, 0xc3, 0x09,
63 0xe0, 0x00, 0xc7, 0x46} };
64
65
66static const MSGUID phys_sector_guid = { .data1 = 0xcda348c7,
67 .data2 = 0x445d,
68 .data3 = 0x4471,
69 .data4 = { 0x9c, 0xc9, 0xe9, 0x88,
70 0x52, 0x51, 0xc5, 0x56} };
71
72static const MSGUID parent_locator_guid = { .data1 = 0xa8d35f2d,
73 .data2 = 0xb30b,
74 .data3 = 0x454d,
75 .data4 = { 0xab, 0xf7, 0xd3,
76 0xd8, 0x48, 0x34,
77 0xab, 0x0c} };
78
79static const MSGUID logical_sector_guid = { .data1 = 0x8141bf1d,
80 .data2 = 0xa96f,
81 .data3 = 0x4709,
82 .data4 = { 0xba, 0x47, 0xf2,
83 0x33, 0xa8, 0xfa,
84 0xab, 0x5f} };
85
86/* Each parent type must have a valid GUID; this is for parent images
87 * of type 'VHDX'. If we were to allow e.g. a QCOW2 parent, we would
88 * need to make up our own QCOW2 GUID type */
89static const MSGUID parent_vhdx_guid = { .data1 = 0xb04aefb7,
90 .data2 = 0xd19e,
91 .data3 = 0x4a81,
92 .data4 = { 0xb7, 0x89, 0x25, 0xb8,
93 0xe9, 0x44, 0x59, 0x13} };
94
95
96#define META_FILE_PARAMETER_PRESENT 0x01
97#define META_VIRTUAL_DISK_SIZE_PRESENT 0x02
98#define META_PAGE_83_PRESENT 0x04
99#define META_LOGICAL_SECTOR_SIZE_PRESENT 0x08
100#define META_PHYS_SECTOR_SIZE_PRESENT 0x10
101#define META_PARENT_LOCATOR_PRESENT 0x20
102
103#define META_ALL_PRESENT \
104 (META_FILE_PARAMETER_PRESENT | META_VIRTUAL_DISK_SIZE_PRESENT | \
105 META_PAGE_83_PRESENT | META_LOGICAL_SECTOR_SIZE_PRESENT | \
106 META_PHYS_SECTOR_SIZE_PRESENT)
107
e8d4e5ff 108
059e2fbb
JC
109typedef struct VHDXSectorInfo {
110 uint32_t bat_idx; /* BAT entry index */
111 uint32_t sectors_avail; /* sectors available in payload block */
112 uint32_t bytes_left; /* bytes left in the block after data to r/w */
113 uint32_t bytes_avail; /* bytes available in payload block */
114 uint64_t file_offset; /* absolute offset in bytes, in file */
115 uint64_t block_offset; /* block offset, in bytes */
116} VHDXSectorInfo;
117
4f18b782
JC
118/* Calculates new checksum.
119 *
120 * Zero is substituted during crc calculation for the original crc field
121 * crc_offset: byte offset in buf of the buffer crc
122 * buf: buffer pointer
123 * size: size of buffer (must be > crc_offset+4)
124 *
125 * Note: The resulting checksum is in the CPU endianness, not necessarily
126 * in the file format endianness (LE). Any header export to disk should
127 * make sure that vhdx_header_le_export() is used to convert to the
128 * correct endianness
129 */
130uint32_t vhdx_update_checksum(uint8_t *buf, size_t size, int crc_offset)
131{
132 uint32_t crc;
133
134 assert(buf != NULL);
135 assert(size > (crc_offset + sizeof(crc)));
136
137 memset(buf + crc_offset, 0, sizeof(crc));
138 crc = crc32c(0xffffffff, buf, size);
139 memcpy(buf + crc_offset, &crc, sizeof(crc));
140
141 return crc;
142}
143
e8d4e5ff
JC
144uint32_t vhdx_checksum_calc(uint32_t crc, uint8_t *buf, size_t size,
145 int crc_offset)
146{
147 uint32_t crc_new;
148 uint32_t crc_orig;
149 assert(buf != NULL);
150
151 if (crc_offset > 0) {
152 memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
153 memset(buf + crc_offset, 0, sizeof(crc_orig));
154 }
155
156 crc_new = crc32c(crc, buf, size);
157 if (crc_offset > 0) {
158 memcpy(buf + crc_offset, &crc_orig, sizeof(crc_orig));
159 }
160
161 return crc_new;
162}
163
164/* Validates the checksum of the buffer, with an in-place CRC.
165 *
166 * Zero is substituted during crc calculation for the original crc field,
167 * and the crc field is restored afterwards. But the buffer will be modifed
168 * during the calculation, so this may not be not suitable for multi-threaded
169 * use.
170 *
171 * crc_offset: byte offset in buf of the buffer crc
172 * buf: buffer pointer
173 * size: size of buffer (must be > crc_offset+4)
174 *
175 * returns true if checksum is valid, false otherwise
176 */
177bool vhdx_checksum_is_valid(uint8_t *buf, size_t size, int crc_offset)
178{
179 uint32_t crc_orig;
180 uint32_t crc;
181
182 assert(buf != NULL);
183 assert(size > (crc_offset + 4));
184
185 memcpy(&crc_orig, buf + crc_offset, sizeof(crc_orig));
186 crc_orig = le32_to_cpu(crc_orig);
187
188 crc = vhdx_checksum_calc(0xffffffff, buf, size, crc_offset);
189
190 return crc == crc_orig;
191}
192
193
4f18b782
JC
194/*
195 * This generates a UUID that is compliant with the MS GUIDs used
196 * in the VHDX spec (and elsewhere).
197 */
198void vhdx_guid_generate(MSGUID *guid)
199{
200 uuid_t uuid;
201 assert(guid != NULL);
202
203 uuid_generate(uuid);
204 memcpy(guid, uuid, sizeof(MSGUID));
205}
206
e8d4e5ff
JC
207/*
208 * Per the MS VHDX Specification, for every VHDX file:
209 * - The header section is fixed size - 1 MB
210 * - The header section is always the first "object"
211 * - The first 64KB of the header is the File Identifier
212 * - The first uint64 (8 bytes) is the VHDX Signature ("vhdxfile")
213 * - The following 512 bytes constitute a UTF-16 string identifiying the
214 * software that created the file, and is optional and diagnostic only.
215 *
216 * Therefore, we probe by looking for the vhdxfile signature "vhdxfile"
217 */
218static int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename)
219{
220 if (buf_size >= 8 && !memcmp(buf, "vhdxfile", 8)) {
221 return 100;
222 }
223 return 0;
224}
225
226/* All VHDX structures on disk are little endian */
227static void vhdx_header_le_import(VHDXHeader *h)
228{
229 assert(h != NULL);
230
231 le32_to_cpus(&h->signature);
232 le32_to_cpus(&h->checksum);
233 le64_to_cpus(&h->sequence_number);
234
235 leguid_to_cpus(&h->file_write_guid);
236 leguid_to_cpus(&h->data_write_guid);
237 leguid_to_cpus(&h->log_guid);
238
239 le16_to_cpus(&h->log_version);
240 le16_to_cpus(&h->version);
241 le32_to_cpus(&h->log_length);
242 le64_to_cpus(&h->log_offset);
243}
244
4f18b782
JC
245/* All VHDX structures on disk are little endian */
246static void vhdx_header_le_export(VHDXHeader *orig_h, VHDXHeader *new_h)
247{
248 assert(orig_h != NULL);
249 assert(new_h != NULL);
250
251 new_h->signature = cpu_to_le32(orig_h->signature);
252 new_h->checksum = cpu_to_le32(orig_h->checksum);
253 new_h->sequence_number = cpu_to_le64(orig_h->sequence_number);
254
255 new_h->file_write_guid = orig_h->file_write_guid;
256 new_h->data_write_guid = orig_h->data_write_guid;
257 new_h->log_guid = orig_h->log_guid;
258
259 cpu_to_leguids(&new_h->file_write_guid);
260 cpu_to_leguids(&new_h->data_write_guid);
261 cpu_to_leguids(&new_h->log_guid);
262
263 new_h->log_version = cpu_to_le16(orig_h->log_version);
264 new_h->version = cpu_to_le16(orig_h->version);
265 new_h->log_length = cpu_to_le32(orig_h->log_length);
266 new_h->log_offset = cpu_to_le64(orig_h->log_offset);
267}
268
269/* Update the VHDX headers
270 *
271 * This follows the VHDX spec procedures for header updates.
272 *
273 * - non-current header is updated with largest sequence number
274 */
275static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s,
276 bool generate_data_write_guid)
277{
278 int ret = 0;
279 int hdr_idx = 0;
280 uint64_t header_offset = VHDX_HEADER1_OFFSET;
281
282 VHDXHeader *active_header;
283 VHDXHeader *inactive_header;
284 VHDXHeader header_le;
285 uint8_t *buffer;
286
287 /* operate on the non-current header */
288 if (s->curr_header == 0) {
289 hdr_idx = 1;
290 header_offset = VHDX_HEADER2_OFFSET;
291 }
292
293 active_header = s->headers[s->curr_header];
294 inactive_header = s->headers[hdr_idx];
295
296 inactive_header->sequence_number = active_header->sequence_number + 1;
297
298 /* a new file guid must be generated before any file write, including
299 * headers */
300 inactive_header->file_write_guid = s->session_guid;
301
302 /* a new data guid only needs to be generated before any guest-visible
303 * writes (i.e. something observable via virtual disk read) */
304 if (generate_data_write_guid) {
305 vhdx_guid_generate(&inactive_header->data_write_guid);
306 }
307
308 /* the header checksum is not over just the packed size of VHDXHeader,
309 * but rather over the entire 'reserved' range for the header, which is
310 * 4KB (VHDX_HEADER_SIZE). */
311
312 buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE);
313 /* we can't assume the extra reserved bytes are 0 */
314 ret = bdrv_pread(bs->file, header_offset, buffer, VHDX_HEADER_SIZE);
315 if (ret < 0) {
316 goto exit;
317 }
318 /* overwrite the actual VHDXHeader portion */
319 memcpy(buffer, inactive_header, sizeof(VHDXHeader));
320 inactive_header->checksum =
321 vhdx_update_checksum(buffer, VHDX_HEADER_SIZE,
322 offsetof(VHDXHeader, checksum));
323 vhdx_header_le_export(inactive_header, &header_le);
324 ret = bdrv_pwrite_sync(bs->file, header_offset, &header_le,
325 sizeof(VHDXHeader));
326 if (ret < 0) {
327 goto exit;
328 }
329 s->curr_header = hdr_idx;
330
331exit:
332 qemu_vfree(buffer);
333 return ret;
334}
335
336/*
337 * The VHDX spec calls for header updates to be performed twice, so that both
338 * the current and non-current header have valid info
339 */
340static int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s,
341 bool generate_data_write_guid)
342{
343 int ret;
344
345 ret = vhdx_update_header(bs, s, generate_data_write_guid);
346 if (ret < 0) {
347 return ret;
348 }
349 ret = vhdx_update_header(bs, s, generate_data_write_guid);
350 return ret;
351}
e8d4e5ff
JC
352
353/* opens the specified header block from the VHDX file header section */
354static int vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s)
355{
356 int ret = 0;
357 VHDXHeader *header1;
358 VHDXHeader *header2;
359 bool h1_valid = false;
360 bool h2_valid = false;
361 uint64_t h1_seq = 0;
362 uint64_t h2_seq = 0;
363 uint8_t *buffer;
364
6e9d290b 365 /* header1 & header2 are freed in vhdx_close() */
e8d4e5ff
JC
366 header1 = qemu_blockalign(bs, sizeof(VHDXHeader));
367 header2 = qemu_blockalign(bs, sizeof(VHDXHeader));
368
369 buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE);
370
371 s->headers[0] = header1;
372 s->headers[1] = header2;
373
374 /* We have to read the whole VHDX_HEADER_SIZE instead of
375 * sizeof(VHDXHeader), because the checksum is over the whole
376 * region */
377 ret = bdrv_pread(bs->file, VHDX_HEADER1_OFFSET, buffer, VHDX_HEADER_SIZE);
378 if (ret < 0) {
379 goto fail;
380 }
381 /* copy over just the relevant portion that we need */
382 memcpy(header1, buffer, sizeof(VHDXHeader));
383 vhdx_header_le_import(header1);
384
385 if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4) &&
386 !memcmp(&header1->signature, "head", 4) &&
387 header1->version == 1) {
388 h1_seq = header1->sequence_number;
389 h1_valid = true;
390 }
391
392 ret = bdrv_pread(bs->file, VHDX_HEADER2_OFFSET, buffer, VHDX_HEADER_SIZE);
393 if (ret < 0) {
394 goto fail;
395 }
396 /* copy over just the relevant portion that we need */
397 memcpy(header2, buffer, sizeof(VHDXHeader));
398 vhdx_header_le_import(header2);
399
400 if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4) &&
401 !memcmp(&header2->signature, "head", 4) &&
402 header2->version == 1) {
403 h2_seq = header2->sequence_number;
404 h2_valid = true;
405 }
406
407 /* If there is only 1 valid header (or no valid headers), we
408 * don't care what the sequence numbers are */
409 if (h1_valid && !h2_valid) {
410 s->curr_header = 0;
411 } else if (!h1_valid && h2_valid) {
412 s->curr_header = 1;
413 } else if (!h1_valid && !h2_valid) {
414 ret = -EINVAL;
415 goto fail;
416 } else {
417 /* If both headers are valid, then we choose the active one by the
418 * highest sequence number. If the sequence numbers are equal, that is
419 * invalid */
420 if (h1_seq > h2_seq) {
421 s->curr_header = 0;
422 } else if (h2_seq > h1_seq) {
423 s->curr_header = 1;
424 } else {
425 ret = -EINVAL;
426 goto fail;
427 }
428 }
429
430 ret = 0;
431
432 goto exit;
433
434fail:
435 qerror_report(ERROR_CLASS_GENERIC_ERROR, "No valid VHDX header found");
436 qemu_vfree(header1);
437 qemu_vfree(header2);
438 s->headers[0] = NULL;
439 s->headers[1] = NULL;
440exit:
441 qemu_vfree(buffer);
442 return ret;
443}
444
445
446static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s)
447{
448 int ret = 0;
449 uint8_t *buffer;
450 int offset = 0;
451 VHDXRegionTableEntry rt_entry;
452 uint32_t i;
453 bool bat_rt_found = false;
454 bool metadata_rt_found = false;
455
456 /* We have to read the whole 64KB block, because the crc32 is over the
457 * whole block */
458 buffer = qemu_blockalign(bs, VHDX_HEADER_BLOCK_SIZE);
459
460 ret = bdrv_pread(bs->file, VHDX_REGION_TABLE_OFFSET, buffer,
461 VHDX_HEADER_BLOCK_SIZE);
462 if (ret < 0) {
463 goto fail;
464 }
465 memcpy(&s->rt, buffer, sizeof(s->rt));
466 le32_to_cpus(&s->rt.signature);
467 le32_to_cpus(&s->rt.checksum);
468 le32_to_cpus(&s->rt.entry_count);
469 le32_to_cpus(&s->rt.reserved);
470 offset += sizeof(s->rt);
471
472 if (!vhdx_checksum_is_valid(buffer, VHDX_HEADER_BLOCK_SIZE, 4) ||
473 memcmp(&s->rt.signature, "regi", 4)) {
474 ret = -EINVAL;
475 goto fail;
476 }
477
478 /* Per spec, maximum region table entry count is 2047 */
479 if (s->rt.entry_count > 2047) {
480 ret = -EINVAL;
481 goto fail;
482 }
483
484 for (i = 0; i < s->rt.entry_count; i++) {
485 memcpy(&rt_entry, buffer + offset, sizeof(rt_entry));
486 offset += sizeof(rt_entry);
487
488 leguid_to_cpus(&rt_entry.guid);
489 le64_to_cpus(&rt_entry.file_offset);
490 le32_to_cpus(&rt_entry.length);
491 le32_to_cpus(&rt_entry.data_bits);
492
493 /* see if we recognize the entry */
494 if (guid_eq(rt_entry.guid, bat_guid)) {
495 /* must be unique; if we have already found it this is invalid */
496 if (bat_rt_found) {
497 ret = -EINVAL;
498 goto fail;
499 }
500 bat_rt_found = true;
501 s->bat_rt = rt_entry;
502 continue;
503 }
504
505 if (guid_eq(rt_entry.guid, metadata_guid)) {
506 /* must be unique; if we have already found it this is invalid */
507 if (metadata_rt_found) {
508 ret = -EINVAL;
509 goto fail;
510 }
511 metadata_rt_found = true;
512 s->metadata_rt = rt_entry;
513 continue;
514 }
515
516 if (rt_entry.data_bits & VHDX_REGION_ENTRY_REQUIRED) {
517 /* cannot read vhdx file - required region table entry that
518 * we do not understand. per spec, we must fail to open */
519 ret = -ENOTSUP;
520 goto fail;
521 }
522 }
523 ret = 0;
524
525fail:
526 qemu_vfree(buffer);
527 return ret;
528}
529
530
531
532/* Metadata initial parser
533 *
534 * This loads all the metadata entry fields. This may cause additional
535 * fields to be processed (e.g. parent locator, etc..).
536 *
537 * There are 5 Metadata items that are always required:
538 * - File Parameters (block size, has a parent)
539 * - Virtual Disk Size (size, in bytes, of the virtual drive)
540 * - Page 83 Data (scsi page 83 guid)
541 * - Logical Sector Size (logical sector size in bytes, either 512 or
542 * 4096. We only support 512 currently)
543 * - Physical Sector Size (512 or 4096)
544 *
545 * Also, if the File Parameters indicate this is a differencing file,
546 * we must also look for the Parent Locator metadata item.
547 */
548static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s)
549{
550 int ret = 0;
551 uint8_t *buffer;
552 int offset = 0;
553 uint32_t i = 0;
554 VHDXMetadataTableEntry md_entry;
555
556 buffer = qemu_blockalign(bs, VHDX_METADATA_TABLE_MAX_SIZE);
557
558 ret = bdrv_pread(bs->file, s->metadata_rt.file_offset, buffer,
559 VHDX_METADATA_TABLE_MAX_SIZE);
560 if (ret < 0) {
561 goto exit;
562 }
563 memcpy(&s->metadata_hdr, buffer, sizeof(s->metadata_hdr));
564 offset += sizeof(s->metadata_hdr);
565
566 le64_to_cpus(&s->metadata_hdr.signature);
567 le16_to_cpus(&s->metadata_hdr.reserved);
568 le16_to_cpus(&s->metadata_hdr.entry_count);
569
570 if (memcmp(&s->metadata_hdr.signature, "metadata", 8)) {
571 ret = -EINVAL;
572 goto exit;
573 }
574
575 s->metadata_entries.present = 0;
576
577 if ((s->metadata_hdr.entry_count * sizeof(md_entry)) >
578 (VHDX_METADATA_TABLE_MAX_SIZE - offset)) {
579 ret = -EINVAL;
580 goto exit;
581 }
582
583 for (i = 0; i < s->metadata_hdr.entry_count; i++) {
584 memcpy(&md_entry, buffer + offset, sizeof(md_entry));
585 offset += sizeof(md_entry);
586
587 leguid_to_cpus(&md_entry.item_id);
588 le32_to_cpus(&md_entry.offset);
589 le32_to_cpus(&md_entry.length);
590 le32_to_cpus(&md_entry.data_bits);
591 le32_to_cpus(&md_entry.reserved2);
592
593 if (guid_eq(md_entry.item_id, file_param_guid)) {
594 if (s->metadata_entries.present & META_FILE_PARAMETER_PRESENT) {
595 ret = -EINVAL;
596 goto exit;
597 }
598 s->metadata_entries.file_parameters_entry = md_entry;
599 s->metadata_entries.present |= META_FILE_PARAMETER_PRESENT;
600 continue;
601 }
602
603 if (guid_eq(md_entry.item_id, virtual_size_guid)) {
604 if (s->metadata_entries.present & META_VIRTUAL_DISK_SIZE_PRESENT) {
605 ret = -EINVAL;
606 goto exit;
607 }
608 s->metadata_entries.virtual_disk_size_entry = md_entry;
609 s->metadata_entries.present |= META_VIRTUAL_DISK_SIZE_PRESENT;
610 continue;
611 }
612
613 if (guid_eq(md_entry.item_id, page83_guid)) {
614 if (s->metadata_entries.present & META_PAGE_83_PRESENT) {
615 ret = -EINVAL;
616 goto exit;
617 }
618 s->metadata_entries.page83_data_entry = md_entry;
619 s->metadata_entries.present |= META_PAGE_83_PRESENT;
620 continue;
621 }
622
623 if (guid_eq(md_entry.item_id, logical_sector_guid)) {
624 if (s->metadata_entries.present &
625 META_LOGICAL_SECTOR_SIZE_PRESENT) {
626 ret = -EINVAL;
627 goto exit;
628 }
629 s->metadata_entries.logical_sector_size_entry = md_entry;
630 s->metadata_entries.present |= META_LOGICAL_SECTOR_SIZE_PRESENT;
631 continue;
632 }
633
634 if (guid_eq(md_entry.item_id, phys_sector_guid)) {
635 if (s->metadata_entries.present & META_PHYS_SECTOR_SIZE_PRESENT) {
636 ret = -EINVAL;
637 goto exit;
638 }
639 s->metadata_entries.phys_sector_size_entry = md_entry;
640 s->metadata_entries.present |= META_PHYS_SECTOR_SIZE_PRESENT;
641 continue;
642 }
643
644 if (guid_eq(md_entry.item_id, parent_locator_guid)) {
645 if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
646 ret = -EINVAL;
647 goto exit;
648 }
649 s->metadata_entries.parent_locator_entry = md_entry;
650 s->metadata_entries.present |= META_PARENT_LOCATOR_PRESENT;
651 continue;
652 }
653
654 if (md_entry.data_bits & VHDX_META_FLAGS_IS_REQUIRED) {
655 /* cannot read vhdx file - required region table entry that
656 * we do not understand. per spec, we must fail to open */
657 ret = -ENOTSUP;
658 goto exit;
659 }
660 }
661
662 if (s->metadata_entries.present != META_ALL_PRESENT) {
663 ret = -ENOTSUP;
664 goto exit;
665 }
666
667 ret = bdrv_pread(bs->file,
668 s->metadata_entries.file_parameters_entry.offset
669 + s->metadata_rt.file_offset,
670 &s->params,
671 sizeof(s->params));
672
673 if (ret < 0) {
674 goto exit;
675 }
676
677 le32_to_cpus(&s->params.block_size);
678 le32_to_cpus(&s->params.data_bits);
679
680
681 /* We now have the file parameters, so we can tell if this is a
682 * differencing file (i.e.. has_parent), is dynamic or fixed
683 * sized (leave_blocks_allocated), and the block size */
684
685 /* The parent locator required iff the file parameters has_parent set */
686 if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
687 if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) {
688 /* TODO: parse parent locator fields */
689 ret = -ENOTSUP; /* temp, until differencing files are supported */
690 goto exit;
691 } else {
692 /* if has_parent is set, but there is not parent locator present,
693 * then that is an invalid combination */
694 ret = -EINVAL;
695 goto exit;
696 }
697 }
698
699 /* determine virtual disk size, logical sector size,
700 * and phys sector size */
701
702 ret = bdrv_pread(bs->file,
703 s->metadata_entries.virtual_disk_size_entry.offset
704 + s->metadata_rt.file_offset,
705 &s->virtual_disk_size,
706 sizeof(uint64_t));
707 if (ret < 0) {
708 goto exit;
709 }
710 ret = bdrv_pread(bs->file,
711 s->metadata_entries.logical_sector_size_entry.offset
712 + s->metadata_rt.file_offset,
713 &s->logical_sector_size,
714 sizeof(uint32_t));
715 if (ret < 0) {
716 goto exit;
717 }
718 ret = bdrv_pread(bs->file,
719 s->metadata_entries.phys_sector_size_entry.offset
720 + s->metadata_rt.file_offset,
721 &s->physical_sector_size,
722 sizeof(uint32_t));
723 if (ret < 0) {
724 goto exit;
725 }
726
727 le64_to_cpus(&s->virtual_disk_size);
728 le32_to_cpus(&s->logical_sector_size);
729 le32_to_cpus(&s->physical_sector_size);
730
731 if (s->logical_sector_size == 0 || s->params.block_size == 0) {
732 ret = -EINVAL;
733 goto exit;
734 }
735
736 /* both block_size and sector_size are guaranteed powers of 2 */
737 s->sectors_per_block = s->params.block_size / s->logical_sector_size;
738 s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) *
739 (uint64_t)s->logical_sector_size /
740 (uint64_t)s->params.block_size;
741
742 /* These values are ones we will want to use for division / multiplication
743 * later on, and they are all guaranteed (per the spec) to be powers of 2,
744 * so we can take advantage of that for shift operations during
745 * reads/writes */
746 if (s->logical_sector_size & (s->logical_sector_size - 1)) {
747 ret = -EINVAL;
748 goto exit;
749 }
750 if (s->sectors_per_block & (s->sectors_per_block - 1)) {
751 ret = -EINVAL;
752 goto exit;
753 }
754 if (s->chunk_ratio & (s->chunk_ratio - 1)) {
755 ret = -EINVAL;
756 goto exit;
757 }
758 s->block_size = s->params.block_size;
759 if (s->block_size & (s->block_size - 1)) {
760 ret = -EINVAL;
761 goto exit;
762 }
763
764 s->logical_sector_size_bits = 31 - clz32(s->logical_sector_size);
765 s->sectors_per_block_bits = 31 - clz32(s->sectors_per_block);
766 s->chunk_ratio_bits = 63 - clz64(s->chunk_ratio);
767 s->block_size_bits = 31 - clz32(s->block_size);
768
769 ret = 0;
770
771exit:
772 qemu_vfree(buffer);
773 return ret;
774}
775
776/* Parse the replay log. Per the VHDX spec, if the log is present
777 * it must be replayed prior to opening the file, even read-only.
778 *
779 * If read-only, we must replay the log in RAM (or refuse to open
780 * a dirty VHDX file read-only */
781static int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s)
782{
783 int ret = 0;
784 int i;
785 VHDXHeader *hdr;
786
787 hdr = s->headers[s->curr_header];
788
789 /* either the log guid, or log length is zero,
790 * then a replay log is present */
791 for (i = 0; i < sizeof(hdr->log_guid.data4); i++) {
792 ret |= hdr->log_guid.data4[i];
793 }
794 if (hdr->log_guid.data1 == 0 &&
795 hdr->log_guid.data2 == 0 &&
796 hdr->log_guid.data3 == 0 &&
797 ret == 0) {
798 goto exit;
799 }
800
801 /* per spec, only log version of 0 is supported */
802 if (hdr->log_version != 0) {
803 ret = -EINVAL;
804 goto exit;
805 }
806
807 if (hdr->log_length == 0) {
808 goto exit;
809 }
810
811 /* We currently do not support images with logs to replay */
812 ret = -ENOTSUP;
813
814exit:
815 return ret;
816}
817
818
015a1036
MR
819static int vhdx_open(BlockDriverState *bs, QDict *options, int flags,
820 Error **errp)
e8d4e5ff
JC
821{
822 BDRVVHDXState *s = bs->opaque;
823 int ret = 0;
824 uint32_t i;
825 uint64_t signature;
826 uint32_t data_blocks_cnt, bitmap_blocks_cnt;
827
828
829 s->bat = NULL;
830
831 qemu_co_mutex_init(&s->lock);
832
833 /* validate the file signature */
834 ret = bdrv_pread(bs->file, 0, &signature, sizeof(uint64_t));
835 if (ret < 0) {
836 goto fail;
837 }
838 if (memcmp(&signature, "vhdxfile", 8)) {
839 ret = -EINVAL;
840 goto fail;
841 }
842
4f18b782
JC
843 /* This is used for any header updates, for the file_write_guid.
844 * The spec dictates that a new value should be used for the first
845 * header update */
846 vhdx_guid_generate(&s->session_guid);
847
e8d4e5ff
JC
848 ret = vhdx_parse_header(bs, s);
849 if (ret) {
850 goto fail;
851 }
852
853 ret = vhdx_parse_log(bs, s);
854 if (ret) {
855 goto fail;
856 }
857
858 ret = vhdx_open_region_tables(bs, s);
859 if (ret) {
860 goto fail;
861 }
862
863 ret = vhdx_parse_metadata(bs, s);
864 if (ret) {
865 goto fail;
866 }
867 s->block_size = s->params.block_size;
868
869 /* the VHDX spec dictates that virtual_disk_size is always a multiple of
870 * logical_sector_size */
871 bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits;
872
873 data_blocks_cnt = s->virtual_disk_size >> s->block_size_bits;
874 if (s->virtual_disk_size - (data_blocks_cnt << s->block_size_bits)) {
875 data_blocks_cnt++;
876 }
877 bitmap_blocks_cnt = data_blocks_cnt >> s->chunk_ratio_bits;
878 if (data_blocks_cnt - (bitmap_blocks_cnt << s->chunk_ratio_bits)) {
879 bitmap_blocks_cnt++;
880 }
881
882 if (s->parent_entries) {
883 s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1);
884 } else {
885 s->bat_entries = data_blocks_cnt +
886 ((data_blocks_cnt - 1) >> s->chunk_ratio_bits);
887 }
888
889 s->bat_offset = s->bat_rt.file_offset;
890
891 if (s->bat_entries > s->bat_rt.length / sizeof(VHDXBatEntry)) {
892 /* BAT allocation is not large enough for all entries */
893 ret = -EINVAL;
894 goto fail;
895 }
896
6e9d290b 897 /* s->bat is freed in vhdx_close() */
e8d4e5ff
JC
898 s->bat = qemu_blockalign(bs, s->bat_rt.length);
899
900 ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length);
901 if (ret < 0) {
902 goto fail;
903 }
904
905 for (i = 0; i < s->bat_entries; i++) {
906 le64_to_cpus(&s->bat[i]);
907 }
908
909 if (flags & BDRV_O_RDWR) {
4f18b782
JC
910 ret = vhdx_update_headers(bs, s, false);
911 if (ret < 0) {
912 goto fail;
913 }
e8d4e5ff
JC
914 }
915
059e2fbb 916 /* TODO: differencing files, write */
e8d4e5ff 917
5641bf40
JC
918 /* Disable migration when VHDX images are used */
919 error_set(&s->migration_blocker,
920 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
921 "vhdx", bs->device_name, "live migration");
922 migrate_add_blocker(s->migration_blocker);
923
e8d4e5ff
JC
924 return 0;
925fail:
926 qemu_vfree(s->headers[0]);
927 qemu_vfree(s->headers[1]);
928 qemu_vfree(s->bat);
929 qemu_vfree(s->parent_entries);
930 return ret;
931}
932
933static int vhdx_reopen_prepare(BDRVReopenState *state,
934 BlockReopenQueue *queue, Error **errp)
935{
936 return 0;
937}
938
939
059e2fbb
JC
940/*
941 * Perform sector to block offset translations, to get various
942 * sector and file offsets into the image. See VHDXSectorInfo
943 */
944static void vhdx_block_translate(BDRVVHDXState *s, int64_t sector_num,
945 int nb_sectors, VHDXSectorInfo *sinfo)
946{
947 uint32_t block_offset;
948
949 sinfo->bat_idx = sector_num >> s->sectors_per_block_bits;
950 /* effectively a modulo - this gives us the offset into the block
951 * (in sector sizes) for our sector number */
952 block_offset = sector_num - (sinfo->bat_idx << s->sectors_per_block_bits);
953 /* the chunk ratio gives us the interleaving of the sector
954 * bitmaps, so we need to advance our page block index by the
955 * sector bitmaps entry number */
956 sinfo->bat_idx += sinfo->bat_idx >> s->chunk_ratio_bits;
957
958 /* the number of sectors we can read/write in this cycle */
959 sinfo->sectors_avail = s->sectors_per_block - block_offset;
960
961 sinfo->bytes_left = sinfo->sectors_avail << s->logical_sector_size_bits;
962
963 if (sinfo->sectors_avail > nb_sectors) {
964 sinfo->sectors_avail = nb_sectors;
965 }
966
967 sinfo->bytes_avail = sinfo->sectors_avail << s->logical_sector_size_bits;
968
969 sinfo->file_offset = s->bat[sinfo->bat_idx] >> VHDX_BAT_FILE_OFF_BITS;
970
971 sinfo->block_offset = block_offset << s->logical_sector_size_bits;
972
973 /* The file offset must be past the header section, so must be > 0 */
974 if (sinfo->file_offset == 0) {
975 return;
976 }
977
978 /* block offset is the offset in vhdx logical sectors, in
979 * the payload data block. Convert that to a byte offset
980 * in the block, and add in the payload data block offset
981 * in the file, in bytes, to get the final read address */
982
983 sinfo->file_offset <<= 20; /* now in bytes, rather than 1MB units */
984 sinfo->file_offset += sinfo->block_offset;
985}
986
987
988
e8d4e5ff
JC
989static coroutine_fn int vhdx_co_readv(BlockDriverState *bs, int64_t sector_num,
990 int nb_sectors, QEMUIOVector *qiov)
991{
059e2fbb
JC
992 BDRVVHDXState *s = bs->opaque;
993 int ret = 0;
994 VHDXSectorInfo sinfo;
995 uint64_t bytes_done = 0;
996 QEMUIOVector hd_qiov;
997
998 qemu_iovec_init(&hd_qiov, qiov->niov);
999
1000 qemu_co_mutex_lock(&s->lock);
1001
1002 while (nb_sectors > 0) {
1003 /* We are a differencing file, so we need to inspect the sector bitmap
1004 * to see if we have the data or not */
1005 if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) {
1006 /* not supported yet */
1007 ret = -ENOTSUP;
1008 goto exit;
1009 } else {
1010 vhdx_block_translate(s, sector_num, nb_sectors, &sinfo);
1011
1012 qemu_iovec_reset(&hd_qiov);
1013 qemu_iovec_concat(&hd_qiov, qiov, bytes_done, sinfo.bytes_avail);
1014
1015 /* check the payload block state */
1016 switch (s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK) {
1017 case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */
1018 case PAYLOAD_BLOCK_UNDEFINED: /* fall through */
1019 case PAYLOAD_BLOCK_UNMAPPED: /* fall through */
1020 case PAYLOAD_BLOCK_ZERO:
1021 /* return zero */
1022 qemu_iovec_memset(&hd_qiov, 0, 0, sinfo.bytes_avail);
1023 break;
1024 case PAYLOAD_BLOCK_FULL_PRESENT:
1025 qemu_co_mutex_unlock(&s->lock);
1026 ret = bdrv_co_readv(bs->file,
1027 sinfo.file_offset >> BDRV_SECTOR_BITS,
1028 sinfo.sectors_avail, &hd_qiov);
1029 qemu_co_mutex_lock(&s->lock);
1030 if (ret < 0) {
1031 goto exit;
1032 }
1033 break;
1034 case PAYLOAD_BLOCK_PARTIALLY_PRESENT:
1035 /* we don't yet support difference files, fall through
1036 * to error */
1037 default:
1038 ret = -EIO;
1039 goto exit;
1040 break;
1041 }
1042 nb_sectors -= sinfo.sectors_avail;
1043 sector_num += sinfo.sectors_avail;
1044 bytes_done += sinfo.bytes_avail;
1045 }
1046 }
1047 ret = 0;
1048exit:
1049 qemu_co_mutex_unlock(&s->lock);
1050 qemu_iovec_destroy(&hd_qiov);
1051 return ret;
e8d4e5ff
JC
1052}
1053
1054
1055
1056static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num,
1057 int nb_sectors, QEMUIOVector *qiov)
1058{
1059 return -ENOTSUP;
1060}
1061
1062
1063static void vhdx_close(BlockDriverState *bs)
1064{
1065 BDRVVHDXState *s = bs->opaque;
1066 qemu_vfree(s->headers[0]);
1067 qemu_vfree(s->headers[1]);
1068 qemu_vfree(s->bat);
1069 qemu_vfree(s->parent_entries);
5641bf40
JC
1070 migrate_del_blocker(s->migration_blocker);
1071 error_free(s->migration_blocker);
e8d4e5ff
JC
1072}
1073
1074static BlockDriver bdrv_vhdx = {
1075 .format_name = "vhdx",
1076 .instance_size = sizeof(BDRVVHDXState),
1077 .bdrv_probe = vhdx_probe,
1078 .bdrv_open = vhdx_open,
1079 .bdrv_close = vhdx_close,
1080 .bdrv_reopen_prepare = vhdx_reopen_prepare,
1081 .bdrv_co_readv = vhdx_co_readv,
1082 .bdrv_co_writev = vhdx_co_writev,
1083};
1084
1085static void bdrv_vhdx_init(void)
1086{
1087 bdrv_register(&bdrv_vhdx);
1088}
1089
1090block_init(bdrv_vhdx_init);