Resolve some minor issues

This commit is contained in:
Ruihan Li 2025-10-24 14:06:55 +08:00 committed by Jianfeng Jiang
parent d447fe0ca8
commit 4e0142b176
11 changed files with 33 additions and 19 deletions

View File

@ -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.

View File

@ -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<dyn Inode>,
inode_allocator: AtomicU64,
}
impl ProcFs {
pub fn new() -> Arc<Self> {
pub(self) fn new() -> Arc<Self> {
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
}
}

View File

@ -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<dyn FileLike>);
impl FileSymOps {

View File

@ -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<Thread>,
}
impl MountInfoFileOps {
pub fn new_inode(thread_ref: Arc<Thread>, parent: Weak<dyn Inode>) -> Arc<dyn Inode> {
// Reference: <https://elixir.bootlin.com/linux/v6.16.5/source/fs/proc/base.c#L3352>
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<Vec<u8>> {
unimplemented!()
unreachable!()
}
fn read_at(&self, offset: usize, writer: &mut VmWriter) -> Result<usize> {
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();

View File

@ -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<O: FileOps> ProcFileBuilder<O> {
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<O: SymOps> ProcSymBuilder<O> {
self.optional_builder(|ob| ob.parent(parent))
}
#[expect(dead_code)]
pub fn volatile(self) -> Self {
self.optional_builder(|ob| ob.volatile())
}

View File

@ -26,7 +26,7 @@ pub struct ProcDir<D: DirOps> {
}
impl<D: DirOps> ProcDir<D> {
pub fn new(
pub(super) fn new(
dir: D,
fs: Weak<dyn FileSystem>,
parent: Option<Weak<dyn Inode>>,
@ -40,7 +40,6 @@ impl<D: DirOps> ProcDir<D> {
let procfs = arc_fs.downcast_ref::<ProcFs>().unwrap();
procfs.alloc_id()
});
let metadata = Metadata::new_dir(ino, mode, super::BLOCK_SIZE);
Common::new(metadata, fs, is_volatile)
};

View File

@ -17,7 +17,12 @@ pub struct ProcFile<F: FileOps> {
}
impl<F: FileOps> ProcFile<F> {
pub fn new(file: F, fs: Weak<dyn FileSystem>, is_volatile: bool, mode: InodeMode) -> Arc<Self> {
pub(super) fn new(
file: F,
fs: Weak<dyn FileSystem>,
is_volatile: bool,
mode: InodeMode,
) -> Arc<Self> {
let common = {
let arc_fs = fs.upgrade().unwrap();
let procfs = arc_fs.downcast_ref::<ProcFs>().unwrap();

View File

@ -17,7 +17,12 @@ pub struct ProcSym<S: SymOps> {
}
impl<S: SymOps> ProcSym<S> {
pub fn new(sym: S, fs: Weak<dyn FileSystem>, is_volatile: bool, mode: InodeMode) -> Arc<Self> {
pub(super) fn new(
sym: S,
fs: Weak<dyn FileSystem>,
is_volatile: bool,
mode: InodeMode,
) -> Arc<Self> {
let common = {
let arc_fs = fs.upgrade().unwrap();
let procfs = arc_fs.downcast_ref::<ProcFs>().unwrap();

View File

@ -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) {

View File

@ -106,12 +106,12 @@ impl PosixThread {
}
/// Returns a read guard to the filesystem information of the thread.
pub fn fs(&self) -> RwMutexReadGuard<Arc<ThreadFsInfo>> {
pub fn read_fs(&self) -> RwMutexReadGuard<Arc<ThreadFsInfo>> {
self.fs.read()
}
/// Updates the filesystem information of the thread.
pub fn update_fs(&self, new_fs: Arc<ThreadFsInfo>) {
/// Sets the filesystem information of the thread.
pub(in crate::process) fn set_fs(&self, new_fs: Arc<ThreadFsInfo>) {
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

View File

@ -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
}