ladybird/Kernel/FileSystem/SysFS/Component.h
kleines Filmröllchen a6a439243f Kernel: Turn lock ranks into template parameters
This step would ideally not have been necessary (increases amount of
refactoring and templates necessary, which in turn increases build
times), but it gives us a couple of nice properties:
- SpinlockProtected inside Singleton (a very common combination) can now
  obtain any lock rank just via the template parameter. It was not
  previously possible to do this with SingletonInstanceCreator magic.
- SpinlockProtected's lock rank is now mandatory; this is the majority
  of cases and allows us to see where we're still missing proper ranks.
- The type already informs us what lock rank a lock has, which aids code
  readability and (possibly, if gdb cooperates) lock mismatch debugging.
- The rank of a lock can no longer be dynamic, which is not something we
  wanted in the first place (or made use of). Locks randomly changing
  their rank sounds like a disaster waiting to happen.
- In some places, we might be able to statically check that locks are
  taken in the right order (with the right lock rank checking
  implementation) as rank information is fully statically known.

This refactoring even more exposes the fact that Mutex has no lock rank
capabilites, which is not fixed here.
2023-01-02 18:15:27 -05:00

93 lines
3.4 KiB
C++

/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/Function.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/Forward.h>
#include <Kernel/Library/LockRefPtr.h>
namespace Kernel {
struct SysFSInodeData : public OpenFileDescriptionData {
OwnPtr<KBuffer> buffer;
};
class SysFSDirectory;
class SysFSComponent : public AtomicRefCounted<SysFSComponent> {
friend class SysFSDirectory;
public:
virtual StringView name() const = 0;
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { return Error::from_errno(ENOTIMPL); }
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const { VERIFY_NOT_REACHED(); }
virtual LockRefPtr<SysFSComponent> lookup(StringView) { VERIFY_NOT_REACHED(); };
virtual mode_t permissions() const;
virtual ErrorOr<void> truncate(u64) { return EPERM; }
virtual size_t size() const { return 0; }
virtual ErrorOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; }
virtual ErrorOr<void> refresh_data(OpenFileDescription&) const { return {}; }
virtual ErrorOr<NonnullLockRefPtr<SysFSInode>> to_inode(SysFS const&) const;
InodeIndex component_index() const { return m_component_index; };
virtual ~SysFSComponent() = default;
ErrorOr<NonnullOwnPtr<KString>> relative_path(NonnullOwnPtr<KString>, size_t current_hop = 0) const;
ErrorOr<size_t> relative_path_hops_count_from_mountpoint(size_t current_hop = 0) const;
protected:
explicit SysFSComponent(SysFSDirectory const& parent_directory);
SysFSComponent();
LockRefPtr<SysFSDirectory> m_parent_directory;
IntrusiveListNode<SysFSComponent, NonnullLockRefPtr<SysFSComponent>> m_list_node;
private:
InodeIndex m_component_index {};
};
class SysFSSymbolicLink : public SysFSComponent {
public:
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override final;
virtual ErrorOr<NonnullLockRefPtr<SysFSInode>> to_inode(SysFS const& sysfs_instance) const override final;
protected:
ErrorOr<NonnullOwnPtr<KString>> try_generate_return_path_to_mount_point() const;
ErrorOr<NonnullOwnPtr<KBuffer>> try_to_generate_buffer() const;
explicit SysFSSymbolicLink(SysFSDirectory const& parent_directory, SysFSComponent const& pointed_component);
LockRefPtr<SysFSComponent> m_pointed_component;
};
class SysFSDirectory : public SysFSComponent {
public:
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override final;
virtual LockRefPtr<SysFSComponent> lookup(StringView name) override final;
virtual ErrorOr<NonnullLockRefPtr<SysFSInode>> to_inode(SysFS const& sysfs_instance) const override final;
using ChildList = SpinlockProtected<IntrusiveList<&SysFSComponent::m_list_node>, LockRank::None>;
protected:
virtual bool is_root_directory() const { return false; }
SysFSDirectory() {};
explicit SysFSDirectory(SysFSDirectory const& parent_directory);
ChildList m_child_components {};
};
}