Skip to content

Commit

Permalink
Merge pull request raspberrypi#709 from wedsonaf/file-operations
Browse files Browse the repository at this point in the history
rust: rename `FileOperations::Wrapper` to `FileOperations::Data`
  • Loading branch information
wedsonaf authored Mar 15, 2022
2 parents c3d9558 + 281c64a commit 7a8aaeb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions drivers/android/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,16 +806,16 @@ impl IoctlHandler for Process {
}

impl FileOperations for Process {
type Wrapper = Ref<Self>;
type Data = Ref<Self>;
type OpenData = Ref<Context>;

kernel::declare_file_operations!(ioctl, compat_ioctl, mmap, poll);

fn open(ctx: &Ref<Context>, file: &File) -> Result<Self::Wrapper> {
fn open(ctx: &Ref<Context>, file: &File) -> Result<Self::Data> {
Self::new(ctx.clone(), file.cred().clone())
}

fn release(obj: Self::Wrapper, _file: &File) {
fn release(obj: Self::Data, _file: &File) {
// Mark this process as dead. We'll do the same for the threads later.
obj.inner.lock().is_dead = true;

Expand Down
76 changes: 38 additions & 38 deletions rust/kernel/file_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
from_kernel_result! {
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).writer() };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
// 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
let read = T::read(
Expand All @@ -148,11 +148,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
let file = unsafe { (*iocb).ki_filp };
let offset = unsafe { (*iocb).ki_pos };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let read =
T::read(f, unsafe { &FileRef::from_ptr(file) }, &mut iter, offset.try_into()?)?;
unsafe { (*iocb).ki_pos += bindings::loff_t::try_from(read).unwrap() };
Expand All @@ -169,11 +169,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
from_kernel_result! {
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).reader() };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
// 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
let written = T::write(
Expand All @@ -196,11 +196,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
let file = unsafe { (*iocb).ki_filp };
let offset = unsafe { (*iocb).ki_pos };
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let written =
T::write(f, unsafe { &FileRef::from_ptr(file) }, &mut iter, offset.try_into()?)?;
unsafe { (*iocb).ki_pos += bindings::loff_t::try_from(written).unwrap() };
Expand All @@ -213,7 +213,7 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
file: *mut bindings::file,
) -> c_types::c_int {
let ptr = mem::replace(unsafe { &mut (*file).private_data }, ptr::null_mut());
T::release(unsafe { T::Wrapper::from_pointer(ptr as _) }, unsafe {
T::release(unsafe { T::Data::from_pointer(ptr as _) }, unsafe {
&FileRef::from_ptr(file)
});
0
Expand All @@ -232,11 +232,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
_ => return Err(Error::EINVAL),
};
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let off = T::seek(f, unsafe { &FileRef::from_ptr(file) }, off)?;
Ok(off as bindings::loff_t)
}
Expand All @@ -249,11 +249,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
) -> c_types::c_long {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let mut cmd = IoctlCommand::new(cmd as _, arg as _);
let ret = T::ioctl(f, unsafe { &FileRef::from_ptr(file) }, &mut cmd)?;
Ok(ret as _)
Expand All @@ -267,11 +267,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
) -> c_types::c_long {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let mut cmd = IoctlCommand::new(cmd as _, arg as _);
let ret = T::compat_ioctl(f, unsafe { &FileRef::from_ptr(file) }, &mut cmd)?;
Ok(ret as _)
Expand All @@ -284,11 +284,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
) -> c_types::c_int {
from_kernel_result! {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };

// SAFETY: The C API guarantees that `vma` is valid for the duration of this call.
// `area` only lives within this call, so it is guaranteed to be valid.
Expand All @@ -312,11 +312,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
let end = end.try_into()?;
let datasync = datasync != 0;
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
// `release` callback, which the C API guarantees that will be called only when all
// references to `file` have been released, so we know it can't be called while this
// function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
let res = T::fsync(f, unsafe { &FileRef::from_ptr(file) }, start, end, datasync)?;
Ok(res.try_into().unwrap())
}
Expand All @@ -327,10 +327,10 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
wait: *mut bindings::poll_table_struct,
) -> bindings::__poll_t {
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the `release`
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the `release`
// callback, which the C API guarantees that will be called only when all references to
// `file` have been released, so we know it can't be called while this function is running.
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
let f = unsafe { T::Data::borrow((*file).private_data) };
match T::poll(f, unsafe { &FileRef::from_ptr(file) }, unsafe {
&PollTable::from_ptr(wait)
}) {
Expand Down Expand Up @@ -619,32 +619,33 @@ pub trait FileOperations {
/// The methods to use to populate [`struct file_operations`].
const TO_USE: ToUse;

/// The pointer type that will be used to hold ourselves.
type Wrapper: PointerWrapper + Send + Sync = ();
/// The type of the context data returned by [`FileOperations::open`] and made available to
/// other methods.
type Data: PointerWrapper + Send + Sync = ();

/// The type of the context data passed to [`FileOperations::open`].
type OpenData: Sync = ();

/// Creates a new instance of this file.
///
/// Corresponds to the `open` function pointer in `struct file_operations`.
fn open(context: &Self::OpenData, file: &File) -> Result<Self::Wrapper>;
fn open(context: &Self::OpenData, file: &File) -> Result<Self::Data>;

/// Cleans up after the last reference to the file goes away.
///
/// Note that the object is moved, so it will be freed automatically unless the implementation
/// moves it elsewhere.
/// Note that context data is moved, so it will be freed automatically unless the
/// implementation moves it elsewhere.
///
/// Corresponds to the `release` function pointer in `struct file_operations`.
fn release(_obj: Self::Wrapper, _file: &File) {}
fn release(_data: Self::Data, _file: &File) {}

/// Reads data from this file to the caller's buffer.
///
/// Corresponds to the `read` and `read_iter` function pointers in `struct file_operations`.
fn read(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_data: &mut impl IoBufferWriter,
_writer: &mut impl IoBufferWriter,
_offset: u64,
) -> Result<usize> {
Err(Error::EINVAL)
Expand All @@ -654,9 +655,9 @@ pub trait FileOperations {
///
/// Corresponds to the `write` and `write_iter` function pointers in `struct file_operations`.
fn write(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_data: &mut impl IoBufferReader,
_reader: &mut impl IoBufferReader,
_offset: u64,
) -> Result<usize> {
Err(Error::EINVAL)
Expand All @@ -666,7 +667,7 @@ pub trait FileOperations {
///
/// Corresponds to the `llseek` function pointer in `struct file_operations`.
fn seek(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_offset: SeekFrom,
) -> Result<u64> {
Expand All @@ -677,7 +678,7 @@ pub trait FileOperations {
///
/// Corresponds to the `unlocked_ioctl` function pointer in `struct file_operations`.
fn ioctl(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_cmd: &mut IoctlCommand,
) -> Result<i32> {
Expand All @@ -688,7 +689,7 @@ pub trait FileOperations {
///
/// Corresponds to the `compat_ioctl` function pointer in `struct file_operations`.
fn compat_ioctl(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_cmd: &mut IoctlCommand,
) -> Result<i32> {
Expand All @@ -699,7 +700,7 @@ pub trait FileOperations {
///
/// Corresponds to the `fsync` function pointer in `struct file_operations`.
fn fsync(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_start: u64,
_end: u64,
Expand All @@ -711,9 +712,8 @@ pub trait FileOperations {
/// Maps areas of the caller's virtual memory with device/file memory.
///
/// Corresponds to the `mmap` function pointer in `struct file_operations`.
/// TODO: Wrap `vm_area_struct` so that we don't have to expose it.
fn mmap(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_vma: &mut mm::virt::Area,
) -> Result {
Expand All @@ -725,7 +725,7 @@ pub trait FileOperations {
///
/// Corresponds to the `poll` function pointer in `struct file_operations`.
fn poll(
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
_file: &File,
_table: &PollTable,
) -> Result<u32> {
Expand Down
4 changes: 2 additions & 2 deletions samples/rust/rust_miscdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ impl SharedState {

struct Token;
impl FileOperations for Token {
type Wrapper = Ref<SharedState>;
type Data = Ref<SharedState>;
type OpenData = Ref<SharedState>;

kernel::declare_file_operations!(read, write);

fn open(shared: &Ref<SharedState>, _file: &File) -> Result<Self::Wrapper> {
fn open(shared: &Ref<SharedState>, _file: &File) -> Result<Self::Data> {
Ok(shared.clone())
}

Expand Down
2 changes: 1 addition & 1 deletion samples/rust/rust_semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl FileState {
}

impl FileOperations for FileState {
type Wrapper = Box<Self>;
type Data = Box<Self>;
type OpenData = Ref<Semaphore>;

declare_file_operations!(read, write, ioctl);
Expand Down

0 comments on commit 7a8aaeb

Please sign in to comment.