From f8e4aefcca5dd41cdc5613a3ab75b27a085a360c Mon Sep 17 00:00:00 2001 From: Chen Chengjun Date: Thu, 28 Aug 2025 09:27:20 +0000 Subject: [PATCH] Add tmpfs support by wrapping ramfs --- kernel/src/fs/mod.rs | 2 ++ kernel/src/fs/ramfs/mod.rs | 2 +- kernel/src/fs/tmpfs/fs.rs | 65 ++++++++++++++++++++++++++++++++++++++ kernel/src/fs/tmpfs/mod.rs | 15 +++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 kernel/src/fs/tmpfs/fs.rs create mode 100644 kernel/src/fs/tmpfs/mod.rs diff --git a/kernel/src/fs/mod.rs b/kernel/src/fs/mod.rs index 3ff7c984a..5fa7ab4b1 100644 --- a/kernel/src/fs/mod.rs +++ b/kernel/src/fs/mod.rs @@ -20,6 +20,7 @@ pub mod registry; pub mod rootfs; pub mod sysfs; pub mod thread_info; +pub mod tmpfs; pub mod utils; use aster_block::BlockDevice; @@ -60,6 +61,7 @@ pub fn init() { procfs::init(); cgroupfs::init(); ramfs::init(); + tmpfs::init(); devpts::init(); ext2::init(); diff --git a/kernel/src/fs/ramfs/mod.rs b/kernel/src/fs/ramfs/mod.rs index 7e9a55a13..c4bc81ffe 100644 --- a/kernel/src/fs/ramfs/mod.rs +++ b/kernel/src/fs/ramfs/mod.rs @@ -11,7 +11,7 @@ use crate::fs::ramfs::fs::RamFsType; mod fs; mod xattr; -const RAMFS_MAGIC: u64 = 0x0102_1994; +const RAMFS_MAGIC: u64 = 0x8584_58f6; const BLOCK_SIZE: usize = 4096; const ROOT_INO: u64 = 1; const NAME_MAX: usize = 255; diff --git a/kernel/src/fs/tmpfs/fs.rs b/kernel/src/fs/tmpfs/fs.rs new file mode 100644 index 000000000..f9b8bb7c6 --- /dev/null +++ b/kernel/src/fs/tmpfs/fs.rs @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: MPL-2.0 + +use crate::{ + fs::{ + ramfs::RamFS, + registry::{FsProperties, FsType}, + utils::{FileSystem, FsFlags, Inode, SuperBlock}, + }, + prelude::*, +}; + +/// The temporary file system (tmpfs) structure. +// +// TODO: Currently, tmpfs is implemented as a thin wrapper around RamFS. +// In the future we need to implement tmpfs-specific features such as +// memory limits and swap support. +pub struct TmpFs { + inner: Arc, +} + +impl FileSystem for TmpFs { + fn sync(&self) -> Result<()> { + // do nothing + Ok(()) + } + + fn root_inode(&self) -> Arc { + self.inner.root_inode() + } + + fn sb(&self) -> SuperBlock { + self.inner.sb() + } + + fn flags(&self) -> FsFlags { + FsFlags::DENTRY_UNEVICTABLE + } +} + +pub(super) struct TmpFsType; + +impl FsType for TmpFsType { + fn name(&self) -> &'static str { + "tmpfs" + } + + fn create( + &self, + _args: Option, + _disk: Option>, + _ctx: &Context, + ) -> Result> { + Ok(Arc::new(TmpFs { + inner: RamFS::new(), + })) + } + + fn properties(&self) -> FsProperties { + FsProperties::empty() + } + + fn sysnode(&self) -> Option> { + None + } +} diff --git a/kernel/src/fs/tmpfs/mod.rs b/kernel/src/fs/tmpfs/mod.rs new file mode 100644 index 000000000..61d78a59c --- /dev/null +++ b/kernel/src/fs/tmpfs/mod.rs @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MPL-2.0 + +//! Temporary file system (tmpfs) based on RamFS. + +use alloc::sync::Arc; + +mod fs; + +#[expect(dead_code)] +const TMPFS_MAGIC: u64 = 0x0102_1994; + +pub(super) fn init() { + let ramfs_type = Arc::new(fs::TmpFsType); + super::registry::register(ramfs_type).unwrap(); +}