Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1327435rwb; Wed, 26 Jul 2023 10:44:42 -0700 (PDT) X-Google-Smtp-Source: APBJJlG1xIg8VmK1/ppPg+C+eAhgiAJM1ggaV97cjJcyZ0/etccB1WHEXsW4JYFlVGf3PMyiy6bC X-Received: by 2002:a17:906:64cb:b0:99b:4bab:2841 with SMTP id p11-20020a17090664cb00b0099b4bab2841mr272418ejn.26.1690393482161; Wed, 26 Jul 2023 10:44:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690393482; cv=none; d=google.com; s=arc-20160816; b=KrT+Ggn7l/2dqFQ4e309XQWJ8g75g4blb/6EWf2vu/ChrtBAheGC2nblHcLPHpTFAC AEA81JPGvP+RuQqa7LJGaa1OLUcknwzJbz85+HxOc3hnlbJHqr85o1zPwLjPjjMIRqlX XdN8toZgoKAlqooq66lJ8HOUJ3GLesjKjCjvzDNWzRe+sz+AB9QCr140bWAmo/QQpqd/ umZm8Kis2b0HjetnhiNPrdyb4nFKVP9rhuMk93FsEMDyLZ/CadXgRSFxO3ZeiZ41AlBF oXYDl34c+g2Cm+pbJcpPY34WJHc+SHkPQ1O1bjbI7N/quzwhZ3UJ8h3x2usPRrt/gSKQ xzrQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=txC1rQAQ0DWansK8Ub3MLfUjC9ipf8Teyvy/PoJW+Ew=; fh=Jpn3R4JmmVOUsuPzBxq53h+Q0nrbpuajN1OLJbY8QB4=; b=zEaMpsuJziG4zuaRgVhLhhW0asq5J8LZRrx3dtsn22dndg3qw8iAP1sUl3G4nUVdKp sRNIbXrMEZFM7pu5X5wQw4Cp6fWN+jm9Lh00ocJs5TzrjGWz2/U4ok45NgW1zUjeUDDB F6Dy90CWgGM0n3ve0n/uufoz6kEfQD0M+39qHSGGZAnkH+OY7viTGh7fFF2Zpn1SWy7U QatgDyhyOe78ln2BCXTOZ6RmZpkJtN8tShxUCxw3oG1SWvZ7ob/I6J8YGn7U4lgVXjEm B4jksvpC9i+xuNsOUwTdNBgdb+pPMFHa39bVniuULVMG5z86tRUaWi+MhNowYekfxf4Z eNCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=WUikDzko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=cisco.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u19-20020a170906c41300b009939cd92a18si9986163ejz.73.2023.07.26.10.44.15; Wed, 26 Jul 2023 10:44:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@cisco.com header.s=iport header.b=WUikDzko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=cisco.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231688AbjGZRLl (ORCPT + 99 others); Wed, 26 Jul 2023 13:11:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230113AbjGZRLk (ORCPT ); Wed, 26 Jul 2023 13:11:40 -0400 Received: from aer-iport-5.cisco.com (aer-iport-5.cisco.com [173.38.203.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1DF01BD6 for ; Wed, 26 Jul 2023 10:11:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=cisco.com; i=@cisco.com; l=4965; q=dns/txt; s=iport; t=1690391499; x=1691601099; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=i/lTVDEDYHsEdChSzawXjVr0aEd2CSBfkYp/e9QDjEg=; b=WUikDzkon8wU1d6unzSqMSjmiheOY3pn+0SB/Lt0DxYZC7gJGfDtX0vZ ATJy6CiBE5riZp/LJVjnt2MsO2kJmR61wfPFuuOtHm8OBDbMKNUmhEXIT lnGvV8Uw0Wsy239oXl7diHyjtz2NKFEpARWl9jMmEHOMAkZEp8lC9Qdir E=; X-IronPort-AV: E=Sophos;i="6.01,232,1684800000"; d="scan'208";a="5874456" Received: from aer-iport-nat.cisco.com (HELO aer-core-7.cisco.com) ([173.38.203.22]) by aer-iport-5.cisco.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Jul 2023 16:46:04 +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 36QGjqTr022602 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 26 Jul 2023 16:46:04 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 01/10] samples: puzzlefs: add initial puzzlefs sample, copied from rust_fs.rs Date: Wed, 26 Jul 2023 19:45:25 +0300 Message-ID: <20230726164535.230515-2-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: 8bit 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Signed-off-by: Ariel Miculas --- 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 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> { + 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 { + Ok(()) + } + + fn read( + _data: (), + file: &file::File, + writer: &mut impl IoBufferWriter, + offset: u64, + ) -> Result { + file::read_from_slice( + file.inode::().ok_or(EINVAL)?.fs_data(), + writer, + offset, + ) + } +} -- 2.41.0