Skip to content

Commit 3a59eac

Browse files
committed
rust: fs: introduce the module_fs macro
This simplifies the declaration of modules that only expose a file system. Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
1 parent 1a0cab8 commit 3a59eac

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

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

0 commit comments

Comments
 (0)