diff --git a/kernel/src/context.rs b/kernel/src/context.rs index 949a7f6dc..54a028515 100644 --- a/kernel/src/context.rs +++ b/kernel/src/context.rs @@ -83,7 +83,7 @@ impl<'a> CurrentUserSpace<'a> { pub fn is_vmar_shared(&self) -> bool { // If the VMAR is not shared, its reference count should be exactly 2: // one reference is held by `ThreadLocal` and the other by `ProcessVm` in `Process`. - Arc::strong_count(self.0.as_ref().unwrap()) != 2 + Arc::strong_count(self.0.as_ref().unwrap()) > 2 } /// Creates a reader to read data from the user space of the current task. diff --git a/kernel/src/fs/procfs/mod.rs b/kernel/src/fs/procfs/mod.rs index 89cbe2787..a543eb6d6 100644 --- a/kernel/src/fs/procfs/mod.rs +++ b/kernel/src/fs/procfs/mod.rs @@ -56,14 +56,14 @@ const PROC_ROOT_INO: u64 = 1; /// Block size. const BLOCK_SIZE: usize = 1024; -pub struct ProcFs { +struct ProcFs { sb: SuperBlock, root: Arc, inode_allocator: AtomicU64, } impl ProcFs { - pub fn new() -> Arc { + pub(self) fn new() -> Arc { Arc::new_cyclic(|weak_fs| Self { sb: SuperBlock::new(PROC_MAGIC, BLOCK_SIZE, NAME_MAX), root: RootDirOps::new_inode(weak_fs.clone()), @@ -71,7 +71,7 @@ impl ProcFs { }) } - pub(in crate::fs::procfs) fn alloc_id(&self) -> u64 { + pub(self) fn alloc_id(&self) -> u64 { self.inode_allocator.fetch_add(1, Ordering::SeqCst) } } @@ -130,8 +130,10 @@ impl RootDirOps { .ino(PROC_ROOT_INO) .build() .unwrap(); + let weak_ptr = Arc::downgrade(&root_inode); process_table::register_observer(weak_ptr); + root_inode } } diff --git a/kernel/src/fs/procfs/pid/task/fd.rs b/kernel/src/fs/procfs/pid/task/fd.rs index f38cdcf90..1672828c9 100644 --- a/kernel/src/fs/procfs/pid/task/fd.rs +++ b/kernel/src/fs/procfs/pid/task/fd.rs @@ -99,7 +99,7 @@ impl DirOps for FdDirOps { } } -/// Represents the inode at `/proc/[pid]/fd/N`. +/// Represents the inode at `/proc/[pid]/task/[tid]/fd/[n]` (and also `/proc/[pid]/fd/[n]`). struct FileSymOps(Arc); impl FileSymOps { diff --git a/kernel/src/fs/procfs/pid/task/mountinfo.rs b/kernel/src/fs/procfs/pid/task/mountinfo.rs index be56991c3..7c7f66172 100644 --- a/kernel/src/fs/procfs/pid/task/mountinfo.rs +++ b/kernel/src/fs/procfs/pid/task/mountinfo.rs @@ -10,13 +10,14 @@ use crate::{ thread::Thread, }; -/// Represents the inode at `/proc/[pid]/mountinfo`. +/// Represents the inode at `/proc/[pid]/task/[tid]/mountinfo` (and also `/proc/[pid]/mountinfo`). pub struct MountInfoFileOps { thread_ref: Arc, } impl MountInfoFileOps { pub fn new_inode(thread_ref: Arc, parent: Weak) -> Arc { + // Reference: ProcFileBuilder::new(Self { thread_ref }, mkmod!(a+r)) .parent(parent) .build() @@ -26,12 +27,12 @@ impl MountInfoFileOps { impl FileOps for MountInfoFileOps { fn data(&self) -> Result> { - unimplemented!() + unreachable!() } fn read_at(&self, offset: usize, writer: &mut VmWriter) -> Result { let posix_thread = self.thread_ref.as_posix_thread().unwrap(); - let fs = posix_thread.fs(); + let fs = posix_thread.read_fs(); let fs_resolver = fs.resolver().read(); let root_mount = fs_resolver.root().mount_node(); diff --git a/kernel/src/fs/procfs/template/builder.rs b/kernel/src/fs/procfs/template/builder.rs index b5bf1551a..7d1d0fcb1 100644 --- a/kernel/src/fs/procfs/template/builder.rs +++ b/kernel/src/fs/procfs/template/builder.rs @@ -1,7 +1,5 @@ // SPDX-License-Identifier: MPL-2.0 -#![expect(dead_code)] - use super::{ dir::{DirOps, ProcDir}, file::{FileOps, ProcFile}, @@ -90,6 +88,7 @@ impl ProcFileBuilder { self.optional_builder(|ob| ob.parent(parent)) } + #[expect(dead_code)] pub fn volatile(self) -> Self { self.optional_builder(|ob| ob.volatile()) } @@ -131,6 +130,7 @@ impl ProcSymBuilder { self.optional_builder(|ob| ob.parent(parent)) } + #[expect(dead_code)] pub fn volatile(self) -> Self { self.optional_builder(|ob| ob.volatile()) } diff --git a/kernel/src/fs/procfs/template/dir.rs b/kernel/src/fs/procfs/template/dir.rs index 5e2fe964a..62b5e0aea 100644 --- a/kernel/src/fs/procfs/template/dir.rs +++ b/kernel/src/fs/procfs/template/dir.rs @@ -26,7 +26,7 @@ pub struct ProcDir { } impl ProcDir { - pub fn new( + pub(super) fn new( dir: D, fs: Weak, parent: Option>, @@ -40,7 +40,6 @@ impl ProcDir { let procfs = arc_fs.downcast_ref::().unwrap(); procfs.alloc_id() }); - let metadata = Metadata::new_dir(ino, mode, super::BLOCK_SIZE); Common::new(metadata, fs, is_volatile) }; diff --git a/kernel/src/fs/procfs/template/file.rs b/kernel/src/fs/procfs/template/file.rs index fd785c8a1..9469c2894 100644 --- a/kernel/src/fs/procfs/template/file.rs +++ b/kernel/src/fs/procfs/template/file.rs @@ -17,7 +17,12 @@ pub struct ProcFile { } impl ProcFile { - pub fn new(file: F, fs: Weak, is_volatile: bool, mode: InodeMode) -> Arc { + pub(super) fn new( + file: F, + fs: Weak, + is_volatile: bool, + mode: InodeMode, + ) -> Arc { let common = { let arc_fs = fs.upgrade().unwrap(); let procfs = arc_fs.downcast_ref::().unwrap(); diff --git a/kernel/src/fs/procfs/template/sym.rs b/kernel/src/fs/procfs/template/sym.rs index d39e0e945..e8ab9a559 100644 --- a/kernel/src/fs/procfs/template/sym.rs +++ b/kernel/src/fs/procfs/template/sym.rs @@ -17,7 +17,12 @@ pub struct ProcSym { } impl ProcSym { - pub fn new(sym: S, fs: Weak, is_volatile: bool, mode: InodeMode) -> Arc { + pub(super) fn new( + sym: S, + fs: Weak, + is_volatile: bool, + mode: InodeMode, + ) -> Arc { let common = { let arc_fs = fs.upgrade().unwrap(); let procfs = arc_fs.downcast_ref::().unwrap(); diff --git a/kernel/src/process/namespace/unshare.rs b/kernel/src/process/namespace/unshare.rs index fddb5cd97..ad0ec7fc1 100644 --- a/kernel/src/process/namespace/unshare.rs +++ b/kernel/src/process/namespace/unshare.rs @@ -33,7 +33,7 @@ impl ContextUnshareAdminApi for Context<'_> { let mut fs_ref = self.thread_local.borrow_fs_mut(); let new_fs = fs_ref.as_ref().clone(); *fs_ref = Arc::new(new_fs); - self.posix_thread.update_fs(fs_ref.clone()); + self.posix_thread.set_fs(fs_ref.clone()); } fn unshare_sysvsem(&self) { diff --git a/kernel/src/process/posix_thread/mod.rs b/kernel/src/process/posix_thread/mod.rs index 390587a7e..6d1edfd9c 100644 --- a/kernel/src/process/posix_thread/mod.rs +++ b/kernel/src/process/posix_thread/mod.rs @@ -106,12 +106,12 @@ impl PosixThread { } /// Returns a read guard to the filesystem information of the thread. - pub fn fs(&self) -> RwMutexReadGuard> { + pub fn read_fs(&self) -> RwMutexReadGuard> { self.fs.read() } - /// Updates the filesystem information of the thread. - pub fn update_fs(&self, new_fs: Arc) { + /// Sets the filesystem information of the thread. + pub(in crate::process) fn set_fs(&self, new_fs: Arc) { let mut fs_lock = self.fs.write(); *fs_lock = new_fs; } @@ -120,7 +120,7 @@ impl PosixThread { &self.file_table } - /// Get the reference to the signal mask of the thread. + /// Gets the reference to the signal mask of the thread. /// /// Note that while this function offers mutable access to the signal mask, /// it is not sound for callers other than the current thread to modify the diff --git a/kernel/src/process/posix_thread/thread_local.rs b/kernel/src/process/posix_thread/thread_local.rs index aa6da9477..ff843abda 100644 --- a/kernel/src/process/posix_thread/thread_local.rs +++ b/kernel/src/process/posix_thread/thread_local.rs @@ -151,6 +151,8 @@ impl ThreadLocal { } pub fn is_fs_shared(&self) -> bool { + // If the filesystem information is not shared, its reference count should be exactly 2: + // one reference is held by `ThreadLocal` and the other by `PosixThread`. Arc::strong_count(&self.fs.borrow()) > 2 }