@@ -43,7 +43,7 @@ unsafe extern "C" fn open_callback<T: FileOperations>(
4343 0
4444}
4545
46- unsafe extern "C" fn read_callback < T : FileOperations > (
46+ unsafe extern "C" fn read_callback < T : Read > (
4747 file : * mut bindings:: file ,
4848 buf : * mut c_types:: c_char ,
4949 len : c_types:: c_size_t ,
@@ -105,7 +105,6 @@ impl FileOperationsVtable {
105105 FileOperationsVtableBuilder (
106106 bindings:: file_operations {
107107 open : Some ( open_callback :: < T > ) ,
108- read : Some ( read_callback :: < T > ) ,
109108 release : Some ( release_callback :: < T > ) ,
110109 llseek : Some ( llseek_callback :: < T > ) ,
111110
@@ -134,6 +133,7 @@ impl FileOperationsVtable {
134133 mmap_supported_flags : 0 ,
135134 owner : ptr:: null_mut ( ) ,
136135 poll : None ,
136+ read : None ,
137137 read_iter : None ,
138138 #[ cfg( kernel_4_20_0_or_greater) ]
139139 remap_file_range : None ,
@@ -161,10 +161,18 @@ impl<T> FileOperationsVtableBuilder<T> {
161161 }
162162}
163163
164+ impl < T : Read > FileOperationsVtableBuilder < T > {
165+ pub const fn read ( mut self ) -> FileOperationsVtableBuilder < T > {
166+ self . 0 . read = Some ( read_callback :: < T > ) ;
167+ self
168+ }
169+ }
170+
164171/// `FileOperations` corresponds to the kernel's `struct file_operations`. You
165- /// implement this trait whenever you'd create a `struct file_operations`. File
166- /// descriptors may be used from multiple threads (or processes) concurrently,
167- /// so your type must be `Sync`.
172+ /// implement this trait whenever you'd create a `struct file_operations`, and
173+ /// also an additional trait for each function pointer in the
174+ /// `struct file_operations`. File descriptors may be used from multiple threads
175+ /// (or processes) concurrently, so your type must be `Sync`.
168176pub trait FileOperations : Sync + Sized {
169177 /// A container for the actual `file_operations` value. This will always be:
170178 /// ```
@@ -177,15 +185,15 @@ pub trait FileOperations: Sync + Sized {
177185 /// pointer in `struct file_operations`.
178186 fn open ( ) -> KernelResult < Self > ;
179187
180- /// Reads data from this file to userspace. Corresponds to the `read`
181- /// function pointer in `struct file_operations`.
182- fn read ( & self , _buf : & mut UserSlicePtrWriter , _offset : u64 ) -> KernelResult < ( ) > {
183- Err ( Error :: EINVAL )
184- }
185-
186188 /// Changes the position of the file. Corresponds to the `llseek` function
187189 /// pointer in `struct file_operations`.
188190 fn seek ( & self , _file : & File , _offset : SeekFrom ) -> KernelResult < u64 > {
189191 Err ( Error :: ESPIPE )
190192 }
191193}
194+
195+ pub trait Read {
196+ /// Reads data from this file to userspace. Corresponds to the `read`
197+ /// function pointer in `struct file_operations`.
198+ fn read ( & self , buf : & mut UserSlicePtrWriter , offset : u64 ) -> KernelResult < ( ) > ;
199+ }
0 commit comments