Plan9FileSystem.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. struct ReceiveCompletion {
  57. Plan9FS& fs;
  58. Message& message;
  59. KResult& result;
  60. Atomic<bool> completed;
  61. };
  62. class Blocker final : public Thread::Blocker {
  63. public:
  64. Blocker(ReceiveCompletion& completion)
  65. : m_completion(completion)
  66. {
  67. }
  68. virtual bool should_unblock(Thread&) override;
  69. virtual const char* state_string() const override { return "Waiting"; }
  70. private:
  71. ReceiveCompletion& m_completion;
  72. };
  73. virtual const char* class_name() const override { return "Plan9FS"; }
  74. KResult post_message(Message&);
  75. KResult do_read(u8* buffer, size_t);
  76. KResult read_and_dispatch_one_message();
  77. KResult wait_for_specific_message(u16 tag, Message& out_message);
  78. KResult post_message_and_wait_for_a_reply(Message&, bool auto_convert_error_reply_to_error = true);
  79. KResult post_message_and_explicitly_ignore_reply(Message&);
  80. ProtocolVersion parse_protocol_version(const StringView&) const;
  81. ssize_t adjust_buffer_size(ssize_t size) const;
  82. RefPtr<Plan9FSInode> m_root_inode;
  83. Atomic<u16> m_next_tag { (u16)-1 };
  84. Atomic<u32> m_next_fid { 1 };
  85. ProtocolVersion m_remote_protocol_version { ProtocolVersion::v9P2000 };
  86. size_t m_max_message_size { 4 * KiB };
  87. Lock m_send_lock { "Plan9FS send" };
  88. Atomic<bool> m_someone_is_reading { false };
  89. HashMap<u16, ReceiveCompletion*> m_completions;
  90. HashTable<u16> m_tags_to_ignore;
  91. };
  92. class Plan9FSInode final : public Inode {
  93. friend class Plan9FS;
  94. public:
  95. virtual ~Plan9FSInode() override;
  96. u32 fid() const { return index(); }
  97. // ^Inode
  98. virtual InodeMetadata metadata() const override;
  99. virtual void flush_metadata() override;
  100. virtual ssize_t read_bytes(off_t, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const override;
  101. virtual ssize_t write_bytes(off_t, ssize_t, const UserOrKernelBuffer& data, FileDescription*) override;
  102. virtual KResult traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const override;
  103. virtual RefPtr<Inode> lookup(StringView name) override;
  104. virtual KResultOr<NonnullRefPtr<Inode>> create_child(const String& name, mode_t, dev_t, uid_t, gid_t) override;
  105. virtual KResult add_child(Inode&, const StringView& name, mode_t) override;
  106. virtual KResult remove_child(const StringView& name) override;
  107. virtual KResultOr<size_t> directory_entry_count() const override;
  108. virtual KResult chmod(mode_t) override;
  109. virtual KResult chown(uid_t, gid_t) override;
  110. virtual KResult truncate(u64) override;
  111. private:
  112. Plan9FSInode(Plan9FS&, u32 fid);
  113. static NonnullRefPtr<Plan9FSInode> create(Plan9FS&, u32 fid);
  114. enum class GetAttrMask : u64 {
  115. Mode = 0x1,
  116. NLink = 0x2,
  117. UID = 0x4,
  118. GID = 0x8,
  119. RDev = 0x10,
  120. ATime = 0x20,
  121. MTime = 0x40,
  122. CTime = 0x80,
  123. Ino = 0x100,
  124. Size = 0x200,
  125. Blocks = 0x400,
  126. BTime = 0x800,
  127. Gen = 0x1000,
  128. DataVersion = 0x2000,
  129. Basic = 0x7ff,
  130. All = 0x3fff
  131. };
  132. enum class SetAttrMask : u64 {
  133. Mode = 0x1,
  134. UID = 0x2,
  135. GID = 0x4,
  136. Size = 0x8,
  137. ATime = 0x10,
  138. MTime = 0x20,
  139. CTime = 0x40,
  140. ATimeSet = 0x80,
  141. MTimeSet = 0x100
  142. };
  143. // Mode in which the file is already open, using SerenityOS constants.
  144. int m_open_mode { 0 };
  145. KResult ensure_open_for_mode(int mode);
  146. Plan9FS& fs() { return reinterpret_cast<Plan9FS&>(Inode::fs()); }
  147. Plan9FS& fs() const
  148. {
  149. return const_cast<Plan9FS&>(reinterpret_cast<const Plan9FS&>(Inode::fs()));
  150. }
  151. };
  152. }