Skip to content

Commit a45aff4

Browse files
authored
Merge pull request #153 from wedsonaf/read-write
Change `read` and `write` to return the number of bytes read/written.
2 parents 5b225b7 + da604d8 commit a45aff4

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

rust/kernel/file_operations.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ unsafe extern "C" fn read_callback<T: FileOperations>(
104104
let f = &*((*file).private_data as *const T);
105105
// No `FMODE_UNSIGNED_OFFSET` support, so `offset` must be in [0, 2^63).
106106
// See discussion in https://github.com/fishinabarrel/linux-kernel-module-rust/pull/113
107-
T::read(f, &File::from_ptr(file), &mut data, (*offset).try_into()?)?;
108-
let written = len - data.len();
109-
(*offset) += bindings::loff_t::try_from(written).unwrap();
110-
Ok(written.try_into().unwrap())
107+
let read = f.read(&File::from_ptr(file), &mut data, (*offset).try_into()?)?;
108+
(*offset) += bindings::loff_t::try_from(read).unwrap();
109+
Ok(read as _)
111110
}
112111
}
113112

@@ -122,10 +121,9 @@ unsafe extern "C" fn write_callback<T: FileOperations>(
122121
let f = &*((*file).private_data as *const T);
123122
// No `FMODE_UNSIGNED_OFFSET` support, so `offset` must be in [0, 2^63).
124123
// See discussion in https://github.com/fishinabarrel/linux-kernel-module-rust/pull/113
125-
T::write(f, &mut data, (*offset).try_into()?)?;
126-
let read = len - data.len();
127-
(*offset) += bindings::loff_t::try_from(read).unwrap();
128-
Ok(read.try_into().unwrap())
124+
let written = f.write(&mut data, (*offset).try_into()?)?;
125+
(*offset) += bindings::loff_t::try_from(written).unwrap();
126+
Ok(written as _)
129127
}
130128
}
131129

@@ -441,14 +439,19 @@ pub trait FileOperations: Send + Sync + Sized {
441439
/// Reads data from this file to userspace.
442440
///
443441
/// Corresponds to the `read` function pointer in `struct file_operations`.
444-
fn read(&self, _file: &File, _data: &mut UserSlicePtrWriter, _offset: u64) -> KernelResult {
442+
fn read(
443+
&self,
444+
_file: &File,
445+
_data: &mut UserSlicePtrWriter,
446+
_offset: u64,
447+
) -> KernelResult<usize> {
445448
Err(Error::EINVAL)
446449
}
447450

448451
/// Writes data from userspace to this file.
449452
///
450453
/// Corresponds to the `write` function pointer in `struct file_operations`.
451-
fn write(&self, _data: &mut UserSlicePtrReader, _offset: u64) -> KernelResult<isize> {
454+
fn write(&self, _data: &mut UserSlicePtrReader, _offset: u64) -> KernelResult<usize> {
452455
Err(Error::EINVAL)
453456
}
454457

0 commit comments

Comments
 (0)