From nobody Tue Sep 9 23:10:16 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2633C001DC for ; Wed, 26 Jul 2023 16:58:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232304AbjGZQ6G (ORCPT ); Wed, 26 Jul 2023 12:58:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232163AbjGZQ5y (ORCPT ); Wed, 26 Jul 2023 12:57:54 -0400 Received: from aer-iport-4.cisco.com (aer-iport-4.cisco.com [173.38.203.54]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26BA5188 for ; Wed, 26 Jul 2023 09:57:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=5818; q=dns/txt; s=iport; t=1690390673; x=1691600273; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UiFHWKZZEisCHqVroCxAGB1BlMOgs6ZoyHjOZIT/iNs=; b=MVs5S1MyB6QrIjLGRAFNIJpmdV5cGMGrrUG+2aoBLZOr4WDwT35RhxqV jDJmztMMH+IiB9zbctJrYRtjM76mS1xQ4bB+QwmQhNr+r9DMCwluV4oKe YZIHahtaVtxr54FGubG0XdV73cHieDepeBkVfIifetlUivnEoNyuhdTr7 g=; X-IronPort-AV: E=Sophos;i="6.01,232,1684800000"; d="scan'208";a="8416073" Received: from aer-iport-nat.cisco.com (HELO aer-core-7.cisco.com) ([173.38.203.22]) by aer-iport-4.cisco.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2023 16:46:21 +0000 Received: from archlinux-cisco.cisco.com (dhcp-10-61-98-211.cisco.com [10.61.98.211]) (authenticated bits=0) by aer-core-7.cisco.com (8.15.2/8.15.2) with ESMTPSA id 36QGjqTw022602 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 26 Jul 2023 16:46:21 GMT From: Ariel Miculas 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 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 Message-ID: <20230726164535.230515-7-amiculas@cisco.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230726164535.230515-1-amiculas@cisco.com> References: <20230726164535.230515-1-amiculas@cisco.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Authenticated-User: amiculas X-Outbound-SMTP-Client: 10.61.98.211, dhcp-10-61-98-211.cisco.com X-Outbound-Node: aer-core-7.cisco.com Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This allows us to create a Vfsmount structure and pass it to the read callback. Signed-off-by: Ariel Miculas --- 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, T: Operations> Opera= tionsVtable { // `fileref` never outlives this function, so it is guaranteed= to be // valid. let fileref =3D unsafe { File::from_ptr(file) }; + + // SAFETY: into_foreign was called in fs::NewSuperBlock<..., N= eedsInit>::init and + // it is valid until from_foreign will be called in fs::Tables= ::free_callback + let fs_info =3D + unsafe { ::Data::borrow((*(*ino= de).i_sb).s_fs_info) }; + // SAFETY: `arg` was previously returned by `A::convert` and m= ust // be a valid non-null pointer. - let ptr =3D T::open(unsafe { &*arg }, fileref)?.into_foreign(); + let ptr =3D T::open(fs_info, unsafe { &*arg }, fileref)?.into_= foreign(); // SAFETY: The C contract guarantees that `private_data` is av= ailable // for implementers of the file operations (no other C code ac= cesses // it), so we know that there are no concurrent threads/CPUs a= ccessing @@ -930,10 +936,17 @@ pub trait Operations { /// The type of the context data passed to [`Operations::open`]. type OpenData: Sync =3D (); =20 + /// 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_operati= ons`. - fn open(context: &Self::OpenData, file: &File) -> Result; + fn open( + fs_info: <::Data as ForeignOwnable>:= :Borrowed<'_>, + context: &Self::OpenData, + file: &File, + ) -> Result; =20 /// 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. =20 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}, +}; =20 mod puzzle; // Required by the autogenerated '_capnp.rs' files @@ -19,6 +25,12 @@ =20 struct PuzzleFsModule; =20 +#[derive(Debug)] +struct PuzzlefsInfo { + base_path: CString, + vfs_mount: Arc, +} + #[vtable] impl fs::Context for PuzzleFsModule { type Data =3D (); @@ -46,14 +58,23 @@ fn try_new() -> Result { impl fs::Type for PuzzleFsModule { type Context =3D Self; type INodeData =3D &'static [u8]; + type Data =3D Box; const SUPER_TYPE: fs::Super =3D fs::Super::Independent; const NAME: &'static CStr =3D c_str!("puzzlefs"); const FLAGS: i32 =3D fs::flags::USERNS_MOUNT; const DCACHE_BASED: bool =3D true; =20 fn fill_super(_data: (), sb: fs::NewSuperBlock<'_, Self>) -> Result<&f= s::SuperBlock> { + let base_path =3D CString::try_from_fmt(fmt!("hello world"))?; + pr_info!("base_path {:?}\n", base_path); + let vfs_mount =3D Vfsmount::new_private_mount(c_str!("/home/puzzle= fs_oci"))?; + pr_info!("vfs_mount {:?}\n", vfs_mount); + let sb =3D 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<'_, Sel= f>) -> Result<&fs::SuperBl =20 #[vtable] impl file::Operations for FsFile { + // must be the same as INodeData type OpenData =3D &'static [u8]; + type Filesystem =3D PuzzleFsModule; + // this is an Arc because Data must be ForeignOwnable and the only imp= lementors of it are Box, + // Arc and (); we cannot pass a reference to read, so we share Vfsmoun= t using and Arc + type Data =3D Arc; =20 - fn open(_context: &Self::OpenData, _file: &file::File) -> Result { - Ok(()) + fn open( + fs_info: &PuzzlefsInfo, + _context: &Self::OpenData, + _file: &file::File, + ) -> Result { + Ok(fs_info.vfs_mount.clone()) } =20 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 =3D &'static [u8]; + type Filesystem =3D RustFs; =20 - fn open(_context: &Self::OpenData, _file: &file::File) -> Result { + fn open(_fs_info: (), _context: &Self::OpenData, _file: &file::File) -= > Result { Ok(()) } =20 --=20 2.41.0