2021-03-13 12:01:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-08-19 17:26:07 +02:00
|
|
|
#include <AK/AtomicRefCounted.h>
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2021-03-13 12:01:44 +02:00
|
|
|
#include <AK/Function.h>
|
2023-04-05 13:21:11 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2021-07-11 00:58:23 +02:00
|
|
|
#include <AK/StringView.h>
|
2021-03-13 12:01:44 +02:00
|
|
|
#include <AK/Types.h>
|
|
|
|
#include <Kernel/FileSystem/File.h>
|
|
|
|
#include <Kernel/FileSystem/FileSystem.h>
|
2021-09-07 13:39:11 +02:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2021-07-11 11:49:16 +02:00
|
|
|
#include <Kernel/Forward.h>
|
2021-03-13 12:01:44 +02:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
struct SysFSInodeData : public OpenFileDescriptionData {
|
2021-09-04 16:23:45 +03:00
|
|
|
OwnPtr<KBuffer> buffer;
|
|
|
|
};
|
|
|
|
|
2022-04-22 20:04:38 +03:00
|
|
|
class SysFSDirectory;
|
2022-08-19 17:26:07 +02:00
|
|
|
class SysFSComponent : public AtomicRefCounted<SysFSComponent> {
|
2022-04-23 01:06:37 +03:00
|
|
|
friend class SysFSDirectory;
|
|
|
|
|
2021-03-13 12:01:44 +02:00
|
|
|
public:
|
2021-12-12 16:33:08 +02:00
|
|
|
virtual StringView name() const = 0;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const { return Error::from_errno(ENOTIMPL); }
|
2021-11-18 15:11:31 +01:00
|
|
|
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const { VERIFY_NOT_REACHED(); }
|
2023-04-05 13:21:11 +03:00
|
|
|
virtual RefPtr<SysFSComponent> lookup(StringView) { VERIFY_NOT_REACHED(); };
|
2021-09-11 12:03:44 +03:00
|
|
|
virtual mode_t permissions() const;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> truncate(u64) { return EPERM; }
|
2022-04-01 11:18:38 +03:00
|
|
|
virtual size_t size() const { return 0; }
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) { return EROFS; }
|
|
|
|
virtual ErrorOr<void> refresh_data(OpenFileDescription&) const { return {}; }
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2023-03-07 12:25:00 +01:00
|
|
|
virtual ErrorOr<NonnullRefPtr<SysFSInode>> to_inode(SysFS const&) const;
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2021-06-29 23:07:02 +02:00
|
|
|
InodeIndex component_index() const { return m_component_index; };
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2021-07-11 01:06:27 +02:00
|
|
|
virtual ~SysFSComponent() = default;
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2022-04-22 20:04:38 +03:00
|
|
|
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;
|
|
|
|
|
2021-03-13 12:01:44 +02:00
|
|
|
protected:
|
2022-04-22 20:04:38 +03:00
|
|
|
explicit SysFSComponent(SysFSDirectory const& parent_directory);
|
2021-12-12 16:33:08 +02:00
|
|
|
SysFSComponent();
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2023-04-05 13:21:11 +03:00
|
|
|
RefPtr<SysFSDirectory> const m_parent_directory;
|
2022-04-22 20:04:38 +03:00
|
|
|
|
2023-04-05 13:21:11 +03:00
|
|
|
IntrusiveListNode<SysFSComponent, NonnullRefPtr<SysFSComponent>> m_list_node;
|
2022-04-23 01:06:37 +03:00
|
|
|
|
2021-03-13 12:01:44 +02:00
|
|
|
private:
|
2021-06-29 23:07:02 +02:00
|
|
|
InodeIndex m_component_index {};
|
2021-03-13 12:01:44 +02:00
|
|
|
};
|
|
|
|
|
2022-04-22 20:35:15 +03:00
|
|
|
class SysFSSymbolicLink : public SysFSComponent {
|
|
|
|
public:
|
|
|
|
virtual ErrorOr<size_t> read_bytes(off_t, size_t, UserOrKernelBuffer&, OpenFileDescription*) const override final;
|
2023-03-07 12:25:00 +01:00
|
|
|
virtual ErrorOr<NonnullRefPtr<SysFSInode>> to_inode(SysFS const& sysfs_instance) const override final;
|
2022-04-22 20:35:15 +03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-04-05 13:21:11 +03:00
|
|
|
NonnullRefPtr<SysFSComponent> const m_pointed_component;
|
2022-04-22 20:35:15 +03:00
|
|
|
};
|
|
|
|
|
2021-07-11 01:07:27 +02:00
|
|
|
class SysFSDirectory : public SysFSComponent {
|
2021-03-13 12:01:44 +02:00
|
|
|
public:
|
2022-04-23 01:06:37 +03:00
|
|
|
virtual ErrorOr<void> traverse_as_directory(FileSystemID, Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)>) const override final;
|
2023-04-05 13:21:11 +03:00
|
|
|
virtual RefPtr<SysFSComponent> lookup(StringView name) override final;
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2023-03-07 12:25:00 +01:00
|
|
|
virtual ErrorOr<NonnullRefPtr<SysFSInode>> to_inode(SysFS const& sysfs_instance) const override final;
|
2021-03-13 12:01:44 +02:00
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
using ChildList = SpinlockProtected<IntrusiveList<&SysFSComponent::m_list_node>, LockRank::None>;
|
2022-04-23 01:06:37 +03:00
|
|
|
|
2021-03-13 12:01:44 +02:00
|
|
|
protected:
|
2022-04-23 01:06:37 +03:00
|
|
|
virtual bool is_root_directory() const { return false; }
|
|
|
|
|
2022-04-22 20:04:38 +03:00
|
|
|
SysFSDirectory() {};
|
2021-12-12 16:33:08 +02:00
|
|
|
explicit SysFSDirectory(SysFSDirectory const& parent_directory);
|
2022-11-09 11:39:58 +01:00
|
|
|
ChildList m_child_components {};
|
2021-03-13 12:01:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|