Skip to content

Commit e909f43

Browse files
committed
rust: fs: introduce the module_fs macro
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 <[email protected]>
1 parent 528babd commit e909f43

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

rust/kernel/fs.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use crate::error::{code::*, from_result, to_result, Error};
1010
use crate::types::Opaque;
1111
use crate::{bindings, init::PinInit, str::CStr, try_pin_init, ThisModule};
12-
use core::{marker::PhantomPinned, pin::Pin};
12+
use core::{marker::PhantomData, marker::PhantomPinned, pin::Pin};
1313
use macros::{pin_data, pinned_drop};
1414

1515
/// A file system type.
@@ -78,3 +78,57 @@ impl PinnedDrop for Registration {
7878
unsafe { bindings::unregister_filesystem(self.fs.get()) };
7979
}
8080
}
81+
82+
/// Kernel module that exposes a single file system implemented by `T`.
83+
#[pin_data]
84+
pub struct Module<T: FileSystem + ?Sized> {
85+
#[pin]
86+
fs_reg: Registration,
87+
_p: PhantomData<T>,
88+
}
89+
90+
impl<T: FileSystem + ?Sized + Sync + Send> crate::InPlaceModule for Module<T> {
91+
fn init(module: &'static ThisModule) -> impl PinInit<Self, Error> {
92+
try_pin_init!(Self {
93+
fs_reg <- Registration::new::<T>(module),
94+
_p: PhantomData,
95+
})
96+
}
97+
}
98+
99+
/// Declares a kernel module that exposes a single file system.
100+
///
101+
/// The `type` argument must be a type which implements the [`FileSystem`] trait. Also accepts
102+
/// various forms of kernel metadata.
103+
///
104+
/// # Examples
105+
///
106+
/// ```
107+
/// # mod module_fs_sample {
108+
/// use kernel::prelude::*;
109+
/// use kernel::{c_str, fs};
110+
///
111+
/// kernel::module_fs! {
112+
/// type: MyFs,
113+
/// name: "myfs",
114+
/// author: "Rust for Linux Contributors",
115+
/// description: "My Rust fs",
116+
/// license: "GPL",
117+
/// }
118+
///
119+
/// struct MyFs;
120+
/// impl fs::FileSystem for MyFs {
121+
/// const NAME: &'static CStr = c_str!("myfs");
122+
/// }
123+
/// # }
124+
/// ```
125+
#[macro_export]
126+
macro_rules! module_fs {
127+
(type: $type:ty, $($f:tt)*) => {
128+
type ModuleType = $crate::fs::Module<$type>;
129+
$crate::macros::module! {
130+
type: ModuleType,
131+
$($f)*
132+
}
133+
}
134+
}

0 commit comments

Comments
 (0)