samples/rust/rust_configfs.rs

Source file repositories/reference/linux-study-clean/samples/rust/rust_configfs.rs

File Facts

System
Linux kernel
Corpus path
samples/rust/rust_configfs.rs
Extension
.rs
Size
5296 bytes
Lines
192
Domain
Support Tooling And Documentation
Bucket
samples
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct RustConfigfs {
    #[pin]
    config: configfs::Subsystem<Configuration>,
}

#[pin_data]
struct Configuration {
    message: &'static CStr,
    #[pin]
    bar: Mutex<(KBox<[u8; PAGE_SIZE]>, usize)>,
}

impl Configuration {
    fn new() -> impl PinInit<Self, Error> {
        try_pin_init!(Self {
            message: c"Hello World\n",
            bar <- new_mutex!((KBox::new([0; PAGE_SIZE], flags::GFP_KERNEL)?, 0)),
        })
    }
}

impl kernel::InPlaceModule for RustConfigfs {
    fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
        pr_info!("Rust configfs sample (init)\n");

        // Define a subsystem with the data type `Configuration`, two
        // attributes, `message` and `bar` and child group type `Child`. `mkdir`
        // in the directory representing this subsystem will create directories
        // backed by the `Child` type.
        let item_type = configfs_attrs! {
            container: configfs::Subsystem<Configuration>,
            data: Configuration,
            child: Child,
            attributes: [
                message: 0,
                bar: 1,
            ],
        };

        try_pin_init!(Self {
            config <- configfs::Subsystem::new(
                c"rust_configfs", item_type, Configuration::new()
            ),
        })
    }
}

#[vtable]
impl configfs::GroupOperations for Configuration {
    type Child = Child;

    fn make_group(&self, name: &CStr) -> Result<impl PinInit<configfs::Group<Child>, Error>> {
        // Define a group with data type `Child`, one attribute `baz` and child
        // group type `GrandChild`. `mkdir` in the directory representing this
        // group will create directories backed by the `GrandChild` type.
        let tpe = configfs_attrs! {
            container: configfs::Group<Child>,
            data: Child,
            child: GrandChild,
            attributes: [
                baz: 0,
            ],
        };

        Ok(configfs::Group::new(name.try_into()?, tpe, Child::new()))
    }
}

#[vtable]
impl configfs::AttributeOperations<0> for Configuration {
    type Data = Configuration;

    fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
        pr_info!("Show message\n");
        let data = container.message.to_bytes();
        page[0..data.len()].copy_from_slice(data);
        Ok(data.len())
    }
}

#[vtable]
impl configfs::AttributeOperations<1> for Configuration {
    type Data = Configuration;

    fn show(container: &Configuration, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
        pr_info!("Show bar\n");
        let guard = container.bar.lock();
        let data = guard.0.as_slice();
        let len = guard.1;
        page[0..len].copy_from_slice(&data[0..len]);

Annotation

Implementation Notes