From a68072dc34ebe430d47e22faff07d684302e0bff Mon Sep 17 00:00:00 2001 From: adamrk Date: Sat, 17 Oct 2020 14:33:30 +0000 Subject: [PATCH 1/2] Add fsync file operation --- rust/kernel/src/file_operations.rs | 34 +++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/rust/kernel/src/file_operations.rs b/rust/kernel/src/file_operations.rs index 100fb62281e946..00bfb2866315b9 100644 --- a/rust/kernel/src/file_operations.rs +++ b/rust/kernel/src/file_operations.rs @@ -140,6 +140,29 @@ unsafe extern "C" fn llseek_callback( } } +unsafe extern "C" fn fsync_callback( + 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 as c_types::c_int, + Err(e) => e.to_kernel_errno(), + } +} + pub(crate) struct FileOperationsVtable(marker::PhantomData); impl FileOperationsVtable { @@ -170,7 +193,11 @@ impl FileOperationsVtable { fasync: None, flock: None, flush: None, - fsync: None, + fsync: if let Some(_) = T::FSYNC { + Some(fsync_callback::) + } else { + None + }, get_unmapped_area: None, iterate: None, iterate_shared: None, @@ -195,6 +222,7 @@ impl FileOperationsVtable { pub type ReadFn = Option KernelResult<()>>; pub type WriteFn = Option KernelResult<()>>; pub type SeekFn = Option KernelResult>; +pub type FSync = Option KernelResult>; /// `FileOperations` corresponds to the kernel's `struct file_operations`. You /// implement this trait whenever you'd create a `struct file_operations`. @@ -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 = None; + + /// Syncs pending changes to this file. Corresponds to the `fsync` function + /// pointer in the `struct file_operations`. + const FSYNC: FSync = None; } From 0ad447f94809bfb032aa91772e4bb3b8cedbb7bd Mon Sep 17 00:00:00 2001 From: adamrk Date: Sat, 17 Oct 2020 19:53:35 +0000 Subject: [PATCH 2/2] Convert return type with try_into --- rust/kernel/src/file_operations.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/src/file_operations.rs b/rust/kernel/src/file_operations.rs index 00bfb2866315b9..1b8d7e3d73a3d4 100644 --- a/rust/kernel/src/file_operations.rs +++ b/rust/kernel/src/file_operations.rs @@ -158,7 +158,7 @@ unsafe extern "C" fn fsync_callback( 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 as c_types::c_int, + Ok(result) => result.try_into().unwrap(), Err(e) => e.to_kernel_errno(), } }