The block allocation table (BAT) is a region of 64-bit little-endian entries, found through the region table. It says where each payload block (a block of the virtual disk) is in the file, and, for differencing disks, where each sector bitmap block is.
Entries
| Bits | Name | Meaning |
|---|---|---|
| 0–2 | State | See states. |
| 3–19 | Reserved | 0. |
| 20–63 | FileOffsetMB | Where the block is, in megabytes from the start of the file. |
So the byte offset of a block is the entry with its low 20 bits cleared:
entry & 0xFFFFFFFFFFF00000.
States
| Value | Payload block | Meaning |
|---|---|---|
| 0 | PAYLOAD_BLOCK_NOT_PRESENT | The default for new blocks of dynamic and differencing disks. In a fixed or dynamic disk the contents are undefined (a reader may return arbitrary data, zeros or the block’s earlier contents); in a differencing disk they are in the parent. The file offset is reserved. |
| 1 | PAYLOAD_BLOCK_UNDEFINED | Contents not defined in the file; reads may return arbitrary data, zeros or the earlier contents. The file offset may be kept, zeroed, or point to the earlier contents. |
| 2 | PAYLOAD_BLOCK_ZERO | The block reads as zeros. The file offset is reserved. |
| 3 | PAYLOAD_BLOCK_UNMAPPED | Every sector of the block was unmapped (trimmed); reads return zeros or the block’s contents from before the unmap. |
| 4 | – | Reserved. |
| 5 | – | Reserved in revision 8.0. QEMU names it PAYLOAD_BLOCK_UNMAPPED_v095: the draft 0.95 of the specification used 5 for unmapped, and QEMU reads it as zeros. The explorer shows it as “unmapped (v0.95 draft)”, reads it like state 3 and does not report it as a problem. |
| 6 | PAYLOAD_BLOCK_FULLY_PRESENT | The whole block is in the file at the given offset (the default for every block of a fixed disk). A differencing disk’s sector bitmap is not consulted. |
| 7 | PAYLOAD_BLOCK_PARTIALLY_PRESENT | Differencing disks only: the block is at the given offset, but each sector is read from here or from the parent as the sector bitmap says; that bitmap block must be present. |
| Value | Sector bitmap block | Meaning |
|---|---|---|
| 0 | SB_BLOCK_NOT_PRESENT | No bitmap in the file. Every sector bitmap entry of a fixed or dynamic disk is in this state. |
| 6 | SB_BLOCK_PRESENT | The bitmap is in the file at the given offset; required in a differencing disk when any payload block of the chunk is partially present. |
Sector bitmap values 1–5 and 7 are reserved. (Sources: [MS-VHDX] revision 8.0,
sections 2.4 and 2.5; QEMU block/vhdx.h for state 5.) State 7 and a present sector bitmap
belong to differencing disks; the explorer reports them in a fixed or dynamic disk.
Blocks that are not present: two readings
For a payload block in state 0 (in a fixed or dynamic disk), 1 (undefined), 3 (unmapped) and the draft state 5, [MS-VHDX] 2.5.1.1 lets a reader return either zeros or the contents the block had before it was released, which a writer may have left at the block’s file offset. The bytes of the file cannot say which one a program expects, so the explorer offers both in the Blocks not present option at the top of the viewer:
- Read as zeros (the default): what QEMU’s read path in
block/vhdx.cdoes: states 0, 1, 2, 3 and 5 fall into one case that fills the buffer with zeros (“return zero”), and it refuses state 7 because it does not support differencing disks. - Read what the block held before: the block is read from its FileOffsetMB when that is not zero and the whole block lies inside the file; otherwise zeros.
State 2 (zero) always reads as zeros, and in a differencing disk state 0 always means the parent. (Sources:
[MS-VHDX] revision 8.0, section 2.5.1.1; QEMU block/vhdx.c.)
Reading the virtual disk
The explorer builds the virtual disk from the payload blocks: a block that is fully present is read from its file offset; a partially present block sector by sector through the sector bitmap of its chunk; a zero, undefined, unmapped or reserved block reads as zeros (or, with the option described below, as what it held before); and a block that is not present reads as zeros too, as does anything a differencing disk leaves to its parent, because the parent is another file that is not opened (the table marks those as “parent unavailable”). The disk is offered as a file of its own for the partition and file system viewers.
Chunks and sector bitmaps
Payload entries are grouped in chunks, and every chunk is followed by one sector bitmap entry. A sector bitmap block is 1 MiB, one bit per logical sector, so it covers 223 sectors. The chunk ratio is how many payload blocks that is:
chunk ratio = 223 × logical sector size ÷ block size
For 32 MiB blocks and 512-byte sectors the ratio is 128: BAT entries 0 to 127 are payload blocks 0 to 127, entry 128 is the sector bitmap for them, entries 129 to 256 are payload blocks 128 to 255, and so on. The payload block b is entry b + b ÷ ratio (dropping the remainder), and its sector bitmap is entry (b ÷ ratio + 1) × (ratio + 1) − 1.
In a sector bitmap, bit i (least significant bit of each byte first) stands for sector i of the chunk: 1 means the sector is in this file, 0 that it must be read from the parent.
Fixed and dynamic disks have the same interleaved layout, with every sector bitmap entry not present; keeping the entries avoids rewriting the table when converting a disk between dynamic and differencing. In a fixed or dynamic disk the last entry locates the last payload block; in a differencing disk the table must reach the sector bitmap entry of the chunk holding the last payload block. The formulas are in the next section.
Size of the table and where blocks may be
Source: [MS-VHDX] revision 8.0, sections 2.4, 2.5 and 2.5.1 and appendix
notes 6 to 11. The formulas are pictures in the specification’s web page; they were read
from the page image of the PDF (page 22) and checked against QEMU’s
block/vhdx.c (vhdx_calc_bat_entries,
chunk_ratio, with VHDX_MAX_SECTORS_PER_BLOCK = 1 << 23
in vhdx.h). Ceil is rounding up, Fl rounding down.
| Quantity | Formula |
|---|---|
| Payload blocks count | Ceil(VirtualDiskSize ÷ BlockSize) |
| Chunk ratio | 223 × LogicalSectorSize ÷ BlockSize (this is the chunk size divided by the block size; the specification defines the terms in 2.4 and QEMU computes it this way) |
| Sector bitmap blocks count | Ceil(Payload blocks count ÷ Chunk ratio) |
| Total BAT entries, fixed or dynamic disk | Payload blocks count + Fl((Payload blocks count − 1) ÷ Chunk ratio) |
| Total BAT entries, differencing disk | Sector bitmap blocks count × (Chunk ratio + 1) |
Example: a 10 GiB disk with 32 MiB blocks and 512-byte sectors has 320 payload blocks and a chunk ratio of 128, so 3 sector bitmap blocks; the table needs 320 + Fl(319 ÷ 128) = 322 entries when fixed or dynamic (the last entry is payload block 319, and only two sector bitmap entries lie before it), and 3 × 129 = 387 when differencing (the last entry is the third sector bitmap entry).
The BAT region must be at least large enough for that many entries. (The specification does not say that a fixed disk may omit the sector bitmap entries; they stay in the table, as above.)
Where blocks may be
- A payload or sector bitmap block must lie after the header section (the first 1 MB) and must not overlap any other structure.
- FileOffsetMB must be unique across all BAT entries when it is not zero: two entries never point at the same megabyte.
- All file objects are 1 MB aligned and non-overlapping (2.1); a block’s offset is a whole number of megabytes by construction. A payload block is BlockSize long (1 MB to 256 MB, a power of 2, from the File Parameters item) and a sector bitmap block is always 1 MB.
- Regions in the region table must not overlap the blocks either: the region table’s non-overlap rule covers “the payload and sector bitmap blocks (defined in the BAT)”.
Fixed disks: which states a file may hold and a writer may set
The specification’s table of validity marks PAYLOAD_BLOCK_NOT_PRESENT,
PAYLOAD_BLOCK_UNDEFINED, PAYLOAD_BLOCK_ZERO and
PAYLOAD_BLOCK_UNMAPPED as valid for a fixed disk with a footnote each
(notes 6 to 9), and PAYLOAD_BLOCK_FULLY_PRESENT as plainly valid.
PAYLOAD_BLOCK_PARTIALLY_PRESENT is not valid in a fixed or dynamic disk.
The notes say:
- (note 6) A reader may accept a fixed disk with blocks in the not-present state, but an implementation cannot transition blocks of a fixed disk to that state. The text of the section states that the behaviour of the states in a fixed disk is the same as in a dynamic disk.
- (notes 7 to 9) For undefined, zero and unmapped blocks, an implementation that chooses to preserve sector stability must keep returning the same data on later reads while the block stays in the state; to preserve it across implementations the block should be moved to a state with tightly defined reads.
- The default state for all blocks of a fixed disk is fully present.
Read behaviours in each state beyond the table above (each specification’s “implement
one of the following”): in PAYLOAD_BLOCK_UNDEFINED a reader may return
arbitrary data, zeros or the earlier contents; PAYLOAD_BLOCK_ZERO must read
zeros; PAYLOAD_BLOCK_UNMAPPED may return zeros or the earlier contents
(note 11 warns that returning earlier data elsewhere on the disk could leak private
data). When a block moves to undefined or unmapped, FileOffsetMB may be left unchanged,
set to zero, or set to a non-zero offset where the block’s earlier contents (possibly
partly zeroed) still are.
Sector bitmap entries: in a fixed or dynamic disk every one must be not present; in a differencing disk one must be present if any payload block of its chunk is partially present.