From fe633972f3c9c0f9fa034a2219f5b3bccd5363c8 Mon Sep 17 00:00:00 2001 From: Wang Siyuan Date: Wed, 24 Dec 2025 03:40:52 +0000 Subject: [PATCH] Add pseudo `Mount` --- kernel/src/fs/path/mount.rs | 15 +++++++++++++-- kernel/src/fs/pseudofs.rs | 22 ++++++++++++++++++++++ kernel/src/fs/ramfs/memfd.rs | 23 ++++++++++++++++++++--- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/kernel/src/fs/path/mount.rs b/kernel/src/fs/path/mount.rs index c07ff8edb..10341ef02 100644 --- a/kernel/src/fs/path/mount.rs +++ b/kernel/src/fs/path/mount.rs @@ -64,6 +64,8 @@ bitflags! { const NODIRATIME = 1 << 11; /// Update atime relative to mtime/ctime. const RELATIME = 1 << 21; + /// Kernel (pseudo) mount. + const KERNMOUNT = 1 << 22; /// Always perform atime updates. const STRICTATIME = 1 << 24; } @@ -188,11 +190,19 @@ impl Mount { Self::new(fs, PerMountFlags::default(), None, mnt_ns) } + /// Creates a pseudo mount node with an associated FS. + /// + /// This pseudo mount is not mounted on other mount nodes, has no parent, and does not + /// belong to any mount namespace. + pub(in crate::fs) fn new_pseudo(fs: Arc) -> Arc { + Self::new(fs, PerMountFlags::KERNMOUNT, None, Weak::new()) + } + /// The internal constructor. /// - /// Root mount node has no mountpoint which other mount nodes must have mountpoint. + /// A root mount node has no mountpoint, while other mount nodes must have one. /// - /// Here, a Mount is instantiated without an initial mountpoint, + /// Here, a `Mount` is instantiated without an initial mountpoint, /// avoiding fixed mountpoint limitations. This allows the root mount node to /// exist without a mountpoint, ensuring uniformity and security, while all other /// mount nodes must be explicitly assigned a mountpoint to maintain structural integrity. @@ -203,6 +213,7 @@ impl Mount { mnt_ns: Weak, ) -> Arc { let id = ID_ALLOCATOR.get().unwrap().lock().alloc().unwrap(); + Arc::new_cyclic(|weak_self| Self { id, root_dentry: Dentry::new_root(fs.root_inode()), diff --git a/kernel/src/fs/pseudofs.rs b/kernel/src/fs/pseudofs.rs index ac2220a7a..af2e4e2cd 100644 --- a/kernel/src/fs/pseudofs.rs +++ b/kernel/src/fs/pseudofs.rs @@ -10,6 +10,7 @@ use spin::Once; use super::utils::{Extension, InodeIo, StatusFlags}; use crate::{ fs::{ + path::Mount, registry::{FsProperties, FsType}, utils::{ FileSystem, FsEventSubscriberStats, FsFlags, Inode, InodeMode, InodeType, Metadata, @@ -105,6 +106,13 @@ impl PipeFs { PseudoFs::singleton(&PIPEFS, "pipefs", PIPEFS_MAGIC) } + + /// Returns the pseudo mount node of the pipe file system. + fn mount_node() -> &'static Arc { + static PIPEFS_MOUNT: Once> = Once::new(); + + PIPEFS_MOUNT.call_once(|| Mount::new_pseudo(Self::singleton().clone())) + } } pub struct SockFs { @@ -118,6 +126,13 @@ impl SockFs { PseudoFs::singleton(&SOCKFS, "sockfs", SOCKFS_MAGIC) } + + /// Returns the pseudo mount node of the socket file system. + pub fn mount_node() -> &'static Arc { + static SOCKFS_MOUNT: Once> = Once::new(); + + SOCKFS_MOUNT.call_once(|| Mount::new_pseudo(Self::singleton().clone())) + } } pub struct AnonInodeFs { @@ -132,6 +147,13 @@ impl AnonInodeFs { PseudoFs::singleton(&ANON_INODEFS, "anon_inodefs", ANON_INODEFS_MAGIC) } + /// Returns the pseudo mount node of the anonymous inode file system. + pub fn mount_node() -> &'static Arc { + static ANON_INODEFS_MOUNT: Once> = Once::new(); + + ANON_INODEFS_MOUNT.call_once(|| Mount::new_pseudo(Self::singleton().clone())) + } + /// Returns the shared inode of the anonymous inode file system singleton. // // Some members of anon_inodefs (such as epollfd, eventfd, timerfd, etc.) share diff --git a/kernel/src/fs/ramfs/memfd.rs b/kernel/src/fs/ramfs/memfd.rs index f0aaf7acd..947180936 100644 --- a/kernel/src/fs/ramfs/memfd.rs +++ b/kernel/src/fs/ramfs/memfd.rs @@ -22,7 +22,7 @@ use crate::{ file_handle::{FileLike, Mappable}, file_table::FdFlags, inode_handle::{do_fallocate_util, do_resize_util, do_seek_util}, - path::{RESERVED_MOUNT_ID, check_open_util}, + path::{Mount, RESERVED_MOUNT_ID, check_open_util}, tmpfs::TmpFs, utils::{ AccessMode, CachePage, CreationFlags, Extension, FallocMode, FileSystem, Inode, @@ -231,9 +231,26 @@ impl Inode for MemfdInode { } fn fs(&self) -> Arc { - // Reference: + MemfdTmpFs::singleton().clone() + } +} + +struct MemfdTmpFs { + _private: (), +} + +impl MemfdTmpFs { + // Reference: + fn singleton() -> &'static Arc { static MEMFD_TMPFS: Once> = Once::new(); - MEMFD_TMPFS.call_once(TmpFs::new).clone() + + MEMFD_TMPFS.call_once(TmpFs::new) + } + + fn mount_node() -> &'static Arc { + static MEMFD_TMPFS_MOUNT: Once> = Once::new(); + + MEMFD_TMPFS_MOUNT.call_once(|| Mount::new_pseudo(Self::singleton().clone())) } }