[PATCH 46/79] block: rust: move gendisk vtable construction to separate function

Andreas Hindborg
2026-02-15T23:45:01Z

Refactor the `GenDiskBuilder::build` method to move the `gendisk`
vtable construction into a separate helper function. This prepares for
adding zoned block device support which requires conditional vtable
setup.

Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
---
drivers/block/rnull/configfs.rs | 5 ++-
rust/kernel/block/mq/gen_disk.rs | 66 ++++++++++++++++++++++------------------
2 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs.rs
index 90f2ecb454285..5dfe7b48af63b 100644
--- a/drivers/block/rnull/configfs.rs
+++ b/drivers/block/rnull/configfs.rs
@@ -266,7 +266,10 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result {
}
}

-configfs_simple_field!(DeviceConfig, 1, block_size, u32, check GenDiskBuilder::validate_block_size);
+configfs_simple_field!(DeviceConfig, 1,
+ block_size, u32,
+ check GenDiskBuilder::<NullBlkDevice>::validate_block_size
+);
configfs_simple_bool_field!(DeviceConfig, 2, rotational);
configfs_simple_field!(DeviceConfig, 3, capacity_mib, u64);
configfs_simple_field!(DeviceConfig, 4, irq_mode, IRQMode);
diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index fbda2f572e17f..eb980079530bd 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -17,20 +17,21 @@
sync::{Arc, UniqueArc},
types::{ForeignOwnable, ScopeGuard},
};
-use core::ptr::NonNull;
+use core::{marker::PhantomData, ptr::NonNull};

/// A builder for [`GenDisk`].
///
/// Use this struct to configure and add new [`GenDisk`] to the VFS.
-pub struct GenDiskBuilder {
+pub struct GenDiskBuilder<T> {
rotational: bool,
logical_block_size: u32,
physical_block_size: u32,
capacity_sectors: u64,
max_hw_discard_sectors: u32,
+ _p: PhantomData<T>,
}

-impl Default for GenDiskBuilder {
+impl<T> Default for GenDiskBuilder<T> {
fn default() -> Self {
Self {
rotational: false,
@@ -38,11 +39,12 @@ fn default() -> Self {
physical_block_size: bindings::PAGE_SIZE as u32,
capacity_sectors: 0,
max_hw_discard_sectors: 0,
+ _p: PhantomData,
}
}
}

-impl GenDiskBuilder {
+impl<T: Operations> GenDiskBuilder<T> {
/// Create a new instance.
pub fn new() -> Self {
Self::default()
@@ -109,7 +111,7 @@ pub fn max_hw_discard_sectors(mut self, max_hw_discard_sectors: u32) -> Self {
}

/// Build a new `GenDisk` and add it to the VFS.
- pub fn build<T: Operations>(
+ pub fn build(
self,
name: fmt::Arguments<'_>,
tagset: Arc<TagSet<T>>,
@@ -141,32 +143,8 @@ pub fn build<T: Operations>(
)
})?;

- const TABLE: bindings::block_device_operations = bindings::block_device_operations {
- submit_bio: None,
- open: None,
- release: None,
- ioctl: None,
- compat_ioctl: None,
- check_events: None,
- unlock_native_capacity: None,
- getgeo: None,
- set_read_only: None,
- swap_slot_free_notify: None,
- report_zones: None,
- devnode: None,
- alternative_gpt_sector: None,
- get_unique_id: None,
- // TODO: Set to THIS_MODULE. Waiting for const_refs_to_static feature to
- // be merged (unstable in rustc 1.78 which is staged for linux 6.10)
- // <https://github.com/rust-lang/rust/issues/119618>
- owner: core::ptr::null_mut(),
- pr_ops: core::ptr::null_mut(),
- free_disk: None,
- poll_bio: None,
- };
-
// SAFETY: `gendisk` is a valid pointer as we initialized it above
- unsafe { (*gendisk).fops = &TABLE };
+ unsafe { (*gendisk).fops = Self::build_vtable() };

let mut writer = NullTerminatedFormatter::new(
// SAFETY: `gendisk` points to a valid and initialized instance. We
@@ -219,6 +197,34 @@ pub fn build<T: Operations>(

Ok(disk.into())
}
+
+ const VTABLE: bindings::block_device_operations = bindings::block_device_operations {
+ submit_bio: None,
+ open: None,
+ release: None,
+ ioctl: None,
+ compat_ioctl: None,
+ check_events: None,
+ unlock_native_capacity: None,
+ getgeo: None,
+ set_read_only: None,
+ swap_slot_free_notify: None,
+ report_zones: None,
+ devnode: None,
+ alternative_gpt_sector: None,
+ get_unique_id: None,
+ // TODO: Set to THIS_MODULE. Waiting for const_refs_to_static feature to
+ // be merged (unstable in rustc 1.78 which is staged for linux 6.10)
+ // <https://github.com/rust-lang/rust/issues/119618>
+ owner: core::ptr::null_mut(),
+ pr_ops: core::ptr::null_mut(),
+ free_disk: None,
+ poll_bio: None,
+ };
+
+ pub(crate) const fn build_vtable() -> &'static bindings::block_device_operations {
+ &Self::VTABLE
+ }
}

/// A generic block device.

--
2.51.2