2023-07-06 09:39:17 -07:00
|
|
|
#include "fs/ext2/inode_table.h"
|
|
|
|
|
2023-08-01 10:39:26 -07:00
|
|
|
InodeTable::InodeTable(const glcr::SharedPtr<Ext2BlockReader>& reader,
|
2023-11-22 14:59:41 -08:00
|
|
|
mmth::OwnedMemoryRegion&& bgdt_region)
|
2023-11-19 20:33:15 -08:00
|
|
|
: ext2_reader_(reader),
|
|
|
|
bgdt_region_(glcr::Move(bgdt_region)),
|
|
|
|
bgdt_(reinterpret_cast<BlockGroupDescriptor*>(bgdt_region_.vaddr())) {
|
2023-08-01 10:39:26 -07:00
|
|
|
inode_tables_.Resize(ext2_reader_->NumberOfBlockGroups());
|
2023-07-06 09:39:17 -07:00
|
|
|
}
|
|
|
|
|
2023-08-01 10:39:26 -07:00
|
|
|
glcr::ErrorOr<Inode*> InodeTable::GetInode(uint32_t inode_num) {
|
|
|
|
uint64_t inodes_per_group = ext2_reader_->GetSuperblock()->inodes_per_group;
|
2023-07-06 09:58:26 -07:00
|
|
|
uint64_t block_group_num = (inode_num - 1) / inodes_per_group;
|
2023-07-06 09:39:17 -07:00
|
|
|
ASSIGN_OR_RETURN(Inode * root, GetRootOfInodeTable(block_group_num));
|
2023-07-06 09:58:26 -07:00
|
|
|
uint64_t local_index = (inode_num - 1) % inodes_per_group;
|
2023-07-06 09:39:17 -07:00
|
|
|
return reinterpret_cast<Inode*>(reinterpret_cast<uint64_t>(root) +
|
2023-08-01 10:39:26 -07:00
|
|
|
local_index * ext2_reader_->InodeSize());
|
2023-07-06 09:39:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
glcr::ErrorOr<Inode*> InodeTable::GetRootOfInodeTable(
|
|
|
|
uint64_t block_group_num) {
|
2023-08-01 10:39:26 -07:00
|
|
|
if (block_group_num > ext2_reader_->NumberOfBlockGroups()) {
|
2023-07-06 09:39:17 -07:00
|
|
|
return glcr::INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!inode_tables_[block_group_num]) {
|
2023-07-06 09:58:26 -07:00
|
|
|
ASSIGN_OR_RETURN(
|
|
|
|
inode_tables_[block_group_num],
|
2023-08-01 10:39:26 -07:00
|
|
|
ext2_reader_->ReadBlocks(bgdt_[block_group_num].inode_table,
|
|
|
|
ext2_reader_->InodeTableBlockSize()));
|
2023-07-06 09:39:17 -07:00
|
|
|
}
|
|
|
|
return reinterpret_cast<Inode*>(inode_tables_[block_group_num].vaddr());
|
|
|
|
}
|