From: Ariel Miculas <amiculas@cisco.com>
To: rust-for-linux@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
tycho@tycho.pizza, brauner@kernel.org, viro@zeniv.linux.org.uk,
ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com,
Ariel Miculas <amiculas@cisco.com>
Subject: [RFC PATCH v2 06/10] rust: file: pass the filesystem context to the open function
Date: Wed, 26 Jul 2023 19:45:30 +0300 [thread overview]
Message-ID: <20230726164535.230515-7-amiculas@cisco.com> (raw)
In-Reply-To: <20230726164535.230515-1-amiculas@cisco.com>
This allows us to create a Vfsmount structure and pass it to the read
callback.
Signed-off-by: Ariel Miculas <amiculas@cisco.com>
---
rust/kernel/file.rs | 17 +++++++++++++++--
samples/rust/puzzlefs.rs | 40 +++++++++++++++++++++++++++++++++++-----
samples/rust/rust_fs.rs | 3 ++-
3 files changed, 52 insertions(+), 8 deletions(-)
diff --git a/rust/kernel/file.rs b/rust/kernel/file.rs
index a3002c416dbb..af1eb1ee9267 100644
--- a/rust/kernel/file.rs
+++ b/rust/kernel/file.rs
@@ -457,9 +457,15 @@ impl<A: OpenAdapter<T::OpenData>, T: Operations> OperationsVtable<A, T> {
// `fileref` never outlives this function, so it is guaranteed to be
// valid.
let fileref = unsafe { File::from_ptr(file) };
+
+ // SAFETY: into_foreign was called in fs::NewSuperBlock<..., NeedsInit>::init and
+ // it is valid until from_foreign will be called in fs::Tables::free_callback
+ let fs_info =
+ unsafe { <T::Filesystem as fs::Type>::Data::borrow((*(*inode).i_sb).s_fs_info) };
+
// SAFETY: `arg` was previously returned by `A::convert` and must
// be a valid non-null pointer.
- let ptr = T::open(unsafe { &*arg }, fileref)?.into_foreign();
+ let ptr = T::open(fs_info, unsafe { &*arg }, fileref)?.into_foreign();
// SAFETY: The C contract guarantees that `private_data` is available
// for implementers of the file operations (no other C code accesses
// it), so we know that there are no concurrent threads/CPUs accessing
@@ -930,10 +936,17 @@ pub trait Operations {
/// The type of the context data passed to [`Operations::open`].
type OpenData: Sync = ();
+ /// Data associated with each file system instance.
+ type Filesystem: fs::Type;
+
/// 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::Data>;
+ fn open(
+ fs_info: <<Self::Filesystem as fs::Type>::Data as ForeignOwnable>::Borrowed<'_>,
+ context: &Self::OpenData,
+ file: &File,
+ ) -> Result<Self::Data>;
/// Cleans up after the last reference to the file goes away.
///
diff --git a/samples/rust/puzzlefs.rs b/samples/rust/puzzlefs.rs
index 9afd82745b64..8a64e0bd437d 100644
--- a/samples/rust/puzzlefs.rs
+++ b/samples/rust/puzzlefs.rs
@@ -3,8 +3,14 @@
//! Rust file system sample.
use kernel::module_fs;
+use kernel::mount::Vfsmount;
use kernel::prelude::*;
-use kernel::{c_str, file, fs, io_buffer::IoBufferWriter};
+use kernel::{
+ c_str, file, fmt, fs,
+ io_buffer::IoBufferWriter,
+ str::CString,
+ sync::{Arc, ArcBorrow},
+};
mod puzzle;
// Required by the autogenerated '_capnp.rs' files
@@ -19,6 +25,12 @@
struct PuzzleFsModule;
+#[derive(Debug)]
+struct PuzzlefsInfo {
+ base_path: CString,
+ vfs_mount: Arc<Vfsmount>,
+}
+
#[vtable]
impl fs::Context<Self> for PuzzleFsModule {
type Data = ();
@@ -46,14 +58,23 @@ fn try_new() -> Result {
impl fs::Type for PuzzleFsModule {
type Context = Self;
type INodeData = &'static [u8];
+ type Data = Box<PuzzlefsInfo>;
const SUPER_TYPE: fs::Super = fs::Super::Independent;
const NAME: &'static CStr = c_str!("puzzlefs");
const FLAGS: i32 = fs::flags::USERNS_MOUNT;
const DCACHE_BASED: bool = true;
fn fill_super(_data: (), sb: fs::NewSuperBlock<'_, Self>) -> Result<&fs::SuperBlock<Self>> {
+ let base_path = CString::try_from_fmt(fmt!("hello world"))?;
+ pr_info!("base_path {:?}\n", base_path);
+ let vfs_mount = Vfsmount::new_private_mount(c_str!("/home/puzzlefs_oci"))?;
+ pr_info!("vfs_mount {:?}\n", vfs_mount);
+
let sb = sb.init(
- (),
+ Box::try_new(PuzzlefsInfo {
+ base_path,
+ vfs_mount: Arc::try_new(vfs_mount)?,
+ })?,
&fs::SuperParams {
magic: 0x72757374,
..fs::SuperParams::DEFAULT
@@ -88,14 +109,23 @@ fn fill_super(_data: (), sb: fs::NewSuperBlock<'_, Self>) -> Result<&fs::SuperBl
#[vtable]
impl file::Operations for FsFile {
+ // must be the same as INodeData
type OpenData = &'static [u8];
+ type Filesystem = PuzzleFsModule;
+ // this is an Arc because Data must be ForeignOwnable and the only implementors of it are Box,
+ // Arc and (); we cannot pass a reference to read, so we share Vfsmount using and Arc
+ type Data = Arc<Vfsmount>;
- fn open(_context: &Self::OpenData, _file: &file::File) -> Result<Self::Data> {
- Ok(())
+ fn open(
+ fs_info: &PuzzlefsInfo,
+ _context: &Self::OpenData,
+ _file: &file::File,
+ ) -> Result<Self::Data> {
+ Ok(fs_info.vfs_mount.clone())
}
fn read(
- _data: (),
+ data: ArcBorrow<'_, Vfsmount>,
file: &file::File,
writer: &mut impl IoBufferWriter,
offset: u64,
diff --git a/samples/rust/rust_fs.rs b/samples/rust/rust_fs.rs
index 7527681ee024..c58ed1560e06 100644
--- a/samples/rust/rust_fs.rs
+++ b/samples/rust/rust_fs.rs
@@ -85,8 +85,9 @@ fn fill_super(_data: (), sb: fs::NewSuperBlock<'_, Self>) -> Result<&fs::SuperBl
#[vtable]
impl file::Operations for FsFile {
type OpenData = &'static [u8];
+ type Filesystem = RustFs;
- fn open(_context: &Self::OpenData, _file: &file::File) -> Result<Self::Data> {
+ fn open(_fs_info: (), _context: &Self::OpenData, _file: &file::File) -> Result<Self::Data> {
Ok(())
}
--
2.41.0
next prev parent reply other threads:[~2023-07-26 16:48 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-26 16:45 [RFC PATCH v2 00/10] Rust PuzleFS filesystem driver Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 01/10] samples: puzzlefs: add initial puzzlefs sample, copied from rust_fs.rs Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 02/10] kernel: configs: enable rust samples in rust.config Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 03/10] rust: kernel: add an abstraction over vfsmount to allow cloning a new private mount Ariel Miculas
2023-07-26 22:34 ` Trevor Gross
2023-07-27 13:10 ` Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 04/10] rust: file: Add a new RegularFile newtype useful for reading files Ariel Miculas
2023-07-26 23:52 ` Trevor Gross
2023-07-27 13:18 ` Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 05/10] samples: puzzlefs: add basic deserializing support for the puzzlefs metadata Ariel Miculas
2023-07-26 16:45 ` Ariel Miculas [this message]
2023-07-27 13:32 ` [RFC PATCH v2 06/10] rust: file: pass the filesystem context to the open function Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 07/10] samples: puzzlefs: populate the directory entries with the inodes from the puzzlefs metadata file Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 08/10] rust: puzzlefs: read the puzzlefs image manifest instead of an individual metadata layer Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 09/10] rust: puzzlefs: add support for reading files Ariel Miculas
2023-07-26 16:45 ` [RFC PATCH v2 10/10] rust: puzzlefs: add oci_root_dir and image_manifest filesystem parameters Ariel Miculas
2023-07-26 21:08 ` Trevor Gross
2023-07-26 23:47 ` Wedson Almeida Filho
2023-07-27 0:02 ` Trevor Gross
2023-07-27 8:06 ` Wedson Almeida Filho
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230726164535.230515-7-amiculas@cisco.com \
--to=amiculas@cisco.com \
--cc=alex.gaynor@gmail.com \
--cc=brauner@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tycho@tycho.pizza \
--cc=viro@zeniv.linux.org.uk \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).