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 "block/vhdx.h"
29 typedef struct VHDXLogSequence
{
33 VHDXLogEntryHeader hdr
;
36 typedef struct VHDXLogDescEntries
{
37 VHDXLogEntryHeader hdr
;
38 VHDXLogDescriptor desc
[];
41 static const MSGUID zero_guid
= { 0 };
43 /* The log located on the disk is circular buffer containing
44 * sectors of 4096 bytes each.
46 * It is assumed for the read/write functions below that the
47 * circular buffer scheme uses a 'one sector open' to indicate
48 * the buffer is full. Given the validation methods used for each
49 * sector, this method should be compatible with other methods that
50 * do not waste a sector.
54 /* Allow peeking at the hdr entry at the beginning of the current
55 * read index, without advancing the read index */
56 static int vhdx_log_peek_hdr(BlockDriverState
*bs
, VHDXLogEntries
*log
,
57 VHDXLogEntryHeader
*hdr
)
65 /* peek is only supported on sector boundaries */
66 if (log
->read
% VHDX_LOG_SECTOR_SIZE
) {
72 /* we are guaranteed that a) log sectors are 4096 bytes,
73 * and b) the log length is a multiple of 1MB. So, there
74 * is always a round number of sectors in the buffer */
75 if ((read
+ sizeof(VHDXLogEntryHeader
)) > log
->length
) {
79 if (read
== log
->write
) {
84 offset
= log
->offset
+ read
;
86 ret
= bdrv_pread(bs
->file
->bs
, offset
, hdr
, sizeof(VHDXLogEntryHeader
));
90 vhdx_log_entry_hdr_le_import(hdr
);
96 /* Index increment for log, based on sector boundaries */
97 static int vhdx_log_inc_idx(uint32_t idx
, uint64_t length
)
99 idx
+= VHDX_LOG_SECTOR_SIZE
;
100 /* we are guaranteed that a) log sectors are 4096 bytes,
101 * and b) the log length is a multiple of 1MB. So, there
102 * is always a round number of sectors in the buffer */
103 return idx
>= length
? 0 : idx
;
107 /* Reset the log to empty */
108 static void vhdx_log_reset(BlockDriverState
*bs
, BDRVVHDXState
*s
)
111 s
->log
.read
= s
->log
.write
= 0;
112 /* a log guid of 0 indicates an empty log to any parser of v0
114 vhdx_update_headers(bs
, s
, false, &guid
);
117 /* Reads num_sectors from the log (all log sectors are 4096 bytes),
118 * into buffer 'buffer'. Upon return, *sectors_read will contain
119 * the number of sectors successfully read.
121 * It is assumed that 'buffer' is already allocated, and of sufficient
122 * size (i.e. >= 4096*num_sectors).
124 * If 'peek' is true, then the tail (read) pointer for the circular buffer is
127 * 0 is returned on success, -errno otherwise. */
128 static int vhdx_log_read_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
129 uint32_t *sectors_read
, void *buffer
,
130 uint32_t num_sectors
, bool peek
)
139 while (num_sectors
) {
140 if (read
== log
->write
) {
144 offset
= log
->offset
+ read
;
146 ret
= bdrv_pread(bs
->file
->bs
, offset
, buffer
, VHDX_LOG_SECTOR_SIZE
);
150 read
= vhdx_log_inc_idx(read
, log
->length
);
152 *sectors_read
= *sectors_read
+ 1;
163 /* Writes num_sectors to the log (all log sectors are 4096 bytes),
164 * from buffer 'buffer'. Upon return, *sectors_written will contain
165 * the number of sectors successfully written.
167 * It is assumed that 'buffer' is at least 4096*num_sectors large.
169 * 0 is returned on success, -errno otherwise */
170 static int vhdx_log_write_sectors(BlockDriverState
*bs
, VHDXLogEntries
*log
,
171 uint32_t *sectors_written
, void *buffer
,
172 uint32_t num_sectors
)
178 BDRVVHDXState
*s
= bs
->opaque
;
180 ret
= vhdx_user_visible_write(bs
, s
);
188 while (num_sectors
) {
190 offset
= log
->offset
+ write
;
191 write
= vhdx_log_inc_idx(write
, log
->length
);
192 if (write
== log
->read
) {
196 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, buffer_tmp
,
197 VHDX_LOG_SECTOR_SIZE
);
201 buffer_tmp
+= VHDX_LOG_SECTOR_SIZE
;
204 *sectors_written
= *sectors_written
+ 1;
213 /* Validates a log entry header */
214 static bool vhdx_log_hdr_is_valid(VHDXLogEntries
*log
, VHDXLogEntryHeader
*hdr
,
219 if (hdr
->signature
!= VHDX_LOG_SIGNATURE
) {
223 /* if the individual entry length is larger than the whole log
224 * buffer, that is obviously invalid */
225 if (log
->length
< hdr
->entry_length
) {
229 /* length of entire entry must be in units of 4KB (log sector size) */
230 if (hdr
->entry_length
% (VHDX_LOG_SECTOR_SIZE
)) {
234 /* per spec, sequence # must be > 0 */
235 if (hdr
->sequence_number
== 0) {
239 /* log entries are only valid if they match the file-wide log guid
240 * found in the active header */
241 if (!guid_eq(hdr
->log_guid
, s
->headers
[s
->curr_header
]->log_guid
)) {
245 if (hdr
->descriptor_count
* sizeof(VHDXLogDescriptor
) > hdr
->entry_length
) {
256 * Given a log header, this will validate that the descriptors and the
257 * corresponding data sectors (if applicable)
259 * Validation consists of:
260 * 1. Making sure the sequence numbers matches the entry header
261 * 2. Verifying a valid signature ('zero' or 'desc' for descriptors)
262 * 3. File offset field is a multiple of 4KB
263 * 4. If a data descriptor, the corresponding data sector
264 * has its signature ('data') and matching sequence number
266 * @desc: the data buffer containing the descriptor
267 * @hdr: the log entry header
269 * Returns true if valid
271 static bool vhdx_log_desc_is_valid(VHDXLogDescriptor
*desc
,
272 VHDXLogEntryHeader
*hdr
)
276 if (desc
->sequence_number
!= hdr
->sequence_number
) {
279 if (desc
->file_offset
% VHDX_LOG_SECTOR_SIZE
) {
283 if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
284 if (desc
->zero_length
% VHDX_LOG_SECTOR_SIZE
== 0) {
288 } else if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
298 /* Prior to sector data for a log entry, there is the header
299 * and the descriptors referenced in the header:
303 * [ hdr, desc ][ desc ][ ... ][ data ][ ... ]
305 * The first sector in a log entry has a 64 byte header, and
306 * up to 126 32-byte descriptors. If more descriptors than
307 * 126 are required, then subsequent sectors can have up to 128
308 * descriptors. Each sector is 4KB. Data follows the descriptor
311 * This will return the number of sectors needed to encompass
312 * the passed number of descriptors in desc_cnt.
314 * This will never return 0, even if desc_cnt is 0.
316 static int vhdx_compute_desc_sectors(uint32_t desc_cnt
)
318 uint32_t desc_sectors
;
320 desc_cnt
+= 2; /* account for header in first sector */
321 desc_sectors
= desc_cnt
/ 128;
322 if (desc_cnt
% 128) {
330 /* Reads the log header, and subsequent descriptors (if any). This
331 * will allocate all the space for buffer, which must be NULL when
332 * passed into this function. Each descriptor will also be validated,
333 * and error returned if any are invalid. */
334 static int vhdx_log_read_desc(BlockDriverState
*bs
, BDRVVHDXState
*s
,
335 VHDXLogEntries
*log
, VHDXLogDescEntries
**buffer
,
339 uint32_t desc_sectors
;
340 uint32_t sectors_read
;
341 VHDXLogEntryHeader hdr
;
342 VHDXLogDescEntries
*desc_entries
= NULL
;
343 VHDXLogDescriptor desc
;
346 assert(*buffer
== NULL
);
348 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
353 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
358 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
359 desc_entries
= qemu_try_blockalign(bs
->file
->bs
,
360 desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
361 if (desc_entries
== NULL
) {
366 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, desc_entries
,
367 desc_sectors
, false);
371 if (sectors_read
!= desc_sectors
) {
376 /* put in proper endianness, and validate each desc */
377 for (i
= 0; i
< hdr
.descriptor_count
; i
++) {
378 desc
= desc_entries
->desc
[i
];
379 vhdx_log_desc_le_import(&desc
);
380 if (convert_endian
) {
381 desc_entries
->desc
[i
] = desc
;
383 if (vhdx_log_desc_is_valid(&desc
, &hdr
) == false) {
388 if (convert_endian
) {
389 desc_entries
->hdr
= hdr
;
392 *buffer
= desc_entries
;
396 qemu_vfree(desc_entries
);
402 /* Flushes the descriptor described by desc to the VHDX image file.
403 * If the descriptor is a data descriptor, than 'data' must be non-NULL,
404 * and >= 4096 bytes (VHDX_LOG_SECTOR_SIZE), containing the data to be
407 * Verification is performed to make sure the sequence numbers of a data
408 * descriptor match the sequence number in the desc.
410 * For a zero descriptor, it may describe multiple sectors to fill with zeroes.
411 * In this case, it should be noted that zeroes are written to disk, and the
412 * image file is not extended as a sparse file. */
413 static int vhdx_log_flush_desc(BlockDriverState
*bs
, VHDXLogDescriptor
*desc
,
414 VHDXLogDataSector
*data
)
417 uint64_t seq
, file_offset
;
423 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
425 if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
432 /* The sequence number of the data sector must match that
433 * in the descriptor */
434 seq
= data
->sequence_high
;
436 seq
|= data
->sequence_low
& 0xffffffff;
438 if (seq
!= desc
->sequence_number
) {
443 /* Each data sector is in total 4096 bytes, however the first
444 * 8 bytes, and last 4 bytes, are located in the descriptor */
445 memcpy(buffer
, &desc
->leading_bytes
, 8);
448 memcpy(buffer
+offset
, data
->data
, 4084);
451 memcpy(buffer
+offset
, &desc
->trailing_bytes
, 4);
453 } else if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
454 /* write 'count' sectors of sector */
455 memset(buffer
, 0, VHDX_LOG_SECTOR_SIZE
);
456 count
= desc
->zero_length
/ VHDX_LOG_SECTOR_SIZE
;
458 error_report("Invalid VHDX log descriptor entry signature 0x%" PRIx32
,
464 file_offset
= desc
->file_offset
;
466 /* count is only > 1 if we are writing zeroes */
467 for (i
= 0; i
< count
; i
++) {
468 ret
= bdrv_pwrite_sync(bs
->file
->bs
, file_offset
, buffer
,
469 VHDX_LOG_SECTOR_SIZE
);
473 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
481 /* Flush the entire log (as described by 'logs') to the VHDX image
482 * file, and then set the log to 'empty' status once complete.
484 * The log entries should be validate prior to flushing */
485 static int vhdx_log_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
486 VHDXLogSequence
*logs
)
490 uint32_t cnt
, sectors_read
;
491 uint64_t new_file_size
;
493 VHDXLogDescEntries
*desc_entries
= NULL
;
494 VHDXLogEntryHeader hdr_tmp
= { 0 };
498 data
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
500 ret
= vhdx_user_visible_write(bs
, s
);
505 /* each iteration represents one log sequence, which may span multiple
508 ret
= vhdx_log_peek_hdr(bs
, &logs
->log
, &hdr_tmp
);
512 /* if the log shows a FlushedFileOffset larger than our current file
513 * size, then that means the file has been truncated / corrupted, and
514 * we must refused to open it / use it */
515 if (hdr_tmp
.flushed_file_offset
> bdrv_getlength(bs
->file
->bs
)) {
520 ret
= vhdx_log_read_desc(bs
, s
, &logs
->log
, &desc_entries
, true);
525 for (i
= 0; i
< desc_entries
->hdr
.descriptor_count
; i
++) {
526 if (desc_entries
->desc
[i
].signature
== VHDX_LOG_DESC_SIGNATURE
) {
527 /* data sector, so read a sector to flush */
528 ret
= vhdx_log_read_sectors(bs
, &logs
->log
, §ors_read
,
533 if (sectors_read
!= 1) {
537 vhdx_log_data_le_import(data
);
540 ret
= vhdx_log_flush_desc(bs
, &desc_entries
->desc
[i
], data
);
545 if (bdrv_getlength(bs
->file
->bs
) < desc_entries
->hdr
.last_file_offset
) {
546 new_file_size
= desc_entries
->hdr
.last_file_offset
;
547 if (new_file_size
% (1024*1024)) {
548 /* round up to nearest 1MB boundary */
549 new_file_size
= ((new_file_size
>> 20) + 1) << 20;
550 bdrv_truncate(bs
->file
->bs
, new_file_size
);
553 qemu_vfree(desc_entries
);
558 /* once the log is fully flushed, indicate that we have an empty log
559 * now. This also sets the log guid to 0, to indicate an empty log */
560 vhdx_log_reset(bs
, s
);
564 qemu_vfree(desc_entries
);
568 static int vhdx_validate_log_entry(BlockDriverState
*bs
, BDRVVHDXState
*s
,
569 VHDXLogEntries
*log
, uint64_t seq
,
570 bool *valid
, VHDXLogEntryHeader
*entry
)
573 VHDXLogEntryHeader hdr
;
575 uint32_t i
, desc_sectors
, total_sectors
, crc
;
576 uint32_t sectors_read
= 0;
577 VHDXLogDescEntries
*desc_buffer
= NULL
;
581 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
586 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
591 if (hdr
.sequence_number
!= seq
+ 1) {
596 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
598 /* Read all log sectors, and calculate log checksum */
600 total_sectors
= hdr
.entry_length
/ VHDX_LOG_SECTOR_SIZE
;
603 /* read_desc() will increment the read idx */
604 ret
= vhdx_log_read_desc(bs
, s
, log
, &desc_buffer
, false);
609 crc
= vhdx_checksum_calc(0xffffffff, (void *)desc_buffer
,
610 desc_sectors
* VHDX_LOG_SECTOR_SIZE
, 4);
613 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
614 if (total_sectors
> desc_sectors
) {
615 for (i
= 0; i
< total_sectors
- desc_sectors
; i
++) {
617 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, buffer
,
619 if (ret
< 0 || sectors_read
!= 1) {
622 crc
= vhdx_checksum_calc(crc
, buffer
, VHDX_LOG_SECTOR_SIZE
, -1);
627 if (crc
!= hdr
.checksum
) {
636 log
->read
= vhdx_log_inc_idx(log
->read
, log
->length
);
640 qemu_vfree(desc_buffer
);
644 /* Search through the log circular buffer, and find the valid, active
645 * log sequence, if any exists
647 static int vhdx_log_search(BlockDriverState
*bs
, BDRVVHDXState
*s
,
648 VHDXLogSequence
*logs
)
652 bool seq_valid
= false;
653 VHDXLogSequence candidate
= { 0 };
654 VHDXLogEntryHeader hdr
= { 0 };
655 VHDXLogEntries curr_log
;
657 memcpy(&curr_log
, &s
->log
, sizeof(VHDXLogEntries
));
658 curr_log
.write
= curr_log
.length
; /* assume log is full */
662 /* now we will go through the whole log sector by sector, until
663 * we find a valid, active log sequence, or reach the end of the
666 uint64_t curr_seq
= 0;
667 VHDXLogSequence current
= { 0 };
669 tail
= curr_log
.read
;
671 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
678 current
.valid
= true;
679 current
.log
= curr_log
;
680 current
.log
.read
= tail
;
681 current
.log
.write
= curr_log
.read
;
687 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
692 if (seq_valid
== false) {
695 current
.log
.write
= curr_log
.read
;
698 curr_seq
= hdr
.sequence_number
;
703 if (candidate
.valid
== false ||
704 current
.hdr
.sequence_number
> candidate
.hdr
.sequence_number
) {
709 if (curr_log
.read
< tail
) {
716 if (candidate
.valid
) {
717 /* this is the next sequence number, for writes */
718 s
->log
.sequence
= candidate
.hdr
.sequence_number
+ 1;
726 /* Parse the replay log. Per the VHDX spec, if the log is present
727 * it must be replayed prior to opening the file, even read-only.
729 * If read-only, we must replay the log in RAM (or refuse to open
730 * a dirty VHDX file read-only) */
731 int vhdx_parse_log(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool *flushed
,
736 VHDXLogSequence logs
= { 0 };
738 hdr
= s
->headers
[s
->curr_header
];
742 /* s->log.hdr is freed in vhdx_close() */
743 if (s
->log
.hdr
== NULL
) {
744 s
->log
.hdr
= qemu_blockalign(bs
, sizeof(VHDXLogEntryHeader
));
747 s
->log
.offset
= hdr
->log_offset
;
748 s
->log
.length
= hdr
->log_length
;
750 if (s
->log
.offset
< VHDX_LOG_MIN_SIZE
||
751 s
->log
.offset
% VHDX_LOG_MIN_SIZE
) {
756 /* per spec, only log version of 0 is supported */
757 if (hdr
->log_version
!= 0) {
762 /* If either the log guid, or log length is zero,
763 * then a replay log is not present */
764 if (guid_eq(hdr
->log_guid
, zero_guid
)) {
768 if (hdr
->log_length
== 0) {
772 if (hdr
->log_length
% VHDX_LOG_MIN_SIZE
) {
778 /* The log is present, we need to find if and where there is an active
779 * sequence of valid entries present in the log. */
781 ret
= vhdx_log_search(bs
, s
, &logs
);
790 "VHDX image file '%s' opened read-only, but "
791 "contains a log that needs to be replayed",
793 error_append_hint(errp
, "To replay the log, run:\n"
794 "qemu-img check -r all '%s'\n",
798 /* now flush the log */
799 ret
= vhdx_log_flush(bs
, s
, &logs
);
813 static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor
*desc
,
814 VHDXLogDataSector
*sector
, void *data
,
817 /* 8 + 4084 + 4 = 4096, 1 log sector */
818 memcpy(&desc
->leading_bytes
, data
, 8);
820 cpu_to_le64s(&desc
->leading_bytes
);
821 memcpy(sector
->data
, data
, 4084);
823 memcpy(&desc
->trailing_bytes
, data
, 4);
824 cpu_to_le32s(&desc
->trailing_bytes
);
827 sector
->sequence_high
= (uint32_t) (seq
>> 32);
828 sector
->sequence_low
= (uint32_t) (seq
& 0xffffffff);
829 sector
->data_signature
= VHDX_LOG_DATA_SIGNATURE
;
831 vhdx_log_desc_le_export(desc
);
832 vhdx_log_data_le_export(sector
);
836 static int vhdx_log_write(BlockDriverState
*bs
, BDRVVHDXState
*s
,
837 void *data
, uint32_t length
, uint64_t offset
)
841 void *merged_sector
= NULL
;
842 void *data_tmp
, *sector_write
;
845 uint32_t desc_sectors
, sectors
, total_length
;
846 uint32_t sectors_written
= 0;
847 uint32_t aligned_length
;
848 uint32_t leading_length
= 0;
849 uint32_t trailing_length
= 0;
850 uint32_t partial_sectors
= 0;
851 uint32_t bytes_written
= 0;
852 uint64_t file_offset
;
854 VHDXLogEntryHeader new_hdr
;
855 VHDXLogDescriptor
*new_desc
= NULL
;
856 VHDXLogDataSector
*data_sector
= NULL
;
857 MSGUID new_guid
= { 0 };
859 header
= s
->headers
[s
->curr_header
];
861 /* need to have offset read data, and be on 4096 byte boundary */
863 if (length
> header
->log_length
) {
864 /* no log present. we could create a log here instead of failing */
869 if (guid_eq(header
->log_guid
, zero_guid
)) {
870 vhdx_guid_generate(&new_guid
);
871 vhdx_update_headers(bs
, s
, false, &new_guid
);
873 /* currently, we require that the log be flushed after
879 /* 0 is an invalid sequence number, but may also represent the first
880 * log write (or a wrapped seq) */
881 if (s
->log
.sequence
== 0) {
885 sector_offset
= offset
% VHDX_LOG_SECTOR_SIZE
;
886 file_offset
= (offset
/ VHDX_LOG_SECTOR_SIZE
) * VHDX_LOG_SECTOR_SIZE
;
888 aligned_length
= length
;
890 /* add in the unaligned head and tail bytes */
892 leading_length
= (VHDX_LOG_SECTOR_SIZE
- sector_offset
);
893 leading_length
= leading_length
> length
? length
: leading_length
;
894 aligned_length
-= leading_length
;
898 sectors
= aligned_length
/ VHDX_LOG_SECTOR_SIZE
;
899 trailing_length
= aligned_length
- (sectors
* VHDX_LOG_SECTOR_SIZE
);
900 if (trailing_length
) {
904 sectors
+= partial_sectors
;
906 /* sectors is now how many sectors the data itself takes, not
907 * including the header and descriptor metadata */
909 new_hdr
= (VHDXLogEntryHeader
) {
910 .signature
= VHDX_LOG_SIGNATURE
,
912 .sequence_number
= s
->log
.sequence
,
913 .descriptor_count
= sectors
,
915 .flushed_file_offset
= bdrv_getlength(bs
->file
->bs
),
916 .last_file_offset
= bdrv_getlength(bs
->file
->bs
),
919 new_hdr
.log_guid
= header
->log_guid
;
921 desc_sectors
= vhdx_compute_desc_sectors(new_hdr
.descriptor_count
);
923 total_length
= (desc_sectors
+ sectors
) * VHDX_LOG_SECTOR_SIZE
;
924 new_hdr
.entry_length
= total_length
;
926 vhdx_log_entry_hdr_le_export(&new_hdr
);
928 buffer
= qemu_blockalign(bs
, total_length
);
929 memcpy(buffer
, &new_hdr
, sizeof(new_hdr
));
931 new_desc
= buffer
+ sizeof(new_hdr
);
932 data_sector
= buffer
+ (desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
935 /* All log sectors are 4KB, so for any partial sectors we must
936 * merge the data with preexisting data from the final file
938 merged_sector
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
940 for (i
= 0; i
< sectors
; i
++) {
941 new_desc
->signature
= VHDX_LOG_DESC_SIGNATURE
;
942 new_desc
->sequence_number
= s
->log
.sequence
;
943 new_desc
->file_offset
= file_offset
;
945 if (i
== 0 && leading_length
) {
946 /* partial sector at the front of the buffer */
947 ret
= bdrv_pread(bs
->file
->bs
, file_offset
, merged_sector
,
948 VHDX_LOG_SECTOR_SIZE
);
952 memcpy(merged_sector
+ sector_offset
, data_tmp
, leading_length
);
953 bytes_written
= leading_length
;
954 sector_write
= merged_sector
;
955 } else if (i
== sectors
- 1 && trailing_length
) {
956 /* partial sector at the end of the buffer */
957 ret
= bdrv_pread(bs
->file
->bs
,
959 merged_sector
+ trailing_length
,
960 VHDX_LOG_SECTOR_SIZE
- trailing_length
);
964 memcpy(merged_sector
, data_tmp
, trailing_length
);
965 bytes_written
= trailing_length
;
966 sector_write
= merged_sector
;
968 bytes_written
= VHDX_LOG_SECTOR_SIZE
;
969 sector_write
= data_tmp
;
972 /* populate the raw sector data into the proper structures,
973 * as well as update the descriptor, and convert to proper
975 vhdx_log_raw_to_le_sector(new_desc
, data_sector
, sector_write
,
978 data_tmp
+= bytes_written
;
981 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
984 /* checksum covers entire entry, from the log header through the
985 * last data sector */
986 vhdx_update_checksum(buffer
, total_length
,
987 offsetof(VHDXLogEntryHeader
, checksum
));
989 /* now write to the log */
990 ret
= vhdx_log_write_sectors(bs
, &s
->log
, §ors_written
, buffer
,
991 desc_sectors
+ sectors
);
996 if (sectors_written
!= desc_sectors
+ sectors
) {
997 /* instead of failing, we could flush the log here */
1003 /* write new tail */
1004 s
->log
.tail
= s
->log
.write
;
1008 qemu_vfree(merged_sector
);
1012 /* Perform a log write, and then immediately flush the entire log */
1013 int vhdx_log_write_and_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
1014 void *data
, uint32_t length
, uint64_t offset
)
1017 VHDXLogSequence logs
= { .valid
= true,
1022 /* Make sure data written (new and/or changed blocks) is stable
1023 * on disk, before creating log entry */
1025 ret
= vhdx_log_write(bs
, s
, data
, length
, offset
);
1031 /* Make sure log is stable on disk */
1033 ret
= vhdx_log_flush(bs
, s
, &logs
);