/* * Copyright (c) 2021, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include namespace Kernel { class SysFSDevicesDirectory; class SysFSRootDirectory final : public SysFSDirectory { friend class SysFSComponentRegistry; public: static NonnullRefPtr create(); virtual ErrorOr traverse_as_directory(unsigned, Function(FileSystem::DirectoryEntryView const&)>) const override; private: SysFSRootDirectory(); RefPtr m_buses_directory; }; class SysFSDeviceComponent final : public SysFSComponent , public Weakable { friend class SysFSComponentRegistry; public: static NonnullRefPtr must_create(Device const&); bool is_block_device() const { return m_block_device; } private: explicit SysFSDeviceComponent(Device const&); IntrusiveListNode> m_list_node; bool m_block_device; }; class SysFSDevicesDirectory final : public SysFSDirectory { public: static NonnullRefPtr must_create(SysFSRootDirectory const&); private: explicit SysFSDevicesDirectory(SysFSRootDirectory const&); }; class SysFSBlockDevicesDirectory final : public SysFSDirectory { public: static NonnullRefPtr must_create(SysFSDevicesDirectory const&); virtual ErrorOr traverse_as_directory(unsigned, Function(FileSystem::DirectoryEntryView const&)>) const override; virtual RefPtr lookup(StringView name) override; private: explicit SysFSBlockDevicesDirectory(SysFSDevicesDirectory const&); }; class SysFSCharacterDevicesDirectory final : public SysFSDirectory { public: static NonnullRefPtr must_create(SysFSDevicesDirectory const&); virtual ErrorOr traverse_as_directory(unsigned, Function(FileSystem::DirectoryEntryView const&)>) const override; virtual RefPtr lookup(StringView name) override; private: explicit SysFSCharacterDevicesDirectory(SysFSDevicesDirectory const&); }; class SysFSBusDirectory : public SysFSDirectory { friend class SysFSComponentRegistry; public: static NonnullRefPtr must_create(SysFSRootDirectory const&); private: explicit SysFSBusDirectory(SysFSRootDirectory const&); }; class SysFSComponentRegistry { using DevicesList = MutexProtected>; 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 m_root_directory; DevicesList m_devices_list; }; class SysFS final : public FileSystem { friend class SysFSInode; friend class SysFSDirectoryInode; public: virtual ~SysFS() override; static ErrorOr> try_create(); virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "SysFS"sv; } virtual Inode& root_inode() override; private: SysFS(); RefPtr m_root_inode; }; class SysFSInode : public Inode { friend class SysFS; friend class SysFSDirectoryInode; public: static ErrorOr> try_create(SysFS const&, SysFSComponent const&); StringView name() const { return m_associated_component->name(); } protected: SysFSInode(SysFS const&, SysFSComponent const&); virtual ErrorOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual ErrorOr traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const override; virtual ErrorOr> lookup(StringView name) override; virtual ErrorOr flush_metadata() override; virtual InodeMetadata metadata() const override; virtual ErrorOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override; virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; virtual ErrorOr add_child(Inode&, StringView name, mode_t) override; virtual ErrorOr remove_child(StringView name) override; virtual ErrorOr chmod(mode_t) override; virtual ErrorOr chown(UserID, GroupID) override; virtual ErrorOr truncate(u64) override; virtual ErrorOr set_mtime(time_t); virtual ErrorOr attach(OpenFileDescription& description) override final; virtual void did_seek(OpenFileDescription&, off_t) override final; NonnullRefPtr m_associated_component; }; class SysFSDirectoryInode : public SysFSInode { friend class SysFS; public: static ErrorOr> try_create(SysFS const&, SysFSComponent const&); virtual ~SysFSDirectoryInode() override; SysFS& fs() { return static_cast(Inode::fs()); } SysFS const& fs() const { return static_cast(Inode::fs()); } protected: SysFSDirectoryInode(SysFS const&, SysFSComponent const&); // ^Inode virtual InodeMetadata metadata() const override; virtual ErrorOr traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const override; virtual ErrorOr> lookup(StringView name) override; }; }