/* * Copyright (c) 2021, Liav A. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include namespace Kernel { static Spinlock s_index_lock { LockRank::None }; static InodeIndex s_next_inode_index { 0 }; static size_t allocate_inode_index() { SpinlockLocker lock(s_index_lock); s_next_inode_index = s_next_inode_index.value() + 1; VERIFY(s_next_inode_index > 0); return s_next_inode_index.value(); } SysFSComponent::SysFSComponent(SysFSDirectory const& parent_directory) : m_parent_directory(parent_directory) , m_component_index(allocate_inode_index()) { } SysFSComponent::SysFSComponent() : m_component_index(allocate_inode_index()) { } ErrorOr> SysFSComponent::relative_path(NonnullOwnPtr name, size_t current_hop) const { if (current_hop >= 128) return Error::from_errno(ELOOP); if (!m_parent_directory) return name; auto joined_name = TRY(KLexicalPath::try_join(m_parent_directory->name(), name->view())); return m_parent_directory->relative_path(move(joined_name), current_hop + 1); } ErrorOr SysFSComponent::relative_path_hops_count_from_mountpoint(size_t current_hop) const { if (current_hop >= 128) return Error::from_errno(ELOOP); if (!m_parent_directory) return current_hop; return m_parent_directory->relative_path_hops_count_from_mountpoint(current_hop + 1); } mode_t SysFSComponent::permissions() const { return S_IRUSR | S_IRGRP | S_IROTH; } ErrorOr SysFSSymbolicLink::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, OpenFileDescription*) const { auto blob = TRY(try_to_generate_buffer()); if ((size_t)offset >= blob->size()) return 0; ssize_t nread = min(static_cast(blob->size() - offset), static_cast(count)); TRY(buffer.write(blob->data() + offset, nread)); return nread; } ErrorOr> SysFSSymbolicLink::try_to_generate_buffer() const { auto return_path_to_mount_point = TRY(try_generate_return_path_to_mount_point()); if (!m_pointed_component) return Error::from_errno(EIO); auto pointed_component_base_name = MUST(KString::try_create(m_pointed_component->name())); auto pointed_component_relative_path = MUST(m_pointed_component->relative_path(move(pointed_component_base_name), 0)); auto full_return_and_target_path = TRY(KString::formatted("{}{}", return_path_to_mount_point->view(), pointed_component_relative_path->view())); return KBuffer::try_create_with_bytes("SysFSSymbolicLink"sv, full_return_and_target_path->view().bytes()); } static ErrorOr> generate_return_path_to_mount_point(NonnullOwnPtr current_path, size_t remaining_hop) { if (remaining_hop == 0) return current_path; auto new_path = TRY(KString::formatted("../{}"sv, current_path->view())); remaining_hop--; return generate_return_path_to_mount_point(move(new_path), remaining_hop); } ErrorOr> SysFSSymbolicLink::try_generate_return_path_to_mount_point() const { VERIFY(m_parent_directory); auto hops_from_mountpoint = TRY(m_parent_directory->relative_path_hops_count_from_mountpoint()); if (hops_from_mountpoint == 0) return KString::try_create("./"sv); auto start_return_path = TRY(KString::try_create("./"sv)); return generate_return_path_to_mount_point(move(start_return_path), hops_from_mountpoint); } SysFSSymbolicLink::SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component) : SysFSComponent(parent_directory) , m_pointed_component(pointed_component) { } ErrorOr SysFSDirectory::traverse_as_directory(FileSystemID fsid, Function(FileSystem::DirectoryEntryView const&)> callback) const { TRY(callback({ "."sv, { fsid, component_index() }, 0 })); if (is_root_directory()) { TRY(callback({ ".."sv, { fsid, component_index() }, 0 })); } else { VERIFY(m_parent_directory); TRY(callback({ ".."sv, { fsid, m_parent_directory->component_index() }, 0 })); } return m_child_components.with([&](auto& list) -> ErrorOr { for (auto& child_component : list) { InodeIdentifier identifier = { fsid, child_component.component_index() }; TRY(callback({ child_component.name(), identifier, 0 })); } return {}; }); } LockRefPtr SysFSDirectory::lookup(StringView name) { return m_child_components.with([&](auto& list) -> LockRefPtr { for (auto& child_component : list) { if (child_component.name() == name) { return child_component; } } return nullptr; }); } SysFSDirectory::SysFSDirectory(SysFSDirectory const& parent_directory) : SysFSComponent(parent_directory) { } ErrorOr> SysFSDirectory::to_inode(SysFS const& sysfs_instance) const { return TRY(SysFSDirectoryInode::try_create(sysfs_instance, *this)); } ErrorOr> SysFSSymbolicLink::to_inode(SysFS const& sysfs_instance) const { return TRY(SysFSLinkInode::try_create(sysfs_instance, *this)); } ErrorOr> SysFSComponent::to_inode(SysFS const& sysfs_instance) const { return SysFSInode::try_create(sysfs_instance, *this); } }