From 4c34414b26bf71e747ea3ecb2586645bab4aba52 Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Fri, 1 Dec 2023 19:42:13 -0800 Subject: Multiple bugfix, it works! --- ayafs-core/src/disk/allocation.rs | 14 +++++++++++-- ayafs-core/src/disk/bitmap.rs | 40 ++++++++++++++++++++++++++++++++++- ayafs-core/src/disk/block.rs | 44 +++++++++++++++++++++------------------ ayafs-core/src/disk/inode.rs | 17 +++------------ 4 files changed, 78 insertions(+), 37 deletions(-) (limited to 'ayafs-core/src/disk') diff --git a/ayafs-core/src/disk/allocation.rs b/ayafs-core/src/disk/allocation.rs index a187fad..6b7167a 100644 --- a/ayafs-core/src/disk/allocation.rs +++ b/ayafs-core/src/disk/allocation.rs @@ -255,6 +255,7 @@ impl AyaFS { Ok(()) } + #[allow(unused)] /// 从 inode 中删去最后一个 block pub(crate) fn deallocate_block_for(&mut self, inode: &mut Inode) -> Option { // 如果 triple indirect 块存在, 则尝试从中销毁一个块 @@ -300,6 +301,7 @@ impl AyaFS { None } + #[allow(unused)] fn deallocate_from_triple_indirect(&mut self, triple_indirect_entry: u32) -> Option { let triple_indirect_entry = triple_indirect_entry as usize; if let Some(triple_indirect_block) = self @@ -339,6 +341,7 @@ impl AyaFS { None } + #[allow(unused)] fn deallocate_from_double_indirect(&mut self, double_indirect_entry: u32) -> Option { let double_indirect_entry = double_indirect_entry as usize; if let Some(double_indirect_block) = self @@ -373,6 +376,7 @@ impl AyaFS { None } + #[allow(unused)] fn deallocate_from_indirect(&mut self, indirect_entry: u32) -> Option { let indirect_entry = indirect_entry as usize; if let Some(indirect_block) = self @@ -521,6 +525,11 @@ impl AyaFS { ) -> Option<&CachedBlock> { self.get_block_index(inode, block_index_within_inode) .map(|block_index| { + debug!( + "access_block(index: {}) found with global index {}", + block_index_within_inode, + block_index, + ); self.get_block::(block_index).unwrap() // 可以 unwrap 吧这里 ?? }) } @@ -533,8 +542,9 @@ impl AyaFS { self.get_block_index(inode, block_index_within_inode) .map(|block_index| { debug!( - "access_block_mut(index: {}) found", - block_index_within_inode + "access_block_mut(index: {}) found with global index {}", + block_index_within_inode, + block_index, ); self.get_block_mut::(block_index).unwrap() // 可以 unwrap 吧这里 ?? }) diff --git a/ayafs-core/src/disk/bitmap.rs b/ayafs-core/src/disk/bitmap.rs index b68c341..cf7eae5 100644 --- a/ayafs-core/src/disk/bitmap.rs +++ b/ayafs-core/src/disk/bitmap.rs @@ -6,22 +6,59 @@ pub struct Bitmap { pub length: usize, pub device: Arc, pub data: Vec, + pub count: u64, } impl Bitmap { - pub(crate) fn new(starting_block: usize, length: usize, device: Arc) -> Self { + pub(crate) fn new(starting_block: usize, length: usize, device: Arc, count: u64) -> Self { Self { starting_block, length, device, data: vec![0u8; length * BLOCK_SIZE], + count, } } + + pub(crate) fn load(starting_block: usize, length: usize, device: Arc, count: u64) -> Self { + let mut data = vec![0u8; length * BLOCK_SIZE]; + for id in 0 .. length { + let block_id = starting_block + id; + device.read(block_id, &mut data[id * BLOCK_SIZE .. (id + 1) * BLOCK_SIZE]); + } + Self { + starting_block, + length, + device, + data, + count, + } + } + + pub(crate) fn write_back(&self) { + for id in 0 .. self.length { + let block_id = self.starting_block + id; + self.device.write(block_id, &self.data[id * BLOCK_SIZE .. (id + 1) * BLOCK_SIZE]); + } + } + + // Allocate, but doesn't modify self. + pub(crate) fn peek(&self) -> Option { + for (i, byte) in self.data.iter().enumerate() { + let leading_ones = byte.leading_ones(); + if leading_ones != 8 { + return Some(i * 8 + leading_ones as usize); + } + } + None + } + pub(crate) fn allocate(&mut self) -> Option { for (i, byte) in self.data.iter_mut().enumerate() { let leading_ones = byte.leading_ones(); if leading_ones != 8 { *byte |= (1 << (7 - leading_ones)) as u8; + self.count += 1; return Some(i * 8 + leading_ones as usize); } } @@ -39,6 +76,7 @@ impl Bitmap { pub(crate) fn deallocate(&mut self, index: usize) -> bool { if self.query(index) { let mask = !(1u8 << (7 - index % 8)); + self.count -= 1; self.data[index / 8] &= mask; true } else { diff --git a/ayafs-core/src/disk/block.rs b/ayafs-core/src/disk/block.rs index 76769b9..e48385d 100644 --- a/ayafs-core/src/disk/block.rs +++ b/ayafs-core/src/disk/block.rs @@ -10,7 +10,10 @@ pub struct SuperBlock { pub(crate) inode_bitmap_block_number: u64, pub(crate) inode_block_number: u64, pub(crate) data_block_number: u64, - padding: [u8; 4064], + pub(crate) total_block_number: u64, + pub(crate) used_inode_number: u64, + pub(crate) used_block_number: u64, + padding: [u8; 4040], } impl SuperBlock { @@ -19,13 +22,19 @@ impl SuperBlock { inode_bitmap_block_number: usize, inode_block_number: usize, data_block_number: usize, + total_block_number: usize, + used_inode_number: u64, + used_block_number: u64, ) -> Self { Self { data_bitmap_block_number: data_bitmap_block_number as u64, inode_bitmap_block_number: inode_bitmap_block_number as u64, inode_block_number: inode_block_number as u64, data_block_number: data_block_number as u64, - padding: [0; 4064], + total_block_number: total_block_number as u64, + used_inode_number, + used_block_number, + padding: [0; 4040], } } } @@ -155,11 +164,6 @@ impl Default for DirectoryBlock { } impl DirectoryBlock { - #[allow(unused)] - pub(crate) fn is_full(&self) -> bool { - self.occupancy[0] == 0xFF && self.occupancy[1] == 0xFF - } - pub(crate) fn query(&self, mut index: usize) -> bool { if index < 7 { // 0-6, first u8 @@ -196,20 +200,12 @@ impl DirectoryBlock { } } - // pub(crate) fn allocate(&mut self) -> Option { - // if self.occupancy[0] != 0xFF { - // let leading_ones = self.occupancy[0].leading_ones(); - // self.occupancy[0] |= (1 << (7 - leading_ones)) as u8; - // Some(leading_ones as usize) - // } else if self.occupancy[1] != 0xFF { - // let leading_ones = self.occupancy[1].leading_ones(); - // self.occupancy[1] |= (1 << (7 - leading_ones)) as u8; - // Some(7 + leading_ones as usize) - // } else { - // None - // } - // } + pub(crate) fn reset(&mut self) { + self.occupancy[0] = 0x80; + self.occupancy[1] = 0x00; + } + #[allow(unused)] pub(crate) fn deallocate(&mut self, mut index: usize) { if index < 7 { index = index + 1; @@ -269,3 +265,11 @@ impl Default for TripleIndirectBlock { } impl Block for TripleIndirectBlock {} + +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); +const_assert_eq!(std::mem::size_of::(), 4096); \ No newline at end of file diff --git a/ayafs-core/src/disk/inode.rs b/ayafs-core/src/disk/inode.rs index d94b795..eccebd4 100644 --- a/ayafs-core/src/disk/inode.rs +++ b/ayafs-core/src/disk/inode.rs @@ -3,7 +3,8 @@ use crate::utils; use bitflags::bitflags; use fuser::FileType; -pub const DIRECT_NUMBER: usize = 15; +pub(crate) const DIRECT_NUMBER: usize = 15; +pub(crate) const INODE_PER_BLOCK: usize = BLOCK_SIZE / INODE_SIZE; #[derive(Debug, Clone, Copy)] pub struct InodeMode(pub u16); @@ -128,7 +129,7 @@ impl From for u8 { pub struct Inode { pub mode: InodeMode, pub uid: u32, - pub size: u32, + pub size: u64, pub atime: u32, // access time, in seconds pub ctime: u32, // change time, in seconds pub mtime: u32, // modify time, in seconds @@ -141,7 +142,6 @@ pub struct Inode { pub single_indirect: u32, pub double_indirect: u32, pub triple_indirect: u32, - pub generation: u32, pub file_acl: u32, pub dir_acl: u32, // TODO do we have to implement ACL......? } @@ -153,7 +153,6 @@ impl Inode { gid: u32, time: u32, flags: u32, - generation: u32, file_acl: u32, dir_acl: u32, ) -> Self { @@ -173,7 +172,6 @@ impl Inode { single_indirect: 0, double_indirect: 0, triple_indirect: 0, - generation, file_acl, dir_acl, } @@ -187,7 +185,6 @@ impl Inode { gid: u32, time: u32, flags: u32, - generation: u32, file_acl: u32, dir_acl: u32, ) -> Self { @@ -197,7 +194,6 @@ impl Inode { gid, time, flags, - generation, file_acl, dir_acl, ) @@ -209,7 +205,6 @@ impl Inode { gid: u32, time: u32, flags: u32, - generation: u32, file_acl: u32, dir_acl: u32, ) -> Self { @@ -219,7 +214,6 @@ impl Inode { gid, time, flags, - generation, file_acl, dir_acl, ) @@ -231,7 +225,6 @@ impl Inode { gid: u32, time: u32, flags: u32, - generation: u32, file_acl: u32, dir_acl: u32, ) -> Self { @@ -241,7 +234,6 @@ impl Inode { gid, time, flags, - generation, file_acl, dir_acl, ) @@ -253,7 +245,6 @@ impl Inode { gid: u32, time: u32, flags: u32, - generation: u32, file_acl: u32, dir_acl: u32, ) -> Self { @@ -263,7 +254,6 @@ impl Inode { gid, time, flags, - generation, file_acl, dir_acl, ) @@ -302,7 +292,6 @@ impl Inode { single_indirect: 0, double_indirect: 0, triple_indirect: 0, - generation: 0, file_acl: 0, dir_acl: 0, } -- cgit v1.2.3-70-g09d2