Skip to main content

starnix_modules_procfs/
fs.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::proc_directory::ProcDirectory;
6use starnix_core::task::CurrentTask;
7use starnix_core::vfs::{
8    CacheMode, FileSystem, FileSystemHandle, FileSystemOps, FileSystemOptions, FsStr,
9};
10
11use starnix_types::vfs::default_statfs;
12use starnix_uapi::errors::Errno;
13use starnix_uapi::{PROC_SUPER_MAGIC, statfs};
14
15struct ProcFsHandle(FileSystemHandle);
16
17/// Returns `kernel`'s procfs instance, initializing it if needed.
18pub fn proc_fs(
19    current_task: &CurrentTask,
20    options: FileSystemOptions,
21) -> Result<FileSystemHandle, Errno> {
22    Ok(current_task
23        .kernel()
24        .expando
25        .get_or_init(|| ProcFsHandle(ProcFs::new_fs(current_task, options)))
26        .0
27        .clone())
28}
29
30/// `ProcFs` is a filesystem that exposes runtime information about a `Kernel` instance.
31#[derive(Debug, Clone)]
32struct ProcFs;
33
34impl FileSystemOps for ProcFs {
35    fn statfs(&self, _fs: &FileSystem, _current_task: &CurrentTask) -> Result<statfs, Errno> {
36        Ok(default_statfs(PROC_SUPER_MAGIC))
37    }
38    fn name(&self) -> &'static FsStr {
39        "proc".into()
40    }
41}
42
43impl ProcFs {
44    /// Creates a new instance of `ProcFs` for the given `kernel`.
45    pub fn new_fs(current_task: &CurrentTask, options: FileSystemOptions) -> FileSystemHandle {
46        let kernel = current_task.kernel();
47        let fs = FileSystem::new(kernel, CacheMode::Uncached, ProcFs, options)
48            .expect("procfs constructed with valid options");
49        let root_ino = fs.allocate_ino();
50        fs.create_root(root_ino, ProcDirectory::new(kernel, &fs));
51        fs
52    }
53}