[PATCH 02/79] block: rnull: add module parameters
From: Andreas Hindborg
Date: Sun Feb 15 2026 - 18:52:40 EST
Module parameter support for Rust modules was merged a few releases back.
Add module parameter support to the rnull driver.
This allows the user to control the driver either via configfs or module
parameters, just like the C counterpart.
Please note that the rust module parameters do not support boolean values.
Flags that should have been booleans are parsed as integers and compared to
zero.
Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
drivers/block/rnull/rnull.rs | 51 ++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 49 insertions(+), 2 deletions(-)
diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs
index 2c83b014b05e9..bd883a5061cab 100644
--- a/drivers/block/rnull/rnull.rs
+++ b/drivers/block/rnull/rnull.rs
@@ -19,11 +19,13 @@
},
},
error::Result,
- pr_info,
+ new_mutex, pr_info,
prelude::*,
+ str::CString,
sync::{
aref::ARef,
- Arc, //
+ Arc,
+ Mutex, //
},
};
use pin_init::PinInit;
@@ -34,20 +36,65 @@
authors: ["Andreas Hindborg"],
description: "Rust implementation of the C null block driver",
license: "GPL v2",
+ params: {
+ gb: u64 {
+ default: 4,
+ description: "Device capacity in GiB",
+ },
+ rotational: u8 {
+ default: 0,
+ description:
+ "Set the rotational feature for the device (0 for false, 1 for true). Default: 0",
+ },
+ bs: u32 {
+ default: 4096,
+ description: "Block size (in bytes)",
+ },
+ nr_devices: u64 {
+ default: 1,
+ description: "Number of devices to register",
+ },
+ irqmode: u8 {
+ default: 0,
+ description: "IRQ completion handler. 0-none, 1-softirq",
+ },
+ },
}
#[pin_data]
struct NullBlkModule {
#[pin]
configfs_subsystem: kernel::configfs::Subsystem<configfs::Config>,
+ #[pin]
+ param_disks: Mutex<KVec<GenDisk<NullBlkDevice>>>,
}
impl kernel::InPlaceModule for NullBlkModule {
fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
pr_info!("Rust null_blk loaded\n");
+ let mut disks = KVec::new();
+
+ let defer_init = move || -> Result<_, Error> {
+ for i in 0..(*module_parameters::nr_devices.value()) {
+ let name = CString::try_from_fmt(fmt!("rnullb{}", i))?;
+
+ let disk = NullBlkDevice::new(
+ &name,
+ *module_parameters::bs.value(),
+ *module_parameters::rotational.value() != 0,
+ *module_parameters::gb.value() * 1024,
+ (*module_parameters::irqmode.value()).try_into()?,
+ )?;
+ disks.push(disk, GFP_KERNEL)?;
+ }
+
+ Ok(disks)
+ };
+
try_pin_init!(Self {
configfs_subsystem <- configfs::subsystem(),
+ param_disks <- new_mutex!(defer_init()?),
})
}
}
--
2.51.2