Skip to content

Commit 1e8be5e

Browse files
committed
rust: fs: introduce the module_ro_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 7754a4b commit 1e8be5e

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
@@ -11,7 +11,7 @@ pub mod ro {
1111
use crate::error::{code::*, from_result, to_result, Error};
1212
use crate::types::Opaque;
1313
use crate::{bindings, init::PinInit, str::CStr, try_pin_init, ThisModule};
14-
use core::{marker::PhantomPinned, pin::Pin};
14+
use core::{marker::PhantomData, marker::PhantomPinned, pin::Pin};
1515
use macros::{pin_data, pinned_drop};
1616

1717
/// A read-only file system type.
@@ -77,4 +77,58 @@ pub mod ro {
7777
unsafe { bindings::unregister_filesystem(self.fs.get()) };
7878
}
7979
}
80+
81+
/// Kernel module that exposes a single read-only file system implemented by `T`.
82+
#[pin_data]
83+
pub struct Module<T: Type + ?Sized> {
84+
#[pin]
85+
fs_reg: Registration,
86+
_p: PhantomData<T>,
87+
}
88+
89+
impl<T: Type + ?Sized + Sync + Send> crate::InPlaceModule for Module<T> {
90+
type Init = impl PinInit<Self, Error>;
91+
fn init(module: &'static ThisModule) -> Self::Init {
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 [`Type`] trait. Also accepts various
102+
/// forms of kernel metadata.
103+
///
104+
/// # Examples
105+
///
106+
/// ```ignore
107+
/// use kernel::prelude::*;
108+
/// use kernel::{c_str, fs};
109+
///
110+
/// kernel::module_ro_fs! {
111+
/// type: MyFs,
112+
/// name: "myfs",
113+
/// author: "Rust for Linux Contributors",
114+
/// description: "My Rust fs",
115+
/// license: "GPL",
116+
/// }
117+
///
118+
/// struct MyFs;
119+
/// impl fs::ro::Type for MyFs {
120+
/// // ...
121+
/// # const NAME: &'static CStr = c_str!("myfs");
122+
/// }
123+
/// ```
124+
#[macro_export]
125+
macro_rules! module_ro_fs {
126+
(type: $type:ty, $($f:tt)*) => {
127+
type ModuleType = $crate::fs::ro::Module<$type>;
128+
$crate::macros::module! {
129+
type: ModuleType,
130+
$($f)*
131+
}
132+
}
133+
}
80134
}

0 commit comments

Comments
 (0)