浏览代码

fix: extent node split

liujingx 11 月之前
父节点
当前提交
d4222ab1c1
共有 3 个文件被更改,包括 17 次插入16 次删除
  1. 3 5
      src/ext4/alloc.rs
  2. 0 3
      src/ext4/dir.rs
  3. 14 8
      src/ext4_defs/extent.rs

+ 3 - 5
src/ext4/alloc.rs

@@ -5,10 +5,7 @@ use crate::prelude::*;
 
 impl Ext4 {
     /// Allocate a new data block for an inode, return the physical block number
-    pub(super) fn alloc_block(
-        &mut self,
-        inode_ref: &mut InodeRef,
-    ) -> Result<PBlockId> {
+    pub(super) fn alloc_block(&mut self, inode_ref: &mut InodeRef) -> Result<PBlockId> {
         // Calc block group id
         let inodes_per_group = self.super_block.inodes_per_group();
         let bgid = ((inode_ref.id - 1) / inodes_per_group) as BlockGroupId;
@@ -144,7 +141,8 @@ impl Ext4 {
 
             // Modify filesystem counters
             let free_inodes = bg.desc.free_inodes_count() - 1;
-            bg.desc.set_free_inodes_count(&self.super_block, free_inodes);
+            bg.desc
+                .set_free_inodes_count(&self.super_block, free_inodes);
 
             // Increment used directories counter
             if is_dir {

+ 0 - 3
src/ext4/dir.rs

@@ -92,7 +92,6 @@ impl Ext4 {
     /// Insert a directory entry of a child inode into a new parent block.
     /// A new block must have enough space
     fn insert_entry_to_new_block(&self, dst_blk: &mut Block, child: &InodeRef, name: &str) {
-        debug!("Dir insert entry to new block {}", dst_blk.block_id);
         // Set the entry
         let rec_len = BLOCK_SIZE - size_of::<DirEntryTail>();
         let new_entry = DirEntry::new(
@@ -126,8 +125,6 @@ impl Ext4 {
         child: &InodeRef,
         name: &str,
     ) -> Result<()> {
-        debug!("Dir insert entry to old block {}", dst_blk.block_id);
-
         let required_size = DirEntry::required_size(name.len());
         let mut offset = 0;
 

+ 14 - 8
src/ext4_defs/extent.rs

@@ -289,9 +289,8 @@ impl<'a> ExtentNode<'a> {
     /// the given logical block number. Return `Err(index)` if not found, and `index` is the
     /// position where the new extent should be inserted.
     pub fn search_extent(&self, lblock: LBlockId) -> core::result::Result<usize, usize> {
-        let mut i = 0;
         debug!("Search extent: {}", lblock);
-        self.print();
+        let mut i = 0;
         while i < self.header().entries_count as usize {
             let extent = self.extent_at(i);
             if extent.start_lblock() <= lblock {
@@ -317,9 +316,8 @@ impl<'a> ExtentNode<'a> {
     /// Return `Err(index)` if not found, and `index` is the position where the new extent index
     /// should be inserted.
     pub fn search_extent_index(&self, lblock: LBlockId) -> core::result::Result<usize, usize> {
-        let mut i = 0;
         debug!("Search extent index: {}", lblock);
-        self.print();
+        let mut i = 0;
         while i < self.header().entries_count as usize {
             let extent_index = self.extent_index_at(i);
             if extent_index.start_lblock() > lblock {
@@ -500,16 +498,20 @@ impl<'a> ExtentNodeMut<'a> {
         // Split the node, return the extents in the right half
         let mut split = Vec::new();
         let mid = self.header().entries_count() as usize / 2;
+        // If `pos` is on the right side, insert it to `split`
         for i in mid..self.header().entries_count() as usize {
             if i == pos {
-                // The extent to insert
                 split.push((*extent).into());
             }
             split.push(*self.fake_extent_at(i));
         }
+        if pos == self.header().entries_count() as usize {
+            split.push((*extent).into());
+        }
+        // Update header
         self.header_mut().entries_count = mid as u16;
+        // If `pos` is on the left side, insert it
         if pos < mid {
-            // If `pos` is on the left side, insert it
             self.insert_extent(extent, pos).expect("Must Succeed");
         }
         // Return the right half
@@ -544,16 +546,20 @@ impl<'a> ExtentNodeMut<'a> {
         // Split the node, return the extent indexs in the right half
         let mut split = Vec::<FakeExtent>::new();
         let mid = self.header().entries_count() as usize / 2;
+        // If `pos` is on the right side, insert it to `split`
         for i in mid..self.header().entries_count() as usize {
             if i == pos {
-                // The extent index to insert
                 split.push((*extent_index).into());
             }
             split.push(*self.fake_extent_at(i));
         }
+        if pos == self.header().entries_count() as usize {
+            split.push((*extent_index).into());
+        }
+        // Update header
         self.header_mut().entries_count = mid as u16;
+        // If `pos` is on the left side, insert it
         if pos < mid {
-            // If `pos` is on the left side, insert it
             self.insert_extent_index(extent_index, pos)
                 .expect("Must Succeed");
         }