Przeglądaj źródła

refactor: use InodeId type

liujingx 11 miesięcy temu
rodzic
commit
881d18bd9f

+ 1 - 1
src/constants.rs

@@ -36,7 +36,7 @@ pub const NAME_MAX: usize = 255;
 pub const SYMLINKS_MAX: usize = 40;
 
 /// The inode number of root inode
-pub const EXT4_ROOT_INO: u32 = 2;
+pub const EXT4_ROOT_INO: InodeId = 2;
 
 pub const BASE_OFFSET: usize = 1024;
 pub const BLOCK_SIZE: usize = 4096;

+ 3 - 3
src/ext4/alloc.rs

@@ -117,7 +117,7 @@ impl Ext4 {
     }
 
     /// Allocate a new inode in the filesystem, returning its number.
-    fn do_alloc_inode(&mut self, is_dir: bool) -> Result<u32> {
+    fn do_alloc_inode(&mut self, is_dir: bool) -> Result<InodeId> {
         let mut bgid = 0;
         let bg_count = self.super_block.block_groups_count();
 
@@ -181,9 +181,9 @@ impl Ext4 {
 
             // Compute the absolute i-node number
             let inodes_per_group = self.super_block.inodes_per_group();
-            let inode_num = bgid * inodes_per_group + (idx_in_bg + 1);
+            let inode_id = bgid * inodes_per_group + (idx_in_bg + 1);
 
-            return Ok(inode_num);
+            return Ok(inode_id);
         }
         log::info!("no free inode");
         Err(Ext4Error::new(ErrCode::ENOSPC))

+ 1 - 1
src/ext4/mod.rs

@@ -41,7 +41,7 @@ impl Ext4 {
 
     /// Read an inode from block device, return an`Ext4InodeRef` that combines
     /// the inode and its id.
-    fn get_inode_ref(&self, inode_id: u32) -> Ext4InodeRef {
+    fn get_inode_ref(&self, inode_id: InodeId) -> Ext4InodeRef {
         Ext4InodeRef::read_from_disk(self.block_device.clone(), &self.super_block, inode_id)
     }
 

+ 4 - 4
src/ext4_defs/dir_entry.rs

@@ -38,7 +38,7 @@ impl Default for Ext4DirEnInner {
 #[repr(C)]
 #[derive(Debug)]
 pub struct Ext4DirEntry {
-    inode: u32,            // 该目录项指向的inode的编号
+    inode: InodeId,        // 该目录项指向的inode的编号
     rec_len: u16,          // 到下一个目录项的距离
     name_len: u8,          // 低8位的文件名长度
     inner: Ext4DirEnInner, // 联合体成员
@@ -59,7 +59,7 @@ impl Default for Ext4DirEntry {
 
 impl Ext4DirEntry {
     /// Create a new directory entry
-    pub fn new(inode: u32, rec_len: u16, name: &str, dirent_type: FileType) -> Self {
+    pub fn new(inode: InodeId, rec_len: u16, name: &str, dirent_type: FileType) -> Self {
         let mut name_bytes = [0u8; 255];
         let name_len = name.as_bytes().len();
         name_bytes[..name_len].copy_from_slice(name.as_bytes());
@@ -103,11 +103,11 @@ impl Ext4DirEntry {
         self.rec_len = len;
     }
 
-    pub fn inode(&self) -> u32 {
+    pub fn inode(&self) -> InodeId {
         self.inode
     }
 
-    pub fn set_inode(&mut self, inode: u32) {
+    pub fn set_inode(&mut self, inode: InodeId) {
         self.inode = inode;
     }
 

+ 1 - 1
src/ext4_defs/file.rs

@@ -6,7 +6,7 @@ pub struct Ext4File {
     /// 挂载点句柄
     pub mp: *mut Ext4MountPoint,
     /// 文件 inode id
-    pub inode: u32,
+    pub inode: InodeId,
     /// 打开标志
     pub flags: u32,
     /// 文件大小

+ 44 - 78
src/ext4_defs/inode.rs

@@ -151,12 +151,7 @@ impl Ext4Inode {
         self.i_extra_isize = extra_isize;
     }
 
-    pub fn set_inode_checksum_value(
-        &mut self,
-        super_block: &Ext4Superblock,
-        _inode_id: u32,
-        checksum: u32,
-    ) {
+    pub fn set_inode_checksum_value(&mut self, super_block: &Ext4Superblock, checksum: u32) {
         let inode_size = super_block.inode_size();
 
         self.osd2.l_i_checksum_lo = ((checksum << 16) >> 16) as u16;
@@ -173,42 +168,6 @@ impl Ext4Inode {
         blocks
     }
 
-    /// Find the position of an inode in the block device.
-    ///
-    /// Each block group contains `sb.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`.
-    fn inode_disk_pos(
-        super_block: &Ext4Superblock,
-        block_device: Arc<dyn BlockDevice>,
-        inode_id: u32,
-    ) -> usize {
-        let inodes_per_group = super_block.inodes_per_group();
-        let inode_size = super_block.inode_size();
-        let group = (inode_id - 1) / inodes_per_group;
-        let index = (inode_id - 1) % inodes_per_group;
-
-        let bg = Ext4BlockGroupDesc::load(block_device, super_block, group as usize).unwrap();
-        bg.inode_table_first_block() as usize * BLOCK_SIZE + (index * inode_size as u32) as usize
-    }
-
-    fn read_from_disk(
-        super_block: &Ext4Superblock,
-        block_device: Arc<dyn BlockDevice>,
-        inode_id: u32,
-    ) -> Self {
-        let pos = Ext4Inode::inode_disk_pos(super_block, block_device.clone(), inode_id);
-        let data = block_device.read_offset(pos);
-        let inode_data = &data[..core::mem::size_of::<Ext4Inode>()];
-        Ext4Inode::from_bytes(inode_data)
-    }
-
     fn copy_to_byte_slice(&self, slice: &mut [u8]) {
         unsafe {
             let inode_ptr = self as *const Ext4Inode as *const u8;
@@ -241,7 +200,7 @@ impl Ext4Inode {
         // inode checksum
         checksum = ext4_crc32c(checksum, &raw_data, inode_size as u32);
 
-        self.set_inode_checksum_value(super_block, inode_id, checksum);
+        self.set_inode_checksum_value(super_block, checksum);
 
         if inode_size == 128 {
             checksum &= 0xFFFF;
@@ -260,31 +219,6 @@ impl Ext4Inode {
         }
     }
 
-    fn sync_to_disk_without_csum(
-        &self,
-        block_device: Arc<dyn BlockDevice>,
-        super_block: &Ext4Superblock,
-        inode_id: u32,
-    ) -> Result<()> {
-        let disk_pos = Self::inode_disk_pos(super_block, block_device.clone(), inode_id);
-        let data = unsafe {
-            core::slice::from_raw_parts(self as *const _ as *const u8, size_of::<Ext4Inode>())
-        };
-        block_device.write_offset(disk_pos, data);
-
-        Ok(())
-    }
-
-    fn sync_to_disk_with_csum(
-        &mut self,
-        block_device: Arc<dyn BlockDevice>,
-        super_block: &Ext4Superblock,
-        inode_id: u32,
-    ) -> Result<()> {
-        self.set_checksum(super_block, inode_id);
-        self.sync_to_disk_without_csum(block_device, super_block, inode_id)
-    }
-
     /* Extent methods */
 
     /// Get the immutable extent root node
@@ -318,24 +252,52 @@ impl Ext4Inode {
 /// A combination of an `Ext4Inode` and its id
 #[derive(Default, Clone)]
 pub struct Ext4InodeRef {
-    pub inode_id: u32,
+    pub inode_id: InodeId,
     pub inode: Ext4Inode,
 }
 
 impl Ext4InodeRef {
-    pub fn new(inode_id: u32, inode: Ext4Inode) -> Self {
+    pub fn new(inode_id: InodeId, inode: Ext4Inode) -> Self {
         Self { inode_id, inode }
     }
 
     pub fn read_from_disk(
         block_device: Arc<dyn BlockDevice>,
         super_block: &Ext4Superblock,
-        inode_id: u32,
+        inode_id: InodeId,
     ) -> Self {
-        Self::new(
+        let pos = Self::inode_disk_pos(super_block, block_device.clone(), inode_id);
+        let data = block_device.read_offset(pos);
+        let inode_data = &data[..core::mem::size_of::<Ext4Inode>()];
+        Self {
             inode_id,
-            Ext4Inode::read_from_disk(super_block, block_device, inode_id),
-        )
+            inode: Ext4Inode::from_bytes(inode_data),
+        }
+    }
+
+    /// Find the position of an inode in the block device.
+    ///
+    /// Each block group contains `sb.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`.
+    fn inode_disk_pos(
+        super_block: &Ext4Superblock,
+        block_device: Arc<dyn BlockDevice>,
+        inode_id: InodeId,
+    ) -> usize {
+        let inodes_per_group = super_block.inodes_per_group();
+        let inode_size = super_block.inode_size();
+        let group = (inode_id - 1) / inodes_per_group;
+        let index = (inode_id - 1) % inodes_per_group;
+
+        let bg = Ext4BlockGroupDesc::load(block_device, super_block, group as usize).unwrap();
+        bg.inode_table_first_block() as usize * BLOCK_SIZE + (index * inode_size as u32) as usize
     }
 
     pub fn sync_to_disk_without_csum(
@@ -343,8 +305,12 @@ impl Ext4InodeRef {
         block_device: Arc<dyn BlockDevice>,
         super_block: &Ext4Superblock,
     ) -> Result<()> {
-        self.inode
-            .sync_to_disk_without_csum(block_device, super_block, self.inode_id)
+        let disk_pos = Self::inode_disk_pos(super_block, block_device.clone(), self.inode_id);
+        let data = unsafe {
+            core::slice::from_raw_parts(self as *const _ as *const u8, size_of::<Ext4Inode>())
+        };
+        block_device.write_offset(disk_pos, data);
+        Ok(())
     }
 
     pub fn sync_to_disk_with_csum(
@@ -352,7 +318,7 @@ impl Ext4InodeRef {
         block_device: Arc<dyn BlockDevice>,
         super_block: &Ext4Superblock,
     ) -> Result<()> {
-        self.inode
-            .sync_to_disk_with_csum(block_device, super_block, self.inode_id)
+        self.inode.set_checksum(super_block, self.inode_id);
+        self.sync_to_disk_without_csum(block_device, super_block)
     }
 }

+ 2 - 1
src/prelude.rs

@@ -29,4 +29,5 @@ pub(crate) use crate::ext4_error::*;
 pub(crate) type Result<T> = core::result::Result<T, Ext4Error>;
 
 pub(crate) type LBlockId = u32;
-pub(crate) type PBlockId = u64;
+pub(crate) type PBlockId = u64;
+pub(crate) type InodeId = u32;