Forráskód Böngészése

refactor: remove unused methods in ext4_defs

liujingx 11 hónapja
szülő
commit
7c5213b110

+ 1 - 1
src/ext4/alloc.rs

@@ -23,7 +23,7 @@ impl Ext4 {
             let mut bg =
                 Ext4BlockGroupDesc::load(block_device.clone(), &super_block, bgid as usize).unwrap();
 
-            let mut free_inodes = bg.get_free_inodes_count();
+            let mut free_inodes = bg.free_inodes_count();
             let mut used_dirs = bg.get_used_dirs_count(&super_block);
 
             if free_inodes > 0 {

+ 5 - 5
src/ext4/dir.rs

@@ -102,7 +102,7 @@ impl Ext4 {
         let el = BLOCK_SIZE - size_of::<Ext4DirEntryTail>();
         self.ext4_dir_write_entry(&mut new_entry, el as u16, &child, path, len);
 
-        copy_dir_entry_to_array(&new_entry, &mut ext4_block.block_data, 0);
+        new_entry.copy_to_byte_slice(&mut ext4_block.block_data, 0);
 
         // init tail
         let ptr = ext4_block.block_data.as_mut_ptr();
@@ -118,7 +118,7 @@ impl Ext4 {
         tail.ext4_dir_set_csum(&self.super_block, &new_entry, &ext4_block.block_data[..]);
 
         let tail_offset = BLOCK_SIZE - size_of::<Ext4DirEntryTail>();
-        copy_diren_tail_to_array(&tail, &mut ext4_block.block_data, tail_offset);
+        tail.copy_to_byte_slice(&mut ext4_block.block_data, tail_offset);
 
         tail.ext4_dir_set_csum(&self.super_block, &new_entry, &ext4_block.block_data[..]);
 
@@ -178,8 +178,8 @@ impl Ext4 {
                     );
 
                     // update parent new_de to blk_data
-                    copy_dir_entry_to_array(&de, &mut dst_blk.block_data, offset);
-                    copy_dir_entry_to_array(&new_entry, &mut dst_blk.block_data, offset + sz);
+                    de.copy_to_byte_slice(&mut dst_blk.block_data, offset);
+                    new_entry.copy_to_byte_slice(&mut dst_blk.block_data, offset + sz);
 
                     // set tail csum
                     let mut tail =
@@ -191,7 +191,7 @@ impl Ext4 {
                     tail.ext4_dir_set_csum(&self.super_block, &parent_de, &dst_blk.block_data[..]);
 
                     let tail_offset = BLOCK_SIZE - size_of::<Ext4DirEntryTail>();
-                    copy_diren_tail_to_array(&tail, &mut dst_blk.block_data, tail_offset);
+                    tail.copy_to_byte_slice(&mut dst_blk.block_data, tail_offset);
 
                     // sync to disk
                     dst_blk.sync_blk_to_disk(block_device.clone());

+ 25 - 28
src/ext4/mod.rs

@@ -2,12 +2,12 @@ use crate::constants::*;
 use crate::ext4_defs::*;
 use crate::prelude::*;
 
-mod dir;
-mod file;
 mod alloc;
-mod utils;
+mod dir;
 mod extent;
+mod file;
 mod link;
+mod utils;
 
 #[derive(Debug)]
 pub struct Ext4 {
@@ -23,44 +23,41 @@ pub struct Ext4 {
 
 impl Ext4 {
     /// Opens and loads an Ext4 from the `block_device`.
-    pub fn new(block_device: Arc<dyn BlockDevice>) -> Arc<Self> {
+    ///
+    /// | Super Block | Group Descriptor | Reserved GDT Blocks |
+    /// | Block Bitmap | Inode Bitmap | Inode Table | Data Blocks |
+    pub fn load(block_device: Arc<dyn BlockDevice>) -> Self {
         // Load the superblock
         // TODO: if the main superblock is corrupted, should we load the backup?
         let raw_data = block_device.read_offset(BASE_OFFSET);
         let super_block = Ext4Superblock::try_from(raw_data).unwrap();
-
-        // log::info!("super_block: {:x?}", super_block);
         let inodes_per_group = super_block.inodes_per_group();
         let blocks_per_group = super_block.blocks_per_group();
-        let inode_size = super_block.inode_size();
-
-        // Load the block groups information
-        let load_block_groups =
-            |_fs: Weak<Ext4>, block_device: Arc<dyn BlockDevice>| -> Result<Vec<Ext4BlockGroupDesc>> {
-                let block_groups_count = super_block.block_groups_count() as usize;
-                let mut block_groups = Vec::with_capacity(block_groups_count);
-                for idx in 0..block_groups_count {
-                    let block_group =
-                        Ext4BlockGroupDesc::load(block_device.clone(), &super_block, idx).unwrap();
-                    block_groups.push(block_group);
-                }
-                Ok(block_groups)
-            };
-
+        let inode_size = super_block.inode_size() as usize;
+
+        // Load the block groups description
+        let block_groups_count = super_block.block_groups_count() as usize;
+        let mut block_groups = Vec::with_capacity(block_groups_count);
+        for idx in 0..block_groups_count {
+            let block_group =
+                Ext4BlockGroupDesc::load(block_device.clone(), &super_block, idx).unwrap();
+            block_groups.push(block_group);
+        }
+
+        // Root mount point
         let mount_point = Ext4MountPoint::new("/");
 
-        let ext4: Arc<Ext4> = Arc::new_cyclic(|weak_ref| Self {
+        // Create Ext4 instance
+        Self {
             super_block,
             inodes_per_group,
             blocks_per_group,
-            inode_size: inode_size as usize,
-            block_groups: load_block_groups(weak_ref.clone(), block_device.clone()).unwrap(),
+            inode_size,
+            block_groups,
             block_device,
             mount_point,
             last_inode_bg_id: 0,
-        });
-
-        ext4
+        }
     }
 
     // start transaction
@@ -77,7 +74,7 @@ impl Ext4 {
         let group = (inode_id - 1) / inodes_per_group;
         let index = (inode_id - 1) % inodes_per_group;
         let group = self.block_groups[group as usize];
-        let inode_table_blk_num = group.get_inode_table_blk_num();
+        let inode_table_blk_num = group.inode_table_blk_num();
         let offset =
             inode_table_blk_num as usize * BLOCK_SIZE + index as usize * inode_size as usize;
 

+ 19 - 16
src/ext4_defs/block_group.rs

@@ -1,17 +1,17 @@
 //!# The Defination of Ext4 Block Group Description
-//! 
+//!
 //! Block Group Descriptor is the second field of Ext4 Block Group.
-//! 
-//! | Super Block | Group Descriptor | Reserved GDT Blocks | Block Bitmap |
-//! | Inode Bitmap | Inode Table | Data Blocks |
-//! 
+//!
+//! | Super Block | Group Descriptor | Reserved GDT Blocks |
+//! | Block Bitmap | Inode Bitmap | Inode Table | Data Blocks |
+//!
 //! See [`super`] for more information.
 
-use crate::constants::*;
-use crate::prelude::*;
 use super::crc::*;
 use super::BlockDevice;
 use super::Ext4Superblock;
+use crate::constants::*;
+use crate::prelude::*;
 
 #[derive(Debug, Default, Clone, Copy)]
 #[repr(C, packed)]
@@ -136,15 +136,15 @@ impl Ext4BlockGroupDesc {
         self.inode_table_first_block_lo
     }
 
-    pub fn get_free_inodes_count(&self) -> u32 {
-        ((self.free_inodes_count_hi as u64) << 32) as u32 | self.free_inodes_count_lo as u32
+    pub fn free_inodes_count(&self) -> u32 {
+        ((self.free_inodes_count_hi as u32) << 16) | self.free_inodes_count_lo as u32
     }
 
-    pub fn get_inode_table_blk_num(&self) -> u32 {
-        ((self.inode_table_first_block_hi as u64) << 32) as u32 | self.inode_table_first_block_lo
+    pub fn inode_table_blk_num(&self) -> u64 {
+        ((self.inode_table_first_block_hi as u64) << 32) | self.inode_table_first_block_lo as u64
     }
 
-    pub fn sync_block_group_to_disk(
+    pub fn sync_to_disk(
         &self,
         block_device: Arc<dyn BlockDevice>,
         bgid: usize,
@@ -159,12 +159,15 @@ impl Ext4BlockGroupDesc {
         let offset = (bgid % dsc_cnt) * super_block.desc_size() as usize;
 
         let data = unsafe {
-            core::slice::from_raw_parts(self as *const _ as *const u8, size_of::<Ext4BlockGroupDesc>())
+            core::slice::from_raw_parts(
+                self as *const _ as *const u8,
+                size_of::<Ext4BlockGroupDesc>(),
+            )
         };
         block_device.write_offset(block_id * BLOCK_SIZE + offset, data);
     }
 
-    pub fn get_block_group_checksum(&mut self, bgid: u32, super_block: &Ext4Superblock) -> u16 {
+    pub fn calc_checksum(&mut self, bgid: u32, super_block: &Ext4Superblock) -> u16 {
         let desc_size = super_block.desc_size();
 
         let orig_checksum = self.checksum;
@@ -197,7 +200,7 @@ impl Ext4BlockGroupDesc {
     }
 
     pub fn set_block_group_checksum(&mut self, bgid: u32, super_block: &Ext4Superblock) {
-        let csum = self.get_block_group_checksum(bgid, super_block);
+        let csum = self.calc_checksum(bgid, super_block);
         self.checksum = csum;
     }
 
@@ -208,7 +211,7 @@ impl Ext4BlockGroupDesc {
         super_block: &Ext4Superblock,
     ) {
         self.set_block_group_checksum(bgid as u32, super_block);
-        self.sync_block_group_to_disk(block_device, bgid, super_block)
+        self.sync_to_disk(block_device, bgid, super_block)
     }
 
     pub fn set_block_group_ialloc_bitmap_csum(&mut self, s: &Ext4Superblock, bitmap: &[u8]) {

+ 25 - 31
src/ext4_defs/dir_entry.rs

@@ -1,3 +1,8 @@
+//! # The Defination of Ext4 Directory Entry
+//!
+//! A directory is a series of data blocks and that each block contains a
+//! linear array of directory entries.
+
 use super::crc::*;
 use super::BlockDevice;
 use super::Ext4Block;
@@ -62,19 +67,18 @@ impl<T> TryFrom<&[T]> for Ext4DirEntry {
 }
 
 impl Ext4DirEntry {
-    pub fn get_name(&self) -> String {
+    pub fn name(&self) -> String {
         let name_len = self.name_len as usize;
         let name = &self.name[..name_len];
         let name = core::str::from_utf8(name).unwrap();
         name.to_string()
     }
 
-    pub fn get_name_len(&self) -> usize {
-        let name_len = self.name_len as usize;
-        name_len
+    pub fn name_len(&self) -> usize {
+        self.name_len as usize
     }
 
-    pub fn ext4_dir_get_csum(&self, s: &Ext4Superblock, blk_data: &[u8]) -> u32 {
+    pub fn calc_csum(&self, s: &Ext4Superblock, blk_data: &[u8]) -> u32 {
         let ino_index = self.inode;
         let ino_gen = 0 as u32;
 
@@ -91,7 +95,7 @@ impl Ext4DirEntry {
         csum
     }
 
-    pub fn write_de_to_blk(&self, dst_blk: &mut Ext4Block, offset: usize) {
+    pub fn write_to_blk(&self, dst_blk: &mut Ext4Block, offset: usize) {
         let count = core::mem::size_of::<Ext4DirEntry>() / core::mem::size_of::<u8>();
         let data = unsafe { core::slice::from_raw_parts(self as *const _ as *const u8, count) };
         dst_blk.block_data.splice(
@@ -100,23 +104,14 @@ impl Ext4DirEntry {
         );
         // assert_eq!(dst_blk.block_data[offset..offset + core::mem::size_of::<Ext4DirEntry>()], data[..]);
     }
-}
-
-pub fn copy_dir_entry_to_array(header: &Ext4DirEntry, array: &mut [u8], offset: usize) {
-    unsafe {
-        let de_ptr = header as *const Ext4DirEntry as *const u8;
-        let array_ptr = array as *mut [u8] as *mut u8;
-        let count = core::mem::size_of::<Ext4DirEntry>() / core::mem::size_of::<u8>();
-        core::ptr::copy_nonoverlapping(de_ptr, array_ptr.add(offset), count);
-    }
-}
 
-pub fn copy_diren_tail_to_array(dir_en: &Ext4DirEntryTail, array: &mut [u8], offset: usize) {
-    unsafe {
-        let de_ptr = dir_en as *const Ext4DirEntryTail as *const u8;
-        let array_ptr = array as *mut [u8] as *mut u8;
-        let count = core::mem::size_of::<Ext4DirEntryTail>();
-        core::ptr::copy_nonoverlapping(de_ptr, array_ptr.add(offset), count);
+    pub fn copy_to_byte_slice(&self, slice: &mut [u8], offset: usize) {
+        let de_ptr = self as *const Ext4DirEntry as *const u8;
+        let slice_ptr = slice as *mut [u8] as *mut u8;
+        let count = core::mem::size_of::<Ext4DirEntry>();
+        unsafe {
+            core::ptr::copy_nonoverlapping(de_ptr, slice_ptr.add(offset), count);
+        }
     }
 }
 
@@ -153,8 +148,7 @@ impl Ext4DirEntryTail {
     }
 
     pub fn ext4_dir_set_csum(&mut self, s: &Ext4Superblock, diren: &Ext4DirEntry, blk_data: &[u8]) {
-        let csum = diren.ext4_dir_get_csum(s, blk_data);
-        self.checksum = csum;
+        self.checksum = diren.calc_csum(s, blk_data);
     }
 
     #[allow(unused)]
@@ -197,14 +191,14 @@ impl Ext4DirEntryTail {
             &dst_blk.block_data,
         );
     }
-}
 
-#[allow(unused)]
-pub fn copy_diren_to_array(diren: &Ext4DirEntry, array: &mut [u8]) {
-    unsafe {
-        let diren_ptr = diren as *const Ext4DirEntry as *const u8;
-        let array_ptr = array as *mut [u8] as *mut u8;
-        core::ptr::copy_nonoverlapping(diren_ptr, array_ptr, core::mem::size_of::<Ext4DirEntry>());
+    pub fn copy_to_byte_slice(&self, slice: &mut [u8], offset: usize) {
+        let de_ptr = self as *const Ext4DirEntryTail as *const u8;
+        let slice_ptr = slice as *mut [u8] as *mut u8;
+        let count = core::mem::size_of::<Ext4DirEntryTail>();
+        unsafe {
+            core::ptr::copy_nonoverlapping(de_ptr, slice_ptr.add(offset), count);
+        }
     }
 }
 

+ 22 - 28
src/ext4_defs/extent.rs

@@ -1,3 +1,18 @@
+//! The Defination of Ext4 Extent (Header, Index)
+//! 
+//! Extents are arranged as a tree. Each node of the tree begins with a struct
+//! [`Ext4ExtentHeader`]. 
+//! 
+//! If the node is an interior node (eh.depth > 0), the header is followed by
+//! eh.entries_count instances of struct [`Ext4ExtentIndex`]; each of these index 
+//! entries points to a block containing more nodes in the extent tree. 
+//! 
+//! If the node is a leaf node (eh.depth == 0), then the header is followed by
+//! eh.entries_count instances of struct [`Ext4Extent`]; these instances point 
+//! to the file's data blocks. The root node of the extent tree is stored in 
+//! inode.i_block, which allows for the first four extents to be recorded without 
+//! the use of extra metadata blocks.
+
 use super::Ext4Inode;
 use crate::constants::*;
 use crate::prelude::*;
@@ -36,17 +51,17 @@ impl<T> TryFrom<&[T]> for Ext4ExtentHeader {
 
 impl Ext4ExtentHeader {
     // 获取extent header的魔数
-    pub fn get_magic(&self) -> u16 {
+    pub fn magic(&self) -> u16 {
         self.magic
     }
 
     // 设置extent header的魔数
-    pub fn set_magic(&mut self, magic: u16) {
-        self.magic = magic;
+    pub fn set_magic(&mut self) {
+        self.magic = EXT4_EXTENT_MAGIC;
     }
 
     // 获取extent header的条目数
-    pub fn get_entries_count(&self) -> u16 {
+    pub fn entries_count(&self) -> u16 {
         self.entries_count
     }
 
@@ -56,7 +71,7 @@ impl Ext4ExtentHeader {
     }
 
     // 获取extent header的最大条目数
-    pub fn get_max_entries_count(&self) -> u16 {
+    pub fn max_entries_count(&self) -> u16 {
         self.max_entries_count
     }
 
@@ -66,7 +81,7 @@ impl Ext4ExtentHeader {
     }
 
     // 获取extent header的深度
-    pub fn get_depth(&self) -> u16 {
+    pub fn depth(&self) -> u16 {
         self.depth
     }
 
@@ -76,7 +91,7 @@ impl Ext4ExtentHeader {
     }
 
     // 获取extent header的生成号
-    pub fn get_generation(&self) -> u32 {
+    pub fn generation(&self) -> u32 {
         self.generation
     }
 
@@ -84,27 +99,6 @@ impl Ext4ExtentHeader {
     pub fn set_generation(&mut self, generation: u32) {
         self.generation = generation;
     }
-
-    pub fn ext4_extent_header_depth(&self) -> u16 {
-        self.depth
-    }
-
-    pub fn ext4_extent_header_set_depth(&mut self, depth: u16) {
-        self.depth = depth;
-    }
-    pub fn ext4_extent_header_set_entries_count(&mut self, entries_count: u16) {
-        self.entries_count = entries_count;
-    }
-    pub fn ext4_extent_header_set_generation(&mut self, generation: u32) {
-        self.generation = generation;
-    }
-    pub fn ext4_extent_header_set_magic(&mut self) {
-        self.magic = EXT4_EXTENT_MAGIC;
-    }
-
-    pub fn ext4_extent_header_set_max_entries_count(&mut self, max_entries_count: u16) {
-        self.max_entries_count = max_entries_count;
-    }
 }
 
 #[derive(Debug, Default, Clone, Copy)]

+ 22 - 20
src/ext4_defs/inode.rs

@@ -1,10 +1,11 @@
 //! # The Defination of Ext4 Inode Table Entry
 //!
-//! The inode table is a linear array of struct ext4_inode. The table is sized to have
-//! enough blocks to store at least sb.s_inode_size * sb.s_inodes_per_group bytes.
+//! The inode table is a linear array of struct `Ext4Inode`. The table is sized to have
+//! enough blocks to store at least `sb.inode_size * sb.inodes_per_group` bytes.
+//!
 //! The number of the block group containing an inode can be calculated as
-//! (inode_number - 1) / sb.s_inodes_per_group, and the offset into the group's table is
-//! (inode_number - 1) % sb.s_inodes_per_group. There is no inode 0.
+//! `(inode_number - 1) / sb.inodes_per_group`, and the offset into the group's table is
+//! `(inode_number - 1) % sb.inodes_per_group`. There is no inode 0.
 
 use super::crc::*;
 use super::BlockDevice;
@@ -156,11 +157,11 @@ impl Ext4Inode {
 
     pub fn extent_tree_init(&mut self) {
         let mut header = Ext4ExtentHeader::default();
-        header.ext4_extent_header_set_depth(0);
-        header.ext4_extent_header_set_entries_count(0);
-        header.ext4_extent_header_set_generation(0);
-        header.ext4_extent_header_set_magic();
-        header.ext4_extent_header_set_max_entries_count(4 as u16);
+        header.set_depth(0);
+        header.set_entries_count(0);
+        header.set_generation(0);
+        header.set_magic();
+        header.set_max_entries_count(4 as u16);
 
         unsafe {
             let header_ptr = &header as *const Ext4ExtentHeader as *const u32;
@@ -177,11 +178,17 @@ impl Ext4Inode {
         blocks
     }
 
-    // pub fn ext4_inode_set_blocks_count(&mut self, inode_blocks: u64){
-    //     self.blocks = inode_blocks as u32;
-    //     self.osd2.l_i_blocks_high = (inode_blocks >> 32) as u16;
-    // }
-
+    /// Find the position of an inode in the block device.
+    ///
+    /// Each block group contains sb->s_inodes_per_group inodes.
+    /// Because inode 0 is defined not to exist, this formula can
+    /// be used to find the block group that an inode lives in:
+    /// `bg = (inode_id - 1) / sb.inodes_per_group`.
+    ///
+    /// The particular inode can be found within the block group's
+    /// inode table at `index = (inode_id - 1) % sb.inodes_per_group`.
+    /// To get the byte address within the inode table, use
+    /// `offset = index * sb.inode_size`.
     pub fn get_inode_disk_pos(
         &self,
         super_block: &Ext4Superblock,
@@ -194,12 +201,7 @@ impl Ext4Inode {
         let index = (inode_id - 1) % inodes_per_group;
 
         let bg = Ext4BlockGroupDesc::load(block_device, super_block, group as usize).unwrap();
-
-        let inode_table_blk_num = ((bg.inode_table_first_block_hi() as u64) << 32)
-            | bg.inode_table_first_block_lo() as u64;
-        let offset =
-            inode_table_blk_num as usize * BLOCK_SIZE + (index * inode_size as u32) as usize;
-        offset
+        bg.inode_table_blk_num() as usize * BLOCK_SIZE + (index * inode_size as u32) as usize
     }
 
     fn copy_to_byte_slice(&self, slice: &mut [u8]) {

+ 2 - 4
src/ext4_defs/mount_point.rs

@@ -3,11 +3,10 @@ use crate::prelude::*;
 /// Mount point descriptor
 #[derive(Clone)]
 pub struct Ext4MountPoint {
-    /**@brief   Mount done flag.*/
+    /// Mount done flag
     pub mounted: bool,
-    /**@brief   Mount point name (@ref ext4_mount)*/
+    ///  Mount point name
     pub mount_name: String,
-    // pub mount_name_string: String,
 }
 
 impl Ext4MountPoint {
@@ -15,7 +14,6 @@ impl Ext4MountPoint {
         Self {
             mounted: false,
             mount_name: name.to_owned(),
-            // mount_name_string: name.to_string(),
         }
     }
 }

+ 1 - 0
src/ext4_defs/super_block.rs

@@ -154,6 +154,7 @@ impl Ext4Superblock {
     }
     
     /// Returns the number of block groups.
+    /// FIXME: This function is not correct.
     pub fn block_groups_count(&self) -> u32 {
         (((self.blocks_count_hi.to_le() as u64) << 32) as u32 | self.blocks_count_lo)
         / self.blocks_per_group