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
|
#include <kernel/devices/storage/management.hpp>
#include <kernel/filesystem/device_inode.hpp>
#include <kernel/filesystem/file_descriptor_table.hpp>
#include <kernel/filesystem/open_file_descriptor.hpp>
#include <kernel/filesystem/vfs.hpp>
#include <kernel/memory.hpp>
#include <kapi/boot_modules.hpp>
#include <kapi/cio.hpp>
#include <kapi/cpu.hpp>
#include <kapi/devices.hpp>
#include <kapi/filesystem.hpp>
#include <kapi/interrupts.hpp>
#include <kapi/memory.hpp>
#include <kapi/system.hpp>
#include <kstd/format>
#include <kstd/memory>
#include <kstd/os/error.hpp>
#include <kstd/print>
#include <kstd/units>
#include <kstd/vector>
#include <algorithm>
#include <cstddef>
#include <string_view>
using namespace kstd::units_literals;
auto test_device_names() -> void
{
auto storage_mgmt = kernel::devices::storage::management::get();
std::ranges::for_each(storage_mgmt.all_controllers(), [](auto const & controller) {
std::ranges::for_each(controller->all_devices(),
[](auto const & device) { kstd::println("{}", device->name().view()); });
});
}
auto test_file_descriptor_manually() -> void
{
// setup
auto fd_table = kernel::filesystem::file_descriptor_table::get();
auto storage_mgmt = kernel::devices::storage::management::get();
auto device = storage_mgmt.device_by_major_minor(1, 0);
auto dev_node = kstd::make_shared<kernel::filesystem::device_inode>(device);
auto ofd = kstd::make_shared<kernel::filesystem::open_file_descriptor>(dev_node);
auto fd_index = fd_table.add_file(ofd);
// use: read two bytes and write two again
auto fd = fd_table.get_file(fd_index);
if (!fd)
{
kstd::os::panic("test code failed");
}
kstd::vector<std::byte> buffer{2};
auto number_of_read_bytes = fd->read(buffer.data(), buffer.size());
kstd::println("read bytes: {}", number_of_read_bytes);
kstd::println("buffer: {::#04x}", buffer);
// write half of the file new
auto const value1 = std::byte{0xAA};
auto const value2 = std::byte{0xBB};
kstd::vector<std::byte> write_buffer{value1, value2};
auto written_bytes = fd->write(write_buffer.data(), write_buffer.size());
kstd::println("written bytes: {}", written_bytes);
fd_table.remove_file(fd_index);
// use: read four bytes again -> two old bytes two new bytes
auto ofd1 = kstd::make_shared<kernel::filesystem::open_file_descriptor>(dev_node);
fd_index = fd_table.add_file(ofd1);
auto fd1 = fd_table.get_file(fd_index);
if (!fd1)
{
kstd::os::panic("test code failed");
}
kstd::vector<std::byte> buffer1{4};
number_of_read_bytes = fd1->read(buffer1.data(), buffer1.size());
kstd::println("read bytes: {}", number_of_read_bytes);
kstd::println("buffer: {::#04x}", buffer1);
}
auto test_device_with_vfs() -> void
{
auto vfs = kernel::filesystem::vfs::get();
auto dentry = vfs.open("/dev/ram0");
if (!dentry)
{
kstd::os::panic("test code failed");
}
auto fd_table = kernel::filesystem::file_descriptor_table::get();
auto ofd = kstd::make_shared<kernel::filesystem::open_file_descriptor>(dentry->get_inode());
auto fd = fd_table.add_file(ofd);
kstd::vector<std::byte> buffer{2};
auto file = fd_table.get_file(fd);
if (!file)
{
kstd::os::panic("test code failed");
}
auto number_of_read_bytes = file->read(buffer.data(), buffer.size());
kstd::println("read bytes: {}", number_of_read_bytes);
kstd::println("buffer: {::#04x}", buffer);
}
auto test_file_lookup() -> void
{
auto vfs = kernel::filesystem::vfs::get();
auto read_and_write_file = [&vfs](std::string_view path) {
kstd::println("[TEST] Reading and writing file at path: {}", path);
auto dentry = vfs.open(path);
if (!dentry)
{
kstd::os::panic("test code failed");
}
kstd::vector<std::byte> buffer{32};
auto ofd = kstd::make_shared<kernel::filesystem::open_file_descriptor>(dentry->get_inode());
auto number_of_read_bytes = ofd->read(buffer.data(), buffer.size());
kstd::println("read bytes: {}", number_of_read_bytes);
kstd::println("buffer: {::#04x}", buffer);
std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), number_of_read_bytes};
kstd::println("buffer_as_str: {}", buffer_as_str);
};
read_and_write_file("/info.txt");
read_and_write_file("/enclosures/info.txt");
read_and_write_file("/enclosures/aquarium/tank_1/fish_4.txt");
read_and_write_file("/enclosures/elephant_house/elephant_1.txt");
read_and_write_file(
"/enclosures/aquarium/tank_2/"
"this_is_a_very_very_long_fish_filename_that_keeps_going_and_going_until_it_almost_breaks_linux_filesystem_"
"limits_for_testing_purposes_and_we_add_more_characters_to_make_it_even_longer_30.txt");
vfs.do_mount("/dev/ram16", "/enclosures/aquarium");
read_and_write_file("/enclosures/aquarium/closed.txt");
read_and_write_file("/enclosures/aquarium/information/info_2.txt");
vfs.unmount("/enclosures/aquarium");
read_and_write_file("/enclosures/aquarium/tank_1/fish_4.txt");
vfs.do_mount("/dev/ram32", "/enclosures/elephant_house");
read_and_write_file("/enclosures/elephant_house/monkey_house/infrastructure/info.txt");
vfs.do_mount("/dev/ram16", "/enclosures/elephant_house/monkey_house");
read_and_write_file("/enclosures/elephant_house/monkey_house/information/info_2.txt");
auto result = vfs.unmount("/enclosures/elephant_house");
if (result == kernel::filesystem::vfs::operation_result::unmount_failed)
{
kstd::println("[TEST] Unmount failed as expected due to active child mount.");
}
vfs.unmount("/enclosures/elephant_house/monkey_house");
result = vfs.unmount("/enclosures/elephant_house");
if (result == kernel::filesystem::vfs::operation_result::success)
{
kstd::println("[TEST] Unmount succeeded after unmounting child mount.");
}
}
auto run_test_code() -> void
{
kstd::println("[TEST] Running test code...");
kstd::println("[TEST] device names");
test_device_names();
kstd::println("---------------------------------");
kstd::println("[TEST] file descriptor manually");
test_file_descriptor_manually();
kstd::println("---------------------------------");
kstd::println("[TEST] device with VFS");
test_device_with_vfs();
kstd::println("---------------------------------");
kstd::println("[TEST] file lookup");
test_file_lookup();
kstd::println("---------------------------------");
}
auto run_demo() -> void
{
// 1) open a file
auto fd_1 = kapi::filesystem::open("/entrance/tickets.txt");
if (fd_1 == -1)
{
kstd::os::panic("demo failed");
}
// 2) read from the file
kstd::vector<std::byte> buffer_1{10};
auto bytes_read = kapi::filesystem::read(fd_1, buffer_1.data(), buffer_1.size());
auto buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_1.data()), static_cast<size_t>(bytes_read)};
kstd::println("Read {} bytes from /entrance/tickets.txt: {}", bytes_read, buffer_as_str);
// 3) show that /entrance/information/info_1.txt is not accessible before mounting
auto fd_before_mount = kapi::filesystem::open("/entrance/information/info_1.txt");
if (fd_before_mount == -1)
{
kstd::println("/entrance/information/info_1.txt is not accessible before mounting, as expected.");
}
// 4) mount a new filesystem on top of /entrance
kapi::filesystem::mount("/dev/ram16", "/entrance");
// 5) open a file from the new filesystem
auto fd_2 = kapi::filesystem::open("/entrance/information/info_1.txt");
if (fd_2 == -1)
{
kstd::os::panic("demo failed");
}
// 6) read from the new file
kstd::vector<std::byte> buffer_2{10};
bytes_read = kapi::filesystem::read(fd_2, buffer_2.data(), buffer_2.size());
buffer_as_str = std::string_view{reinterpret_cast<char *>(buffer_2.data()), static_cast<size_t>(bytes_read)};
kstd::println("Read {} bytes from /entrance/information/info_1.txt: {}", bytes_read, buffer_as_str);
// 7) open device as file
auto fd_3 = kapi::filesystem::open("/dev/ram48");
if (fd_3 == -1)
{
kstd::os::panic("demo failed");
}
// 8) read from the device file
kstd::vector<std::byte> buffer_3{2};
bytes_read = kapi::filesystem::read(fd_3, buffer_3.data(), buffer_3.size());
kstd::println("Read {} bytes from /dev/ram48: {::#04x}", bytes_read, buffer_3);
// 9) write to the device file
kstd::vector<std::byte> write_buffer{std::
|