[PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir

Danilo Krummrich posted 7 patches 4 months, 1 week ago
There is a newer version of this series
[PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir
Posted by Danilo Krummrich 4 months, 1 week ago
Add support for creating binary debugfs files via ScopedDir. This
mirrors the existing functionality for Dir, but without producing an
owning handle -- files are automatically removed when the associated
Scope is dropped.

Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
 rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
index 3c3bbcc126ef..0eb1719e4953 100644
--- a/rust/kernel/debugfs.rs
+++ b/rust/kernel/debugfs.rs
@@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
         self.create_file(name, data, &T::FILE_OPS)
     }
 
+    /// Creates a read-only binary file in this directory.
+    ///
+    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        self.create_file(name, data, &T::FILE_OPS)
+    }
+
     /// Creates a read-only file in this directory, with contents from a callback.
     ///
     /// The file contents are generated by calling `f` with `data`.
@@ -568,6 +582,22 @@ pub fn read_write_file<T: Writer + Reader + Send + Sync + 'static>(
         self.create_file(name, data, vtable)
     }
 
+    /// Creates a read-write binary file in this directory.
+    ///
+    /// Reading the file uses the [`BinaryWriter`] implementation on `data`. Writing to the file
+    /// uses the [`BinaryReader`] implementation on `data`.
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn read_write_binary_file<T: BinaryWriter + BinaryReader + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        let vtable = &<T as BinaryReadWriteFile<_>>::FILE_OPS;
+        self.create_file(name, data, vtable)
+    }
+
     /// Creates a read-write file in this directory, with logic from callbacks.
     ///
     /// Reading from the file is handled by `f`. Writing to the file is handled by `w`.
@@ -607,6 +637,21 @@ pub fn write_only_file<T: Reader + Send + Sync + 'static>(&self, name: &CStr, da
         self.create_file(name, data, vtable)
     }
 
+    /// Creates a write-only binary file in this directory.
+    ///
+    /// Writing to the file uses the [`BinaryReader`] implementation on `data`.
+    ///
+    /// This function does not produce an owning handle to the file. The created file is removed
+    /// when the [`Scope`] that this directory belongs to is dropped.
+    pub fn write_binary_file<T: BinaryReader + Send + Sync + 'static>(
+        &self,
+        name: &CStr,
+        data: &'data T,
+    ) {
+        let vtable = &<T as BinaryWriteFile<_>>::FILE_OPS;
+        self.create_file(name, data, vtable)
+    }
+
     /// Creates a write-only file in this directory, with write logic from a callback.
     ///
     /// Writing to the file is handled by `w`.
-- 
2.51.0
Re: [PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir
Posted by Alice Ryhl 3 months, 3 weeks ago
On Sat, Oct 04, 2025 at 12:26:43AM +0200, Danilo Krummrich wrote:
> Add support for creating binary debugfs files via ScopedDir. This
> mirrors the existing functionality for Dir, but without producing an
> owning handle -- files are automatically removed when the associated
> Scope is dropped.
> 
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
>  rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 45 insertions(+)
> 
> diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
> index 3c3bbcc126ef..0eb1719e4953 100644
> --- a/rust/kernel/debugfs.rs
> +++ b/rust/kernel/debugfs.rs
> @@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
>          self.create_file(name, data, &T::FILE_OPS)
>      }
>  
> +    /// Creates a read-only binary file in this directory.
> +    ///
> +    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
> +    ///
> +    /// This function does not produce an owning handle to the file. The created file is removed
> +    /// when the [`Scope`] that this directory belongs to is dropped.
> +    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
> +        &self,
> +        name: &CStr,
> +        data: &'data T,
> +    ) {
> +        self.create_file(name, data, &T::FILE_OPS)

Why isn't <T as MyTrait> need here when it's needed for the other
methods?

Alice
Re: [PATCH 6/7] rust: debugfs: support binary large objects for ScopedDir
Posted by Danilo Krummrich 3 months, 3 weeks ago
On Fri Oct 17, 2025 at 3:00 PM CEST, Alice Ryhl wrote:
> On Sat, Oct 04, 2025 at 12:26:43AM +0200, Danilo Krummrich wrote:
>> Add support for creating binary debugfs files via ScopedDir. This
>> mirrors the existing functionality for Dir, but without producing an
>> owning handle -- files are automatically removed when the associated
>> Scope is dropped.
>> 
>> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
>> ---
>>  rust/kernel/debugfs.rs | 45 ++++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 45 insertions(+)
>> 
>> diff --git a/rust/kernel/debugfs.rs b/rust/kernel/debugfs.rs
>> index 3c3bbcc126ef..0eb1719e4953 100644
>> --- a/rust/kernel/debugfs.rs
>> +++ b/rust/kernel/debugfs.rs
>> @@ -531,6 +531,20 @@ pub fn read_only_file<T: Writer + Send + Sync + 'static>(&self, name: &CStr, dat
>>          self.create_file(name, data, &T::FILE_OPS)
>>      }
>>  
>> +    /// Creates a read-only binary file in this directory.
>> +    ///
>> +    /// The file's contents are produced by invoking [`BinaryWriter::write_to_slice`].
>> +    ///
>> +    /// This function does not produce an owning handle to the file. The created file is removed
>> +    /// when the [`Scope`] that this directory belongs to is dropped.
>> +    pub fn read_binary_file<T: BinaryWriter + Send + Sync + 'static>(
>> +        &self,
>> +        name: &CStr,
>> +        data: &'data T,
>> +    ) {
>> +        self.create_file(name, data, &T::FILE_OPS)
>
> Why isn't <T as MyTrait> need here when it's needed for the other
> methods?

It's not needed for write_binary_file() I think, but read_write_binary_file()
needs it because:

	fn read_write_binary_file<T: BinaryWriter + BinaryReader + Send + Sync + 'static>()

So, just &T::FILE_OPS is ambiguous, because it implements BinaryReadFile,
BinaryWriteFile and BinaryReadWriteFile.