rw.rs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. use crate::constants::*;
  2. use crate::ext4_defs::*;
  3. use crate::prelude::*;
  4. use super::Ext4;
  5. impl Ext4 {
  6. /// Read a block from block device
  7. pub(super) fn read_block(&self, block_id: PBlockId) -> Block {
  8. self.block_device.read_block(block_id)
  9. }
  10. /// Write a block to block device
  11. pub(super) fn write_block(&self, block: &Block) {
  12. self.block_device.write_block(block)
  13. }
  14. /// Read super block from block device
  15. #[allow(unused)]
  16. pub(super) fn read_super_block(&self) -> SuperBlock {
  17. let block = self.read_block(0);
  18. block.read_offset_as(BASE_OFFSET)
  19. }
  20. /// Write super block to block device
  21. pub(super) fn write_super_block(&self, sb: &SuperBlock) {
  22. let mut block = Block::new(0, [0; BLOCK_SIZE]);
  23. block.write_offset_as(BASE_OFFSET, sb);
  24. self.block_device.write_block(&block)
  25. }
  26. /// Read an inode from block device, return an `InodeRef` that
  27. /// combines the inode and its id.
  28. pub(super) fn read_inode(&self, inode_id: InodeId) -> InodeRef {
  29. let (block_id, offset) = self.inode_disk_pos(inode_id);
  30. let block = self.read_block(block_id);
  31. let inode = InodeRef::new(inode_id, block.read_offset_as(offset));
  32. inode
  33. }
  34. /// Read the root inode from block device
  35. #[allow(unused)]
  36. pub(super) fn read_root_inode(&self) -> InodeRef {
  37. self.read_inode(EXT4_ROOT_INO)
  38. }
  39. /// Write an inode to block device with checksum
  40. pub(super) fn write_inode_with_csum(&self, inode_ref: &mut InodeRef) {
  41. let super_block = self.read_super_block();
  42. inode_ref.set_checksum(&super_block.uuid());
  43. self.write_inode_without_csum(inode_ref);
  44. }
  45. /// Write an inode to block device without checksum
  46. pub(super) fn write_inode_without_csum(&self, inode_ref: &InodeRef) {
  47. let (block_id, offset) = self.inode_disk_pos(inode_ref.id);
  48. let mut block = self.read_block(block_id);
  49. block.write_offset_as(offset, &inode_ref.inode);
  50. self.write_block(&block)
  51. }
  52. /// Read a block group descriptor from block device, return an `BlockGroupRef`
  53. /// that combines the block group descriptor and its id.
  54. pub(super) fn read_block_group(&self, block_group_id: BlockGroupId) -> BlockGroupRef {
  55. let (block_id, offset) = self.block_group_disk_pos(block_group_id);
  56. let block = self.read_block(block_id as PBlockId);
  57. BlockGroupRef::new(
  58. block_group_id,
  59. block.read_offset_as::<BlockGroupDesc>(offset),
  60. )
  61. }
  62. /// Write a block group descriptor to block device with checksum
  63. pub(super) fn write_block_group_with_csum(&self, bg_ref: &mut BlockGroupRef) {
  64. let super_block = self.read_super_block();
  65. bg_ref.set_checksum(&super_block.uuid());
  66. self.write_block_group_without_csum(bg_ref);
  67. }
  68. /// Write a block group descriptor to block device without checksum
  69. #[allow(unused)]
  70. pub(super) fn write_block_group_without_csum(&self, bg_ref: &BlockGroupRef) {
  71. let (block_id, offset) = self.block_group_disk_pos(bg_ref.id);
  72. let mut block = self.read_block(block_id as PBlockId);
  73. block.write_offset_as(offset, &bg_ref.desc);
  74. self.write_block(&block);
  75. }
  76. /// Get disk position of an inode. Return block id and offset within the block.
  77. ///
  78. /// Each block group contains `sb.inodes_per_group` inodes.
  79. /// Because inode 0 is defined not to exist, this formula can
  80. /// be used to find the block group that an inode lives in:
  81. /// `bg = (inode_id - 1) / sb.inodes_per_group`.
  82. ///
  83. /// The particular inode can be found within the block group's
  84. /// inode table at `index = (inode_id - 1) % sb.inodes_per_group`.
  85. /// To get the byte address within the inode table, use
  86. /// `offset = index * sb.inode_size`.
  87. fn inode_disk_pos(&self, inode_id: InodeId) -> (PBlockId, usize) {
  88. let super_block = self.read_super_block();
  89. let inodes_per_group = super_block.inodes_per_group();
  90. let bg_id = ((inode_id - 1) / inodes_per_group) as BlockGroupId;
  91. let inode_size = super_block.inode_size() as usize;
  92. let bg = self.read_block_group(bg_id);
  93. let id_in_bg = ((inode_id - 1) % inodes_per_group) as usize;
  94. let block_id =
  95. bg.desc.inode_table_first_block() + (id_in_bg * inode_size / BLOCK_SIZE) as PBlockId;
  96. let offset = (id_in_bg * inode_size) % BLOCK_SIZE;
  97. (block_id, offset)
  98. }
  99. /// Get disk position of a block group. Return block id and offset within the block.
  100. fn block_group_disk_pos(&self, block_group_id: BlockGroupId) -> (PBlockId, usize) {
  101. let super_block = self.read_super_block();
  102. let desc_per_block = BLOCK_SIZE as u32 / super_block.desc_size() as u32;
  103. let block_id = super_block.first_data_block() + block_group_id / desc_per_block + 1;
  104. let offset = (block_group_id % desc_per_block) * super_block.desc_size() as u32;
  105. (block_id as PBlockId, offset as usize)
  106. }
  107. }