2020-07-02 07:05:56 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-02 07:05:56 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Atomic.h>
|
|
|
|
|
#include <Kernel/FileSystem/FileBackedFileSystem.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2022-10-24 04:28:24 -03:00
|
|
|
#include <Kernel/FileSystem/Plan9FS/Definitions.h>
|
|
|
|
|
#include <Kernel/FileSystem/Plan9FS/Message.h>
|
2023-02-24 15:10:59 -03:00
|
|
|
#include <Kernel/Library/KBufferBuilder.h>
|
2020-07-02 07:05:56 -03:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
|
|
class Plan9FSInode;
|
|
|
|
|
|
2021-07-10 19:33:27 -03:00
|
|
|
class Plan9FS final : public FileBackedFileSystem {
|
2020-07-02 07:05:56 -03:00
|
|
|
friend class Plan9FSInode;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Plan9FS() override;
|
2022-12-15 06:42:40 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<FileSystem>> try_create(OpenFileDescription&, ReadonlyBytes);
|
2020-07-02 07:05:56 -03:00
|
|
|
|
|
|
|
|
virtual bool supports_watchers() const override { return false; }
|
|
|
|
|
|
2021-07-17 20:50:47 -03:00
|
|
|
virtual Inode& root_inode() override;
|
2020-07-02 07:05:56 -03:00
|
|
|
|
|
|
|
|
u16 allocate_tag() { return m_next_tag++; }
|
|
|
|
|
u32 allocate_fid() { return m_next_fid++; }
|
|
|
|
|
|
|
|
|
|
enum class ProtocolVersion {
|
|
|
|
|
v9P2000,
|
|
|
|
|
v9P2000u,
|
|
|
|
|
v9P2000L
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
2021-09-07 08:39:11 -03:00
|
|
|
Plan9FS(OpenFileDescription&);
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2023-07-02 09:23:53 -03:00
|
|
|
virtual ErrorOr<void> prepare_to_clear_last_mount(Inode&) override;
|
2022-08-19 18:03:24 -03:00
|
|
|
|
|
|
|
|
virtual bool is_initialized_while_locked() override;
|
|
|
|
|
virtual ErrorOr<void> initialize_while_locked() override;
|
|
|
|
|
|
2020-11-29 20:05:27 -03:00
|
|
|
class Blocker;
|
|
|
|
|
|
2021-08-22 10:59:47 -03:00
|
|
|
class Plan9FSBlockerSet final : public Thread::BlockerSet {
|
2020-11-29 20:05:27 -03:00
|
|
|
public:
|
2021-08-22 10:59:47 -03:00
|
|
|
Plan9FSBlockerSet(Plan9FS& fs)
|
2020-11-29 20:05:27 -03:00
|
|
|
: m_fs(fs)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void unblock_completed(u16);
|
|
|
|
|
void unblock_all();
|
|
|
|
|
void try_unblock(Blocker&);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
virtual bool should_add_blocker(Thread::Blocker&, void*) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Plan9FS& m_fs;
|
2022-11-09 07:39:58 -03:00
|
|
|
mutable Spinlock<LockRank::None> m_lock {};
|
2020-11-29 20:05:27 -03:00
|
|
|
};
|
|
|
|
|
|
2022-08-19 12:26:07 -03:00
|
|
|
struct ReceiveCompletion final : public AtomicRefCounted<ReceiveCompletion> {
|
2022-11-09 07:39:58 -03:00
|
|
|
mutable Spinlock<LockRank::None> lock {};
|
2020-11-29 20:05:27 -03:00
|
|
|
bool completed { false };
|
2024-04-18 16:32:56 -03:00
|
|
|
u16 const tag;
|
2022-10-24 04:28:24 -03:00
|
|
|
OwnPtr<Plan9FSMessage> message;
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<void> result;
|
2020-11-29 20:05:27 -03:00
|
|
|
|
|
|
|
|
ReceiveCompletion(u16 tag);
|
|
|
|
|
~ReceiveCompletion();
|
2020-07-02 07:05:56 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Blocker final : public Thread::Blocker {
|
|
|
|
|
public:
|
2022-10-24 04:28:24 -03:00
|
|
|
Blocker(Plan9FS& fs, Plan9FSMessage& message, NonnullLockRefPtr<ReceiveCompletion> completion)
|
2020-11-29 20:05:27 -03:00
|
|
|
: m_fs(fs)
|
|
|
|
|
, m_message(message)
|
|
|
|
|
, m_completion(move(completion))
|
2020-07-02 07:05:56 -03:00
|
|
|
{
|
|
|
|
|
}
|
2021-08-24 07:14:14 -03:00
|
|
|
virtual bool setup_blocker() override;
|
2021-08-05 15:48:14 -03:00
|
|
|
virtual StringView state_string() const override { return "Waiting"sv; }
|
2020-11-29 20:05:27 -03:00
|
|
|
virtual Type blocker_type() const override { return Type::Plan9FS; }
|
2021-08-22 21:09:08 -03:00
|
|
|
virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override;
|
2020-11-29 20:05:27 -03:00
|
|
|
|
2022-08-19 15:53:40 -03:00
|
|
|
NonnullLockRefPtr<ReceiveCompletion> const& completion() const { return m_completion; }
|
2020-11-29 20:05:27 -03:00
|
|
|
u16 tag() const { return m_completion->tag; }
|
|
|
|
|
bool is_completed() const;
|
|
|
|
|
|
|
|
|
|
bool unblock()
|
|
|
|
|
{
|
|
|
|
|
unblock_from_blocker();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool unblock(u16 tag);
|
2020-07-02 07:05:56 -03:00
|
|
|
|
|
|
|
|
private:
|
2020-11-29 20:05:27 -03:00
|
|
|
Plan9FS& m_fs;
|
2022-10-24 04:28:24 -03:00
|
|
|
Plan9FSMessage& m_message;
|
2022-08-19 15:53:40 -03:00
|
|
|
NonnullLockRefPtr<ReceiveCompletion> m_completion;
|
2020-11-29 20:05:27 -03:00
|
|
|
bool m_did_unblock { false };
|
2020-07-02 07:05:56 -03:00
|
|
|
};
|
2020-11-29 20:05:27 -03:00
|
|
|
friend class Blocker;
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2021-07-17 15:59:06 -03:00
|
|
|
virtual StringView class_name() const override { return "Plan9FS"sv; }
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
bool is_complete(ReceiveCompletion const&);
|
2022-10-24 04:28:24 -03:00
|
|
|
ErrorOr<void> post_message(Plan9FSMessage&, LockRefPtr<ReceiveCompletion>);
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<void> do_read(u8* buffer, size_t);
|
|
|
|
|
ErrorOr<void> read_and_dispatch_one_message();
|
2022-10-24 04:28:24 -03:00
|
|
|
ErrorOr<void> post_message_and_wait_for_a_reply(Plan9FSMessage&);
|
|
|
|
|
ErrorOr<void> post_message_and_explicitly_ignore_reply(Plan9FSMessage&);
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
ProtocolVersion parse_protocol_version(StringView) const;
|
2021-06-16 11:44:15 -03:00
|
|
|
size_t adjust_buffer_size(size_t size) const;
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2020-11-29 20:05:27 -03:00
|
|
|
void thread_main();
|
|
|
|
|
void ensure_thread();
|
|
|
|
|
|
2023-03-07 08:25:00 -03:00
|
|
|
RefPtr<Plan9FSInode> m_root_inode;
|
2020-07-02 07:05:56 -03:00
|
|
|
Atomic<u16> m_next_tag { (u16)-1 };
|
|
|
|
|
Atomic<u32> m_next_fid { 1 };
|
|
|
|
|
|
|
|
|
|
ProtocolVersion m_remote_protocol_version { ProtocolVersion::v9P2000 };
|
AK: Rename KB, MB, GB to KiB, MiB, GiB
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".
Let's use the correct name, at least in code.
Only changes the name of the constants, no other behavior change.
2020-08-15 14:55:00 -03:00
|
|
|
size_t m_max_message_size { 4 * KiB };
|
2020-07-02 07:05:56 -03:00
|
|
|
|
2022-07-11 14:32:29 -03:00
|
|
|
Mutex m_send_lock { "Plan9FS send"sv };
|
2021-08-22 10:59:47 -03:00
|
|
|
Plan9FSBlockerSet m_completion_blocker;
|
2022-08-19 15:53:40 -03:00
|
|
|
HashMap<u16, NonnullLockRefPtr<ReceiveCompletion>> m_completions;
|
2020-11-29 20:05:27 -03:00
|
|
|
|
2022-11-09 07:39:58 -03:00
|
|
|
Spinlock<LockRank::None> m_thread_lock {};
|
2023-04-02 15:40:47 -03:00
|
|
|
RefPtr<Thread> m_thread;
|
2020-11-29 20:05:27 -03:00
|
|
|
Atomic<bool> m_thread_running { false };
|
2020-07-02 07:05:56 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|