Skip to content

Commit a5d0b7d

Browse files
authored
Merge pull request #839 from wedsonaf/module_fs
rust: add `module_fs` macro
2 parents 61f94d9 + 17058e1 commit a5d0b7d

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

rust/kernel/fs.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
bindings, error::code::*, error::from_kernel_result, str::CStr, to_result,
99
types::PointerWrapper, AlwaysRefCounted, Error, Result, ScopeGuard, ThisModule,
1010
};
11+
use alloc::boxed::Box;
1112
use core::{
1213
cell::UnsafeCell,
1314
marker::{PhantomData, PhantomPinned},
@@ -763,3 +764,79 @@ impl Filename {
763764
unsafe { &*ptr.cast() }
764765
}
765766
}
767+
768+
/// Kernel module that exposes a single file system implemented by `T`.
769+
pub struct Module<T: Type> {
770+
_fs: Pin<Box<Registration>>,
771+
_p: PhantomData<T>,
772+
}
773+
774+
impl<T: Type + Sync> crate::Module for Module<T> {
775+
fn init(_name: &'static CStr, module: &'static ThisModule) -> Result<Self> {
776+
let mut reg = Pin::from(Box::try_new(Registration::new())?);
777+
reg.as_mut().register::<T>(module)?;
778+
Ok(Self {
779+
_fs: reg,
780+
_p: PhantomData,
781+
})
782+
}
783+
}
784+
785+
/// Declares a kernel module that exposes a single file system.
786+
///
787+
/// The `type` argument must be a type which implements the [`Type`] trait. Also accepts various
788+
/// forms of kernel metadata.
789+
///
790+
/// # Examples
791+
///
792+
/// ```ignore
793+
/// use kernel::prelude::*;
794+
/// use kernel::{c_str, fs};
795+
///
796+
/// module_fs! {
797+
/// type: MyFs,
798+
/// name: b"my_fs_kernel_module",
799+
/// author: b"Rust for Linux Contributors",
800+
/// description: b"My very own file system kernel module!",
801+
/// license: b"GPL",
802+
/// }
803+
///
804+
/// struct MyFs;
805+
///
806+
/// #[vtable]
807+
/// impl fs::Context<Self> for MyFs {
808+
/// type Data = ();
809+
/// fn try_new() -> Result {
810+
/// Ok(())
811+
/// }
812+
/// }
813+
///
814+
/// impl fs::Type for MyFs {
815+
/// type Context = Self;
816+
/// const SUPER_TYPE: fs::Super = fs::Super::Independent;
817+
/// const NAME: &'static CStr = c_str!("example");
818+
/// const FLAGS: i32 = 0;
819+
///
820+
/// fn fill_super(_data: (), sb: fs::NewSuperBlock<'_, Self>) -> Result<&fs::SuperBlock<Self>> {
821+
/// let sb = sb.init(
822+
/// (),
823+
/// &fs::SuperParams {
824+
/// magic: 0x6578616d,
825+
/// ..fs::SuperParams::DEFAULT
826+
/// },
827+
/// )?;
828+
/// let sb = sb.init_root()?;
829+
/// Ok(sb)
830+
/// }
831+
/// }
832+
/// ```
833+
#[macro_export]
834+
macro_rules! module_fs {
835+
(type: $type:ty, $($f:tt)*) => {
836+
type ModuleType = $crate::fs::Module<$type>;
837+
$crate::macros::module! {
838+
type: ModuleType,
839+
$($f)*
840+
}
841+
}
842+
}

rust/kernel/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub use super::{
2424
pr_alert, pr_crit, pr_debug, pr_emerg, pr_err, pr_info, pr_notice, pr_warn,
2525
};
2626

27-
pub use super::module_misc_device;
27+
pub use super::{module_fs, module_misc_device};
2828

2929
#[cfg(CONFIG_ARM_AMBA)]
3030
pub use super::module_amba_driver;

samples/rust/rust_fs.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use kernel::prelude::*;
66
use kernel::{c_str, fs};
77

8-
module! {
9-
type: FsModule,
8+
module_fs! {
9+
type: RustFs,
1010
name: b"rust_fs",
1111
author: b"Rust for Linux Contributors",
1212
license: b"GPL",
@@ -57,15 +57,3 @@ impl fs::Type for RustFs {
5757
Ok(sb)
5858
}
5959
}
60-
61-
struct FsModule {
62-
_fs: Pin<Box<fs::Registration>>,
63-
}
64-
65-
impl kernel::Module for FsModule {
66-
fn init(_name: &'static CStr, module: &'static ThisModule) -> Result<Self> {
67-
let mut reg = Pin::from(Box::try_new(fs::Registration::new())?);
68-
reg.as_mut().register::<RustFs>(module)?;
69-
Ok(Self { _fs: reg })
70-
}
71-
}

0 commit comments

Comments
 (0)