mod.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use crate::constants::*;
  2. use crate::ext4_defs::*;
  3. use crate::prelude::*;
  4. mod alloc;
  5. mod dir;
  6. mod extent;
  7. mod file;
  8. mod journal;
  9. mod link;
  10. #[derive(Debug)]
  11. pub struct Ext4 {
  12. pub block_device: Arc<dyn BlockDevice>,
  13. pub super_block: Superblock,
  14. pub mount_point: MountPoint,
  15. }
  16. impl Ext4 {
  17. /// Opens and loads an Ext4 from the `block_device`.
  18. ///
  19. /// | Super Block | Group Descriptor | Reserved GDT Blocks |
  20. /// | Block Bitmap | Inode Bitmap | Inode Table | Data Blocks |
  21. pub fn load(block_device: Arc<dyn BlockDevice>) -> Result<Self> {
  22. // Load the superblock
  23. // TODO: if the main superblock is corrupted, should we load the backup?
  24. let block = block_device.read_block(0);
  25. let super_block = block.read_offset_as::<Superblock>(BASE_OFFSET);
  26. // Root mount point
  27. let mount_point = MountPoint::new("/");
  28. // Create Ext4 instance
  29. let mut ext4 = Self {
  30. super_block,
  31. block_device,
  32. mount_point,
  33. };
  34. // Create root directory
  35. ext4.alloc_root_inode()?;
  36. Ok(ext4)
  37. }
  38. /// Read an inode from block device, return an `InodeRef` that combines
  39. /// the inode and its id.
  40. fn get_inode_ref(&self, inode_id: InodeId) -> InodeRef {
  41. InodeRef::read_from_disk(self.block_device.clone(), &self.super_block, inode_id)
  42. }
  43. /// Read the root inode from block device
  44. fn get_root_inode_ref(&self) -> InodeRef {
  45. self.get_inode_ref(EXT4_ROOT_INO)
  46. }
  47. /// Write back an inode to block device with checksum
  48. fn write_back_inode_with_csum(&self, inode_ref: &mut InodeRef) {
  49. inode_ref
  50. .sync_to_disk_with_csum(self.block_device.clone(), &self.super_block)
  51. }
  52. /// Write back an inode to block device without checksum
  53. fn write_back_inode_without_csum(&self, inode_ref: &mut InodeRef) {
  54. inode_ref
  55. .sync_to_disk_without_csum(self.block_device.clone(), &self.super_block)
  56. }
  57. }