/* * Copyright (c) 2019-2020, Sergey Bugaev * Copyright (c) 2022, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace Kernel { class TmpFSInode; class TmpFS final : public FileSystem { friend class TmpFSInode; public: virtual ~TmpFS() override; static ErrorOr> try_create(); virtual ErrorOr initialize() override; virtual StringView class_name() const override { return "TmpFS"sv; } virtual bool supports_watchers() const override { return true; } virtual Inode& root_inode() override; private: TmpFS(); LockRefPtr m_root_inode; unsigned m_next_inode_index { 1 }; unsigned next_inode_index(); }; class TmpFSInode final : public Inode { friend class TmpFS; public: virtual ~TmpFSInode() override; TmpFS& fs() { return static_cast(Inode::fs()); } TmpFS const& fs() const { return static_cast(Inode::fs()); } // ^Inode virtual InodeMetadata metadata() 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 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 update_timestamps(Optional atime, Optional ctime, Optional mtime) override; private: TmpFSInode(TmpFS& fs, InodeMetadata const& metadata, LockWeakPtr parent); static ErrorOr> try_create(TmpFS&, InodeMetadata const& metadata, LockWeakPtr parent); static ErrorOr> try_create_root(TmpFS&); // ^Inode virtual ErrorOr read_bytes_locked(off_t, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const override; virtual ErrorOr write_bytes_locked(off_t, size_t, UserOrKernelBuffer const& buffer, OpenFileDescription*) override; ErrorOr do_io_on_content_space(Memory::Region& mapping_region, size_t offset, size_t io_size, UserOrKernelBuffer& buffer, bool write); struct Child { NonnullOwnPtr name; NonnullLockRefPtr inode; IntrusiveListNode list_node {}; using List = IntrusiveList<&Child::list_node>; }; Child* find_child_by_name(StringView); InodeMetadata m_metadata; LockWeakPtr m_parent; ErrorOr ensure_allocated_blocks(size_t offset, size_t io_size); ErrorOr truncate_to_block_index(size_t block_index); ErrorOr read_bytes_from_content_space(size_t offset, size_t io_size, UserOrKernelBuffer& buffer) const; ErrorOr write_bytes_to_content_space(size_t offset, size_t io_size, UserOrKernelBuffer const& buffer); struct DataBlock { public: using List = Vector>; static ErrorOr> create(); constexpr static size_t block_size = 128 * KiB; Memory::AnonymousVMObject& vmobject() { return *m_content_buffer_vmobject; } Memory::AnonymousVMObject const& vmobject() const { return *m_content_buffer_vmobject; } private: explicit DataBlock(NonnullLockRefPtr content_buffer_vmobject) : m_content_buffer_vmobject(move(content_buffer_vmobject)) { } NonnullLockRefPtr m_content_buffer_vmobject; }; DataBlock::List m_blocks; Child::List m_children; }; }