Add comments in ch6

This commit is contained in:
hypocrasy 2022-04-30 16:04:58 +08:00
parent cea2febe35
commit f9346edad1
35 changed files with 374 additions and 116 deletions

View file

@ -6,7 +6,7 @@ use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use spin::{Mutex, MutexGuard};
/// Virtual filesystem layer over easy-fs
pub struct Inode {
block_id: usize,
block_offset: usize,
@ -15,7 +15,7 @@ pub struct Inode {
}
impl Inode {
/// We should not acquire efs lock here.
/// Create a vfs inode
pub fn new(
block_id: u32,
block_offset: usize,
@ -29,19 +29,19 @@ impl Inode {
block_device,
}
}
/// Call a function over a disk inode to read it
fn read_disk_inode<V>(&self, f: impl FnOnce(&DiskInode) -> V) -> V {
get_block_cache(self.block_id, Arc::clone(&self.block_device))
.lock()
.read(self.block_offset, f)
}
/// Call a function over a disk inode to modify it
fn modify_disk_inode<V>(&self, f: impl FnOnce(&mut DiskInode) -> V) -> V {
get_block_cache(self.block_id, Arc::clone(&self.block_device))
.lock()
.modify(self.block_offset, f)
}
/// Find inode under a disk inode by name
fn find_inode_id(&self, name: &str, disk_inode: &DiskInode) -> Option<u32> {
// assert it is a directory
assert!(disk_inode.is_dir());
@ -58,7 +58,7 @@ impl Inode {
}
None
}
/// Find inode under current inode by name
pub fn find(&self, name: &str) -> Option<Arc<Inode>> {
let fs = self.fs.lock();
self.read_disk_inode(|disk_inode| {
@ -73,7 +73,7 @@ impl Inode {
})
})
}
/// Increase the size of a disk inode
fn increase_size(
&self,
new_size: u32,
@ -90,7 +90,7 @@ impl Inode {
}
disk_inode.increase_size(new_size, v, &self.block_device);
}
/// Create inode under current inode by name
pub fn create(&self, name: &str) -> Option<Arc<Inode>> {
let mut fs = self.fs.lock();
let op = |root_inode: &DiskInode| {
@ -138,7 +138,7 @@ impl Inode {
)))
// release efs lock automatically by compiler
}
/// List inodes under current inode
pub fn ls(&self) -> Vec<String> {
let _fs = self.fs.lock();
self.read_disk_inode(|disk_inode| {
@ -155,12 +155,12 @@ impl Inode {
v
})
}
/// Read data from current inode
pub fn read_at(&self, offset: usize, buf: &mut [u8]) -> usize {
let _fs = self.fs.lock();
self.read_disk_inode(|disk_inode| disk_inode.read_at(offset, buf, &self.block_device))
}
/// Write data to current inode
pub fn write_at(&self, offset: usize, buf: &[u8]) -> usize {
let mut fs = self.fs.lock();
let size = self.modify_disk_inode(|disk_inode| {
@ -170,7 +170,7 @@ impl Inode {
block_cache_sync_all();
size
}
/// Clear the data in current inode
pub fn clear(&self) {
let mut fs = self.fs.lock();
self.modify_disk_inode(|disk_inode| {