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

Commit 908591f

Browse files
committed
Split Seek into it's own trait
1 parent 2add1b1 commit 908591f

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/file_operations.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ unsafe extern "C" fn release_callback<T: FileOperations>(
7979
0
8080
}
8181

82-
unsafe extern "C" fn llseek_callback<T: FileOperations>(
82+
unsafe extern "C" fn llseek_callback<T: Seek>(
8383
file: *mut bindings::file,
8484
offset: bindings::loff_t,
8585
whence: c_types::c_int,
@@ -106,7 +106,6 @@ impl FileOperationsVtable {
106106
bindings::file_operations {
107107
open: Some(open_callback::<T>),
108108
release: Some(release_callback::<T>),
109-
llseek: Some(llseek_callback::<T>),
110109

111110
check_flags: None,
112111
#[cfg(not(kernel_4_20_0_or_greater))]
@@ -127,6 +126,7 @@ impl FileOperationsVtable {
127126
iterate_shared: None,
128127
#[cfg(kernel_5_1_0_or_greater)]
129128
iopoll: None,
129+
llseek: None,
130130
lock: None,
131131
mmap: None,
132132
#[cfg(kernel_4_15_0_or_greater)]
@@ -168,6 +168,13 @@ impl<T: Read> FileOperationsVtableBuilder<T> {
168168
}
169169
}
170170

171+
impl<T: Seek> FileOperationsVtableBuilder<T> {
172+
pub const fn seek(mut self) -> FileOperationsVtableBuilder<T> {
173+
self.0.llseek = Some(llseek_callback::<T>);
174+
self
175+
}
176+
}
177+
171178
/// `FileOperations` corresponds to the kernel's `struct file_operations`. You
172179
/// implement this trait whenever you'd create a `struct file_operations`, and
173180
/// also an additional trait for each function pointer in the
@@ -184,16 +191,16 @@ pub trait FileOperations: Sync + Sized {
184191
/// Creates a new instance of this file. Corresponds to the `open` function
185192
/// pointer in `struct file_operations`.
186193
fn open() -> KernelResult<Self>;
187-
188-
/// Changes the position of the file. Corresponds to the `llseek` function
189-
/// pointer in `struct file_operations`.
190-
fn seek(&self, _file: &File, _offset: SeekFrom) -> KernelResult<u64> {
191-
Err(Error::ESPIPE)
192-
}
193194
}
194195

195196
pub trait Read {
196197
/// Reads data from this file to userspace. Corresponds to the `read`
197198
/// function pointer in `struct file_operations`.
198199
fn read(&self, buf: &mut UserSlicePtrWriter, offset: u64) -> KernelResult<()>;
199200
}
201+
202+
pub trait Seek {
203+
/// Changes the position of the file. Corresponds to the `llseek` function
204+
/// pointer in `struct file_operations`.
205+
fn seek(&self, file: &File, offset: SeekFrom) -> KernelResult<u64>;
206+
}

tests/chrdev/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ struct SeekFile;
3636

3737
impl linux_kernel_module::file_operations::FileOperations for SeekFile {
3838
const VTABLE: linux_kernel_module::file_operations::FileOperationsVtable =
39-
linux_kernel_module::file_operations::FileOperationsVtable::builder::<Self>().build();
39+
linux_kernel_module::file_operations::FileOperationsVtable::builder::<Self>()
40+
.seek()
41+
.build();
4042

4143
fn open() -> linux_kernel_module::KernelResult<Self> {
4244
return Ok(SeekFile);
4345
}
46+
}
4447

48+
impl linux_kernel_module::file_operations::Seek for SeekFile {
4549
fn seek(
4650
&self,
4751
_file: &linux_kernel_module::file_operations::File,

0 commit comments

Comments
 (0)