starnix_core/vfs/socket/
socket_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::task::{CurrentTask, Kernel};
6use crate::vfs::{
7    CacheMode, FileSystem, FileSystemHandle, FileSystemOps, FileSystemOptions, FsStr,
8};
9use starnix_sync::{FileOpsCore, LockEqualOrBefore, Locked};
10use starnix_types::vfs::default_statfs;
11use starnix_uapi::errors::Errno;
12use starnix_uapi::{SOCKFS_MAGIC, statfs};
13
14/// `SocketFs` is the file system where anonymous socket nodes are created, for example in
15/// `sys_socket`.
16pub struct SocketFs;
17impl FileSystemOps for SocketFs {
18    fn statfs(
19        &self,
20        _locked: &mut Locked<FileOpsCore>,
21        _fs: &FileSystem,
22        _current_task: &CurrentTask,
23    ) -> Result<statfs, Errno> {
24        Ok(default_statfs(SOCKFS_MAGIC))
25    }
26    fn name(&self) -> &'static FsStr {
27        "sockfs".into()
28    }
29}
30
31/// Returns a handle to the `SocketFs` instance in `kernel`, initializing it if needed.
32pub fn socket_fs(
33    locked: &mut Locked<impl LockEqualOrBefore<FileOpsCore>>,
34    kernel: &Kernel,
35) -> FileSystemHandle {
36    struct SocketFsHandle(FileSystemHandle);
37
38    kernel
39        .expando
40        .get_or_init(|| {
41            SocketFsHandle(
42                FileSystem::new(
43                    locked,
44                    kernel,
45                    CacheMode::Uncached,
46                    SocketFs,
47                    FileSystemOptions::default(),
48                )
49                .expect("socketfs constructed with valid options"),
50            )
51        })
52        .0
53        .clone()
54}