Plan9FileSystem.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/FileSystem/Plan9FileSystem.h>
  7. #include <Kernel/Process.h>
  8. namespace Kernel {
  9. ErrorOr<NonnullLockRefPtr<FileSystem>> Plan9FS::try_create(OpenFileDescription& file_description)
  10. {
  11. return TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) Plan9FS(file_description)));
  12. }
  13. Plan9FS::Plan9FS(OpenFileDescription& file_description)
  14. : FileBackedFileSystem(file_description)
  15. , m_completion_blocker(*this)
  16. {
  17. }
  18. ErrorOr<void> Plan9FS::prepare_to_clear_last_mount()
  19. {
  20. // FIXME: Do proper cleaning here.
  21. return {};
  22. }
  23. Plan9FS::~Plan9FS()
  24. {
  25. // Make sure to destroy the root inode before the FS gets destroyed.
  26. if (m_root_inode) {
  27. VERIFY(m_root_inode->ref_count() == 1);
  28. m_root_inode = nullptr;
  29. }
  30. }
  31. class Plan9FS::Message {
  32. public:
  33. enum class Type : u8 {
  34. // 9P2000.L
  35. Tlerror = 6,
  36. Rlerror = 7,
  37. Tstatfs = 8,
  38. Rstatfs = 9,
  39. Tlopen = 12,
  40. Rlopen = 13,
  41. Tlcreate = 14,
  42. Rlcreate = 15,
  43. Tsymlink = 16,
  44. Rsymlink = 17,
  45. Tmknod = 18,
  46. Rmknod = 19,
  47. Trename = 20,
  48. Rrename = 21,
  49. Treadlink = 22,
  50. Rreadlink = 23,
  51. Tgetattr = 24,
  52. Rgetattr = 25,
  53. Tsetattr = 26,
  54. Rsetattr = 27,
  55. Txattrwalk = 30,
  56. Rxattrwalk = 31,
  57. Txattrcreate = 32,
  58. Rxattrcreate = 33,
  59. Treaddir = 40,
  60. Rreaddir = 41,
  61. Tfsync = 50,
  62. Rfsync = 51,
  63. Tlock = 52,
  64. Rlock = 53,
  65. Tgetlock = 54,
  66. Rgetlock = 55,
  67. Tlink = 70,
  68. Rlink = 71,
  69. Tmkdir = 72,
  70. Rmkdir = 73,
  71. Trenameat = 74,
  72. Rrenameat = 75,
  73. Tunlinkat = 76,
  74. Runlinkat = 77,
  75. // 9P2000
  76. Tversion = 100,
  77. Rversion = 101,
  78. Tauth = 102,
  79. Rauth = 103,
  80. Tattach = 104,
  81. Rattach = 105,
  82. Terror = 106,
  83. Rerror = 107,
  84. Tflush = 108,
  85. Rflush = 109,
  86. Twalk = 110,
  87. Rwalk = 111,
  88. Topen = 112,
  89. Ropen = 113,
  90. Tcreate = 114,
  91. Rcreate = 115,
  92. Tread = 116,
  93. Rread = 117,
  94. Twrite = 118,
  95. Rwrite = 119,
  96. Tclunk = 120,
  97. Rclunk = 121,
  98. Tremove = 122,
  99. Rremove = 123,
  100. Tstat = 124,
  101. Rstat = 125,
  102. Twstat = 126,
  103. Rwstat = 127
  104. };
  105. class Decoder {
  106. public:
  107. explicit Decoder(StringView data)
  108. : m_data(data)
  109. {
  110. }
  111. Decoder& operator>>(u8&);
  112. Decoder& operator>>(u16&);
  113. Decoder& operator>>(u32&);
  114. Decoder& operator>>(u64&);
  115. Decoder& operator>>(StringView&);
  116. Decoder& operator>>(qid&);
  117. StringView read_data();
  118. bool has_more_data() const { return !m_data.is_empty(); }
  119. private:
  120. StringView m_data;
  121. template<typename N>
  122. Decoder& read_number(N& number)
  123. {
  124. VERIFY(sizeof(number) <= m_data.length());
  125. memcpy(&number, m_data.characters_without_null_termination(), sizeof(number));
  126. m_data = m_data.substring_view(sizeof(number), m_data.length() - sizeof(number));
  127. return *this;
  128. }
  129. };
  130. Message& operator<<(u8);
  131. Message& operator<<(u16);
  132. Message& operator<<(u32);
  133. Message& operator<<(u64);
  134. Message& operator<<(StringView);
  135. void append_data(StringView);
  136. template<typename T>
  137. Message& operator>>(T& t)
  138. {
  139. VERIFY(m_have_been_built);
  140. m_built.decoder >> t;
  141. return *this;
  142. }
  143. StringView read_data()
  144. {
  145. VERIFY(m_have_been_built);
  146. return m_built.decoder.read_data();
  147. }
  148. Type type() const { return m_type; }
  149. u16 tag() const { return m_tag; }
  150. Message(Plan9FS&, Type);
  151. Message(NonnullOwnPtr<KBuffer>&&);
  152. ~Message();
  153. Message& operator=(Message&&);
  154. KBuffer const& build();
  155. static constexpr size_t max_header_size = 24;
  156. private:
  157. template<typename N>
  158. Message& append_number(N number)
  159. {
  160. VERIFY(!m_have_been_built);
  161. // FIXME: Handle append failure.
  162. (void)m_builder.append(reinterpret_cast<char const*>(&number), sizeof(number));
  163. return *this;
  164. }
  165. union {
  166. KBufferBuilder m_builder;
  167. struct {
  168. NonnullOwnPtr<KBuffer> buffer;
  169. Decoder decoder;
  170. } m_built;
  171. };
  172. u16 m_tag { 0 };
  173. Type m_type { 0 };
  174. bool m_have_been_built { false };
  175. };
  176. bool Plan9FS::is_initialized_while_locked()
  177. {
  178. VERIFY(m_lock.is_locked());
  179. return !m_root_inode.is_null();
  180. }
  181. ErrorOr<void> Plan9FS::initialize_while_locked()
  182. {
  183. VERIFY(m_lock.is_locked());
  184. VERIFY(!is_initialized_while_locked());
  185. ensure_thread();
  186. Message version_message { *this, Message::Type::Tversion };
  187. version_message << (u32)m_max_message_size << "9P2000.L"sv;
  188. TRY(post_message_and_wait_for_a_reply(version_message));
  189. u32 msize;
  190. StringView remote_protocol_version;
  191. version_message >> msize >> remote_protocol_version;
  192. dbgln("Remote supports msize={} and protocol version {}", msize, remote_protocol_version);
  193. m_remote_protocol_version = parse_protocol_version(remote_protocol_version);
  194. m_max_message_size = min(m_max_message_size, (size_t)msize);
  195. // TODO: auth
  196. u32 root_fid = allocate_fid();
  197. Message attach_message { *this, Message::Type::Tattach };
  198. // FIXME: This needs a user name and an "export" name; but how do we get them?
  199. // Perhaps initialize() should accept a string of FS-specific options...
  200. attach_message << root_fid << (u32)-1 << "sergey"sv
  201. << "/"sv;
  202. if (m_remote_protocol_version >= ProtocolVersion::v9P2000u)
  203. attach_message << (u32)-1;
  204. TRY(post_message_and_wait_for_a_reply(attach_message));
  205. m_root_inode = TRY(Plan9FSInode::try_create(*this, root_fid));
  206. return {};
  207. }
  208. Plan9FS::ProtocolVersion Plan9FS::parse_protocol_version(StringView s) const
  209. {
  210. if (s == "9P2000.L")
  211. return ProtocolVersion::v9P2000L;
  212. if (s == "9P2000.u")
  213. return ProtocolVersion::v9P2000u;
  214. return ProtocolVersion::v9P2000;
  215. }
  216. Inode& Plan9FS::root_inode()
  217. {
  218. return *m_root_inode;
  219. }
  220. Plan9FS::Message& Plan9FS::Message::operator<<(u8 number)
  221. {
  222. return append_number(number);
  223. }
  224. Plan9FS::Message& Plan9FS::Message::operator<<(u16 number)
  225. {
  226. return append_number(number);
  227. }
  228. Plan9FS::Message& Plan9FS::Message::operator<<(u32 number)
  229. {
  230. return append_number(number);
  231. }
  232. Plan9FS::Message& Plan9FS::Message::operator<<(u64 number)
  233. {
  234. return append_number(number);
  235. }
  236. Plan9FS::Message& Plan9FS::Message::operator<<(StringView string)
  237. {
  238. *this << static_cast<u16>(string.length());
  239. // FIXME: Handle append failure.
  240. (void)m_builder.append(string);
  241. return *this;
  242. }
  243. void Plan9FS::Message::append_data(StringView data)
  244. {
  245. *this << static_cast<u32>(data.length());
  246. // FIXME: Handle append failure.
  247. (void)m_builder.append(data);
  248. }
  249. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u8& number)
  250. {
  251. return read_number(number);
  252. }
  253. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u16& number)
  254. {
  255. return read_number(number);
  256. }
  257. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u32& number)
  258. {
  259. return read_number(number);
  260. }
  261. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u64& number)
  262. {
  263. return read_number(number);
  264. }
  265. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(qid& qid)
  266. {
  267. return *this >> qid.type >> qid.version >> qid.path;
  268. }
  269. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(StringView& string)
  270. {
  271. u16 length;
  272. *this >> length;
  273. VERIFY(length <= m_data.length());
  274. string = m_data.substring_view(0, length);
  275. m_data = m_data.substring_view_starting_after_substring(string);
  276. return *this;
  277. }
  278. StringView Plan9FS::Message::Decoder::read_data()
  279. {
  280. u32 length;
  281. *this >> length;
  282. VERIFY(length <= m_data.length());
  283. auto data = m_data.substring_view(0, length);
  284. m_data = m_data.substring_view_starting_after_substring(data);
  285. return data;
  286. }
  287. Plan9FS::Message::Message(Plan9FS& fs, Type type)
  288. : m_builder(KBufferBuilder::try_create().release_value()) // FIXME: Don't assume KBufferBuilder allocation success.
  289. , m_tag(fs.allocate_tag())
  290. , m_type(type)
  291. , m_have_been_built(false)
  292. {
  293. u32 size_placeholder = 0;
  294. *this << size_placeholder << (u8)type << m_tag;
  295. }
  296. Plan9FS::Message::Message(NonnullOwnPtr<KBuffer>&& buffer)
  297. : m_built { move(buffer), Decoder({ buffer->bytes() }) }
  298. , m_have_been_built(true)
  299. {
  300. u32 size;
  301. u8 raw_type;
  302. *this >> size >> raw_type >> m_tag;
  303. m_type = (Type)raw_type;
  304. }
  305. Plan9FS::Message::~Message()
  306. {
  307. if (m_have_been_built) {
  308. m_built.buffer.~NonnullOwnPtr<KBuffer>();
  309. m_built.decoder.~Decoder();
  310. } else {
  311. m_builder.~KBufferBuilder();
  312. }
  313. }
  314. Plan9FS::Message& Plan9FS::Message::operator=(Message&& message)
  315. {
  316. m_tag = message.m_tag;
  317. m_type = message.m_type;
  318. if (m_have_been_built) {
  319. m_built.buffer.~NonnullOwnPtr<KBuffer>();
  320. m_built.decoder.~Decoder();
  321. } else {
  322. m_builder.~KBufferBuilder();
  323. }
  324. m_have_been_built = message.m_have_been_built;
  325. if (m_have_been_built) {
  326. new (&m_built.buffer) NonnullOwnPtr<KBuffer>(move(message.m_built.buffer));
  327. new (&m_built.decoder) Decoder(move(message.m_built.decoder));
  328. } else {
  329. new (&m_builder) KBufferBuilder(move(message.m_builder));
  330. }
  331. return *this;
  332. }
  333. KBuffer const& Plan9FS::Message::build()
  334. {
  335. VERIFY(!m_have_been_built);
  336. auto tmp_buffer = m_builder.build();
  337. // FIXME: We should not assume success here.
  338. VERIFY(tmp_buffer);
  339. m_have_been_built = true;
  340. m_builder.~KBufferBuilder();
  341. new (&m_built.buffer) NonnullOwnPtr<KBuffer>(tmp_buffer.release_nonnull());
  342. new (&m_built.decoder) Decoder({ m_built.buffer->data(), m_built.buffer->size() });
  343. u32* size = reinterpret_cast<u32*>(m_built.buffer->data());
  344. *size = m_built.buffer->size();
  345. return *m_built.buffer;
  346. }
  347. Plan9FS::ReceiveCompletion::ReceiveCompletion(u16 tag)
  348. : tag(tag)
  349. {
  350. }
  351. Plan9FS::ReceiveCompletion::~ReceiveCompletion() = default;
  352. bool Plan9FS::Blocker::unblock(u16 tag)
  353. {
  354. {
  355. SpinlockLocker lock(m_lock);
  356. if (m_did_unblock)
  357. return false;
  358. m_did_unblock = true;
  359. if (m_completion->tag != tag)
  360. return false;
  361. if (!m_completion->result.is_error())
  362. m_message = move(*m_completion->message);
  363. }
  364. return unblock();
  365. }
  366. bool Plan9FS::Blocker::setup_blocker()
  367. {
  368. return add_to_blocker_set(m_fs.m_completion_blocker);
  369. }
  370. void Plan9FS::Blocker::will_unblock_immediately_without_blocking(UnblockImmediatelyReason)
  371. {
  372. {
  373. SpinlockLocker lock(m_lock);
  374. if (m_did_unblock)
  375. return;
  376. }
  377. m_fs.m_completion_blocker.try_unblock(*this);
  378. }
  379. bool Plan9FS::Blocker::is_completed() const
  380. {
  381. SpinlockLocker lock(m_completion->lock);
  382. return m_completion->completed;
  383. }
  384. bool Plan9FS::Plan9FSBlockerSet::should_add_blocker(Thread::Blocker& b, void*)
  385. {
  386. // NOTE: m_lock is held already!
  387. auto& blocker = static_cast<Blocker&>(b);
  388. return !blocker.is_completed();
  389. }
  390. void Plan9FS::Plan9FSBlockerSet::unblock_completed(u16 tag)
  391. {
  392. unblock_all_blockers_whose_conditions_are_met([&](Thread::Blocker& b, void*, bool&) {
  393. VERIFY(b.blocker_type() == Thread::Blocker::Type::Plan9FS);
  394. auto& blocker = static_cast<Blocker&>(b);
  395. return blocker.unblock(tag);
  396. });
  397. }
  398. void Plan9FS::Plan9FSBlockerSet::unblock_all()
  399. {
  400. unblock_all_blockers_whose_conditions_are_met([&](Thread::Blocker& b, void*, bool&) {
  401. VERIFY(b.blocker_type() == Thread::Blocker::Type::Plan9FS);
  402. auto& blocker = static_cast<Blocker&>(b);
  403. return blocker.unblock();
  404. });
  405. }
  406. void Plan9FS::Plan9FSBlockerSet::try_unblock(Plan9FS::Blocker& blocker)
  407. {
  408. if (m_fs.is_complete(*blocker.completion())) {
  409. SpinlockLocker lock(m_lock);
  410. blocker.unblock(blocker.completion()->tag);
  411. }
  412. }
  413. bool Plan9FS::is_complete(ReceiveCompletion const& completion)
  414. {
  415. MutexLocker locker(m_lock);
  416. if (m_completions.contains(completion.tag)) {
  417. // If it's still in the map then it can't be complete
  418. VERIFY(!completion.completed);
  419. return false;
  420. }
  421. // if it's not in the map anymore, it must be complete. But we MUST
  422. // hold m_lock to be able to check completion.completed!
  423. VERIFY(completion.completed);
  424. return true;
  425. }
  426. ErrorOr<void> Plan9FS::post_message(Message& message, LockRefPtr<ReceiveCompletion> completion)
  427. {
  428. auto const& buffer = message.build();
  429. u8 const* data = buffer.data();
  430. size_t size = buffer.size();
  431. auto& description = file_description();
  432. MutexLocker locker(m_send_lock);
  433. if (completion) {
  434. // Save the completion record *before* we send the message. This
  435. // ensures that it exists when the thread reads the response
  436. MutexLocker locker(m_lock);
  437. auto tag = completion->tag;
  438. m_completions.set(tag, completion.release_nonnull());
  439. // TODO: What if there is a collision? Do we need to wait until
  440. // the existing record with the tag completes before queueing
  441. // this one?
  442. }
  443. while (size > 0) {
  444. if (!description.can_write()) {
  445. auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
  446. if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted())
  447. return EINTR;
  448. }
  449. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>(data));
  450. auto nwritten = TRY(description.write(data_buffer, size));
  451. data += nwritten;
  452. size -= nwritten;
  453. }
  454. return {};
  455. }
  456. ErrorOr<void> Plan9FS::do_read(u8* data, size_t size)
  457. {
  458. auto& description = file_description();
  459. while (size > 0) {
  460. if (!description.can_read()) {
  461. auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
  462. if (Thread::current()->block<Thread::ReadBlocker>({}, description, unblock_flags).was_interrupted())
  463. return EINTR;
  464. }
  465. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data);
  466. auto nread = TRY(description.read(data_buffer, size));
  467. if (nread == 0)
  468. return EIO;
  469. data += nread;
  470. size -= nread;
  471. }
  472. return {};
  473. }
  474. ErrorOr<void> Plan9FS::read_and_dispatch_one_message()
  475. {
  476. struct [[gnu::packed]] Header {
  477. u32 size;
  478. u8 type;
  479. u16 tag;
  480. };
  481. Header header;
  482. TRY(do_read(reinterpret_cast<u8*>(&header), sizeof(header)));
  483. auto buffer = TRY(KBuffer::try_create_with_size("Plan9FS: Message read buffer"sv, header.size, Memory::Region::Access::ReadWrite));
  484. // Copy the already read header into the buffer.
  485. memcpy(buffer->data(), &header, sizeof(header));
  486. TRY(do_read(buffer->data() + sizeof(header), header.size - sizeof(header)));
  487. MutexLocker locker(m_lock);
  488. auto optional_completion = m_completions.get(header.tag);
  489. if (optional_completion.has_value()) {
  490. auto* completion = optional_completion.value();
  491. SpinlockLocker lock(completion->lock);
  492. completion->result = {};
  493. completion->message = adopt_own_if_nonnull(new (nothrow) Message { move(buffer) });
  494. completion->completed = true;
  495. m_completions.remove(header.tag);
  496. m_completion_blocker.unblock_completed(header.tag);
  497. } else {
  498. dbgln("Received a 9p message of type {} with an unexpected tag {}, dropping", header.type, header.tag);
  499. }
  500. return {};
  501. }
  502. ErrorOr<void> Plan9FS::post_message_and_explicitly_ignore_reply(Message& message)
  503. {
  504. return post_message(message, {});
  505. }
  506. ErrorOr<void> Plan9FS::post_message_and_wait_for_a_reply(Message& message)
  507. {
  508. auto request_type = message.type();
  509. auto tag = message.tag();
  510. auto completion = adopt_lock_ref(*new ReceiveCompletion(tag));
  511. TRY(post_message(message, completion));
  512. if (Thread::current()->block<Plan9FS::Blocker>({}, *this, message, completion).was_interrupted())
  513. return EINTR;
  514. if (completion->result.is_error()) {
  515. dbgln("Plan9FS: Message was aborted with error {}", completion->result.error());
  516. return EIO;
  517. }
  518. auto reply_type = message.type();
  519. if (reply_type == Message::Type::Rlerror) {
  520. // Contains a numerical Linux errno; hopefully our errno numbers match.
  521. u32 error_code;
  522. message >> error_code;
  523. return Error::from_errno((ErrnoCode)error_code);
  524. }
  525. if (reply_type == Message::Type::Rerror) {
  526. // Contains an error message. We could attempt to parse it, but for now
  527. // we simply return EIO instead. In 9P200.u, it can also contain a
  528. // numerical errno in an unspecified encoding; we ignore those too.
  529. StringView error_name;
  530. message >> error_name;
  531. dbgln("Plan9FS: Received error name {}", error_name);
  532. return EIO;
  533. }
  534. if ((u8)reply_type != (u8)request_type + 1) {
  535. // Other than those error messages. we only expect the matching reply
  536. // message type.
  537. dbgln("Plan9FS: Received unexpected message type {} in response to {}", (u8)reply_type, (u8)request_type);
  538. return EIO;
  539. }
  540. return {};
  541. }
  542. size_t Plan9FS::adjust_buffer_size(size_t size) const
  543. {
  544. size_t max_size = m_max_message_size - Message::max_header_size;
  545. return min(size, max_size);
  546. }
  547. void Plan9FS::thread_main()
  548. {
  549. dbgln("Plan9FS: Thread running");
  550. do {
  551. auto result = read_and_dispatch_one_message();
  552. if (result.is_error()) {
  553. // If we fail to read, wake up everyone with an error.
  554. MutexLocker locker(m_lock);
  555. for (auto& it : m_completions) {
  556. it.value->result = result;
  557. it.value->completed = true;
  558. }
  559. m_completions.clear();
  560. m_completion_blocker.unblock_all();
  561. dbgln("Plan9FS: Thread terminating, error reading");
  562. return;
  563. }
  564. } while (!m_thread_shutdown);
  565. dbgln("Plan9FS: Thread terminating");
  566. }
  567. void Plan9FS::ensure_thread()
  568. {
  569. SpinlockLocker lock(m_thread_lock);
  570. if (!m_thread_running.exchange(true, AK::MemoryOrder::memory_order_acq_rel)) {
  571. auto process_name = KString::try_create("Plan9FS"sv);
  572. if (process_name.is_error())
  573. TODO();
  574. (void)Process::create_kernel_process(m_thread, process_name.release_value(), [&]() {
  575. thread_main();
  576. m_thread_running.store(false, AK::MemoryOrder::memory_order_release);
  577. });
  578. }
  579. }
  580. Plan9FSInode::Plan9FSInode(Plan9FS& fs, u32 fid)
  581. : Inode(fs, fid)
  582. {
  583. }
  584. ErrorOr<NonnullLockRefPtr<Plan9FSInode>> Plan9FSInode::try_create(Plan9FS& fs, u32 fid)
  585. {
  586. return adopt_nonnull_lock_ref_or_enomem(new (nothrow) Plan9FSInode(fs, fid));
  587. }
  588. Plan9FSInode::~Plan9FSInode()
  589. {
  590. Plan9FS::Message clunk_request { fs(), Plan9FS::Message::Type::Tclunk };
  591. clunk_request << fid();
  592. // FIXME: Should we observe this error somehow?
  593. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(clunk_request);
  594. }
  595. ErrorOr<void> Plan9FSInode::ensure_open_for_mode(int mode)
  596. {
  597. bool use_lopen = fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L;
  598. u32 l_mode = 0;
  599. u8 p9_mode = 0;
  600. {
  601. MutexLocker locker(m_inode_lock);
  602. // If it's already open in this mode, we're done.
  603. if ((m_open_mode & mode) == mode)
  604. return {};
  605. m_open_mode |= mode;
  606. if ((m_open_mode & O_RDWR) == O_RDWR) {
  607. l_mode |= 2;
  608. p9_mode |= 2;
  609. } else if (m_open_mode & O_WRONLY) {
  610. l_mode |= 1;
  611. p9_mode |= 1;
  612. } else if (m_open_mode & O_RDONLY) {
  613. // Leave the values at 0.
  614. }
  615. }
  616. if (use_lopen) {
  617. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tlopen };
  618. message << fid() << l_mode;
  619. return fs().post_message_and_wait_for_a_reply(message);
  620. }
  621. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Topen };
  622. message << fid() << p9_mode;
  623. return fs().post_message_and_wait_for_a_reply(message);
  624. }
  625. ErrorOr<size_t> Plan9FSInode::read_bytes_locked(off_t offset, size_t size, UserOrKernelBuffer& buffer, OpenFileDescription*) const
  626. {
  627. TRY(const_cast<Plan9FSInode&>(*this).ensure_open_for_mode(O_RDONLY));
  628. size = fs().adjust_buffer_size(size);
  629. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treadlink };
  630. StringView data;
  631. // Try readlink first.
  632. bool readlink_succeeded = false;
  633. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L && offset == 0) {
  634. message << fid();
  635. if (auto result = fs().post_message_and_wait_for_a_reply(message); !result.is_error()) {
  636. readlink_succeeded = true;
  637. message >> data;
  638. }
  639. }
  640. if (!readlink_succeeded) {
  641. message = Plan9FS::Message { fs(), Plan9FS::Message::Type::Tread };
  642. message << fid() << (u64)offset << (u32)size;
  643. TRY(fs().post_message_and_wait_for_a_reply(message));
  644. data = message.read_data();
  645. }
  646. // Guard against the server returning more data than requested.
  647. size_t nread = min(data.length(), size);
  648. TRY(buffer.write(data.characters_without_null_termination(), nread));
  649. return nread;
  650. }
  651. ErrorOr<size_t> Plan9FSInode::write_bytes_locked(off_t offset, size_t size, UserOrKernelBuffer const& data, OpenFileDescription*)
  652. {
  653. TRY(ensure_open_for_mode(O_WRONLY));
  654. size = fs().adjust_buffer_size(size);
  655. auto data_copy = TRY(data.try_copy_into_kstring(size)); // FIXME: this seems ugly
  656. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twrite };
  657. message << fid() << (u64)offset;
  658. message.append_data(data_copy->view());
  659. TRY(fs().post_message_and_wait_for_a_reply(message));
  660. u32 nwritten;
  661. message >> nwritten;
  662. return nwritten;
  663. }
  664. InodeMetadata Plan9FSInode::metadata() const
  665. {
  666. InodeMetadata metadata;
  667. metadata.inode = identifier();
  668. // 9P2000.L; TODO: 9P2000 & 9P2000.u
  669. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tgetattr };
  670. message << fid() << (u64)GetAttrMask::Basic;
  671. auto result = fs().post_message_and_wait_for_a_reply(message);
  672. if (result.is_error()) {
  673. // Just return blank metadata; hopefully that's enough to result in an
  674. // error at some upper layer. Ideally, there would be a way for
  675. // Inode::metadata() to return failure.
  676. return metadata;
  677. }
  678. u64 valid;
  679. Plan9FS::qid qid;
  680. u32 mode;
  681. u32 uid;
  682. u32 gid;
  683. u64 nlink;
  684. u64 rdev;
  685. u64 size;
  686. u64 blksize;
  687. u64 blocks;
  688. message >> valid >> qid >> mode >> uid >> gid >> nlink >> rdev >> size >> blksize >> blocks;
  689. // TODO: times...
  690. if (valid & (u64)GetAttrMask::Mode)
  691. metadata.mode = mode;
  692. if (valid & (u64)GetAttrMask::NLink)
  693. metadata.link_count = nlink;
  694. #if 0
  695. // FIXME: Map UID/GID somehow? Or what do we do?
  696. if (valid & (u64)GetAttrMask::UID)
  697. metadata.uid = uid;
  698. if (valid & (u64)GetAttrMask::GID)
  699. metadata.uid = gid;
  700. // FIXME: What about device nodes?
  701. if (valid & (u64)GetAttrMask::RDev)
  702. metadata.encoded_device = 0; // TODO
  703. #endif
  704. if (valid & (u64)GetAttrMask::Size)
  705. metadata.size = size;
  706. if (valid & (u64)GetAttrMask::Blocks) {
  707. metadata.block_size = blksize;
  708. metadata.block_count = blocks;
  709. }
  710. return metadata;
  711. }
  712. ErrorOr<void> Plan9FSInode::flush_metadata()
  713. {
  714. // Do nothing.
  715. return {};
  716. }
  717. ErrorOr<void> Plan9FSInode::traverse_as_directory(Function<ErrorOr<void>(FileSystem::DirectoryEntryView const&)> callback) const
  718. {
  719. // TODO: Should we synthesize "." and ".." here?
  720. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  721. // Start by cloning the fid and opening it.
  722. auto clone_fid = fs().allocate_fid();
  723. {
  724. Plan9FS::Message clone_message { fs(), Plan9FS::Message::Type::Twalk };
  725. clone_message << fid() << clone_fid << (u16)0;
  726. TRY(fs().post_message_and_wait_for_a_reply(clone_message));
  727. Plan9FS::Message open_message { fs(), Plan9FS::Message::Type::Tlopen };
  728. open_message << clone_fid << (u32)0;
  729. auto result = fs().post_message_and_wait_for_a_reply(open_message);
  730. if (result.is_error()) {
  731. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  732. close_message << clone_fid;
  733. // FIXME: Should we observe this error?
  734. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(close_message);
  735. return result;
  736. }
  737. }
  738. u64 offset = 0;
  739. u32 count = fs().adjust_buffer_size(8 * MiB);
  740. ErrorOr<void> result;
  741. while (true) {
  742. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treaddir };
  743. message << clone_fid << offset << count;
  744. result = fs().post_message_and_wait_for_a_reply(message);
  745. if (result.is_error())
  746. break;
  747. StringView data = message.read_data();
  748. if (data.is_empty()) {
  749. // We've reached the end.
  750. break;
  751. }
  752. for (Plan9FS::Message::Decoder decoder { data }; decoder.has_more_data();) {
  753. Plan9FS::qid qid;
  754. u8 type;
  755. StringView name;
  756. decoder >> qid >> offset >> type >> name;
  757. result = callback({ name, { fsid(), fs().allocate_fid() }, 0 });
  758. if (result.is_error())
  759. break;
  760. }
  761. if (result.is_error())
  762. break;
  763. }
  764. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  765. close_message << clone_fid;
  766. // FIXME: Should we observe this error?
  767. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(close_message);
  768. return result;
  769. }
  770. // TODO
  771. return ENOTIMPL;
  772. }
  773. ErrorOr<NonnullLockRefPtr<Inode>> Plan9FSInode::lookup(StringView name)
  774. {
  775. u32 newfid = fs().allocate_fid();
  776. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk };
  777. message << fid() << newfid << (u16)1 << name;
  778. TRY(fs().post_message_and_wait_for_a_reply(message));
  779. return TRY(Plan9FSInode::try_create(fs(), newfid));
  780. }
  781. ErrorOr<NonnullLockRefPtr<Inode>> Plan9FSInode::create_child(StringView, mode_t, dev_t, UserID, GroupID)
  782. {
  783. // TODO
  784. return ENOTIMPL;
  785. }
  786. ErrorOr<void> Plan9FSInode::add_child(Inode&, StringView, mode_t)
  787. {
  788. // TODO
  789. return ENOTIMPL;
  790. }
  791. ErrorOr<void> Plan9FSInode::remove_child(StringView)
  792. {
  793. // TODO
  794. return ENOTIMPL;
  795. }
  796. ErrorOr<void> Plan9FSInode::chmod(mode_t)
  797. {
  798. // TODO
  799. return ENOTIMPL;
  800. }
  801. ErrorOr<void> Plan9FSInode::chown(UserID, GroupID)
  802. {
  803. // TODO
  804. return ENOTIMPL;
  805. }
  806. ErrorOr<void> Plan9FSInode::truncate(u64 new_size)
  807. {
  808. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  809. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tsetattr };
  810. SetAttrMask valid = SetAttrMask::Size;
  811. u32 mode = 0;
  812. u32 uid = 0;
  813. u32 gid = 0;
  814. u64 atime_sec = 0;
  815. u64 atime_nsec = 0;
  816. u64 mtime_sec = 0;
  817. u64 mtime_nsec = 0;
  818. message << fid() << (u64)valid << mode << uid << gid << new_size << atime_sec << atime_nsec << mtime_sec << mtime_nsec;
  819. return fs().post_message_and_wait_for_a_reply(message);
  820. }
  821. // TODO: wstat version
  822. return {};
  823. }
  824. }