Browse Source

Kernel/SysFS: Migrate components code from SysFS.cpp to the SysFS folder

Liav A 3 years ago
parent
commit
23c1c40e86
29 changed files with 496 additions and 286 deletions
  1. 7 0
      Kernel/CMakeLists.txt
  2. 0 101
      Kernel/Devices/Device.cpp
  3. 2 1
      Kernel/Devices/Device.h
  4. 1 80
      Kernel/FileSystem/SysFS.cpp
  5. 0 99
      Kernel/FileSystem/SysFS.h
  6. 1 0
      Kernel/FileSystem/SysFS/Component.cpp
  7. 54 0
      Kernel/FileSystem/SysFS/Registry.cpp
  8. 43 0
      Kernel/FileSystem/SysFS/Registry.h
  9. 42 0
      Kernel/FileSystem/SysFS/RootDirectory.cpp
  10. 27 0
      Kernel/FileSystem/SysFS/RootDirectory.h
  11. 24 0
      Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.cpp
  12. 25 0
      Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.h
  13. 1 0
      Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp
  14. 1 1
      Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h
  15. 1 1
      Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h
  16. 1 1
      Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h
  17. 1 0
      Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
  18. 1 1
      Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h
  19. 1 1
      Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h
  20. 51 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.cpp
  21. 25 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.h
  22. 50 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.cpp
  23. 25 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.h
  24. 27 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.cpp
  25. 33 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.h
  26. 27 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.cpp
  27. 23 0
      Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h
  28. 1 0
      Kernel/FileSystem/SysFS/Subsystems/Firmware/Directory.cpp
  29. 1 0
      Kernel/Forward.h

+ 7 - 0
Kernel/CMakeLists.txt

@@ -143,11 +143,18 @@ set(KERNEL_SOURCES
     FileSystem/ProcFS.cpp
     FileSystem/SysFS.cpp
     FileSystem/SysFS/Component.cpp
+    FileSystem/SysFS/Registry.cpp
+    FileSystem/SysFS/RootDirectory.cpp
     FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp
     FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.cpp
     FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.cpp
     FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp
     FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.cpp
+    FileSystem/SysFS/Subsystems/Bus/Directory.cpp
+    FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.cpp
+    FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.cpp
+    FileSystem/SysFS/Subsystems/Devices/DeviceComponent.cpp
+    FileSystem/SysFS/Subsystems/Devices/Directory.cpp
     FileSystem/SysFS/Subsystems/Firmware/BIOS/DMI/EntryPointBlob.cpp
     FileSystem/SysFS/Subsystems/Firmware/BIOS/DMI/Table.cpp
     FileSystem/SysFS/Subsystems/Firmware/BIOS/Component.cpp

+ 0 - 101
Kernel/Devices/Device.cpp

@@ -13,107 +13,6 @@
 
 namespace Kernel {
 
-NonnullRefPtr<SysFSDeviceComponent> SysFSDeviceComponent::must_create(Device const& device)
-{
-    // FIXME: Handle allocation failure gracefully
-    auto device_name = MUST(KString::formatted("{}:{}", device.major(), device.minor()));
-    return adopt_ref_if_nonnull(new SysFSDeviceComponent(move(device_name), device)).release_nonnull();
-}
-SysFSDeviceComponent::SysFSDeviceComponent(NonnullOwnPtr<KString> major_minor_formatted_device_name, Device const& device)
-    : SysFSComponent()
-    , m_block_device(device.is_block_device())
-    , m_major_minor_formatted_device_name(move(major_minor_formatted_device_name))
-{
-    VERIFY(device.is_block_device() || device.is_character_device());
-}
-
-UNMAP_AFTER_INIT NonnullRefPtr<SysFSDevicesDirectory> SysFSDevicesDirectory::must_create(SysFSRootDirectory const& root_directory)
-{
-    auto devices_directory = adopt_ref_if_nonnull(new SysFSDevicesDirectory(root_directory)).release_nonnull();
-    devices_directory->m_components.append(SysFSBlockDevicesDirectory::must_create(*devices_directory));
-    devices_directory->m_components.append(SysFSCharacterDevicesDirectory::must_create(*devices_directory));
-    return devices_directory;
-}
-SysFSDevicesDirectory::SysFSDevicesDirectory(SysFSRootDirectory const& root_directory)
-    : SysFSDirectory(root_directory)
-{
-}
-
-NonnullRefPtr<SysFSBlockDevicesDirectory> SysFSBlockDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
-{
-    return adopt_ref_if_nonnull(new SysFSBlockDevicesDirectory(devices_directory)).release_nonnull();
-}
-SysFSBlockDevicesDirectory::SysFSBlockDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
-    : SysFSDirectory(devices_directory)
-{
-}
-
-ErrorOr<void> SysFSBlockDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
-{
-    VERIFY(m_parent_directory);
-    TRY(callback({ ".", { fsid, component_index() }, 0 }));
-    TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
-
-    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> ErrorOr<void> {
-        for (auto& exposed_device : list) {
-            if (!exposed_device.is_block_device())
-                continue;
-            TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
-        }
-        return {};
-    });
-}
-
-RefPtr<SysFSComponent> SysFSBlockDevicesDirectory::lookup(StringView name)
-{
-    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
-        for (auto& exposed_device : list) {
-            if (!exposed_device.is_block_device())
-                continue;
-            if (exposed_device.name() == name)
-                return exposed_device;
-        }
-        return nullptr;
-    });
-}
-
-NonnullRefPtr<SysFSCharacterDevicesDirectory> SysFSCharacterDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
-{
-    return adopt_ref_if_nonnull(new SysFSCharacterDevicesDirectory(devices_directory)).release_nonnull();
-}
-SysFSCharacterDevicesDirectory::SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
-    : SysFSDirectory(devices_directory)
-{
-}
-ErrorOr<void> SysFSCharacterDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
-{
-    VERIFY(m_parent_directory);
-    TRY(callback({ ".", { fsid, component_index() }, 0 }));
-    TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
-
-    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> ErrorOr<void> {
-        for (auto& exposed_device : list) {
-            if (exposed_device.is_block_device())
-                continue;
-            TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
-        }
-        return {};
-    });
-}
-
-RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
-{
-    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
-        for (auto& exposed_device : list) {
-            if (exposed_device.is_block_device())
-                continue;
-            if (exposed_device.name() == name)
-                return exposed_device;
-        }
-        return nullptr;
-    });
-}
-
 Device::Device(MajorNumber major, MinorNumber minor)
     : m_major(major)
     , m_minor(minor)

+ 2 - 1
Kernel/Devices/Device.h

@@ -22,7 +22,8 @@
 #include <Kernel/Devices/AsyncDeviceRequest.h>
 #include <Kernel/FileSystem/DeviceFileTypes.h>
 #include <Kernel/FileSystem/File.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Registry.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.h>
 #include <Kernel/UnixTypes.h>
 
 namespace Kernel {

+ 1 - 80
Kernel/FileSystem/SysFS.cpp

@@ -4,70 +4,13 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
-#include <AK/Singleton.h>
 #include <AK/StringView.h>
-#include <Kernel/Devices/Device.h>
 #include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Registry.h>
 #include <Kernel/Sections.h>
 
 namespace Kernel {
 
-static Singleton<SysFSComponentRegistry> s_the;
-
-SysFSComponentRegistry& SysFSComponentRegistry::the()
-{
-    return *s_the;
-}
-
-UNMAP_AFTER_INIT void SysFSComponentRegistry::initialize()
-{
-    VERIFY(!s_the.is_initialized());
-    s_the.ensure_instance();
-}
-
-UNMAP_AFTER_INIT SysFSComponentRegistry::SysFSComponentRegistry()
-    : m_root_directory(SysFSRootDirectory::create())
-{
-}
-
-UNMAP_AFTER_INIT void SysFSComponentRegistry::register_new_component(SysFSComponent& component)
-{
-    MutexLocker locker(m_lock);
-    m_root_directory->m_components.append(component);
-}
-
-SysFSComponentRegistry::DevicesList& SysFSComponentRegistry::devices_list()
-{
-    return m_devices_list;
-}
-
-NonnullRefPtr<SysFSRootDirectory> SysFSRootDirectory::create()
-{
-    return adopt_ref(*new (nothrow) SysFSRootDirectory);
-}
-
-ErrorOr<void> SysFSRootDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
-{
-    MutexLocker locker(SysFSComponentRegistry::the().get_lock());
-    TRY(callback({ ".", { fsid, component_index() }, 0 }));
-    TRY(callback({ "..", { fsid, 0 }, 0 }));
-
-    for (auto const& component : m_components) {
-        InodeIdentifier identifier = { fsid, component.component_index() };
-        TRY(callback({ component.name(), identifier, 0 }));
-    }
-    return {};
-}
-
-SysFSRootDirectory::SysFSRootDirectory()
-{
-    auto buses_directory = SysFSBusDirectory::must_create(*this);
-    auto devices_directory = SysFSDevicesDirectory::must_create(*this);
-    m_components.append(buses_directory);
-    m_components.append(devices_directory);
-    m_buses_directory = buses_directory;
-}
-
 ErrorOr<NonnullRefPtr<FileSystem>> SysFS::try_create()
 {
     return TRY(adopt_nonnull_ref_or_enomem(new (nothrow) SysFS));
@@ -226,26 +169,4 @@ ErrorOr<NonnullRefPtr<Inode>> SysFSDirectoryInode::lookup(StringView name)
     return TRY(component->to_inode(fs()));
 }
 
-SysFSBusDirectory& SysFSComponentRegistry::buses_directory()
-{
-    return *m_root_directory->m_buses_directory;
-}
-
-void SysFSComponentRegistry::register_new_bus_directory(SysFSDirectory& new_bus_directory)
-{
-    VERIFY(!m_root_directory->m_buses_directory.is_null());
-    m_root_directory->m_buses_directory->m_components.append(new_bus_directory);
-}
-
-UNMAP_AFTER_INIT NonnullRefPtr<SysFSBusDirectory> SysFSBusDirectory::must_create(SysFSRootDirectory const& parent_directory)
-{
-    auto directory = adopt_ref(*new (nothrow) SysFSBusDirectory(parent_directory));
-    return directory;
-}
-
-UNMAP_AFTER_INIT SysFSBusDirectory::SysFSBusDirectory(SysFSRootDirectory const& parent_directory)
-    : SysFSDirectory(parent_directory)
-{
-}
-
 }

+ 0 - 99
Kernel/FileSystem/SysFS.h

@@ -13,105 +13,6 @@
 
 namespace Kernel {
 
-class SysFSDevicesDirectory;
-class SysFSRootDirectory final : public SysFSDirectory {
-    friend class SysFSComponentRegistry;
-
-public:
-    virtual StringView name() const override { return "."sv; }
-    static NonnullRefPtr<SysFSRootDirectory> create();
-    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
-
-private:
-    SysFSRootDirectory();
-    RefPtr<SysFSBusDirectory> m_buses_directory;
-};
-
-class SysFSDeviceComponent final
-    : public SysFSComponent
-    , public Weakable<SysFSDeviceComponent> {
-    friend class SysFSComponentRegistry;
-
-public:
-    static NonnullRefPtr<SysFSDeviceComponent> must_create(Device const&);
-    virtual StringView name() const override { return m_major_minor_formatted_device_name->view(); }
-    bool is_block_device() const { return m_block_device; }
-
-private:
-    SysFSDeviceComponent(NonnullOwnPtr<KString> major_minor_formatted_device_name, Device const&);
-    IntrusiveListNode<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>> m_list_node;
-    bool m_block_device;
-
-    NonnullOwnPtr<KString> m_major_minor_formatted_device_name;
-};
-
-class SysFSDevicesDirectory final : public SysFSDirectory {
-public:
-    virtual StringView name() const override { return "dev"sv; }
-    static NonnullRefPtr<SysFSDevicesDirectory> must_create(SysFSRootDirectory const&);
-
-private:
-    explicit SysFSDevicesDirectory(SysFSRootDirectory const&);
-};
-
-class SysFSBlockDevicesDirectory final : public SysFSDirectory {
-public:
-    virtual StringView name() const override { return "block"sv; }
-    static NonnullRefPtr<SysFSBlockDevicesDirectory> must_create(SysFSDevicesDirectory const&);
-    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
-    virtual RefPtr<SysFSComponent> lookup(StringView name) override;
-
-private:
-    explicit SysFSBlockDevicesDirectory(SysFSDevicesDirectory const&);
-};
-
-class SysFSCharacterDevicesDirectory final : public SysFSDirectory {
-public:
-    virtual StringView name() const override { return "char"sv; }
-    static NonnullRefPtr<SysFSCharacterDevicesDirectory> must_create(SysFSDevicesDirectory const&);
-    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
-    virtual RefPtr<SysFSComponent> lookup(StringView name) override;
-
-private:
-    explicit SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const&);
-};
-
-class SysFSBusDirectory : public SysFSDirectory {
-    friend class SysFSComponentRegistry;
-
-public:
-    virtual StringView name() const override { return "bus"sv; }
-    static NonnullRefPtr<SysFSBusDirectory> must_create(SysFSRootDirectory const&);
-
-private:
-    explicit SysFSBusDirectory(SysFSRootDirectory const&);
-};
-
-class SysFSComponentRegistry {
-    using DevicesList = MutexProtected<IntrusiveList<&SysFSDeviceComponent::m_list_node>>;
-
-public:
-    static SysFSComponentRegistry& the();
-
-    static void initialize();
-
-    SysFSComponentRegistry();
-    void register_new_component(SysFSComponent&);
-
-    SysFSDirectory& root_directory() { return m_root_directory; }
-    Mutex& get_lock() { return m_lock; }
-
-    void register_new_bus_directory(SysFSDirectory&);
-    SysFSBusDirectory& buses_directory();
-
-    DevicesList& devices_list();
-
-private:
-    Mutex m_lock;
-    NonnullRefPtr<SysFSRootDirectory> m_root_directory;
-    DevicesList m_devices_list;
-};
-
 class SysFS final : public FileSystem {
     friend class SysFSInode;
     friend class SysFSDirectoryInode;

+ 1 - 0
Kernel/FileSystem/SysFS/Component.cpp

@@ -6,6 +6,7 @@
 
 #include <Kernel/FileSystem/SysFS.h>
 #include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/FileSystem/SysFS/Registry.h>
 
 namespace Kernel {
 

+ 54 - 0
Kernel/FileSystem/SysFS/Registry.cpp

@@ -0,0 +1,54 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Singleton.h>
+#include <AK/StringView.h>
+#include <Kernel/FileSystem/SysFS/Registry.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+static Singleton<SysFSComponentRegistry> s_the;
+
+SysFSComponentRegistry& SysFSComponentRegistry::the()
+{
+    return *s_the;
+}
+
+UNMAP_AFTER_INIT void SysFSComponentRegistry::initialize()
+{
+    VERIFY(!s_the.is_initialized());
+    s_the.ensure_instance();
+}
+
+UNMAP_AFTER_INIT SysFSComponentRegistry::SysFSComponentRegistry()
+    : m_root_directory(SysFSRootDirectory::create())
+{
+}
+
+UNMAP_AFTER_INIT void SysFSComponentRegistry::register_new_component(SysFSComponent& component)
+{
+    MutexLocker locker(m_lock);
+    m_root_directory->m_components.append(component);
+}
+
+SysFSComponentRegistry::DevicesList& SysFSComponentRegistry::devices_list()
+{
+    return m_devices_list;
+}
+
+SysFSBusDirectory& SysFSComponentRegistry::buses_directory()
+{
+    return *m_root_directory->m_buses_directory;
+}
+
+void SysFSComponentRegistry::register_new_bus_directory(SysFSDirectory& new_bus_directory)
+{
+    VERIFY(!m_root_directory->m_buses_directory.is_null());
+    m_root_directory->m_buses_directory->m_components.append(new_bus_directory);
+}
+
+}

+ 43 - 0
Kernel/FileSystem/SysFS/Registry.h

@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/FileSystem.h>
+#include <Kernel/FileSystem/Inode.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/FileSystem/SysFS/RootDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.h>
+#include <Kernel/Locking/MutexProtected.h>
+
+namespace Kernel {
+
+class SysFSComponentRegistry {
+    using DevicesList = MutexProtected<IntrusiveList<&SysFSDeviceComponent::m_list_node>>;
+
+public:
+    static SysFSComponentRegistry& the();
+
+    static void initialize();
+
+    SysFSComponentRegistry();
+    void register_new_component(SysFSComponent&);
+
+    SysFSDirectory& root_directory() { return m_root_directory; }
+    Mutex& get_lock() { return m_lock; }
+
+    void register_new_bus_directory(SysFSDirectory&);
+    SysFSBusDirectory& buses_directory();
+
+    DevicesList& devices_list();
+
+private:
+    Mutex m_lock;
+    NonnullRefPtr<SysFSRootDirectory> m_root_directory;
+    DevicesList m_devices_list;
+};
+
+}

+ 42 - 0
Kernel/FileSystem/SysFS/RootDirectory.cpp

@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/FileSystem/SysFS/Registry.h>
+#include <Kernel/FileSystem/SysFS/RootDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+NonnullRefPtr<SysFSRootDirectory> SysFSRootDirectory::create()
+{
+    return adopt_ref(*new (nothrow) SysFSRootDirectory);
+}
+
+ErrorOr<void> SysFSRootDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
+{
+    MutexLocker locker(SysFSComponentRegistry::the().get_lock());
+    TRY(callback({ ".", { fsid, component_index() }, 0 }));
+    TRY(callback({ "..", { fsid, 0 }, 0 }));
+
+    for (auto const& component : m_components) {
+        InodeIdentifier identifier = { fsid, component.component_index() };
+        TRY(callback({ component.name(), identifier, 0 }));
+    }
+    return {};
+}
+
+SysFSRootDirectory::SysFSRootDirectory()
+{
+    auto buses_directory = SysFSBusDirectory::must_create(*this);
+    auto devices_directory = SysFSDevicesDirectory::must_create(*this);
+    m_components.append(buses_directory);
+    m_components.append(devices_directory);
+    m_buses_directory = buses_directory;
+}
+
+}

+ 27 - 0
Kernel/FileSystem/SysFS/RootDirectory.h

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.h>
+
+namespace Kernel {
+
+class SysFSRootDirectory final : public SysFSDirectory {
+    friend class SysFSComponentRegistry;
+
+public:
+    virtual StringView name() const override { return "."sv; }
+    static NonnullRefPtr<SysFSRootDirectory> create();
+    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
+
+private:
+    SysFSRootDirectory();
+    RefPtr<SysFSBusDirectory> m_buses_directory;
+};
+
+}

+ 24 - 0
Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.cpp

@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/FileSystem/SysFS/RootDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+UNMAP_AFTER_INIT NonnullRefPtr<SysFSBusDirectory> SysFSBusDirectory::must_create(SysFSRootDirectory const& parent_directory)
+{
+    auto directory = adopt_ref(*new (nothrow) SysFSBusDirectory(parent_directory));
+    return directory;
+}
+
+UNMAP_AFTER_INIT SysFSBusDirectory::SysFSBusDirectory(SysFSRootDirectory const& parent_directory)
+    : SysFSDirectory(parent_directory)
+{
+}
+
+}

+ 25 - 0
Kernel/FileSystem/SysFS/Subsystems/Bus/Directory.h

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/Forward.h>
+
+namespace Kernel {
+
+class SysFSBusDirectory : public SysFSDirectory {
+    friend class SysFSComponentRegistry;
+
+public:
+    virtual StringView name() const override { return "bus"sv; }
+    static NonnullRefPtr<SysFSBusDirectory> must_create(SysFSRootDirectory const&);
+
+private:
+    explicit SysFSBusDirectory(SysFSRootDirectory const&);
+};
+
+}

+ 1 - 0
Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.cpp

@@ -7,6 +7,7 @@
 #include <Kernel/Bus/PCI/API.h>
 #include <Kernel/Bus/PCI/Access.h>
 #include <Kernel/Debug.h>
+#include <Kernel/FileSystem/SysFS/Registry.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h>
 #include <Kernel/Sections.h>

+ 1 - 1
Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/BusDirectory.h

@@ -7,7 +7,7 @@
 #pragma once
 
 #include <Kernel/Bus/PCI/Definitions.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
 
 namespace Kernel {
 

+ 1 - 1
Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceAttribute.h

@@ -8,7 +8,7 @@
 
 #include <AK/Vector.h>
 #include <Kernel/Bus/PCI/Definitions.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h>
 
 namespace Kernel {

+ 1 - 1
Kernel/FileSystem/SysFS/Subsystems/Bus/PCI/DeviceDirectory.h

@@ -7,7 +7,7 @@
 #pragma once
 
 #include <Kernel/Bus/PCI/Definitions.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
 #include <Kernel/KString.h>
 
 namespace Kernel {

+ 1 - 0
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.cpp

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <Kernel/FileSystem/SysFS/Registry.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h>
 #include <Kernel/KBufferBuilder.h>
 

+ 1 - 1
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/BusDirectory.h

@@ -7,7 +7,7 @@
 #pragma once
 
 #include <Kernel/Bus/USB/USBDevice.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h>
 #include <Kernel/Locking/Spinlock.h>
 

+ 1 - 1
Kernel/FileSystem/SysFS/Subsystems/Bus/USB/DeviceInformation.h

@@ -7,7 +7,7 @@
 #pragma once
 
 #include <Kernel/Bus/USB/USBDevice.h>
-#include <Kernel/FileSystem/SysFS.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
 #include <Kernel/KBufferBuilder.h>
 #include <Kernel/KString.h>
 #include <Kernel/Locking/Mutex.h>

+ 51 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.cpp

@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/FileSystem/SysFS/Registry.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+NonnullRefPtr<SysFSBlockDevicesDirectory> SysFSBlockDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
+{
+    return adopt_ref_if_nonnull(new SysFSBlockDevicesDirectory(devices_directory)).release_nonnull();
+}
+SysFSBlockDevicesDirectory::SysFSBlockDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
+    : SysFSDirectory(devices_directory)
+{
+}
+
+ErrorOr<void> SysFSBlockDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
+{
+    VERIFY(m_parent_directory);
+    TRY(callback({ ".", { fsid, component_index() }, 0 }));
+    TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
+
+    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> ErrorOr<void> {
+        for (auto& exposed_device : list) {
+            if (!exposed_device.is_block_device())
+                continue;
+            TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
+        }
+        return {};
+    });
+}
+
+RefPtr<SysFSComponent> SysFSBlockDevicesDirectory::lookup(StringView name)
+{
+    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
+        for (auto& exposed_device : list) {
+            if (!exposed_device.is_block_device())
+                continue;
+            if (exposed_device.name() == name)
+                return exposed_device;
+        }
+        return nullptr;
+    });
+}
+
+}

+ 25 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.h

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h>
+
+namespace Kernel {
+
+class SysFSBlockDevicesDirectory final : public SysFSDirectory {
+public:
+    virtual StringView name() const override { return "block"sv; }
+    static NonnullRefPtr<SysFSBlockDevicesDirectory> must_create(SysFSDevicesDirectory const&);
+    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
+    virtual RefPtr<SysFSComponent> lookup(StringView name) override;
+
+private:
+    explicit SysFSBlockDevicesDirectory(SysFSDevicesDirectory const&);
+};
+
+}

+ 50 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.cpp

@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/FileSystem/SysFS/Registry.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+NonnullRefPtr<SysFSCharacterDevicesDirectory> SysFSCharacterDevicesDirectory::must_create(SysFSDevicesDirectory const& devices_directory)
+{
+    return adopt_ref_if_nonnull(new SysFSCharacterDevicesDirectory(devices_directory)).release_nonnull();
+}
+SysFSCharacterDevicesDirectory::SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const& devices_directory)
+    : SysFSDirectory(devices_directory)
+{
+}
+ErrorOr<void> SysFSCharacterDevicesDirectory::traverse_as_directory(FileSystemID fsid, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
+{
+    VERIFY(m_parent_directory);
+    TRY(callback({ ".", { fsid, component_index() }, 0 }));
+    TRY(callback({ "..", { fsid, m_parent_directory->component_index() }, 0 }));
+
+    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> ErrorOr<void> {
+        for (auto& exposed_device : list) {
+            if (exposed_device.is_block_device())
+                continue;
+            TRY(callback({ exposed_device.name(), { fsid, exposed_device.component_index() }, 0 }));
+        }
+        return {};
+    });
+}
+
+RefPtr<SysFSComponent> SysFSCharacterDevicesDirectory::lookup(StringView name)
+{
+    return SysFSComponentRegistry::the().devices_list().with_exclusive([&](auto& list) -> RefPtr<SysFSComponent> {
+        for (auto& exposed_device : list) {
+            if (exposed_device.is_block_device())
+                continue;
+            if (exposed_device.name() == name)
+                return exposed_device;
+        }
+        return nullptr;
+    });
+}
+
+}

+ 25 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.h

@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h>
+
+namespace Kernel {
+
+class SysFSCharacterDevicesDirectory final : public SysFSDirectory {
+public:
+    virtual StringView name() const override { return "char"sv; }
+    static NonnullRefPtr<SysFSCharacterDevicesDirectory> must_create(SysFSDevicesDirectory const&);
+    virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override;
+    virtual RefPtr<SysFSComponent> lookup(StringView name) override;
+
+private:
+    explicit SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const&);
+};
+
+}

+ 27 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.cpp

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/Devices/Device.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+NonnullRefPtr<SysFSDeviceComponent> SysFSDeviceComponent::must_create(Device const& device)
+{
+    // FIXME: Handle allocation failure gracefully
+    auto device_name = MUST(KString::formatted("{}:{}", device.major(), device.minor()));
+    return adopt_ref_if_nonnull(new SysFSDeviceComponent(move(device_name), device)).release_nonnull();
+}
+SysFSDeviceComponent::SysFSDeviceComponent(NonnullOwnPtr<KString> major_minor_formatted_device_name, Device const& device)
+    : SysFSComponent()
+    , m_block_device(device.is_block_device())
+    , m_major_minor_formatted_device_name(move(major_minor_formatted_device_name))
+{
+    VERIFY(device.is_block_device() || device.is_character_device());
+}
+
+}

+ 33 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/DeviceComponent.h

@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/IntrusiveList.h>
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/KString.h>
+
+namespace Kernel {
+
+class SysFSDeviceComponent final
+    : public SysFSComponent
+    , public Weakable<SysFSDeviceComponent> {
+    friend class SysFSComponentRegistry;
+
+public:
+    static NonnullRefPtr<SysFSDeviceComponent> must_create(Device const&);
+    virtual StringView name() const override { return m_major_minor_formatted_device_name->view(); }
+    bool is_block_device() const { return m_block_device; }
+
+private:
+    SysFSDeviceComponent(NonnullOwnPtr<KString> major_minor_formatted_device_name, Device const&);
+    IntrusiveListNode<SysFSDeviceComponent, NonnullRefPtr<SysFSDeviceComponent>> m_list_node;
+    bool m_block_device;
+
+    NonnullOwnPtr<KString> m_major_minor_formatted_device_name;
+};
+
+}

+ 27 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.cpp

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <Kernel/FileSystem/SysFS/RootDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/BlockDevicesDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/CharacterDevicesDirectory.h>
+#include <Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h>
+#include <Kernel/Sections.h>
+
+namespace Kernel {
+
+UNMAP_AFTER_INIT NonnullRefPtr<SysFSDevicesDirectory> SysFSDevicesDirectory::must_create(SysFSRootDirectory const& root_directory)
+{
+    auto devices_directory = adopt_ref_if_nonnull(new SysFSDevicesDirectory(root_directory)).release_nonnull();
+    devices_directory->m_components.append(SysFSBlockDevicesDirectory::must_create(*devices_directory));
+    devices_directory->m_components.append(SysFSCharacterDevicesDirectory::must_create(*devices_directory));
+    return devices_directory;
+}
+SysFSDevicesDirectory::SysFSDevicesDirectory(SysFSRootDirectory const& root_directory)
+    : SysFSDirectory(root_directory)
+{
+}
+
+}

+ 23 - 0
Kernel/FileSystem/SysFS/Subsystems/Devices/Directory.h

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <Kernel/FileSystem/SysFS/Component.h>
+#include <Kernel/Forward.h>
+
+namespace Kernel {
+
+class SysFSDevicesDirectory final : public SysFSDirectory {
+public:
+    virtual StringView name() const override { return "dev"sv; }
+    static NonnullRefPtr<SysFSDevicesDirectory> must_create(SysFSRootDirectory const&);
+
+private:
+    explicit SysFSDevicesDirectory(SysFSRootDirectory const&);
+};
+
+}

+ 1 - 0
Kernel/FileSystem/SysFS/Subsystems/Firmware/Directory.cpp

@@ -4,6 +4,7 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <Kernel/FileSystem/SysFS/Registry.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Firmware/BIOS/Directory.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Firmware/Directory.h>
 #include <Kernel/FileSystem/SysFS/Subsystems/Firmware/PowerStateSwitch.h>

+ 1 - 0
Kernel/Forward.h

@@ -54,6 +54,7 @@ class Scheduler;
 class Socket;
 class SysFS;
 class SysFSDirectory;
+class SysFSRootDirectory;
 class SysFSBusDirectory;
 class SysFSDirectoryInode;
 class SysFSInode;