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 01/10] samples: puzzlefs: add initial puzzlefs sample, copied from rust_fs.rs
Date: Wed, 26 Jul 2023 19:45:25 +0300 [thread overview]
Message-ID: <20230726164535.230515-2-amiculas@cisco.com> (raw)
In-Reply-To: <20230726164535.230515-1-amiculas@cisco.com>
Signed-off-by: Ariel Miculas <amiculas@cisco.com>
---
samples/rust/Kconfig | 10 ++++
samples/rust/Makefile | 1 +
samples/rust/puzzlefs.rs | 105 +++++++++++++++++++++++++++++++++++++++
3 files changed, 116 insertions(+)
create mode 100644 samples/rust/puzzlefs.rs
diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig
index 2bd736f99189..05ca21fbba06 100644
--- a/samples/rust/Kconfig
+++ b/samples/rust/Kconfig
@@ -40,6 +40,16 @@ config SAMPLE_RUST_FS
If unsure, say N.
+config SAMPLE_PUZZLEFS
+ tristate "Puzzlefs file system"
+ help
+ This option builds the Rust puzzlefs file system sample.
+
+ To compile this as a module, choose M here:
+ the module will be called puzzlefs.
+
+ If unsure, say N.
+
config SAMPLE_RUST_HOSTPROGS
bool "Host programs"
help
diff --git a/samples/rust/Makefile b/samples/rust/Makefile
index e5941037e673..364a38dbf90b 100644
--- a/samples/rust/Makefile
+++ b/samples/rust/Makefile
@@ -3,5 +3,6 @@
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
obj-$(CONFIG_SAMPLE_RUST_FS) += rust_fs.o
+obj-$(CONFIG_SAMPLE_PUZZLEFS) += puzzlefs.o
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
diff --git a/samples/rust/puzzlefs.rs b/samples/rust/puzzlefs.rs
new file mode 100644
index 000000000000..0cf42762e81a
--- /dev/null
+++ b/samples/rust/puzzlefs.rs
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Rust file system sample.
+
+use kernel::module_fs;
+use kernel::prelude::*;
+use kernel::{c_str, file, fs, io_buffer::IoBufferWriter};
+
+module_fs! {
+ type: PuzzleFsModule,
+ name: "puzzlefs",
+ author: "Ariel Miculas",
+ license: "GPL",
+}
+
+struct PuzzleFsModule;
+
+#[vtable]
+impl fs::Context<Self> for PuzzleFsModule {
+ type Data = ();
+
+ kernel::define_fs_params! {(),
+ {flag, "flag", |_, v| { pr_info!("flag passed-in: {v}\n"); Ok(()) } },
+ {flag_no, "flagno", |_, v| { pr_info!("flagno passed-in: {v}\n"); Ok(()) } },
+ {bool, "bool", |_, v| { pr_info!("bool passed-in: {v}\n"); Ok(()) } },
+ {u32, "u32", |_, v| { pr_info!("u32 passed-in: {v}\n"); Ok(()) } },
+ {u32oct, "u32oct", |_, v| { pr_info!("u32oct passed-in: {v}\n"); Ok(()) } },
+ {u32hex, "u32hex", |_, v| { pr_info!("u32hex passed-in: {v}\n"); Ok(()) } },
+ {s32, "s32", |_, v| { pr_info!("s32 passed-in: {v}\n"); Ok(()) } },
+ {u64, "u64", |_, v| { pr_info!("u64 passed-in: {v}\n"); Ok(()) } },
+ {string, "string", |_, v| { pr_info!("string passed-in: {v}\n"); Ok(()) } },
+ {enum, "enum", [("first", 10), ("second", 20)], |_, v| {
+ pr_info!("enum passed-in: {v}\n"); Ok(()) }
+ },
+ }
+
+ fn try_new() -> Result {
+ Ok(())
+ }
+}
+
+impl fs::Type for PuzzleFsModule {
+ type Context = Self;
+ type INodeData = &'static [u8];
+ 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 sb = sb.init(
+ (),
+ &fs::SuperParams {
+ magic: 0x72757374,
+ ..fs::SuperParams::DEFAULT
+ },
+ )?;
+ let root = sb.try_new_populated_root_dentry(
+ &[],
+ kernel::fs_entries![
+ file("test1", 0o600, "abc\n".as_bytes(), FsFile),
+ file("test2", 0o600, "def\n".as_bytes(), FsFile),
+ char("test3", 0o600, [].as_slice(), (10, 125)),
+ sock("test4", 0o755, [].as_slice()),
+ fifo("test5", 0o755, [].as_slice()),
+ block("test6", 0o755, [].as_slice(), (1, 1)),
+ dir(
+ "dir1",
+ 0o755,
+ [].as_slice(),
+ [
+ file("test1", 0o600, "abc\n".as_bytes(), FsFile),
+ file("test2", 0o600, "def\n".as_bytes(), FsFile),
+ ]
+ ),
+ ],
+ )?;
+ let sb = sb.init_root(root)?;
+ Ok(sb)
+ }
+}
+
+struct FsFile;
+
+#[vtable]
+impl file::Operations for FsFile {
+ type OpenData = &'static [u8];
+
+ fn open(_context: &Self::OpenData, _file: &file::File) -> Result<Self::Data> {
+ Ok(())
+ }
+
+ fn read(
+ _data: (),
+ file: &file::File,
+ writer: &mut impl IoBufferWriter,
+ offset: u64,
+ ) -> Result<usize> {
+ file::read_from_slice(
+ file.inode::<PuzzleFsModule>().ok_or(EINVAL)?.fs_data(),
+ writer,
+ offset,
+ )
+ }
+}
--
2.41.0
next prev parent reply other threads:[~2023-07-26 16:47 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 ` Ariel Miculas [this message]
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 ` [RFC PATCH v2 06/10] rust: file: pass the filesystem context to the open function Ariel Miculas
2023-07-27 13:32 ` 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-2-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).