Skip to content

Commit b229d7c

Browse files
wedsonaffbq
authored andcommitted
rust: file: add Rust abstraction for struct file
This abstraction makes it possible to manipulate the open files for a process. The new `File` struct wraps the C `struct file`. When accessing it using the smart pointer `ARef<File>`, the pointer will own a reference count to the file. When accessing it as `&File`, then the reference does not own a refcount, but the borrow checker will ensure that the reference count does not hit zero while the `&File` is live. Since this is intended to manipulate the open files of a process, we introduce an `fget` constructor that corresponds to the C `fget` method. In future patches, it will become possible to create a new fd in a process and bind it to a `File`. Rust Binder will use these to send fds from one process to another. We also provide a method for accessing the file's flags. Rust Binder will use this to access the flags of the Binder fd to check whether the non-blocking flag is set, which affects what the Binder ioctl does. This introduces a struct for the EBADF error type, rather than just using the Error type directly. This has two advantages: * `File::fget` returns a `Result<ARef<File>, BadFdError>`, which the compiler will represent as a single pointer, with null being an error. This is possible because the compiler understands that `BadFdError` has only one possible value, and it also understands that the `ARef<File>` smart pointer is guaranteed non-null. * Additionally, we promise to users of the method that the method can only fail with EBADF, which means that they can rely on this promise without having to inspect its implementation. That said, there are also two disadvantages: * Defining additional error types involves boilerplate. * The question mark operator will only utilize the `From` trait once, which prevents you from using the question mark operator on `BadFdError` in methods that return some third error type that the kernel `Error` is convertible into. (However, it works fine in methods that return `Error`.) Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Daniel Xu <[email protected]> Signed-off-by: Daniel Xu <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [Boqun: drop the `ARef::into_raw` in favor of the standalone patch]
1 parent 409cf16 commit b229d7c

File tree

6 files changed

+400
-0
lines changed

6 files changed

+400
-0
lines changed

fs/file.c

+7
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,13 @@ EXPORT_SYMBOL(task_lookup_next_fdget_rcu);
11271127
*
11281128
* The fput_needed flag returned by fget_light should be passed to the
11291129
* corresponding fput_light.
1130+
*
1131+
* (As an exception to rule 2, you can call filp_close between fget_light and
1132+
* fput_light provided that you capture a real refcount with get_file before
1133+
* the call to filp_close, and ensure that this real refcount is fput *after*
1134+
* the fput_light call.)
1135+
*
1136+
* See also the documentation in rust/kernel/file.rs.
11301137
*/
11311138
static unsigned long __fget_light(unsigned int fd, fmode_t mask)
11321139
{

rust/bindings/bindings_helper.h

+2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include <linux/blkdev.h>
1313
#include <linux/errname.h>
1414
#include <linux/ethtool.h>
15+
#include <linux/file.h>
1516
#include <linux/firmware.h>
17+
#include <linux/fs.h>
1618
#include <linux/jiffies.h>
1719
#include <linux/mdio.h>
1820
#include <linux/phy.h>

rust/helpers.c

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/device.h>
2727
#include <linux/err.h>
2828
#include <linux/errname.h>
29+
#include <linux/fs.h>
2930
#include <linux/gfp.h>
3031
#include <linux/highmem.h>
3132
#include <linux/mutex.h>
@@ -207,6 +208,12 @@ void rust_helper_rb_link_node(struct rb_node *node, struct rb_node *parent,
207208
}
208209
EXPORT_SYMBOL_GPL(rust_helper_rb_link_node);
209210

211+
struct file *rust_helper_get_file(struct file *f)
212+
{
213+
return get_file(f);
214+
}
215+
EXPORT_SYMBOL_GPL(rust_helper_get_file);
216+
210217
/*
211218
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
212219
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/fs.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Kernel file systems.
4+
//!
5+
//! C headers: [`include/linux/fs.h`](srctree/include/linux/fs.h)
6+
7+
pub mod file;
8+
pub use self::file::{File, LocalFile};

0 commit comments

Comments
 (0)