Skip to content

disk_blockdevice: support discard for file-backed disks - #4088

Merged
jstarks merged 1 commit into
microsoft:mainfrom
jstarks:discard_file
Jul 30, 2026
Merged

disk_blockdevice: support discard for file-backed disks#4088
jstarks merged 1 commit into
microsoft:mainfrom
jstarks:discard_file

Conversation

@jstarks

@jstarks jstarks commented Jul 30, 2026

Copy link
Copy Markdown
Member

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.

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.
@jstarks
jstarks requested a review from a team as a code owner July 30, 2026 12:25
Copilot AI review requested due to automatic review settings July 30, 2026 12:25
@jstarks
jstarks requested a review from a team as a code owner July 30, 2026 12:25
@github-actions github-actions Bot added the unsafe Related to unsafe code label Jul 30, 2026
@github-actions

Copy link
Copy Markdown

⚠️ Unsafe Code Detected

This PR modifies files containing unsafe Rust code. Extra scrutiny is required during review.

For more on why we check whole files, instead of just diffs, check out the Rustonomicon

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 unmap via io-uring fallocate hole-punch, and report UnmapBehavior::Zeroes when enabled.
  • Add an async test covering file-backed unmap semantics; wire in test_with_tracing for 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.

Comment thread vm/devices/storage/disk_blockdevice/src/lib.rs

@sprt sprt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't test this e2e yet but code lgtm.

@jstarks
jstarks merged commit d8c1f25 into microsoft:main Jul 30, 2026
69 checks passed
@jstarks
jstarks deleted the discard_file branch July 30, 2026 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

unsafe Related to unsafe code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants