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

Add fsync file operation #26

Merged
merged 2 commits into from
Oct 17, 2020
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
34 changes: 33 additions & 1 deletion rust/kernel/src/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ unsafe extern "C" fn llseek_callback<T: FileOperations>(
}
}

unsafe extern "C" fn fsync_callback<T: FileOperations>(
file: *mut bindings::file,
start: bindings::loff_t,
end: bindings::loff_t,
datasync: c_types::c_int,
) -> c_types::c_int {
let start = match start.try_into() {
Ok(v) => v,
Err(_) => return Error::EINVAL.to_kernel_errno(),
};
let end = match end.try_into() {
Ok(v) => v,
Err(_) => return Error::EINVAL.to_kernel_errno(),
};
let datasync = datasync != 0;
let fsync = T::FSYNC.unwrap();
let f = &*((*file).private_data as *const T);
match fsync(f, &File::from_ptr(file), start, end, datasync) {
Ok(result) => result.try_into().unwrap(),
Err(e) => e.to_kernel_errno(),
}
}

pub(crate) struct FileOperationsVtable<T>(marker::PhantomData<T>);

impl<T: FileOperations> FileOperationsVtable<T> {
Expand Down Expand Up @@ -170,7 +193,11 @@ impl<T: FileOperations> FileOperationsVtable<T> {
fasync: None,
flock: None,
flush: None,
fsync: None,
fsync: if let Some(_) = T::FSYNC {
Some(fsync_callback::<T>)
} else {
None
},
get_unmapped_area: None,
iterate: None,
iterate_shared: None,
Expand All @@ -195,6 +222,7 @@ impl<T: FileOperations> FileOperationsVtable<T> {
pub type ReadFn<T> = Option<fn(&T, &File, &mut UserSlicePtrWriter, u64) -> KernelResult<()>>;
pub type WriteFn<T> = Option<fn(&T, &mut UserSlicePtrReader, u64) -> KernelResult<()>>;
pub type SeekFn<T> = Option<fn(&T, &File, SeekFrom) -> KernelResult<u64>>;
pub type FSync<T> = Option<fn(&T, &File, u64, u64, bool) -> KernelResult<u32>>;

/// `FileOperations` corresponds to the kernel's `struct file_operations`. You
/// implement this trait whenever you'd create a `struct file_operations`.
Expand All @@ -216,4 +244,8 @@ pub trait FileOperations: Sync + Sized {
/// Changes the position of the file. Corresponds to the `llseek` function
/// pointer in `struct file_operations`.
const SEEK: SeekFn<Self> = None;

/// Syncs pending changes to this file. Corresponds to the `fsync` function
/// pointer in the `struct file_operations`.
const FSYNC: FSync<Self> = None;
}