mod.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. /// The Ext4 filesystem implementation.
  14. pub struct Ext4 {
  15. #[cfg(feature = "block_cache")]
  16. block_cache: BlockCache,
  17. #[cfg(not(feature = "block_cache"))]
  18. block_device: Arc<dyn BlockDevice>,
  19. }
  20. impl Ext4 {
  21. /// Opens and loads an Ext4 from the `block_device`.
  22. pub fn load(block_device: Arc<dyn BlockDevice>) -> Result<Self> {
  23. // Load the superblock
  24. // TODO: if the main superblock is corrupted, should we load the backup?
  25. let block = block_device.read_block(0);
  26. let sb = block.read_offset_as::<SuperBlock>(BASE_OFFSET);
  27. log::debug!("Load Ext4 Superblock: {:?}", sb);
  28. // Check magic number
  29. if !sb.check_magic() {
  30. return_error!(ErrCode::EINVAL, "Invalid magic number");
  31. }
  32. // Check inode size
  33. if sb.inode_size() != SB_GOOD_INODE_SIZE {
  34. return_error!(ErrCode::EINVAL, "Invalid inode size {}", sb.inode_size());
  35. }
  36. // Check block group desc size
  37. if sb.desc_size() != SB_GOOD_DESC_SIZE {
  38. return_error!(
  39. ErrCode::EINVAL,
  40. "Invalid block group desc size {}",
  41. sb.desc_size()
  42. );
  43. }
  44. // Create Ext4 instance
  45. Ok(Self {
  46. #[cfg(feature = "block_cache")]
  47. block_cache: BlockCache::new(block_device),
  48. #[cfg(not(feature = "block_cache"))]
  49. block_device,
  50. })
  51. }
  52. /// Initializes the root directory.
  53. pub fn init(&mut self) -> Result<()> {
  54. // Create root directory
  55. self.create_root_inode().map(|_| ())
  56. }
  57. }