Re: [PATCH v3 06/10] rust: debugfs: support for binary large objects
From: Alice Ryhl
Date: Fri Oct 24 2025 - 06:37:24 EST
On Thu, Oct 23, 2025 at 12:21:24PM +0200, Alice Ryhl wrote:
> On Thu, Oct 23, 2025 at 12:09 PM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
> >
> > On Thu Oct 23, 2025 at 10:26 AM CEST, Alice Ryhl wrote:
> > > On Wed, Oct 22, 2025 at 04:30:40PM +0200, Danilo Krummrich wrote:
> > >> Introduce support for read-only, write-only, and read-write binary files
> > >> in Rust debugfs. This adds:
> > >>
> > >> - BinaryWriter and BinaryReader traits for writing to and reading from
> > >> user slices in binary form.
> > >> - New Dir methods: read_binary_file(), write_binary_file(),
> > >> `read_write_binary_file`.
> > >> - Corresponding FileOps implementations: BinaryReadFile,
> > >> BinaryWriteFile, BinaryReadWriteFile.
> > >>
> > >> This allows kernel modules to expose arbitrary binary data through
> > >> debugfs, with proper support for offsets and partial reads/writes.
> > >>
> > >> Reviewed-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
> > >> Reviewed-by: Matthew Maurer <mmaurer@xxxxxxxxxx>
> > >> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
> > >
> > >> +extern "C" fn blob_read<T: BinaryWriter>(
> > >> + file: *mut bindings::file,
> > >> + buf: *mut c_char,
> > >> + count: usize,
> > >> + ppos: *mut bindings::loff_t,
> > >> +) -> isize {
> > >> + // SAFETY:
> > >> + // - `file` is a valid pointer to a `struct file`.
> > >> + // - The type invariant of `FileOps` guarantees that `private_data` points to a valid `T`.
> > >> + let this = unsafe { &*((*file).private_data.cast::<T>()) };
> > >> +
> > >> + // SAFETY:
> > >> + // `ppos` is a valid `file::Offset` pointer.
> > >> + // We have exclusive access to `ppos`.
> > >> + let pos = unsafe { file::Offset::from_raw(ppos) };
> > >> +
> > >> + let mut writer = UserSlice::new(UserPtr::from_ptr(buf.cast()), count).writer();
> > >> +
> > >> + let ret = || -> Result<isize> {
> > >> + let written = this.write_to_slice(&mut writer, pos)?;
> > >> +
> > >> + Ok(written.try_into()?)
> > >
> > > Hmm ... a conversion? Sounds like write_to_slice() has the wrong return
> > > type.
> >
> > write_to_slice() returns the number of bytes written as usize, which seems
> > correct, no?
>
> Yes, you're right, I think usize is the right value. The cast is
> unfortunate, but it can't really be avoided. In practice it should
> never fail because slice lengths always fit in an isize, but isize
> isn't the right type.
>
> Alice
Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>