From nobody Fri Nov 29 20:59:19 2024 Received: from mail.tlmp.cc (unknown [148.135.17.20]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69BD5157E61; Mon, 16 Sep 2024 13:56:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.135.17.20 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726495005; cv=none; b=LzM6M/5g79cgiTNIM4vfZO9goXz8tnIe5Hmep/CxIJTXvI2/XWj7z1sc2QeG8bCAlWDTflI0/zCu+OWGtyKvctMLxu2Fvwh/ozIhXWW93570rsP8zhprTPnbOmnpmVtuXBWcAK29xQbTzOQx6yWa+cuEqxh+94gpqir+ml5FKmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726495005; c=relaxed/simple; bh=d/XOj0hoNuU9nqfS2fwNSQ1cJ90NqcO0hLBokdH7oBY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KhXMD1TjYJMbbZ9bweDnBT4z1cJ9rvZkjbrLBgsoLde1DuUR8h4ptj36c/EGU8xfpmGigtztSxiReWuFGQl7dRWISzi8fdloAnoLrDbXRxIIoDgwnZoSRbwBKFVbbvE0++eWfw3RwcGs2cXOlav8lDWOVWNWBtC5U9YyReCD5eU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=tlmp.cc; spf=pass smtp.mailfrom=tlmp.cc; dkim=pass (2048-bit key) header.d=tlmp.cc header.i=@tlmp.cc header.b=EXrdKMbP; arc=none smtp.client-ip=148.135.17.20 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=tlmp.cc Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tlmp.cc Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tlmp.cc header.i=@tlmp.cc header.b="EXrdKMbP" Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id 1A8176983D; Mon, 16 Sep 2024 09:56:40 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tlmp.cc; s=dkim; t=1726495002; h=from:subject:date:message-id:to:cc:mime-version: content-transfer-encoding:in-reply-to:references; bh=eiSTaIp2oxZp3XntcBK5EeEI9ViTxMs6XcQ702Or/vY=; b=EXrdKMbPRSLWjUM9rpjDioPo1pyJcUduJGi9/DcPSUznc4+n3PK+QocMh5/3zd5PEb+T1I YRcB2n2fyzAcWZwwf1x8r2KQmlq6J4K5JQvfg4s+6GGtMZsBRkedc66rf1mDJIKEQ/X5hP pkDmgPNP4cuhtoSUWOoBzpATRGIYwu0Ik16Pm7SIcU03oydEj7wY68Y//2CZPg1pSF59FC lTGyi9FjWi9q1YWpDnxQiLyvRugQXGa5w2I4jlum9qaViG9tL16UoCpoEL+oezba+vps0Q dBRUkamfzg3bffUqyoJzfRAhdgDJB7KyE0fXR+BcuA1OYY5yVbxV72L5+vAJ8Q== From: Yiyang Wu To: linux-erofs@lists.ozlabs.org Cc: rust-for-linux@vger.kernel.org, linux-fsdevel@vger.kernel.org, LKML Subject: [RFC PATCH 02/24] erofs: add superblock data structure in Rust Date: Mon, 16 Sep 2024 21:56:12 +0800 Message-ID: <20240916135634.98554-3-toolmanp@tlmp.cc> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916135634.98554-1-toolmanp@tlmp.cc> References: <20240916135634.98554-1-toolmanp@tlmp.cc> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Last-TLS-Session-Version: TLSv1.3 Content-Type: text/plain; charset="utf-8" This patch adds a compilable super_rs.rs and introduces superblock data structure in Rust. Note that this patch leaves C-side code untouched. Signed-off-by: Yiyang Wu --- fs/erofs/Kconfig | 10 ++ fs/erofs/Makefile | 1 + fs/erofs/rust/erofs_sys.rs | 22 +++++ fs/erofs/rust/erofs_sys/superblock.rs | 132 ++++++++++++++++++++++++++ fs/erofs/rust/mod.rs | 4 + fs/erofs/super_rs.rs | 9 ++ 6 files changed, 178 insertions(+) create mode 100644 fs/erofs/rust/erofs_sys.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock.rs create mode 100644 fs/erofs/rust/mod.rs create mode 100644 fs/erofs/super_rs.rs diff --git a/fs/erofs/Kconfig b/fs/erofs/Kconfig index 6ea60661fa55..e2883efbf497 100644 --- a/fs/erofs/Kconfig +++ b/fs/erofs/Kconfig @@ -178,3 +178,13 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI at higher priority. =20 If unsure, say N. + +config EROFS_FS_RUST + bool "EROFS use RUST Replacement (EXPERIMENTAL)" + depends on EROFS_FS && RUST + help + This permits EROFS to use EXPERIMENTAL Rust implementation + for EROFS. This should be considered as an experimental + feature for now. + + If unsure, say N. diff --git a/fs/erofs/Makefile b/fs/erofs/Makefile index 4331d53c7109..fb46a2c7fb50 100644 --- a/fs/erofs/Makefile +++ b/fs/erofs/Makefile @@ -9,3 +9,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) +=3D decompressor_defl= ate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) +=3D decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_BACKED_BY_FILE) +=3D fileio.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) +=3D fscache.o +erofs-$(CONFIG_EROFS_FS_RUST) +=3D super_rs.o diff --git a/fs/erofs/rust/erofs_sys.rs b/fs/erofs/rust/erofs_sys.rs new file mode 100644 index 000000000000..0f1400175fc2 --- /dev/null +++ b/fs/erofs/rust/erofs_sys.rs @@ -0,0 +1,22 @@ +#![allow(dead_code)] +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-or-later + +//! A pure Rust implementation of the EROFS filesystem. +//! Technical Details are documented in the [EROFS Documentation](https://= erofs.docs.kernel.org/en/latest/) + +// It's unavoidable to import alloc here. Since there are so many backends= there and if we want to +// to use trait object to export Filesystem pointer. The alloc crate here = is necessary. + +#[cfg(not(CONFIG_EROFS_FS =3D "y"))] +extern crate alloc; + +/// Erofs requires block index to a 32 bit unsigned integer. +pub(crate) type Blk =3D u32; +/// Erofs requires normal offset to be a 64bit unsigned integer. +pub(crate) type Off =3D u64; +/// Erofs requires inode nid to be a 64bit unsigned integer. +pub(crate) type Nid =3D u64; +/// Erofs Super Offset to read the ondisk superblock +pub(crate) const EROFS_SUPER_OFFSET: Off =3D 1024; +pub(crate) mod superblock; diff --git a/fs/erofs/rust/erofs_sys/superblock.rs b/fs/erofs/rust/erofs_sy= s/superblock.rs new file mode 100644 index 000000000000..213be6dbc553 --- /dev/null +++ b/fs/erofs/rust/erofs_sys/superblock.rs @@ -0,0 +1,132 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-or-later + +use super::*; +use core::mem::size_of; + +/// The ondisk superblock structure. +#[derive(Debug, Clone, Copy, Default)] +#[repr(C)] +pub(crate) struct SuperBlock { + pub(crate) magic: u32, + pub(crate) checksum: i32, + pub(crate) feature_compat: i32, + pub(crate) blkszbits: u8, + pub(crate) sb_extslots: u8, + pub(crate) root_nid: i16, + pub(crate) inos: i64, + pub(crate) build_time: i64, + pub(crate) build_time_nsec: i32, + pub(crate) blocks: i32, + pub(crate) meta_blkaddr: u32, + pub(crate) xattr_blkaddr: u32, + pub(crate) uuid: [u8; 16], + pub(crate) volume_name: [u8; 16], + pub(crate) feature_incompat: i32, + pub(crate) compression: i16, + pub(crate) extra_devices: i16, + pub(crate) devt_slotoff: i16, + pub(crate) dirblkbits: u8, + pub(crate) xattr_prefix_count: u8, + pub(crate) xattr_prefix_start: i32, + pub(crate) packed_nid: i64, + pub(crate) xattr_filter_reserved: u8, + pub(crate) reserved: [u8; 23], +} + +impl TryFrom<&[u8]> for SuperBlock { + type Error =3D core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + value[0..128].try_into() + } +} + +impl From<[u8; 128]> for SuperBlock { + fn from(value: [u8; 128]) -> Self { + Self { + magic: u32::from_le_bytes([value[0], value[1], value[2], value= [3]]), + checksum: i32::from_le_bytes([value[4], value[5], value[6], va= lue[7]]), + feature_compat: i32::from_le_bytes([value[8], value[9], value[= 10], value[11]]), + blkszbits: value[12], + sb_extslots: value[13], + root_nid: i16::from_le_bytes([value[14], value[15]]), + inos: i64::from_le_bytes([ + value[16], value[17], value[18], value[19], value[20], val= ue[21], value[22], + value[23], + ]), + build_time: i64::from_le_bytes([ + value[24], value[25], value[26], value[27], value[28], val= ue[29], value[30], + value[31], + ]), + build_time_nsec: i32::from_le_bytes([value[32], value[33], val= ue[34], value[35]]), + blocks: i32::from_le_bytes([value[36], value[37], value[38], v= alue[39]]), + meta_blkaddr: u32::from_le_bytes([value[40], value[41], value[= 42], value[43]]), + xattr_blkaddr: u32::from_le_bytes([value[44], value[45], value= [46], value[47]]), + uuid: value[48..64].try_into().unwrap(), + volume_name: value[64..80].try_into().unwrap(), + feature_incompat: i32::from_le_bytes([value[80], value[81], va= lue[82], value[83]]), + compression: i16::from_le_bytes([value[84], value[85]]), + extra_devices: i16::from_le_bytes([value[86], value[87]]), + devt_slotoff: i16::from_le_bytes([value[88], value[89]]), + dirblkbits: value[90], + xattr_prefix_count: value[91], + xattr_prefix_start: i32::from_le_bytes([value[92], value[93], = value[94], value[95]]), + packed_nid: i64::from_le_bytes([ + value[96], value[97], value[98], value[99], value[100], va= lue[101], value[102], + value[103], + ]), + xattr_filter_reserved: value[104], + reserved: value[105..128].try_into().unwrap(), + } + } +} + +pub(crate) type SuperBlockBuf =3D [u8; size_of::()]; +pub(crate) const SUPERBLOCK_EMPTY_BUF: SuperBlockBuf =3D [0; size_of::()]; + +/// Used for external address calculation. +pub(crate) struct Accessor { + pub(crate) base: Off, + pub(crate) off: Off, + pub(crate) len: Off, + pub(crate) nr: Off, +} + +impl Accessor { + pub(crate) fn new(address: Off, bits: Off) -> Self { + let sz =3D 1 << bits; + let mask =3D sz - 1; + Accessor { + base: (address >> bits) << bits, + off: address & mask, + len: sz - (address & mask), + nr: address >> bits, + } + } +} + +impl SuperBlock { + pub(crate) fn blk_access(&self, address: Off) -> Accessor { + Accessor::new(address, self.blkszbits as Off) + } + + pub(crate) fn blknr(&self, pos: Off) -> Blk { + (pos >> self.blkszbits) as Blk + } + + pub(crate) fn blkpos(&self, blk: Blk) -> Off { + (blk as Off) << self.blkszbits + } + + pub(crate) fn blksz(&self) -> Off { + 1 << self.blkszbits + } + + pub(crate) fn blk_round_up(&self, addr: Off) -> Blk { + ((addr + self.blksz() - 1) >> self.blkszbits) as Blk + } + + pub(crate) fn iloc(&self, nid: Nid) -> Off { + self.blkpos(self.meta_blkaddr) + ((nid as Off) << (5 as Off)) + } +} diff --git a/fs/erofs/rust/mod.rs b/fs/erofs/rust/mod.rs new file mode 100644 index 000000000000..e6c0731f2533 --- /dev/null +++ b/fs/erofs/rust/mod.rs @@ -0,0 +1,4 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-or-later + +pub(crate) mod erofs_sys; diff --git a/fs/erofs/super_rs.rs b/fs/erofs/super_rs.rs new file mode 100644 index 000000000000..4b8cbef507e3 --- /dev/null +++ b/fs/erofs/super_rs.rs @@ -0,0 +1,9 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-or-later + +//! EROFS Rust Kernel Module Helpers Implementation +//! This is only for experimental purpose. Feedback is always welcome. + +#[allow(dead_code)] +#[allow(missing_docs)] +pub(crate) mod rust; --=20 2.46.0