aboutsummaryrefslogtreecommitdiff
path: root/kernel/filesystem/src/vfs.cpp
blob: 573994ae83e465992a168f4a3d1cd66b6f6f00f6 (plain)
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
#include "filesystem/vfs.hpp"

#include "kapi/system.hpp"

#include "devices/device.hpp"
#include "devices/storage/storage_management.hpp"
#include "filesystem/ext2/ext2_filesystem.hpp"
#include "filesystem/mount.hpp"

#include <kstd/cstring>

#include <algorithm>
#include <array>
#include <optional>
#include <string_view>

namespace filesystem
{
  namespace
  {
    constinit auto static active_vfs = std::optional<vfs>{};

    // TODO BA-FS26 @Felix better solution? while dynamic memory not available?
    // TODO BA-FS26 remove when dynamic memory available;
    constinit auto static root_fs = std::optional<ext2::ext2_filesystem>{};
    // TODO BA-FS26 use kstd::vector when available
    constinit auto static filesystems = std::array<std::optional<ext2::ext2_filesystem>, 1>{};

    // TODO BA-FS26 @Felix
    // TODO BA-FS26 depends on the length of ram_disk_device name
    constinit std::array<char, 10> device_mount_path = {'/', 'd', 'e', 'v', '/', 'x', 'x', 'x', 'x', '\0'};
  }  // namespace

  auto vfs::init() -> void
  {
    if (active_vfs)
    {
      kapi::system::panic("[FILESYSTEM] vfs has already been initialized.");
    }

    active_vfs.emplace(vfs{});

    auto storage_mgmt = devices::storage::storage_management::get();
    if (auto boot_device = storage_mgmt.determine_boot_device())
    {
      root_fs.emplace(ext2::ext2_filesystem{});
      if (root_fs->mount(boot_device) != 0)
      {
        kapi::system::panic("[FILESYSTEM] Failed to mount root filesystem.");
      }

      active_vfs->m_root_mount = mount{"/", &*root_fs};

      std::ranges::for_each(storage_mgmt.all_controllers(), [&](auto controller) {
        std::ranges::for_each(controller->all_devices(), [&](auto device) { active_vfs->make_device_node(device); });
      });
    }
    else
    {
      // TODO BA-FS26 ?? what when no boot_device == no modules loaded??
    }
  }

  auto vfs::get() -> vfs &
  {
    if (!active_vfs)
    {
      kapi::system::panic("[FILESYSTEM] vfs has not been initialized.");
    }

    return *active_vfs;
  }

  auto vfs::make_device_node(devices::device * device) -> void
  {
    auto device_name = device->name();
    kstd::libc::memcpy(&device_mount_path[5], device_name.data(), device_name.size());

    // TODO BA-FS26 use kstd::vector and push_back when available
    filesystems[0].emplace(ext2::ext2_filesystem{});
    if (filesystems[0]->mount(device) != 0)
    {
      kapi::system::panic("[FILESYSTEM] Failed to mount device filesystem.");
    }

    m_mounts[0] = mount{std::string_view{device_mount_path.data()}, &*filesystems[0]};
  }

}  // namespace filesystem