1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
#include "kernel/filesystem/ext2/filesystem.hpp"
#include "kapi/devices/device.hpp"
#include "kernel/devices/block_device_utils.hpp"
#include "kernel/filesystem/ext2/block_group_descriptor.hpp"
#include "kernel/filesystem/ext2/inode.hpp"
#include "kernel/filesystem/ext2/linked_directory_entry.hpp"
#include "kernel/filesystem/ext2/superblock.hpp"
#include "kernel/filesystem/filesystem.hpp"
#include "kernel/filesystem/inode.hpp"
#include <kstd/memory>
#include <kstd/print>
#include <kstd/string>
#include <kstd/vector>
#include <cstddef>
#include <cstdint>
#include <string_view>
namespace kernel::filesystem::ext2
{
namespace
{
constexpr size_t SUPERBLOCK_OFFSET = 1024;
constexpr uint16_t MAGIC_NUMBER = 0xEF53;
constexpr uint32_t ROOT_INODE_NUMBER = 2;
constexpr size_t DIRECT_BLOCK_COUNT = 12;
constexpr size_t INDIRECT_BLOCK_INDEX = DIRECT_BLOCK_COUNT;
constexpr size_t DOUBLY_INDIRECT_BLOCK_INDEX = INDIRECT_BLOCK_INDEX + 1;
constexpr size_t TRIPLY_INDIRECT_BLOCK_INDEX = DOUBLY_INDIRECT_BLOCK_INDEX + 1;
// Mode bits
constexpr uint16_t S_IFMT = 0xF000;
constexpr uint16_t S_IFREG = 0x8000;
constexpr uint16_t S_IFDIR = 0x4000;
auto S_ISREG(uint16_t mode) -> bool
{
return (mode & S_IFMT) == S_IFREG;
}
auto S_ISDIR(uint16_t mode) -> bool
{
return (mode & S_IFMT) == S_IFDIR;
}
} // namespace
auto filesystem::mount(kstd::shared_ptr<kapi::devices::device> const & device) -> int
{
kernel::filesystem::filesystem::mount(device);
kernel::devices::block_device_utils::read(m_device, &m_superblock, SUPERBLOCK_OFFSET, sizeof(m_superblock));
if (m_superblock.magic != MAGIC_NUMBER)
{
return -1;
}
auto const block_size = get_block_size();
auto const blocks_per_group = m_superblock.blocks_per_group;
auto const num_block_groups = (m_superblock.blocks_count + blocks_per_group - 1) / blocks_per_group;
m_block_group_descriptors = kstd::vector<block_group_descriptor>(num_block_groups);
auto const block_group_descriptor_table_offset = block_size == 1024 ? 2 * block_size : block_size;
kernel::devices::block_device_utils::read(m_device, m_block_group_descriptors.data(),
block_group_descriptor_table_offset,
num_block_groups * sizeof(block_group_descriptor));
m_root_inode = read_inode(ROOT_INODE_NUMBER);
// TODO BA-FS26 check if root inode is valid and is a directory ??
return 0;
}
auto filesystem::lookup(kstd::shared_ptr<kernel::filesystem::inode> const & parent, std::string_view name)
-> kstd::shared_ptr<kernel::filesystem::inode>
{
if (!parent || !parent->is_directory())
{
return nullptr;
}
auto * ext2_parent = static_cast<inode *>(parent.get());
if (!ext2_parent)
{
return nullptr;
}
auto const block_size = get_block_size();
auto const & inode_data = ext2_parent->m_data;
kstd::vector<uint8_t> buffer(block_size);
for (uint32_t i = 0; i < get_inode_block_count(inode_data); ++i)
{
auto const global_block_number = map_inode_block_index_to_global_block_number(i, inode_data);
auto const block_offset = global_block_number * block_size;
kernel::devices::block_device_utils::read(m_device, buffer.data(), block_offset, block_size);
auto const * entry = reinterpret_cast<linked_directory_entry const *>(buffer.data());
auto bytes_read = 0uz;
while (bytes_read < block_size && entry->inode != 0)
{
auto const entry_name = std::string_view{entry->name.data(), entry->name_len};
if (entry_name == name)
{
return read_inode(entry->inode);
}
bytes_read += entry->rec_len;
entry = reinterpret_cast<linked_directory_entry const *>(buffer.data() + bytes_read);
}
}
return nullptr;
}
auto filesystem::read_inode(uint32_t inode_number) -> kstd::shared_ptr<inode>
{
auto const block_size = get_block_size();
auto const inodes_per_group = m_superblock.inodes_per_group;
auto const block_group_index = (inode_number - 1) / inodes_per_group;
auto const inode_index_within_group = (inode_number - 1) % inodes_per_group;
if (block_group_index >= m_block_group_descriptors.size())
{
return nullptr;
}
auto const & block_group_descriptor = m_block_group_descriptors.at(block_group_index);
auto const inode_table_start_block = block_group_descriptor.inode_table;
auto const inode_table_offset = static_cast<size_t>(inode_table_start_block) * block_size;
auto const inode_offset = inode_table_offset + inode_index_within_group * get_inode_size();
auto new_inode = kstd::make_shared<inode>();
kernel::devices::block_device_utils::read(m_device, &new_inode->m_data, inode_offset, sizeof(inode_data));
// TODO BA-FS26 improve inode_kind really needed? or just map it to the mode bits?
if (S_ISREG(new_inode->m_data.mode))
{
new_inode->m_kind = inode::inode_kind::regular;
}
else if (S_ISDIR(new_inode->m_data.mode))
{
new_inode->m_kind = inode::inode_kind::directory;
}
else
{
// TODO BA-FS26 really correct??
return nullptr;
}
return new_inode;
}
auto filesystem::map_inode_block_index_to_global_block_number(uint32_t inode_block_index, inode_data data) -> uint32_t
{
if (inode_block_index < DIRECT_BLOCK_COUNT)
{
return data.block.at(inode_block_index);
}
inode_block_index -= DIRECT_BLOCK_COUNT;
auto const block_size = get_block_size();
auto const numbers_per_block = block_size / sizeof(uint32_t);
uint32_t block_number_buffer = 0;
auto const number_of_singly_indirect_blocks = numbers_per_block;
if (inode_block_index < number_of_singly_indirect_blocks)
{
auto const indirect_block_start_offset = data.block.at(INDIRECT_BLOCK_INDEX) * block_size;
auto const direct_block_number_index = inode_block_index;
auto const direct_block_number_offset =
indirect_block_start_offset + direct_block_number_index * sizeof(uint32_t);
kernel::devices::block_device_utils::read(m_device, &block_number_buffer, direct_block_number_offset,
sizeof(uint32_t));
return block_number_buffer;
}
inode_block_index -= number_of_singly_indirect_blocks;
auto const number_of_doubly_indirect_blocks = numbers_per_block * numbers_per_block;
if (inode_block_index < number_of_doubly_indirect_blocks)
{
auto const doubly_indirect_block_start_offset = data.block.at(DOUBLY_INDIRECT_BLOCK_INDEX) * block_size;
auto const indirect_block_number_index = inode_block_index / numbers_per_block;
auto const indirect_block_number_offset =
doubly_indirect_block_start_offset + indirect_block_number_index * sizeof(uint32_t);
kernel::devices::block_device_utils::read(m_device, &block_number_buffer, indirect_block_number_offset,
sizeof(uint32_t));
auto const indirect_block_start_offset = block_number_buffer * block_size;
auto const direct_block_number_index = inode_block_index % numbers_per_block;
auto const direct_block_number_offset =
indirect_block_start_offset + direct_block_number_index * sizeof(uint32_t);
kernel::devices::block_device_utils::read(m_device, &block_number_buffer, direct_block_number_offset,
sizeof(uint32_t));
return block_number_buffer;
}
inode_block_index -= number_of_doubly_indirect_blocks;
auto const number_of_triply_indirect_blocks = numbers_per_block * numbers_per_block * numbers_per_block;
if (inode_block_index < number_of_triply_indirect_blocks)
{
auto const triply_indirect_block_start_offset = data.block.at(TRIPLY_INDIRECT_BLOCK_INDEX) * block_size;
auto const doubly_indirect_block_number_index = inode_block_index / (numbers_per_block * numbers_per_block);
auto const doubly_indirect_block_number_offset =
triply_indirect_block_start_offset + doubly_indirect_block_number_index * sizeof(uint32_t);
kernel::devices::block_device_utils::read(m_device, &block_number_buffer, doubly_indirect_block_number_offset,
sizeof(uint32_t));
auto const doubly_indirect_block_start_offset = block_number_buffer * block_size;
auto const indirect_block_number_index = (inode_block_index / numbers_per_block) % numbers_per_block;
auto const indirect_block_num
|