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

This commit is contained in:
Liav A 2022-04-22 12:46:19 +03:00 committed by Andreas Kling
parent 4d05a41b30
commit 23c1c40e86
29 changed files with 496 additions and 286 deletions

View file

@ -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

View file

@ -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)

View file

@ -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 {

View file

@ -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)
{
}
}

View file

@ -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;

View file

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

View file

@ -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);
}
}

View file

@ -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;
};
}

View file

@ -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;
}
}

View file

@ -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;
};
}

View file

@ -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)
{
}
}

View file

@ -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&);
};
}

View file

@ -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>

View file

@ -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 {

View file

@ -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 {

View file

@ -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 {

View file

@ -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>

View file

@ -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>

View file

@ -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>

View file

@ -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;
});
}
}

View file

@ -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&);
};
}

View file

@ -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;
});
}
}

View file

@ -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&);
};
}

View file

@ -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());
}
}

View file

@ -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;
};
}

View file

@ -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)
{
}
}

View file

@ -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&);
};
}

View file

@ -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>

View file

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