Sparse VHDX leaks host space: vhdmp drops file-level TRIM when a request crosses an unmapped block
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- linux
- Domain
- operating-systems
Research direction
The report implicates Windows vhdmp.sys paths Vhd2iContinueFileTrim, VhdmpiVhd2HandleFileTrimSubIo, and VhdmpiVhd2ProcessSingleSrbRange; it does not identify WSL source files or tests for these components. Start by checking whether the suspected trim handling is in code maintained by this repository and reproduce with the listed sparse VHDX steps. Done means the full deleted extent is released to the host for both default and one-block discard limits.
Written by the indexing model from the issue text.
Description
Windows Version
Microsoft Windows [Version 10.0.20348.5761] (Windows Server 2022, vhdmp.sys 10.0.20348.5756)
WSL Version
3.0.3.0
Are you using WSL 1, WSL 2, or WSLC?
- WSL 2
- WSL 1
- WSLC
Kernel Version
6.18.54.1-microsoft-standard-WSL2
Distro Version
Ubuntu 24.04.5 LTS
Other Software
None relevant.
Repro Steps
Summary: on a sparse VHDX, part of every guest TRIM is never passed to NTFS. The blocks become UNMAPPED in the BAT but keep their host clusters, and later TRIMs do not recover them. This looks like a bug in how vhdmp.sys handles a TRIM that spans both present and already-unmapped blocks, and it may explain reports such as #12103.
Setup: wsl --manage <distro> --set-sparse true --allow-unsafe. The root filesystem is ext4 mounted with discard. The VHDX block size and the guest discard_granularity are both 1 MiB.
-
On the host, measure the size on disk (the file's reported size does not change):
Add-Type -Namespace W -Name K -MemberDefinition '[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern uint GetCompressedFileSizeW(string n, out uint hi);' $hi = 0; $lo = [W.K]::GetCompressedFileSizeW('<path>\ext4.vhdx', [ref]$hi) '{0:N1} MiB on disk' -f ((([uint64]$hi -shl 32) + $lo) / 1MB) -
In the distro, as root, write a file, then measure on the host:
dd if=/dev/urandom of=/root/trimtest.bin bs=1M count=256 conv=fsync status=none; sync -
Delete it, then measure on the host again:
rm -f /root/trimtest.bin; sync; sync; sleep 6; sync -
Repeat steps 2 and 3, but before deleting, limit each UNMAP to one VHDX block:
echo 1048576 > /sys/block/<root device>/queue/discard_max_bytesThis value cannot be written back: the kernel rejects both the original
4294966784and 4 GiB.wsl --shutdownrestores it.
Expected Behavior
Deleting the file returns about 256 MiB to the host, whatever the discard_max_bytes value.
Actual Behavior
| Round | discard_max_bytes |
Size on disk released by the delete |
|---|---|---|
| A | default (4294966784) |
187.9 MiB of 256 MiB |
| B | 1048576 |
256.0 MiB |
| C | default, after wsl --shutdown |
187.9 MiB of 256 MiB |
The amount lost depends on the free-space layout. Over a longer period, this disk accumulated 137.7 GiB of UNMAPPED blocks that still held clusters, in a file using 528.3 GiB on disk while the guest used 356.7 GiB. Some of that may predate the current sparse setup (see Limits below).
Suspected cause
From traces of the guest requests and static analysis of vhdmp.sys with public symbols:
-
The guest TRIM reaches past the freed extent. ext4 extends it to the end of the free run, for online
discardandfstrimalike. For a 40 MiB extent at 9752 MiB,block_rq_issueshows19972096 + 84416sectors, which is[9752, 9793.2)MiB. The blocks after the extent are usually already unmapped. -
Vhd2iContinueFileTrimforgets the pending run at a non-present block. Present blocks (state 6 or 7) are accumulated byVhd2iExtendRangeinto a file-contiguous run. Other blocks take this path, which moves the start of the pending run without callingVhd2iDispatchRange:vhdmp!Vhd2iContinueFileTrim+0xa1b4: add dword ptr [rbx+1C8h],edi ; advance the position in the request mov edx,dword ptr [rbx+1C8h] mov dword ptr [rbx+1FCh],edx ; start of pending run = current position -
Queued runs can be lost too.
VhdmpiVhd2HandleFileTrimSubIoholds runs until it has 32 or receives the final dispatch. If the request ends on a non-present block,VhdmpiVhd2ProcessSingleSrbRangehas nothing pending, so it makes no final dispatch and the queued runs are not sent. -
The loss is permanent and silent. The file trim still completes successfully, so
VhdmpiVhd2SrbRangeCompletegoes on to the block trim and the BAT entries becomeUNMAPPED. Later TRIMs skip those blocks because they are no longer present.
Evidence
- The on/off comparison above is repeatable and reversible.
- Applying rules 2 and 3 to traced requests predicted the outcome of 1021 of 1023 blocks across four file layouts. The two mismatches are unexplained.
- With WSL stopped,
FSCTL_FILE_LEVEL_TRIMsent from user mode to these stale ranges released their clusters immediately, so NTFS can free them. Its conditions inNtfsFileLevelTrim(sparse attribute, alignment, length) also do not depend on position. - Serial
fstrimand onlinediscardlose the same blocks, and runningfstrimagain over them recovers nothing.
Limits of this analysis
- I did not trace the host-side FSCTLs, so steps 2 to 4 are inferred from the disassembly and the prediction above. The field meanings are my interpretation.
- I tested one Windows build. I did not check other builds or a freshly created distro.
- This VHDX is not pristine. In October 2026 it failed to attach, had 27 inconsistent BAT entries repaired by hand, and was rewritten and made sparse again. Stale ranges were later released by hand with
FSCTL_SET_ZERO_DATA. The test blocks above are freshly written each round, so this should not affect the repro, but it may account for part of the 137.7 GiB.
Suggested fix (untested)
In the skip path of Vhd2iContinueFileTrim, dispatch the pending run before moving its start, and make sure queued runs are flushed when a request ends on a non-present block.
Workaround
Set discard_max_bytes to 1048576 in the guest, so each UNMAP covers at most one block. A 268 MiB TRIM took about 174 ms instead of about 23 ms. The setting does not persist across a VM restart, and I have not measured its effect on foreground I/O.
Related: #12103, #13241.
Diagnostic Logs
No log archive attached. Microsoft-Windows-VHDMP-Operational and Microsoft-Windows-Ntfs/Operational recorded no warnings or errors on the day of these tests.
- Dominant language
- C++
- Stars
- 34.4k
- Forks
- 1.9k
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 166
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/WSL
-
feature wsl2
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
microsoft/WSL#42306 · 3 comments ·
Maintainers usually reply within 1 day
-
feature
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
microsoft/WSL#42114 · 3 comments ·
Maintainers usually reply within 1 day
-
feature kernel wsl2
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoft/WSL#41107 · 3 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
feature kernel
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
microsoft/WSL#41054 · 9 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
feature GPU
Difficulty 1/5 1-3 hours Newbie friendliness 70/100
microsoft/WSL#13592 · 4 comments ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
nextcloud/passman-android#214 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
2026.11.0 fails to install on Intel macOS: requirements.txt pins cryptography==50.0.2 for Darwin x86_64May be free again A pull request for this issue was closed without being merged. Open
Difficulty 1/5 Under an hour Newbie friendliness 87/100
esphome/esphome#22892 · 1 comment ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
distante/esphome-components#50 · 1 comment ·
Maintainers usually reply within 1 day
-
arch-based hyprland
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
noctalia-dev/noctalia#5423 ·
Maintainers usually reply within 1 day