Skip to content

Commit

Permalink
rust: fs: introduce the module_fs macro
Browse files Browse the repository at this point in the history
Simplify the declaration of modules that only expose a file system type.
They can now do it using the `module_fs` macro.

Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
  • Loading branch information
wedsonaf committed Oct 18, 2023
1 parent 528babd commit e909f43
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion rust/kernel/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use crate::error::{code::*, from_result, to_result, Error};
use crate::types::Opaque;
use crate::{bindings, init::PinInit, str::CStr, try_pin_init, ThisModule};
use core::{marker::PhantomPinned, pin::Pin};
use core::{marker::PhantomData, marker::PhantomPinned, pin::Pin};
use macros::{pin_data, pinned_drop};

/// A file system type.
Expand Down Expand Up @@ -78,3 +78,57 @@ impl PinnedDrop for Registration {
unsafe { bindings::unregister_filesystem(self.fs.get()) };
}
}

/// Kernel module that exposes a single file system implemented by `T`.
#[pin_data]
pub struct Module<T: FileSystem + ?Sized> {
#[pin]
fs_reg: Registration,
_p: PhantomData<T>,
}

impl<T: FileSystem + ?Sized + Sync + Send> crate::InPlaceModule for Module<T> {
fn init(module: &'static ThisModule) -> impl PinInit<Self, Error> {
try_pin_init!(Self {
fs_reg <- Registration::new::<T>(module),
_p: PhantomData,
})
}
}

/// Declares a kernel module that exposes a single file system.
///
/// The `type` argument must be a type which implements the [`FileSystem`] trait. Also accepts
/// various forms of kernel metadata.
///
/// # Examples
///
/// ```
/// # mod module_fs_sample {
/// use kernel::prelude::*;
/// use kernel::{c_str, fs};
///
/// kernel::module_fs! {
/// type: MyFs,
/// name: "myfs",
/// author: "Rust for Linux Contributors",
/// description: "My Rust fs",
/// license: "GPL",
/// }
///
/// struct MyFs;
/// impl fs::FileSystem for MyFs {
/// const NAME: &'static CStr = c_str!("myfs");
/// }
/// # }
/// ```
#[macro_export]
macro_rules! module_fs {
(type: $type:ty, $($f:tt)*) => {
type ModuleType = $crate::fs::Module<$type>;
$crate::macros::module! {
type: ModuleType,
$($f)*
}
}
}

0 comments on commit e909f43

Please sign in to comment.