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. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs index b36d24382cc3..2b204b0ed49a 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,14 +7,27 @@ use crate::{ bindings, - block::mq::{Operations, TagSet}, - error::{self, from_err_ptr, Result}, - fmt::{self, Write}, + block::mq::{ + Operations, + TagSet, // + }, + error::{ + self, + from_err_ptr, + Result, // + }, + fmt::{ + self, + Write, // + }, prelude::*, static_lock_class, str::NullTerminatedFormatter, sync::Arc, - types::{ForeignOwnable, ScopeGuard}, + types::{ + ForeignOwnable, + ScopeGuard, // + }, }; /// A builder for [`GenDisk`]. @@ -25,6 +38,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 +48,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 +109,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( self, @@ -111,6 +136,7 @@ pub fn build( 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