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