]>
Commit | Line | Data |
---|---|---|
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 ---------------------- */ | |
32 | static const MSGUID bat_guid = { .data1 = 0x2dc27766, | |
33 | .data2 = 0xf623, | |
34 | .data3 = 0x4200, | |
35 | .data4 = { 0x9d, 0x64, 0x11, 0x5e, | |
36 | 0x9b, 0xfd, 0x4a, 0x08} }; | |
37 | ||
38 | static 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 ---------------------- */ | |
47 | static 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 | ||
53 | static 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 | ||
59 | static 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 | ||
66 | static 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 | ||
72 | static 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 | ||
79 | static 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 */ | |
89 | static 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 |
109 | typedef 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 | */ | |
130 | uint32_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 |
144 | uint32_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 | */ | |
177 | bool 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 | */ | |
198 | void 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 | */ | |
218 | static 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 | ||
4f18b782 JC |
226 | /* Update the VHDX headers |
227 | * | |
228 | * This follows the VHDX spec procedures for header updates. | |
229 | * | |
230 | * - non-current header is updated with largest sequence number | |
231 | */ | |
232 | static int vhdx_update_header(BlockDriverState *bs, BDRVVHDXState *s, | |
233 | bool generate_data_write_guid) | |
234 | { | |
235 | int ret = 0; | |
236 | int hdr_idx = 0; | |
237 | uint64_t header_offset = VHDX_HEADER1_OFFSET; | |
238 | ||
239 | VHDXHeader *active_header; | |
240 | VHDXHeader *inactive_header; | |
241 | VHDXHeader header_le; | |
242 | uint8_t *buffer; | |
243 | ||
244 | /* operate on the non-current header */ | |
245 | if (s->curr_header == 0) { | |
246 | hdr_idx = 1; | |
247 | header_offset = VHDX_HEADER2_OFFSET; | |
248 | } | |
249 | ||
250 | active_header = s->headers[s->curr_header]; | |
251 | inactive_header = s->headers[hdr_idx]; | |
252 | ||
253 | inactive_header->sequence_number = active_header->sequence_number + 1; | |
254 | ||
255 | /* a new file guid must be generated before any file write, including | |
256 | * headers */ | |
257 | inactive_header->file_write_guid = s->session_guid; | |
258 | ||
259 | /* a new data guid only needs to be generated before any guest-visible | |
260 | * writes (i.e. something observable via virtual disk read) */ | |
261 | if (generate_data_write_guid) { | |
262 | vhdx_guid_generate(&inactive_header->data_write_guid); | |
263 | } | |
264 | ||
265 | /* the header checksum is not over just the packed size of VHDXHeader, | |
266 | * but rather over the entire 'reserved' range for the header, which is | |
267 | * 4KB (VHDX_HEADER_SIZE). */ | |
268 | ||
269 | buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE); | |
270 | /* we can't assume the extra reserved bytes are 0 */ | |
271 | ret = bdrv_pread(bs->file, header_offset, buffer, VHDX_HEADER_SIZE); | |
272 | if (ret < 0) { | |
273 | goto exit; | |
274 | } | |
275 | /* overwrite the actual VHDXHeader portion */ | |
276 | memcpy(buffer, inactive_header, sizeof(VHDXHeader)); | |
277 | inactive_header->checksum = | |
278 | vhdx_update_checksum(buffer, VHDX_HEADER_SIZE, | |
279 | offsetof(VHDXHeader, checksum)); | |
280 | vhdx_header_le_export(inactive_header, &header_le); | |
281 | ret = bdrv_pwrite_sync(bs->file, header_offset, &header_le, | |
282 | sizeof(VHDXHeader)); | |
283 | if (ret < 0) { | |
284 | goto exit; | |
285 | } | |
286 | s->curr_header = hdr_idx; | |
287 | ||
288 | exit: | |
289 | qemu_vfree(buffer); | |
290 | return ret; | |
291 | } | |
292 | ||
293 | /* | |
294 | * The VHDX spec calls for header updates to be performed twice, so that both | |
295 | * the current and non-current header have valid info | |
296 | */ | |
297 | static int vhdx_update_headers(BlockDriverState *bs, BDRVVHDXState *s, | |
298 | bool generate_data_write_guid) | |
299 | { | |
300 | int ret; | |
301 | ||
302 | ret = vhdx_update_header(bs, s, generate_data_write_guid); | |
303 | if (ret < 0) { | |
304 | return ret; | |
305 | } | |
306 | ret = vhdx_update_header(bs, s, generate_data_write_guid); | |
307 | return ret; | |
308 | } | |
e8d4e5ff JC |
309 | |
310 | /* opens the specified header block from the VHDX file header section */ | |
311 | static int vhdx_parse_header(BlockDriverState *bs, BDRVVHDXState *s) | |
312 | { | |
313 | int ret = 0; | |
314 | VHDXHeader *header1; | |
315 | VHDXHeader *header2; | |
316 | bool h1_valid = false; | |
317 | bool h2_valid = false; | |
318 | uint64_t h1_seq = 0; | |
319 | uint64_t h2_seq = 0; | |
320 | uint8_t *buffer; | |
321 | ||
6e9d290b | 322 | /* header1 & header2 are freed in vhdx_close() */ |
e8d4e5ff JC |
323 | header1 = qemu_blockalign(bs, sizeof(VHDXHeader)); |
324 | header2 = qemu_blockalign(bs, sizeof(VHDXHeader)); | |
325 | ||
326 | buffer = qemu_blockalign(bs, VHDX_HEADER_SIZE); | |
327 | ||
328 | s->headers[0] = header1; | |
329 | s->headers[1] = header2; | |
330 | ||
331 | /* We have to read the whole VHDX_HEADER_SIZE instead of | |
332 | * sizeof(VHDXHeader), because the checksum is over the whole | |
333 | * region */ | |
334 | ret = bdrv_pread(bs->file, VHDX_HEADER1_OFFSET, buffer, VHDX_HEADER_SIZE); | |
335 | if (ret < 0) { | |
336 | goto fail; | |
337 | } | |
338 | /* copy over just the relevant portion that we need */ | |
339 | memcpy(header1, buffer, sizeof(VHDXHeader)); | |
340 | vhdx_header_le_import(header1); | |
341 | ||
342 | if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4) && | |
343 | !memcmp(&header1->signature, "head", 4) && | |
344 | header1->version == 1) { | |
345 | h1_seq = header1->sequence_number; | |
346 | h1_valid = true; | |
347 | } | |
348 | ||
349 | ret = bdrv_pread(bs->file, VHDX_HEADER2_OFFSET, buffer, VHDX_HEADER_SIZE); | |
350 | if (ret < 0) { | |
351 | goto fail; | |
352 | } | |
353 | /* copy over just the relevant portion that we need */ | |
354 | memcpy(header2, buffer, sizeof(VHDXHeader)); | |
355 | vhdx_header_le_import(header2); | |
356 | ||
357 | if (vhdx_checksum_is_valid(buffer, VHDX_HEADER_SIZE, 4) && | |
358 | !memcmp(&header2->signature, "head", 4) && | |
359 | header2->version == 1) { | |
360 | h2_seq = header2->sequence_number; | |
361 | h2_valid = true; | |
362 | } | |
363 | ||
364 | /* If there is only 1 valid header (or no valid headers), we | |
365 | * don't care what the sequence numbers are */ | |
366 | if (h1_valid && !h2_valid) { | |
367 | s->curr_header = 0; | |
368 | } else if (!h1_valid && h2_valid) { | |
369 | s->curr_header = 1; | |
370 | } else if (!h1_valid && !h2_valid) { | |
371 | ret = -EINVAL; | |
372 | goto fail; | |
373 | } else { | |
374 | /* If both headers are valid, then we choose the active one by the | |
375 | * highest sequence number. If the sequence numbers are equal, that is | |
376 | * invalid */ | |
377 | if (h1_seq > h2_seq) { | |
378 | s->curr_header = 0; | |
379 | } else if (h2_seq > h1_seq) { | |
380 | s->curr_header = 1; | |
381 | } else { | |
382 | ret = -EINVAL; | |
383 | goto fail; | |
384 | } | |
385 | } | |
386 | ||
387 | ret = 0; | |
388 | ||
389 | goto exit; | |
390 | ||
391 | fail: | |
392 | qerror_report(ERROR_CLASS_GENERIC_ERROR, "No valid VHDX header found"); | |
393 | qemu_vfree(header1); | |
394 | qemu_vfree(header2); | |
395 | s->headers[0] = NULL; | |
396 | s->headers[1] = NULL; | |
397 | exit: | |
398 | qemu_vfree(buffer); | |
399 | return ret; | |
400 | } | |
401 | ||
402 | ||
403 | static int vhdx_open_region_tables(BlockDriverState *bs, BDRVVHDXState *s) | |
404 | { | |
405 | int ret = 0; | |
406 | uint8_t *buffer; | |
407 | int offset = 0; | |
408 | VHDXRegionTableEntry rt_entry; | |
409 | uint32_t i; | |
410 | bool bat_rt_found = false; | |
411 | bool metadata_rt_found = false; | |
412 | ||
413 | /* We have to read the whole 64KB block, because the crc32 is over the | |
414 | * whole block */ | |
415 | buffer = qemu_blockalign(bs, VHDX_HEADER_BLOCK_SIZE); | |
416 | ||
417 | ret = bdrv_pread(bs->file, VHDX_REGION_TABLE_OFFSET, buffer, | |
418 | VHDX_HEADER_BLOCK_SIZE); | |
419 | if (ret < 0) { | |
420 | goto fail; | |
421 | } | |
422 | memcpy(&s->rt, buffer, sizeof(s->rt)); | |
423 | le32_to_cpus(&s->rt.signature); | |
424 | le32_to_cpus(&s->rt.checksum); | |
425 | le32_to_cpus(&s->rt.entry_count); | |
426 | le32_to_cpus(&s->rt.reserved); | |
427 | offset += sizeof(s->rt); | |
428 | ||
429 | if (!vhdx_checksum_is_valid(buffer, VHDX_HEADER_BLOCK_SIZE, 4) || | |
430 | memcmp(&s->rt.signature, "regi", 4)) { | |
431 | ret = -EINVAL; | |
432 | goto fail; | |
433 | } | |
434 | ||
435 | /* Per spec, maximum region table entry count is 2047 */ | |
436 | if (s->rt.entry_count > 2047) { | |
437 | ret = -EINVAL; | |
438 | goto fail; | |
439 | } | |
440 | ||
441 | for (i = 0; i < s->rt.entry_count; i++) { | |
442 | memcpy(&rt_entry, buffer + offset, sizeof(rt_entry)); | |
443 | offset += sizeof(rt_entry); | |
444 | ||
445 | leguid_to_cpus(&rt_entry.guid); | |
446 | le64_to_cpus(&rt_entry.file_offset); | |
447 | le32_to_cpus(&rt_entry.length); | |
448 | le32_to_cpus(&rt_entry.data_bits); | |
449 | ||
450 | /* see if we recognize the entry */ | |
451 | if (guid_eq(rt_entry.guid, bat_guid)) { | |
452 | /* must be unique; if we have already found it this is invalid */ | |
453 | if (bat_rt_found) { | |
454 | ret = -EINVAL; | |
455 | goto fail; | |
456 | } | |
457 | bat_rt_found = true; | |
458 | s->bat_rt = rt_entry; | |
459 | continue; | |
460 | } | |
461 | ||
462 | if (guid_eq(rt_entry.guid, metadata_guid)) { | |
463 | /* must be unique; if we have already found it this is invalid */ | |
464 | if (metadata_rt_found) { | |
465 | ret = -EINVAL; | |
466 | goto fail; | |
467 | } | |
468 | metadata_rt_found = true; | |
469 | s->metadata_rt = rt_entry; | |
470 | continue; | |
471 | } | |
472 | ||
473 | if (rt_entry.data_bits & VHDX_REGION_ENTRY_REQUIRED) { | |
474 | /* cannot read vhdx file - required region table entry that | |
475 | * we do not understand. per spec, we must fail to open */ | |
476 | ret = -ENOTSUP; | |
477 | goto fail; | |
478 | } | |
479 | } | |
480 | ret = 0; | |
481 | ||
482 | fail: | |
483 | qemu_vfree(buffer); | |
484 | return ret; | |
485 | } | |
486 | ||
487 | ||
488 | ||
489 | /* Metadata initial parser | |
490 | * | |
491 | * This loads all the metadata entry fields. This may cause additional | |
492 | * fields to be processed (e.g. parent locator, etc..). | |
493 | * | |
494 | * There are 5 Metadata items that are always required: | |
495 | * - File Parameters (block size, has a parent) | |
496 | * - Virtual Disk Size (size, in bytes, of the virtual drive) | |
497 | * - Page 83 Data (scsi page 83 guid) | |
498 | * - Logical Sector Size (logical sector size in bytes, either 512 or | |
499 | * 4096. We only support 512 currently) | |
500 | * - Physical Sector Size (512 or 4096) | |
501 | * | |
502 | * Also, if the File Parameters indicate this is a differencing file, | |
503 | * we must also look for the Parent Locator metadata item. | |
504 | */ | |
505 | static int vhdx_parse_metadata(BlockDriverState *bs, BDRVVHDXState *s) | |
506 | { | |
507 | int ret = 0; | |
508 | uint8_t *buffer; | |
509 | int offset = 0; | |
510 | uint32_t i = 0; | |
511 | VHDXMetadataTableEntry md_entry; | |
512 | ||
513 | buffer = qemu_blockalign(bs, VHDX_METADATA_TABLE_MAX_SIZE); | |
514 | ||
515 | ret = bdrv_pread(bs->file, s->metadata_rt.file_offset, buffer, | |
516 | VHDX_METADATA_TABLE_MAX_SIZE); | |
517 | if (ret < 0) { | |
518 | goto exit; | |
519 | } | |
520 | memcpy(&s->metadata_hdr, buffer, sizeof(s->metadata_hdr)); | |
521 | offset += sizeof(s->metadata_hdr); | |
522 | ||
523 | le64_to_cpus(&s->metadata_hdr.signature); | |
524 | le16_to_cpus(&s->metadata_hdr.reserved); | |
525 | le16_to_cpus(&s->metadata_hdr.entry_count); | |
526 | ||
527 | if (memcmp(&s->metadata_hdr.signature, "metadata", 8)) { | |
528 | ret = -EINVAL; | |
529 | goto exit; | |
530 | } | |
531 | ||
532 | s->metadata_entries.present = 0; | |
533 | ||
534 | if ((s->metadata_hdr.entry_count * sizeof(md_entry)) > | |
535 | (VHDX_METADATA_TABLE_MAX_SIZE - offset)) { | |
536 | ret = -EINVAL; | |
537 | goto exit; | |
538 | } | |
539 | ||
540 | for (i = 0; i < s->metadata_hdr.entry_count; i++) { | |
541 | memcpy(&md_entry, buffer + offset, sizeof(md_entry)); | |
542 | offset += sizeof(md_entry); | |
543 | ||
544 | leguid_to_cpus(&md_entry.item_id); | |
545 | le32_to_cpus(&md_entry.offset); | |
546 | le32_to_cpus(&md_entry.length); | |
547 | le32_to_cpus(&md_entry.data_bits); | |
548 | le32_to_cpus(&md_entry.reserved2); | |
549 | ||
550 | if (guid_eq(md_entry.item_id, file_param_guid)) { | |
551 | if (s->metadata_entries.present & META_FILE_PARAMETER_PRESENT) { | |
552 | ret = -EINVAL; | |
553 | goto exit; | |
554 | } | |
555 | s->metadata_entries.file_parameters_entry = md_entry; | |
556 | s->metadata_entries.present |= META_FILE_PARAMETER_PRESENT; | |
557 | continue; | |
558 | } | |
559 | ||
560 | if (guid_eq(md_entry.item_id, virtual_size_guid)) { | |
561 | if (s->metadata_entries.present & META_VIRTUAL_DISK_SIZE_PRESENT) { | |
562 | ret = -EINVAL; | |
563 | goto exit; | |
564 | } | |
565 | s->metadata_entries.virtual_disk_size_entry = md_entry; | |
566 | s->metadata_entries.present |= META_VIRTUAL_DISK_SIZE_PRESENT; | |
567 | continue; | |
568 | } | |
569 | ||
570 | if (guid_eq(md_entry.item_id, page83_guid)) { | |
571 | if (s->metadata_entries.present & META_PAGE_83_PRESENT) { | |
572 | ret = -EINVAL; | |
573 | goto exit; | |
574 | } | |
575 | s->metadata_entries.page83_data_entry = md_entry; | |
576 | s->metadata_entries.present |= META_PAGE_83_PRESENT; | |
577 | continue; | |
578 | } | |
579 | ||
580 | if (guid_eq(md_entry.item_id, logical_sector_guid)) { | |
581 | if (s->metadata_entries.present & | |
582 | META_LOGICAL_SECTOR_SIZE_PRESENT) { | |
583 | ret = -EINVAL; | |
584 | goto exit; | |
585 | } | |
586 | s->metadata_entries.logical_sector_size_entry = md_entry; | |
587 | s->metadata_entries.present |= META_LOGICAL_SECTOR_SIZE_PRESENT; | |
588 | continue; | |
589 | } | |
590 | ||
591 | if (guid_eq(md_entry.item_id, phys_sector_guid)) { | |
592 | if (s->metadata_entries.present & META_PHYS_SECTOR_SIZE_PRESENT) { | |
593 | ret = -EINVAL; | |
594 | goto exit; | |
595 | } | |
596 | s->metadata_entries.phys_sector_size_entry = md_entry; | |
597 | s->metadata_entries.present |= META_PHYS_SECTOR_SIZE_PRESENT; | |
598 | continue; | |
599 | } | |
600 | ||
601 | if (guid_eq(md_entry.item_id, parent_locator_guid)) { | |
602 | if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) { | |
603 | ret = -EINVAL; | |
604 | goto exit; | |
605 | } | |
606 | s->metadata_entries.parent_locator_entry = md_entry; | |
607 | s->metadata_entries.present |= META_PARENT_LOCATOR_PRESENT; | |
608 | continue; | |
609 | } | |
610 | ||
611 | if (md_entry.data_bits & VHDX_META_FLAGS_IS_REQUIRED) { | |
612 | /* cannot read vhdx file - required region table entry that | |
613 | * we do not understand. per spec, we must fail to open */ | |
614 | ret = -ENOTSUP; | |
615 | goto exit; | |
616 | } | |
617 | } | |
618 | ||
619 | if (s->metadata_entries.present != META_ALL_PRESENT) { | |
620 | ret = -ENOTSUP; | |
621 | goto exit; | |
622 | } | |
623 | ||
624 | ret = bdrv_pread(bs->file, | |
625 | s->metadata_entries.file_parameters_entry.offset | |
626 | + s->metadata_rt.file_offset, | |
627 | &s->params, | |
628 | sizeof(s->params)); | |
629 | ||
630 | if (ret < 0) { | |
631 | goto exit; | |
632 | } | |
633 | ||
634 | le32_to_cpus(&s->params.block_size); | |
635 | le32_to_cpus(&s->params.data_bits); | |
636 | ||
637 | ||
638 | /* We now have the file parameters, so we can tell if this is a | |
639 | * differencing file (i.e.. has_parent), is dynamic or fixed | |
640 | * sized (leave_blocks_allocated), and the block size */ | |
641 | ||
642 | /* The parent locator required iff the file parameters has_parent set */ | |
643 | if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) { | |
644 | if (s->metadata_entries.present & META_PARENT_LOCATOR_PRESENT) { | |
645 | /* TODO: parse parent locator fields */ | |
646 | ret = -ENOTSUP; /* temp, until differencing files are supported */ | |
647 | goto exit; | |
648 | } else { | |
649 | /* if has_parent is set, but there is not parent locator present, | |
650 | * then that is an invalid combination */ | |
651 | ret = -EINVAL; | |
652 | goto exit; | |
653 | } | |
654 | } | |
655 | ||
656 | /* determine virtual disk size, logical sector size, | |
657 | * and phys sector size */ | |
658 | ||
659 | ret = bdrv_pread(bs->file, | |
660 | s->metadata_entries.virtual_disk_size_entry.offset | |
661 | + s->metadata_rt.file_offset, | |
662 | &s->virtual_disk_size, | |
663 | sizeof(uint64_t)); | |
664 | if (ret < 0) { | |
665 | goto exit; | |
666 | } | |
667 | ret = bdrv_pread(bs->file, | |
668 | s->metadata_entries.logical_sector_size_entry.offset | |
669 | + s->metadata_rt.file_offset, | |
670 | &s->logical_sector_size, | |
671 | sizeof(uint32_t)); | |
672 | if (ret < 0) { | |
673 | goto exit; | |
674 | } | |
675 | ret = bdrv_pread(bs->file, | |
676 | s->metadata_entries.phys_sector_size_entry.offset | |
677 | + s->metadata_rt.file_offset, | |
678 | &s->physical_sector_size, | |
679 | sizeof(uint32_t)); | |
680 | if (ret < 0) { | |
681 | goto exit; | |
682 | } | |
683 | ||
684 | le64_to_cpus(&s->virtual_disk_size); | |
685 | le32_to_cpus(&s->logical_sector_size); | |
686 | le32_to_cpus(&s->physical_sector_size); | |
687 | ||
688 | if (s->logical_sector_size == 0 || s->params.block_size == 0) { | |
689 | ret = -EINVAL; | |
690 | goto exit; | |
691 | } | |
692 | ||
693 | /* both block_size and sector_size are guaranteed powers of 2 */ | |
694 | s->sectors_per_block = s->params.block_size / s->logical_sector_size; | |
695 | s->chunk_ratio = (VHDX_MAX_SECTORS_PER_BLOCK) * | |
696 | (uint64_t)s->logical_sector_size / | |
697 | (uint64_t)s->params.block_size; | |
698 | ||
699 | /* These values are ones we will want to use for division / multiplication | |
700 | * later on, and they are all guaranteed (per the spec) to be powers of 2, | |
701 | * so we can take advantage of that for shift operations during | |
702 | * reads/writes */ | |
703 | if (s->logical_sector_size & (s->logical_sector_size - 1)) { | |
704 | ret = -EINVAL; | |
705 | goto exit; | |
706 | } | |
707 | if (s->sectors_per_block & (s->sectors_per_block - 1)) { | |
708 | ret = -EINVAL; | |
709 | goto exit; | |
710 | } | |
711 | if (s->chunk_ratio & (s->chunk_ratio - 1)) { | |
712 | ret = -EINVAL; | |
713 | goto exit; | |
714 | } | |
715 | s->block_size = s->params.block_size; | |
716 | if (s->block_size & (s->block_size - 1)) { | |
717 | ret = -EINVAL; | |
718 | goto exit; | |
719 | } | |
720 | ||
721 | s->logical_sector_size_bits = 31 - clz32(s->logical_sector_size); | |
722 | s->sectors_per_block_bits = 31 - clz32(s->sectors_per_block); | |
723 | s->chunk_ratio_bits = 63 - clz64(s->chunk_ratio); | |
724 | s->block_size_bits = 31 - clz32(s->block_size); | |
725 | ||
726 | ret = 0; | |
727 | ||
728 | exit: | |
729 | qemu_vfree(buffer); | |
730 | return ret; | |
731 | } | |
732 | ||
733 | /* Parse the replay log. Per the VHDX spec, if the log is present | |
734 | * it must be replayed prior to opening the file, even read-only. | |
735 | * | |
736 | * If read-only, we must replay the log in RAM (or refuse to open | |
737 | * a dirty VHDX file read-only */ | |
738 | static int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s) | |
739 | { | |
740 | int ret = 0; | |
741 | int i; | |
742 | VHDXHeader *hdr; | |
743 | ||
744 | hdr = s->headers[s->curr_header]; | |
745 | ||
746 | /* either the log guid, or log length is zero, | |
747 | * then a replay log is present */ | |
748 | for (i = 0; i < sizeof(hdr->log_guid.data4); i++) { | |
749 | ret |= hdr->log_guid.data4[i]; | |
750 | } | |
751 | if (hdr->log_guid.data1 == 0 && | |
752 | hdr->log_guid.data2 == 0 && | |
753 | hdr->log_guid.data3 == 0 && | |
754 | ret == 0) { | |
755 | goto exit; | |
756 | } | |
757 | ||
758 | /* per spec, only log version of 0 is supported */ | |
759 | if (hdr->log_version != 0) { | |
760 | ret = -EINVAL; | |
761 | goto exit; | |
762 | } | |
763 | ||
764 | if (hdr->log_length == 0) { | |
765 | goto exit; | |
766 | } | |
767 | ||
768 | /* We currently do not support images with logs to replay */ | |
769 | ret = -ENOTSUP; | |
770 | ||
771 | exit: | |
772 | return ret; | |
773 | } | |
774 | ||
775 | ||
015a1036 HR |
776 | static int vhdx_open(BlockDriverState *bs, QDict *options, int flags, |
777 | Error **errp) | |
e8d4e5ff JC |
778 | { |
779 | BDRVVHDXState *s = bs->opaque; | |
780 | int ret = 0; | |
781 | uint32_t i; | |
782 | uint64_t signature; | |
783 | uint32_t data_blocks_cnt, bitmap_blocks_cnt; | |
784 | ||
785 | ||
786 | s->bat = NULL; | |
787 | ||
788 | qemu_co_mutex_init(&s->lock); | |
789 | ||
790 | /* validate the file signature */ | |
791 | ret = bdrv_pread(bs->file, 0, &signature, sizeof(uint64_t)); | |
792 | if (ret < 0) { | |
793 | goto fail; | |
794 | } | |
795 | if (memcmp(&signature, "vhdxfile", 8)) { | |
796 | ret = -EINVAL; | |
797 | goto fail; | |
798 | } | |
799 | ||
4f18b782 JC |
800 | /* This is used for any header updates, for the file_write_guid. |
801 | * The spec dictates that a new value should be used for the first | |
802 | * header update */ | |
803 | vhdx_guid_generate(&s->session_guid); | |
804 | ||
e8d4e5ff JC |
805 | ret = vhdx_parse_header(bs, s); |
806 | if (ret) { | |
807 | goto fail; | |
808 | } | |
809 | ||
810 | ret = vhdx_parse_log(bs, s); | |
811 | if (ret) { | |
812 | goto fail; | |
813 | } | |
814 | ||
815 | ret = vhdx_open_region_tables(bs, s); | |
816 | if (ret) { | |
817 | goto fail; | |
818 | } | |
819 | ||
820 | ret = vhdx_parse_metadata(bs, s); | |
821 | if (ret) { | |
822 | goto fail; | |
823 | } | |
824 | s->block_size = s->params.block_size; | |
825 | ||
826 | /* the VHDX spec dictates that virtual_disk_size is always a multiple of | |
827 | * logical_sector_size */ | |
828 | bs->total_sectors = s->virtual_disk_size >> s->logical_sector_size_bits; | |
829 | ||
830 | data_blocks_cnt = s->virtual_disk_size >> s->block_size_bits; | |
831 | if (s->virtual_disk_size - (data_blocks_cnt << s->block_size_bits)) { | |
832 | data_blocks_cnt++; | |
833 | } | |
834 | bitmap_blocks_cnt = data_blocks_cnt >> s->chunk_ratio_bits; | |
835 | if (data_blocks_cnt - (bitmap_blocks_cnt << s->chunk_ratio_bits)) { | |
836 | bitmap_blocks_cnt++; | |
837 | } | |
838 | ||
839 | if (s->parent_entries) { | |
840 | s->bat_entries = bitmap_blocks_cnt * (s->chunk_ratio + 1); | |
841 | } else { | |
842 | s->bat_entries = data_blocks_cnt + | |
843 | ((data_blocks_cnt - 1) >> s->chunk_ratio_bits); | |
844 | } | |
845 | ||
846 | s->bat_offset = s->bat_rt.file_offset; | |
847 | ||
848 | if (s->bat_entries > s->bat_rt.length / sizeof(VHDXBatEntry)) { | |
849 | /* BAT allocation is not large enough for all entries */ | |
850 | ret = -EINVAL; | |
851 | goto fail; | |
852 | } | |
853 | ||
6e9d290b | 854 | /* s->bat is freed in vhdx_close() */ |
e8d4e5ff JC |
855 | s->bat = qemu_blockalign(bs, s->bat_rt.length); |
856 | ||
857 | ret = bdrv_pread(bs->file, s->bat_offset, s->bat, s->bat_rt.length); | |
858 | if (ret < 0) { | |
859 | goto fail; | |
860 | } | |
861 | ||
862 | for (i = 0; i < s->bat_entries; i++) { | |
863 | le64_to_cpus(&s->bat[i]); | |
864 | } | |
865 | ||
866 | if (flags & BDRV_O_RDWR) { | |
4f18b782 JC |
867 | ret = vhdx_update_headers(bs, s, false); |
868 | if (ret < 0) { | |
869 | goto fail; | |
870 | } | |
e8d4e5ff JC |
871 | } |
872 | ||
059e2fbb | 873 | /* TODO: differencing files, write */ |
e8d4e5ff | 874 | |
5641bf40 JC |
875 | /* Disable migration when VHDX images are used */ |
876 | error_set(&s->migration_blocker, | |
877 | QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
878 | "vhdx", bs->device_name, "live migration"); | |
879 | migrate_add_blocker(s->migration_blocker); | |
880 | ||
e8d4e5ff JC |
881 | return 0; |
882 | fail: | |
883 | qemu_vfree(s->headers[0]); | |
884 | qemu_vfree(s->headers[1]); | |
885 | qemu_vfree(s->bat); | |
886 | qemu_vfree(s->parent_entries); | |
887 | return ret; | |
888 | } | |
889 | ||
890 | static int vhdx_reopen_prepare(BDRVReopenState *state, | |
891 | BlockReopenQueue *queue, Error **errp) | |
892 | { | |
893 | return 0; | |
894 | } | |
895 | ||
896 | ||
059e2fbb JC |
897 | /* |
898 | * Perform sector to block offset translations, to get various | |
899 | * sector and file offsets into the image. See VHDXSectorInfo | |
900 | */ | |
901 | static void vhdx_block_translate(BDRVVHDXState *s, int64_t sector_num, | |
902 | int nb_sectors, VHDXSectorInfo *sinfo) | |
903 | { | |
904 | uint32_t block_offset; | |
905 | ||
906 | sinfo->bat_idx = sector_num >> s->sectors_per_block_bits; | |
907 | /* effectively a modulo - this gives us the offset into the block | |
908 | * (in sector sizes) for our sector number */ | |
909 | block_offset = sector_num - (sinfo->bat_idx << s->sectors_per_block_bits); | |
910 | /* the chunk ratio gives us the interleaving of the sector | |
911 | * bitmaps, so we need to advance our page block index by the | |
912 | * sector bitmaps entry number */ | |
913 | sinfo->bat_idx += sinfo->bat_idx >> s->chunk_ratio_bits; | |
914 | ||
915 | /* the number of sectors we can read/write in this cycle */ | |
916 | sinfo->sectors_avail = s->sectors_per_block - block_offset; | |
917 | ||
918 | sinfo->bytes_left = sinfo->sectors_avail << s->logical_sector_size_bits; | |
919 | ||
920 | if (sinfo->sectors_avail > nb_sectors) { | |
921 | sinfo->sectors_avail = nb_sectors; | |
922 | } | |
923 | ||
924 | sinfo->bytes_avail = sinfo->sectors_avail << s->logical_sector_size_bits; | |
925 | ||
926 | sinfo->file_offset = s->bat[sinfo->bat_idx] >> VHDX_BAT_FILE_OFF_BITS; | |
927 | ||
928 | sinfo->block_offset = block_offset << s->logical_sector_size_bits; | |
929 | ||
930 | /* The file offset must be past the header section, so must be > 0 */ | |
931 | if (sinfo->file_offset == 0) { | |
932 | return; | |
933 | } | |
934 | ||
935 | /* block offset is the offset in vhdx logical sectors, in | |
936 | * the payload data block. Convert that to a byte offset | |
937 | * in the block, and add in the payload data block offset | |
938 | * in the file, in bytes, to get the final read address */ | |
939 | ||
940 | sinfo->file_offset <<= 20; /* now in bytes, rather than 1MB units */ | |
941 | sinfo->file_offset += sinfo->block_offset; | |
942 | } | |
943 | ||
944 | ||
945 | ||
e8d4e5ff JC |
946 | static coroutine_fn int vhdx_co_readv(BlockDriverState *bs, int64_t sector_num, |
947 | int nb_sectors, QEMUIOVector *qiov) | |
948 | { | |
059e2fbb JC |
949 | BDRVVHDXState *s = bs->opaque; |
950 | int ret = 0; | |
951 | VHDXSectorInfo sinfo; | |
952 | uint64_t bytes_done = 0; | |
953 | QEMUIOVector hd_qiov; | |
954 | ||
955 | qemu_iovec_init(&hd_qiov, qiov->niov); | |
956 | ||
957 | qemu_co_mutex_lock(&s->lock); | |
958 | ||
959 | while (nb_sectors > 0) { | |
960 | /* We are a differencing file, so we need to inspect the sector bitmap | |
961 | * to see if we have the data or not */ | |
962 | if (s->params.data_bits & VHDX_PARAMS_HAS_PARENT) { | |
963 | /* not supported yet */ | |
964 | ret = -ENOTSUP; | |
965 | goto exit; | |
966 | } else { | |
967 | vhdx_block_translate(s, sector_num, nb_sectors, &sinfo); | |
968 | ||
969 | qemu_iovec_reset(&hd_qiov); | |
970 | qemu_iovec_concat(&hd_qiov, qiov, bytes_done, sinfo.bytes_avail); | |
971 | ||
972 | /* check the payload block state */ | |
973 | switch (s->bat[sinfo.bat_idx] & VHDX_BAT_STATE_BIT_MASK) { | |
974 | case PAYLOAD_BLOCK_NOT_PRESENT: /* fall through */ | |
975 | case PAYLOAD_BLOCK_UNDEFINED: /* fall through */ | |
976 | case PAYLOAD_BLOCK_UNMAPPED: /* fall through */ | |
977 | case PAYLOAD_BLOCK_ZERO: | |
978 | /* return zero */ | |
979 | qemu_iovec_memset(&hd_qiov, 0, 0, sinfo.bytes_avail); | |
980 | break; | |
981 | case PAYLOAD_BLOCK_FULL_PRESENT: | |
982 | qemu_co_mutex_unlock(&s->lock); | |
983 | ret = bdrv_co_readv(bs->file, | |
984 | sinfo.file_offset >> BDRV_SECTOR_BITS, | |
985 | sinfo.sectors_avail, &hd_qiov); | |
986 | qemu_co_mutex_lock(&s->lock); | |
987 | if (ret < 0) { | |
988 | goto exit; | |
989 | } | |
990 | break; | |
991 | case PAYLOAD_BLOCK_PARTIALLY_PRESENT: | |
992 | /* we don't yet support difference files, fall through | |
993 | * to error */ | |
994 | default: | |
995 | ret = -EIO; | |
996 | goto exit; | |
997 | break; | |
998 | } | |
999 | nb_sectors -= sinfo.sectors_avail; | |
1000 | sector_num += sinfo.sectors_avail; | |
1001 | bytes_done += sinfo.bytes_avail; | |
1002 | } | |
1003 | } | |
1004 | ret = 0; | |
1005 | exit: | |
1006 | qemu_co_mutex_unlock(&s->lock); | |
1007 | qemu_iovec_destroy(&hd_qiov); | |
1008 | return ret; | |
e8d4e5ff JC |
1009 | } |
1010 | ||
1011 | ||
1012 | ||
1013 | static coroutine_fn int vhdx_co_writev(BlockDriverState *bs, int64_t sector_num, | |
1014 | int nb_sectors, QEMUIOVector *qiov) | |
1015 | { | |
1016 | return -ENOTSUP; | |
1017 | } | |
1018 | ||
1019 | ||
1020 | static void vhdx_close(BlockDriverState *bs) | |
1021 | { | |
1022 | BDRVVHDXState *s = bs->opaque; | |
1023 | qemu_vfree(s->headers[0]); | |
1024 | qemu_vfree(s->headers[1]); | |
1025 | qemu_vfree(s->bat); | |
1026 | qemu_vfree(s->parent_entries); | |
5641bf40 JC |
1027 | migrate_del_blocker(s->migration_blocker); |
1028 | error_free(s->migration_blocker); | |
e8d4e5ff JC |
1029 | } |
1030 | ||
1031 | static BlockDriver bdrv_vhdx = { | |
1032 | .format_name = "vhdx", | |
1033 | .instance_size = sizeof(BDRVVHDXState), | |
1034 | .bdrv_probe = vhdx_probe, | |
1035 | .bdrv_open = vhdx_open, | |
1036 | .bdrv_close = vhdx_close, | |
1037 | .bdrv_reopen_prepare = vhdx_reopen_prepare, | |
1038 | .bdrv_co_readv = vhdx_co_readv, | |
1039 | .bdrv_co_writev = vhdx_co_writev, | |
1040 | }; | |
1041 | ||
1042 | static void bdrv_vhdx_init(void) | |
1043 | { | |
1044 | bdrv_register(&bdrv_vhdx); | |
1045 | } | |
1046 | ||
1047 | block_init(bdrv_vhdx_init); |