/src/qemu/block/vhdx-log.c
Line | Count | Source |
1 | | /* |
2 | | * Block driver for Hyper-V VHDX Images |
3 | | * |
4 | | * Copyright (c) 2013 Red Hat, Inc., |
5 | | * |
6 | | * Authors: |
7 | | * Jeff Cody <jcody@redhat.com> |
8 | | * |
9 | | * This is based on the "VHDX Format Specification v1.00", published 8/25/2012 |
10 | | * by Microsoft: |
11 | | * https://www.microsoft.com/en-us/download/details.aspx?id=34750 |
12 | | * |
13 | | * This file covers the functionality of the metadata log writing, parsing, and |
14 | | * replay. |
15 | | * |
16 | | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
17 | | * See the COPYING.LIB file in the top-level directory. |
18 | | * |
19 | | */ |
20 | | |
21 | | #include "qemu/osdep.h" |
22 | | #include "qapi/error.h" |
23 | | #include "block/block-io.h" |
24 | | #include "block/block_int.h" |
25 | | #include "qemu/error-report.h" |
26 | | #include "qemu/bswap.h" |
27 | | #include "qemu/memalign.h" |
28 | | #include "vhdx.h" |
29 | | |
30 | | |
31 | | typedef struct VHDXLogSequence { |
32 | | bool valid; |
33 | | uint32_t count; |
34 | | VHDXLogEntries log; |
35 | | VHDXLogEntryHeader hdr; |
36 | | } VHDXLogSequence; |
37 | | |
38 | | typedef struct VHDXLogDescEntries { |
39 | | VHDXLogEntryHeader hdr; |
40 | | VHDXLogDescriptor desc[]; |
41 | | } VHDXLogDescEntries; |
42 | | |
43 | | static const MSGUID zero_guid = { 0 }; |
44 | | |
45 | | /* The log located on the disk is circular buffer containing |
46 | | * sectors of 4096 bytes each. |
47 | | * |
48 | | * It is assumed for the read/write functions below that the |
49 | | * circular buffer scheme uses a 'one sector open' to indicate |
50 | | * the buffer is full. Given the validation methods used for each |
51 | | * sector, this method should be compatible with other methods that |
52 | | * do not waste a sector. |
53 | | */ |
54 | | |
55 | | |
56 | | /* Allow peeking at the hdr entry at the beginning of the current |
57 | | * read index, without advancing the read index */ |
58 | | static int GRAPH_RDLOCK |
59 | | vhdx_log_peek_hdr(BlockDriverState *bs, VHDXLogEntries *log, |
60 | | VHDXLogEntryHeader *hdr) |
61 | 0 | { |
62 | 0 | int ret = 0; |
63 | 0 | uint64_t offset; |
64 | 0 | uint32_t read; |
65 | |
|
66 | 0 | assert(hdr != NULL); |
67 | | |
68 | | /* peek is only supported on sector boundaries */ |
69 | 0 | if (log->read % VHDX_LOG_SECTOR_SIZE) { |
70 | 0 | ret = -EFAULT; |
71 | 0 | goto exit; |
72 | 0 | } |
73 | | |
74 | 0 | read = log->read; |
75 | | /* we are guaranteed that a) log sectors are 4096 bytes, |
76 | | * and b) the log length is a multiple of 1MB. So, there |
77 | | * is always a round number of sectors in the buffer */ |
78 | 0 | if ((read + sizeof(VHDXLogEntryHeader)) > log->length) { |
79 | 0 | read = 0; |
80 | 0 | } |
81 | |
|
82 | 0 | if (read == log->write) { |
83 | 0 | ret = -EINVAL; |
84 | 0 | goto exit; |
85 | 0 | } |
86 | | |
87 | 0 | offset = log->offset + read; |
88 | |
|
89 | 0 | ret = bdrv_pread(bs->file, offset, sizeof(VHDXLogEntryHeader), hdr, 0); |
90 | 0 | if (ret < 0) { |
91 | 0 | goto exit; |
92 | 0 | } |
93 | 0 | vhdx_log_entry_hdr_le_import(hdr); |
94 | |
|
95 | 0 | exit: |
96 | 0 | return ret; |
97 | 0 | } |
98 | | |
99 | | /* Index increment for log, based on sector boundaries */ |
100 | | static int vhdx_log_inc_idx(uint32_t idx, uint64_t length) |
101 | 0 | { |
102 | 0 | idx += VHDX_LOG_SECTOR_SIZE; |
103 | | /* we are guaranteed that a) log sectors are 4096 bytes, |
104 | | * and b) the log length is a multiple of 1MB. So, there |
105 | | * is always a round number of sectors in the buffer */ |
106 | 0 | return idx >= length ? 0 : idx; |
107 | 0 | } |
108 | | |
109 | | |
110 | | /* Reset the log to empty */ |
111 | | static void GRAPH_RDLOCK vhdx_log_reset(BlockDriverState *bs, BDRVVHDXState *s) |
112 | 0 | { |
113 | 0 | MSGUID guid = { 0 }; |
114 | 0 | s->log.read = s->log.write = 0; |
115 | | /* a log guid of 0 indicates an empty log to any parser of v0 |
116 | | * VHDX logs */ |
117 | 0 | vhdx_update_headers(bs, s, false, &guid); |
118 | 0 | } |
119 | | |
120 | | /* Reads num_sectors from the log (all log sectors are 4096 bytes), |
121 | | * into buffer 'buffer'. Upon return, *sectors_read will contain |
122 | | * the number of sectors successfully read. |
123 | | * |
124 | | * It is assumed that 'buffer' is already allocated, and of sufficient |
125 | | * size (i.e. >= 4096*num_sectors). |
126 | | * |
127 | | * If 'peek' is true, then the tail (read) pointer for the circular buffer is |
128 | | * not modified. |
129 | | * |
130 | | * 0 is returned on success, -errno otherwise. */ |
131 | | static int GRAPH_RDLOCK |
132 | | vhdx_log_read_sectors(BlockDriverState *bs, VHDXLogEntries *log, |
133 | | uint32_t *sectors_read, void *buffer, |
134 | | uint32_t num_sectors, bool peek) |
135 | 0 | { |
136 | 0 | int ret = 0; |
137 | 0 | uint64_t offset; |
138 | 0 | uint32_t read; |
139 | |
|
140 | 0 | read = log->read; |
141 | |
|
142 | 0 | *sectors_read = 0; |
143 | 0 | while (num_sectors) { |
144 | 0 | if (read == log->write) { |
145 | | /* empty */ |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | offset = log->offset + read; |
149 | |
|
150 | 0 | ret = bdrv_pread(bs->file, offset, VHDX_LOG_SECTOR_SIZE, buffer, 0); |
151 | 0 | if (ret < 0) { |
152 | 0 | goto exit; |
153 | 0 | } |
154 | 0 | read = vhdx_log_inc_idx(read, log->length); |
155 | |
|
156 | 0 | *sectors_read = *sectors_read + 1; |
157 | 0 | num_sectors--; |
158 | 0 | } |
159 | | |
160 | 0 | exit: |
161 | 0 | if (!peek) { |
162 | 0 | log->read = read; |
163 | 0 | } |
164 | 0 | return ret; |
165 | 0 | } |
166 | | |
167 | | /* Writes num_sectors to the log (all log sectors are 4096 bytes), |
168 | | * from buffer 'buffer'. Upon return, *sectors_written will contain |
169 | | * the number of sectors successfully written. |
170 | | * |
171 | | * It is assumed that 'buffer' is at least 4096*num_sectors large. |
172 | | * |
173 | | * 0 is returned on success, -errno otherwise */ |
174 | | static int coroutine_fn GRAPH_RDLOCK |
175 | | vhdx_log_write_sectors(BlockDriverState *bs, VHDXLogEntries *log, |
176 | | uint32_t *sectors_written, void *buffer, |
177 | | uint32_t num_sectors) |
178 | 0 | { |
179 | 0 | int ret = 0; |
180 | 0 | uint64_t offset; |
181 | 0 | uint32_t write; |
182 | 0 | void *buffer_tmp; |
183 | 0 | BDRVVHDXState *s = bs->opaque; |
184 | |
|
185 | 0 | ret = vhdx_user_visible_write(bs, s); |
186 | 0 | if (ret < 0) { |
187 | 0 | goto exit; |
188 | 0 | } |
189 | | |
190 | 0 | write = log->write; |
191 | |
|
192 | 0 | buffer_tmp = buffer; |
193 | 0 | while (num_sectors) { |
194 | |
|
195 | 0 | offset = log->offset + write; |
196 | 0 | write = vhdx_log_inc_idx(write, log->length); |
197 | 0 | if (write == log->read) { |
198 | | /* full */ |
199 | 0 | break; |
200 | 0 | } |
201 | 0 | ret = bdrv_co_pwrite(bs->file, offset, VHDX_LOG_SECTOR_SIZE, buffer_tmp, 0); |
202 | 0 | if (ret < 0) { |
203 | 0 | goto exit; |
204 | 0 | } |
205 | 0 | buffer_tmp += VHDX_LOG_SECTOR_SIZE; |
206 | |
|
207 | 0 | log->write = write; |
208 | 0 | *sectors_written = *sectors_written + 1; |
209 | 0 | num_sectors--; |
210 | 0 | } |
211 | | |
212 | 0 | exit: |
213 | 0 | return ret; |
214 | 0 | } |
215 | | |
216 | | |
217 | | /* Validates a log entry header */ |
218 | | static bool vhdx_log_hdr_is_valid(VHDXLogEntries *log, VHDXLogEntryHeader *hdr, |
219 | | BDRVVHDXState *s) |
220 | 0 | { |
221 | 0 | int valid = false; |
222 | |
|
223 | 0 | if (hdr->signature != VHDX_LOG_SIGNATURE) { |
224 | 0 | goto exit; |
225 | 0 | } |
226 | | |
227 | | /* if the individual entry length is larger than the whole log |
228 | | * buffer, that is obviously invalid */ |
229 | 0 | if (log->length < hdr->entry_length) { |
230 | 0 | goto exit; |
231 | 0 | } |
232 | | |
233 | | /* length of entire entry must be in units of 4KB (log sector size) */ |
234 | 0 | if (hdr->entry_length % (VHDX_LOG_SECTOR_SIZE)) { |
235 | 0 | goto exit; |
236 | 0 | } |
237 | | |
238 | | /* per spec, sequence # must be > 0 */ |
239 | 0 | if (hdr->sequence_number == 0) { |
240 | 0 | goto exit; |
241 | 0 | } |
242 | | |
243 | | /* log entries are only valid if they match the file-wide log guid |
244 | | * found in the active header */ |
245 | 0 | if (!guid_eq(hdr->log_guid, s->headers[s->curr_header]->log_guid)) { |
246 | 0 | goto exit; |
247 | 0 | } |
248 | | |
249 | 0 | if (hdr->descriptor_count > hdr->entry_length / sizeof(VHDXLogDescriptor)) { |
250 | 0 | goto exit; |
251 | 0 | } |
252 | | |
253 | 0 | valid = true; |
254 | |
|
255 | 0 | exit: |
256 | 0 | return valid; |
257 | 0 | } |
258 | | |
259 | | /* |
260 | | * Given a log header, this will validate that the descriptors and the |
261 | | * corresponding data sectors (if applicable) |
262 | | * |
263 | | * Validation consists of: |
264 | | * 1. Making sure the sequence numbers matches the entry header |
265 | | * 2. Verifying a valid signature ('zero' or 'desc' for descriptors) |
266 | | * 3. File offset field is a multiple of 4KB |
267 | | * 4. If a data descriptor, the corresponding data sector |
268 | | * has its signature ('data') and matching sequence number |
269 | | * |
270 | | * @desc: the data buffer containing the descriptor |
271 | | * @hdr: the log entry header |
272 | | * |
273 | | * Returns true if valid |
274 | | */ |
275 | | static bool vhdx_log_desc_is_valid(VHDXLogDescriptor *desc, |
276 | | VHDXLogEntryHeader *hdr) |
277 | 0 | { |
278 | 0 | bool ret = false; |
279 | |
|
280 | 0 | if (desc->sequence_number != hdr->sequence_number) { |
281 | 0 | goto exit; |
282 | 0 | } |
283 | 0 | if (desc->file_offset % VHDX_LOG_SECTOR_SIZE) { |
284 | 0 | goto exit; |
285 | 0 | } |
286 | | |
287 | 0 | if (desc->signature == VHDX_LOG_ZERO_SIGNATURE) { |
288 | 0 | if (desc->zero_length % VHDX_LOG_SECTOR_SIZE == 0) { |
289 | | /* valid */ |
290 | 0 | ret = true; |
291 | 0 | } |
292 | 0 | } else if (desc->signature == VHDX_LOG_DESC_SIGNATURE) { |
293 | | /* valid */ |
294 | 0 | ret = true; |
295 | 0 | } |
296 | |
|
297 | 0 | exit: |
298 | 0 | return ret; |
299 | 0 | } |
300 | | |
301 | | |
302 | | /* Prior to sector data for a log entry, there is the header |
303 | | * and the descriptors referenced in the header: |
304 | | * |
305 | | * [] = 4KB sector |
306 | | * |
307 | | * [ hdr, desc ][ desc ][ ... ][ data ][ ... ] |
308 | | * |
309 | | * The first sector in a log entry has a 64 byte header, and |
310 | | * up to 126 32-byte descriptors. If more descriptors than |
311 | | * 126 are required, then subsequent sectors can have up to 128 |
312 | | * descriptors. Each sector is 4KB. Data follows the descriptor |
313 | | * sectors. |
314 | | * |
315 | | * This will return the number of sectors needed to encompass |
316 | | * the passed number of descriptors in desc_cnt. |
317 | | * |
318 | | * This will never return 0, even if desc_cnt is 0. |
319 | | */ |
320 | | static int vhdx_compute_desc_sectors(uint32_t desc_cnt) |
321 | 0 | { |
322 | 0 | uint32_t desc_sectors; |
323 | |
|
324 | 0 | desc_cnt += 2; /* account for header in first sector */ |
325 | 0 | desc_sectors = desc_cnt / 128; |
326 | 0 | if (desc_cnt % 128) { |
327 | 0 | desc_sectors++; |
328 | 0 | } |
329 | |
|
330 | 0 | return desc_sectors; |
331 | 0 | } |
332 | | |
333 | | |
334 | | /* Reads the log header, and subsequent descriptors (if any). This |
335 | | * will allocate all the space for buffer, which must be NULL when |
336 | | * passed into this function. Each descriptor will also be validated, |
337 | | * and error returned if any are invalid. */ |
338 | | static int GRAPH_RDLOCK |
339 | | vhdx_log_read_desc(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogEntries *log, |
340 | | VHDXLogDescEntries **buffer, bool convert_endian) |
341 | 0 | { |
342 | 0 | int ret = 0; |
343 | 0 | uint32_t desc_sectors; |
344 | 0 | uint32_t sectors_read; |
345 | 0 | VHDXLogEntryHeader hdr; |
346 | 0 | VHDXLogDescEntries *desc_entries = NULL; |
347 | 0 | VHDXLogDescriptor desc; |
348 | 0 | int i; |
349 | |
|
350 | 0 | assert(*buffer == NULL); |
351 | |
|
352 | 0 | ret = vhdx_log_peek_hdr(bs, log, &hdr); |
353 | 0 | if (ret < 0) { |
354 | 0 | goto exit; |
355 | 0 | } |
356 | | |
357 | 0 | if (vhdx_log_hdr_is_valid(log, &hdr, s) == false) { |
358 | 0 | ret = -EINVAL; |
359 | 0 | goto exit; |
360 | 0 | } |
361 | | |
362 | 0 | desc_sectors = vhdx_compute_desc_sectors(hdr.descriptor_count); |
363 | 0 | if (desc_sectors > UINT32_MAX / VHDX_LOG_SECTOR_SIZE) { |
364 | 0 | ret = -EINVAL; |
365 | 0 | goto exit; |
366 | 0 | } |
367 | 0 | desc_entries = qemu_try_blockalign(bs->file->bs, |
368 | 0 | desc_sectors * VHDX_LOG_SECTOR_SIZE); |
369 | 0 | if (desc_entries == NULL) { |
370 | 0 | ret = -ENOMEM; |
371 | 0 | goto exit; |
372 | 0 | } |
373 | | |
374 | 0 | ret = vhdx_log_read_sectors(bs, log, §ors_read, desc_entries, |
375 | 0 | desc_sectors, false); |
376 | 0 | if (ret < 0) { |
377 | 0 | goto free_and_exit; |
378 | 0 | } |
379 | 0 | if (sectors_read != desc_sectors) { |
380 | 0 | ret = -EINVAL; |
381 | 0 | goto free_and_exit; |
382 | 0 | } |
383 | | |
384 | | /* put in proper endianness, and validate each desc */ |
385 | 0 | for (i = 0; i < hdr.descriptor_count; i++) { |
386 | 0 | desc = desc_entries->desc[i]; |
387 | 0 | vhdx_log_desc_le_import(&desc); |
388 | 0 | if (convert_endian) { |
389 | 0 | desc_entries->desc[i] = desc; |
390 | 0 | } |
391 | 0 | if (vhdx_log_desc_is_valid(&desc, &hdr) == false) { |
392 | 0 | ret = -EINVAL; |
393 | 0 | goto free_and_exit; |
394 | 0 | } |
395 | 0 | } |
396 | 0 | if (convert_endian) { |
397 | 0 | desc_entries->hdr = hdr; |
398 | 0 | } |
399 | |
|
400 | 0 | *buffer = desc_entries; |
401 | 0 | goto exit; |
402 | | |
403 | 0 | free_and_exit: |
404 | 0 | qemu_vfree(desc_entries); |
405 | 0 | exit: |
406 | 0 | return ret; |
407 | 0 | } |
408 | | |
409 | | |
410 | | /* Flushes the descriptor described by desc to the VHDX image file. |
411 | | * If the descriptor is a data descriptor, than 'data' must be non-NULL, |
412 | | * and >= 4096 bytes (VHDX_LOG_SECTOR_SIZE), containing the data to be |
413 | | * written. |
414 | | * |
415 | | * Verification is performed to make sure the sequence numbers of a data |
416 | | * descriptor match the sequence number in the desc. |
417 | | * |
418 | | * For a zero descriptor, it may describe multiple sectors to fill with zeroes. |
419 | | * In this case, it should be noted that zeroes are written to disk, and the |
420 | | * image file is not extended as a sparse file. */ |
421 | | static int GRAPH_RDLOCK |
422 | | vhdx_log_flush_desc(BlockDriverState *bs, VHDXLogDescriptor *desc, |
423 | | VHDXLogDataSector *data) |
424 | 0 | { |
425 | 0 | int ret = 0; |
426 | 0 | uint64_t seq, file_offset; |
427 | 0 | uint32_t offset = 0; |
428 | 0 | void *buffer = NULL; |
429 | 0 | uint64_t count = 1; |
430 | 0 | int i; |
431 | |
|
432 | 0 | buffer = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE); |
433 | |
|
434 | 0 | if (desc->signature == VHDX_LOG_DESC_SIGNATURE) { |
435 | | /* data sector */ |
436 | 0 | if (data == NULL) { |
437 | 0 | ret = -EFAULT; |
438 | 0 | goto exit; |
439 | 0 | } |
440 | | |
441 | | /* The sequence number of the data sector must match that |
442 | | * in the descriptor */ |
443 | 0 | seq = data->sequence_high; |
444 | 0 | seq <<= 32; |
445 | 0 | seq |= data->sequence_low & 0xffffffff; |
446 | |
|
447 | 0 | if (seq != desc->sequence_number) { |
448 | 0 | ret = -EINVAL; |
449 | 0 | goto exit; |
450 | 0 | } |
451 | | |
452 | | /* Each data sector is in total 4096 bytes, however the first |
453 | | * 8 bytes, and last 4 bytes, are located in the descriptor */ |
454 | 0 | memcpy(buffer, &desc->leading_bytes, 8); |
455 | 0 | offset += 8; |
456 | |
|
457 | 0 | memcpy(buffer+offset, data->data, 4084); |
458 | 0 | offset += 4084; |
459 | |
|
460 | 0 | memcpy(buffer+offset, &desc->trailing_bytes, 4); |
461 | |
|
462 | 0 | } else if (desc->signature == VHDX_LOG_ZERO_SIGNATURE) { |
463 | | /* write 'count' sectors of sector */ |
464 | 0 | memset(buffer, 0, VHDX_LOG_SECTOR_SIZE); |
465 | 0 | count = desc->zero_length / VHDX_LOG_SECTOR_SIZE; |
466 | 0 | } else { |
467 | 0 | error_report("Invalid VHDX log descriptor entry signature 0x%" PRIx32, |
468 | 0 | desc->signature); |
469 | 0 | ret = -EINVAL; |
470 | 0 | goto exit; |
471 | 0 | } |
472 | | |
473 | 0 | file_offset = desc->file_offset; |
474 | | |
475 | | /* count is only > 1 if we are writing zeroes */ |
476 | 0 | for (i = 0; i < count; i++) { |
477 | 0 | ret = bdrv_pwrite_sync(bs->file, file_offset, VHDX_LOG_SECTOR_SIZE, |
478 | 0 | buffer, 0); |
479 | 0 | if (ret < 0) { |
480 | 0 | goto exit; |
481 | 0 | } |
482 | 0 | file_offset += VHDX_LOG_SECTOR_SIZE; |
483 | 0 | } |
484 | | |
485 | 0 | exit: |
486 | 0 | qemu_vfree(buffer); |
487 | 0 | return ret; |
488 | 0 | } |
489 | | |
490 | | /* Flush the entire log (as described by 'logs') to the VHDX image |
491 | | * file, and then set the log to 'empty' status once complete. |
492 | | * |
493 | | * The log entries should be validate prior to flushing */ |
494 | | static int GRAPH_RDLOCK |
495 | | vhdx_log_flush(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *logs) |
496 | 0 | { |
497 | 0 | int ret = 0; |
498 | 0 | int i; |
499 | 0 | uint32_t cnt, sectors_read; |
500 | 0 | uint64_t new_file_size; |
501 | 0 | void *data = NULL; |
502 | 0 | int64_t file_length; |
503 | 0 | VHDXLogDescEntries *desc_entries = NULL; |
504 | 0 | VHDXLogEntryHeader hdr_tmp = { 0 }; |
505 | |
|
506 | 0 | cnt = logs->count; |
507 | |
|
508 | 0 | data = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE); |
509 | |
|
510 | 0 | ret = vhdx_user_visible_write(bs, s); |
511 | 0 | if (ret < 0) { |
512 | 0 | goto exit; |
513 | 0 | } |
514 | | |
515 | | /* each iteration represents one log sequence, which may span multiple |
516 | | * sectors */ |
517 | 0 | while (cnt--) { |
518 | 0 | ret = vhdx_log_peek_hdr(bs, &logs->log, &hdr_tmp); |
519 | 0 | if (ret < 0) { |
520 | 0 | goto exit; |
521 | 0 | } |
522 | 0 | file_length = bdrv_getlength(bs->file->bs); |
523 | 0 | if (file_length < 0) { |
524 | 0 | ret = file_length; |
525 | 0 | goto exit; |
526 | 0 | } |
527 | | /* if the log shows a FlushedFileOffset larger than our current file |
528 | | * size, then that means the file has been truncated / corrupted, and |
529 | | * we must refused to open it / use it */ |
530 | 0 | if (hdr_tmp.flushed_file_offset > file_length) { |
531 | 0 | ret = -EINVAL; |
532 | 0 | goto exit; |
533 | 0 | } |
534 | | |
535 | 0 | ret = vhdx_log_read_desc(bs, s, &logs->log, &desc_entries, true); |
536 | 0 | if (ret < 0) { |
537 | 0 | goto exit; |
538 | 0 | } |
539 | | |
540 | 0 | for (i = 0; i < desc_entries->hdr.descriptor_count; i++) { |
541 | 0 | if (desc_entries->desc[i].signature == VHDX_LOG_DESC_SIGNATURE) { |
542 | | /* data sector, so read a sector to flush */ |
543 | 0 | ret = vhdx_log_read_sectors(bs, &logs->log, §ors_read, |
544 | 0 | data, 1, false); |
545 | 0 | if (ret < 0) { |
546 | 0 | goto exit; |
547 | 0 | } |
548 | 0 | if (sectors_read != 1) { |
549 | 0 | ret = -EINVAL; |
550 | 0 | goto exit; |
551 | 0 | } |
552 | 0 | vhdx_log_data_le_import(data); |
553 | 0 | } |
554 | | |
555 | 0 | ret = vhdx_log_flush_desc(bs, &desc_entries->desc[i], data); |
556 | 0 | if (ret < 0) { |
557 | 0 | goto exit; |
558 | 0 | } |
559 | 0 | } |
560 | 0 | if (file_length < desc_entries->hdr.last_file_offset) { |
561 | 0 | new_file_size = desc_entries->hdr.last_file_offset; |
562 | 0 | if (new_file_size % (1 * MiB)) { |
563 | | /* round up to nearest 1MB boundary */ |
564 | 0 | new_file_size = QEMU_ALIGN_UP(new_file_size, MiB); |
565 | 0 | if (new_file_size > INT64_MAX) { |
566 | 0 | ret = -EINVAL; |
567 | 0 | goto exit; |
568 | 0 | } |
569 | 0 | ret = bdrv_truncate(bs->file, new_file_size, false, |
570 | 0 | PREALLOC_MODE_OFF, 0, NULL); |
571 | 0 | if (ret < 0) { |
572 | 0 | goto exit; |
573 | 0 | } |
574 | 0 | } |
575 | 0 | } |
576 | 0 | qemu_vfree(desc_entries); |
577 | 0 | desc_entries = NULL; |
578 | 0 | } |
579 | | |
580 | 0 | ret = bdrv_flush(bs); |
581 | 0 | if (ret < 0) { |
582 | 0 | goto exit; |
583 | 0 | } |
584 | | /* once the log is fully flushed, indicate that we have an empty log |
585 | | * now. This also sets the log guid to 0, to indicate an empty log */ |
586 | 0 | vhdx_log_reset(bs, s); |
587 | |
|
588 | 0 | exit: |
589 | 0 | qemu_vfree(data); |
590 | 0 | qemu_vfree(desc_entries); |
591 | 0 | return ret; |
592 | 0 | } |
593 | | |
594 | | static int GRAPH_RDLOCK |
595 | | vhdx_validate_log_entry(BlockDriverState *bs, BDRVVHDXState *s, |
596 | | VHDXLogEntries *log, uint64_t seq, |
597 | | bool *valid, VHDXLogEntryHeader *entry) |
598 | 0 | { |
599 | 0 | int ret = 0; |
600 | 0 | VHDXLogEntryHeader hdr; |
601 | 0 | void *buffer = NULL; |
602 | 0 | uint32_t i, desc_sectors, total_sectors, crc; |
603 | 0 | uint32_t sectors_read = 0; |
604 | 0 | VHDXLogDescEntries *desc_buffer = NULL; |
605 | |
|
606 | 0 | *valid = false; |
607 | |
|
608 | 0 | ret = vhdx_log_peek_hdr(bs, log, &hdr); |
609 | 0 | if (ret < 0) { |
610 | 0 | goto inc_and_exit; |
611 | 0 | } |
612 | | |
613 | 0 | if (vhdx_log_hdr_is_valid(log, &hdr, s) == false) { |
614 | 0 | goto inc_and_exit; |
615 | 0 | } |
616 | | |
617 | 0 | if (seq > 0) { |
618 | 0 | if (hdr.sequence_number != seq + 1) { |
619 | 0 | goto inc_and_exit; |
620 | 0 | } |
621 | 0 | } |
622 | | |
623 | 0 | desc_sectors = vhdx_compute_desc_sectors(hdr.descriptor_count); |
624 | | |
625 | | /* Read all log sectors, and calculate log checksum */ |
626 | |
|
627 | 0 | total_sectors = hdr.entry_length / VHDX_LOG_SECTOR_SIZE; |
628 | | |
629 | | |
630 | | /* read_desc() will increment the read idx */ |
631 | 0 | ret = vhdx_log_read_desc(bs, s, log, &desc_buffer, false); |
632 | 0 | if (ret < 0) { |
633 | 0 | goto free_and_exit; |
634 | 0 | } |
635 | | |
636 | 0 | crc = vhdx_checksum_calc(0xffffffff, (void *)desc_buffer, |
637 | 0 | desc_sectors * VHDX_LOG_SECTOR_SIZE, 4); |
638 | 0 | crc ^= 0xffffffff; |
639 | |
|
640 | 0 | buffer = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE); |
641 | 0 | if (total_sectors > desc_sectors) { |
642 | 0 | for (i = 0; i < total_sectors - desc_sectors; i++) { |
643 | 0 | sectors_read = 0; |
644 | 0 | ret = vhdx_log_read_sectors(bs, log, §ors_read, buffer, |
645 | 0 | 1, false); |
646 | 0 | if (ret < 0 || sectors_read != 1) { |
647 | 0 | goto free_and_exit; |
648 | 0 | } |
649 | 0 | crc = vhdx_checksum_calc(crc, buffer, VHDX_LOG_SECTOR_SIZE, -1); |
650 | 0 | crc ^= 0xffffffff; |
651 | 0 | } |
652 | 0 | } |
653 | 0 | crc ^= 0xffffffff; |
654 | 0 | if (crc != hdr.checksum) { |
655 | 0 | goto free_and_exit; |
656 | 0 | } |
657 | | |
658 | 0 | *valid = true; |
659 | 0 | *entry = hdr; |
660 | 0 | goto free_and_exit; |
661 | | |
662 | 0 | inc_and_exit: |
663 | 0 | log->read = vhdx_log_inc_idx(log->read, log->length); |
664 | |
|
665 | 0 | free_and_exit: |
666 | 0 | qemu_vfree(buffer); |
667 | 0 | qemu_vfree(desc_buffer); |
668 | 0 | return ret; |
669 | 0 | } |
670 | | |
671 | | /* Search through the log circular buffer, and find the valid, active |
672 | | * log sequence, if any exists |
673 | | * */ |
674 | | static int GRAPH_RDLOCK |
675 | | vhdx_log_search(BlockDriverState *bs, BDRVVHDXState *s, VHDXLogSequence *logs) |
676 | 0 | { |
677 | 0 | int ret = 0; |
678 | 0 | uint32_t tail; |
679 | 0 | bool seq_valid = false; |
680 | 0 | VHDXLogSequence candidate = { 0 }; |
681 | 0 | VHDXLogEntryHeader hdr = { 0 }; |
682 | 0 | VHDXLogEntries curr_log; |
683 | |
|
684 | 0 | memcpy(&curr_log, &s->log, sizeof(VHDXLogEntries)); |
685 | 0 | curr_log.write = curr_log.length; /* assume log is full */ |
686 | 0 | curr_log.read = 0; |
687 | | |
688 | | |
689 | | /* now we will go through the whole log sector by sector, until |
690 | | * we find a valid, active log sequence, or reach the end of the |
691 | | * log buffer */ |
692 | 0 | for (;;) { |
693 | 0 | uint64_t curr_seq = 0; |
694 | 0 | VHDXLogSequence current = { 0 }; |
695 | |
|
696 | 0 | tail = curr_log.read; |
697 | |
|
698 | 0 | ret = vhdx_validate_log_entry(bs, s, &curr_log, curr_seq, |
699 | 0 | &seq_valid, &hdr); |
700 | 0 | if (ret < 0) { |
701 | 0 | goto exit; |
702 | 0 | } |
703 | | |
704 | 0 | if (seq_valid) { |
705 | 0 | current.valid = true; |
706 | 0 | current.log = curr_log; |
707 | 0 | current.log.read = tail; |
708 | 0 | current.log.write = curr_log.read; |
709 | 0 | current.count = 1; |
710 | 0 | current.hdr = hdr; |
711 | | |
712 | |
|
713 | 0 | for (;;) { |
714 | 0 | ret = vhdx_validate_log_entry(bs, s, &curr_log, curr_seq, |
715 | 0 | &seq_valid, &hdr); |
716 | 0 | if (ret < 0) { |
717 | 0 | goto exit; |
718 | 0 | } |
719 | 0 | if (seq_valid == false) { |
720 | 0 | break; |
721 | 0 | } |
722 | 0 | current.log.write = curr_log.read; |
723 | 0 | current.count++; |
724 | |
|
725 | 0 | curr_seq = hdr.sequence_number; |
726 | 0 | } |
727 | 0 | } |
728 | | |
729 | 0 | if (current.valid) { |
730 | 0 | if (candidate.valid == false || |
731 | 0 | current.hdr.sequence_number > candidate.hdr.sequence_number) { |
732 | 0 | candidate = current; |
733 | 0 | } |
734 | 0 | } |
735 | |
|
736 | 0 | if (curr_log.read < tail) { |
737 | 0 | break; |
738 | 0 | } |
739 | 0 | } |
740 | | |
741 | 0 | *logs = candidate; |
742 | |
|
743 | 0 | if (candidate.valid) { |
744 | | /* this is the next sequence number, for writes */ |
745 | 0 | s->log.sequence = candidate.hdr.sequence_number + 1; |
746 | 0 | } |
747 | | |
748 | |
|
749 | 0 | exit: |
750 | 0 | return ret; |
751 | 0 | } |
752 | | |
753 | | /* Parse the replay log. Per the VHDX spec, if the log is present |
754 | | * it must be replayed prior to opening the file, even read-only. |
755 | | * |
756 | | * If read-only, we must replay the log in RAM (or refuse to open |
757 | | * a dirty VHDX file read-only) */ |
758 | | int vhdx_parse_log(BlockDriverState *bs, BDRVVHDXState *s, bool *flushed, |
759 | | Error **errp) |
760 | 0 | { |
761 | 0 | int ret = 0; |
762 | 0 | VHDXHeader *hdr; |
763 | 0 | VHDXLogSequence logs = { 0 }; |
764 | |
|
765 | 0 | hdr = s->headers[s->curr_header]; |
766 | |
|
767 | 0 | *flushed = false; |
768 | | |
769 | | /* s->log.hdr is freed in vhdx_close() */ |
770 | 0 | if (s->log.hdr == NULL) { |
771 | 0 | s->log.hdr = qemu_blockalign(bs, sizeof(VHDXLogEntryHeader)); |
772 | 0 | } |
773 | |
|
774 | 0 | s->log.offset = hdr->log_offset; |
775 | 0 | s->log.length = hdr->log_length; |
776 | |
|
777 | 0 | if (s->log.offset < VHDX_LOG_MIN_SIZE || |
778 | 0 | s->log.offset % VHDX_LOG_MIN_SIZE) { |
779 | 0 | ret = -EINVAL; |
780 | 0 | goto exit; |
781 | 0 | } |
782 | | |
783 | | /* per spec, only log version of 0 is supported */ |
784 | 0 | if (hdr->log_version != 0) { |
785 | 0 | ret = -EINVAL; |
786 | 0 | goto exit; |
787 | 0 | } |
788 | | |
789 | | /* If either the log guid, or log length is zero, |
790 | | * then a replay log is not present */ |
791 | 0 | if (guid_eq(hdr->log_guid, zero_guid)) { |
792 | 0 | goto exit; |
793 | 0 | } |
794 | | |
795 | 0 | if (hdr->log_length == 0) { |
796 | 0 | goto exit; |
797 | 0 | } |
798 | | |
799 | 0 | if (hdr->log_length % VHDX_LOG_MIN_SIZE) { |
800 | 0 | ret = -EINVAL; |
801 | 0 | goto exit; |
802 | 0 | } |
803 | | |
804 | | |
805 | | /* The log is present, we need to find if and where there is an active |
806 | | * sequence of valid entries present in the log. */ |
807 | | |
808 | 0 | ret = vhdx_log_search(bs, s, &logs); |
809 | 0 | if (ret < 0) { |
810 | 0 | goto exit; |
811 | 0 | } |
812 | | |
813 | 0 | if (logs.valid) { |
814 | 0 | if (bdrv_is_read_only(bs)) { |
815 | 0 | bdrv_refresh_filename(bs); |
816 | 0 | ret = -EPERM; |
817 | 0 | error_setg(errp, |
818 | 0 | "VHDX image file '%s' opened read-only, but " |
819 | 0 | "contains a log that needs to be replayed", |
820 | 0 | bs->filename); |
821 | 0 | error_append_hint(errp, "To replay the log, run:\n" |
822 | 0 | "qemu-img check -r all '%s'\n", |
823 | 0 | bs->filename); |
824 | 0 | goto exit; |
825 | 0 | } |
826 | | /* now flush the log */ |
827 | 0 | ret = vhdx_log_flush(bs, s, &logs); |
828 | 0 | if (ret < 0) { |
829 | 0 | goto exit; |
830 | 0 | } |
831 | 0 | *flushed = true; |
832 | 0 | } |
833 | | |
834 | | |
835 | 0 | exit: |
836 | 0 | return ret; |
837 | 0 | } |
838 | | |
839 | | |
840 | | |
841 | | static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor *desc, |
842 | | VHDXLogDataSector *sector, void *data, |
843 | | uint64_t seq) |
844 | 0 | { |
845 | | /* 8 + 4084 + 4 = 4096, 1 log sector */ |
846 | 0 | memcpy(&desc->leading_bytes, data, 8); |
847 | 0 | data += 8; |
848 | 0 | desc->leading_bytes = cpu_to_le64(desc->leading_bytes); |
849 | 0 | memcpy(sector->data, data, 4084); |
850 | 0 | data += 4084; |
851 | 0 | memcpy(&desc->trailing_bytes, data, 4); |
852 | 0 | desc->trailing_bytes = cpu_to_le32(desc->trailing_bytes); |
853 | 0 | data += 4; |
854 | |
|
855 | 0 | sector->sequence_high = (uint32_t) (seq >> 32); |
856 | 0 | sector->sequence_low = (uint32_t) (seq & 0xffffffff); |
857 | 0 | sector->data_signature = VHDX_LOG_DATA_SIGNATURE; |
858 | |
|
859 | 0 | vhdx_log_desc_le_export(desc); |
860 | 0 | vhdx_log_data_le_export(sector); |
861 | 0 | } |
862 | | |
863 | | |
864 | | static int coroutine_fn GRAPH_RDLOCK |
865 | | vhdx_log_write(BlockDriverState *bs, BDRVVHDXState *s, |
866 | | void *data, uint32_t length, uint64_t offset) |
867 | 0 | { |
868 | 0 | int ret = 0; |
869 | 0 | void *buffer = NULL; |
870 | 0 | void *merged_sector = NULL; |
871 | 0 | void *data_tmp, *sector_write; |
872 | 0 | unsigned int i; |
873 | 0 | int sector_offset; |
874 | 0 | uint32_t desc_sectors, sectors, total_length; |
875 | 0 | uint32_t sectors_written = 0; |
876 | 0 | uint32_t aligned_length; |
877 | 0 | uint32_t leading_length = 0; |
878 | 0 | uint32_t trailing_length = 0; |
879 | 0 | uint32_t partial_sectors = 0; |
880 | 0 | uint32_t bytes_written = 0; |
881 | 0 | uint64_t file_offset; |
882 | 0 | int64_t file_length; |
883 | 0 | VHDXHeader *header; |
884 | 0 | VHDXLogEntryHeader new_hdr; |
885 | 0 | VHDXLogDescriptor *new_desc = NULL; |
886 | 0 | VHDXLogDataSector *data_sector = NULL; |
887 | 0 | MSGUID new_guid = { 0 }; |
888 | |
|
889 | 0 | header = s->headers[s->curr_header]; |
890 | | |
891 | | /* need to have offset read data, and be on 4096 byte boundary */ |
892 | |
|
893 | 0 | if (length > header->log_length) { |
894 | | /* no log present. we could create a log here instead of failing */ |
895 | 0 | ret = -EINVAL; |
896 | 0 | goto exit; |
897 | 0 | } |
898 | | |
899 | 0 | if (guid_eq(header->log_guid, zero_guid)) { |
900 | 0 | vhdx_guid_generate(&new_guid); |
901 | 0 | vhdx_update_headers(bs, s, false, &new_guid); |
902 | 0 | } else { |
903 | | /* currently, we require that the log be flushed after |
904 | | * every write. */ |
905 | 0 | ret = -ENOTSUP; |
906 | 0 | goto exit; |
907 | 0 | } |
908 | | |
909 | | /* 0 is an invalid sequence number, but may also represent the first |
910 | | * log write (or a wrapped seq) */ |
911 | 0 | if (s->log.sequence == 0) { |
912 | 0 | s->log.sequence = 1; |
913 | 0 | } |
914 | |
|
915 | 0 | sector_offset = offset % VHDX_LOG_SECTOR_SIZE; |
916 | 0 | file_offset = QEMU_ALIGN_DOWN(offset, VHDX_LOG_SECTOR_SIZE); |
917 | |
|
918 | 0 | aligned_length = length; |
919 | | |
920 | | /* add in the unaligned head and tail bytes */ |
921 | 0 | if (sector_offset) { |
922 | 0 | leading_length = (VHDX_LOG_SECTOR_SIZE - sector_offset); |
923 | 0 | leading_length = leading_length > length ? length : leading_length; |
924 | 0 | aligned_length -= leading_length; |
925 | 0 | partial_sectors++; |
926 | 0 | } |
927 | |
|
928 | 0 | sectors = aligned_length / VHDX_LOG_SECTOR_SIZE; |
929 | 0 | trailing_length = aligned_length - (sectors * VHDX_LOG_SECTOR_SIZE); |
930 | 0 | if (trailing_length) { |
931 | 0 | partial_sectors++; |
932 | 0 | } |
933 | |
|
934 | 0 | sectors += partial_sectors; |
935 | |
|
936 | 0 | file_length = bdrv_co_getlength(bs->file->bs); |
937 | 0 | if (file_length < 0) { |
938 | 0 | ret = file_length; |
939 | 0 | goto exit; |
940 | 0 | } |
941 | | |
942 | | /* sectors is now how many sectors the data itself takes, not |
943 | | * including the header and descriptor metadata */ |
944 | | |
945 | 0 | new_hdr = (VHDXLogEntryHeader) { |
946 | 0 | .signature = VHDX_LOG_SIGNATURE, |
947 | 0 | .tail = s->log.tail, |
948 | 0 | .sequence_number = s->log.sequence, |
949 | 0 | .descriptor_count = sectors, |
950 | 0 | .reserved = 0, |
951 | 0 | .flushed_file_offset = file_length, |
952 | 0 | .last_file_offset = file_length, |
953 | 0 | .log_guid = header->log_guid, |
954 | 0 | }; |
955 | | |
956 | |
|
957 | 0 | desc_sectors = vhdx_compute_desc_sectors(new_hdr.descriptor_count); |
958 | |
|
959 | 0 | total_length = (desc_sectors + sectors) * VHDX_LOG_SECTOR_SIZE; |
960 | 0 | new_hdr.entry_length = total_length; |
961 | |
|
962 | 0 | vhdx_log_entry_hdr_le_export(&new_hdr); |
963 | |
|
964 | 0 | buffer = qemu_blockalign(bs, total_length); |
965 | 0 | memcpy(buffer, &new_hdr, sizeof(new_hdr)); |
966 | |
|
967 | 0 | new_desc = buffer + sizeof(new_hdr); |
968 | 0 | data_sector = buffer + (desc_sectors * VHDX_LOG_SECTOR_SIZE); |
969 | 0 | data_tmp = data; |
970 | | |
971 | | /* All log sectors are 4KB, so for any partial sectors we must |
972 | | * merge the data with preexisting data from the final file |
973 | | * destination */ |
974 | 0 | merged_sector = qemu_blockalign(bs, VHDX_LOG_SECTOR_SIZE); |
975 | |
|
976 | 0 | for (i = 0; i < sectors; i++) { |
977 | 0 | new_desc->signature = VHDX_LOG_DESC_SIGNATURE; |
978 | 0 | new_desc->sequence_number = s->log.sequence; |
979 | 0 | new_desc->file_offset = file_offset; |
980 | |
|
981 | 0 | if (i == 0 && leading_length) { |
982 | | /* partial sector at the front of the buffer */ |
983 | 0 | ret = bdrv_co_pread(bs->file, file_offset, VHDX_LOG_SECTOR_SIZE, |
984 | 0 | merged_sector, 0); |
985 | 0 | if (ret < 0) { |
986 | 0 | goto exit; |
987 | 0 | } |
988 | 0 | memcpy(merged_sector + sector_offset, data_tmp, leading_length); |
989 | 0 | bytes_written = leading_length; |
990 | 0 | sector_write = merged_sector; |
991 | 0 | } else if (i == sectors - 1 && trailing_length) { |
992 | | /* partial sector at the end of the buffer */ |
993 | 0 | ret = bdrv_co_pread(bs->file, file_offset + trailing_length, |
994 | 0 | VHDX_LOG_SECTOR_SIZE - trailing_length, |
995 | 0 | merged_sector + trailing_length, 0); |
996 | 0 | if (ret < 0) { |
997 | 0 | goto exit; |
998 | 0 | } |
999 | 0 | memcpy(merged_sector, data_tmp, trailing_length); |
1000 | 0 | bytes_written = trailing_length; |
1001 | 0 | sector_write = merged_sector; |
1002 | 0 | } else { |
1003 | 0 | bytes_written = VHDX_LOG_SECTOR_SIZE; |
1004 | 0 | sector_write = data_tmp; |
1005 | 0 | } |
1006 | | |
1007 | | /* populate the raw sector data into the proper structures, |
1008 | | * as well as update the descriptor, and convert to proper |
1009 | | * endianness */ |
1010 | 0 | vhdx_log_raw_to_le_sector(new_desc, data_sector, sector_write, |
1011 | 0 | s->log.sequence); |
1012 | |
|
1013 | 0 | data_tmp += bytes_written; |
1014 | 0 | data_sector++; |
1015 | 0 | new_desc++; |
1016 | 0 | file_offset += VHDX_LOG_SECTOR_SIZE; |
1017 | 0 | } |
1018 | | |
1019 | | /* checksum covers entire entry, from the log header through the |
1020 | | * last data sector */ |
1021 | 0 | vhdx_update_checksum(buffer, total_length, |
1022 | 0 | offsetof(VHDXLogEntryHeader, checksum)); |
1023 | | |
1024 | | /* now write to the log */ |
1025 | 0 | ret = vhdx_log_write_sectors(bs, &s->log, §ors_written, buffer, |
1026 | 0 | desc_sectors + sectors); |
1027 | 0 | if (ret < 0) { |
1028 | 0 | goto exit; |
1029 | 0 | } |
1030 | | |
1031 | 0 | if (sectors_written != desc_sectors + sectors) { |
1032 | | /* instead of failing, we could flush the log here */ |
1033 | 0 | ret = -EINVAL; |
1034 | 0 | goto exit; |
1035 | 0 | } |
1036 | | |
1037 | 0 | s->log.sequence++; |
1038 | | /* write new tail */ |
1039 | 0 | s->log.tail = s->log.write; |
1040 | |
|
1041 | 0 | exit: |
1042 | 0 | qemu_vfree(buffer); |
1043 | 0 | qemu_vfree(merged_sector); |
1044 | 0 | return ret; |
1045 | 0 | } |
1046 | | |
1047 | | /* Perform a log write, and then immediately flush the entire log */ |
1048 | | int coroutine_fn |
1049 | | vhdx_log_write_and_flush(BlockDriverState *bs, BDRVVHDXState *s, |
1050 | | void *data, uint32_t length, uint64_t offset) |
1051 | 0 | { |
1052 | 0 | int ret = 0; |
1053 | 0 | VHDXLogSequence logs = { .valid = true, |
1054 | 0 | .count = 1, |
1055 | 0 | .hdr = { 0 } }; |
1056 | | |
1057 | | |
1058 | | /* Make sure data written (new and/or changed blocks) is stable |
1059 | | * on disk, before creating log entry */ |
1060 | 0 | ret = bdrv_co_flush(bs); |
1061 | 0 | if (ret < 0) { |
1062 | 0 | goto exit; |
1063 | 0 | } |
1064 | | |
1065 | 0 | ret = vhdx_log_write(bs, s, data, length, offset); |
1066 | 0 | if (ret < 0) { |
1067 | 0 | goto exit; |
1068 | 0 | } |
1069 | 0 | logs.log = s->log; |
1070 | | |
1071 | | /* Make sure log is stable on disk */ |
1072 | 0 | ret = bdrv_co_flush(bs); |
1073 | 0 | if (ret < 0) { |
1074 | 0 | goto exit; |
1075 | 0 | } |
1076 | | |
1077 | 0 | ret = vhdx_log_flush(bs, s, &logs); |
1078 | 0 | if (ret < 0) { |
1079 | 0 | goto exit; |
1080 | 0 | } |
1081 | | |
1082 | 0 | s->log = logs.log; |
1083 | |
|
1084 | 0 | exit: |
1085 | 0 | return ret; |
1086 | 0 | } |
1087 | | |