Skip to content

Commit 9a15cd9

Browse files
authored
Merge pull request #574 from wedsonaf/file-in-open
rust: add `&File` argument to `open` callback.
2 parents fc4aa49 + 29a96d1 commit 9a15cd9

File tree

5 files changed

+17
-24
lines changed

5 files changed

+17
-24
lines changed

drivers/android/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ impl IoctlHandler for Process {
802802
}
803803

804804
impl FileOpener<Ref<Context>> for Process {
805-
fn open(ctx: &Ref<Context>) -> Result<Self::Wrapper> {
805+
fn open(ctx: &Ref<Context>, _file: &File) -> Result<Self::Wrapper> {
806806
Self::new(ctx.clone())
807807
}
808808
}

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66
#![feature(allocator_api, global_asm)]
77

88
use kernel::{
9-
file::File,
10-
file_operations::{FileOpener, FileOperations},
11-
io_buffer::IoBufferWriter,
12-
miscdev,
13-
of::ConstOfMatchTable,
14-
platdev::PlatformDriver,
15-
prelude::*,
16-
str::CStr,
17-
ThisModule, {c_str, platdev},
9+
c_str, file::File, file_operations::FileOperations, io_buffer::IoBufferWriter, miscdev,
10+
of::ConstOfMatchTable, platdev, platdev::PlatformDriver, prelude::*,
1811
};
1912

2013
module! {
@@ -25,14 +18,9 @@ module! {
2518
license: b"GPL v2",
2619
}
2720

21+
#[derive(Default)]
2822
struct RngDevice;
2923

30-
impl FileOpener<()> for RngDevice {
31-
fn open(_state: &()) -> Result<Self::Wrapper> {
32-
Ok(Box::try_new(RngDevice)?)
33-
}
34-
}
35-
3624
impl FileOperations for RngDevice {
3725
kernel::declare_file_operations!(read);
3826

rust/kernel/file_operations.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,17 @@ unsafe extern "C" fn open_callback<A: FileOpenAdapter, T: FileOpener<A::Arg>>(
9595
// should point to data in the inode or file that lives longer
9696
// than the following use of `T::open`.
9797
let arg = unsafe { A::convert(inode, file) };
98+
// SAFETY: The C contract guarantees that `file` is valid. Additionally,
99+
// `fileref` never outlives this function, so it is guaranteed to be
100+
// valid.
101+
let fileref = unsafe { FileRef::from_ptr(file) };
98102
// SAFETY: `arg` was previously returned by `A::convert` and must
99103
// be a valid non-null pointer.
100-
let ptr = T::open(unsafe { &*arg })?.into_pointer();
101-
// SAFETY: `ptr` was previously returned by `T::open`. The returned
102-
// value should be a boxed value and should live the length of the
103-
// given file.
104+
let ptr = T::open(unsafe { &*arg }, &fileref)?.into_pointer();
105+
// SAFETY: The C contract guarantees that `private_data` is available
106+
// for implementers of the file operations (no other C code accesses
107+
// it), so we know that there are no concurrent threads/CPUs accessing
108+
// it (it's not visible to any other Rust code).
104109
unsafe { (*file).private_data = ptr as *mut c_types::c_void };
105110
Ok(0)
106111
}
@@ -591,11 +596,11 @@ pub trait FileOpener<T: ?Sized>: FileOperations {
591596
/// Creates a new instance of this file.
592597
///
593598
/// Corresponds to the `open` function pointer in `struct file_operations`.
594-
fn open(context: &T) -> Result<Self::Wrapper>;
599+
fn open(context: &T, file: &File) -> Result<Self::Wrapper>;
595600
}
596601

597602
impl<T: FileOperations<Wrapper = Box<T>> + Default> FileOpener<()> for T {
598-
fn open(_: &()) -> Result<Self::Wrapper> {
603+
fn open(_: &(), _file: &File) -> Result<Self::Wrapper> {
599604
Ok(Box::try_new(T::default())?)
600605
}
601606
}

samples/rust/rust_miscdev.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl SharedState {
5757
struct Token;
5858

5959
impl FileOpener<Ref<SharedState>> for Token {
60-
fn open(shared: &Ref<SharedState>) -> Result<Self::Wrapper> {
60+
fn open(shared: &Ref<SharedState>, _file: &File) -> Result<Self::Wrapper> {
6161
Ok(shared.clone())
6262
}
6363
}

samples/rust/rust_semaphore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl FileState {
6666
}
6767

6868
impl FileOpener<Ref<Semaphore>> for FileState {
69-
fn open(shared: &Ref<Semaphore>) -> Result<Box<Self>> {
69+
fn open(shared: &Ref<Semaphore>, _file: &File) -> Result<Box<Self>> {
7070
Ok(Box::try_new(Self {
7171
read_count: AtomicU64::new(0),
7272
shared: shared.clone(),

0 commit comments

Comments
 (0)