Skip to content

Commit d1367c9

Browse files
committed
rust: fs: introduce FileSystem::read_xattr
This allows file systems to expose xattrs associated with inodes. Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
1 parent 31d63a6 commit d1367c9

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

rust/bindings/bindings_helper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/refcount.h>
1616
#include <linux/wait.h>
1717
#include <linux/sched.h>
18+
#include <linux/xattr.h>
1819

1920
/* `bindgen` gets confused at certain things. */
2021
const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;

rust/kernel/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub mod code {
8181
declare_err!(EIOCBQUEUED, "iocb queued, will get completion event.");
8282
declare_err!(ERECALLCONFLICT, "Conflict with recalled state.");
8383
declare_err!(ENOGRACE, "NFS file lock reclaim refused.");
84+
declare_err!(ENODATA, "No data available.");
85+
declare_err!(EOPNOTSUPP, "Operation not supported on transport endpoint.");
8486
}
8587

8688
/// Generic integer kernel error.

rust/kernel/fs.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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`].
@@ -406,6 +414,7 @@ impl<'a, T: FileSystem + ?Sized> NewSuperBlock<'a, T> {
406414

407415
sb.s_magic = params.magic as _;
408416
sb.s_op = &Tables::<T>::SUPER_BLOCK;
417+
sb.s_xattr = &Tables::<T>::XATTR_HANDLERS[0];
409418
sb.s_maxbytes = params.maxbytes;
410419
sb.s_time_gran = params.time_gran;
411420
sb.s_blocksize_bits = params.blocksize_bits;
@@ -506,6 +515,40 @@ impl<T: FileSystem + ?Sized> Tables<T> {
506515
shutdown: None,
507516
};
508517

518+
const XATTR_HANDLERS: [*const bindings::xattr_handler; 2] = [&Self::XATTR_HANDLER, ptr::null()];
519+
520+
const XATTR_HANDLER: bindings::xattr_handler = bindings::xattr_handler {
521+
name: ptr::null(),
522+
prefix: crate::c_str!("").as_char_ptr(),
523+
flags: 0,
524+
list: None,
525+
get: Some(Self::xattr_get_callback),
526+
set: None,
527+
};
528+
529+
unsafe extern "C" fn xattr_get_callback(
530+
_handler: *const bindings::xattr_handler,
531+
_dentry: *mut bindings::dentry,
532+
inode_ptr: *mut bindings::inode,
533+
name: *const core::ffi::c_char,
534+
buffer: *mut core::ffi::c_void,
535+
size: usize,
536+
) -> core::ffi::c_int {
537+
from_result(|| {
538+
// SAFETY: The C API guarantees that `inode_ptr` is a valid inode.
539+
let inode = unsafe { &*inode_ptr.cast::<INode<T>>() };
540+
541+
// SAFETY: The c API guarantees that `name` is a valid null-terminated string. It
542+
// also guarantees that it's valid for the duration of the callback.
543+
let name = unsafe { CStr::from_char_ptr(name) };
544+
545+
// SAFETY: The C API guarantees that `buffer` is at least `size` bytes in length.
546+
let buf = unsafe { core::slice::from_raw_parts_mut(buffer.cast(), size) };
547+
let len = T::read_xattr(inode, name, buf)?;
548+
Ok(len.try_into()?)
549+
})
550+
}
551+
509552
const DIR_FILE_OPERATIONS: bindings::file_operations = bindings::file_operations {
510553
owner: ptr::null_mut(),
511554
llseek: Some(bindings::generic_file_llseek),

0 commit comments

Comments
 (0)