Plan9FileSystem.cpp 27 KB

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