Before a writer changes the BAT or the metadata, it writes the change to the log, so that after a crash the change can be completed (replayed) or ignored, and the file is never left half-updated. Guest data itself is not logged. The log is a circular buffer at the place the header gives, a whole number of megabytes long, made of 4 KiB sectors. All numbers are little-endian.
Log entry header
This page follows [MS-VHDX] revision 8.0, section 2.3. Each log entry is a run of 4 KiB sectors, aligned to 4 KiB: one or more descriptor sectors, then one data sector per data descriptor. The first descriptor sector holds the 64-byte entry header and up to 126 descriptors of 32 bytes; further descriptor sectors hold up to 128.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
0 | 4 | text | Signature | loge. |
4 | 4 | uint32 | Checksum | CRC-32C over the whole entry (as long as the entry length says), computed with this field as 0. |
8 | 4 | uint32 | Entry length | Bytes in the entry, a multiple of 4 KiB. |
12 | 4 | uint32 | Tail | Offset from the start of the log of the first entry of the sequence that ends with this entry; a multiple of 4 KiB. It may point to the entry itself. |
16 | 8 | uint64 | Sequence number | Raised by one for each entry; greater than 0. |
24 | 4 | uint32 | Descriptor count | Number of descriptors in the entry; may be 0. |
28 | 4 | – | Reserved | 0. |
32 | 16 | GUID | Log GUID | The header’s log GUID when the entry was written; if it differs from the header’s now, the entry is not valid. |
48 | 8 | uint64 | Flushed file offset | A file size, in bytes, that the file was known to have reached stably when the entry was written; a multiple of 1 MiB. A shorter file means it was truncated. |
56 | 8 | uint64 | Last file offset | A size, in bytes, that all the file’s structures fit in; a multiple of 1 MiB. After replay the file is extended to at least this size. |
Descriptors
32 bytes each, straight after the entry header. A zero descriptor says “write zeros here”; a data descriptor says “write this data sector here”.
| Offset | Size | Zero descriptor | Data descriptor |
|---|---|---|---|
0 | 4 | Signature zero | Signature desc |
4 | 4 | Reserved, 0 | Trailing bytes: the last 4 bytes of the 4 KiB sector to write (bytes 4092–4095) |
8 | 8 | Zero length: bytes to zero, a multiple of 4 KiB | Leading bytes: the first 8 bytes of the sector to write (bytes 0–7) |
16 | 8 | File offset to start zeroing, a multiple of 4 KiB | File offset to write the sector to, a multiple of 4 KiB |
24 | 8 | Sequence number, equal to the entry’s | Sequence number, equal to the entry’s |
Data sectors
One 4 KiB sector per data descriptor, in the same order. Its first 8 and last 4 bytes are replaced by markers, which is why the descriptor keeps the real bytes.
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
0 | 4 | text | Signature | data. |
4 | 4 | uint32 | Sequence high | The high 32 bits of the entry’s sequence number. |
8 | 4084 | bytes | Data | Bytes 8 to 4091 of the sector to write. |
4092 | 4 | uint32 | Sequence low | The low 32 bits of the sequence number. |
Replaying the log
When the log is not empty (the header’s log GUID is not zero), a reader must replay it before any other access; one that cannot must not open the file. Replay uses the active sequence: the newest valid and complete run of entries. A sequence is valid when every entry checks (signature, checksum, descriptors, log GUID), each entry after the tail has a sequence number one greater than the one before, and the head entry’s tail lies within the sequence. The specification scans the log from offset 0 in 4 KiB steps, building candidate sequences and keeping the one whose head has the highest sequence number. If none is found the file is corrupt; if the file is shorter than the head entry’s flushed file offset, it has been truncated. Otherwise every descriptor of every entry, from the tail entry on, is applied in order (writing zeros or the restored data sector, extending the file if needed), and the file is then extended to at least the head entry’s last file offset.
For every valid entry the explorer also checks that the tail lies inside the log, that the entry header’s reserved field (offset 28) and the reserved field of each zero descriptor (offset 4) are 0, and lists any that are not in the Check row. Such an entry stays valid for the replay: the specification does not make these fields a reason to reject it.
In the explorer the replay is done in memory over the stored bytes, and nothing is written. A data descriptor’s sector is made of its 8 leading bytes (sector bytes 0 to 7), the data sector’s bytes 8 to 4091 and the descriptor’s 4 trailing bytes (sector bytes 4092 to 4095). The metadata region, the block allocation table and the virtual disk are then read from the replayed state, and the Replayed changes table lists each descriptor with the offset it writes, what lies there (header section, region, log, payload block or sector bitmap block) and how many bytes it changes. A file that is shorter than the head entry’s flushed file offset, or whose log has no valid active sequence, is not replayed, and the reason is reported.
Writing through the log
Rules for a writer, all from [MS-VHDX] revision 8.0, section 2.3, 2.3.1 and 2.3.1.1 (the PDF of the specification, read as text):
- What goes through the log. The log MUST be used for every update to metadata except the headers, and MUST NOT be used for updates to payload blocks. The headers cannot use it because they locate it (2.2.2), so they have their own two-copy procedure. Region table updates also go through the log (2.2.3).
- Update order. To change a metadata structure: (1) write all the updates to the log; (2) flush the log so they are stable on the host disk; (3) apply the updates to their final places in the file; (4) flush those so they are stable. Updates that a power failure kept from reaching their final place are found, validated and replayed from the log at the next open.
- Log GUID. A zero Log GUID in the header means the log is empty or holds no valid entry and MUST NOT be replayed; otherwise only entries carrying that GUID in their header are valid. On open, before overwriting any existing space in the log region, a writer MUST change the header’s Log GUID to a new non-zero value. (The entry’s own Log GUID field is the header’s value at the time the entry was written, and a note in the specification explains the point: a complete but older entry must not be replayed on the strength of the sequence number alone.)
- Reusing log space. The space of log entries may be reused for later log writes only when those entries are not part of the active log sequence.
- Shrinking the file. Before shrinking the file while the log is in use, a writer MUST write the target size to a log entry (its Flushed file offset) and flush the entry, so that the file is not taken for truncated during a later replay. It SHOULD write the largest value that satisfies the Flushed file offset requirement (a size the file is certain to have reached on the host disk) and the smallest value for Last file offset (a size all allocated structures fit in). Both are multiples of 1 MB.
- Entry fields on writing. Each descriptor and data sector repeats the entry’s sequence number (the data sector splits it between its first and last four bytes), the entry has a CRC-32C over its whole length, and the entry’s alignment is at least 4 KB, possibly more where writes have to be isolated on the host disk; a reader must be ready to replay entries with 4 KB or smaller alignment (specification note 3).