1
0
mirror of https://github.com/sgmarz/osblog.git synced 2024-11-24 02:16:19 +04:00

Use global cache. fs::init will now check to see if the global cache is good or not. If it is already good, do nothing, otherwise cache the inodes.

This commit is contained in:
Stephen Marz 2020-05-15 12:29:59 -04:00
parent 8e414c62be
commit 3dd49c615e
2 changed files with 32 additions and 16 deletions

View File

@ -68,9 +68,17 @@ pub struct DirEntry {
} }
/// The MinixFileSystem implements the FileSystem trait for the VFS. /// The MinixFileSystem implements the FileSystem trait for the VFS.
pub struct MinixFileSystem { pub struct MinixFileSystem;
inode_cache: BTreeMap<String, Inode> static mut MFS_INODE_CACHE: [Option<BTreeMap<String, Inode>>; 8] = [
} None,
None,
None,
None,
None,
None,
None,
None
];
impl MinixFileSystem { impl MinixFileSystem {
/// Inodes are the meta-data of a file, including the mode (permissions and type) and /// Inodes are the meta-data of a file, including the mode (permissions and type) and
@ -165,24 +173,32 @@ impl MinixFileSystem {
} }
} }
// Run this ONLY in a process! // Run this ONLY in a process!
pub fn init(bdev: usize) -> Self { pub fn init(bdev: usize) {
let mut btm = BTreeMap::new(); if unsafe {MFS_INODE_CACHE[bdev-1].is_none() } {
let cwd = String::from("/"); let mut btm = BTreeMap::new();
let cwd = String::from("/");
// Let's look at the root (inode #1) // Let's look at the root (inode #1)
Self::cache_at(&mut btm, &cwd, 1, bdev); Self::cache_at(&mut btm, &cwd, 1, bdev);
unsafe {
Self { MFS_INODE_CACHE[bdev-1] = Some(btm);
inode_cache: btm }
} }
} }
/// The goal of open is to traverse the path given by path. If we cache the inodes /// The goal of open is to traverse the path given by path. If we cache the inodes
/// in RAM, it might make this much quicker. For now, this doesn't do anything since /// in RAM, it might make this much quicker. For now, this doesn't do anything since
/// we're just testing read based on if we know the Inode we're looking for. /// we're just testing read based on if we know the Inode we're looking for.
pub fn open(&self, path: &String) -> Result<Inode, FsError> { pub fn open(bdev: usize, path: &String) -> Result<Inode, FsError> {
if let Some(inode) = self.inode_cache.get(path) { if let Some(cache) = unsafe { MFS_INODE_CACHE[bdev-1].take() } {
Ok(*inode) let ret;
if let Some(inode) = cache.get(path) {
ret = Ok(*inode);
}
else {
ret = Err(FsError::FileNotFound);
}
ret
} }
else { else {
Err(FsError::FileNotFound) Err(FsError::FileNotFound)

View File

@ -12,9 +12,9 @@ use alloc::string::String;
pub fn test_elf() { pub fn test_elf() {
// This won't be necessary after we connect this to the VFS, but for now, we need it. // This won't be necessary after we connect this to the VFS, but for now, we need it.
const BDEV: usize = 8; const BDEV: usize = 8;
let mfs = MinixFileSystem::init(BDEV); MinixFileSystem::init(BDEV);
let file_to_read = String::from("/helloworld.elf"); let file_to_read = String::from("/helloworld.elf");
let desc = mfs.open(&file_to_read).ok(); let desc = MinixFileSystem::open(BDEV, &file_to_read).ok();
if desc.is_none() { if desc.is_none() {
println!("Error reading {}", file_to_read); println!("Error reading {}", file_to_read);
return; return;