/* * Copyright (c) 2021, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include namespace Kernel { class SysFSRootDirectory final : public SysFSDirectory { friend class SysFSComponentRegistry; public: static NonnullRefPtr create(); virtual KResult traverse_as_directory(unsigned, Function) const override; private: SysFSRootDirectory(); RefPtr m_buses_directory; }; class SysFSBusDirectory : public SysFSDirectory { friend class SysFSComponentRegistry; public: static NonnullRefPtr must_create(SysFSRootDirectory const&); private: explicit SysFSBusDirectory(SysFSRootDirectory const&); }; class SysFSComponentRegistry { 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(); private: Mutex m_lock; NonnullRefPtr m_root_directory; }; class SysFS final : public FileSystem { friend class SysFSInode; friend class SysFSDirectoryInode; public: virtual ~SysFS() override; static NonnullRefPtr create(); virtual KResult initialize() override; virtual StringView class_name() const override { return "SysFS"sv; } virtual Inode& root_inode() override; private: SysFS(); NonnullRefPtr m_root_inode; }; class SysFSInode : public Inode { friend class SysFS; friend class SysFSDirectoryInode; public: static NonnullRefPtr create(SysFS const&, SysFSComponent const&); StringView name() const { return m_associated_component->name(); } protected: SysFSInode(SysFS const&, SysFSComponent const&); virtual KResultOr read_bytes(off_t, size_t, UserOrKernelBuffer& buffer, FileDescription*) const override; virtual KResult traverse_as_directory(Function) const override; virtual KResultOr> lookup(StringView name) override; virtual void flush_metadata() override; virtual InodeMetadata metadata() const override; virtual KResultOr write_bytes(off_t, size_t, UserOrKernelBuffer const&, FileDescription*) override; virtual KResultOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; virtual KResult add_child(Inode&, StringView const& name, mode_t) override; virtual KResult remove_child(StringView const& name) override; virtual KResult chmod(mode_t) override; virtual KResult chown(UserID, GroupID) override; virtual KResult truncate(u64) override; virtual KResult attach(FileDescription& description) override final; virtual void did_seek(FileDescription&, off_t) override final; NonnullRefPtr m_associated_component; }; class SysFSDirectoryInode : public SysFSInode { friend class SysFS; public: static NonnullRefPtr 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 KResult traverse_as_directory(Function) const override; virtual KResultOr> lookup(StringView name) override; }; }