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-common.h"
21 #include "block/block_int.h"
22 #include "qemu/module.h"
23 #include "block/vhdx.h"
26 typedef struct VHDXLogSequence
{
30 VHDXLogEntryHeader hdr
;
33 typedef struct VHDXLogDescEntries
{
34 VHDXLogEntryHeader hdr
;
35 VHDXLogDescriptor desc
[];
38 static const MSGUID zero_guid
= { 0 };
40 /* The log located on the disk is circular buffer containing
41 * sectors of 4096 bytes each.
43 * It is assumed for the read/write functions below that the
44 * circular buffer scheme uses a 'one sector open' to indicate
45 * the buffer is full. Given the validation methods used for each
46 * sector, this method should be compatible with other methods that
47 * do not waste a sector.
51 /* Allow peeking at the hdr entry at the beginning of the current
52 * read index, without advancing the read index */
53 static int vhdx_log_peek_hdr(BlockDriverState
*bs
, VHDXLogEntries
*log
,
54 VHDXLogEntryHeader
*hdr
)
62 /* peek is only supported on sector boundaries */
63 if (log
->read
% VHDX_LOG_SECTOR_SIZE
) {
69 /* we are guaranteed that a) log sectors are 4096 bytes,
70 * and b) the log length is a multiple of 1MB. So, there
71 * is always a round number of sectors in the buffer */
72 if ((read
+ sizeof(VHDXLogEntryHeader
)) > log
->length
) {
76 if (read
== log
->write
) {
81 offset
= log
->offset
+ read
;
83 ret
= bdrv_pread(bs
->file
, offset
, hdr
, sizeof(VHDXLogEntryHeader
));
92 /* Index increment for log, based on sector boundaries */
93 static int vhdx_log_inc_idx(uint32_t idx
, uint64_t length
)
95 idx
+= VHDX_LOG_SECTOR_SIZE
;
96 /* we are guaranteed that a) log sectors are 4096 bytes,
97 * and b) the log length is a multiple of 1MB. So, there
98 * is always a round number of sectors in the buffer */
99 return idx
>= length
? 0 : idx
;
103 /* Reset the log to empty */
104 static void vhdx_log_reset(BlockDriverState
*bs
, BDRVVHDXState
*s
)
107 s
->log
.read
= s
->log
.write
= 0;
108 /* a log guid of 0 indicates an empty log to any parser of v0
110 vhdx_update_headers(bs
, s
, false, &guid
);
113 /* Reads num_sectors from the log (all log sectors are 4096 bytes),
114 * into buffer 'buffer'. Upon return, *sectors_read will contain
115 * the number of sectors successfully read.
117 * It is assumed that 'buffer' is already allocated, and of sufficient
118 * size (i.e. >= 4096*num_sectors).
120 * If 'peek' is true, then the tail (read) pointer for the circular buffer is
123 * 0 is returned on success, -errno otherwise. */
124 static int vhdx_log_read_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
125 uint32_t *sectors_read
, void *buffer
,
126 uint32_t num_sectors
, bool peek
)
135 while (num_sectors
) {
136 if (read
== log
->write
) {
140 offset
= log
->offset
+ read
;
142 ret
= bdrv_pread(bs
->file
, offset
, buffer
, VHDX_LOG_SECTOR_SIZE
);
146 read
= vhdx_log_inc_idx(read
, log
->length
);
148 *sectors_read
= *sectors_read
+ 1;
159 /* Writes num_sectors to the log (all log sectors are 4096 bytes),
160 * from buffer 'buffer'. Upon return, *sectors_written will contain
161 * the number of sectors successfully written.
163 * It is assumed that 'buffer' is at least 4096*num_sectors large.
165 * 0 is returned on success, -errno otherwise */
166 static int vhdx_log_write_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
167 uint32_t *sectors_written
, void *buffer
,
168 uint32_t num_sectors
)
174 BDRVVHDXState
*s
= bs
->opaque
;
176 ret
= vhdx_user_visible_write(bs
, s
);
184 while (num_sectors
) {
186 offset
= log
->offset
+ write
;
187 write
= vhdx_log_inc_idx(write
, log
->length
);
188 if (write
== log
->read
) {
192 ret
= bdrv_pwrite(bs
->file
, offset
, buffer_tmp
, VHDX_LOG_SECTOR_SIZE
);
196 buffer_tmp
+= VHDX_LOG_SECTOR_SIZE
;
199 *sectors_written
= *sectors_written
+ 1;
208 /* Validates a log entry header */
209 static bool vhdx_log_hdr_is_valid(VHDXLogEntries
*log
, VHDXLogEntryHeader
*hdr
,
214 if (memcmp(&hdr
->signature
, "loge", 4)) {
218 /* if the individual entry length is larger than the whole log
219 * buffer, that is obviously invalid */
220 if (log
->length
< hdr
->entry_length
) {
224 /* length of entire entry must be in units of 4KB (log sector size) */
225 if (hdr
->entry_length
% (VHDX_LOG_SECTOR_SIZE
)) {
229 /* per spec, sequence # must be > 0 */
230 if (hdr
->sequence_number
== 0) {
234 /* log entries are only valid if they match the file-wide log guid
235 * found in the active header */
236 if (!guid_eq(hdr
->log_guid
, s
->headers
[s
->curr_header
]->log_guid
)) {
240 if (hdr
->descriptor_count
* sizeof(VHDXLogDescriptor
) > hdr
->entry_length
) {
251 * Given a log header, this will validate that the descriptors and the
252 * corresponding data sectors (if applicable)
254 * Validation consists of:
255 * 1. Making sure the sequence numbers matches the entry header
256 * 2. Verifying a valid signature ('zero' or 'desc' for descriptors)
257 * 3. File offset field is a multiple of 4KB
258 * 4. If a data descriptor, the corresponding data sector
259 * has its signature ('data') and matching sequence number
261 * @desc: the data buffer containing the descriptor
262 * @hdr: the log entry header
264 * Returns true if valid
266 static bool vhdx_log_desc_is_valid(VHDXLogDescriptor
*desc
,
267 VHDXLogEntryHeader
*hdr
)
271 if (desc
->sequence_number
!= hdr
->sequence_number
) {
274 if (desc
->file_offset
% VHDX_LOG_SECTOR_SIZE
) {
278 if (!memcmp(&desc
->signature
, "zero", 4)) {
279 if (desc
->zero_length
% VHDX_LOG_SECTOR_SIZE
== 0) {
283 } else if (!memcmp(&desc
->signature
, "desc", 4)) {
293 /* Prior to sector data for a log entry, there is the header
294 * and the descriptors referenced in the header:
298 * [ hdr, desc ][ desc ][ ... ][ data ][ ... ]
300 * The first sector in a log entry has a 64 byte header, and
301 * up to 126 32-byte descriptors. If more descriptors than
302 * 126 are required, then subsequent sectors can have up to 128
303 * descriptors. Each sector is 4KB. Data follows the descriptor
306 * This will return the number of sectors needed to encompass
307 * the passed number of descriptors in desc_cnt.
309 * This will never return 0, even if desc_cnt is 0.
311 static int vhdx_compute_desc_sectors(uint32_t desc_cnt
)
313 uint32_t desc_sectors
;
315 desc_cnt
+= 2; /* account for header in first sector */
316 desc_sectors
= desc_cnt
/ 128;
317 if (desc_cnt
% 128) {
325 /* Reads the log header, and subsequent descriptors (if any). This
326 * will allocate all the space for buffer, which must be NULL when
327 * passed into this function. Each descriptor will also be validated,
328 * and error returned if any are invalid. */
329 static int vhdx_log_read_desc(BlockDriverState
*bs
, BDRVVHDXState
*s
,
330 VHDXLogEntries
*log
, VHDXLogDescEntries
**buffer
)
333 uint32_t desc_sectors
;
334 uint32_t sectors_read
;
335 VHDXLogEntryHeader hdr
;
336 VHDXLogDescEntries
*desc_entries
= NULL
;
339 assert(*buffer
== NULL
);
341 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
345 vhdx_log_entry_hdr_le_import(&hdr
);
346 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
351 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
352 desc_entries
= qemu_blockalign(bs
, desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
354 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, desc_entries
,
355 desc_sectors
, false);
359 if (sectors_read
!= desc_sectors
) {
364 /* put in proper endianness, and validate each desc */
365 for (i
= 0; i
< hdr
.descriptor_count
; i
++) {
366 vhdx_log_desc_le_import(&desc_entries
->desc
[i
]);
367 if (vhdx_log_desc_is_valid(&desc_entries
->desc
[i
], &hdr
) == false) {
373 *buffer
= desc_entries
;
377 qemu_vfree(desc_entries
);
383 /* Flushes the descriptor described by desc to the VHDX image file.
384 * If the descriptor is a data descriptor, than 'data' must be non-NULL,
385 * and >= 4096 bytes (VHDX_LOG_SECTOR_SIZE), containing the data to be
388 * Verification is performed to make sure the sequence numbers of a data
389 * descriptor match the sequence number in the desc.
391 * For a zero descriptor, it may describe multiple sectors to fill with zeroes.
392 * In this case, it should be noted that zeroes are written to disk, and the
393 * image file is not extended as a sparse file. */
394 static int vhdx_log_flush_desc(BlockDriverState
*bs
, VHDXLogDescriptor
*desc
,
395 VHDXLogDataSector
*data
)
398 uint64_t seq
, file_offset
;
404 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
406 if (!memcmp(&desc
->signature
, "desc", 4)) {
413 /* The sequence number of the data sector must match that
414 * in the descriptor */
415 seq
= data
->sequence_high
;
417 seq
|= data
->sequence_low
& 0xffffffff;
419 if (seq
!= desc
->sequence_number
) {
424 /* Each data sector is in total 4096 bytes, however the first
425 * 8 bytes, and last 4 bytes, are located in the descriptor */
426 memcpy(buffer
, &desc
->leading_bytes
, 8);
429 memcpy(buffer
+offset
, data
->data
, 4084);
432 memcpy(buffer
+offset
, &desc
->trailing_bytes
, 4);
434 } else if (!memcmp(&desc
->signature
, "zero", 4)) {
435 /* write 'count' sectors of sector */
436 memset(buffer
, 0, VHDX_LOG_SECTOR_SIZE
);
437 count
= desc
->zero_length
/ VHDX_LOG_SECTOR_SIZE
;
440 file_offset
= desc
->file_offset
;
442 /* count is only > 1 if we are writing zeroes */
443 for (i
= 0; i
< count
; i
++) {
444 ret
= bdrv_pwrite_sync(bs
->file
, file_offset
, buffer
,
445 VHDX_LOG_SECTOR_SIZE
);
449 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
457 /* Flush the entire log (as described by 'logs') to the VHDX image
458 * file, and then set the log to 'empty' status once complete.
460 * The log entries should be validate prior to flushing */
461 static int vhdx_log_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
462 VHDXLogSequence
*logs
)
466 uint32_t cnt
, sectors_read
;
467 uint64_t new_file_size
;
469 VHDXLogDescEntries
*desc_entries
= NULL
;
470 VHDXLogEntryHeader hdr_tmp
= { 0 };
474 data
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
476 ret
= vhdx_user_visible_write(bs
, s
);
481 /* each iteration represents one log sequence, which may span multiple
484 ret
= vhdx_log_peek_hdr(bs
, &logs
->log
, &hdr_tmp
);
488 /* if the log shows a FlushedFileOffset larger than our current file
489 * size, then that means the file has been truncated / corrupted, and
490 * we must refused to open it / use it */
491 if (hdr_tmp
.flushed_file_offset
> bdrv_getlength(bs
->file
)) {
496 ret
= vhdx_log_read_desc(bs
, s
, &logs
->log
, &desc_entries
);
501 for (i
= 0; i
< desc_entries
->hdr
.descriptor_count
; i
++) {
502 if (!memcmp(&desc_entries
->desc
[i
].signature
, "desc", 4)) {
503 /* data sector, so read a sector to flush */
504 ret
= vhdx_log_read_sectors(bs
, &logs
->log
, §ors_read
,
509 if (sectors_read
!= 1) {
515 ret
= vhdx_log_flush_desc(bs
, &desc_entries
->desc
[i
], data
);
520 if (bdrv_getlength(bs
->file
) < desc_entries
->hdr
.last_file_offset
) {
521 new_file_size
= desc_entries
->hdr
.last_file_offset
;
522 if (new_file_size
% (1024*1024)) {
523 /* round up to nearest 1MB boundary */
524 new_file_size
= ((new_file_size
>> 20) + 1) << 20;
525 bdrv_truncate(bs
->file
, new_file_size
);
528 qemu_vfree(desc_entries
);
533 /* once the log is fully flushed, indicate that we have an empty log
534 * now. This also sets the log guid to 0, to indicate an empty log */
535 vhdx_log_reset(bs
, s
);
539 qemu_vfree(desc_entries
);
543 static int vhdx_validate_log_entry(BlockDriverState
*bs
, BDRVVHDXState
*s
,
544 VHDXLogEntries
*log
, uint64_t seq
,
545 bool *valid
, VHDXLogEntryHeader
*entry
)
548 VHDXLogEntryHeader hdr
;
550 uint32_t i
, desc_sectors
, total_sectors
, crc
;
551 uint32_t sectors_read
= 0;
552 VHDXLogDescEntries
*desc_buffer
= NULL
;
556 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
561 vhdx_log_entry_hdr_le_import(&hdr
);
564 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
569 if (hdr
.sequence_number
!= seq
+ 1) {
574 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
576 /* Read desc sectors, and calculate log checksum */
578 total_sectors
= hdr
.entry_length
/ VHDX_LOG_SECTOR_SIZE
;
581 /* read_desc() will increment the read idx */
582 ret
= vhdx_log_read_desc(bs
, s
, log
, &desc_buffer
);
587 crc
= vhdx_checksum_calc(0xffffffff, (void *)desc_buffer
,
588 desc_sectors
* VHDX_LOG_SECTOR_SIZE
, 4);
591 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
592 if (total_sectors
> desc_sectors
) {
593 for (i
= 0; i
< total_sectors
- desc_sectors
; i
++) {
595 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, buffer
,
597 if (ret
< 0 || sectors_read
!= 1) {
600 crc
= vhdx_checksum_calc(crc
, buffer
, VHDX_LOG_SECTOR_SIZE
, -1);
605 if (crc
!= desc_buffer
->hdr
.checksum
) {
614 log
->read
= vhdx_log_inc_idx(log
->read
, log
->length
);
618 qemu_vfree(desc_buffer
);
622 /* Search through the log circular buffer, and find the valid, active
623 * log sequence, if any exists
625 static int vhdx_log_search(BlockDriverState
*bs
, BDRVVHDXState
*s
,
626 VHDXLogSequence
*logs
)
630 bool seq_valid
= false;
631 VHDXLogSequence candidate
= { 0 };
632 VHDXLogEntryHeader hdr
= { 0 };
633 VHDXLogEntries curr_log
;
635 memcpy(&curr_log
, &s
->log
, sizeof(VHDXLogEntries
));
636 curr_log
.write
= curr_log
.length
; /* assume log is full */
640 /* now we will go through the whole log sector by sector, until
641 * we find a valid, active log sequence, or reach the end of the
644 uint64_t curr_seq
= 0;
645 VHDXLogSequence current
= { 0 };
647 tail
= curr_log
.read
;
649 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
656 current
.valid
= true;
657 current
.log
= curr_log
;
658 current
.log
.read
= tail
;
659 current
.log
.write
= curr_log
.read
;
665 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
670 if (seq_valid
== false) {
673 current
.log
.write
= curr_log
.read
;
676 curr_seq
= hdr
.sequence_number
;
681 if (candidate
.valid
== false ||
682 current
.hdr
.sequence_number
> candidate
.hdr
.sequence_number
) {
687 if (curr_log
.read
< tail
) {
694 if (candidate
.valid
) {
695 /* this is the next sequence number, for writes */
696 s
->log
.sequence
= candidate
.hdr
.sequence_number
+ 1;
704 /* Parse the replay log. Per the VHDX spec, if the log is present
705 * it must be replayed prior to opening the file, even read-only.
707 * If read-only, we must replay the log in RAM (or refuse to open
708 * a dirty VHDX file read-only) */
709 int vhdx_parse_log(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool *flushed
,
714 VHDXLogSequence logs
= { 0 };
716 hdr
= s
->headers
[s
->curr_header
];
720 /* s->log.hdr is freed in vhdx_close() */
721 if (s
->log
.hdr
== NULL
) {
722 s
->log
.hdr
= qemu_blockalign(bs
, sizeof(VHDXLogEntryHeader
));
725 s
->log
.offset
= hdr
->log_offset
;
726 s
->log
.length
= hdr
->log_length
;
728 if (s
->log
.offset
< VHDX_LOG_MIN_SIZE
||
729 s
->log
.offset
% VHDX_LOG_MIN_SIZE
) {
734 /* per spec, only log version of 0 is supported */
735 if (hdr
->log_version
!= 0) {
740 /* If either the log guid, or log length is zero,
741 * then a replay log is not present */
742 if (guid_eq(hdr
->log_guid
, zero_guid
)) {
746 if (hdr
->log_length
== 0) {
750 if (hdr
->log_length
% VHDX_LOG_MIN_SIZE
) {
756 /* The log is present, we need to find if and where there is an active
757 * sequence of valid entries present in the log. */
759 ret
= vhdx_log_search(bs
, s
, &logs
);
767 error_setg_errno(errp
, EPERM
,
768 "VHDX image file '%s' opened read-only, but "
769 "contains a log that needs to be replayed. To "
770 "replay the log, execute:\n qemu-img check -r "
772 bs
->filename
, bs
->filename
);
775 /* now flush the log */
776 ret
= vhdx_log_flush(bs
, s
, &logs
);
790 static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor
*desc
,
791 VHDXLogDataSector
*sector
, void *data
,
794 /* 8 + 4084 + 4 = 4096, 1 log sector */
795 memcpy(&desc
->leading_bytes
, data
, 8);
797 cpu_to_le64s(&desc
->leading_bytes
);
798 memcpy(sector
->data
, data
, 4084);
800 memcpy(&desc
->trailing_bytes
, data
, 4);
801 cpu_to_le32s(&desc
->trailing_bytes
);
804 sector
->sequence_high
= (uint32_t) (seq
>> 32);
805 sector
->sequence_low
= (uint32_t) (seq
& 0xffffffff);
806 sector
->data_signature
= VHDX_LOG_DATA_SIGNATURE
;
808 vhdx_log_desc_le_export(desc
);
809 vhdx_log_data_le_export(sector
);
813 static int vhdx_log_write(BlockDriverState
*bs
, BDRVVHDXState
*s
,
814 void *data
, uint32_t length
, uint64_t offset
)
818 void *merged_sector
= NULL
;
819 void *data_tmp
, *sector_write
;
822 uint32_t desc_sectors
, sectors
, total_length
;
823 uint32_t sectors_written
= 0;
824 uint32_t aligned_length
;
825 uint32_t leading_length
= 0;
826 uint32_t trailing_length
= 0;
827 uint32_t partial_sectors
= 0;
828 uint32_t bytes_written
= 0;
829 uint64_t file_offset
;
831 VHDXLogEntryHeader new_hdr
;
832 VHDXLogDescriptor
*new_desc
= NULL
;
833 VHDXLogDataSector
*data_sector
= NULL
;
834 MSGUID new_guid
= { 0 };
836 header
= s
->headers
[s
->curr_header
];
838 /* need to have offset read data, and be on 4096 byte boundary */
840 if (length
> header
->log_length
) {
841 /* no log present. we could create a log here instead of failing */
846 if (guid_eq(header
->log_guid
, zero_guid
)) {
847 vhdx_guid_generate(&new_guid
);
848 vhdx_update_headers(bs
, s
, false, &new_guid
);
850 /* currently, we require that the log be flushed after
856 /* 0 is an invalid sequence number, but may also represent the first
857 * log write (or a wrapped seq) */
858 if (s
->log
.sequence
== 0) {
862 sector_offset
= offset
% VHDX_LOG_SECTOR_SIZE
;
863 file_offset
= (offset
/ VHDX_LOG_SECTOR_SIZE
) * VHDX_LOG_SECTOR_SIZE
;
865 aligned_length
= length
;
867 /* add in the unaligned head and tail bytes */
869 leading_length
= (VHDX_LOG_SECTOR_SIZE
- sector_offset
);
870 leading_length
= leading_length
> length
? length
: leading_length
;
871 aligned_length
-= leading_length
;
875 sectors
= aligned_length
/ VHDX_LOG_SECTOR_SIZE
;
876 trailing_length
= aligned_length
- (sectors
* VHDX_LOG_SECTOR_SIZE
);
877 if (trailing_length
) {
881 sectors
+= partial_sectors
;
883 /* sectors is now how many sectors the data itself takes, not
884 * including the header and descriptor metadata */
886 new_hdr
= (VHDXLogEntryHeader
) {
887 .signature
= VHDX_LOG_SIGNATURE
,
889 .sequence_number
= s
->log
.sequence
,
890 .descriptor_count
= sectors
,
892 .flushed_file_offset
= bdrv_getlength(bs
->file
),
893 .last_file_offset
= bdrv_getlength(bs
->file
),
896 new_hdr
.log_guid
= header
->log_guid
;
898 desc_sectors
= vhdx_compute_desc_sectors(new_hdr
.descriptor_count
);
900 total_length
= (desc_sectors
+ sectors
) * VHDX_LOG_SECTOR_SIZE
;
901 new_hdr
.entry_length
= total_length
;
903 vhdx_log_entry_hdr_le_export(&new_hdr
);
905 buffer
= qemu_blockalign(bs
, total_length
);
906 memcpy(buffer
, &new_hdr
, sizeof(new_hdr
));
908 new_desc
= (VHDXLogDescriptor
*) (buffer
+ sizeof(new_hdr
));
909 data_sector
= buffer
+ (desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
912 /* All log sectors are 4KB, so for any partial sectors we must
913 * merge the data with preexisting data from the final file
915 merged_sector
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
917 for (i
= 0; i
< sectors
; i
++) {
918 new_desc
->signature
= VHDX_LOG_DESC_SIGNATURE
;
919 new_desc
->sequence_number
= s
->log
.sequence
;
920 new_desc
->file_offset
= file_offset
;
922 if (i
== 0 && leading_length
) {
923 /* partial sector at the front of the buffer */
924 ret
= bdrv_pread(bs
->file
, file_offset
, merged_sector
,
925 VHDX_LOG_SECTOR_SIZE
);
929 memcpy(merged_sector
+ sector_offset
, data_tmp
, leading_length
);
930 bytes_written
= leading_length
;
931 sector_write
= merged_sector
;
932 } else if (i
== sectors
- 1 && trailing_length
) {
933 /* partial sector at the end of the buffer */
934 ret
= bdrv_pread(bs
->file
,
936 merged_sector
+ trailing_length
,
937 VHDX_LOG_SECTOR_SIZE
- trailing_length
);
941 memcpy(merged_sector
, data_tmp
, trailing_length
);
942 bytes_written
= trailing_length
;
943 sector_write
= merged_sector
;
945 bytes_written
= VHDX_LOG_SECTOR_SIZE
;
946 sector_write
= data_tmp
;
949 /* populate the raw sector data into the proper structures,
950 * as well as update the descriptor, and convert to proper
952 vhdx_log_raw_to_le_sector(new_desc
, data_sector
, sector_write
,
955 data_tmp
+= bytes_written
;
958 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
961 /* checksum covers entire entry, from the log header through the
962 * last data sector */
963 vhdx_update_checksum(buffer
, total_length
,
964 offsetof(VHDXLogEntryHeader
, checksum
));
965 cpu_to_le32s((uint32_t *)(buffer
+ 4));
967 /* now write to the log */
968 ret
= vhdx_log_write_sectors(bs
, &s
->log
, §ors_written
, buffer
,
969 desc_sectors
+ sectors
);
974 if (sectors_written
!= desc_sectors
+ sectors
) {
975 /* instead of failing, we could flush the log here */
982 s
->log
.tail
= s
->log
.write
;
986 qemu_vfree(merged_sector
);
990 /* Perform a log write, and then immediately flush the entire log */
991 int vhdx_log_write_and_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
992 void *data
, uint32_t length
, uint64_t offset
)
995 VHDXLogSequence logs
= { .valid
= true,
1000 /* Make sure data written (new and/or changed blocks) is stable
1001 * on disk, before creating log entry */
1003 ret
= vhdx_log_write(bs
, s
, data
, length
, offset
);
1009 /* Make sure log is stable on disk */
1011 ret
= vhdx_log_flush(bs
, s
, &logs
);