9
9
use crate :: error:: { code:: * , from_result, to_result, Error , Result } ;
10
10
use crate :: types:: { ARef , AlwaysRefCounted , Either , ForeignOwnable , Opaque } ;
11
11
use crate :: {
12
- bindings, folio:: LockedFolio , init:: PinInit , str :: CStr , time :: Timespec , try_pin_init ,
13
- ThisModule ,
12
+ bindings, container_of , folio:: LockedFolio , init:: PinInit , mem_cache :: MemCache , str :: CStr ,
13
+ time :: Timespec , try_pin_init , ThisModule ,
14
14
} ;
15
- use core:: { marker:: PhantomData , marker:: PhantomPinned , mem:: ManuallyDrop , pin:: Pin , ptr} ;
15
+ use core:: mem:: { size_of, ManuallyDrop , MaybeUninit } ;
16
+ use core:: { marker:: PhantomData , marker:: PhantomPinned , pin:: Pin , ptr} ;
16
17
use macros:: { pin_data, pinned_drop} ;
17
18
18
19
pub mod buffer;
@@ -36,6 +37,9 @@ pub trait FileSystem {
36
37
/// Data associated with each file system instance (super-block).
37
38
type Data : ForeignOwnable + Send + Sync ;
38
39
40
+ /// Type of data associated with each inode.
41
+ type INodeData : Send + Sync ;
42
+
39
43
/// The name of the file system type.
40
44
const NAME : & ' static CStr ;
41
45
@@ -171,6 +175,7 @@ impl core::convert::TryFrom<u32> for DirEntryType {
171
175
pub struct Registration {
172
176
#[ pin]
173
177
fs : Opaque < bindings:: file_system_type > ,
178
+ inode_cache : Option < MemCache > ,
174
179
#[ pin]
175
180
_pin : PhantomPinned ,
176
181
}
@@ -188,6 +193,14 @@ impl Registration {
188
193
pub fn new < T : FileSystem + ?Sized > ( module : & ' static ThisModule ) -> impl PinInit < Self , Error > {
189
194
try_pin_init ! ( Self {
190
195
_pin: PhantomPinned ,
196
+ inode_cache: if size_of:: <T :: INodeData >( ) == 0 {
197
+ None
198
+ } else {
199
+ Some ( MemCache :: try_new:: <INodeWithData <T :: INodeData >>(
200
+ T :: NAME ,
201
+ Some ( Self :: inode_init_once_callback:: <T >) ,
202
+ ) ?)
203
+ } ,
191
204
fs <- Opaque :: try_ffi_init( |fs_ptr| {
192
205
// SAFETY: `try_ffi_init` guarantees that `fs_ptr` is valid for write.
193
206
let fs = unsafe { & mut * fs_ptr } ;
@@ -242,6 +255,16 @@ impl Registration {
242
255
unsafe { T :: Data :: from_foreign ( ptr) } ;
243
256
}
244
257
}
258
+
259
+ unsafe extern "C" fn inode_init_once_callback < T : FileSystem + ?Sized > (
260
+ outer_inode : * mut core:: ffi:: c_void ,
261
+ ) {
262
+ let ptr = outer_inode. cast :: < INodeWithData < T :: INodeData > > ( ) ;
263
+
264
+ // SAFETY: This is only used in `new`, so we know that we have a valid `INodeWithData`
265
+ // instance whose inode part can be initialised.
266
+ unsafe { bindings:: inode_init_once ( ptr:: addr_of_mut!( ( * ptr) . inode) ) } ;
267
+ }
245
268
}
246
269
247
270
#[ pinned_drop]
@@ -280,6 +303,15 @@ impl<T: FileSystem + ?Sized> INode<T> {
280
303
unsafe { & * ( * self . 0 . get ( ) ) . i_sb . cast ( ) }
281
304
}
282
305
306
+ /// Returns the data associated with the inode.
307
+ pub fn data ( & self ) -> & T :: INodeData {
308
+ let outerp = container_of ! ( self . 0 . get( ) , INodeWithData <T :: INodeData >, inode) ;
309
+ // SAFETY: `self` is guaranteed to be valid by the existence of a shared reference
310
+ // (`&self`) to it. Additionally, we know `T::INodeData` is always initialised in an
311
+ // `INode`.
312
+ unsafe { & * ( * outerp) . data . as_ptr ( ) }
313
+ }
314
+
283
315
/// Returns the size of the inode contents.
284
316
pub fn size ( & self ) -> i64 {
285
317
// SAFETY: `self` is guaranteed to be valid by the existence of a shared reference.
@@ -300,15 +332,29 @@ unsafe impl<T: FileSystem + ?Sized> AlwaysRefCounted for INode<T> {
300
332
}
301
333
}
302
334
335
+ struct INodeWithData < T > {
336
+ data : MaybeUninit < T > ,
337
+ inode : bindings:: inode ,
338
+ }
339
+
303
340
/// An inode that is locked and hasn't been initialised yet.
304
341
#[ repr( transparent) ]
305
342
pub struct NewINode < T : FileSystem + ?Sized > ( ARef < INode < T > > ) ;
306
343
307
344
impl < T : FileSystem + ?Sized > NewINode < T > {
308
345
/// Initialises the new inode with the given parameters.
309
- pub fn init ( self , params : INodeParams ) -> Result < ARef < INode < T > > > {
310
- // SAFETY: This is a new inode, so it's safe to manipulate it mutably.
311
- let inode = unsafe { & mut * self . 0 . 0 . get ( ) } ;
346
+ pub fn init ( self , params : INodeParams < T :: INodeData > ) -> Result < ARef < INode < T > > > {
347
+ let outerp = container_of ! ( self . 0 . 0 . get( ) , INodeWithData <T :: INodeData >, inode) ;
348
+
349
+ // SAFETY: This is a newly-created inode. No other references to it exist, so it is
350
+ // safe to mutably dereference it.
351
+ let outer = unsafe { & mut * outerp. cast_mut ( ) } ;
352
+
353
+ // N.B. We must always write this to a newly allocated inode because the free callback
354
+ // expects the data to be initialised and drops it.
355
+ outer. data . write ( params. value ) ;
356
+
357
+ let inode = & mut outer. inode ;
312
358
313
359
let mode = match params. typ {
314
360
INodeType :: Dir => {
@@ -424,7 +470,7 @@ pub enum INodeType {
424
470
/// Required inode parameters.
425
471
///
426
472
/// This is used when creating new inodes.
427
- pub struct INodeParams {
473
+ pub struct INodeParams < T > {
428
474
/// The access mode. It's a mask that grants execute (1), write (2) and read (4) access to
429
475
/// everyone, the owner group, and the owner.
430
476
pub mode : u16 ,
@@ -459,6 +505,9 @@ pub struct INodeParams {
459
505
460
506
/// Last access time.
461
507
pub atime : Timespec ,
508
+
509
+ /// Value to attach to this node.
510
+ pub value : T ,
462
511
}
463
512
464
513
/// A file system super block.
@@ -752,8 +801,12 @@ impl<T: FileSystem + ?Sized> Tables<T> {
752
801
}
753
802
754
803
const SUPER_BLOCK : bindings:: super_operations = bindings:: super_operations {
755
- alloc_inode : None ,
756
- destroy_inode : None ,
804
+ alloc_inode : if size_of :: < T :: INodeData > ( ) != 0 {
805
+ Some ( Self :: alloc_inode_callback)
806
+ } else {
807
+ None
808
+ } ,
809
+ destroy_inode : Some ( Self :: destroy_inode_callback) ,
757
810
free_inode : None ,
758
811
dirty_inode : None ,
759
812
write_inode : None ,
@@ -783,6 +836,61 @@ impl<T: FileSystem + ?Sized> Tables<T> {
783
836
shutdown : None ,
784
837
} ;
785
838
839
+ unsafe extern "C" fn alloc_inode_callback (
840
+ sb : * mut bindings:: super_block ,
841
+ ) -> * mut bindings:: inode {
842
+ // SAFETY: The callback contract guarantees that `sb` is valid for read.
843
+ let super_type = unsafe { ( * sb) . s_type } ;
844
+
845
+ // SAFETY: This callback is only used in `Registration`, so `super_type` is necessarily
846
+ // embedded in a `Registration`, which is guaranteed to be valid because it has a
847
+ // superblock associated to it.
848
+ let reg = unsafe { & * container_of ! ( super_type, Registration , fs) } ;
849
+
850
+ // SAFETY: `sb` and `cache` are guaranteed to be valid by the callback contract and by
851
+ // the existence of a superblock respectively.
852
+ let ptr = unsafe {
853
+ bindings:: alloc_inode_sb ( sb, MemCache :: ptr ( & reg. inode_cache ) , bindings:: GFP_KERNEL )
854
+ }
855
+ . cast :: < INodeWithData < T :: INodeData > > ( ) ;
856
+ if ptr. is_null ( ) {
857
+ return ptr:: null_mut ( ) ;
858
+ }
859
+ ptr:: addr_of_mut!( ( * ptr) . inode)
860
+ }
861
+
862
+ unsafe extern "C" fn destroy_inode_callback ( inode : * mut bindings:: inode ) {
863
+ // SAFETY: By the C contract, `inode` is a valid pointer.
864
+ let is_bad = unsafe { bindings:: is_bad_inode ( inode) } ;
865
+
866
+ // SAFETY: The inode is guaranteed to be valid by the callback contract. Additionally, the
867
+ // superblock is also guaranteed to still be valid by the inode existence.
868
+ let super_type = unsafe { ( * ( * inode) . i_sb ) . s_type } ;
869
+
870
+ // SAFETY: This callback is only used in `Registration`, so `super_type` is necessarily
871
+ // embedded in a `Registration`, which is guaranteed to be valid because it has a
872
+ // superblock associated to it.
873
+ let reg = unsafe { & * container_of ! ( super_type, Registration , fs) } ;
874
+ let ptr = container_of ! ( inode, INodeWithData <T :: INodeData >, inode) . cast_mut ( ) ;
875
+
876
+ if !is_bad {
877
+ // SAFETY: The code either initialises the data or marks the inode as bad. Since the
878
+ // inode is not bad, the data is initialised, and thus safe to drop.
879
+ unsafe { ptr:: drop_in_place ( ( * ptr) . data . as_mut_ptr ( ) ) } ;
880
+ }
881
+
882
+ if size_of :: < T :: INodeData > ( ) == 0 {
883
+ // SAFETY: When the size of `INodeData` is zero, we don't use a separate mem_cache, so
884
+ // it is allocated from the regular mem_cache, which is what `free_inode_nonrcu` uses
885
+ // to free the inode.
886
+ unsafe { bindings:: free_inode_nonrcu ( inode) } ;
887
+ } else {
888
+ // The callback contract guarantees that the inode was previously allocated via the
889
+ // `alloc_inode_callback` callback, so it is safe to free it back to the cache.
890
+ unsafe { bindings:: kmem_cache_free ( MemCache :: ptr ( & reg. inode_cache ) , ptr. cast ( ) ) } ;
891
+ }
892
+ }
893
+
786
894
unsafe extern "C" fn statfs_callback (
787
895
dentry : * mut bindings:: dentry ,
788
896
buf : * mut bindings:: kstatfs ,
@@ -1079,6 +1187,7 @@ impl<T: FileSystem + ?Sized + Sync + Send> crate::InPlaceModule for Module<T> {
1079
1187
/// struct MyFs;
1080
1188
/// impl fs::FileSystem for MyFs {
1081
1189
/// type Data = ();
1190
+ /// type INodeData =();
1082
1191
/// const NAME: &'static CStr = c_str!("myfs");
1083
1192
/// fn fill_super(_: NewSuperBlock<'_, Self>) -> Result<&SuperBlock<Self>> {
1084
1193
/// todo!()
0 commit comments