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.
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"
31 typedef struct VHDXLogSequence
{
35 VHDXLogEntryHeader hdr
;
38 typedef struct VHDXLogDescEntries
{
39 VHDXLogEntryHeader hdr
;
40 VHDXLogDescriptor desc
[];
43 static const MSGUID zero_guid
= { 0 };
45 /* The log located on the disk is circular buffer containing
46 * sectors of 4096 bytes each.
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.
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
)
68 /* peek is only supported on sector boundaries */
69 if (log
->read
% VHDX_LOG_SECTOR_SIZE
) {
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 if ((read
+ sizeof(VHDXLogEntryHeader
)) > log
->length
) {
82 if (read
== log
->write
) {
87 offset
= log
->offset
+ read
;
89 ret
= bdrv_pread(bs
->file
, offset
, sizeof(VHDXLogEntryHeader
), hdr
, 0);
93 vhdx_log_entry_hdr_le_import(hdr
);
99 /* Index increment for log, based on sector boundaries */
100 static int vhdx_log_inc_idx(uint32_t idx
, uint64_t length
)
102 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 return idx
>= length
? 0 : idx
;
110 /* Reset the log to empty */
111 static void GRAPH_RDLOCK
vhdx_log_reset(BlockDriverState
*bs
, BDRVVHDXState
*s
)
114 s
->log
.read
= s
->log
.write
= 0;
115 /* a log guid of 0 indicates an empty log to any parser of v0
117 vhdx_update_headers(bs
, s
, false, &guid
);
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.
124 * It is assumed that 'buffer' is already allocated, and of sufficient
125 * size (i.e. >= 4096*num_sectors).
127 * If 'peek' is true, then the tail (read) pointer for the circular buffer is
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
)
143 while (num_sectors
) {
144 if (read
== log
->write
) {
148 offset
= log
->offset
+ read
;
150 ret
= bdrv_pread(bs
->file
, offset
, VHDX_LOG_SECTOR_SIZE
, buffer
, 0);
154 read
= vhdx_log_inc_idx(read
, log
->length
);
156 *sectors_read
= *sectors_read
+ 1;
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.
171 * It is assumed that 'buffer' is at least 4096*num_sectors large.
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
)
183 BDRVVHDXState
*s
= bs
->opaque
;
185 ret
= vhdx_user_visible_write(bs
, s
);
193 while (num_sectors
) {
195 offset
= log
->offset
+ write
;
196 write
= vhdx_log_inc_idx(write
, log
->length
);
197 if (write
== log
->read
) {
201 ret
= bdrv_co_pwrite(bs
->file
, offset
, VHDX_LOG_SECTOR_SIZE
, buffer_tmp
, 0);
205 buffer_tmp
+= VHDX_LOG_SECTOR_SIZE
;
208 *sectors_written
= *sectors_written
+ 1;
217 /* Validates a log entry header */
218 static bool vhdx_log_hdr_is_valid(VHDXLogEntries
*log
, VHDXLogEntryHeader
*hdr
,
223 if (hdr
->signature
!= VHDX_LOG_SIGNATURE
) {
227 /* if the individual entry length is larger than the whole log
228 * buffer, that is obviously invalid */
229 if (log
->length
< hdr
->entry_length
) {
233 /* length of entire entry must be in units of 4KB (log sector size) */
234 if (hdr
->entry_length
% (VHDX_LOG_SECTOR_SIZE
)) {
238 /* per spec, sequence # must be > 0 */
239 if (hdr
->sequence_number
== 0) {
243 /* log entries are only valid if they match the file-wide log guid
244 * found in the active header */
245 if (!guid_eq(hdr
->log_guid
, s
->headers
[s
->curr_header
]->log_guid
)) {
249 if (hdr
->descriptor_count
* sizeof(VHDXLogDescriptor
) > hdr
->entry_length
) {
260 * Given a log header, this will validate that the descriptors and the
261 * corresponding data sectors (if applicable)
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
270 * @desc: the data buffer containing the descriptor
271 * @hdr: the log entry header
273 * Returns true if valid
275 static bool vhdx_log_desc_is_valid(VHDXLogDescriptor
*desc
,
276 VHDXLogEntryHeader
*hdr
)
280 if (desc
->sequence_number
!= hdr
->sequence_number
) {
283 if (desc
->file_offset
% VHDX_LOG_SECTOR_SIZE
) {
287 if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
288 if (desc
->zero_length
% VHDX_LOG_SECTOR_SIZE
== 0) {
292 } else if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
302 /* Prior to sector data for a log entry, there is the header
303 * and the descriptors referenced in the header:
307 * [ hdr, desc ][ desc ][ ... ][ data ][ ... ]
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
315 * This will return the number of sectors needed to encompass
316 * the passed number of descriptors in desc_cnt.
318 * This will never return 0, even if desc_cnt is 0.
320 static int vhdx_compute_desc_sectors(uint32_t desc_cnt
)
322 uint32_t desc_sectors
;
324 desc_cnt
+= 2; /* account for header in first sector */
325 desc_sectors
= desc_cnt
/ 128;
326 if (desc_cnt
% 128) {
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
)
343 uint32_t desc_sectors
;
344 uint32_t sectors_read
;
345 VHDXLogEntryHeader hdr
;
346 VHDXLogDescEntries
*desc_entries
= NULL
;
347 VHDXLogDescriptor desc
;
350 assert(*buffer
== NULL
);
352 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
357 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
362 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
363 desc_entries
= qemu_try_blockalign(bs
->file
->bs
,
364 desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
365 if (desc_entries
== NULL
) {
370 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, desc_entries
,
371 desc_sectors
, false);
375 if (sectors_read
!= desc_sectors
) {
380 /* put in proper endianness, and validate each desc */
381 for (i
= 0; i
< hdr
.descriptor_count
; i
++) {
382 desc
= desc_entries
->desc
[i
];
383 vhdx_log_desc_le_import(&desc
);
384 if (convert_endian
) {
385 desc_entries
->desc
[i
] = desc
;
387 if (vhdx_log_desc_is_valid(&desc
, &hdr
) == false) {
392 if (convert_endian
) {
393 desc_entries
->hdr
= hdr
;
396 *buffer
= desc_entries
;
400 qemu_vfree(desc_entries
);
406 /* Flushes the descriptor described by desc to the VHDX image file.
407 * If the descriptor is a data descriptor, than 'data' must be non-NULL,
408 * and >= 4096 bytes (VHDX_LOG_SECTOR_SIZE), containing the data to be
411 * Verification is performed to make sure the sequence numbers of a data
412 * descriptor match the sequence number in the desc.
414 * For a zero descriptor, it may describe multiple sectors to fill with zeroes.
415 * In this case, it should be noted that zeroes are written to disk, and the
416 * image file is not extended as a sparse file. */
417 static int GRAPH_RDLOCK
418 vhdx_log_flush_desc(BlockDriverState
*bs
, VHDXLogDescriptor
*desc
,
419 VHDXLogDataSector
*data
)
422 uint64_t seq
, file_offset
;
428 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
430 if (desc
->signature
== VHDX_LOG_DESC_SIGNATURE
) {
437 /* The sequence number of the data sector must match that
438 * in the descriptor */
439 seq
= data
->sequence_high
;
441 seq
|= data
->sequence_low
& 0xffffffff;
443 if (seq
!= desc
->sequence_number
) {
448 /* Each data sector is in total 4096 bytes, however the first
449 * 8 bytes, and last 4 bytes, are located in the descriptor */
450 memcpy(buffer
, &desc
->leading_bytes
, 8);
453 memcpy(buffer
+offset
, data
->data
, 4084);
456 memcpy(buffer
+offset
, &desc
->trailing_bytes
, 4);
458 } else if (desc
->signature
== VHDX_LOG_ZERO_SIGNATURE
) {
459 /* write 'count' sectors of sector */
460 memset(buffer
, 0, VHDX_LOG_SECTOR_SIZE
);
461 count
= desc
->zero_length
/ VHDX_LOG_SECTOR_SIZE
;
463 error_report("Invalid VHDX log descriptor entry signature 0x%" PRIx32
,
469 file_offset
= desc
->file_offset
;
471 /* count is only > 1 if we are writing zeroes */
472 for (i
= 0; i
< count
; i
++) {
473 ret
= bdrv_pwrite_sync(bs
->file
, file_offset
, VHDX_LOG_SECTOR_SIZE
,
478 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
486 /* Flush the entire log (as described by 'logs') to the VHDX image
487 * file, and then set the log to 'empty' status once complete.
489 * The log entries should be validate prior to flushing */
490 static int GRAPH_RDLOCK
491 vhdx_log_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
, VHDXLogSequence
*logs
)
495 uint32_t cnt
, sectors_read
;
496 uint64_t new_file_size
;
499 VHDXLogDescEntries
*desc_entries
= NULL
;
500 VHDXLogEntryHeader hdr_tmp
= { 0 };
504 data
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
506 ret
= vhdx_user_visible_write(bs
, s
);
511 /* each iteration represents one log sequence, which may span multiple
514 ret
= vhdx_log_peek_hdr(bs
, &logs
->log
, &hdr_tmp
);
518 file_length
= bdrv_getlength(bs
->file
->bs
);
519 if (file_length
< 0) {
523 /* if the log shows a FlushedFileOffset larger than our current file
524 * size, then that means the file has been truncated / corrupted, and
525 * we must refused to open it / use it */
526 if (hdr_tmp
.flushed_file_offset
> file_length
) {
531 ret
= vhdx_log_read_desc(bs
, s
, &logs
->log
, &desc_entries
, true);
536 for (i
= 0; i
< desc_entries
->hdr
.descriptor_count
; i
++) {
537 if (desc_entries
->desc
[i
].signature
== VHDX_LOG_DESC_SIGNATURE
) {
538 /* data sector, so read a sector to flush */
539 ret
= vhdx_log_read_sectors(bs
, &logs
->log
, §ors_read
,
544 if (sectors_read
!= 1) {
548 vhdx_log_data_le_import(data
);
551 ret
= vhdx_log_flush_desc(bs
, &desc_entries
->desc
[i
], data
);
556 if (file_length
< desc_entries
->hdr
.last_file_offset
) {
557 new_file_size
= desc_entries
->hdr
.last_file_offset
;
558 if (new_file_size
% (1 * MiB
)) {
559 /* round up to nearest 1MB boundary */
560 new_file_size
= QEMU_ALIGN_UP(new_file_size
, MiB
);
561 if (new_file_size
> INT64_MAX
) {
565 ret
= bdrv_truncate(bs
->file
, new_file_size
, false,
566 PREALLOC_MODE_OFF
, 0, NULL
);
572 qemu_vfree(desc_entries
);
576 ret
= bdrv_flush(bs
);
580 /* once the log is fully flushed, indicate that we have an empty log
581 * now. This also sets the log guid to 0, to indicate an empty log */
582 vhdx_log_reset(bs
, s
);
586 qemu_vfree(desc_entries
);
590 static int GRAPH_RDLOCK
591 vhdx_validate_log_entry(BlockDriverState
*bs
, BDRVVHDXState
*s
,
592 VHDXLogEntries
*log
, uint64_t seq
,
593 bool *valid
, VHDXLogEntryHeader
*entry
)
596 VHDXLogEntryHeader hdr
;
598 uint32_t i
, desc_sectors
, total_sectors
, crc
;
599 uint32_t sectors_read
= 0;
600 VHDXLogDescEntries
*desc_buffer
= NULL
;
604 ret
= vhdx_log_peek_hdr(bs
, log
, &hdr
);
609 if (vhdx_log_hdr_is_valid(log
, &hdr
, s
) == false) {
614 if (hdr
.sequence_number
!= seq
+ 1) {
619 desc_sectors
= vhdx_compute_desc_sectors(hdr
.descriptor_count
);
621 /* Read all log sectors, and calculate log checksum */
623 total_sectors
= hdr
.entry_length
/ VHDX_LOG_SECTOR_SIZE
;
626 /* read_desc() will increment the read idx */
627 ret
= vhdx_log_read_desc(bs
, s
, log
, &desc_buffer
, false);
632 crc
= vhdx_checksum_calc(0xffffffff, (void *)desc_buffer
,
633 desc_sectors
* VHDX_LOG_SECTOR_SIZE
, 4);
636 buffer
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
637 if (total_sectors
> desc_sectors
) {
638 for (i
= 0; i
< total_sectors
- desc_sectors
; i
++) {
640 ret
= vhdx_log_read_sectors(bs
, log
, §ors_read
, buffer
,
642 if (ret
< 0 || sectors_read
!= 1) {
645 crc
= vhdx_checksum_calc(crc
, buffer
, VHDX_LOG_SECTOR_SIZE
, -1);
650 if (crc
!= hdr
.checksum
) {
659 log
->read
= vhdx_log_inc_idx(log
->read
, log
->length
);
663 qemu_vfree(desc_buffer
);
667 /* Search through the log circular buffer, and find the valid, active
668 * log sequence, if any exists
670 static int GRAPH_RDLOCK
671 vhdx_log_search(BlockDriverState
*bs
, BDRVVHDXState
*s
, VHDXLogSequence
*logs
)
675 bool seq_valid
= false;
676 VHDXLogSequence candidate
= { 0 };
677 VHDXLogEntryHeader hdr
= { 0 };
678 VHDXLogEntries curr_log
;
680 memcpy(&curr_log
, &s
->log
, sizeof(VHDXLogEntries
));
681 curr_log
.write
= curr_log
.length
; /* assume log is full */
685 /* now we will go through the whole log sector by sector, until
686 * we find a valid, active log sequence, or reach the end of the
689 uint64_t curr_seq
= 0;
690 VHDXLogSequence current
= { 0 };
692 tail
= curr_log
.read
;
694 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
701 current
.valid
= true;
702 current
.log
= curr_log
;
703 current
.log
.read
= tail
;
704 current
.log
.write
= curr_log
.read
;
710 ret
= vhdx_validate_log_entry(bs
, s
, &curr_log
, curr_seq
,
715 if (seq_valid
== false) {
718 current
.log
.write
= curr_log
.read
;
721 curr_seq
= hdr
.sequence_number
;
726 if (candidate
.valid
== false ||
727 current
.hdr
.sequence_number
> candidate
.hdr
.sequence_number
) {
732 if (curr_log
.read
< tail
) {
739 if (candidate
.valid
) {
740 /* this is the next sequence number, for writes */
741 s
->log
.sequence
= candidate
.hdr
.sequence_number
+ 1;
749 /* Parse the replay log. Per the VHDX spec, if the log is present
750 * it must be replayed prior to opening the file, even read-only.
752 * If read-only, we must replay the log in RAM (or refuse to open
753 * a dirty VHDX file read-only) */
754 int vhdx_parse_log(BlockDriverState
*bs
, BDRVVHDXState
*s
, bool *flushed
,
759 VHDXLogSequence logs
= { 0 };
761 hdr
= s
->headers
[s
->curr_header
];
765 /* s->log.hdr is freed in vhdx_close() */
766 if (s
->log
.hdr
== NULL
) {
767 s
->log
.hdr
= qemu_blockalign(bs
, sizeof(VHDXLogEntryHeader
));
770 s
->log
.offset
= hdr
->log_offset
;
771 s
->log
.length
= hdr
->log_length
;
773 if (s
->log
.offset
< VHDX_LOG_MIN_SIZE
||
774 s
->log
.offset
% VHDX_LOG_MIN_SIZE
) {
779 /* per spec, only log version of 0 is supported */
780 if (hdr
->log_version
!= 0) {
785 /* If either the log guid, or log length is zero,
786 * then a replay log is not present */
787 if (guid_eq(hdr
->log_guid
, zero_guid
)) {
791 if (hdr
->log_length
== 0) {
795 if (hdr
->log_length
% VHDX_LOG_MIN_SIZE
) {
801 /* The log is present, we need to find if and where there is an active
802 * sequence of valid entries present in the log. */
804 ret
= vhdx_log_search(bs
, s
, &logs
);
810 if (bdrv_is_read_only(bs
)) {
811 bdrv_refresh_filename(bs
);
814 "VHDX image file '%s' opened read-only, but "
815 "contains a log that needs to be replayed",
817 error_append_hint(errp
, "To replay the log, run:\n"
818 "qemu-img check -r all '%s'\n",
822 /* now flush the log */
823 ret
= vhdx_log_flush(bs
, s
, &logs
);
837 static void vhdx_log_raw_to_le_sector(VHDXLogDescriptor
*desc
,
838 VHDXLogDataSector
*sector
, void *data
,
841 /* 8 + 4084 + 4 = 4096, 1 log sector */
842 memcpy(&desc
->leading_bytes
, data
, 8);
844 desc
->leading_bytes
= cpu_to_le64(desc
->leading_bytes
);
845 memcpy(sector
->data
, data
, 4084);
847 memcpy(&desc
->trailing_bytes
, data
, 4);
848 desc
->trailing_bytes
= cpu_to_le32(desc
->trailing_bytes
);
851 sector
->sequence_high
= (uint32_t) (seq
>> 32);
852 sector
->sequence_low
= (uint32_t) (seq
& 0xffffffff);
853 sector
->data_signature
= VHDX_LOG_DATA_SIGNATURE
;
855 vhdx_log_desc_le_export(desc
);
856 vhdx_log_data_le_export(sector
);
860 static int coroutine_fn GRAPH_RDLOCK
861 vhdx_log_write(BlockDriverState
*bs
, BDRVVHDXState
*s
,
862 void *data
, uint32_t length
, uint64_t offset
)
866 void *merged_sector
= NULL
;
867 void *data_tmp
, *sector_write
;
870 uint32_t desc_sectors
, sectors
, total_length
;
871 uint32_t sectors_written
= 0;
872 uint32_t aligned_length
;
873 uint32_t leading_length
= 0;
874 uint32_t trailing_length
= 0;
875 uint32_t partial_sectors
= 0;
876 uint32_t bytes_written
= 0;
877 uint64_t file_offset
;
880 VHDXLogEntryHeader new_hdr
;
881 VHDXLogDescriptor
*new_desc
= NULL
;
882 VHDXLogDataSector
*data_sector
= NULL
;
883 MSGUID new_guid
= { 0 };
885 header
= s
->headers
[s
->curr_header
];
887 /* need to have offset read data, and be on 4096 byte boundary */
889 if (length
> header
->log_length
) {
890 /* no log present. we could create a log here instead of failing */
895 if (guid_eq(header
->log_guid
, zero_guid
)) {
896 vhdx_guid_generate(&new_guid
);
897 vhdx_update_headers(bs
, s
, false, &new_guid
);
899 /* currently, we require that the log be flushed after
905 /* 0 is an invalid sequence number, but may also represent the first
906 * log write (or a wrapped seq) */
907 if (s
->log
.sequence
== 0) {
911 sector_offset
= offset
% VHDX_LOG_SECTOR_SIZE
;
912 file_offset
= QEMU_ALIGN_DOWN(offset
, VHDX_LOG_SECTOR_SIZE
);
914 aligned_length
= length
;
916 /* add in the unaligned head and tail bytes */
918 leading_length
= (VHDX_LOG_SECTOR_SIZE
- sector_offset
);
919 leading_length
= leading_length
> length
? length
: leading_length
;
920 aligned_length
-= leading_length
;
924 sectors
= aligned_length
/ VHDX_LOG_SECTOR_SIZE
;
925 trailing_length
= aligned_length
- (sectors
* VHDX_LOG_SECTOR_SIZE
);
926 if (trailing_length
) {
930 sectors
+= partial_sectors
;
932 file_length
= bdrv_co_getlength(bs
->file
->bs
);
933 if (file_length
< 0) {
938 /* sectors is now how many sectors the data itself takes, not
939 * including the header and descriptor metadata */
941 new_hdr
= (VHDXLogEntryHeader
) {
942 .signature
= VHDX_LOG_SIGNATURE
,
944 .sequence_number
= s
->log
.sequence
,
945 .descriptor_count
= sectors
,
947 .flushed_file_offset
= file_length
,
948 .last_file_offset
= file_length
,
949 .log_guid
= header
->log_guid
,
953 desc_sectors
= vhdx_compute_desc_sectors(new_hdr
.descriptor_count
);
955 total_length
= (desc_sectors
+ sectors
) * VHDX_LOG_SECTOR_SIZE
;
956 new_hdr
.entry_length
= total_length
;
958 vhdx_log_entry_hdr_le_export(&new_hdr
);
960 buffer
= qemu_blockalign(bs
, total_length
);
961 memcpy(buffer
, &new_hdr
, sizeof(new_hdr
));
963 new_desc
= buffer
+ sizeof(new_hdr
);
964 data_sector
= buffer
+ (desc_sectors
* VHDX_LOG_SECTOR_SIZE
);
967 /* All log sectors are 4KB, so for any partial sectors we must
968 * merge the data with preexisting data from the final file
970 merged_sector
= qemu_blockalign(bs
, VHDX_LOG_SECTOR_SIZE
);
972 for (i
= 0; i
< sectors
; i
++) {
973 new_desc
->signature
= VHDX_LOG_DESC_SIGNATURE
;
974 new_desc
->sequence_number
= s
->log
.sequence
;
975 new_desc
->file_offset
= file_offset
;
977 if (i
== 0 && leading_length
) {
978 /* partial sector at the front of the buffer */
979 ret
= bdrv_co_pread(bs
->file
, file_offset
, VHDX_LOG_SECTOR_SIZE
,
984 memcpy(merged_sector
+ sector_offset
, data_tmp
, leading_length
);
985 bytes_written
= leading_length
;
986 sector_write
= merged_sector
;
987 } else if (i
== sectors
- 1 && trailing_length
) {
988 /* partial sector at the end of the buffer */
989 ret
= bdrv_co_pread(bs
->file
, file_offset
+ trailing_length
,
990 VHDX_LOG_SECTOR_SIZE
- trailing_length
,
991 merged_sector
+ trailing_length
, 0);
995 memcpy(merged_sector
, data_tmp
, trailing_length
);
996 bytes_written
= trailing_length
;
997 sector_write
= merged_sector
;
999 bytes_written
= VHDX_LOG_SECTOR_SIZE
;
1000 sector_write
= data_tmp
;
1003 /* populate the raw sector data into the proper structures,
1004 * as well as update the descriptor, and convert to proper
1006 vhdx_log_raw_to_le_sector(new_desc
, data_sector
, sector_write
,
1009 data_tmp
+= bytes_written
;
1012 file_offset
+= VHDX_LOG_SECTOR_SIZE
;
1015 /* checksum covers entire entry, from the log header through the
1016 * last data sector */
1017 vhdx_update_checksum(buffer
, total_length
,
1018 offsetof(VHDXLogEntryHeader
, checksum
));
1020 /* now write to the log */
1021 ret
= vhdx_log_write_sectors(bs
, &s
->log
, §ors_written
, buffer
,
1022 desc_sectors
+ sectors
);
1027 if (sectors_written
!= desc_sectors
+ sectors
) {
1028 /* instead of failing, we could flush the log here */
1034 /* write new tail */
1035 s
->log
.tail
= s
->log
.write
;
1039 qemu_vfree(merged_sector
);
1043 /* Perform a log write, and then immediately flush the entire log */
1045 vhdx_log_write_and_flush(BlockDriverState
*bs
, BDRVVHDXState
*s
,
1046 void *data
, uint32_t length
, uint64_t offset
)
1049 VHDXLogSequence logs
= { .valid
= true,
1054 /* Make sure data written (new and/or changed blocks) is stable
1055 * on disk, before creating log entry */
1056 ret
= bdrv_co_flush(bs
);
1061 ret
= vhdx_log_write(bs
, s
, data
, length
, offset
);
1067 /* Make sure log is stable on disk */
1068 ret
= bdrv_co_flush(bs
);
1073 ret
= vhdx_log_flush(bs
, s
, &logs
);