Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change read and write to return the number of bytes read/written. #153

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions rust/kernel/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ unsafe extern "C" fn read_callback<T: FileOperations>(
let f = &*((*file).private_data as *const T);
// No `FMODE_UNSIGNED_OFFSET` support, so `offset` must be in [0, 2^63).
// See discussion in https://github.com/fishinabarrel/linux-kernel-module-rust/pull/113
T::read(f, &File::from_ptr(file), &mut data, (*offset).try_into()?)?;
let written = len - data.len();
(*offset) += bindings::loff_t::try_from(written).unwrap();
Ok(written.try_into().unwrap())
let read = f.read(&File::from_ptr(file), &mut data, (*offset).try_into()?)?;
(*offset) += bindings::loff_t::try_from(read).unwrap();
Ok(read as _)
}
}

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

Expand Down Expand Up @@ -441,14 +439,19 @@ pub trait FileOperations: Send + Sync + Sized {
/// Reads data from this file to userspace.
///
/// Corresponds to the `read` function pointer in `struct file_operations`.
fn read(&self, _file: &File, _data: &mut UserSlicePtrWriter, _offset: u64) -> KernelResult {
fn read(
&self,
_file: &File,
_data: &mut UserSlicePtrWriter,
_offset: u64,
) -> KernelResult<usize> {
Err(Error::EINVAL)
}

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

Expand Down