Browse Source

refactor: remove useless codes

liujingx 11 tháng trước cách đây
mục cha
commit
207efa7968

+ 1 - 1
ext4_test/src/main.rs

@@ -57,7 +57,7 @@ fn make_ext4() {
 fn open_ext4() -> Ext4 {
     let file = BlockFile::new("ext4.img");
     println!("creating ext4");
-    Ext4::load(Arc::new(file))
+    Ext4::load(Arc::new(file)).expect("open ext4 failed")
 }
 
 fn mkdir_test(ext4: &mut Ext4) {

+ 0 - 36
src/constants.rs

@@ -38,41 +38,5 @@ pub const SYMLINKS_MAX: usize = 40;
 /// The inode number of root inode
 pub const EXT4_ROOT_INO: u32 = 2;
 
-pub const EOK: usize = 0;
-pub const EPERM: usize = 1; /* Operation not permitted */
-pub const ENOENT: usize = 2; /* No such file or directory */
-pub const ESRCH: usize = 3; /* No such process */
-pub const EINTR: usize = 4; /* Interrupted system call */
-pub const EIO: usize = 5; /* I/O error */
-pub const ENXIO: usize = 6; /* No such device or address */
-pub const E2BIG: usize = 7; /* Argument list too long */
-pub const ENOEXEC: usize = 8; /* Exec format error */
-pub const EBADF: usize = 9; /* Bad file number */
-pub const ECHILD: usize = 10; /* No child processes */
-pub const EAGAIN: usize = 11; /* Try again */
-pub const ENOMEM: usize = 12; /* Out of memory */
-pub const EACCES: usize = 13; /* Permission denied */
-pub const EFAULT: usize = 14; /* Bad address */
-pub const ENOTBLK: usize = 15; /* Block device required */
-pub const EBUSY: usize = 16; /* Device or resource busy */
-pub const EEXIST: usize = 17; /* File exists */
-pub const EXDEV: usize = 18; /* Cross-device link */
-pub const ENODEV: usize = 19; /* No such device */
-pub const ENOTDIR: usize = 20; /* Not a directory */
-pub const EISDIR: usize = 21; /* Is a directory */
-pub const EINVAL: usize = 22; /* Invalid argument */
-pub const ENFILE: usize = 23; /* File table overflow */
-pub const EMFILE: usize = 24; /* Too many open files */
-pub const ENOTTY: usize = 25; /* Not a typewriter */
-pub const ETXTBSY: usize = 26; /* Text file busy */
-pub const EFBIG: usize = 27; /* File too large */
-pub const ENOSPC: usize = 28; /* No space left on device */
-pub const ESPIPE: usize = 29; /* Illegal seek */
-pub const EROFS: usize = 30; /* Read-only file system */
-pub const EMLINK: usize = 31; /* Too many links */
-pub const EPIPE: usize = 32; /* Broken pipe */
-pub const EDOM: usize = 33; /* Math argument out of domain of func */
-pub const ERANGE: usize = 34; /* Math result not representable */
-
 pub const BASE_OFFSET: usize = 1024;
 pub const BLOCK_SIZE: usize = 4096;

+ 2 - 2
src/ext4/alloc.rs

@@ -47,7 +47,7 @@ impl Ext4 {
 
         bg.sync_to_disk_with_csum(self.block_device.clone(), bgid as usize, &self.super_block);
 
-        info!("Alloc block {}", fblock);
+        info!("Alloc block {} ok", fblock);
         Ok(fblock)
     }
 
@@ -112,7 +112,7 @@ impl Ext4 {
         // Sync the inode to disk
         self.write_back_inode_with_csum(&mut inode_ref);
 
-        info!("Alloc inode {}", inode_ref.inode_id);
+        info!("Alloc inode {} ok", inode_ref.inode_id);
         Ok(inode_ref)
     }
 

+ 6 - 12
src/ext4/dir.rs

@@ -6,7 +6,7 @@ use crate::prelude::*;
 impl Ext4 {
     /// Find a directory entry that matches a given name under a parent directory
     pub(super) fn dir_find_entry(&self, parent: &Ext4InodeRef, name: &str) -> Result<Ext4DirEntry> {
-        info!("dir find entry: {} under parent {}", name, parent.inode_id);
+        info!("Dir find entry: {} under parent {}", name, parent.inode_id);
         let inode_size: u32 = parent.inode.size;
         let total_blocks: u32 = inode_size / BLOCK_SIZE as u32;
         let mut iblock: LBlockId = 0;
@@ -54,7 +54,7 @@ impl Ext4 {
         path: &str,
     ) -> Result<()> {
         info!(
-            "Adding entry: parent {}, child {}, path {}",
+            "Dir add entry: parent {}, child {}, path {}",
             parent.inode_id, child.inode_id, path
         );
         let inode_size = parent.inode.size();
@@ -68,12 +68,9 @@ impl Ext4 {
             // Load the parent block from disk
             let mut data = self.block_device.read_offset(fblock as usize * BLOCK_SIZE);
             let mut ext4_block = Ext4Block {
-                logical_block_id: iblock,
-                disk_block_id: fblock,
+                pblock_id: fblock,
                 block_data: &mut data,
-                dirty: false,
             };
-            debug!("Insert dirent to old block {}", fblock);
             // Try inserting the entry to parent block
             self.insert_entry_to_old_block(&mut ext4_block, child, path)?;
             // Current block has no enough space
@@ -82,17 +79,14 @@ impl Ext4 {
 
         // No free block found - needed to allocate a new data block
         // Append a new data block
-        let (iblock, fblock) = self.inode_append_block(parent)?;
+        let (_, fblock) = self.inode_append_block(parent)?;
         // Load new block
         let block_device = self.block_device.clone();
         let mut data = block_device.read_offset(fblock as usize * BLOCK_SIZE);
         let mut new_block = Ext4Block {
-            logical_block_id: iblock,
-            disk_block_id: fblock,
+            pblock_id: fblock,
             block_data: &mut data,
-            dirty: false,
         };
-        debug!("Insert dirent to new block {}", fblock);
         // Write the entry to block
         self.insert_entry_to_new_block(&mut new_block, child, path)
     }
@@ -197,7 +191,7 @@ impl Ext4 {
             &self.get_root_inode_ref(),
         )
         .map(|_| {
-            info!("ext4_dir_mk: {}", path);
+            info!("ext4_dir_mk: {} ok", path);
         })
     }
 }

+ 1 - 2
src/ext4/file.rs

@@ -18,14 +18,13 @@ impl Ext4 {
         ftype: FileType,
         parent_inode: &Ext4InodeRef,
     ) -> Result<Ext4File> {
+        info!("generic open: {}", path);
         // Search from the given parent inode
         let mut parent = parent_inode.clone();
         let search_path = Self::split_path(path);
 
-        info!("generic open: {}", path);
         for (i, path) in search_path.iter().enumerate() {
             let res = self.dir_find_entry(&parent, path);
-            debug!("dir_find_entry: {:?}", res);
             match res {
                 Ok(entry) => {
                     parent = self.get_inode_ref(entry.inode());

+ 3 - 2
src/ext4/journal.rs

@@ -1,9 +1,10 @@
 use super::Ext4;
 
 impl Ext4 {
-    // start transaction
+    /// start transaction
     pub(super) fn ext4_trans_start(&self) {}
 
-    // stop transaction
+    /// stop transaction
+    #[allow(unused)]
     pub(super) fn ext4_trans_abort(&self) {}
 }

+ 5 - 10
src/ext4_defs/block.rs

@@ -5,20 +5,15 @@ use super::BlockDevice;
 #[derive(Debug)]
 // A single block descriptor
 pub struct Ext4Block<'a> {
-    pub logical_block_id: u32, // 逻辑块号
-
-    // disk block id
-    pub disk_block_id: u64,
-
-    // size BLOCK_SIZE
-    pub block_data: &'a mut Vec<u8>,
-
-    pub dirty: bool,
+    /// Physical block id
+    pub pblock_id: PBlockId,
+    /// Raw block data
+    pub block_data: &'a mut [u8],
 }
 
 impl<'a> Ext4Block<'a> {
     pub fn sync_to_disk(&self, block_device: Arc<dyn BlockDevice>) {
-        let block_id = self.disk_block_id as usize;
+        let block_id = self.pblock_id as usize;
         block_device.write_offset(block_id * BLOCK_SIZE, &self.block_data);
     }
 }

+ 0 - 11
src/ext4_defs/dir_entry.rs

@@ -4,7 +4,6 @@
 //! linear array of directory entries.
 
 use super::crc::*;
-use super::Ext4Block;
 use super::Ext4Superblock;
 use crate::constants::*;
 use crate::prelude::*;
@@ -150,16 +149,6 @@ impl Ext4DirEntry {
         csum
     }
 
-    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(
-            offset..offset + core::mem::size_of::<Ext4DirEntry>(),
-            data.iter().cloned(),
-        );
-        // assert_eq!(dst_blk.block_data[offset..offset + core::mem::size_of::<Ext4DirEntry>()], data[..]);
-    }
-
     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;

+ 4 - 12
src/ext4_defs/inode.rs

@@ -59,15 +59,11 @@ pub struct Ext4Inode {
     pub i_version_hi: u32,   // 64位版本的高32位
 }
 
-impl TryFrom<&[u8]> for Ext4Inode {
-    type Error = u64;
-    fn try_from(data: &[u8]) -> core::result::Result<Self, u64> {
-        let data = &data[..size_of::<Ext4Inode>()];
-        Ok(unsafe { core::ptr::read(data.as_ptr() as *const _) })
+impl Ext4Inode {
+    pub fn from_bytes(bytes: &[u8]) -> Self {
+        unsafe { *(bytes.as_ptr() as *const Ext4Inode) }
     }
-}
 
-impl Ext4Inode {
     pub fn flags(&self) -> u32 {
         self.flags
     }
@@ -210,7 +206,7 @@ impl Ext4Inode {
         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::try_from(inode_data).unwrap()
+        Ext4Inode::from_bytes(inode_data)
     }
 
     fn copy_to_byte_slice(&self, slice: &mut [u8]) {
@@ -305,10 +301,6 @@ impl Ext4Inode {
         })
     }
 
-    pub fn extent_depth(&mut self) -> u16 {
-        self.extent().header().depth()
-    }
-
     /// Initialize the `flags` and `block` field of inode. Mark the
     /// inode to use extent for block mapping. Initialize the root
     /// node of the extent tree