[PATCH 19/79] block: rust: mq: add max_hw_discard_sectors support to GenDiskBuilder

Andreas Hindborg
2026-02-15T23:42:18Z

Add support for configuring the maximum hardware discard sectors
through GenDiskBuilder. This allows block devices to specify their
discard/trim capabilities.

Setting this value to 0 (the default) indicates that discard is not
supported by the device. Non-zero values specify the maximum number
of sectors that can be discarded in a single operation.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
rust/kernel/block/mq/gen_disk.rs | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index 1ce815c8cdab0..75968d6a57639 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -25,6 +25,7 @@ pub struct GenDiskBuilder {
logical_block_size: u32,
physical_block_size: u32,
capacity_sectors: u64,
+ max_hw_discard_sectors: u32,
}

impl Default for GenDiskBuilder {
@@ -34,6 +35,7 @@ fn default() -> Self {
logical_block_size: bindings::PAGE_SIZE as u32,
physical_block_size: bindings::PAGE_SIZE as u32,
capacity_sectors: 0,
+ max_hw_discard_sectors: 0,
}
}
}
@@ -94,6 +96,16 @@ pub fn capacity_sectors(mut self, capacity: u64) -> Self {
self
}

+ /// Set the maximum amount of sectors the underlying hardware device can
+ /// discard/trim in a single operation.
+ ///
+ /// Setting 0 (default) here will cause the disk to report discard not
+ /// supported.
+ pub fn max_hw_discard_sectors(mut self, max_hw_discard_sectors: u32) -> Self {
+ self.max_hw_discard_sectors = max_hw_discard_sectors;
+ self
+ }
+
/// Build a new `GenDisk` and add it to the VFS.
pub fn build<T: Operations>(
self,
@@ -112,6 +124,7 @@ pub fn build<T: Operations>(

lim.logical_block_size = self.logical_block_size;
lim.physical_block_size = self.physical_block_size;
+ lim.max_hw_discard_sectors = self.max_hw_discard_sectors;
if self.rotational {
lim.features = bindings::BLK_FEAT_ROTATIONAL;
}

--
2.51.2