Skip to content

Commit 7a8aaeb

Browse files
authored
Merge pull request raspberrypi#709 from wedsonaf/file-operations
rust: rename `FileOperations::Wrapper` to `FileOperations::Data`
2 parents c3d9558 + 281c64a commit 7a8aaeb

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

drivers/android/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,16 +806,16 @@ impl IoctlHandler for Process {
806806
}
807807

808808
impl FileOperations for Process {
809-
type Wrapper = Ref<Self>;
809+
type Data = Ref<Self>;
810810
type OpenData = Ref<Context>;
811811

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

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

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

rust/kernel/file_operations.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
121121
from_kernel_result! {
122122
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).writer() };
123123
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
124-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
124+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
125125
// `release` callback, which the C API guarantees that will be called only when all
126126
// references to `file` have been released, so we know it can't be called while this
127127
// function is running.
128-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
128+
let f = unsafe { T::Data::borrow((*file).private_data) };
129129
// No `FMODE_UNSIGNED_OFFSET` support, so `offset` must be in [0, 2^63).
130130
// See discussion in https://github.com/fishinabarrel/linux-kernel-module-rust/pull/113
131131
let read = T::read(
@@ -148,11 +148,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
148148
let file = unsafe { (*iocb).ki_filp };
149149
let offset = unsafe { (*iocb).ki_pos };
150150
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
151-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
151+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
152152
// `release` callback, which the C API guarantees that will be called only when all
153153
// references to `file` have been released, so we know it can't be called while this
154154
// function is running.
155-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
155+
let f = unsafe { T::Data::borrow((*file).private_data) };
156156
let read =
157157
T::read(f, unsafe { &FileRef::from_ptr(file) }, &mut iter, offset.try_into()?)?;
158158
unsafe { (*iocb).ki_pos += bindings::loff_t::try_from(read).unwrap() };
@@ -169,11 +169,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
169169
from_kernel_result! {
170170
let mut data = unsafe { UserSlicePtr::new(buf as *mut c_types::c_void, len).reader() };
171171
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
172-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
172+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
173173
// `release` callback, which the C API guarantees that will be called only when all
174174
// references to `file` have been released, so we know it can't be called while this
175175
// function is running.
176-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
176+
let f = unsafe { T::Data::borrow((*file).private_data) };
177177
// No `FMODE_UNSIGNED_OFFSET` support, so `offset` must be in [0, 2^63).
178178
// See discussion in https://github.com/fishinabarrel/linux-kernel-module-rust/pull/113
179179
let written = T::write(
@@ -196,11 +196,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
196196
let file = unsafe { (*iocb).ki_filp };
197197
let offset = unsafe { (*iocb).ki_pos };
198198
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
199-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
199+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
200200
// `release` callback, which the C API guarantees that will be called only when all
201201
// references to `file` have been released, so we know it can't be called while this
202202
// function is running.
203-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
203+
let f = unsafe { T::Data::borrow((*file).private_data) };
204204
let written =
205205
T::write(f, unsafe { &FileRef::from_ptr(file) }, &mut iter, offset.try_into()?)?;
206206
unsafe { (*iocb).ki_pos += bindings::loff_t::try_from(written).unwrap() };
@@ -213,7 +213,7 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
213213
file: *mut bindings::file,
214214
) -> c_types::c_int {
215215
let ptr = mem::replace(unsafe { &mut (*file).private_data }, ptr::null_mut());
216-
T::release(unsafe { T::Wrapper::from_pointer(ptr as _) }, unsafe {
216+
T::release(unsafe { T::Data::from_pointer(ptr as _) }, unsafe {
217217
&FileRef::from_ptr(file)
218218
});
219219
0
@@ -232,11 +232,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
232232
_ => return Err(Error::EINVAL),
233233
};
234234
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
235-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
235+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
236236
// `release` callback, which the C API guarantees that will be called only when all
237237
// references to `file` have been released, so we know it can't be called while this
238238
// function is running.
239-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
239+
let f = unsafe { T::Data::borrow((*file).private_data) };
240240
let off = T::seek(f, unsafe { &FileRef::from_ptr(file) }, off)?;
241241
Ok(off as bindings::loff_t)
242242
}
@@ -249,11 +249,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
249249
) -> c_types::c_long {
250250
from_kernel_result! {
251251
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
252-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
252+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
253253
// `release` callback, which the C API guarantees that will be called only when all
254254
// references to `file` have been released, so we know it can't be called while this
255255
// function is running.
256-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
256+
let f = unsafe { T::Data::borrow((*file).private_data) };
257257
let mut cmd = IoctlCommand::new(cmd as _, arg as _);
258258
let ret = T::ioctl(f, unsafe { &FileRef::from_ptr(file) }, &mut cmd)?;
259259
Ok(ret as _)
@@ -267,11 +267,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
267267
) -> c_types::c_long {
268268
from_kernel_result! {
269269
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
270-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
270+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
271271
// `release` callback, which the C API guarantees that will be called only when all
272272
// references to `file` have been released, so we know it can't be called while this
273273
// function is running.
274-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
274+
let f = unsafe { T::Data::borrow((*file).private_data) };
275275
let mut cmd = IoctlCommand::new(cmd as _, arg as _);
276276
let ret = T::compat_ioctl(f, unsafe { &FileRef::from_ptr(file) }, &mut cmd)?;
277277
Ok(ret as _)
@@ -284,11 +284,11 @@ impl<A: FileOpenAdapter<T::OpenData>, T: FileOperations> FileOperationsVtable<A,
284284
) -> c_types::c_int {
285285
from_kernel_result! {
286286
// SAFETY: `private_data` was initialised by `open_callback` with a value returned by
287-
// `T::Wrapper::into_pointer`. `T::Wrapper::from_pointer` is only called by the
287+
// `T::Data::into_pointer`. `T::Data::from_pointer` is only called by the
288288
// `release` callback, which the C API guarantees that will be called only when all
289289
// references to `file` have been released, so we know it can't be called while this
290290
// function is running.
291-
let f = unsafe { T::Wrapper::borrow((*file).private_data) };
291+
let f = unsafe { T::Data::borrow((*file).private_data) };
292292

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

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

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

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

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

641642
/// Reads data from this file to the caller's buffer.
642643
///
643644
/// Corresponds to the `read` and `read_iter` function pointers in `struct file_operations`.
644645
fn read(
645-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
646+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
646647
_file: &File,
647-
_data: &mut impl IoBufferWriter,
648+
_writer: &mut impl IoBufferWriter,
648649
_offset: u64,
649650
) -> Result<usize> {
650651
Err(Error::EINVAL)
@@ -654,9 +655,9 @@ pub trait FileOperations {
654655
///
655656
/// Corresponds to the `write` and `write_iter` function pointers in `struct file_operations`.
656657
fn write(
657-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
658+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
658659
_file: &File,
659-
_data: &mut impl IoBufferReader,
660+
_reader: &mut impl IoBufferReader,
660661
_offset: u64,
661662
) -> Result<usize> {
662663
Err(Error::EINVAL)
@@ -666,7 +667,7 @@ pub trait FileOperations {
666667
///
667668
/// Corresponds to the `llseek` function pointer in `struct file_operations`.
668669
fn seek(
669-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
670+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
670671
_file: &File,
671672
_offset: SeekFrom,
672673
) -> Result<u64> {
@@ -677,7 +678,7 @@ pub trait FileOperations {
677678
///
678679
/// Corresponds to the `unlocked_ioctl` function pointer in `struct file_operations`.
679680
fn ioctl(
680-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
681+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
681682
_file: &File,
682683
_cmd: &mut IoctlCommand,
683684
) -> Result<i32> {
@@ -688,7 +689,7 @@ pub trait FileOperations {
688689
///
689690
/// Corresponds to the `compat_ioctl` function pointer in `struct file_operations`.
690691
fn compat_ioctl(
691-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
692+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
692693
_file: &File,
693694
_cmd: &mut IoctlCommand,
694695
) -> Result<i32> {
@@ -699,7 +700,7 @@ pub trait FileOperations {
699700
///
700701
/// Corresponds to the `fsync` function pointer in `struct file_operations`.
701702
fn fsync(
702-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
703+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
703704
_file: &File,
704705
_start: u64,
705706
_end: u64,
@@ -711,9 +712,8 @@ pub trait FileOperations {
711712
/// Maps areas of the caller's virtual memory with device/file memory.
712713
///
713714
/// Corresponds to the `mmap` function pointer in `struct file_operations`.
714-
/// TODO: Wrap `vm_area_struct` so that we don't have to expose it.
715715
fn mmap(
716-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
716+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
717717
_file: &File,
718718
_vma: &mut mm::virt::Area,
719719
) -> Result {
@@ -725,7 +725,7 @@ pub trait FileOperations {
725725
///
726726
/// Corresponds to the `poll` function pointer in `struct file_operations`.
727727
fn poll(
728-
_this: <Self::Wrapper as PointerWrapper>::Borrowed<'_>,
728+
_data: <Self::Data as PointerWrapper>::Borrowed<'_>,
729729
_file: &File,
730730
_table: &PollTable,
731731
) -> Result<u32> {

samples/rust/rust_miscdev.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ impl SharedState {
5353

5454
struct Token;
5555
impl FileOperations for Token {
56-
type Wrapper = Ref<SharedState>;
56+
type Data = Ref<SharedState>;
5757
type OpenData = Ref<SharedState>;
5858

5959
kernel::declare_file_operations!(read, write);
6060

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

samples/rust/rust_semaphore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl FileState {
6363
}
6464

6565
impl FileOperations for FileState {
66-
type Wrapper = Box<Self>;
66+
type Data = Box<Self>;
6767
type OpenData = Ref<Semaphore>;
6868

6969
declare_file_operations!(read, write, ioctl);

0 commit comments

Comments
 (0)