|
9 | 9 | use crate::error::{code::*, from_result, to_result, Error};
|
10 | 10 | use crate::types::Opaque;
|
11 | 11 | 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}; |
13 | 13 | use macros::{pin_data, pinned_drop};
|
14 | 14 |
|
15 | 15 | /// A file system type.
|
@@ -78,3 +78,57 @@ impl PinnedDrop for Registration {
|
78 | 78 | unsafe { bindings::unregister_filesystem(self.fs.get()) };
|
79 | 79 | }
|
80 | 80 | }
|
| 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