Module configfs

Source
Expand description

configfs interface: Userspace-driven Kernel Object Configuration

configfs is an in-memory pseudo file system for configuration of kernel modules. Please see the C documentation for details and intended use of configfs.

This module does not support the following configfs features:

  • Items. All group children are groups.
  • Symlink support.
  • disconnect_notify hook.
  • Default groups.

See the rust_configfs.rs sample for a full example use of this module.

C header: include/linux/configfs.h

§Example

use kernel::alloc::flags;
use kernel::c_str;
use kernel::configfs_attrs;
use kernel::configfs;
use kernel::new_mutex;
use kernel::page::PAGE_SIZE;
use kernel::sync::Mutex;
use kernel::ThisModule;

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

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

        let item_type = configfs_attrs! {
            container: configfs::Subsystem<Configuration>,
            data: Configuration,
            attributes: [
                message: 0,
                bar: 1,
            ],
        };

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

#[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_str!("Hello World\n"),
            bar <- new_mutex!((KBox::new([0; PAGE_SIZE], flags::GFP_KERNEL)?, 0)),
        })
    }
}

#[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;
        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]);
        Ok(len)
    }

    fn store(container: &Configuration, page: &[u8]) -> Result {
        pr_info!("Store bar\n");
        let mut guard = container.bar.lock();
        guard.0[0..page.len()].copy_from_slice(page);
        guard.1 = page.len();
        Ok(())
    }
}

Structs§

Attribute
A configfs attribute.
AttributeList
A list of attributes.
Group
A configfs group.
ItemType
A representation of the attributes that will appear in a Group or Subsystem.
Subsystem
A configfs subsystem.

Traits§

AttributeOperations
Operations supported by an attribute.
GroupOperations
Operations implemented by configfs groups that can create subgroups.
HasGroup
Trait that allows offset calculations for structs that embed a bindings::config_group.