2 * Block driver for Hyper-V VHDX Images
4 * Copyright (c) 2013 Red Hat, Inc.,
7 * Jeff Cody <jcody@redhat.com>
9 * This is based on the "VHDX Format Specification v1.00", published 8/25/2012
11 * https://www.microsoft.com/en-us/download/details.aspx?id=34750
13 * This file covers the functionality of the metadata log writing, parsing, and
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.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "qemu-common.h"
23 #include "block/block_int.h"
24 #include "qemu/error-report.h"
25 #include "qemu/module.h"
26 #include "qemu/bswap.h"
27 #include "block/vhdx.h"
30 typedef struct VHDXLogSequence
{
34 VHDXLogEntryHeader hdr
;
37 typedef struct VHDXLogDescEntries
{
38 VHDXLogEntryHeader hdr
;
39 VHDXLogDescriptor desc
[];
42 static const MSGUID zero_guid
= { 0 };
44 /* The log located on the disk is circular buffer containing
45 * sectors of 4096 bytes each.
47 * It is assumed for the read/write functions below that the
48 * circular buffer scheme uses a 'one sector open' to indicate
49 * the buffer is full. Given the validation methods used for each
50 * sector, this method should be compatible with other methods that
51 * do not waste a sector.
55 /* Allow peeking at the hdr entry at the beginning of the current
56 * read index, without advancing the read index */
57 static int vhdx_log_peek_hdr(BlockDriverState
*bs
, VHDXLogEntries
*log
,
58 VHDXLogEntryHeader
*hdr
)
66 /* peek is only supported on sector boundaries */
67 if (log
->read
% VHDX_LOG_SECTOR_SIZE
) {
73 /* we are guaranteed that a) log sectors are 4096 bytes,
74 * and b) the log length is a multiple of 1MB. So, there
75 * is always a round number of sectors in the buffer */
76 if ((read
+ sizeof(VHDXLogEntryHeader
)) > log
->length
) {
80 if (read
== log
->write
) {
85 offset
= log
->offset
+ read
;
87 ret
= bdrv_pread(bs
->file
, offset
, hdr
, sizeof(VHDXLogEntryHeader
));
91 vhdx_log_entry_hdr_le_import(hdr
);
97 /* Index increment for log, based on sector boundaries */
98 static int vhdx_log_inc_idx(uint32_t idx
, uint64_t length
)
100 idx
+= VHDX_LOG_SECTOR_SIZE
;
101 /* we are guaranteed that a) log sectors are 4096 bytes,
102 * and b) the log length is a multiple of 1MB. So, there
103 * is always a round number of sectors in the buffer */
104 return idx
>= length
? 0 : idx
;
108 /* Reset the log to empty */
109 static void vhdx_log_reset(BlockDriverState
*bs
, BDRVVHDXState
*s
)
112 s
->log
.read
= s
->log
.write
= 0;
113 /* a log guid of 0 indicates an empty log to any parser of v0
115 vhdx_update_headers(bs
, s
, false, &guid
);
118 /* Reads num_sectors from the log (all log sectors are 4096 bytes),
119 * into buffer 'buffer'. Upon return, *sectors_read will contain
120 * the number of sectors successfully read.
122 * It is assumed that 'buffer' is already allocated, and of sufficient
123 * size (i.e. >= 4096*num_sectors).
125 * If 'peek' is true, then the tail (read) pointer for the circular buffer is
128 * 0 is returned on success, -errno otherwise. */
129 static int vhdx_log_read_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
130 uint32_t *sectors_read
, void *buffer
,
131 uint32_t num_sectors
, bool peek
)
140 while (num_sectors
) {
141 if (read
== log
->write
) {
145 offset
= log
->offset
+ read
;
147 ret
= bdrv_pread(bs
->file
, offset
, buffer
, VHDX_LOG_SECTOR_SIZE
);
151 read
= vhdx_log_inc_idx(read
, log
->length
);
153 *sectors_read
= *sectors_read
+ 1;
164 /* Writes num_sectors to the log (all log sectors are 4096 bytes),
165 * from buffer 'buffer'. Upon return, *sectors_written will contain
166 * the number of sectors successfully written.
168 * It is assumed that 'buffer' is at least 4096*num_sectors large.
170 * 0 is returned on success, -errno otherwise */
171 static int vhdx_log_write_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
172 uint32_t *sectors_written
, void *buffer
,
173 uint32_t num_sectors
)
179 BDRVVHDXState
*s
= bs
->opaque
;
181 ret
= vhdx_user_visible_write(bs
, s
);
189 while (num_sectors
) {
191 offset
= log
->offset
+ write
;
192 write
= vhdx_log_inc_idx(write
, log
->length
);
193 if (write
== log
->read
) {
197 ret
= bdrv_pwrite(bs
->file
, offset
, buffer_tmp
,
198 VHDX_LOG_SECTOR_SIZE
);
202 buffer_tmp
+= VHDX_LOG_SECTOR_SIZE
;
205 *sectors_written
= *sectors_written
+ 1;
214 /* Validates a log entry header */
215 static bool vhdx_log_hdr_is_valid(VHDXLogEntries
*log
, VHDXLogEntryHeader
*hdr
,
220 if (hdr
->signature
!= VHDX_LOG_SIGNATURE
) {
224 /* if the individual entry length is larger than the whole log
225 * buffer, that is obviously invalid */
226 if (log
->length
< hdr
->entry_length
) {
230 /* length of entire entry must be in units of 4KB (log sector size) */
231 if (hdr
->entry_length
% (VHDX_LOG_SECTOR_SIZE
)) {
235 /* per spec, sequence # must be > 0 */
236 if (hdr
->sequence_number
== 0) {
240 /* log entries are only valid if they match the file-wide log guid
241 * found in the active header */
242 if (!guid_eq(hdr
->log_guid
, s
->headers
[s
->curr_header
]->log_guid
)) {
246 if (hdr
->descriptor_count
* sizeof(VHDXLogDescriptor
) > hdr
->entry_length
) {
257 * Given a log header, this will validate that the descriptors and the
258 * corresponding data sectors (if applicable)
260 * Validation consists of:
261 * 1. Making sure the sequence numbers matches the entry header
262 * 2. Verifying a valid signature ('zero' or 'desc' for descriptors)
263 * 3. File offset field is a multiple of 4KB
264 * 4. If a data descriptor, the corresponding data sector
265 * has its signature ('data') and matching sequence number
267 * @desc: the data buffer containing the descriptor
268 * @hdr: the log entry header
270 * Returns true if valid
272 static bool vhdx_log_desc_is_valid(VHDXLogDescriptor
*desc
,
273 VHDXLogEntryHeader
*hdr
)
277 if (desc
->sequence_number
!= hdr
->sequence_number
) {
280 if (desc
->file_offset
% VHDX_LOG_SECTOR_SIZE
) {
284 if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
285 if (desc
->zero_length
% VHDX_LOG_SECTOR_SIZE
== 0) {
289 } else if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
299 /* Prior to sector data for a log entry, there is the header
300 * and the descriptors referenced in the header:
304 * [ hdr, desc ][ desc ][ ... ][ data ][ ... ]
306 * The first sector in a log entry has a 64 byte header, and
307 * up to 126 32-byte descriptors. If more descriptors than
308 * 126 are required, then subsequent sectors can have up to 128
309 * descriptors. Each sector is 4KB. Data follows the descriptor
312 * This will return the number of sectors needed to encompass
313 * the passed number of descriptors in desc_cnt.
315 * This will never return 0, even if desc_cnt is 0.
317 static int vhdx_compute_desc_sectors(uint32_t desc_cnt
)
319 uint32_t desc_sectors
;
321 desc_cnt
+= 2; /* account for header in first sector */
322 desc_sectors
= desc_cnt
/ 128;
323 if (desc_cnt
% 128) {
331 /* Reads the log header, and subsequent descriptors (if any). This
332 * will allocate all the space for buffer, which must be NULL when
333 * passed into this function. Each descriptor will also be validated,
334 * and error returned if any are invalid. */
335 static int vhdx_log_read_desc(BlockDriverState
*bs
, BDRVVHDXState
*s
,
336 VHDXLogEntries
*log
, VHDXLogDescEntries
**buffer
,
340 uint32_t desc_sectors
;
341 uint32_t sectors_read
;
342 VHDXLogEntryHeader hdr
;
343 VHDXLogDescEntries
*desc_entries
= NULL
;
344 VHDXLogDescriptor desc
;
347 assert(*buffer
== NULL
);
349 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
354 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
359 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
360 desc_entries
= qemu_try_blockalign(bs
->file
->bs
,
361 desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
362 if (desc_entries
== NULL
) {
367 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, desc_entries
,
368 desc_sectors
, false);
372 if (sectors_read
!= desc_sectors
) {
377 /* put in proper endianness, and validate each desc */
378 for (i
= 0; i
< hdr
.descriptor_count
; i
++) {
379 desc
= desc_entries
->desc
[i
];
380 vhdx_log_desc_le_import(&desc
);
381 if (convert_endian
) {
382 desc_entries
->desc
[i
] = desc
;
384 if (vhdx_log_desc_is_valid(&desc
, &hdr
) == false) {
389 if (convert_endian
) {
390 desc_entries
->hdr
= hdr
;
393 *buffer
= desc_entries
;
397 qemu_vfree(desc_entries
);
403 /* Flushes the descriptor described by desc to the VHDX image file.
404 * If the descriptor is a data descriptor, than 'data' must be non-NULL,
405 * and >= 4096 bytes (VHDX_LOG_SECTOR_SIZE), containing the data to be
408 * Verification is performed to make sure the sequence numbers of a data
409 * descriptor match the sequence number in the desc.
411 * For a zero descriptor, it may describe multiple sectors to fill with zeroes.
412 * In this case, it should be noted that zeroes are written to disk, and the
413 * image file is not extended as a sparse file. */
414 static int vhdx_log_flush_desc(BlockDriverState
*bs
, VHDXLogDescriptor
*desc
,
415 VHDXLogDataSector
*data
)
418 uint64_t seq
, file_offset
;
424 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
426 if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
433 /* The sequence number of the data sector must match that
434 * in the descriptor */
435 seq
= data
->sequence_high
;
437 seq
|= data
->sequence_low
& 0xffffffff;
439 if (seq
!= desc
->sequence_number
) {
444 /* Each data sector is in total 4096 bytes, however the first
445 * 8 bytes, and last 4 bytes, are located in the descriptor */
446 memcpy(buffer
, &desc
->leading_bytes
, 8);
449 memcpy(buffer
+offset
, data
->data
, 4084);
452 memcpy(buffer
+offset
, &desc
->trailing_bytes
, 4);
454 } else if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
455 /* write 'count' sectors of sector */
456 memset(buffer
, 0, VHDX_LOG_SECTOR_SIZE
);
457 count
= desc
->zero_length
/ VHDX_LOG_SECTOR_SIZE
;
459 error_report("Invalid VHDX log descriptor entry signature 0x%" PRIx32
,
465 file_offset
= desc
->file_offset
;
467 /* count is only > 1 if we are writing zeroes */
468 for (i
= 0; i
< count
; i
++) {
469 ret
= bdrv_pwrite_sync(bs
->file
, file_offset
, buffer
,
470 VHDX_LOG_SECTOR_SIZE
);
474 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
482 /* Flush the entire log (as described by 'logs') to the VHDX image
483 * file, and then set the log to 'empty' status once complete.
485 * The log entries should be validate prior to flushing */
486 static int vhdx_log_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
487 VHDXLogSequence
*logs
)
491 uint32_t cnt
, sectors_read
;
492 uint64_t new_file_size
;
495 VHDXLogDescEntries
*desc_entries
= NULL
;
496 VHDXLogEntryHeader hdr_tmp
= { 0 };
500 data
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
502 ret
= vhdx_user_visible_write(bs
, s
);
507 /* each iteration represents one log sequence, which may span multiple
510 ret
= vhdx_log_peek_hdr(bs
, &logs
->log
, &hdr_tmp
);
514 file_length
= bdrv_getlength(bs
->file
->bs
);
515 if (file_length
< 0) {
519 /* if the log shows a FlushedFileOffset larger than our current file
520 * size, then that means the file has been truncated / corrupted, and
521 * we must refused to open it / use it */
522 if (hdr_tmp
.flushed_file_offset
> file_length
) {
527 ret
= vhdx_log_read_desc(bs
, s
, &logs
->log
, &desc_entries
, true);
532 for (i
= 0; i
< desc_entries
->hdr
.descriptor_count
; i
++) {
533 if (desc_entries
->desc
[i
].signature
== VHDX_LOG_DESC_SIGNATURE
) {
534 /* data sector, so read a sector to flush */
535 ret
= vhdx_log_read_sectors(bs
, &logs
->log
, §ors_read
,
540 if (sectors_read
!= 1) {
544 vhdx_log_data_le_import(data
);
547 ret
= vhdx_log_flush_desc(bs
, &desc_entries
->desc
[i
], data
);
552 if (file_length
< desc_entries
->hdr
.last_file_offset
) {
553 new_file_size
= desc_entries
->hdr
.last_file_offset
;
554 if (new_file_size
% (1024*1024)) {
555 /* round up to nearest 1MB boundary */
556 new_file_size
= QEMU_ALIGN_UP(new_file_size
, MiB
);
557 if (new_file_size
> INT64_MAX
) {
561 ret
= bdrv_truncate(bs
->file
, new_file_size
, PREALLOC_MODE_OFF
,
568 qemu_vfree(desc_entries
);
572 ret
= bdrv_flush(bs
);
576 /* once the log is fully flushed, indicate that we have an empty log
577 * now. This also sets the log guid to 0, to indicate an empty log */
578 vhdx_log_reset(bs
, s
);
582 qemu_vfree(desc_entries
);
586 static int vhdx_validate_log_entry(BlockDriverState
*bs
, BDRVVHDXState
*s
,
587 VHDXLogEntries
*log
, uint64_t seq
,
588 bool *valid
, VHDXLogEntryHeader
*entry
)
591 VHDXLogEntryHeader hdr
;
593 uint32_t i
, desc_sectors
, total_sectors
, crc
;
594 uint32_t sectors_read
= 0;
595 VHDXLogDescEntries
*desc_buffer
= NULL
;
599 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
604 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
609 if (hdr
.sequence_number
!= seq
+ 1) {
614 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
616 /* Read all log sectors, and calculate log checksum */
618 total_sectors
= hdr
.entry_length
/ VHDX_LOG_SECTOR_SIZE
;
621 /* read_desc() will increment the read idx */
622 ret
= vhdx_log_read_desc(bs
, s
, log
, &desc_buffer
, false);
627 crc
= vhdx_checksum_calc(0xffffffff, (void *)desc_buffer
,
628 desc_sectors
* VHDX_LOG_SECTOR_SIZE
, 4);
631 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
632 if (total_sectors
> desc_sectors
) {
633 for (i
= 0; i
< total_sectors
- desc_sectors
; i
++) {
635 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, buffer
,
637 if (ret
< 0 || sectors_read
!= 1) {
640 crc
= vhdx_checksum_calc(crc
, buffer
, VHDX_LOG_SECTOR_SIZE
, -1);
645 if (crc
!= hdr
.checksum
) {
654 log
->read
= vhdx_log_inc_idx(log
->read
, log
->length
);
658 qemu_vfree(desc_buffer
);
662 /* Search through the log circular buffer, and find the valid, active
663 * log sequence, if any exists
665 static int vhdx_log_search(BlockDriverState
*bs
, BDRVVHDXState
*s
,
666 VHDXLogSequence
*logs
)
670 bool seq_valid
= false;
671 VHDXLogSequence candidate
= { 0 };
672 VHDXLogEntryHeader hdr
= { 0 };
673 VHDXLogEntries curr_log
;
675 memcpy(&curr_log
, &s
->log
, sizeof(VHDXLogEntries
));
676 curr_log
.write
= curr_log
.length
; /* assume log is full */
680 /* now we will go through the whole log sector by sector, until
681 * we find a valid, active log sequence, or reach the end of the
684 uint64_t curr_seq
= 0;
685 VHDXLogSequence current
= { 0 };
687 tail
= curr_log
.read
;
689 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
696 current
.valid
= true;
697 current
.log
= curr_log
;
698 current
.log
.read
= tail
;
699 current
.log
.write
= curr_log
.read
;
705 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
710 if (seq_valid
== false) {
713 current
.log
.write
= curr_log
.read
;
716 curr_seq
= hdr
.sequence_number
;
721 if (candidate
.valid
== false ||
722 current
.hdr
.sequence_number
> candidate
.hdr
.sequence_number
) {
727 if (curr_log
.read
< tail
) {
734 if (candidate
.valid
) {
735 /* this is the next sequence number, for writes */
736 s
->log
.sequence
= candidate
.hdr
.sequence_number
+ 1;
744 /* Parse the replay log. Per the VHDX spec, if the log is present
745 * it must be replayed prior to opening the file, even read-only.
747 * If read-only, we must replay the log in RAM (or refuse to open
748 * a dirty VHDX file read-only) */
749 int vhdx_parse_log(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool *flushed
,
754 VHDXLogSequence logs
= { 0 };
756 hdr
= s
->headers
[s
->curr_header
];
760 /* s->log.hdr is freed in vhdx_close() */
761 if (s
->log
.hdr
== NULL
) {
762 s
->log
.hdr
= qemu_blockalign(bs
, sizeof(VHDXLogEntryHeader
));
765 s
->log
.offset
= hdr
->log_offset
;
766 s
->log
.length
= hdr
->log_length
;
768 if (s
->log
.offset
< VHDX_LOG_MIN_SIZE
||
769 s
->log
.offset
% VHDX_LOG_MIN_SIZE
) {
774 /* per spec, only log version of 0 is supported */
775 if (hdr
->log_version
!= 0) {
780 /* If either the log guid, or log length is zero,
781 * then a replay log is not present */
782 if (guid_eq(hdr
->log_guid
, zero_guid
)) {
786 if (hdr
->log_length
== 0) {
790 if (hdr
->log_length
% VHDX_LOG_MIN_SIZE
) {
796 /* The log is present, we need to find if and where there is an active
797 * sequence of valid entries present in the log. */
799 ret
= vhdx_log_search(bs
, s
, &logs
);
808 "VHDX image file '%s' opened read-only, but "
809 "contains a log that needs to be replayed",
811 error_append_hint(errp
, "To replay the log, run:\n"
812 "qemu-img check -r all '%s'\n",
816 /* now flush the log */
817 ret
= vhdx_log_flush(bs
, s
, &logs
);
831 static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor
*desc
,
832 VHDXLogDataSector
*sector
, void *data
,
835 /* 8 + 4084 + 4 = 4096, 1 log sector */
836 memcpy(&desc
->leading_bytes
, data
, 8);
838 cpu_to_le64s(&desc
->leading_bytes
);
839 memcpy(sector
->data
, data
, 4084);
841 memcpy(&desc
->trailing_bytes
, data
, 4);
842 cpu_to_le32s(&desc
->trailing_bytes
);
845 sector
->sequence_high
= (uint32_t) (seq
>> 32);
846 sector
->sequence_low
= (uint32_t) (seq
& 0xffffffff);
847 sector
->data_signature
= VHDX_LOG_DATA_SIGNATURE
;
849 vhdx_log_desc_le_export(desc
);
850 vhdx_log_data_le_export(sector
);
854 static int vhdx_log_write(BlockDriverState
*bs
, BDRVVHDXState
*s
,
855 void *data
, uint32_t length
, uint64_t offset
)
859 void *merged_sector
= NULL
;
860 void *data_tmp
, *sector_write
;
863 uint32_t desc_sectors
, sectors
, total_length
;
864 uint32_t sectors_written
= 0;
865 uint32_t aligned_length
;
866 uint32_t leading_length
= 0;
867 uint32_t trailing_length
= 0;
868 uint32_t partial_sectors
= 0;
869 uint32_t bytes_written
= 0;
870 uint64_t file_offset
;
873 VHDXLogEntryHeader new_hdr
;
874 VHDXLogDescriptor
*new_desc
= NULL
;
875 VHDXLogDataSector
*data_sector
= NULL
;
876 MSGUID new_guid
= { 0 };
878 header
= s
->headers
[s
->curr_header
];
880 /* need to have offset read data, and be on 4096 byte boundary */
882 if (length
> header
->log_length
) {
883 /* no log present. we could create a log here instead of failing */
888 if (guid_eq(header
->log_guid
, zero_guid
)) {
889 vhdx_guid_generate(&new_guid
);
890 vhdx_update_headers(bs
, s
, false, &new_guid
);
892 /* currently, we require that the log be flushed after
898 /* 0 is an invalid sequence number, but may also represent the first
899 * log write (or a wrapped seq) */
900 if (s
->log
.sequence
== 0) {
904 sector_offset
= offset
% VHDX_LOG_SECTOR_SIZE
;
905 file_offset
= (offset
/ VHDX_LOG_SECTOR_SIZE
) * VHDX_LOG_SECTOR_SIZE
;
907 aligned_length
= length
;
909 /* add in the unaligned head and tail bytes */
911 leading_length
= (VHDX_LOG_SECTOR_SIZE
- sector_offset
);
912 leading_length
= leading_length
> length
? length
: leading_length
;
913 aligned_length
-= leading_length
;
917 sectors
= aligned_length
/ VHDX_LOG_SECTOR_SIZE
;
918 trailing_length
= aligned_length
- (sectors
* VHDX_LOG_SECTOR_SIZE
);
919 if (trailing_length
) {
923 sectors
+= partial_sectors
;
925 file_length
= bdrv_getlength(bs
->file
->bs
);
926 if (file_length
< 0) {
931 /* sectors is now how many sectors the data itself takes, not
932 * including the header and descriptor metadata */
934 new_hdr
= (VHDXLogEntryHeader
) {
935 .signature
= VHDX_LOG_SIGNATURE
,
937 .sequence_number
= s
->log
.sequence
,
938 .descriptor_count
= sectors
,
940 .flushed_file_offset
= file_length
,
941 .last_file_offset
= file_length
,
942 .log_guid
= header
->log_guid
,
946 desc_sectors
= vhdx_compute_desc_sectors(new_hdr
.descriptor_count
);
948 total_length
= (desc_sectors
+ sectors
) * VHDX_LOG_SECTOR_SIZE
;
949 new_hdr
.entry_length
= total_length
;
951 vhdx_log_entry_hdr_le_export(&new_hdr
);
953 buffer
= qemu_blockalign(bs
, total_length
);
954 memcpy(buffer
, &new_hdr
, sizeof(new_hdr
));
956 new_desc
= buffer
+ sizeof(new_hdr
);
957 data_sector
= buffer
+ (desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
960 /* All log sectors are 4KB, so for any partial sectors we must
961 * merge the data with preexisting data from the final file
963 merged_sector
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
965 for (i
= 0; i
< sectors
; i
++) {
966 new_desc
->signature
= VHDX_LOG_DESC_SIGNATURE
;
967 new_desc
->sequence_number
= s
->log
.sequence
;
968 new_desc
->file_offset
= file_offset
;
970 if (i
== 0 && leading_length
) {
971 /* partial sector at the front of the buffer */
972 ret
= bdrv_pread(bs
->file
, file_offset
, merged_sector
,
973 VHDX_LOG_SECTOR_SIZE
);
977 memcpy(merged_sector
+ sector_offset
, data_tmp
, leading_length
);
978 bytes_written
= leading_length
;
979 sector_write
= merged_sector
;
980 } else if (i
== sectors
- 1 && trailing_length
) {
981 /* partial sector at the end of the buffer */
982 ret
= bdrv_pread(bs
->file
,
984 merged_sector
+ trailing_length
,
985 VHDX_LOG_SECTOR_SIZE
- trailing_length
);
989 memcpy(merged_sector
, data_tmp
, trailing_length
);
990 bytes_written
= trailing_length
;
991 sector_write
= merged_sector
;
993 bytes_written
= VHDX_LOG_SECTOR_SIZE
;
994 sector_write
= data_tmp
;
997 /* populate the raw sector data into the proper structures,
998 * as well as update the descriptor, and convert to proper
1000 vhdx_log_raw_to_le_sector(new_desc
, data_sector
, sector_write
,
1003 data_tmp
+= bytes_written
;
1006 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
1009 /* checksum covers entire entry, from the log header through the
1010 * last data sector */
1011 vhdx_update_checksum(buffer
, total_length
,
1012 offsetof(VHDXLogEntryHeader
, checksum
));
1014 /* now write to the log */
1015 ret
= vhdx_log_write_sectors(bs
, &s
->log
, §ors_written
, buffer
,
1016 desc_sectors
+ sectors
);
1021 if (sectors_written
!= desc_sectors
+ sectors
) {
1022 /* instead of failing, we could flush the log here */
1028 /* write new tail */
1029 s
->log
.tail
= s
->log
.write
;
1033 qemu_vfree(merged_sector
);
1037 /* Perform a log write, and then immediately flush the entire log */
1038 int vhdx_log_write_and_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
1039 void *data
, uint32_t length
, uint64_t offset
)
1042 VHDXLogSequence logs
= { .valid
= true,
1047 /* Make sure data written (new and/or changed blocks) is stable
1048 * on disk, before creating log entry */
1049 ret
= bdrv_flush(bs
);
1054 ret
= vhdx_log_write(bs
, s
, data
, length
, offset
);
1060 /* Make sure log is stable on disk */
1061 ret
= bdrv_flush(bs
);
1066 ret
= vhdx_log_flush(bs
, s
, &logs
);