@@ -36,6 +36,14 @@ pub trait FileSystem {
3636
3737 /// Reads the contents of the inode into the given folio.
3838 fn read_folio ( inode : & INode < Self > , folio : LockedFolio < ' _ > ) -> Result ;
39+
40+ /// Reads an xattr.
41+ ///
42+ /// Returns the number of bytes written to `outbuf`. If it is too small, returns the number of
43+ /// bytes needs to hold the attribute.
44+ fn read_xattr ( _inode : & INode < Self > , _name : & CStr , _outbuf : & mut [ u8 ] ) -> Result < usize > {
45+ Err ( EOPNOTSUPP )
46+ }
3947}
4048
4149/// The types of directory entries reported by [`FileSystem::read_dir`].
@@ -409,6 +417,7 @@ impl<'a, T: FileSystem + ?Sized> NewSuperBlock<'a, T> {
409417
410418 sb. s_magic = params. magic as _ ;
411419 sb. s_op = & Tables :: < T > :: SUPER_BLOCK ;
420+ sb. s_xattr = & Tables :: < T > :: XATTR_HANDLERS [ 0 ] ;
412421 sb. s_maxbytes = params. maxbytes ;
413422 sb. s_time_gran = params. time_gran ;
414423 sb. s_blocksize_bits = params. blocksize_bits ;
@@ -509,6 +518,40 @@ impl<T: FileSystem + ?Sized> Tables<T> {
509518 shutdown : None ,
510519 } ;
511520
521+ const XATTR_HANDLERS : [ * const bindings:: xattr_handler ; 2 ] = [ & Self :: XATTR_HANDLER , ptr:: null ( ) ] ;
522+
523+ const XATTR_HANDLER : bindings:: xattr_handler = bindings:: xattr_handler {
524+ name : ptr:: null ( ) ,
525+ prefix : crate :: c_str!( "" ) . as_char_ptr ( ) ,
526+ flags : 0 ,
527+ list : None ,
528+ get : Some ( Self :: xattr_get_callback) ,
529+ set : None ,
530+ } ;
531+
532+ unsafe extern "C" fn xattr_get_callback (
533+ _handler : * const bindings:: xattr_handler ,
534+ _dentry : * mut bindings:: dentry ,
535+ inode_ptr : * mut bindings:: inode ,
536+ name : * const core:: ffi:: c_char ,
537+ buffer : * mut core:: ffi:: c_void ,
538+ size : usize ,
539+ ) -> core:: ffi:: c_int {
540+ from_result ( || {
541+ // SAFETY: The C API guarantees that `inode_ptr` is a valid inode.
542+ let inode = unsafe { & * inode_ptr. cast :: < INode < T > > ( ) } ;
543+
544+ // SAFETY: The c API guarantees that `name` is a valid null-terminated string. It
545+ // also guarantees that it's valid for the duration of the callback.
546+ let name = unsafe { CStr :: from_char_ptr ( name) } ;
547+
548+ // SAFETY: The C API guarantees that `buffer` is at least `size` bytes in length.
549+ let buf = unsafe { core:: slice:: from_raw_parts_mut ( buffer. cast ( ) , size) } ;
550+ let len = T :: read_xattr ( inode, name, buf) ?;
551+ Ok ( len. try_into ( ) ?)
552+ } )
553+ }
554+
512555 const DIR_FILE_OPERATIONS : bindings:: file_operations = bindings:: file_operations {
513556 owner : ptr:: null_mut ( ) ,
514557 llseek : Some ( bindings:: generic_file_llseek) ,
0 commit comments