diff --git a/kernel/src/fs/ext2/Inode.cpp b/kernel/src/fs/ext2/Inode.cpp index 53c063f9..309a4278 100644 --- a/kernel/src/fs/ext2/Inode.cpp +++ b/kernel/src/fs/ext2/Inode.cpp @@ -28,7 +28,7 @@ namespace Ext2 if (offset % block_size) { usize block_offset = (offset % block_size); - usize block = find_block(offset / block_size); + usize block = TRY(find_block(offset / block_size)); usize size_to_read = block_size - block_offset; if (size_to_read > to_read) size_to_read = to_read; @@ -44,7 +44,7 @@ namespace Ext2 while (to_read >= block_size) { - usize block = find_block(offset / block_size); + usize block = TRY(find_block(offset / block_size)); usize host_offset = block * block_size; @@ -58,7 +58,7 @@ namespace Ext2 if (to_read > 0) { - usize block = find_block(offset / block_size); + usize block = TRY(find_block(offset / block_size)); usize host_offset = block * block_size; @@ -69,11 +69,26 @@ namespace Ext2 return length; } - usize Inode::find_block(usize index) const + Result Inode::find_block(usize index) const { - expect(index < 12, "ext2: Finding blocks in the indirect pointers is not yet supported"); + if (index < 12) return m_raw_inode.direct_pointers[index]; - return m_raw_inode.direct_pointers[index]; + usize block_index = (index - 12) * sizeof(u32); + if (block_index >= m_fs->m_block_size) + { + fail("ext2: Finding blocks beyond the singly indirect pointer block is not yet supported"); + } + + usize block_size = m_fs->m_block_size; + + if (m_singly_indirect_block.is_empty()) + { + TRY(m_singly_indirect_block.try_resize(block_size)); + TRY(m_fs->m_host_device->read(m_singly_indirect_block.data(), m_raw_inode.singly_indirect_ptr * block_size, + block_size)); + } + + return *reinterpret_cast(&m_singly_indirect_block.data()[block_index]); } Result Inode::lazy_initialize_dir() const diff --git a/kernel/src/fs/ext2/Inode.h b/kernel/src/fs/ext2/Inode.h index d1359795..5c2973d0 100644 --- a/kernel/src/fs/ext2/Inode.h +++ b/kernel/src/fs/ext2/Inode.h @@ -1,5 +1,6 @@ #pragma once #include "fs/ext2/FileSystem.h" +#include #include #define EXT2_FIFO 0x1000 @@ -141,12 +142,14 @@ namespace Ext2 FileSystem* m_fs; ino_t m_inum; + mutable Buffer m_singly_indirect_block; + String m_link; mutable Vector m_entries; mutable bool m_dir_already_lazily_initialized { false }; - usize find_block(usize index) const; + Result find_block(usize index) const; friend class FileSystem; };