diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index eb7b98d8b35..6f6b9daa09d 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -109,7 +109,6 @@ set(KERNEL_SOURCES FileSystem/BlockBasedFileSystem.cpp FileSystem/Custody.cpp FileSystem/DevPtsFS.cpp - FileSystem/DevTmpFS.cpp FileSystem/Ext2FileSystem.cpp FileSystem/FATFileSystem.cpp FileSystem/FIFO.cpp diff --git a/Kernel/FileSystem/DevTmpFS.cpp b/Kernel/FileSystem/DevTmpFS.cpp deleted file mode 100644 index ea41a1bb7e3..00000000000 --- a/Kernel/FileSystem/DevTmpFS.cpp +++ /dev/null @@ -1,335 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include - -namespace Kernel { - -ErrorOr> DevTmpFS::try_create() -{ - return TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DevTmpFS)); -} - -DevTmpFS::DevTmpFS() = default; - -size_t DevTmpFS::allocate_inode_index() -{ - MutexLocker locker(m_lock); - m_next_inode_index = m_next_inode_index.value() + 1; - VERIFY(m_next_inode_index > 0); - return 1 + m_next_inode_index.value(); -} - -DevTmpFS::~DevTmpFS() = default; - -ErrorOr DevTmpFS::initialize() -{ - m_root_inode = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DevTmpFSRootDirectoryInode(*this))); - return {}; -} - -Inode& DevTmpFS::root_inode() -{ - return *m_root_inode; -} - -DevTmpFSInode::DevTmpFSInode(DevTmpFS& fs) - : Inode(fs, fs.allocate_inode_index()) -{ -} - -DevTmpFSInode::DevTmpFSInode(DevTmpFS& fs, MajorNumber major_number, MinorNumber minor_number) - : Inode(fs, fs.allocate_inode_index()) - , m_major_number(major_number) - , m_minor_number(minor_number) -{ -} - -ErrorOr DevTmpFSInode::read_bytes_locked(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr DevTmpFSInode::traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr> DevTmpFSInode::lookup(StringView) -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr DevTmpFSInode::flush_metadata() -{ - return {}; -} - -ErrorOr DevTmpFSInode::write_bytes_locked(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr> DevTmpFSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID) -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr DevTmpFSInode::add_child(Inode&, StringView, mode_t) -{ - VERIFY_NOT_REACHED(); -} - -InodeMetadata DevTmpFSInode::metadata() const -{ - MutexLocker locker(m_inode_lock); - VERIFY((m_mode & 0777) == m_mode); - InodeMetadata metadata; - metadata.uid = m_uid; - metadata.gid = m_gid; - metadata.size = 0; - metadata.mtime = TimeManagement::boot_time(); - switch (node_type()) { - case Type::RootDirectory: - metadata.inode = { fsid(), 1 }; - metadata.mode = 0040555; - metadata.uid = 0; - metadata.gid = 0; - metadata.size = 0; - metadata.mtime = TimeManagement::boot_time(); - break; - case Type::Directory: - metadata.inode = { fsid(), index() }; - metadata.mode = S_IFDIR | m_mode; - break; - case Type::BlockDevice: - metadata.inode = { fsid(), index() }; - metadata.mode = S_IFBLK | m_mode; - metadata.major_device = m_major_number; - metadata.minor_device = m_minor_number; - break; - case Type::CharacterDevice: - metadata.inode = { fsid(), index() }; - metadata.mode = S_IFCHR | m_mode; - metadata.major_device = m_major_number; - metadata.minor_device = m_minor_number; - break; - case Type::Link: - metadata.inode = { fsid(), index() }; - metadata.mode = S_IFLNK | m_mode; - break; - default: - VERIFY_NOT_REACHED(); - } - return metadata; -} - -ErrorOr DevTmpFSInode::remove_child(StringView) -{ - VERIFY_NOT_REACHED(); -} - -ErrorOr DevTmpFSInode::chmod(mode_t mode) -{ - MutexLocker locker(m_inode_lock); - mode &= 0777; - if (m_mode == mode) - return {}; - m_mode = mode; - return {}; -} - -ErrorOr DevTmpFSInode::chown(UserID uid, GroupID gid) -{ - MutexLocker locker(m_inode_lock); - m_uid = uid; - m_gid = gid; - return {}; -} - -ErrorOr DevTmpFSInode::truncate(u64) -{ - return EPERM; -} - -StringView DevTmpFSLinkInode::name() const -{ - return m_name->view(); -} - -DevTmpFSLinkInode::~DevTmpFSLinkInode() = default; - -DevTmpFSLinkInode::DevTmpFSLinkInode(DevTmpFS& fs, NonnullOwnPtr name) - : DevTmpFSInode(fs) - , m_name(move(name)) -{ -} - -ErrorOr DevTmpFSLinkInode::read_bytes_locked(off_t offset, size_t, UserOrKernelBuffer& buffer, OpenFileDescription*) const -{ - VERIFY(m_inode_lock.is_locked()); - VERIFY(offset == 0); - VERIFY(m_link); - TRY(buffer.write(m_link->characters() + offset, m_link->length())); - return m_link->length(); -} - -ErrorOr DevTmpFSLinkInode::write_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*) -{ - VERIFY(m_inode_lock.is_locked()); - auto new_string = TRY(buffer.try_copy_into_kstring(count)); - VERIFY(offset == 0); - VERIFY(buffer.is_kernel_buffer()); - m_link = move(new_string); - return count; -} - -DevTmpFSDirectoryInode::DevTmpFSDirectoryInode(DevTmpFS& fs) - : DevTmpFSInode(fs) -{ -} -DevTmpFSDirectoryInode::DevTmpFSDirectoryInode(DevTmpFS& fs, NonnullOwnPtr name) - : DevTmpFSInode(fs) - , m_name(move(name)) -{ -} -DevTmpFSDirectoryInode::~DevTmpFSDirectoryInode() = default; - -ErrorOr DevTmpFSDirectoryInode::traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)> callback) const -{ - MutexLocker locker(m_inode_lock); - TRY(callback({ "."sv, identifier(), 0 })); - TRY(callback({ ".."sv, identifier(), 0 })); - for (auto& node : m_nodes) { - InodeIdentifier identifier = { fsid(), node.index() }; - TRY(callback({ node.name(), identifier, 0 })); - } - return {}; -} - -ErrorOr> DevTmpFSDirectoryInode::lookup(StringView name) -{ - MutexLocker locker(m_inode_lock); - for (auto& node : m_nodes) { - if (node.name() == name) { - return node; - } - } - return Error::from_errno(ENOENT); -} - -ErrorOr DevTmpFSDirectoryInode::remove_child(StringView name) -{ - MutexLocker locker(m_inode_lock); - for (auto& node : m_nodes) { - if (node.name() == name) { - m_nodes.remove(node); - return {}; - } - } - return Error::from_errno(ENOENT); -} - -ErrorOr> DevTmpFSDirectoryInode::create_child(StringView name, mode_t mode, dev_t device_mode, UserID, GroupID) -{ - MutexLocker locker(m_inode_lock); - for (auto& node : m_nodes) { - if (node.name() == name) - return Error::from_errno(EEXIST); - } - - InodeMetadata metadata; - metadata.mode = mode; - if (metadata.is_directory()) { - auto name_kstring = TRY(KString::try_create(name)); - auto new_directory_inode = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DevTmpFSDirectoryInode(fs(), move(name_kstring)))); - TRY(new_directory_inode->chmod(mode)); - m_nodes.append(*new_directory_inode); - return new_directory_inode; - } - if (metadata.is_device()) { - auto name_kstring = TRY(KString::try_create(name)); - auto major = major_from_encoded_device(device_mode); - auto minor = minor_from_encoded_device(device_mode); - auto new_device_inode = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DevTmpFSDeviceInode(fs(), major, minor, is_block_device(mode), move(name_kstring)))); - TRY(new_device_inode->chmod(mode)); - m_nodes.append(*new_device_inode); - return new_device_inode; - } - if (metadata.is_symlink()) { - auto name_kstring = TRY(KString::try_create(name)); - auto new_link_inode = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) DevTmpFSLinkInode(fs(), move(name_kstring)))); - TRY(new_link_inode->chmod(mode)); - m_nodes.append(*new_link_inode); - return new_link_inode; - } - return EROFS; -} - -DevTmpFSRootDirectoryInode::DevTmpFSRootDirectoryInode(DevTmpFS& fs) - : DevTmpFSDirectoryInode(fs) -{ - m_mode = 0555; -} -DevTmpFSRootDirectoryInode::~DevTmpFSRootDirectoryInode() = default; -ErrorOr DevTmpFSRootDirectoryInode::chmod(mode_t) -{ - return EPERM; -} - -ErrorOr DevTmpFSRootDirectoryInode::chown(UserID, GroupID) -{ - return EPERM; -} - -DevTmpFSDeviceInode::DevTmpFSDeviceInode(DevTmpFS& fs, MajorNumber major_number, MinorNumber minor_number, bool block_device, NonnullOwnPtr name) - : DevTmpFSInode(fs, major_number, minor_number) - , m_name(move(name)) - , m_block_device(block_device) -{ -} - -DevTmpFSDeviceInode::~DevTmpFSDeviceInode() = default; - -StringView DevTmpFSDeviceInode::name() const -{ - return m_name->view(); -} - -ErrorOr DevTmpFSDeviceInode::read_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription* description) const -{ - VERIFY(m_inode_lock.is_locked()); - VERIFY(!!description); - LockRefPtr device = DeviceManagement::the().get_device(m_major_number, m_minor_number); - if (!device) - return Error::from_errno(ENODEV); - if (!device->can_read(*description, offset)) - return Error::from_errno(ENOTIMPL); - auto result = const_cast(*device).read(*description, offset, buffer, count); - if (result.is_error()) - return result; - return result.value(); -} - -ErrorOr DevTmpFSDeviceInode::write_bytes_locked(off_t offset, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription* description) -{ - VERIFY(m_inode_lock.is_locked()); - VERIFY(!!description); - LockRefPtr device = DeviceManagement::the().get_device(m_major_number, m_minor_number); - if (!device) - return Error::from_errno(ENODEV); - if (!device->can_write(*description, offset)) - return Error::from_errno(ENOTIMPL); - auto result = const_cast(*device).write(*description, offset, buffer, count); - if (result.is_error()) - return result; - return result.value(); -} - -} diff --git a/Kernel/FileSystem/DevTmpFS.h b/Kernel/FileSystem/DevTmpFS.h deleted file mode 100644 index 034cd7cde56..00000000000 --- a/Kernel/FileSystem/DevTmpFS.h +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright (c) 2020, Liav A. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include -#include -#include - -namespace Kernel { - -class DevTmpFS final : public FileSystem { - friend class DevTmpFSInode; - friend class DevTmpFSRootDirectoryInode; - -public: - virtual ~DevTmpFS() override; - static ErrorOr> try_create(); - - virtual ErrorOr initialize() override; - virtual StringView class_name() const override { return "DevTmpFS"sv; } - virtual Inode& root_inode() override; - -private: - DevTmpFS(); - size_t allocate_inode_index(); - - LockRefPtr m_root_inode; - InodeIndex m_next_inode_index { 0 }; -}; - -class DevTmpFSInode : public Inode { - friend class DevTmpFS; - friend class DevTmpFSRootDirectoryInode; - friend class DevTmpFSDirectoryInode; - -public: - virtual StringView name() const = 0; - - DevTmpFS& fs() { return static_cast(Inode::fs()); } - DevTmpFS const& fs() const { return static_cast(Inode::fs()); } - -protected: - explicit DevTmpFSInode(DevTmpFS&); - DevTmpFSInode(DevTmpFS&, MajorNumber, MinorNumber); - virtual ErrorOr read_bytes_locked(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 final; - virtual ErrorOr write_bytes_locked(off_t, size_t, UserOrKernelBuffer const& buffer, 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; - - mode_t m_mode { 0600 }; - UserID m_uid { 0 }; - GroupID m_gid { 0 }; - const MajorNumber m_major_number { 0 }; - const MinorNumber m_minor_number { 0 }; - - enum class Type { - BlockDevice, - CharacterDevice, - Directory, - RootDirectory, - Link - }; - virtual Type node_type() const = 0; - -private: - IntrusiveListNode> m_list_node; -}; - -class DevTmpFSDeviceInode final : public DevTmpFSInode { - friend class DevTmpFS; - friend class DevTmpFSRootDirectoryInode; - friend class DevTmpFSDirectoryInode; - -public: - virtual StringView name() const override; - virtual ~DevTmpFSDeviceInode() override; - -private: - DevTmpFSDeviceInode(DevTmpFS&, MajorNumber, MinorNumber, bool, NonnullOwnPtr name); - // ^DevTmpFSInode - virtual Type node_type() const override { return m_block_device ? Type::BlockDevice : Type::CharacterDevice; } - - // ^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; - - NonnullOwnPtr m_name; - bool const m_block_device; -}; - -class DevTmpFSLinkInode final : public DevTmpFSInode { - friend class DevTmpFS; - friend class DevTmpFSDirectoryInode; - -public: - virtual StringView name() const override; - virtual ~DevTmpFSLinkInode() override; - -protected: - DevTmpFSLinkInode(DevTmpFS&, NonnullOwnPtr); - // ^DevTmpFSInode - virtual Type node_type() const override { return Type::Link; } - - // ^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; - - NonnullOwnPtr m_name; - OwnPtr m_link; -}; - -class DevTmpFSDirectoryInode : public DevTmpFSInode { - friend class DevTmpFS; - friend class DevTmpFSRootDirectoryInode; - -public: - virtual ~DevTmpFSDirectoryInode() override; - virtual StringView name() const override { return m_name->view(); } - -protected: - // ^DevTmpFSInode - virtual Type node_type() const override { return Type::Directory; } - - virtual ErrorOr> create_child(StringView name, mode_t, dev_t, UserID, GroupID) override; - virtual ErrorOr remove_child(StringView name) override; - virtual ErrorOr traverse_as_directory(Function(FileSystem::DirectoryEntryView const&)>) const override; - virtual ErrorOr> lookup(StringView name) override; - DevTmpFSDirectoryInode(DevTmpFS&, NonnullOwnPtr name); - // ^Inode - OwnPtr m_name; - IntrusiveList<&DevTmpFSInode::m_list_node> m_nodes; - -private: - explicit DevTmpFSDirectoryInode(DevTmpFS&); -}; - -class DevTmpFSRootDirectoryInode final : public DevTmpFSDirectoryInode { - friend class DevTmpFS; - -public: - virtual ~DevTmpFSRootDirectoryInode() override; - virtual StringView name() const override { return "."sv; } - -private: - // ^DevTmpFSInode - virtual Type node_type() const override { return Type::Directory; } - - explicit DevTmpFSRootDirectoryInode(DevTmpFS&); - virtual ErrorOr chmod(mode_t) override; - virtual ErrorOr chown(UserID, GroupID) override; -}; - -} diff --git a/Kernel/Forward.h b/Kernel/Forward.h index 04bacdf7c1b..645ac420a93 100644 --- a/Kernel/Forward.h +++ b/Kernel/Forward.h @@ -16,11 +16,6 @@ class CharacterDevice; class Coredump; class Credentials; class Custody; -class DevTmpFSDeviceInode; -class DevTmpFSDirectoryInode; -class DevTmpFSInode; -class DevTmpFSPtsDirectoryInode; -class DevTmpFSRootDirectoryInode; class Device; class DiskCache; class DoubleBuffer; diff --git a/Kernel/Syscalls/mount.cpp b/Kernel/Syscalls/mount.cpp index 27b1eeda73c..4b1f306dcf9 100644 --- a/Kernel/Syscalls/mount.cpp +++ b/Kernel/Syscalls/mount.cpp @@ -6,7 +6,6 @@ #include #include -#include #include #include #include @@ -32,7 +31,6 @@ struct FileSystemInitializer { static constexpr FileSystemInitializer s_initializers[] = { { "proc"sv, "ProcFS"sv, false, false, false, {}, ProcFS::try_create }, { "devpts"sv, "DevPtsFS"sv, false, false, false, {}, DevPtsFS::try_create }, - { "dev"sv, "DevTmpFS"sv, false, false, false, {}, DevTmpFS::try_create }, { "sys"sv, "SysFS"sv, false, false, false, {}, SysFS::try_create }, { "tmp"sv, "TmpFS"sv, false, false, false, {}, TmpFS::try_create }, { "ext2"sv, "Ext2FS"sv, true, true, true, Ext2FS::try_create, {} },