@@ -47,6 +47,14 @@ pub trait FileSystem {
4747
4848 /// Reads the contents of the inode into the given folio.
4949 fn read_folio ( inode : & INode < Self > , folio : LockedFolio < ' _ > ) -> Result ;
50+
51+ /// Reads an xattr.
52+ ///
53+ /// Returns the number of bytes written to `outbuf`. If it is too small, returns the number of
54+ /// bytes needs to hold the attribute.
55+ fn read_xattr ( _inode : & INode < Self > , _name : & CStr , _outbuf : & mut [ u8 ] ) -> Result < usize > {
56+ Err ( EOPNOTSUPP )
57+ }
5058}
5159
5260/// The types of directory entries reported by [`FileSystem::read_dir`].
@@ -414,6 +422,7 @@ impl<'a, T: FileSystem + ?Sized> NewSuperBlock<'a, T> {
414422
415423 sb. s_magic = params. magic as _ ;
416424 sb. s_op = & Tables :: < T > :: SUPER_BLOCK ;
425+ sb. s_xattr = & Tables :: < T > :: XATTR_HANDLERS [ 0 ] ;
417426 sb. s_maxbytes = params. maxbytes ;
418427 sb. s_time_gran = params. time_gran ;
419428 sb. s_blocksize_bits = params. blocksize_bits ;
@@ -514,6 +523,40 @@ impl<T: FileSystem + ?Sized> Tables<T> {
514523 shutdown : None ,
515524 } ;
516525
526+ const XATTR_HANDLERS : [ * const bindings:: xattr_handler ; 2 ] = [ & Self :: XATTR_HANDLER , ptr:: null ( ) ] ;
527+
528+ const XATTR_HANDLER : bindings:: xattr_handler = bindings:: xattr_handler {
529+ name : ptr:: null ( ) ,
530+ prefix : crate :: c_str!( "" ) . as_char_ptr ( ) ,
531+ flags : 0 ,
532+ list : None ,
533+ get : Some ( Self :: xattr_get_callback) ,
534+ set : None ,
535+ } ;
536+
537+ unsafe extern "C" fn xattr_get_callback (
538+ _handler : * const bindings:: xattr_handler ,
539+ _dentry : * mut bindings:: dentry ,
540+ inode_ptr : * mut bindings:: inode ,
541+ name : * const core:: ffi:: c_char ,
542+ buffer : * mut core:: ffi:: c_void ,
543+ size : usize ,
544+ ) -> core:: ffi:: c_int {
545+ from_result ( || {
546+ // SAFETY: The C API guarantees that `inode_ptr` is a valid inode.
547+ let inode = unsafe { & * inode_ptr. cast :: < INode < T > > ( ) } ;
548+
549+ // SAFETY: The c API guarantees that `name` is a valid null-terminated string. It
550+ // also guarantees that it's valid for the duration of the callback.
551+ let name = unsafe { CStr :: from_char_ptr ( name) } ;
552+
553+ // SAFETY: The C API guarantees that `buffer` is at least `size` bytes in length.
554+ let buf = unsafe { core:: slice:: from_raw_parts_mut ( buffer. cast ( ) , size) } ;
555+ let len = T :: read_xattr ( inode, name, buf) ?;
556+ Ok ( len. try_into ( ) ?)
557+ } )
558+ }
559+
517560 const DIR_FILE_OPERATIONS : bindings:: file_operations = bindings:: file_operations {
518561 owner : ptr:: null_mut ( ) ,
519562 llseek : Some ( bindings:: generic_file_llseek) ,
0 commit comments