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