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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#include <kernel/filesystem/vfs.hpp>
#include <kernel/filesystem/constants.hpp>
#include <kernel/filesystem/dentry.hpp>
#include <kernel/filesystem/devfs/filesystem.hpp>
#include <kernel/filesystem/filesystem.hpp>
#include <kernel/filesystem/mount.hpp>
#include <kernel/filesystem/mount_table.hpp>
#include <kernel/filesystem/path.hpp>
#include <kernel/filesystem/rootfs/filesystem.hpp>
#include <kapi/system.hpp>
#include <kstd/memory>
#include <kstd/string>
#include <kstd/vector>
#include <algorithm>
#include <cstdint>
#include <optional>
#include <ranges>
#include <string_view>
#include <utility>
namespace
{
constinit auto static active_vfs = std::optional<kernel::filesystem::vfs>{};
} // namespace
namespace kernel::filesystem
{
auto vfs::init() -> void
{
if (active_vfs)
{
kapi::system::panic("[FILESYSTEM] vfs has already been initialized.");
}
active_vfs.emplace(vfs{});
active_vfs->init_internal();
}
auto vfs::init_internal() -> void
{
// mount rootfs at /
auto root_fs = kstd::make_shared<rootfs::filesystem>();
root_fs->mount(nullptr);
auto root_fs_root_dentry = kstd::make_shared<dentry>(nullptr, root_fs->root_inode(), "/");
auto root_mount = kstd::make_shared<mount>(nullptr, root_fs_root_dentry, root_fs, nullptr);
m_mount_table.add_mount(root_mount);
// mount devfs at /dev (inside rootfs, temporary, will be shadowed)
auto device_fs = kstd::make_shared<devfs::filesystem>();
device_fs->mount(nullptr);
if (auto dev_target_dentry = resolve_path("/dev"))
{
do_mount_internal(dev_target_dentry, root_mount, device_fs);
}
else
{
kapi::system::panic("[FILESYSTEM] failed to resolve /dev for initial devfs mount.");
}
// mount boot fs at / (shadows rootfs), re-graft devfs
if (auto boot_device_dentry = resolve_path("/dev/ram0"))
{
if (auto boot_root_fs = kernel::filesystem::filesystem::probe_and_mount(boot_device_dentry->get_inode()))
{
if (auto root_dentry = resolve_path("/"))
{
do_mount_internal(root_dentry, root_mount, boot_root_fs);
graft_persistent_device_fs(device_fs);
}
}
}
}
auto vfs::get() -> vfs &
{
if (!active_vfs)
{
kapi::system::panic("[FILESYSTEM] vfs has not been initialized.");
}
return *active_vfs;
}
auto vfs::open(std::string_view path) -> kstd::shared_ptr<dentry>
{
return resolve_path(path);
}
auto vfs::do_mount(std::string_view source, std::string_view target) -> operation_result
{
if (!path::is_valid_path(source) || !path::is_valid_path(target))
{
return operation_result::invalid_path;
}
auto [target_dentry, target_mount] = resolve_path_internal(target);
if (target_dentry && target_mount)
{
if (auto source_dentry = resolve_path(source))
{
if (auto fs = kernel::filesystem::filesystem::probe_and_mount(source_dentry->get_inode()))
{
do_mount_internal(target_dentry, target_mount, fs);
return operation_result::success;
}
return operation_result::invalid_filesystem;
}
return operation_result::non_existent_path;
}
return operation_result::mount_point_not_found;
}
auto vfs::unmount(std::string_view path) -> operation_result
{
if (!path::is_valid_path(path))
{
return operation_result::invalid_path;
}
auto remove_result = m_mount_table.remove_mount(path);
if (remove_result == mount_table::operation_result::removed)
{
return operation_result::success;
}
if (remove_result == mount_table::operation_result::has_child_mounts)
{
return operation_result::unmount_failed;
}
return operation_result::mount_point_not_found;
}
auto vfs::do_mount_internal(kstd::shared_ptr<dentry> const & target_dentry,
kstd::shared_ptr<mount> const & target_mount, kstd::shared_ptr<filesystem> const & fs)
-> void
{
auto new_fs_root =
kstd::make_shared<dentry>(target_dentry->get_parent(), fs->root_inode(), target_dentry->get_name());
auto new_mount = kstd::make_shared<mount>(target_dentry, new_fs_root, fs, target_mount);
m_mount_table.add_mount(new_mount);
}
auto vfs::graft_persistent_device_fs(kstd::shared_ptr<devfs::filesystem> const & device_fs) -> void
{
auto [new_root_dentry, root_mount] = resolve_path_internal("/");
if (new_root_dentry && root_mount)
{
auto dev_dentry = new_root_dentry->find_child("dev");
if (!dev_dentry)
{
dev_dentry = kstd::make_shared<dentry>(new_root_dentry, device_fs->root_inode(), "dev");
new_root_dentry->add_child(dev_dentry);
}
do_mount_internal(dev_dentry, root_mount, device_fs);
}
}
auto vfs::resolve_path_internal(std::string_view path) -> std::pair<kstd::shared_ptr<dentry>, kstd::shared_ptr<mount>>
{
if (!path::is_valid_absolute_path(path))
{
return {nullptr, nullptr};
}
auto current_mount = m_mount_table.find_exact_mount("/");
if (!current_mount)
{
kapi::system::panic("[FILESYSTEM] no root mount found.");
}
auto current_dentry = current_mount->get_root_dentry();
auto path_parts = path::split(path);
kstd::vector path_parts_vector(path_parts.begin(), path_parts.end());
std::ranges::reverse(path_parts_vector);
auto symlink_counter = 0uz;
while (!path_parts_vector.empty())
{
auto part = path_parts_vector.back();
path_parts_vector.pop_back();
if (part == ".")
{
continue;
}
if (part == "..")
{
auto parent_dentry = current_dentry->get_parent();
if (current_dentry == current_mount->get_root_dentry())
{
if (current_mount->get_mount_path() == "/")
{
continue;
}
if (auto parent_mount = current_mount->get_parent_mount())
{
current_mount = parent_mount;
current_dentry = parent_dentry;
}
}
current_dentry = parent_dentry;
continue;
}
auto next_dentry = current_dentry->find_child(part.view());
if (!next_dentry)
{
auto current_fs = current_mount->get_filesystem();
auto found_inode = current_fs->lookup(current_dentry->get_inode(), part.view());
if (!found_inode)
{
return {nullptr, nullptr};
}
next_dentry = kstd::make_shared<dentry>(current_dentry, found_inode, part.view());
current_dentry->add_child(next_dentry);
}
else if (next_dentry->has_flag(dentry::dentry_flags::is_mount_point))
{
current_mount = m_mount_table.find_exact_mount(next_dentry->get_absolute_path().view());
if (!current_mount)
{
kapi::system::panic("[FILESYSTEM] mount for dentry with mounted flag not found.");
}
next_dentry = current_mount->get_root_dentry();
}
if (next_dentry->get_inode()->is_symbolic_link())
{
if (symlink_counter++ > constants::symloop_max)
{
return {nullptr, nullptr};
}
kstd::vector<uint8_t> buffer(constants::symlink_max_path_length);
auto const bytes_read = next_dentry->get_inode()->read(buffer.data(), 0, buffer.size());
auto const symbolic_link_path = std::string_view{reinterpret_cast<char const *>(buffer.data()), bytes_read};
auto symbolic_link_parts = path::split(symbolic_link_path);
kstd::vector symbolic_link_parts_vector(symbolic_link_parts.begin(), symbolic_link_parts.end());
std::ranges::reverse(symbolic_link_parts_vector);
path_parts_vector.insert_range(path_parts_vector.end(), symbolic_link_parts_vector);
if (path::is_valid_absolute_path(symbolic_link_path))
{
current_mount = m_mount_table.find_exact_mount("/");
current_dentry = current_mount->get_root_dentry();
}
continue;
}
current_dentry = next_dentry;
}
return {current_dentry, current_mount};
}
auto vfs::resolve_path(std::string_view path) -> kstd::shared_ptr<dentry>
{
return resolve_path_internal(path).first;
}
auto vfs::find_mount(std::string_view path) -> kstd::shared_ptr<mount>
{
return resolve_path_internal(path).second;
}
} // namespace kernel::filesystem
namespace kernel::tests::filesystem::vfs
{
auto deinit() -> void
{
active_vfs.reset();
}
} // namespace kernel::tests::filesystem::vfs
|