aboutsummaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'kernel')
-rw-r--r--kernel/devices/include/devices/storage/ram_disk/ram_disk_device.hpp2
-rw-r--r--kernel/devices/include/devices/storage/storage_controller.hpp11
-rw-r--r--kernel/devices/include/devices/storage/storage_management.hpp15
-rw-r--r--kernel/devices/src/storage/ram_disk/ram_disk_controller.cpp15
-rw-r--r--kernel/devices/src/storage/ram_disk/ram_disk_device.cpp4
-rw-r--r--kernel/devices/src/storage/storage_controller.cpp8
-rw-r--r--kernel/devices/src/storage/storage_management.cpp25
-rw-r--r--kernel/filesystem/include/filesystem/custody.hpp12
-rw-r--r--kernel/filesystem/include/filesystem/device_file.hpp6
-rw-r--r--kernel/filesystem/include/filesystem/ext2/ext2_filesystem.hpp6
-rw-r--r--kernel/filesystem/include/filesystem/file_descriptor_table.hpp6
-rw-r--r--kernel/filesystem/include/filesystem/filesystem.hpp7
-rw-r--r--kernel/filesystem/include/filesystem/inode.hpp8
-rw-r--r--kernel/filesystem/include/filesystem/inode_file.hpp6
-rw-r--r--kernel/filesystem/include/filesystem/mount.hpp9
-rw-r--r--kernel/filesystem/include/filesystem/open_file_description.hpp5
-rw-r--r--kernel/filesystem/include/filesystem/vfs.hpp15
-rw-r--r--kernel/filesystem/src/custody.cpp8
-rw-r--r--kernel/filesystem/src/device_file.cpp16
-rw-r--r--kernel/filesystem/src/ext2/ext2_filesystem.cpp5
-rw-r--r--kernel/filesystem/src/file_descriptor_table.cpp9
-rw-r--r--kernel/filesystem/src/filesystem.cpp9
-rw-r--r--kernel/filesystem/src/inode.cpp6
-rw-r--r--kernel/filesystem/src/inode_file.cpp4
-rw-r--r--kernel/filesystem/src/mount.cpp6
-rw-r--r--kernel/filesystem/src/open_file_description.cpp14
-rw-r--r--kernel/filesystem/src/vfs.cpp45
-rw-r--r--kernel/src/main.cpp19
28 files changed, 154 insertions, 147 deletions
diff --git a/kernel/devices/include/devices/storage/ram_disk/ram_disk_device.hpp b/kernel/devices/include/devices/storage/ram_disk/ram_disk_device.hpp
index 678ee99..1edf48c 100644
--- a/kernel/devices/include/devices/storage/ram_disk/ram_disk_device.hpp
+++ b/kernel/devices/include/devices/storage/ram_disk/ram_disk_device.hpp
@@ -14,8 +14,6 @@ namespace devices::storage::ram_disk
*/
struct ram_disk_device : block_device
{
- ram_disk_device(); // TODO BA-FS26 remove when kstd::vector is available
-
/**
* @brief Create a RAM disk for the @p module.
* @param module Boot module providing the memory region.
diff --git a/kernel/devices/include/devices/storage/storage_controller.hpp b/kernel/devices/include/devices/storage/storage_controller.hpp
index d10d27d..e90b01c 100644
--- a/kernel/devices/include/devices/storage/storage_controller.hpp
+++ b/kernel/devices/include/devices/storage/storage_controller.hpp
@@ -3,7 +3,9 @@
#include "devices/device.hpp"
-#include <array>
+#include <kstd/memory>
+#include <kstd/vector>
+
#include <cstddef>
namespace devices::storage
@@ -46,8 +48,7 @@ namespace devices::storage
[[nodiscard]] auto devices_count() const -> size_t;
// TODO BA-FS26 add comment
- // TODO BA-FS26 use kstd::vector when available
- [[nodiscard]] auto all_devices() const -> std::array<devices::device *, 1> const &;
+ [[nodiscard]] auto all_devices() const -> kstd::vector<kstd::shared_ptr<devices::device>> const &;
/**
* @brief Find a managed device by major/minor numbers.
@@ -55,12 +56,12 @@ namespace devices::storage
* @param minor Device minor number.
* @return Matching block device, or nullptr if no device matches.
*/
- [[nodiscard]] auto device_by_minor(size_t minor) const -> device *;
+ [[nodiscard]] auto device_by_minor(size_t minor) const -> kstd::shared_ptr<devices::device>;
protected:
size_t m_major{};
size_t m_minors_per_device{};
- std::array<devices::device *, 1> m_devices{}; // TODO BA-FS26 use kstd::vector when available
+ kstd::vector<kstd::shared_ptr<devices::device>> m_devices{};
};
} // namespace devices::storage
diff --git a/kernel/devices/include/devices/storage/storage_management.hpp b/kernel/devices/include/devices/storage/storage_management.hpp
index 550db65..dc2f6c9 100644
--- a/kernel/devices/include/devices/storage/storage_management.hpp
+++ b/kernel/devices/include/devices/storage/storage_management.hpp
@@ -4,7 +4,9 @@
#include "devices/device.hpp"
#include "devices/storage/storage_controller.hpp"
-#include <array>
+#include <kstd/memory>
+#include <kstd/vector>
+
#include <cstddef>
namespace devices::storage
@@ -40,11 +42,10 @@ namespace devices::storage
*
* Assigns controller IDs (major number range and minors per device).
*/
- auto add_controller(storage_controller * controller) -> void;
+ auto add_controller(kstd::shared_ptr<storage_controller> controller) -> void;
// TODO BA-FS26 add comment
- // TODO BA-FS26 use kstd::vector when available
- [[nodiscard]] auto all_controllers() const -> std::array<storage_controller *, 1> const &;
+ [[nodiscard]] auto all_controllers() const -> kstd::vector<kstd::shared_ptr<storage_controller>> const &;
/**
* @brief Find a device by major/minor numbers.
@@ -52,13 +53,13 @@ namespace devices::storage
* @param minor Device minor number.
* @return Matching device, or nullptr if no device matches.
*/
- auto device_by_major_minor(size_t major, size_t minor) -> device *;
+ auto device_by_major_minor(size_t major, size_t minor) -> kstd::shared_ptr<device>;
/**
* @brief Determine the boot device.
* @return Boot device, or nullptr if it cannot be determined.
*/
- auto determine_boot_device() -> device *;
+ auto determine_boot_device() -> kstd::shared_ptr<device>;
private:
/**
@@ -66,7 +67,7 @@ namespace devices::storage
*/
storage_management() = default;
- std::array<storage_controller *, 1> m_controllers{}; // TODO BA-FS26 use kstd::vector when available
+ kstd::vector<kstd::shared_ptr<storage_controller>> m_controllers{};
};
} // namespace devices::storage
diff --git a/kernel/devices/src/storage/ram_disk/ram_disk_controller.cpp b/kernel/devices/src/storage/ram_disk/ram_disk_controller.cpp
index b57dcfb..3d14faf 100644
--- a/kernel/devices/src/storage/ram_disk/ram_disk_controller.cpp
+++ b/kernel/devices/src/storage/ram_disk/ram_disk_controller.cpp
@@ -4,22 +4,14 @@
#include "devices/storage/ram_disk/ram_disk_device.hpp"
+#include <kstd/memory>
#include <kstd/print>
#include <algorithm>
-#include <array>
#include <cstddef>
-#include <optional>
namespace devices::storage::ram_disk
{
- namespace
- {
- // TODO BA-FS26 @Felix gibts besseren weg (ausser dynamic Memory)
- // TODO BA-FS26 remove again, when dynamic memory available
- constinit auto static active_ram_disk_device = std::optional<ram_disk_device>{};
- } // namespace
-
ram_disk_controller::ram_disk_controller(kapi::boot_modules::boot_module_registry const * registry)
: m_boot_module_registry(registry)
{}
@@ -30,10 +22,7 @@ namespace devices::storage::ram_disk
std::ranges::for_each(*m_boot_module_registry, [this, &current_device_index](auto const & module) {
auto const minor = current_device_index++ * m_minors_per_device;
-
- // TODO BA-FS26 use push_back from kstd::vector when available
- active_ram_disk_device.emplace(module, m_major, minor);
- m_devices.at(0) = &*active_ram_disk_device;
+ m_devices.push_back(kstd::make_shared<ram_disk_device>(module, m_major, minor));
});
}
} // namespace devices::storage::ram_disk \ No newline at end of file
diff --git a/kernel/devices/src/storage/ram_disk/ram_disk_device.cpp b/kernel/devices/src/storage/ram_disk/ram_disk_device.cpp
index 774e949..6ff2a83 100644
--- a/kernel/devices/src/storage/ram_disk/ram_disk_device.cpp
+++ b/kernel/devices/src/storage/ram_disk/ram_disk_device.cpp
@@ -28,10 +28,6 @@ namespace devices::storage::ram_disk
}
} // namespace
- ram_disk_device::ram_disk_device() // TODO BA-FS26 remove when kstd::vector is available
- : block_device(0, 0, determine_device_name(0), RAM_DISK_BLOCK_SIZE)
- {}
-
ram_disk_device::ram_disk_device(kapi::boot_modules::boot_module const & module, size_t major, size_t minor)
: block_device(major, minor, determine_device_name(minor), RAM_DISK_BLOCK_SIZE)
, m_boot_module(module)
diff --git a/kernel/devices/src/storage/storage_controller.cpp b/kernel/devices/src/storage/storage_controller.cpp
index d9bc806..16d40f4 100644
--- a/kernel/devices/src/storage/storage_controller.cpp
+++ b/kernel/devices/src/storage/storage_controller.cpp
@@ -2,8 +2,10 @@
#include "devices/device.hpp"
+#include <kstd/memory>
+#include <kstd/vector>
+
#include <algorithm>
-#include <array>
#include <cstddef>
namespace devices::storage
@@ -19,7 +21,7 @@ namespace devices::storage
return m_major;
}
- auto storage_controller::device_by_minor(size_t minor) const -> device *
+ auto storage_controller::device_by_minor(size_t minor) const -> kstd::shared_ptr<devices::device>
{
auto it = std::ranges::find_if(m_devices, [minor](auto const & device) { return device->minor() == minor; });
@@ -35,7 +37,7 @@ namespace devices::storage
return m_devices.size();
}
- auto storage_controller::all_devices() const -> std::array<devices::device *, 1> const &
+ auto storage_controller::all_devices() const -> kstd::vector<kstd::shared_ptr<devices::device>> const &
{
return m_devices;
}
diff --git a/kernel/devices/src/storage/storage_management.cpp b/kernel/devices/src/storage/storage_management.cpp
index e1f1bcc..00449fb 100644
--- a/kernel/devices/src/storage/storage_management.cpp
+++ b/kernel/devices/src/storage/storage_management.cpp
@@ -7,8 +7,10 @@
#include "devices/storage/ram_disk/ram_disk_controller.hpp"
#include "devices/storage/storage_controller.hpp"
+#include <kstd/memory>
+#include <kstd/vector>
+
#include <algorithm>
-#include <array>
#include <cstddef>
#include <optional>
@@ -21,8 +23,6 @@ namespace devices::storage
constinit size_t static next_free_major = START_MAJOR;
constinit auto static active_storage_management = std::optional<storage_management>{};
- // TODO BA-FS26 remove again, when dynamic memory available
- constinit auto static active_ram_disk_controller = std::optional<ram_disk::ram_disk_controller>{};
} // namespace
auto storage_management::init() -> void
@@ -33,8 +33,9 @@ namespace devices::storage
}
active_storage_management.emplace(storage_management{});
- active_ram_disk_controller.emplace(&kapi::boot_modules::get_boot_module_registry());
- active_storage_management->add_controller(&active_ram_disk_controller.value());
+ auto current_ram_disk_controller =
+ kstd::make_shared<ram_disk::ram_disk_controller>(&kapi::boot_modules::get_boot_module_registry());
+ active_storage_management->add_controller(current_ram_disk_controller);
std::ranges::for_each(active_storage_management->m_controllers, [](auto controller) { controller->probe(); });
}
@@ -49,22 +50,22 @@ namespace devices::storage
return *active_storage_management;
}
- auto storage_management::add_controller(storage_controller * controller) -> void
+ auto storage_management::add_controller(kstd::shared_ptr<storage_controller> controller) -> void
{
controller->set_ids(next_free_major++, MINORS_PER_DEVICE);
- m_controllers.at(0) = controller; // TODO BA-FS26 use push_back from kstd:vector
+ m_controllers.push_back(controller);
}
- auto storage_management::all_controllers() const -> std::array<storage_controller *, 1> const &
+ auto storage_management::all_controllers() const -> kstd::vector<kstd::shared_ptr<storage_controller>> const &
{
return m_controllers;
}
- auto storage_management::device_by_major_minor(size_t major, size_t minor) -> device *
+ auto storage_management::device_by_major_minor(size_t major, size_t minor) -> kstd::shared_ptr<device>
{
- device * found = nullptr;
+ kstd::shared_ptr<device> found = nullptr;
- std::ranges::find_if(m_controllers, [&](auto const controller) {
+ std::ranges::find_if(m_controllers, [&](auto const & controller) {
if (controller != nullptr && controller->major() == major)
{
found = controller->device_by_minor(minor);
@@ -76,7 +77,7 @@ namespace devices::storage
return found;
}
- auto storage_management::determine_boot_device() -> device *
+ auto storage_management::determine_boot_device() -> kstd::shared_ptr<device>
{
return device_by_major_minor(START_MAJOR, 0);
}
diff --git a/kernel/filesystem/include/filesystem/custody.hpp b/kernel/filesystem/include/filesystem/custody.hpp
index 9ee984d..b6eae22 100644
--- a/kernel/filesystem/include/filesystem/custody.hpp
+++ b/kernel/filesystem/include/filesystem/custody.hpp
@@ -3,18 +3,20 @@
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
namespace filesystem
{
struct custody
{
- custody(custody * parent, inode * node);
+ custody(kstd::shared_ptr<custody> parent, kstd::shared_ptr<inode> node);
- [[nodiscard]] auto get_inode() const -> inode *;
- [[nodiscard]] auto get_parent() const -> custody *;
+ [[nodiscard]] auto get_inode() const -> kstd::shared_ptr<inode>;
+ [[nodiscard]] auto get_parent() const -> kstd::shared_ptr<custody>;
private:
- custody * m_parent;
- inode * m_inode;
+ kstd::shared_ptr<custody> m_parent;
+ kstd::shared_ptr<inode> m_inode;
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/device_file.hpp b/kernel/filesystem/include/filesystem/device_file.hpp
index 033317b..4dce37f 100644
--- a/kernel/filesystem/include/filesystem/device_file.hpp
+++ b/kernel/filesystem/include/filesystem/device_file.hpp
@@ -5,13 +5,15 @@
#include "devices/device.hpp"
#include "filesystem/file.hpp"
+#include <kstd/memory>
+
#include <cstddef>
namespace filesystem
{
struct device_file : file
{
- explicit device_file(devices::device * device);
+ explicit device_file(kstd::shared_ptr<devices::device> device);
auto open() -> void override;
@@ -23,7 +25,7 @@ namespace filesystem
std::byte * scratch, void * buffer);
auto process_blocks(size_t offset, size_t size, void * buffer, block_op op) const -> size_t;
- devices::device * m_device;
+ kstd::shared_ptr<devices::device> m_device;
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/ext2/ext2_filesystem.hpp b/kernel/filesystem/include/filesystem/ext2/ext2_filesystem.hpp
index cba2192..dd52e72 100644
--- a/kernel/filesystem/include/filesystem/ext2/ext2_filesystem.hpp
+++ b/kernel/filesystem/include/filesystem/ext2/ext2_filesystem.hpp
@@ -5,17 +5,19 @@
#include "filesystem/filesystem.hpp"
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
#include <string_view>
namespace filesystem::ext2
{
struct ext2_filesystem : filesystem
{
- auto mount(devices::device * device) -> int override;
+ auto mount(kstd::shared_ptr<devices::device> device) -> int override;
auto lookup(inode const & parent, std::string_view name) -> inode * override;
private:
- devices::device * m_device{};
+ kstd::shared_ptr<devices::device> m_device{};
};
} // namespace filesystem::ext2
diff --git a/kernel/filesystem/include/filesystem/file_descriptor_table.hpp b/kernel/filesystem/include/filesystem/file_descriptor_table.hpp
index 3ac03d1..6d78532 100644
--- a/kernel/filesystem/include/filesystem/file_descriptor_table.hpp
+++ b/kernel/filesystem/include/filesystem/file_descriptor_table.hpp
@@ -3,7 +3,8 @@
#include "open_file_description.hpp"
-#include <array>
+#include <kstd/vector>
+
#include <optional>
namespace filesystem
@@ -23,8 +24,7 @@ namespace filesystem
file_descriptor_table() = default;
// TODO BA-FS26 use kstd::shared_ptr when available
- // TODO BA-FS26 use kstd::vector when available
- std::array<std::optional<open_file_description>, 32> m_open_files{};
+ kstd::vector<std::optional<open_file_description>> m_open_files{};
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/filesystem.hpp b/kernel/filesystem/include/filesystem/filesystem.hpp
index 8deb336..eabefc0 100644
--- a/kernel/filesystem/include/filesystem/filesystem.hpp
+++ b/kernel/filesystem/include/filesystem/filesystem.hpp
@@ -4,6 +4,8 @@
#include "devices/device.hpp"
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
#include <string_view>
namespace filesystem
@@ -12,11 +14,10 @@ namespace filesystem
{
virtual ~filesystem() = default;
- virtual auto mount(devices::device * device) -> int = 0;
+ virtual auto mount(kstd::shared_ptr<devices::device> device) -> int = 0;
virtual auto lookup(inode const & parent, std::string_view name) -> inode * = 0;
- [[nodiscard]] auto root_inode() -> inode *;
- [[nodiscard]] auto root_inode() const -> inode const *;
+ [[nodiscard]] auto root_inode() const -> inode const &;
protected:
inode m_root_inode{}; // TODO BA-FS26 set during mount?
diff --git a/kernel/filesystem/include/filesystem/inode.hpp b/kernel/filesystem/include/filesystem/inode.hpp
index bceabb6..2b5ee6c 100644
--- a/kernel/filesystem/include/filesystem/inode.hpp
+++ b/kernel/filesystem/include/filesystem/inode.hpp
@@ -4,6 +4,8 @@
#include "devices/device.hpp"
#include "filesystem/inode_metadata.hpp"
+#include <kstd/memory>
+
#include <cstddef>
namespace filesystem
@@ -12,7 +14,7 @@ namespace filesystem
{
inode() = default;
explicit inode(inode_kind kind);
- explicit inode(devices::device * device);
+ explicit inode(kstd::shared_ptr<devices::device> device);
[[nodiscard]] auto metadata() const -> inode_metadata;
@@ -22,14 +24,14 @@ namespace filesystem
[[nodiscard]] auto is_block_device() const -> bool;
[[nodiscard]] auto major_device() const -> size_t;
[[nodiscard]] auto minor_device() const -> size_t;
- [[nodiscard]] auto backing_device() const -> devices::device *;
+ [[nodiscard]] auto backing_device() const -> kstd::shared_ptr<devices::device>;
auto read(void * buffer, size_t offset, size_t size) const -> size_t;
auto write(void const * buffer, size_t offset, size_t size) -> size_t;
private:
inode_kind m_kind{inode_kind::regular};
- devices::device * m_device{};
+ kstd::shared_ptr<devices::device> m_device{};
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/inode_file.hpp b/kernel/filesystem/include/filesystem/inode_file.hpp
index c091280..512f51d 100644
--- a/kernel/filesystem/include/filesystem/inode_file.hpp
+++ b/kernel/filesystem/include/filesystem/inode_file.hpp
@@ -4,13 +4,15 @@
#include "filesystem/file.hpp"
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
#include <cstddef>
namespace filesystem
{
struct inode_file : file
{
- explicit inode_file(inode * inode);
+ explicit inode_file(kstd::shared_ptr<inode> inode);
auto open() -> void override;
@@ -18,7 +20,7 @@ namespace filesystem
auto write(void const * buffer, size_t offset, size_t size) -> size_t override;
private:
- inode * m_inode;
+ kstd::shared_ptr<inode> m_inode;
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/mount.hpp b/kernel/filesystem/include/filesystem/mount.hpp
index fe5d9cc..f28de74 100644
--- a/kernel/filesystem/include/filesystem/mount.hpp
+++ b/kernel/filesystem/include/filesystem/mount.hpp
@@ -3,21 +3,22 @@
#include "filesystem/filesystem.hpp"
+#include <kstd/memory>
+
#include <string_view>
namespace filesystem
{
struct mount
{
- mount() = default; // TODO BA-FS26 remove again when kstd::vector is available and used in vfs
- mount(std::string_view const & path, filesystem * fs);
+ mount(std::string_view const & path, kstd::shared_ptr<filesystem> fs);
[[nodiscard]] auto path() const -> std::string_view;
- [[nodiscard]] auto get_filesystem() const -> filesystem *;
+ [[nodiscard]] auto get_filesystem() const -> kstd::shared_ptr<filesystem>;
private:
std::string_view m_path;
- filesystem * m_filesystem{};
+ kstd::shared_ptr<filesystem> m_filesystem{};
};
} // namespace filesystem
diff --git a/kernel/filesystem/include/filesystem/open_file_description.hpp b/kernel/filesystem/include/filesystem/open_file_description.hpp
index 1589196..035b0ee 100644
--- a/kernel/filesystem/include/filesystem/open_file_description.hpp
+++ b/kernel/filesystem/include/filesystem/open_file_description.hpp
@@ -2,6 +2,7 @@
#define TEACH_OS_KERNEL_FILESYSTEM_OPEN_FILE_DESCRIPTION_HPP
#include "file.hpp"
+#include <kstd/memory>
#include <cstddef>
@@ -9,7 +10,7 @@ namespace filesystem
{
struct open_file_description
{
- open_file_description(file * file);
+ open_file_description(kstd::shared_ptr<file> file);
~open_file_description() = default;
@@ -17,7 +18,7 @@ namespace filesystem
auto write(void const * buffer, size_t size) -> size_t;
private:
- file * m_file;
+ kstd::shared_ptr<file> m_file;
size_t m_offset;
};
diff --git a/kernel/filesystem/include/filesystem/vfs.hpp b/kernel/filesystem/include/filesystem/vfs.hpp
index 1d95d4a..9c89044 100644
--- a/kernel/filesystem/include/filesystem/vfs.hpp
+++ b/kernel/filesystem/include/filesystem/vfs.hpp
@@ -3,11 +3,14 @@
#include "devices/device.hpp"
#include "filesystem/custody.hpp"
+#include "filesystem/ext2/ext2_filesystem.hpp"
#include "filesystem/inode.hpp"
#include "filesystem/mount.hpp"
#include "filesystem/open_file_description.hpp"
-#include <array>
+#include <kstd/memory>
+#include <kstd/vector>
+
#include <optional>
#include <string_view>
@@ -26,17 +29,17 @@ namespace filesystem
struct device_node_entry
{
std::string_view name;
- inode node;
+ kstd::shared_ptr<inode> node;
};
vfs() = default;
- auto make_device_node(devices::device * device) -> void;
+ auto make_device_node(kstd::shared_ptr<devices::device> device) -> void;
[[nodiscard]] auto resolve_path(std::string_view path) -> std::optional<custody>;
+ kstd::shared_ptr<ext2::ext2_filesystem> m_root_fs;
std::optional<mount> m_root_mount;
- std::array<mount, 10> m_mounts; // TODO BA-FS26 remove when kstd::vector is available and used
- std::array<std::optional<device_node_entry>, 10>
- m_device_nodes; // TODO BA-FS26 use kstd::vector // TODO BA-FS26 remove again, use
+ // kstd::vector<mount> m_mounts; // TODO BA-FS26 really needed?
+ kstd::vector<std::optional<device_node_entry>> m_device_nodes; // TODO BA-FS26 remove again, use devtempfs
};
} // namespace filesystem
diff --git a/kernel/filesystem/src/custody.cpp b/kernel/filesystem/src/custody.cpp
index 614e63b..7a58229 100644
--- a/kernel/filesystem/src/custody.cpp
+++ b/kernel/filesystem/src/custody.cpp
@@ -4,9 +4,11 @@
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
namespace filesystem
{
- custody::custody(custody * parent, inode * node)
+ custody::custody(kstd::shared_ptr<custody> parent, kstd::shared_ptr<inode> node)
: m_parent(parent)
, m_inode(node)
{
@@ -16,12 +18,12 @@ namespace filesystem
}
}
- auto custody::get_inode() const -> inode *
+ auto custody::get_inode() const -> kstd::shared_ptr<inode>
{
return m_inode;
}
- auto custody::get_parent() const -> custody *
+ auto custody::get_parent() const -> kstd::shared_ptr<custody>
{
return m_parent;
}
diff --git a/kernel/filesystem/src/device_file.cpp b/kernel/filesystem/src/device_file.cpp
index f11638e..882c9b1 100644
--- a/kernel/filesystem/src/device_file.cpp
+++ b/kernel/filesystem/src/device_file.cpp
@@ -6,14 +6,15 @@
#include "devices/device.hpp"
#include <kstd/cstring>
+#include <kstd/memory>
+#include <kstd/vector>
#include <algorithm>
-#include <array>
#include <cstddef>
namespace filesystem
{
- device_file::device_file(devices::device * device)
+ device_file::device_file(kstd::shared_ptr<devices::device> device)
: m_device(device)
{
if (!m_device)
@@ -90,7 +91,13 @@ namespace filesystem
return 0;
}
- auto * block_dev = static_cast<devices::block_device *>(m_device);
+ // @Felix rtti not activated why? e.g. dynamic_cast does not work, whats with std::dynamic_pointer_cast
+ // online: rtti overhead, bad design, ... ?
+ auto * block_dev = static_cast<devices::block_device *>(m_device.get());
+ if (block_dev == nullptr)
+ {
+ kapi::system::panic("[FILESYSTEM] device_file: expected block_device.");
+ }
size_t const block_size = block_dev->block_size();
size_t const capacity = block_dev->capacity();
@@ -99,8 +106,7 @@ namespace filesystem
return 0;
size_t const total_to_process = std::min(size, capacity - offset);
- // TODO BA-FS26 use kstd::vector when available
- std::array<std::byte, 512> scratch_buffer{}; // TODO BA-FS26 better solution than fixed scratch_buffer ??
+ kstd::vector<std::byte> scratch_buffer{block_size};
auto processed = 0uz;
while (processed < total_to_process)
diff --git a/kernel/filesystem/src/ext2/ext2_filesystem.cpp b/kernel/filesystem/src/ext2/ext2_filesystem.cpp
index f092ddf..2ae5ab7 100644
--- a/kernel/filesystem/src/ext2/ext2_filesystem.cpp
+++ b/kernel/filesystem/src/ext2/ext2_filesystem.cpp
@@ -2,12 +2,15 @@
#include "devices/device.hpp"
#include "filesystem/inode.hpp"
+#include "filesystem/inode_metadata.hpp"
+
+#include <kstd/memory>
#include <string_view>
namespace filesystem::ext2
{
- auto ext2_filesystem::mount(devices::device * device) -> int
+ auto ext2_filesystem::mount(kstd::shared_ptr<devices::device> device) -> int
{
if (!device)
{
diff --git a/kernel/filesystem/src/file_descriptor_table.cpp b/kernel/filesystem/src/file_descriptor_table.cpp
index eb9a281..64fad0c 100644
--- a/kernel/filesystem/src/file_descriptor_table.cpp
+++ b/kernel/filesystem/src/file_descriptor_table.cpp
@@ -44,7 +44,8 @@ namespace filesystem
return static_cast<int>(it - m_open_files.begin());
}
- return -1;
+ m_open_files.push_back(file_description);
+ return static_cast<int>(m_open_files.size() - 1);
}
auto file_descriptor_table::get_file(int fd) const -> std::optional<open_file_description>
@@ -55,12 +56,12 @@ namespace filesystem
}
auto const index = static_cast<size_t>(fd);
- if (index >= m_open_files.size() || !m_open_files[index].has_value())
+ if (index >= m_open_files.size() || !m_open_files.at(fd).has_value())
{
return std::nullopt;
}
- return *m_open_files[index];
+ return m_open_files.at(fd);
}
auto file_descriptor_table::remove_file(int fd) -> void
@@ -76,6 +77,6 @@ namespace filesystem
return;
}
- m_open_files[index].reset();
+ m_open_files.at(fd).reset();
}
} // namespace filesystem \ No newline at end of file
diff --git a/kernel/filesystem/src/filesystem.cpp b/kernel/filesystem/src/filesystem.cpp
index a06bccc..cdfe7f8 100644
--- a/kernel/filesystem/src/filesystem.cpp
+++ b/kernel/filesystem/src/filesystem.cpp
@@ -4,13 +4,8 @@
namespace filesystem
{
- auto filesystem::root_inode() -> inode *
+ auto filesystem::root_inode() const -> inode const &
{
- return &m_root_inode;
- }
-
- auto filesystem::root_inode() const -> inode const *
- {
- return &m_root_inode;
+ return m_root_inode;
}
} // namespace filesystem \ No newline at end of file
diff --git a/kernel/filesystem/src/inode.cpp b/kernel/filesystem/src/inode.cpp
index 17aa52a..10a9a30 100644
--- a/kernel/filesystem/src/inode.cpp
+++ b/kernel/filesystem/src/inode.cpp
@@ -5,6 +5,8 @@
#include "devices/device.hpp"
#include "filesystem/inode_metadata.hpp"
+#include <kstd/memory>
+
#include <cstddef>
namespace filesystem
@@ -13,7 +15,7 @@ namespace filesystem
: m_kind(kind)
{}
- inode::inode(devices::device * device)
+ inode::inode(kstd::shared_ptr<devices::device> device)
: m_kind(inode_kind::device)
, m_device(device)
{
@@ -77,7 +79,7 @@ namespace filesystem
return m_device->minor();
}
- auto inode::backing_device() const -> devices::device *
+ auto inode::backing_device() const -> kstd::shared_ptr<devices::device>
{
return m_device;
}
diff --git a/kernel/filesystem/src/inode_file.cpp b/kernel/filesystem/src/inode_file.cpp
index 071661f..b68b3ee 100644
--- a/kernel/filesystem/src/inode_file.cpp
+++ b/kernel/filesystem/src/inode_file.cpp
@@ -4,11 +4,13 @@
#include "filesystem/inode.hpp"
+#include <kstd/memory>
+
#include <cstddef>
namespace filesystem
{
- inode_file::inode_file(inode * inode)
+ inode_file::inode_file(kstd::shared_ptr<inode> inode)
: m_inode(inode)
{
if (!m_inode)
diff --git a/kernel/filesystem/src/mount.cpp b/kernel/filesystem/src/mount.cpp
index 6594598..53a8143 100644
--- a/kernel/filesystem/src/mount.cpp
+++ b/kernel/filesystem/src/mount.cpp
@@ -4,11 +4,13 @@
#include "filesystem/filesystem.hpp"
+#include <kstd/memory>
+
#include <string_view>
namespace filesystem
{
- mount::mount(std::string_view const & path, filesystem * fs)
+ mount::mount(std::string_view const & path, kstd::shared_ptr<filesystem> fs)
: m_path(path)
, m_filesystem(fs)
{
@@ -23,7 +25,7 @@ namespace filesystem
return m_path;
}
- auto mount::get_filesystem() const -> filesystem *
+ auto mount::get_filesystem() const -> kstd::shared_ptr<filesystem>
{
return m_filesystem;
}
diff --git a/kernel/filesystem/src/open_file_description.cpp b/kernel/filesystem/src/open_file_description.cpp
index d483746..9ebf67d 100644
--- a/kernel/filesystem/src/open_file_description.cpp
+++ b/kernel/filesystem/src/open_file_description.cpp
@@ -2,18 +2,25 @@
#include "filesystem/file.hpp"
+#include <kstd/memory>
+#include <kstd/os/error.hpp>
+
#include <cstddef>
namespace filesystem
{
- open_file_description::open_file_description(file * file)
+ open_file_description::open_file_description(kstd::shared_ptr<file> file)
: m_file(file)
, m_offset(0)
- {}
+ {
+ if (!file)
+ {
+ kstd::os::panic("[FILESYSTEM] open_file_description constructed with null file.");
+ }
+ }
auto open_file_description::read(void * buffer, size_t size) -> size_t
{
- // TODO BA-FS26 nullptr check
auto read_bytes = m_file->read(buffer, m_offset, size);
m_offset += read_bytes;
return read_bytes;
@@ -21,7 +28,6 @@ namespace filesystem
auto open_file_description::write(void const * buffer, size_t size) -> size_t
{
- // TODO BA-FS26 nullptr check
auto written_bytes = m_file->write(buffer, m_offset, size);
m_offset += written_bytes;
return written_bytes;
diff --git a/kernel/filesystem/src/vfs.cpp b/kernel/filesystem/src/vfs.cpp
index 2ecf847..578fde4 100644
--- a/kernel/filesystem/src/vfs.cpp
+++ b/kernel/filesystem/src/vfs.cpp
@@ -13,6 +13,7 @@
#include "filesystem/open_file_description.hpp"
#include <kstd/cstring>
+#include <kstd/memory>
#include <algorithm>
#include <optional>
@@ -23,15 +24,6 @@ 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 remove when dynamic memory available;
- constinit auto static temp_device_file = std::optional<device_file>{};
- // TODO BA-FS26 remove when dynamic memory available;
- constinit auto static temp_inode_file = std::optional<inode_file>{};
} // namespace
auto vfs::init() -> void
@@ -46,13 +38,13 @@ namespace filesystem
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)
+ active_vfs->m_root_fs = kstd::make_shared<ext2::ext2_filesystem>();
+ if (active_vfs->m_root_fs->mount(boot_device) != 0)
{
kapi::system::panic("[FILESYSTEM] Failed to mount root filesystem.");
}
- active_vfs->m_root_mount = mount{"/", &*root_fs};
+ active_vfs->m_root_mount = mount{"/", active_vfs->m_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); });
@@ -78,39 +70,30 @@ namespace filesystem
{
if (auto custody = resolve_path(path))
{
- auto * node = custody->get_inode();
+ auto node = custody->get_inode();
if (node->is_device())
{
- temp_device_file.emplace(node->backing_device());
- temp_device_file->open();
- return open_file_description{&*temp_device_file};
+ auto current_device_file = kstd::make_shared<device_file>(node->backing_device());
+ current_device_file->open();
+ return open_file_description{current_device_file};
}
- temp_inode_file.emplace(node);
- temp_inode_file->open();
- return open_file_description{&*temp_inode_file};
+ auto current_inode_file = kstd::make_shared<inode_file>(node);
+ current_inode_file->open();
+ return open_file_description{current_inode_file};
}
return std::nullopt;
}
- auto vfs::make_device_node(devices::device * device) -> void
+ auto vfs::make_device_node(kstd::shared_ptr<devices::device> device) -> void
{
if (!device)
{
kapi::system::panic("[FILESYSTEM] make_device_node called with null device.");
}
- auto const device_name = device->name();
-
- // TODO BA-FS26 this logic isn't needed anymore when kstd::vector available, just use push_back
- auto const slot = std::ranges::find_if(m_device_nodes, [](auto const & entry) { return !entry.has_value(); });
- if (slot == m_device_nodes.end())
- {
- kapi::system::panic("[FILESYSTEM] No free slot available for device nodes.");
- }
-
- slot->emplace(device_node_entry{device_name, inode{device}});
+ m_device_nodes.push_back(device_node_entry{device->name(), kstd::make_shared<inode>(device)});
}
auto vfs::resolve_path(std::string_view path) -> std::optional<custody>
@@ -128,7 +111,7 @@ namespace filesystem
if (entry != m_device_nodes.end())
{
- return custody{nullptr, &entry->value().node};
+ return custody{static_cast<kstd::shared_ptr<custody>>(nullptr), entry->value().node};
}
return std::nullopt;
diff --git a/kernel/src/main.cpp b/kernel/src/main.cpp
index 011821a..cf3afdf 100644
--- a/kernel/src/main.cpp
+++ b/kernel/src/main.cpp
@@ -8,13 +8,13 @@
#include "filesystem/file_descriptor_table.hpp"
#include "filesystem/open_file_description.hpp"
#include "filesystem/vfs.hpp"
-
#include "kernel/memory.hpp"
+#include <kstd/memory>
#include <kstd/os/error.hpp>
#include <kstd/print>
+#include <kstd/vector>
-#include <array>
#include <cstddef>
#include <cstdint>
@@ -25,8 +25,8 @@ auto run_test_code() -> void
auto storage_mgmt = devices::storage::storage_management::get();
auto device = storage_mgmt.device_by_major_minor(1, 0);
- filesystem::device_file dev_file(device);
- filesystem::open_file_description ofd(&dev_file);
+ auto dev_file = kstd::make_shared<filesystem::device_file>(device);
+ filesystem::open_file_description ofd(dev_file);
auto fd_index = fd_table.add_file(ofd);
// use: read two bytes and write two again
@@ -36,7 +36,7 @@ auto run_test_code() -> void
kstd::os::panic("test code failed");
}
- std::array<std::byte, 2> buffer{};
+ kstd::vector<std::byte> buffer{2};
auto number_of_read_bytes = fd->read(buffer.data(), buffer.size());
for (size_t i = 0; i < number_of_read_bytes; ++i)
@@ -51,7 +51,7 @@ auto run_test_code() -> void
kstd::println("---");
// write half of the file new
- std::array<std::byte, 2> write_buffer{std::byte{0xBB}, std::byte{0xAA}};
+ kstd::vector<std::byte> write_buffer{std::byte{0xBB}, std::byte{0xAA}};
auto written_bytes = fd->write(write_buffer.data(), write_buffer.size());
kstd::println("written bytes: {}", written_bytes);
@@ -59,7 +59,7 @@ auto run_test_code() -> void
fd_table.remove_file(fd_index);
// use: read four bytes again -> two old bytes two new bytes
- filesystem::open_file_description ofd1(&dev_file);
+ filesystem::open_file_description ofd1(dev_file);
fd_index = fd_table.add_file(ofd1);
auto fd1 = fd_table.get_file(fd_index);
@@ -68,7 +68,7 @@ auto run_test_code() -> void
kstd::os::panic("test code failed");
}
- std::array<std::byte, 4> buffer1{};
+ kstd::vector<std::byte> buffer1{4};
number_of_read_bytes = fd1->read(buffer1.data(), buffer1.size());
for (size_t i = 0; i < number_of_read_bytes; ++i)
@@ -89,7 +89,8 @@ auto main() -> int
kstd::println("[OS] IO subsystem initialized.");
kapi::memory::init();
- kernel::memory::init_heap(kapi::memory::heap_base); kstd::println("[OS] Memory subsystem initialized.");
+ kernel::memory::init_heap(kapi::memory::heap_base);
+ kstd::println("[OS] Memory subsystem initialized.");
kapi::system::memory_initialized();
kapi::boot_modules::init();