Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 2add1b1

Browse files
committed
Split Read into it's own trait
1 parent 71fc1ba commit 2add1b1

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

src/file_operations.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
168176
pub 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+
}

tests/chrdev/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ struct CycleFile;
66

77
impl linux_kernel_module::file_operations::FileOperations for CycleFile {
88
const VTABLE: linux_kernel_module::file_operations::FileOperationsVtable =
9-
linux_kernel_module::file_operations::FileOperationsVtable::builder::<Self>().build();
9+
linux_kernel_module::file_operations::FileOperationsVtable::builder::<Self>()
10+
.read()
11+
.build();
1012

1113
fn open() -> linux_kernel_module::KernelResult<Self> {
1214
return Ok(CycleFile);
1315
}
14-
16+
}
17+
impl linux_kernel_module::file_operations::Read for CycleFile {
1518
fn read(
1619
&self,
1720
buf: &mut linux_kernel_module::user_ptr::UserSlicePtrWriter,

0 commit comments

Comments
 (0)