erofs: add rust support
cover erofs: add rust support
This is a patchset to add rust skeleton codes to the current Extended Read-Only File System (erofs) implementation suggested by a recent OSPP Project[1]. The implementation is deeply inspired by the current C implementation, and it's based on a generic erofs_sys crate[2] written by me.
The purpose of this patchset is to provide infrastructure to potentially replace all of C codes with rust with better maintainability and performance with the help of Rust's safety features and better optimization capabilities.
This patchset implements basic I/O operations and it's generally functional, however i haven't tested all possible testcases (some of them are chunk-based image files) yet. So it's
welcome to be tested and given supportive reviews.
Many of the "extended" features are disabled by default and still not implemented yet but some of them have been already implemented in the crate but kernel-side trait implementations are not ready yet. I'm considering implementing it in the near future.
Note that, currently Rust VFS patches are still not merged because of the previous rejections suggested by fs-devel team[3]. So this patch set only uses C bindings internally and each unsafe operation is examined. This implementation only offers functions impls and gets its exposed to original C implementation as hooks. Only some of super operations are modified slightly to make sure memory allocation and deallocation are done correctly. Other changes to C are basically importing bindings from rust. Besides, A new erofs_rust_helper.c file is introduced to help rust to deal with folio, iomap and inode internal initializations.
Though it may violate some of the Rust-For-Linux principles, i deem it's still ok to at least give it a try by using the Bindgen C bindings since this work does not affect other Kernel APIs or the current Rust For Linux Kernel crate designs.
This patchset is baed on the latest erofs-linux dev tree. And the corresponding code can also be found on my github repo[4].
[1]: https://summer-ospp.ac.cn/org/prodetail/241920019?list=org&navpage=org
[2]: https://github.com/ToolmanP/erofs-rs
[3]: https://lore.kernel.org/rust-for-linux/ZZWhQGkl0xPiBD5%[email protected]/
[4]: https://github.com/ToolmanP/erofs-rs-linux
Yiyang Wu (3):
erofs: add erofs_sys crate
erofs: add implementation for erofs_sys
erofs: add rust options
fs/erofs/Kconfig | 11 + fs/erofs/Makefile | 1 + fs/erofs/data.c | 83 ++- fs/erofs/dir.c | 20 +- fs/erofs/erofs_rust.rs | 294 +++++++++ fs/erofs/erofs_rust_bindings.h | 47 ++ fs/erofs/erofs_rust_helper.c | 107 ++++ fs/erofs/erofs_rust_helper.h | 40 ++ fs/erofs/inode.c | 51 +- fs/erofs/internal.h | 3 + fs/erofs/namei.c | 30 +- fs/erofs/rust/erofs_sys.rs | 67 ++ fs/erofs/rust/erofs_sys/alloc_helper.rs | 57 ++ fs/erofs/rust/erofs_sys/compression.rs | 18 + fs/erofs/rust/erofs_sys/data.rs | 640 +++++++++++++++++++ fs/erofs/rust/erofs_sys/data/uncompressed.rs | 61 ++ fs/erofs/rust/erofs_sys/devices.rs | 53 ++ fs/erofs/rust/erofs_sys/dir.rs | 83 +++ fs/erofs/rust/erofs_sys/inode.rs | 407 ++++++++++++ fs/erofs/rust/erofs_sys/map.rs | 28 + fs/erofs/rust/erofs_sys/operations.rs | 96 +++ fs/erofs/rust/erofs_sys/superblock.rs | 554 ++++++++++++++++ fs/erofs/rust/erofs_sys/superblock/file.rs | 114 ++++ fs/erofs/rust/erofs_sys/superblock/mem.rs | 156 +++++ fs/erofs/rust/erofs_sys/xattrs.rs | 175 +++++ fs/erofs/rust/kinode.rs | 103 +++ fs/erofs/rust/kinode/kinode_helper.rs | 26 + fs/erofs/rust/mod.rs | 6 + fs/erofs/rust/sources.rs | 5 + fs/erofs/rust/sources/mm.rs | 62 ++ fs/erofs/rust/sources/page_helper.rs | 12 + fs/erofs/super.c | 223 +++++-- 32 files changed, 3523 insertions(+), 110 deletions(-) create mode 100644 fs/erofs/erofs_rust.rs create mode 100644 fs/erofs/erofs_rust_bindings.h create mode 100644 fs/erofs/erofs_rust_helper.c create mode 100644 fs/erofs/erofs_rust_helper.h create mode 100644 fs/erofs/rust/erofs_sys.rs create mode 100644 fs/erofs/rust/erofs_sys/alloc_helper.rs create mode 100644 fs/erofs/rust/erofs_sys/compression.rs create mode 100644 fs/erofs/rust/erofs_sys/data.rs create mode 100644 fs/erofs/rust/erofs_sys/data/uncompressed.rs create mode 100644 fs/erofs/rust/erofs_sys/devices.rs create mode 100644 fs/erofs/rust/erofs_sys/dir.rs create mode 100644 fs/erofs/rust/erofs_sys/inode.rs create mode 100644 fs/erofs/rust/erofs_sys/map.rs create mode 100644 fs/erofs/rust/erofs_sys/operations.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock/file.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock/mem.rs create mode 100644 fs/erofs/rust/erofs_sys/xattrs.rs create mode 100644 fs/erofs/rust/kinode.rs create mode 100644 fs/erofs/rust/kinode/kinode_helper.rs create mode 100644 fs/erofs/rust/mod.rs create mode 100644 fs/erofs/rust/sources.rs create mode 100644 fs/erofs/rust/sources/mm.rs create mode 100644 fs/erofs/rust/sources/page_helper.rs
Signature · 1 line
2.45.2
RFC PATCH 1/3 erofs: add erofs_sys crate
This commit imports a erofs rust implementation from the following repo [1]. This crate introduces several key traits with only transparent access, it does not support compression backend. In kernel, the folio source is used as the memory backend for this filesystem implementation.
Also this crate provides helper function for Box operations to bridge the difference between heap allocations API from userspace.
However, since in-tree crate support is not yet completed, currently the crate is imported as a module to the rust code.
[1]: https://github.com/ToolmanP/erofs-rs
--- fs/erofs/rust/erofs_sys/alloc_helper.rs | 57 ++ fs/erofs/rust/erofs_sys/compression.rs | 18 + fs/erofs/rust/erofs_sys/data.rs | 640 +++++++++++++++++++ fs/erofs/rust/erofs_sys/data/uncompressed.rs | 61 ++ fs/erofs/rust/erofs_sys/devices.rs | 53 ++ fs/erofs/rust/erofs_sys/dir.rs | 83 +++ fs/erofs/rust/erofs_sys/inode.rs | 407 ++++++++++++ fs/erofs/rust/erofs_sys/map.rs | 28 + fs/erofs/rust/erofs_sys/operations.rs | 96 +++ fs/erofs/rust/erofs_sys/superblock.rs | 554 ++++++++++++++++ fs/erofs/rust/erofs_sys/superblock/file.rs | 114 ++++ fs/erofs/rust/erofs_sys/superblock/mem.rs | 156 +++++ fs/erofs/rust/erofs_sys/xattrs.rs | 175 +++++ 13 files changed, 2442 insertions(+) create mode 100644 fs/erofs/rust/erofs_sys/alloc_helper.rs create mode 100644 fs/erofs/rust/erofs_sys/compression.rs create mode 100644 fs/erofs/rust/erofs_sys/data.rs create mode 100644 fs/erofs/rust/erofs_sys/data/uncompressed.rs create mode 100644 fs/erofs/rust/erofs_sys/devices.rs create mode 100644 fs/erofs/rust/erofs_sys/dir.rs create mode 100644 fs/erofs/rust/erofs_sys/inode.rs create mode 100644 fs/erofs/rust/erofs_sys/map.rs create mode 100644 fs/erofs/rust/erofs_sys/operations.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock/file.rs create mode 100644 fs/erofs/rust/erofs_sys/superblock/mem.rs create mode 100644 fs/erofs/rust/erofs_sys/xattrs.rs
fs/erofs/rust/erofs_sys/alloc_helper.rs new file +57 -0
@@ -0,0 +1,57 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +/// This module provides helper functions for the alloc crate +/// Note that in linux kernel, the allocation is fallible however in userland it is not. +/// Since most of the functions depend on infallible allocation, here we provide helper functions +/// so that most of codes don't need to be changed. + +#[cfg( CONFIG_EROFS_FS = "y" )] +use kernel::prelude::*; + +use alloc::boxed::Box ; +use alloc::vec::Vec ; + +pub (crate )fn push_vec <T >(v: &mut Vec <T >, value:T ) { +match () { +#[cfg( CONFIG_EROFS_FS = "y" )] + () => { + v.push (value,GFP_KERNEL ).unwrap (); + } +#[cfg(not( CONFIG_EROFS_FS = "y" ))] + () => { + v.push (value); + } + } +} + +pub (crate )fn extend_from_slice <T :Clone >(v: &mut Vec <T >, slice: &[T ]) { +match () { +#[cfg( CONFIG_EROFS_FS = "y" )] + () => { + v.extend_from_slice (slice,GFP_KERNEL ).unwrap (); + } +#[cfg(not( CONFIG_EROFS_FS = "y" ))] + () => { + v.extend_from_slice (slice); + } + } +} + +pub (crate )fn heap_alloc <T >(value:T ) ->Box <T > { +match () { +#[cfg( CONFIG_EROFS_FS = "y" )] + () =>Box ::new (value,GFP_KERNEL ).unwrap (), +#[cfg(not( CONFIG_EROFS_FS = "y" ))] + () =>Box ::new (value), + } +} + +pub (crate )fn vec_with_capacity <T >(capacity:usize ) ->Vec <T > { +match () { +#[cfg( CONFIG_EROFS_FS = "y" )] + () =>Vec ::with_capacity (capacity,GFP_KERNEL ).unwrap (), +#[cfg(not( CONFIG_EROFS_FS = "y" ))] + () =>Vec ::with_capacity (capacity), + } +}
fs/erofs/rust/erofs_sys/compression.rs new file +18 -0
@@ -0,0 +1,18 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +#[derive( Debug , Clone , Copy )] +#[repr( C )] +pub (crate )enum SuperblockCompressionInfo { +AvailableComprAlgs (u16 ), +Lz4MaxDistance (u16 ), +} + +#[allow(dead_code)] +pub (crate )enum InodeCompressionInfo {} + +impl Default for SuperblockCompressionInfo { +fn default () ->Self { +Self ::AvailableComprAlgs (0 ) + } +}
fs/erofs/rust/erofs_sys/data.rs new file +640 -0
640 lines not on this page · raw message
fs/erofs/rust/erofs_sys/data/uncompressed.rs new file +61 -0
61 lines not on this page · raw message
fs/erofs/rust/erofs_sys/devices.rs new file +53 -0
53 lines not on this page · raw message
fs/erofs/rust/erofs_sys/dir.rs new file +83 -0
83 lines not on this page · raw message
fs/erofs/rust/erofs_sys/inode.rs new file +407 -0
407 lines not on this page · raw message
fs/erofs/rust/erofs_sys/map.rs new file +28 -0
@@ -0,0 +1,28 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +use super ::*; + +pub (crate )const MAP_MAPPED :u32 =0x0001 ; +pub (crate )const MAP_META :u32 =0x0002 ; +pub (crate )const MAP_ENCODED :u32 =0x0004 ; +pub (crate )const MAP_FULL_MAPPED :u32 =0x0008 ; +pub (crate )const MAP_FRAGMENT :u32 =0x0010 ; +pub (crate )const MAP_PARTIAL_REF :u32 =0x0020 ; + +#[derive( Debug , Default )] +#[repr( C )] +pub (crate )struct AddressMap { +pub (crate ) start:Off , +pub (crate ) len:Off , +} + +#[derive( Debug , Default )] +#[repr( C )] +pub (crate )struct Map { +pub (crate ) logical:AddressMap , +pub (crate ) physical:AddressMap , +pub (crate ) device_id:u16 , +pub (crate ) algorithm_format:u16 , +pub (crate ) flags:u32 , +}
fs/erofs/rust/erofs_sys/operations.rs new file +96 -0
96 lines not on this page · raw message
fs/erofs/rust/erofs_sys/superblock.rs new file +554 -0
554 lines not on this page · raw message
fs/erofs/rust/erofs_sys/superblock/file.rs new file +114 -0
114 lines not on this page · raw message
fs/erofs/rust/erofs_sys/superblock/mem.rs new file +156 -0
156 lines not on this page · raw message
fs/erofs/rust/erofs_sys/xattrs.rs new file +175 -0
175 lines not on this page · raw message
Signature · 1 line
2.45.2
RFC PATCH 2/3 erofs: add implementation for erofs_sys
In reply to Yiyang Wu via Linux-erofsImplements Inode, InodeCollection, MemorySource in Kernel Space. Exposes VFS Compatible Function Pointers to the Kernel Space.
Note that currently vfs abstraction support is not completed, a lot of c bindings are used here.
This commit also provides some helper functions to be exported to rust so that rust can call inline functions not generated by bindgen.
--- fs/erofs/erofs_rust.rs | 294 ++++++++++++++++++++++++++ fs/erofs/erofs_rust_bindings.h | 47 ++++ fs/erofs/erofs_rust_helper.c | 107 ++++++++++ fs/erofs/erofs_rust_helper.h | 40 ++++ fs/erofs/rust/erofs_sys.rs | 67 ++++++ fs/erofs/rust/kinode.rs | 103 +++++++++ fs/erofs/rust/kinode/kinode_helper.rs | 26 +++ fs/erofs/rust/mod.rs | 6 + fs/erofs/rust/sources.rs | 5 + fs/erofs/rust/sources/mm.rs | 62 ++++++ fs/erofs/rust/sources/page_helper.rs | 12 ++ 11 files changed, 769 insertions(+) create mode 100644 fs/erofs/erofs_rust.rs create mode 100644 fs/erofs/erofs_rust_bindings.h create mode 100644 fs/erofs/erofs_rust_helper.c create mode 100644 fs/erofs/erofs_rust_helper.h create mode 100644 fs/erofs/rust/erofs_sys.rs create mode 100644 fs/erofs/rust/kinode.rs create mode 100644 fs/erofs/rust/kinode/kinode_helper.rs create mode 100644 fs/erofs/rust/mod.rs create mode 100644 fs/erofs/rust/sources.rs create mode 100644 fs/erofs/rust/sources/mm.rs create mode 100644 fs/erofs/rust/sources/page_helper.rs
fs/erofs/erofs_rust.rs new file +294 -0
@@ -0,0 +1,294 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +//! EROFS Rust Kernel Module Helpers Implementation +//! This is only for experimental purpose. This is not guaranteed to work. +//! Note that this module must be rewritten after the rust VFS abstraction is merged. +//! Currently a lot of c bindings are used which violate the principles provided by Rust-For-Linux + +#[allow(dead_code)] +#[allow(missing_docs)] +pub (crate )mod rust; +use core::ffi::c_void; +use core::ptr::NonNull ; +use kernel::bindings::{ + address_space, d_make_root, d_obtain_alias, d_splice_alias, dentry, dir_context, file, inode, + inode_init_once, super_block, +}; +use kernel::types::ForeignOwnable ; +use kernel::{ + container_of, + prelude::{Box ,Vec }, +}; +use rust::{ + erofs_sys::{ + alloc_helper::*, + data::uncompressed::*, + data::*, + inode::*, + map::*, + operations::*, + superblock::{mem::*, *}, + *, + }, + kinode::{KernelInode ,KernelInodeCollection }, + sources::mm::FolioSource , +}; + +/// This is just a log prefix for kernel module so that pr_info can be used +pub const __LOG_PREFIX: &[u8 ] =b"erofs_rust: \0" ; + +/// KernelSuperblockInfo defined by embedded Kernel Inode +pub (crate )type KernelSuperblockInfo =SuperblockInfo <KernelInode ,KernelInodeCollection >; + +/// Allocating a rust implementation of super_block_info c_void when calling from fill_super +/// Note that this function needs a fixed address_space before we can use the vfs vtable to bind the +/// operations. This is left as it is for now. +#[no_mangle] +pub unsafe extern "C" fn erofs_alloc_sbi_rust ( + sb:NonNull <super_block >, + address_space:NonNull <address_space >, +) -> *const c_void { +// We have to use heap_alloc here to erase the signature of MemFileSystem +let sb =heap_alloc (SuperblockInfo ::new ( +heap_alloc (MemFileSystem ::new (UncompressedBackend ::new ( +FolioSource ::new (address_space), + ))), +KernelInodeCollection ::new (sb), + )); +return sb.into_foreign (); +} + +/// SAFETY: +/// Cast the c_void back to KernelSuperblockInfo. +/// This seems to prune to some concurrency issues +/// but the fact is that only KernelInodeCollection field can have mutability. +/// However, it's backed by the original iget_locked5 and it's already preventing +/// any concurrency issues. So it's safe to be casted mutable here even if it's not backed by +/// Arc/Mutex instead of using generic method from Foriegn Ownable which only provides +/// immutable reference casting. +fn as_sbi (f: *mut c_void ) -> &'staticmut KernelSuperblockInfo { +unsafe { &mut *(fas *mut KernelSuperblockInfo ) } +} + +/// Free the KernelSuperblockInfo that was embedded into the superblock in the c implementation +#[no_mangle] +pub unsafe extern "C" fn erofs_free_sbi_rust (sb: *mut super_block ) { +// this will simply get dropped immediately after receiving the ownership. +unsafe {drop (Box ::<KernelSuperblockInfo >::from_foreign ((*sb).s_fs_info)) }; +} + +/// A helper sturct to map blocks for iomap_begin because iomap is not generated by bindgen +#[repr( C )] +pub struct ErofsRustMap { + m_pa:u64 , + m_la:u64 , + m_plen:u64 , + m_llen:u64 , + m_flags:u32 , + inline_data: *const core::ffi::c_void , + private: *const core::ffi::c_void , +} + +/// Lookup function for dentry-inode lookup replacement +#[no_mangle] +pub unsafe extern "C" fn erofs_lookup_rust ( + k_inode: *mut inode , + dentry: *mut dentry , + _flags:u32 , +) -> *mut dentry { +// SAFETY: We are sure that the inode is a Kernel Inode since alloc_inode is called +let inode =unsafe { &*container_of! (k_inode,KernelInode , k_inode) }; +let erofs_sbi =as_sbi (unsafe { (*(*k_inode).i_sb).s_fs_info }); + +// SAFETY: this is backed by qstr which is c representation of a valid slice. +let name =unsafe { + core::str::from_utf8_unchecked (core::slice::from_raw_parts ( + (*dentry).d_name.name, + (*dentry).d_name.__bindgen_anon_1.__bindgen_anon_1.lenas usize , + )) + }; + +let k_inode: *mut inode =dir_lookup ( + erofs_sbi.filesystem.as_ref (), + &mut erofs_sbi.inodes, + inode, + name, + ) + .map_or (core::ptr::null_mut (), |result| result.k_inode.as_mut_ptr ()); + +// SAFETY: We are sure that the inner k_inode has already been initialized +unsafe {d_splice_alias (k_inode, dentry) } +} + +/// Get parent inode +#[no_mangle] +pub unsafe extern "C" fn erofs_get_parent_rust (child: *mut dentry ) -> *mut dentry { +// SAFETY: We are sure that the inode is a Kernel Inode since alloc_inode is called +let k_inode =unsafe { (*child).d_inode }; +let erofs_sbi =as_sbi (unsafe { (*(*k_inode).i_sb).s_fs_info }); +let inode =unsafe { &*container_of! (k_inode,KernelInode , k_inode) }; + +let k_inode: *mut inode =dir_lookup ( + erofs_sbi.filesystem.as_ref (), + &mut erofs_sbi.inodes, + inode, +".." , + ) + .map_or (core::ptr::null_mut (), |result| result.k_inode.as_mut_ptr ()); + +// SAFETY: We are sure that the inner k_inode has already been initialized +unsafe {d_obtain_alias (k_inode) } +} + +/// Readdir +#[no_mangle] +pub unsafe extern "C" fn erofs_readdir_rust (f: *mut file , ctx: *mut dir_context ) ->i32 { +let inode =unsafe { &*container_of! ((*f).f_inode,KernelInode , k_inode) }; +let vnode =unsafe { (*f).f_inode }; +let sb =unsafe { (*vnode).i_sb }; +let erofs_sbi =as_sbi (unsafe { (*sb).s_fs_info }); +let offset =unsafe { (*ctx).pos }; + erofs_sbi + .filesystem + .fill_dentries (inode, offsetas Off , &mut |dir, pos|unsafe { +// inline expansion from dir_emit + (*ctx).actor.unwrap ()( + ctx, + dir.name.as_ptr ().cast (), + dir.name.len ()as i32 , + posas i64 , + dir.desc.nidas u64 , + dir.desc.file_typeas u32 , + ); + (*ctx).pos = posas i64 ; + }); +unsafe { (*ctx).pos = inode.info ().file_size ()as i64 } +0 +} + +/// MapBlocks used for iomap_begin +#[no_mangle] +pub unsafe extern "C" fn erofs_iomap_begin_rust (k_inode: *mut inode , map: *mut ErofsRustMap ) { +let erofs_sbi =as_sbi (unsafe { (*(*k_inode).i_sb).s_fs_info }); +let k_inode =unsafe { &*container_of! (k_inode,KernelInode , k_inode) }; +let m = erofs_sbi.filesystem.map (k_inode,unsafe { (*map).m_la }); + +unsafe { + (*map).m_pa = m.physical.start; + (*map).m_la = m.logical.start; + (*map).m_plen = m.physical.len; + (*map).m_llen = m.logical.len; + (*map).m_flags = m.flags; + } +if m.flags &MAP_META !=0 && m.logical.len !=0 { +let buffer = erofs_sbi + .filesystem + .continous_iter (m.physical.start, m.physical.len) + .next () + .unwrap (); +unsafe { + (*map).inline_data = buffer.content ().as_ptr ().cast (); + (*map).private =heap_alloc (buffer).into_foreign (); + } + }else { +unsafe { + (*map).inline_data = core::ptr::null_mut (); + (*map).private = core::ptr::null_mut (); + } + } +} + +/// iomap_end +#[no_mangle] +pub unsafe extern "C" fn erofs_iomap_end_rust (map: *mut ErofsRustMap ) { +unsafe { +if !(*map).private.is_null () { +drop (Box ::<Box <dyn Buffer + 'static>>::from_foreign ( + (*map).private, + )) + } + } +} + +/// Map blocks in inline mode exported for iget5_locked to be actually functional +#[no_mangle] +pub unsafe extern "C" fn erofs_iget5_eq_rust (kinode: *const c_void , opaque: *const c_void ) ->i32 { +let nid =unsafe { *(opaqueas *const Nid ) }; +let inode =unsafe {container_of! (kinode,KernelInode , k_inode) }; + (unsafe { (*inode).nid () == nid })as i32 +} + +/// init_once replacement +#[no_mangle] +pub unsafe extern "C" fn erofs_inode_init_once_rust (ptr: *const c_void ) { +let inode =unsafe { &mut *(ptras *mut KernelInode ) }; +unsafe {inode_init_once (inode.k_inode.as_mut_ptr ()) } +} + +/// iget replacement +#[no_mangle] +pub unsafe extern "C" fn erofs_iget_rust (sb: *mut super_block , nid:Nid ) -> *mut inode { +let erofs_sbi =as_sbi (unsafe { (*sb).s_fs_info }); +read_inode (erofs_sbi.filesystem.as_ref (), &mut erofs_sbi.inodes, nid) + .k_inode + .as_mut_ptr () +} + +///make_root_call for setting up the root dentry for superblock +#[no_mangle] +pub unsafe extern "C" fn erofs_make_root_rust (sb: *mut super_block ) -> *mut dentry { +let erofs_sbi =as_sbi (unsafe { (*sb).s_fs_info }); +unsafe { +d_make_root ( +read_inode ( + erofs_sbi.filesystem.as_ref (), + &mut erofs_sbi.inodes, + erofs_sbi.filesystem.superblock ().root_nidas u64 , + ) + .k_inode + .as_mut_ptr (), + ) + } +} + +/// get_symlink_data_inline +#[no_mangle] +pub unsafe extern "C" fn erofs_get_inline_symlink_data_rust (k_inode: *mut inode ) -> *mut c_void { +let inode =unsafe { &*container_of! (k_inode,KernelInode , k_inode) }; +let erofs_sbi =as_sbi (unsafe { (*(*k_inode).i_sb).s_fs_info }); + +if inode.info ().format ().layout () !=Layout ::FlatInline + || inode.info ().inode_size () >= erofs_sbi.filesystem.blksz () + { + core::ptr::null_mut () + }else { +let mut cursor:usize =0 ; +let mut symlink:Vec <u8 > =vec_with_capacity (inode.info ().inode_size ()as usize +1 ); +for blockin erofs_sbi.filesystem.mapped_iter (inode,0 ) { +let data = block.content (); +let len = data.len (); +unsafe { + symlink + .as_mut_ptr () + .add (cursor) + .copy_from (data.as_ptr (), len); + } + cursor += len; + } + +unsafe { + *(symlink.as_mut_ptr ().add (cursor)) ='\0' as u8 ; + } + + symlink.leak ().as_mut_ptr ().cast () + } +} + +/// Exposed as the inode_size which is used as a param to guide the kmem_cache_create. +#[no_mangle] +pub static EROFS_INODE_SIZE :Off = core::mem::size_of ::<KernelInode >()as Off ; + +/// Exposed as the inode_offset so that when freeing the inode, the correct offset can be used. +#[no_mangle] +pub static EROFS_INODE_OFFSET :Off = core::mem::offset_of!(KernelInode , k_inode)as Off ;
fs/erofs/erofs_rust_bindings.h new file +47 -0
47 lines not on this page · raw message
fs/erofs/erofs_rust_helper.c new file +107 -0
107 lines not on this page · raw message
fs/erofs/erofs_rust_helper.h new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0-only +// This is a helper header to dodge the missing static inline in bindgen + +#ifndef __EROFS_RUST_HELPER_H +#define __EROFS_RUST_HELPER_H + +#include <linux/fs.h> +#include <linux/dax.h> +#include <linux/dcache.h> +#include <linux/mm.h> +#include <linux/module.h> +#include <linux/pagemap.h> +#include <linux/bio.h> +#include <linux/magic.h> +#include <linux/slab.h> +#include <linux/vmalloc.h> +#include <linux/iomap.h> + +#include "internal.h" + + +struct erofs_inode_init_param_rust { +__le16 i_mode; +__le32 i_uid; +__le32 i_gid; +__le32 i_nlink; +__le32 i_mtime_nsec; +__le64 i_mtime; +__le64 i_size; +__le64 i_blocks; +}; +void *erofs_get_page (void *mapping,pgoff_t index); +void erofs_put_page (void *address); +void erofs_init_inode_unlock (struct inode *inode,const struct erofs_inode_init_param_rust *param); +struct inode *erofs_iget_locked_noinit (struct super_block *sb,erofs_nid_t nid, +int *init); + +void *erofs_get_inline_symlink_data_rust (struct inode *inode); + +#endif
fs/erofs/rust/erofs_sys.rs new file +67 -0
67 lines not on this page · raw message
fs/erofs/rust/kinode.rs new file +103 -0
103 lines not on this page · raw message
fs/erofs/rust/kinode/kinode_helper.rs new file +26 -0
@@ -0,0 +1,26 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +use super ::super ::erofs_sys::Off ; +use core::ffi::{c_int, c_void}; + +#[repr( C )] +pub (crate )struct ErofsInodeInitParam { +pub (crate ) i_mode:u16 , +pub (crate ) i_uid:u32 , +pub (crate ) i_gid:u32 , +pub (crate ) i_nlink:u32 , +pub (crate ) i_mtime_nsec:u32 , +pub (crate ) i_mtime:u64 , +pub (crate ) i_size:u64 , +pub (crate ) i_blocks:u64 +} + +extern "C" { + +#[link_name = "erofs_init_inode_unlock" ] +pub (crate )fn iget_unlock (inode: *mut c_void , param: *const ErofsInodeInitParam ); + +#[link_name = "erofs_iget_locked_noinit" ] +pub (crate )fn iget_locked (sb: *mut c_void , nid:Off , is_new: *mut c_int ) -> *mut c_void ; +}
fs/erofs/rust/mod.rs new file +6 -0
@@ -0,0 +1,6 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +pub (crate )mod erofs_sys; +pub (crate )mod kinode; +pub (crate )mod sources;
fs/erofs/rust/sources.rs new file +5 -0
@@ -0,0 +1,5 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +pub (crate )mod mm; +mod page_helper;
fs/erofs/rust/sources/mm.rs new file +62 -0
62 lines not on this page · raw message
fs/erofs/rust/sources/page_helper.rs new file +12 -0
@@ -0,0 +1,12 @@ +// Copyright 2024 Yiyang Wu +// SPDX-License-Identifier: MIT or GPL-2.0-only + +use core::ffi::{c_ulong, c_void}; + +extern "C" { +#[link_name = "erofs_put_page" ] +pub (crate )fn put_page (addr: *mut c_void ); + +#[link_name = "erofs_get_page" ] +pub (crate )fn get_page (mapping: *mut c_void , index:c_ulong ) -> *mut c_void ; +}
Signature · 1 line
2.45.2
RFC PATCH 3/3 erofs: add rust options
In reply to Yiyang Wu via Linux-erofsThis commit adds rust option in Kconfig. Note that currently when enabling rust support, all extended features are disabled only basic operations are supported.
--- fs/erofs/Kconfig | 11 +++ fs/erofs/Makefile | 1 + fs/erofs/data.c | 83 +++++++++++++---- fs/erofs/dir.c | 20 +++- fs/erofs/inode.c | 51 ++++++---- fs/erofs/internal.h | 3 + fs/erofs/namei.c | 30 ++++-- fs/erofs/super.c | 223 ++++++++++++++++++++++++++++++++------------ 8 files changed, 312 insertions(+), 110 deletions(-)
fs/erofs/Kconfig +11 -0
@@ -158,3 +158,14 @@ config EROFS_FS_PCPU_KTHREAD_HIPRI at higher priority. If unsure, say N. + +config EROFS_FS_RUST + bool "EROFS use RUST Replacement (EXPERIMENTAL)" + depends on EROFS_FS && RUST && !EROFS_FS_ZIP && !EROFS_FS_ONDEMAND && !EROFS_FS_XATTR + default n + help + This permits EROFS to use EXPERIMENTAL rust implementation for + EROFS. This is highly unstable and buggy and should be considered + as an experimental feature only. + + IF SURE, say Y.
fs/erofs/Makefile +1 -0
@@ -8,3 +8,4 @@ erofs-$(CONFIG_EROFS_FS_ZIP_LZMA) += decompressor_lzma.o erofs-$(CONFIG_EROFS_FS_ZIP_DEFLATE) += decompressor_deflate.o erofs-$(CONFIG_EROFS_FS_ZIP_ZSTD) += decompressor_zstd.o erofs-$(CONFIG_EROFS_FS_ONDEMAND) += fscache.o +erofs-$(CONFIG_EROFS_FS_RUST) += erofs_rust_helper.o erofs_rust.o
fs/erofs/data.c +66 -17
153 lines not on this page · raw message
fs/erofs/dir.c +17 -3
@@ -4,8 +4,15 @@* https://www.huawei.com/ * Copyright (C) 2022, Alibaba Cloud */ + +#ifdef CONFIG_EROFS_FS_RUST +#include "erofs_rust_bindings.h" +#endif +#include "internal.h" +#ifndef CONFIG_EROFS_FS_RUST +static int erofs_fill_dentries (struct inode *dir,struct dir_context *ctx,void *dentry_blk,struct erofs_dirent *de,unsigned int nameoff0,unsigned int maxsize)
@@ -93,8 +100,15 @@ static int erofs_readdir(struct file *f, struct dir_context *ctx)return err <0 ? err :0 ; } +#endif +const struct file_operations erofs_dir_fops = { - .llseek = generic_file_llseek, - .read = generic_read_dir, - .iterate_shared = erofs_readdir, + .llseek = generic_file_llseek, + .read = generic_read_dir, +#ifdef CONFIG_EROFS_FS_RUST + .iterate_shared = erofs_readdir_rust, +#else + + .iterate_shared = erofs_readdir, +#endif };
fs/erofs/inode.c +32 -19
145 lines not on this page · raw message
fs/erofs/internal.h +3 -0
@@ -19,6 +19,9 @@#include <linux/vmalloc.h> #include <linux/iomap.h> #include "erofs_fs.h" +#ifdef CONFIG_EROFS_FS_RUST +#include "erofs_rust_bindings.h" +#endif /* redefine pr_fmt "erofs: " */ #undef pr_fmt
fs/erofs/namei.c +20 -10
@@ -4,6 +4,7 @@* https://www.huawei.com/ * Copyright (C) 2022, Alibaba Cloud */ +#include "xattr.h" #include <trace/events/erofs.h>
@@ -40,7 +41,7 @@ static inline int erofs_dirnamecmp(const struct erofs_qstr *qn,return qn->name[i] =='\0' ?0 :1 ; } -#define nameoff_from_disk (off, sz) (le16_to_cpu(off) & ((sz) - 1)) +#define nameoff_from_disk (off, sz) (le16_to_cpu(off) & ((sz) - 1)) static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name, u8 *data,
@@ -58,15 +59,16 @@ static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name,while (head <= back) {const int mid = head + (back - head) /2 ; -const int nameoff =nameoff_from_disk (de[mid].nameoff, - dirblksize); +const int nameoff = +nameoff_from_disk (de[mid].nameoff, dirblksize);unsigned int matched =min (startprfx, endprfx);struct erofs_qstr dname = { .name = data + nameoff, .end = mid >= ndirents -1 ? - data + dirblksize : - data +nameoff_from_disk (de[mid +1 ].nameoff, - dirblksize) + data + dirblksize : + data +nameoff_from_disk ( + de[mid +1 ].nameoff, + dirblksize) };/* string comparison without already matched prefix */
@@ -87,7 +89,8 @@ static struct erofs_dirent *find_target_dirent(struct erofs_qstr *name, }static void *erofs_find_target_block (struct erofs_buf *target, -struct inode *dir,struct erofs_qstr *name,int *_ndirents) +struct inode *dir,struct erofs_qstr *name, +int *_ndirents) {unsigned int bsz =i_blocksize (dir);int head =0 , back =erofs_iblks (dir) -1 ;
@@ -124,7 +127,8 @@ static void *erofs_find_target_block(struct erofs_buf *target,if (ndirents ==1 ) dname.end = (u8 *)de + bsz;else - dname.end = (u8 *)de + + dname.end = + (u8 *)de +nameoff_from_disk (de[1 ].nameoff, bsz);/* string comparison without already matched prefix */
@@ -150,7 +154,7 @@ static void *erofs_find_target_block(struct erofs_buf *target, *_ndirents = ndirents;continue ; } -out:/* free if the candidate is valid */ +out:/* free if the candidate is valid */ if (!IS_ERR (candidate))erofs_put_metabuf (target);return de;
@@ -190,6 +194,7 @@ int erofs_namei(struct inode *dir, const struct qstr *name, erofs_nid_t *nid,return PTR_ERR_OR_ZERO (de); } +#ifndef CONFIG_EROFS_FS_RUST static struct dentry *erofs_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags) {
@@ -214,11 +219,16 @@ static struct dentry *erofs_lookup(struct inode *dir, struct dentry *dentry, inode =erofs_iget (dir->i_sb, nid);return d_splice_alias (inode, dentry); } +#endif const struct inode_operations erofs_dir_iops = { - .lookup = erofs_lookup, .getattr = erofs_getattr, +#ifdef CONFIG_EROFS_FS_RUST + .lookup = erofs_lookup_rust, +#else .listxattr = erofs_listxattr, .get_inode_acl = erofs_get_acl, + .lookup = erofs_lookup, +#endif .fiemap = erofs_fiemap, };
fs/erofs/super.c +162 -61
524 lines not on this page · raw message
Signature · 1 line
2.45.2