2020-07-02 13:05:56 +03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-02 13:05:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Atomic.h>
|
|
|
|
|
#include <Kernel/FileSystem/FileBackedFileSystem.h>
|
|
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
2022-10-24 10:28:24 +03:00
|
|
|
#include <Kernel/FileSystem/Plan9FS/Definitions.h>
|
|
|
|
|
#include <Kernel/FileSystem/Plan9FS/Message.h>
|
2020-07-02 13:05:56 +03:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
|
|
class Plan9FSInode;
|
|
|
|
|
|
2021-07-11 00:33:27 +02:00
|
|
|
class Plan9FS final : public FileBackedFileSystem {
|
2020-07-02 13:05:56 +03:00
|
|
|
friend class Plan9FSInode;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
virtual ~Plan9FS() override;
|
2022-08-19 20:53:40 +02:00
|
|
|
static ErrorOr<NonnullLockRefPtr<FileSystem>> try_create(OpenFileDescription&);
|
2020-07-02 13:05:56 +03:00
|
|
|
|
|
|
|
|
virtual bool supports_watchers() const override { return false; }
|
|
|
|
|
|
2021-07-18 01:50:47 +02:00
|
|
|
virtual Inode& root_inode() override;
|
2020-07-02 13: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 13:39:11 +02:00
|
|
|
Plan9FS(OpenFileDescription&);
|
2020-07-02 13:05:56 +03:00
|
|
|
|
2022-08-20 00:03:24 +03:00
|
|
|
virtual ErrorOr<void> prepare_to_clear_last_mount() override;
|
|
|
|
|
|
|
|
|
|
virtual bool is_initialized_while_locked() override;
|
|
|
|
|
virtual ErrorOr<void> initialize_while_locked() override;
|
|
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
class Blocker;
|
|
|
|
|
|
2021-08-22 15:59:47 +02:00
|
|
|
class Plan9FSBlockerSet final : public Thread::BlockerSet {
|
2020-11-29 16:05:27 -07:00
|
|
|
public:
|
2021-08-22 15:59:47 +02:00
|
|
|
Plan9FSBlockerSet(Plan9FS& fs)
|
2020-11-29 16:05:27 -07: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 11:39:58 +01:00
|
|
|
mutable Spinlock<LockRank::None> m_lock {};
|
2020-11-29 16:05:27 -07:00
|
|
|
};
|
|
|
|
|
|
2022-08-19 17:26:07 +02:00
|
|
|
struct ReceiveCompletion final : public AtomicRefCounted<ReceiveCompletion> {
|
2022-11-09 11:39:58 +01:00
|
|
|
mutable Spinlock<LockRank::None> lock {};
|
2020-11-29 16:05:27 -07:00
|
|
|
bool completed { false };
|
|
|
|
|
const u16 tag;
|
2022-10-24 10:28:24 +03:00
|
|
|
OwnPtr<Plan9FSMessage> message;
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> result;
|
2020-11-29 16:05:27 -07:00
|
|
|
|
|
|
|
|
ReceiveCompletion(u16 tag);
|
|
|
|
|
~ReceiveCompletion();
|
2020-07-02 13:05:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class Blocker final : public Thread::Blocker {
|
|
|
|
|
public:
|
2022-10-24 10:28:24 +03:00
|
|
|
Blocker(Plan9FS& fs, Plan9FSMessage& message, NonnullLockRefPtr<ReceiveCompletion> completion)
|
2020-11-29 16:05:27 -07:00
|
|
|
: m_fs(fs)
|
|
|
|
|
, m_message(message)
|
|
|
|
|
, m_completion(move(completion))
|
2020-07-02 13:05:56 +03:00
|
|
|
{
|
|
|
|
|
}
|
2021-08-24 12:14:14 +02:00
|
|
|
virtual bool setup_blocker() override;
|
2021-08-05 20:48:14 +02:00
|
|
|
virtual StringView state_string() const override { return "Waiting"sv; }
|
2020-11-29 16:05:27 -07:00
|
|
|
virtual Type blocker_type() const override { return Type::Plan9FS; }
|
2021-08-23 02:09:08 +02:00
|
|
|
virtual void will_unblock_immediately_without_blocking(UnblockImmediatelyReason) override;
|
2020-11-29 16:05:27 -07:00
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
NonnullLockRefPtr<ReceiveCompletion> const& completion() const { return m_completion; }
|
2020-11-29 16:05:27 -07: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 13:05:56 +03:00
|
|
|
|
|
|
|
|
private:
|
2020-11-29 16:05:27 -07:00
|
|
|
Plan9FS& m_fs;
|
2022-10-24 10:28:24 +03:00
|
|
|
Plan9FSMessage& m_message;
|
2022-08-19 20:53:40 +02:00
|
|
|
NonnullLockRefPtr<ReceiveCompletion> m_completion;
|
2020-11-29 16:05:27 -07:00
|
|
|
bool m_did_unblock { false };
|
2020-07-02 13:05:56 +03:00
|
|
|
};
|
2020-11-29 16:05:27 -07:00
|
|
|
friend class Blocker;
|
2020-07-02 13:05:56 +03:00
|
|
|
|
2021-07-17 20:59:06 +02:00
|
|
|
virtual StringView class_name() const override { return "Plan9FS"sv; }
|
2020-07-02 13:05:56 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
bool is_complete(ReceiveCompletion const&);
|
2022-10-24 10:28:24 +03:00
|
|
|
ErrorOr<void> post_message(Plan9FSMessage&, LockRefPtr<ReceiveCompletion>);
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> do_read(u8* buffer, size_t);
|
|
|
|
|
ErrorOr<void> read_and_dispatch_one_message();
|
2022-10-24 10: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 13:05:56 +03:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
ProtocolVersion parse_protocol_version(StringView) const;
|
2021-06-16 16:44:15 +02:00
|
|
|
size_t adjust_buffer_size(size_t size) const;
|
2020-07-02 13:05:56 +03:00
|
|
|
|
2020-11-29 16:05:27 -07:00
|
|
|
void thread_main();
|
|
|
|
|
void ensure_thread();
|
|
|
|
|
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Plan9FSInode> m_root_inode;
|
2020-07-02 13: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 13:55:00 -04:00
|
|
|
size_t m_max_message_size { 4 * KiB };
|
2020-07-02 13:05:56 +03:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
Mutex m_send_lock { "Plan9FS send"sv };
|
2021-08-22 15:59:47 +02:00
|
|
|
Plan9FSBlockerSet m_completion_blocker;
|
2022-08-19 20:53:40 +02:00
|
|
|
HashMap<u16, NonnullLockRefPtr<ReceiveCompletion>> m_completions;
|
2020-11-29 16:05:27 -07:00
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
Spinlock<LockRank::None> m_thread_lock {};
|
2022-08-19 20:53:40 +02:00
|
|
|
LockRefPtr<Thread> m_thread;
|
2020-11-29 16:05:27 -07:00
|
|
|
Atomic<bool> m_thread_running { false };
|
2021-01-03 16:58:50 -07:00
|
|
|
Atomic<bool, AK::MemoryOrder::memory_order_relaxed> m_thread_shutdown { false };
|
2020-07-02 13:05:56 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|