From f790ecb5f872a12b42dc785ff4136c342dc59464 Mon Sep 17 00:00:00 2001 From: Wedson Almeida Filho Date: Fri, 30 Apr 2021 16:05:14 +0100 Subject: [PATCH] Move `File` to its own module. This is a pure refactor in preparation for adding more features to `File` so that file descriptors can be transferred between processes by Binder. Signed-off-by: Wedson Almeida Filho --- drivers/android/process.rs | 3 ++- drivers/android/thread.rs | 3 ++- rust/kernel/file.rs | 41 ++++++++++++++++++++++++++++++++++ rust/kernel/file_operations.rs | 34 +--------------------------- rust/kernel/lib.rs | 1 + samples/rust/rust_miscdev.rs | 3 ++- samples/rust/rust_random.rs | 3 ++- samples/rust/rust_semaphore.rs | 3 ++- 8 files changed, 53 insertions(+), 38 deletions(-) create mode 100644 rust/kernel/file.rs diff --git a/drivers/android/process.rs b/drivers/android/process.rs index fe3f99bb5733c1..59b4eaa6f920f2 100644 --- a/drivers/android/process.rs +++ b/drivers/android/process.rs @@ -8,7 +8,8 @@ use core::{ }; use kernel::{ bindings, c_types, - file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable}, + file::File, + file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler, PollTable}, io_buffer::{IoBufferReader, IoBufferWriter}, linked_list::List, pages::Pages, diff --git a/drivers/android/thread.rs b/drivers/android/thread.rs index ef9e05304d6c91..f0c1a6cabe2449 100644 --- a/drivers/android/thread.rs +++ b/drivers/android/thread.rs @@ -4,7 +4,8 @@ use alloc::sync::Arc; use core::{alloc::AllocError, mem::size_of, pin::Pin}; use kernel::{ bindings, - file_operations::{File, PollTable}, + file::File, + file_operations::PollTable, io_buffer::{IoBufferReader, IoBufferWriter}, linked_list::{GetLinks, Links, List}, prelude::*, diff --git a/rust/kernel/file.rs b/rust/kernel/file.rs new file mode 100644 index 00000000000000..15b101503d4183 --- /dev/null +++ b/rust/kernel/file.rs @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Files and file descriptors. +//! +//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and +//! [`include/linux/file.h`](../../../../include/linux/file.h) + +use crate::bindings; + +/// Wraps the kernel's `struct file`. +/// +/// # Invariants +/// +/// The pointer [`File::ptr`] is non-null and valid. +pub struct File { + pub(crate) ptr: *const bindings::file, +} + +impl File { + /// Constructs a new [`struct file`] wrapper. + /// + /// # Safety + /// + /// The pointer `ptr` must be non-null and valid for the lifetime of the object. + pub(crate) unsafe fn from_ptr(ptr: *const bindings::file) -> File { + // INVARIANTS: the safety contract ensures the type invariant will hold. + File { ptr } + } + + /// Returns the current seek/cursor/pointer position (`struct file::f_pos`). + pub fn pos(&self) -> u64 { + // SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. + unsafe { (*self.ptr).f_pos as u64 } + } + + /// Returns whether the file is in blocking mode. + pub fn is_blocking(&self) -> bool { + // SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. + unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 } + } +} diff --git a/rust/kernel/file_operations.rs b/rust/kernel/file_operations.rs index 4c1860a4fa72fa..ad379ac981ca44 100644 --- a/rust/kernel/file_operations.rs +++ b/rust/kernel/file_operations.rs @@ -13,45 +13,13 @@ use alloc::sync::Arc; use crate::{ bindings, c_types, error::{Error, KernelResult}, + file::File, io_buffer::{IoBufferReader, IoBufferWriter}, iov_iter::IovIter, sync::{CondVar, Ref, RefCounted}, user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter}, }; -/// Wraps the kernel's `struct file`. -/// -/// # Invariants -/// -/// The pointer [`File::ptr`] is non-null and valid. -pub struct File { - ptr: *const bindings::file, -} - -impl File { - /// Constructs a new [`struct file`] wrapper. - /// - /// # Safety - /// - /// The pointer `ptr` must be non-null and valid for the lifetime of the object. - unsafe fn from_ptr(ptr: *const bindings::file) -> File { - // INVARIANTS: the safety contract ensures the type invariant will hold. - File { ptr } - } - - /// Returns the current seek/cursor/pointer position (`struct file::f_pos`). - pub fn pos(&self) -> u64 { - // SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. - unsafe { (*self.ptr).f_pos as u64 } - } - - /// Returns whether the file is in blocking mode. - pub fn is_blocking(&self) -> bool { - // SAFETY: `File::ptr` is guaranteed to be valid by the type invariants. - unsafe { (*self.ptr).f_flags & bindings::O_NONBLOCK == 0 } - } -} - /// Wraps the kernel's `struct poll_table_struct`. /// /// # Invariants diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 3799a03fcf875a..d34cb84eff50a0 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -40,6 +40,7 @@ pub mod buffer; pub mod c_types; pub mod chrdev; mod error; +pub mod file; pub mod file_operations; pub mod miscdev; pub mod pages; diff --git a/samples/rust/rust_miscdev.rs b/samples/rust/rust_miscdev.rs index 944da811f4c164..77104228924644 100644 --- a/samples/rust/rust_miscdev.rs +++ b/samples/rust/rust_miscdev.rs @@ -10,7 +10,8 @@ use core::pin::Pin; use kernel::prelude::*; use kernel::{ cstr, - file_operations::{File, FileOpener, FileOperations}, + file::File, + file_operations::{FileOpener, FileOperations}, io_buffer::{IoBufferReader, IoBufferWriter}, miscdev, sync::{CondVar, Mutex}, diff --git a/samples/rust/rust_random.rs b/samples/rust/rust_random.rs index 6eac30e7e44417..ed2d159180f52d 100644 --- a/samples/rust/rust_random.rs +++ b/samples/rust/rust_random.rs @@ -9,7 +9,8 @@ #![feature(allocator_api, global_asm)] use kernel::{ - file_operations::{File, FileOperations}, + file::File, + file_operations::FileOperations, io_buffer::{IoBufferReader, IoBufferWriter}, prelude::*, }; diff --git a/samples/rust/rust_semaphore.rs b/samples/rust/rust_semaphore.rs index 83b163f647f20c..931785ac35e112 100644 --- a/samples/rust/rust_semaphore.rs +++ b/samples/rust/rust_semaphore.rs @@ -23,7 +23,8 @@ use core::{ }; use kernel::{ condvar_init, cstr, declare_file_operations, - file_operations::{File, FileOpener, FileOperations, IoctlCommand, IoctlHandler}, + file::File, + file_operations::{FileOpener, FileOperations, IoctlCommand, IoctlHandler}, io_buffer::{IoBufferReader, IoBufferWriter}, miscdev::Registration, mutex_init,