summaryrefslogtreecommitdiff
path: root/src/disk
diff options
context:
space:
mode:
authorChuyan Zhang <me@zcy.moe>2023-11-18 02:15:11 -0800
committerChuyan Zhang <me@zcy.moe>2023-11-18 02:15:11 -0800
commitcd0163da154367f5437ae1423bc97c450d74adf7 (patch)
treec8f88dae8da14f9c2614170e48c5eedd279459f5 /src/disk
parent95d8d84eef645b52d92fd3fb8fdea7aed1f6d474 (diff)
downloadmyfs-cd0163da154367f5437ae1423bc97c450d74adf7.tar.gz
myfs-cd0163da154367f5437ae1423bc97c450d74adf7.zip
I hate cache!
Diffstat (limited to 'src/disk')
-rw-r--r--src/disk/bitmap.rs28
-rw-r--r--src/disk/data_block.rs89
-rw-r--r--src/disk/inode.rs4
3 files changed, 98 insertions, 23 deletions
diff --git a/src/disk/bitmap.rs b/src/disk/bitmap.rs
index d5a8fe9..64389c2 100644
--- a/src/disk/bitmap.rs
+++ b/src/disk/bitmap.rs
@@ -1,4 +1,5 @@
use crate::block_device::{BlockDevice, BLOCK_SIZE};
+use std::cell::RefCell;
use std::sync::Arc;
pub struct Bitmap {
@@ -6,7 +7,6 @@ pub struct Bitmap {
pub length: usize,
pub device: Arc<dyn BlockDevice>,
pub data: Vec<u8>,
- pub dirty_blocks: Vec<usize>,
}
impl Bitmap {
@@ -16,15 +16,14 @@ impl Bitmap {
length,
device,
data: vec![0u8; length * BLOCK_SIZE],
- dirty_blocks: Vec::new(),
}
}
pub fn allocate(&mut self) -> Option<usize> {
+ // let mut data = self.data.borrow_mut();
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.dirty_blocks.push(i / BLOCK_SIZE);
return Some(i * 8 + leading_ones as usize);
}
}
@@ -33,6 +32,7 @@ impl Bitmap {
}
pub fn query(&self, index: usize) -> bool {
+ // let data = self.data.borrow();
if index == 0 {
false
} else {
@@ -40,15 +40,15 @@ impl Bitmap {
}
}
- fn write_back(&mut self) {
- for block_index_offset in self.dirty_blocks.iter() {
- let buffer_front_index = BLOCK_SIZE * block_index_offset;
- let buffer_back_index = BLOCK_SIZE * (block_index_offset + 1);
- self.device.write(
- self.starting_block + block_index_offset,
- &self.data[buffer_front_index..buffer_back_index],
- );
- }
- self.dirty_blocks = Vec::new();
- }
+ // fn write_back(&mut self) {
+ // for block_index_offset in self.dirty_blocks.iter() {
+ // let buffer_front_index = BLOCK_SIZE * block_index_offset;
+ // let buffer_back_index = BLOCK_SIZE * (block_index_offset + 1);
+ // self.device.write(
+ // self.starting_block + block_index_offset,
+ // &self.data[buffer_front_index..buffer_back_index],
+ // );
+ // }
+ // self.dirty_blocks = Vec::new();
+ // }
}
diff --git a/src/disk/data_block.rs b/src/disk/data_block.rs
index 2160b91..9c8cc26 100644
--- a/src/disk/data_block.rs
+++ b/src/disk/data_block.rs
@@ -1,12 +1,54 @@
-pub trait Block: Default {}
+use crate::disk::inode::Inode;
+use libc::pathconf;
+
+pub trait Block: Default + Clone {}
+
+#[derive(Clone)]
+pub struct DataBlock(pub(crate) [u8; 4096]);
+
+impl Default for DataBlock {
+ fn default() -> Self {
+ Self([0; 4096])
+ }
+}
-#[derive(Default)]
-pub struct DataBlock([u8; 4096]);
impl Block for DataBlock {}
+#[derive(Clone)]
+pub struct InodeBlock {
+ pub(crate) inodes: [Inode; 16],
+}
+
+impl Default for InodeBlock {
+ fn default() -> Self {
+ Self {
+ inodes: [
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ Inode::empty(),
+ ],
+ }
+ }
+}
+
+impl Block for InodeBlock {}
+
const FULL_MAP: u32 = 0b111_111_111_111_111;
-#[derive(Default)]
+#[derive(Clone)]
pub struct DirectoryBlock {
entries: [[u8; 256]; 15],
inode_ids: [usize; 15],
@@ -14,6 +56,17 @@ pub struct DirectoryBlock {
reserved: [u8; 132],
}
+impl Default for DirectoryBlock {
+ fn default() -> Self {
+ Self {
+ entries: [[0; 256]; 15],
+ inode_ids: [0; 15],
+ occupancy_map: 0,
+ reserved: [0xFF; 132],
+ }
+ }
+}
+
impl Block for DirectoryBlock {}
impl DirectoryBlock {
@@ -42,11 +95,17 @@ impl DirectoryBlock {
}
}
-#[derive(Default)]
+#[derive(Clone)]
pub struct IndirectBlock {
pub entries: [u32; 1024],
}
+impl Default for IndirectBlock {
+ fn default() -> Self {
+ Self { entries: [0; 1024] }
+ }
+}
+
impl Block for IndirectBlock {}
impl IndirectBlock {
@@ -59,11 +118,19 @@ impl IndirectBlock {
}
}
-#[derive(Default)]
+#[derive(Clone)]
pub struct DoubleIndirectBlock {
pub indirect: [u32; 1024],
}
+impl Default for DoubleIndirectBlock {
+ fn default() -> Self {
+ Self {
+ indirect: [0; 1024],
+ }
+ }
+}
+
impl Block for DoubleIndirectBlock {}
impl DoubleIndirectBlock {
@@ -76,11 +143,19 @@ impl DoubleIndirectBlock {
}
}
-#[derive(Default)]
+#[derive(Clone)]
pub struct TripleIndirectBlock {
pub double_indirect: [u32; 1024],
}
+impl Default for TripleIndirectBlock {
+ fn default() -> Self {
+ Self {
+ double_indirect: [0; 1024],
+ }
+ }
+}
+
impl Block for TripleIndirectBlock {}
impl TripleIndirectBlock {
diff --git a/src/disk/inode.rs b/src/disk/inode.rs
index 6adc75d..48085d8 100644
--- a/src/disk/inode.rs
+++ b/src/disk/inode.rs
@@ -2,7 +2,7 @@ use bitflags::bitflags;
const DIRECT_NUMBER: usize = 15;
-#[derive(Debug)]
+#[derive(Debug, Clone, Copy)]
pub struct InodeMode(u16);
bitflags! {
@@ -37,7 +37,7 @@ bitflags! {
/// - added more direct blocks for a total size of 128 bytes
/// TODO: do we need to extend time precision?
#[repr(C)]
-#[derive(Debug)]
+#[derive(Debug, Clone)]
pub struct Inode {
mode: InodeMode,
uid: u32,