Rm spin::Mutex except for easy-fs & add new test huge_write & flush cache to disk after a write transaction

This commit is contained in:
Yifan Wu 2021-07-21 19:10:04 +08:00
parent 569e2fe2fe
commit b8a14182cd
36 changed files with 339 additions and 229 deletions

View file

@ -17,7 +17,7 @@ pub struct BlockCache {
impl BlockCache {
/// Load a new BlockCache from disk.
pub fn new(
block_id: usize,
block_id: usize,
block_device: Arc<dyn BlockDevice>
) -> Self {
let mut cache = [0u8; BLOCK_SZ];
@ -125,4 +125,11 @@ pub fn get_block_cache(
block_device: Arc<dyn BlockDevice>
) -> Arc<Mutex<BlockCache>> {
BLOCK_CACHE_MANAGER.lock().get_block_cache(block_id, block_device)
}
}
pub fn block_cache_sync_all() {
let manager = BLOCK_CACHE_MANAGER.lock();
for (_, cache) in manager.queue.iter() {
cache.lock().sync();
}
}

View file

@ -8,6 +8,7 @@ use super::{
DiskInodeType,
Inode,
get_block_cache,
block_cache_sync_all,
};
use crate::BLOCK_SZ;
@ -50,7 +51,7 @@ impl EasyFileSystem {
// clear all blocks
for i in 0..total_blocks {
get_block_cache(
i as usize,
i as usize,
Arc::clone(&block_device)
)
.lock()
@ -82,6 +83,7 @@ impl EasyFileSystem {
.modify(root_inode_offset, |disk_inode: &mut DiskInode| {
disk_inode.initialize(DiskInodeType::Directory);
});
block_cache_sync_all();
Arc::new(Mutex::new(efs))
}
@ -107,7 +109,7 @@ impl EasyFileSystem {
data_area_start_block: 1 + inode_total_blocks + super_block.data_bitmap_blocks,
};
Arc::new(Mutex::new(efs))
})
})
}
pub fn root_inode(efs: &Arc<Mutex<Self>>) -> Inode {

View file

@ -231,7 +231,7 @@ impl DiskInode {
b0 = 0;
a0 += 1;
}
}
}
});
}
@ -416,7 +416,7 @@ impl DirEntry {
}
pub fn new(name: &str, inode_number: u32) -> Self {
let mut bytes = [0u8; NAME_LENGTH_LIMIT + 1];
&mut bytes[..name.len()].copy_from_slice(name.as_bytes());
bytes[..name.len()].copy_from_slice(name.as_bytes());
Self {
name: bytes,
inode_number,

View file

@ -15,4 +15,4 @@ pub use efs::EasyFileSystem;
pub use vfs::Inode;
use layout::*;
use bitmap::Bitmap;
use block_cache::get_block_cache;
use block_cache::{get_block_cache, block_cache_sync_all};

View file

@ -6,6 +6,7 @@ use super::{
EasyFileSystem,
DIRENT_SZ,
get_block_cache,
block_cache_sync_all,
};
use alloc::sync::Arc;
use alloc::string::String;
@ -145,6 +146,7 @@ impl Inode {
});
let (block_id, block_offset) = fs.get_disk_inode_pos(new_inode_id);
block_cache_sync_all();
// return inode
Some(Arc::new(Self::new(
block_id,
@ -185,10 +187,12 @@ impl Inode {
pub fn write_at(&self, offset: usize, buf: &[u8]) -> usize {
let mut fs = self.fs.lock();
self.modify_disk_inode(|disk_inode| {
let size = self.modify_disk_inode(|disk_inode| {
self.increase_size((offset + buf.len()) as u32, disk_inode, &mut fs);
disk_inode.write_at(offset, buf, &self.block_device)
})
});
block_cache_sync_all();
size
}
pub fn clear(&self) {
@ -201,5 +205,6 @@ impl Inode {
fs.dealloc_data(data_block);
}
});
block_cache_sync_all();
}
}