mod.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. use crate::constants::*;
  2. use crate::ext4_defs::*;
  3. use crate::prelude::*;
  4. use crate::return_error;
  5. mod alloc;
  6. mod dir;
  7. mod extent;
  8. mod high_level;
  9. mod journal;
  10. mod link;
  11. mod low_level;
  12. mod rw;
  13. #[derive(Debug)]
  14. pub struct Ext4 {
  15. block_device: Arc<dyn BlockDevice>,
  16. }
  17. impl Ext4 {
  18. /// Opens and loads an Ext4 from the `block_device`.
  19. pub fn load(block_device: Arc<dyn BlockDevice>) -> Result<Self> {
  20. // Load the superblock
  21. // TODO: if the main superblock is corrupted, should we load the backup?
  22. let block = block_device.read_block(0);
  23. let sb = block.read_offset_as::<SuperBlock>(BASE_OFFSET);
  24. // Check magic number
  25. if !sb.check_magic() {
  26. return_error!(ErrCode::EINVAL, "Invalid magic number");
  27. }
  28. // Check inode size
  29. if !sb.inode_size() == SB_GOOD_INODE_SIZE {
  30. return_error!(ErrCode::EINVAL, "Invalid inode size");
  31. }
  32. // Check block group desc size
  33. if !sb.desc_size() as usize != SB_GOOD_DESC_SIZE {
  34. return_error!(ErrCode::EINVAL, "Invalid block group desc size");
  35. }
  36. log::debug!("Ext4 loaded: {:?}", sb);
  37. // Create Ext4 instance
  38. Ok(Self { block_device })
  39. }
  40. /// Initializes the root directory.
  41. pub fn init(&mut self) -> Result<()> {
  42. // Create root directory
  43. self.create_root_inode().map(|_| ())
  44. }
  45. }