disk_blockdevice: support discard for file-backed disks - #4088
Merged
Conversation
File-backed disks previously ignored guest UNMAP requests entirely: the only discard path was the BLKDISCARD ioctl, which is valid only on block devices, so regular files reported no discard support and silently dropped every unmap. Service discard on files by punching a hole with fallocate (PUNCH_HOLE | KEEP_SIZE), submitted asynchronously through io-uring so it sits on the same completion path as reads, writes, and flush rather than blocking a worker thread. Capability is detected at open time with a non-destructive probe that punches a zero-length-effect hole past the end of the file; filesystems that cannot punch holes report no discard support and continue to treat unmap as a no-op. Because a punched hole deterministically reads back as zero, file-backed disks now report UnmapBehavior::Zeroes, letting the guest rely on unmapped ranges being zero. To preserve that guarantee, any failure of the file punch-hole path is propagated as an error rather than swallowed. The block device path and its behavior are unchanged.
|
This PR modifies files containing For more on why we check whole files, instead of just diffs, check out the Rustonomicon |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds discard/UNMAP support for file-backed block devices by using hole-punching (fallocate(PUNCH_HOLE|KEEP_SIZE)) submitted via io-uring, and updates capability reporting so guests can rely on unmapped ranges reading back as zero when supported.
Changes:
- Detect and advertise discard support for regular files when hole punching is available and io-uring supports
IORING_OP_FALLOCATE. - Implement file-backed
unmapvia io-uring fallocate hole-punch, and reportUnmapBehavior::Zeroeswhen enabled. - Add an async test covering file-backed unmap semantics; wire in
test_with_tracingfor async tests.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| vm/devices/storage/disk_blockdevice/src/lib.rs | Add hole-punch probing, enable discard granularity for files, implement io-uring fallocate-based unmap, and add a test validating zero-readback behavior. |
| vm/devices/storage/disk_blockdevice/Cargo.toml | Add test_with_tracing to dev-dependencies for async tests. |
| Cargo.lock | Lockfile update for the new dev-dependency usage. |
sprt
approved these changes
Jul 30, 2026
sprt
left a comment
Contributor
There was a problem hiding this comment.
I can't test this e2e yet but code lgtm.
smalis-msft
approved these changes
Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
File-backed disks previously ignored guest UNMAP requests entirely: the only discard path was the BLKDISCARD ioctl, which is valid only on block devices, so regular files reported no discard support and silently dropped every unmap.
Service discard on files by punching a hole with fallocate (PUNCH_HOLE | KEEP_SIZE), submitted asynchronously through io-uring so it sits on the same completion path as reads, writes, and flush rather than blocking a worker thread. Capability is detected at open time with a non-destructive probe that punches a zero-length-effect hole past the end of the file; filesystems that cannot punch holes report no discard support and continue to treat unmap as a no-op.
Because a punched hole deterministically reads back as zero, file-backed disks now report UnmapBehavior::Zeroes, letting the guest rely on unmapped ranges being zero. To preserve that guarantee, any failure of the file punch-hole path is propagated as an error rather than swallowed. The block device path and its behavior are unchanged.