1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::error::CNFSResult;
use crate::sync::UPCell;
use crate::vfs::dentry::remove_dcache;
use crate::vfs::fs::FileSystem;
use crate::vfs::lookup_dentry;
use crate::vfs::path::Path;
use crate::CNFSError::{AlreadyMountedPath, NoMountedFilesystem};
use alloc::collections::BTreeMap;
use alloc::sync::Arc;
use lazy_static::lazy_static;

pub struct Mount
{
    pub(crate) fs: Arc<dyn FileSystem>
}

lazy_static! {
    pub static ref MNTPOINT_TABLE: UPCell<BTreeMap<Path, Mount>> = unsafe{UPCell::new(BTreeMap::<Path, Mount>::new())};
}

/// Mount a filesystem at the given path.
pub fn mount(fs: Arc<dyn FileSystem>, mnt_point: Path) -> CNFSResult
{
    if mnt_point.to_string() != "/" {
        let dentry = lookup_dentry(&mnt_point)?;
        remove_dcache(&dentry.path);
    }
    let mut table = MNTPOINT_TABLE.exclusive_access();
    let already_mounted = table.get(&mnt_point);
    if already_mounted.is_some() { return Err(AlreadyMountedPath); }
    table.insert(mnt_point.clone(), Mount { fs });
    Ok(())
}
/// Mount the filesystem at the given path.
pub fn umount(mnt_point: Path) -> CNFSResult
{
    let mut table = MNTPOINT_TABLE.exclusive_access();
    let already_mounted = table.get(&mnt_point);
    if already_mounted.is_none() { return Err(NoMountedFilesystem); }
    table.remove(&mnt_point);
    Ok(())
}