Skip to content

feat(virtio-block): Add support for VIRTIO_BLK_T_DISCARD request type #5168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

LDagnachew
Copy link

(Reupload) Opening this as a Draft PR, as I am still working on fully integrating unmap/fstrim/discard. Right now I'm currently working on the functionality.

Changes

Expose discard feature bit

  • Added VIRTIO_BLK_F_DISCARD to the device’s advertised avail_features so guests can negotiate discard support

Extend RequestType enum

  • Introduced a new Discard variant and mapped VIRTIO_BLK_T_DISCARDRequestType::Discard in the request conversion

Update request parsing (Request::parse) in request.rs

  • Allowed Discard (alongside Flush) to skip the data descriptor
  • Enforced sector‑aligned length and bounds checks for Discard requests

Implement handle_discard backend

  • Created handle_discard(offset, len) to call fallocate() on the host file descriptor

Reason

Attempting to resolve Issue #2708

Next Steps

  • Checking for VIRTIO_BLK_F_DISCARD support (need suggestions for this, not sure of an efficient way to check for Discard Support within a given VirtIO block).
  • Writing Unit & Integration Tests.

License Acceptance

By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache 2.0 license. For more information on following Developer
Certificate of Origin and signing off your commits, please check
CONTRIBUTING.md.

PR Checklist

  • I have read and understand CONTRIBUTING.md.
  • I have run tools/devtool checkstyle to verify that the PR passes the
    automated style checks.
  • I have described what is done in these changes, why they are needed, and
    how they are solving the problem in a clear and encompassing way.
  • I have updated any relevant documentation (both in code and in the docs)
    in the PR.
  • I have mentioned all user-facing changes in CHANGELOG.md.
  • If a specific issue led to this PR, this PR closes the issue.
  • When making API changes, I have followed the
    Runbook for Firecracker API changes.
  • I have tested all new and changed functionalities in unit tests and/or
    integration tests.
  • I have linked an issue to every new TODO.

  • This functionality cannot be added in rust-vmm.

- Implemented parsing and validation for `VIRTIO_BLK_T_DISCARD` requests.
- Added `SyncIoError::Discard` to represent errors specific to discard operations.
- Updated `Request::process` to handle discard requests using `handle_discard` in `FileEngine`.
- Integrated discard metrics (`discard_count` and `invalid_reqs_count`) for successful and invalid discard operations.

Signed-off-by: Leul Dagnachew <[email protected]>
Signed-off-by: LDagnachew <[email protected]>
@LDagnachew
Copy link
Author

LDagnachew commented Apr 24, 2025

As far as checking whether the system even supports fstrim/unmap/discard, a trivial way would be to simply check whether fallocate() returns an error, however not entirely sure if there's a better way to do this. @ShadowCurse

@LDagnachew LDagnachew marked this pull request as ready for review April 24, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant