Ver código fonte

refactor: update data types to use Box for memory management. should use kernel inside cache for
improvement (but not. waiting for integration)

Samuka007 1 mês atrás
pai
commit
28bb8731fc
6 arquivos alterados com 32 adições e 30 exclusões
  1. 2 2
      Cargo.toml
  2. 11 9
      src/ext4/alloc.rs
  3. 10 10
      src/ext4/extent.rs
  4. 3 3
      src/ext4/rw.rs
  5. 4 4
      src/ext4_defs/block.rs
  6. 2 2
      src/ext4_defs/inode.rs

+ 2 - 2
Cargo.toml

@@ -6,8 +6,8 @@ edition = "2021"
 [dependencies]
 bitflags = "2.2.1"
 log = "0.4"
-axsync = { git = "https://github.com/Starry-OS/axsync.git", optional = true }
+# axsync = { git = "https://github.com/Starry-OS/axsync.git", optional = true }
 
 [features]
-block_cache = ["dep:axsync"]
+# block_cache = ["dep:axsync"]
 fuser_root_inode = []

+ 11 - 9
src/ext4/alloc.rs

@@ -7,13 +7,14 @@ use crate::return_error;
 
 impl Ext4 {
     /// Create a new inode, returning the inode and its number
+    #[inline(never)]
     pub(super) fn create_inode(&self, mode: InodeMode) -> Result<InodeRef> {
         // Allocate an inode
         let is_dir = mode.file_type() == FileType::Directory;
         let id = self.alloc_inode(is_dir)?;
 
         // Initialize the inode
-        let mut inode = Inode::default();
+        let mut inode = Box::new(Inode::default());
         inode.set_mode(mode);
         inode.extent_init();
         let mut inode_ref = InodeRef::new(id, inode);
@@ -26,8 +27,9 @@ impl Ext4 {
     }
 
     /// Create(initialize) the root inode of the file system
+    #[inline(never)]
     pub(super) fn create_root_inode(&self) -> Result<InodeRef> {
-        let mut inode = Inode::default();
+        let mut inode = Box::new(Inode::default());
         inode.set_mode(InodeMode::from_type_and_perm(
             FileType::Directory,
             InodeMode::from_bits_retain(0o755),
@@ -54,7 +56,7 @@ impl Ext4 {
             // Deallocate the block
             self.dealloc_block(inode, pblock)?;
             // Clear the block content
-            self.write_block(&Block::new(pblock, [0; BLOCK_SIZE]));
+            self.write_block(&Block::new(pblock, Box::new([0; BLOCK_SIZE])));
         }
         // Free extent tree
         let pblocks = self.extent_all_tree_blocks(inode);
@@ -62,7 +64,7 @@ impl Ext4 {
             // Deallocate the block
             self.dealloc_block(inode, pblock)?;
             // Clear the block content
-            self.write_block(&Block::new(pblock, [0; BLOCK_SIZE]));
+            self.write_block(&Block::new(pblock, Box::new([0; BLOCK_SIZE])));
         }
         // Free xattr block
         let xattr_block = inode.inode.xattr_block();
@@ -70,7 +72,7 @@ impl Ext4 {
             // Deallocate the block
             self.dealloc_block(inode, xattr_block)?;
             // Clear the block content
-            self.write_block(&Block::new(xattr_block, [0; BLOCK_SIZE]));
+            self.write_block(&Block::new(xattr_block, Box::new([0; BLOCK_SIZE])));
         }
         // Deallocate the inode
         self.dealloc_inode(inode)?;
@@ -115,7 +117,7 @@ impl Ext4 {
         // Load block bitmap
         let bitmap_block_id = bg.desc.block_bitmap_block();
         let mut bitmap_block = self.read_block(bitmap_block_id);
-        let mut bitmap = Bitmap::new(&mut bitmap_block.data, 8 * BLOCK_SIZE);
+        let mut bitmap = Bitmap::new(&mut *bitmap_block.data, 8 * BLOCK_SIZE);
 
         // Find the first free block
         let fblock = bitmap
@@ -156,7 +158,7 @@ impl Ext4 {
         // Load block bitmap
         let bitmap_block_id = bg.desc.block_bitmap_block();
         let mut bitmap_block = self.read_block(bitmap_block_id);
-        let mut bitmap = Bitmap::new(&mut bitmap_block.data, 8 * BLOCK_SIZE);
+        let mut bitmap = Bitmap::new(&mut *bitmap_block.data, 8 * BLOCK_SIZE);
 
         // Free the block
         if bitmap.is_bit_clear(pblock as usize) {
@@ -198,7 +200,7 @@ impl Ext4 {
             let bitmap_block_id = bg.desc.inode_bitmap_block();
             let mut bitmap_block = self.read_block(bitmap_block_id);
             let inode_count = sb.inode_count_in_group(bgid) as usize;
-            let mut bitmap = Bitmap::new(&mut bitmap_block.data, inode_count);
+            let mut bitmap = Bitmap::new(&mut *bitmap_block.data, inode_count);
 
             // Find a free inode
             let idx_in_bg =
@@ -254,7 +256,7 @@ impl Ext4 {
         let bitmap_block_id = bg.desc.inode_bitmap_block();
         let mut bitmap_block = self.read_block(bitmap_block_id);
         let inode_count = sb.inode_count_in_group(bgid) as usize;
-        let mut bitmap = Bitmap::new(&mut bitmap_block.data, inode_count);
+        let mut bitmap = Bitmap::new(&mut *bitmap_block.data, inode_count);
 
         // Free the inode
         if bitmap.is_bit_clear(idx_in_bg as usize) {

+ 10 - 10
src/ext4/extent.rs

@@ -35,7 +35,7 @@ impl Ext4 {
                 // Load the extent node
                 block_data = self.read_block(leaf.pblock);
                 // Load the next extent header
-                ExtentNode::from_bytes(&block_data.data)
+                ExtentNode::from_bytes(&*block_data.data)
             } else {
                 // Root node
                 inode_ref.inode.extent_root()
@@ -67,7 +67,7 @@ impl Ext4 {
         let mut block_data: Block;
         let ex_node = if leaf.pblock != 0 {
             block_data = self.read_block(leaf.pblock);
-            ExtentNodeMut::from_bytes(&mut block_data.data)
+            ExtentNodeMut::from_bytes(&mut *block_data.data)
         } else {
             // Root node
             inode_ref.inode.extent_root_mut()
@@ -122,7 +122,7 @@ impl Ext4 {
             for i in 0..ex_node.header().entries_count() as usize {
                 let ex_idx = ex_node.extent_index_at(i);
                 let child_block = self.read_block(ex_idx.leaf());
-                let child_node = ExtentNode::from_bytes(&child_block.data);
+                let child_node = ExtentNode::from_bytes(&*child_block.data);
                 self.get_all_pblocks_recursive(&child_node, pblocks);
             }
         }
@@ -135,7 +135,7 @@ impl Ext4 {
                 let ex_idx = ex_node.extent_index_at(i);
                 pblocks.push(ex_idx.leaf());
                 let child_block = self.read_block(ex_idx.leaf());
-                let child_node = ExtentNode::from_bytes(&child_block.data);
+                let child_node = ExtentNode::from_bytes(&*child_block.data);
                 self.get_all_nodes_recursive(&child_node, pblocks);
             }
         }
@@ -159,7 +159,7 @@ impl Ext4 {
             // Note: block data cannot be released until the next assigment
             block_data = self.read_block(next);
             // Load the next extent header
-            ex_node = ExtentNode::from_bytes(&block_data.data);
+            ex_node = ExtentNode::from_bytes(&*block_data.data);
             pblock = next;
         }
         // Leaf
@@ -192,7 +192,7 @@ impl Ext4 {
         }
         // 2. Leaf is not root, load the leaf node
         let mut leaf_block = self.read_block(leaf.pblock);
-        let mut leaf_node = ExtentNodeMut::from_bytes(&mut leaf_block.data);
+        let mut leaf_node = ExtentNodeMut::from_bytes(&mut *leaf_block.data);
         // Insert the extent
         let res = leaf_node.insert_extent(new_ext, leaf.index.unwrap_err());
         self.write_block(&leaf_block);
@@ -234,7 +234,7 @@ impl Ext4 {
     ) -> core::result::Result<(), Vec<FakeExtent>> {
         let right_bid = self.alloc_block(inode_ref).unwrap();
         let mut right_block = self.read_block(right_bid);
-        let mut right_node = ExtentNodeMut::from_bytes(&mut right_block.data);
+        let mut right_node = ExtentNodeMut::from_bytes(&mut *right_block.data);
 
         // Insert the split half to right node
         right_node.init(0, 0);
@@ -259,7 +259,7 @@ impl Ext4 {
         } else {
             // Parent is not root
             let mut parent_block = self.read_block(parent_pblock);
-            let mut parent_node = ExtentNodeMut::from_bytes(&mut parent_block.data);
+            let mut parent_node = ExtentNodeMut::from_bytes(&mut *parent_block.data);
             parent_depth = parent_node.header().depth();
             res = parent_node.insert_extent_index(&extent_index, child_pos + 1);
             self.write_block(&parent_block);
@@ -287,8 +287,8 @@ impl Ext4 {
 
         // Load root, left, right nodes
         let mut root = inode_ref.inode.extent_root_mut();
-        let mut left = ExtentNodeMut::from_bytes(&mut l_block.data);
-        let mut right = ExtentNodeMut::from_bytes(&mut r_block.data);
+        let mut left = ExtentNodeMut::from_bytes(&mut *l_block.data);
+        let mut right = ExtentNodeMut::from_bytes(&mut *r_block.data);
 
         // Copy the left half to left node
         left.init(root.header().depth(), 0);

+ 3 - 3
src/ext4/rw.rs

@@ -37,7 +37,7 @@ impl Ext4 {
 
     /// Write super block to block device
     pub(super) fn write_super_block(&self, sb: &SuperBlock) {
-        let mut block = Block::new(0, [0; BLOCK_SIZE]);
+        let mut block = Block::new(0, Box::new([0; BLOCK_SIZE]));
         block.write_offset_as(BASE_OFFSET, sb);
         self.write_block(&block)
     }
@@ -48,7 +48,7 @@ impl Ext4 {
         let (block_id, offset) = self.inode_disk_pos(inode_id);
         let block = self.read_block(block_id);
         
-        InodeRef::new(inode_id, block.read_offset_as(offset))
+        InodeRef::new(inode_id, Box::new(block.read_offset_as(offset)))
     }
 
     /// Read the root inode from block device
@@ -68,7 +68,7 @@ impl Ext4 {
     pub(super) fn write_inode_without_csum(&self, inode_ref: &InodeRef) {
         let (block_id, offset) = self.inode_disk_pos(inode_ref.id);
         let mut block = self.read_block(block_id);
-        block.write_offset_as(offset, &inode_ref.inode);
+        block.write_offset_as(offset, &*inode_ref.inode);
         self.write_block(&block)
     }
 

+ 4 - 4
src/ext4_defs/block.rs

@@ -24,26 +24,26 @@ where
 }
 
 /// Common data block descriptor.
-#[derive(Debug, Clone, Copy)]
+#[derive(Debug, Clone)]
 pub struct Block {
     /// Physical block id
     pub id: PBlockId,
     /// Raw block data
-    pub data: [u8; BLOCK_SIZE],
+    pub data: Box<[u8; BLOCK_SIZE]>,
 }
 
 impl Default for Block {
     fn default() -> Self {
         Self {
             id: 0,
-            data: [0; BLOCK_SIZE],
+            data: Box::new([0; BLOCK_SIZE]),
         }
     }
 }
 
 impl Block {
     /// Create new block with given physical block id and data.
-    pub fn new(block_id: PBlockId, data: [u8; BLOCK_SIZE]) -> Self {
+    pub fn new(block_id: PBlockId, data: Box::<[u8; BLOCK_SIZE]>) -> Self {
         Self { id: block_id, data }
     }
 

+ 2 - 2
src/ext4_defs/inode.rs

@@ -359,11 +359,11 @@ impl Inode {
 #[derive(Clone, Debug)]
 pub struct InodeRef {
     pub id: InodeId,
-    pub inode: Inode,
+    pub inode: Box<Inode>,
 }
 
 impl InodeRef {
-    pub fn new(id: InodeId, inode: Inode) -> Self {
+    pub fn new(id: InodeId, inode: Box<Inode>) -> Self {
         Self { id, inode }
     }