Plan9FileSystem.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Atomic.h>
  28. #include <Kernel/FileSystem/FileBackedFileSystem.h>
  29. #include <Kernel/FileSystem/Inode.h>
  30. #include <Kernel/KBufferBuilder.h>
  31. namespace Kernel {
  32. class Plan9FSInode;
  33. class Plan9FS final : public FileBackedFS {
  34. friend class Plan9FSInode;
  35. public:
  36. virtual ~Plan9FS() override;
  37. static NonnullRefPtr<Plan9FS> create(FileDescription&);
  38. virtual bool initialize() override;
  39. virtual bool supports_watchers() const override { return false; }
  40. virtual NonnullRefPtr<Inode> root_inode() const override;
  41. u16 allocate_tag() { return m_next_tag++; }
  42. u32 allocate_fid() { return m_next_fid++; }
  43. enum class ProtocolVersion {
  44. v9P2000,
  45. v9P2000u,
  46. v9P2000L
  47. };
  48. struct qid {
  49. u8 type;
  50. u32 version;
  51. u64 path;
  52. };
  53. class Message;
  54. private:
  55. Plan9FS(FileDescription&);
  56. class Blocker;
  57. class Plan9FSBlockCondition : public Thread::BlockCondition {
  58. public:
  59. Plan9FSBlockCondition(Plan9FS& fs)
  60. : m_fs(fs)
  61. {
  62. }
  63. void unblock_completed(u16);
  64. void unblock_all();
  65. void try_unblock(Blocker&);
  66. protected:
  67. virtual bool should_add_blocker(Thread::Blocker&, void*) override;
  68. private:
  69. Plan9FS& m_fs;
  70. mutable SpinLock<u8> m_lock;
  71. };
  72. struct ReceiveCompletion : public RefCounted<ReceiveCompletion> {
  73. mutable SpinLock<u8> lock;
  74. bool completed { false };
  75. const u16 tag;
  76. OwnPtr<Message> message;
  77. KResult result { KSuccess };
  78. ReceiveCompletion(u16 tag);
  79. ~ReceiveCompletion();
  80. };
  81. class Blocker final : public Thread::Blocker {
  82. public:
  83. Blocker(Plan9FS& fs, Message& message, NonnullRefPtr<ReceiveCompletion> completion)
  84. : m_fs(fs)
  85. , m_message(message)
  86. , m_completion(move(completion))
  87. {
  88. set_block_condition(fs.m_completion_blocker);
  89. }
  90. virtual const char* state_string() const override { return "Waiting"; }
  91. virtual Type blocker_type() const override { return Type::Plan9FS; }
  92. virtual void not_blocking(bool) override;
  93. const NonnullRefPtr<ReceiveCompletion>& completion() const { return m_completion; }
  94. u16 tag() const { return m_completion->tag; }
  95. bool is_completed() const;
  96. bool unblock()
  97. {
  98. unblock_from_blocker();
  99. return true;
  100. }
  101. bool unblock(u16 tag);
  102. private:
  103. Plan9FS& m_fs;
  104. Message& m_message;
  105. NonnullRefPtr<ReceiveCompletion> m_completion;
  106. bool m_did_unblock { false };
  107. };
  108. friend class Blocker;
  109. virtual const char* class_name() const override { return "Plan9FS"; }
  110. bool is_complete(const ReceiveCompletion&);
  111. KResult post_message(Message&, RefPtr<ReceiveCompletion>);
  112. KResult do_read(u8* buffer, size_t);
  113. KResult read_and_dispatch_one_message();
  114. KResult post_message_and_wait_for_a_reply(Message&);
  115. KResult post_message_and_explicitly_ignore_reply(Message&);
  116. ProtocolVersion parse_protocol_version(const StringView&) const;
  117. ssize_t adjust_buffer_size(ssize_t size) const;
  118. void thread_main();
  119. void ensure_thread();
  120. RefPtr<Plan9FSInode> m_root_inode;
  121. Atomic<u16> m_next_tag { (u16)-1 };
  122. Atomic<u32> m_next_fid { 1 };
  123. ProtocolVersion m_remote_protocol_version { ProtocolVersion::v9P2000 };
  124. size_t m_max_message_size { 4 * KiB };
  125. Lock m_send_lock { "Plan9FS send" };
  126. Plan9FSBlockCondition m_completion_blocker;
  127. HashMap<u16, NonnullRefPtr<ReceiveCompletion>> m_completions;
  128. SpinLock<u8> m_thread_lock;
  129. RefPtr<Thread> m_thread;
  130. Atomic<bool> m_thread_running { false };
  131. Atomic<bool, AK::MemoryOrder::memory_order_relaxed> m_thread_shutdown { false };
  132. };
  133. class Plan9FSInode final : public Inode {
  134. friend class Plan9FS;
  135. public:
  136. virtual ~Plan9FSInode() override;
  137. u32 fid() const { return index(); }
  138. // ^Inode
  139. virtual InodeMetadata metadata() const override;
  140. virtual void flush_metadata() override;
  141. virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const override;
  142. virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) override;
  143. virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const override;
  144. virtual RefPtr<Inode> lookup(StringView name) override;
  145. virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) override;
  146. virtual KResult add_child(Inode&, const StringView& name, mode_t) override;
  147. virtual KResult remove_child(const StringView& name) override;
  148. virtual KResultOr<size_t> directory_entry_count() const override;
  149. virtual KResult chmod(mode_t) override;
  150. virtual KResult chown(uid_t, gid_t) override;
  151. virtual KResult truncate(u64) override;
  152. private:
  153. Plan9FSInode(Plan9FS&, u32 fid);
  154. static NonnullRefPtr<Plan9FSInode> create(Plan9FS&, u32 fid);
  155. enum class GetAttrMask : u64 {
  156. Mode = 0x1,
  157. NLink = 0x2,
  158. UID = 0x4,
  159. GID = 0x8,
  160. RDev = 0x10,
  161. ATime = 0x20,
  162. MTime = 0x40,
  163. CTime = 0x80,
  164. Ino = 0x100,
  165. Size = 0x200,
  166. Blocks = 0x400,
  167. BTime = 0x800,
  168. Gen = 0x1000,
  169. DataVersion = 0x2000,
  170. Basic = 0x7ff,
  171. All = 0x3fff
  172. };
  173. enum class SetAttrMask : u64 {
  174. Mode = 0x1,
  175. UID = 0x2,
  176. GID = 0x4,
  177. Size = 0x8,
  178. ATime = 0x10,
  179. MTime = 0x20,
  180. CTime = 0x40,
  181. ATimeSet = 0x80,
  182. MTimeSet = 0x100
  183. };
  184. // Mode in which the file is already open, using SerenityOS constants.
  185. int m_open_mode { 0 };
  186. KResult ensure_open_for_mode(int mode);
  187. Plan9FS& fs() { return reinterpret_cast<Plan9FS&>(Inode::fs()); }
  188. Plan9FS& fs() const
  189. {
  190. return const_cast<Plan9FS&>(reinterpret_cast<const Plan9FS&>(Inode::fs()));
  191. }
  192. };
  193. }