linux/drivers/block/rnull/configfs.rs
Linus Torvalds 8c0901b6f9 configfs changes for v7.0
-----BEGIN PGP SIGNATURE-----
 
 iQJKBAABCgA0FiEEEsH5R1a/fCoV1sAS4bgaPnkoY3cFAmmMPp0WHGEuaGluZGJv
 cmdAa2VybmVsLm9yZwAKCRDhuBo+eShjdx9+D/4hz5pC/L0w21231hz00CLJqHps
 Mv4uPhy3vd0QqQ8/S25gQNesCirjGuC57NSQ4K1O3+UBJKaPNEnGJYS6nlc8/vsI
 4heTR+F7QPu34RD/kgzeUu03/VhQyhQzHx10+e3qiQBEn6lwKqfZaKHnsUD5M2Zk
 eL8LydI87rXoC9TL5ASPEq33jB7d7ec8uH2fgPhUbysTmiFTIJkaOvIv6ukRo0y7
 skdSJemXRjlknXqrhLtAxe5Bt7Ycq1BzEaPYc3xRGXT1B/jp/fJxhfK9AHc5ej+T
 7A/Ewjdf9IEHZYKGnUDappXC/7SWsZw47BDrqljj/6a+aODYXlt+E4q1kNBBnA4L
 I8oVMf485BaZhLbpWdtVTztlLzO6DQg5MvN5KWxdhbz4JSn54eumLmvSjvvPfi0S
 FhRfR0+/vAvY7nSzZ2FXLQWt3Yg34mUUQUkJWb09TcNSts7WpdwcJP7gEwQSSVif
 wtEL65CNGoJlKH+KmImHFh4hINrwac53Jl5+qCbj+sfXmCRehb2XPSOZCHa+ivXq
 +0p9hneOgaPOfFZ67GSU6OAGtnT5X0tL69PiK3gvVD3P/t9W7/Qr8KF4E1Ksc00H
 CRxzhk2GkRgLaNR00xIY2L0vaeLO6R1Dsg+HFVJGMCM+8fm/Swsy3Mt0/LpG7Rl7
 8EOqsbwRIBWySEz6og==
 =eFAh
 -----END PGP SIGNATURE-----

Merge tag 'configfs-for-v7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux

Pull configfs updates from Andreas Hindborg:

 - Switch the configfs rust bindings to use c string literals provided
   by the compiler, rather than a macro

 - A follow up on constifying `configfs_item_operations`, applying the
   change to the configfs sample

* tag 'configfs-for-v7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/a.hindborg/linux:
  samples: configfs: Constify struct configfs_item_operations and configfs_group_operations
  rust: configfs: replace `kernel::c_str!` with C-Strings
2026-02-12 14:01:38 -08:00

261 lines
6.8 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
use super::{NullBlkDevice, THIS_MODULE};
use kernel::{
block::mq::gen_disk::{GenDisk, GenDiskBuilder},
configfs::{self, AttributeOperations},
configfs_attrs,
fmt::{self, Write as _},
new_mutex,
page::PAGE_SIZE,
prelude::*,
str::{kstrtobool_bytes, CString},
sync::Mutex,
};
pub(crate) fn subsystem() -> impl PinInit<kernel::configfs::Subsystem<Config>, Error> {
let item_type = configfs_attrs! {
container: configfs::Subsystem<Config>,
data: Config,
child: DeviceConfig,
attributes: [
features: 0,
],
};
kernel::configfs::Subsystem::new(c"rnull", item_type, try_pin_init!(Config {}))
}
#[pin_data]
pub(crate) struct Config {}
#[vtable]
impl AttributeOperations<0> for Config {
type Data = Config;
fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_str("blocksize,size,rotational,irqmode\n")?;
Ok(writer.bytes_written())
}
}
#[vtable]
impl configfs::GroupOperations for Config {
type Child = DeviceConfig;
fn make_group(
&self,
name: &CStr,
) -> Result<impl PinInit<configfs::Group<DeviceConfig>, Error>> {
let item_type = configfs_attrs! {
container: configfs::Group<DeviceConfig>,
data: DeviceConfig,
attributes: [
// Named for compatibility with C null_blk
power: 0,
blocksize: 1,
rotational: 2,
size: 3,
irqmode: 4,
],
};
Ok(configfs::Group::new(
name.try_into()?,
item_type,
// TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>, so put mutex inside
try_pin_init!( DeviceConfig {
data <- new_mutex!(DeviceConfigInner {
powered: false,
block_size: 4096,
rotational: false,
disk: None,
capacity_mib: 4096,
irq_mode: IRQMode::None,
name: name.try_into()?,
}),
}),
))
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum IRQMode {
None,
Soft,
}
impl TryFrom<u8> for IRQMode {
type Error = kernel::error::Error;
fn try_from(value: u8) -> Result<Self> {
match value {
0 => Ok(Self::None),
1 => Ok(Self::Soft),
_ => Err(EINVAL),
}
}
}
impl fmt::Display for IRQMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::None => f.write_str("0")?,
Self::Soft => f.write_str("1")?,
}
Ok(())
}
}
#[pin_data]
pub(crate) struct DeviceConfig {
#[pin]
data: Mutex<DeviceConfigInner>,
}
#[pin_data]
struct DeviceConfigInner {
powered: bool,
name: CString,
block_size: u32,
rotational: bool,
capacity_mib: u64,
irq_mode: IRQMode,
disk: Option<GenDisk<NullBlkDevice>>,
}
#[vtable]
impl configfs::AttributeOperations<0> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
if this.data.lock().powered {
writer.write_str("1\n")?;
} else {
writer.write_str("0\n")?;
}
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
let power_op = kstrtobool_bytes(page)?;
let mut guard = this.data.lock();
if !guard.powered && power_op {
guard.disk = Some(NullBlkDevice::new(
&guard.name,
guard.block_size,
guard.rotational,
guard.capacity_mib,
guard.irq_mode,
)?);
guard.powered = true;
} else if guard.powered && !power_op {
drop(guard.disk.take());
guard.powered = false;
}
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<1> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u32>().map_err(|_| EINVAL)?;
GenDiskBuilder::validate_block_size(value)?;
this.data.lock().block_size = value;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<2> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
if this.data.lock().rotational {
writer.write_str("1\n")?;
} else {
writer.write_str("0\n")?;
}
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
this.data.lock().rotational = kstrtobool_bytes(page)?;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<3> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u64>().map_err(|_| EINVAL)?;
this.data.lock().capacity_mib = value;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<4> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u8>().map_err(|_| EINVAL)?;
this.data.lock().irq_mode = IRQMode::try_from(value)?;
Ok(())
}
}