Skip to content

Commit

Permalink
Merge pull request thesofproject#26 from adamrk/add-fsync
Browse files Browse the repository at this point in the history
Add fsync file operation
  • Loading branch information
alex authored Oct 17, 2020
2 parents f60edb2 + 0ad447f commit fe8d5f6
Showing 1 changed file with 33 additions and 1 deletion.
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;
}

0 comments on commit fe8d5f6

Please sign in to comment.