99use crate :: error:: { code:: * , from_result, to_result, Error , Result } ;
1010use crate :: types:: { ARef , AlwaysRefCounted , Either , ForeignOwnable , Opaque } ;
1111use 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 ,
1414} ;
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} ;
1617use macros:: { pin_data, pinned_drop} ;
1718
1819#[ cfg( CONFIG_BUFFER_HEAD ) ]
@@ -37,6 +38,9 @@ pub trait FileSystem {
3738 /// Data associated with each file system instance (super-block).
3839 type Data : ForeignOwnable + Send + Sync ;
3940
41+ /// Type of data associated with each inode.
42+ type INodeData : Send + Sync ;
43+
4044 /// The name of the file system type.
4145 const NAME : & ' static CStr ;
4246
@@ -161,6 +165,7 @@ impl core::convert::TryFrom<u32> for DirEntryType {
161165pub struct Registration {
162166 #[ pin]
163167 fs : Opaque < bindings:: file_system_type > ,
168+ inode_cache : Option < MemCache > ,
164169 #[ pin]
165170 _pin : PhantomPinned ,
166171}
@@ -178,6 +183,14 @@ impl Registration {
178183 pub fn new < T : FileSystem + ?Sized > ( module : & ' static ThisModule ) -> impl PinInit < Self , Error > {
179184 try_pin_init ! ( Self {
180185 _pin: PhantomPinned ,
186+ inode_cache: if size_of:: <T :: INodeData >( ) == 0 {
187+ None
188+ } else {
189+ Some ( MemCache :: try_new:: <INodeWithData <T :: INodeData >>(
190+ T :: NAME ,
191+ Some ( Self :: inode_init_once_callback:: <T >) ,
192+ ) ?)
193+ } ,
181194 fs <- Opaque :: try_ffi_init( |fs_ptr| {
182195 // SAFETY: `try_ffi_init` guarantees that `fs_ptr` is valid for write.
183196 let fs = unsafe { & mut * fs_ptr } ;
@@ -232,6 +245,16 @@ impl Registration {
232245 unsafe { T :: Data :: from_foreign ( ptr) } ;
233246 }
234247 }
248+
249+ unsafe extern "C" fn inode_init_once_callback < T : FileSystem + ?Sized > (
250+ outer_inode : * mut core:: ffi:: c_void ,
251+ ) {
252+ let ptr = outer_inode. cast :: < INodeWithData < T :: INodeData > > ( ) ;
253+
254+ // SAFETY: This is only used in `new`, so we know that we have a valid `INodeWithData`
255+ // instance whose inode part can be initialised.
256+ unsafe { bindings:: inode_init_once ( ptr:: addr_of_mut!( ( * ptr) . inode) ) } ;
257+ }
235258}
236259
237260#[ pinned_drop]
@@ -273,6 +296,15 @@ impl<T: FileSystem + ?Sized> INode<T> {
273296 unsafe { & * ( * self . 0 . get ( ) ) . i_sb . cast ( ) }
274297 }
275298
299+ /// Returns the data associated with the inode.
300+ pub fn data ( & self ) -> & T :: INodeData {
301+ let outerp = container_of ! ( self . 0 . get( ) , INodeWithData <T :: INodeData >, inode) ;
302+ // SAFETY: `self` is guaranteed to be valid by the existence of a shared reference
303+ // (`&self`) to it. Additionally, we know `T::INodeData` is always initialised in an
304+ // `INode`.
305+ unsafe { & * ( * outerp) . data . as_ptr ( ) }
306+ }
307+
276308 /// Returns the size of the inode contents.
277309 pub fn size ( & self ) -> i64 {
278310 // SAFETY: `self` is guaranteed to be valid by the existence of a shared reference.
@@ -293,15 +325,29 @@ unsafe impl<T: FileSystem + ?Sized> AlwaysRefCounted for INode<T> {
293325 }
294326}
295327
328+ struct INodeWithData < T > {
329+ data : MaybeUninit < T > ,
330+ inode : bindings:: inode ,
331+ }
332+
296333/// An inode that is locked and hasn't been initialised yet.
297334#[ repr( transparent) ]
298335pub struct NewINode < T : FileSystem + ?Sized > ( ARef < INode < T > > ) ;
299336
300337impl < T : FileSystem + ?Sized > NewINode < T > {
301338 /// Initialises the new inode with the given parameters.
302- pub fn init ( self , params : INodeParams ) -> Result < ARef < INode < T > > > {
303- // SAFETY: This is a new inode, so it's safe to manipulate it mutably.
304- let inode = unsafe { & mut * self . 0 . 0 . get ( ) } ;
339+ pub fn init ( self , params : INodeParams < T :: INodeData > ) -> Result < ARef < INode < T > > > {
340+ let outerp = container_of ! ( self . 0 . 0 . get( ) , INodeWithData <T :: INodeData >, inode) ;
341+
342+ // SAFETY: This is a newly-created inode. No other references to it exist, so it is
343+ // safe to mutably dereference it.
344+ let outer = unsafe { & mut * outerp. cast_mut ( ) } ;
345+
346+ // N.B. We must always write this to a newly allocated inode because the free callback
347+ // expects the data to be initialised and drops it.
348+ outer. data . write ( params. value ) ;
349+
350+ let inode = & mut outer. inode ;
305351
306352 let mode = match params. typ {
307353 INodeType :: Dir => {
@@ -417,7 +463,7 @@ pub enum INodeType {
417463/// Required inode parameters.
418464///
419465/// This is used when creating new inodes.
420- pub struct INodeParams {
466+ pub struct INodeParams < T > {
421467 /// The access mode. It's a mask that grants execute (1), write (2) and read (4) access to
422468 /// everyone, the owner group, and the owner.
423469 pub mode : u16 ,
@@ -452,6 +498,9 @@ pub struct INodeParams {
452498
453499 /// Last access time.
454500 pub atime : Timespec ,
501+
502+ /// Value to attach to this node.
503+ pub value : T ,
455504}
456505
457506/// A file system super block.
@@ -748,8 +797,12 @@ impl<T: FileSystem + ?Sized> Tables<T> {
748797 }
749798
750799 const SUPER_BLOCK : bindings:: super_operations = bindings:: super_operations {
751- alloc_inode : None ,
752- destroy_inode : None ,
800+ alloc_inode : if size_of :: < T :: INodeData > ( ) != 0 {
801+ Some ( Self :: alloc_inode_callback)
802+ } else {
803+ None
804+ } ,
805+ destroy_inode : Some ( Self :: destroy_inode_callback) ,
753806 free_inode : None ,
754807 dirty_inode : None ,
755808 write_inode : None ,
@@ -779,6 +832,61 @@ impl<T: FileSystem + ?Sized> Tables<T> {
779832 shutdown : None ,
780833 } ;
781834
835+ unsafe extern "C" fn alloc_inode_callback (
836+ sb : * mut bindings:: super_block ,
837+ ) -> * mut bindings:: inode {
838+ // SAFETY: The callback contract guarantees that `sb` is valid for read.
839+ let super_type = unsafe { ( * sb) . s_type } ;
840+
841+ // SAFETY: This callback is only used in `Registration`, so `super_type` is necessarily
842+ // embedded in a `Registration`, which is guaranteed to be valid because it has a
843+ // superblock associated to it.
844+ let reg = unsafe { & * container_of ! ( super_type, Registration , fs) } ;
845+
846+ // SAFETY: `sb` and `cache` are guaranteed to be valid by the callback contract and by
847+ // the existence of a superblock respectively.
848+ let ptr = unsafe {
849+ bindings:: alloc_inode_sb ( sb, MemCache :: ptr ( & reg. inode_cache ) , bindings:: GFP_KERNEL )
850+ }
851+ . cast :: < INodeWithData < T :: INodeData > > ( ) ;
852+ if ptr. is_null ( ) {
853+ return ptr:: null_mut ( ) ;
854+ }
855+ ptr:: addr_of_mut!( ( * ptr) . inode)
856+ }
857+
858+ unsafe extern "C" fn destroy_inode_callback ( inode : * mut bindings:: inode ) {
859+ // SAFETY: By the C contract, `inode` is a valid pointer.
860+ let is_bad = unsafe { bindings:: is_bad_inode ( inode) } ;
861+
862+ // SAFETY: The inode is guaranteed to be valid by the callback contract. Additionally, the
863+ // superblock is also guaranteed to still be valid by the inode existence.
864+ let super_type = unsafe { ( * ( * inode) . i_sb ) . s_type } ;
865+
866+ // SAFETY: This callback is only used in `Registration`, so `super_type` is necessarily
867+ // embedded in a `Registration`, which is guaranteed to be valid because it has a
868+ // superblock associated to it.
869+ let reg = unsafe { & * container_of ! ( super_type, Registration , fs) } ;
870+ let ptr = container_of ! ( inode, INodeWithData <T :: INodeData >, inode) . cast_mut ( ) ;
871+
872+ if !is_bad {
873+ // SAFETY: The code either initialises the data or marks the inode as bad. Since the
874+ // inode is not bad, the data is initialised, and thus safe to drop.
875+ unsafe { ptr:: drop_in_place ( ( * ptr) . data . as_mut_ptr ( ) ) } ;
876+ }
877+
878+ if size_of :: < T :: INodeData > ( ) == 0 {
879+ // SAFETY: When the size of `INodeData` is zero, we don't use a separate mem_cache, so
880+ // it is allocated from the regular mem_cache, which is what `free_inode_nonrcu` uses
881+ // to free the inode.
882+ unsafe { bindings:: free_inode_nonrcu ( inode) } ;
883+ } else {
884+ // The callback contract guarantees that the inode was previously allocated via the
885+ // `alloc_inode_callback` callback, so it is safe to free it back to the cache.
886+ unsafe { bindings:: kmem_cache_free ( MemCache :: ptr ( & reg. inode_cache ) , ptr. cast ( ) ) } ;
887+ }
888+ }
889+
782890 unsafe extern "C" fn statfs_callback (
783891 dentry : * mut bindings:: dentry ,
784892 buf : * mut bindings:: kstatfs ,
@@ -1133,6 +1241,7 @@ impl<T: FileSystem + ?Sized + Sync + Send> crate::InPlaceModule for Module<T> {
11331241/// struct MyFs;
11341242/// impl fs::FileSystem for MyFs {
11351243/// type Data = ();
1244+ /// type INodeData =();
11361245/// const NAME: &'static CStr = c_str!("myfs");
11371246/// fn fill_super(_: NewSuperBlock<'_, Self>) -> Result<&SuperBlock<Self>> {
11381247/// todo!()
0 commit comments