Plan9FileSystem.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. NonnullRefPtr<Plan9FS> Plan9FS::create(FileDescription& file_description)
  10. {
  11. return adopt_ref(*new Plan9FS(file_description));
  12. }
  13. Plan9FS::Plan9FS(FileDescription& 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(const 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<<(const StringView&);
  130. void append_data(const 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. const KBuffer& 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. m_builder.append(reinterpret_cast<const char*>(&number), sizeof(number));
  157. return *this;
  158. }
  159. union {
  160. KBufferBuilder m_builder;
  161. struct {
  162. NonnullOwnPtr<KBuffer> buffer;
  163. Decoder decoder;
  164. } m_built;
  165. };
  166. u16 m_tag { 0 };
  167. Type m_type { 0 };
  168. bool m_have_been_built { false };
  169. };
  170. KResult Plan9FS::initialize()
  171. {
  172. ensure_thread();
  173. Message version_message { *this, Message::Type::Tversion };
  174. version_message << (u32)m_max_message_size << "9P2000.L";
  175. auto result = post_message_and_wait_for_a_reply(version_message);
  176. if (result.is_error())
  177. return result;
  178. u32 msize;
  179. StringView remote_protocol_version;
  180. version_message >> msize >> remote_protocol_version;
  181. dbgln("Remote supports msize={} and protocol version {}", msize, remote_protocol_version);
  182. m_remote_protocol_version = parse_protocol_version(remote_protocol_version);
  183. m_max_message_size = min(m_max_message_size, (size_t)msize);
  184. // TODO: auth
  185. u32 root_fid = allocate_fid();
  186. Message attach_message { *this, Message::Type::Tattach };
  187. // FIXME: This needs a user name and an "export" name; but how do we get them?
  188. // Perhaps initialize() should accept a string of FS-specific options...
  189. attach_message << root_fid << (u32)-1 << "sergey"
  190. << "/";
  191. if (m_remote_protocol_version >= ProtocolVersion::v9P2000u)
  192. attach_message << (u32)-1;
  193. result = post_message_and_wait_for_a_reply(attach_message);
  194. if (result.is_error()) {
  195. dbgln("Attaching failed");
  196. return result;
  197. }
  198. m_root_inode = Plan9FSInode::create(*this, root_fid);
  199. if (!m_root_inode)
  200. return ENOMEM;
  201. return KSuccess;
  202. }
  203. Plan9FS::ProtocolVersion Plan9FS::parse_protocol_version(const StringView& s) const
  204. {
  205. if (s == "9P2000.L")
  206. return ProtocolVersion::v9P2000L;
  207. if (s == "9P2000.u")
  208. return ProtocolVersion::v9P2000u;
  209. return ProtocolVersion::v9P2000;
  210. }
  211. Inode& Plan9FS::root_inode()
  212. {
  213. return *m_root_inode;
  214. }
  215. Plan9FS::Message& Plan9FS::Message::operator<<(u8 number)
  216. {
  217. return append_number(number);
  218. }
  219. Plan9FS::Message& Plan9FS::Message::operator<<(u16 number)
  220. {
  221. return append_number(number);
  222. }
  223. Plan9FS::Message& Plan9FS::Message::operator<<(u32 number)
  224. {
  225. return append_number(number);
  226. }
  227. Plan9FS::Message& Plan9FS::Message::operator<<(u64 number)
  228. {
  229. return append_number(number);
  230. }
  231. Plan9FS::Message& Plan9FS::Message::operator<<(const StringView& string)
  232. {
  233. *this << static_cast<u16>(string.length());
  234. m_builder.append(string);
  235. return *this;
  236. }
  237. void Plan9FS::Message::append_data(const StringView& data)
  238. {
  239. *this << static_cast<u32>(data.length());
  240. m_builder.append(data);
  241. }
  242. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u8& number)
  243. {
  244. return read_number(number);
  245. }
  246. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u16& number)
  247. {
  248. return read_number(number);
  249. }
  250. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u32& number)
  251. {
  252. return read_number(number);
  253. }
  254. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u64& number)
  255. {
  256. return read_number(number);
  257. }
  258. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(qid& qid)
  259. {
  260. return *this >> qid.type >> qid.version >> qid.path;
  261. }
  262. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(StringView& string)
  263. {
  264. u16 length;
  265. *this >> length;
  266. VERIFY(length <= m_data.length());
  267. string = m_data.substring_view(0, length);
  268. m_data = m_data.substring_view_starting_after_substring(string);
  269. return *this;
  270. }
  271. StringView Plan9FS::Message::Decoder::read_data()
  272. {
  273. u32 length;
  274. *this >> length;
  275. VERIFY(length <= m_data.length());
  276. auto data = m_data.substring_view(0, length);
  277. m_data = m_data.substring_view_starting_after_substring(data);
  278. return data;
  279. }
  280. Plan9FS::Message::Message(Plan9FS& fs, Type type)
  281. : m_builder()
  282. , m_tag(fs.allocate_tag())
  283. , m_type(type)
  284. , m_have_been_built(false)
  285. {
  286. u32 size_placeholder = 0;
  287. *this << size_placeholder << (u8)type << m_tag;
  288. }
  289. Plan9FS::Message::Message(NonnullOwnPtr<KBuffer>&& buffer)
  290. : m_built { move(buffer), Decoder({ buffer->data(), buffer->size() }) }
  291. , m_have_been_built(true)
  292. {
  293. u32 size;
  294. u8 raw_type;
  295. *this >> size >> raw_type >> m_tag;
  296. m_type = (Type)raw_type;
  297. }
  298. Plan9FS::Message::~Message()
  299. {
  300. if (m_have_been_built) {
  301. m_built.buffer.~NonnullOwnPtr<KBuffer>();
  302. m_built.decoder.~Decoder();
  303. } else {
  304. m_builder.~KBufferBuilder();
  305. }
  306. }
  307. Plan9FS::Message& Plan9FS::Message::operator=(Message&& message)
  308. {
  309. m_tag = message.m_tag;
  310. m_type = message.m_type;
  311. if (m_have_been_built) {
  312. m_built.buffer.~NonnullOwnPtr<KBuffer>();
  313. m_built.decoder.~Decoder();
  314. } else {
  315. m_builder.~KBufferBuilder();
  316. }
  317. m_have_been_built = message.m_have_been_built;
  318. if (m_have_been_built) {
  319. new (&m_built.buffer) NonnullOwnPtr<KBuffer>(move(message.m_built.buffer));
  320. new (&m_built.decoder) Decoder(move(message.m_built.decoder));
  321. } else {
  322. new (&m_builder) KBufferBuilder(move(message.m_builder));
  323. }
  324. return *this;
  325. }
  326. const KBuffer& Plan9FS::Message::build()
  327. {
  328. VERIFY(!m_have_been_built);
  329. auto tmp_buffer = m_builder.build();
  330. // FIXME: We should not assume success here.
  331. VERIFY(tmp_buffer);
  332. m_have_been_built = true;
  333. m_builder.~KBufferBuilder();
  334. new (&m_built.buffer) NonnullOwnPtr<KBuffer>(tmp_buffer.release_nonnull());
  335. new (&m_built.decoder) Decoder({ m_built.buffer->data(), m_built.buffer->size() });
  336. u32* size = reinterpret_cast<u32*>(m_built.buffer->data());
  337. *size = m_built.buffer->size();
  338. return *m_built.buffer;
  339. }
  340. Plan9FS::ReceiveCompletion::ReceiveCompletion(u16 tag)
  341. : tag(tag)
  342. {
  343. }
  344. Plan9FS::ReceiveCompletion::~ReceiveCompletion()
  345. {
  346. }
  347. bool Plan9FS::Blocker::unblock(u16 tag)
  348. {
  349. {
  350. SpinlockLocker lock(m_lock);
  351. if (m_did_unblock)
  352. return false;
  353. m_did_unblock = true;
  354. if (m_completion->tag != tag)
  355. return false;
  356. if (!m_completion->result.is_error())
  357. m_message = move(*m_completion->message);
  358. }
  359. return unblock();
  360. }
  361. void Plan9FS::Blocker::not_blocking(bool)
  362. {
  363. {
  364. SpinlockLocker lock(m_lock);
  365. if (m_did_unblock)
  366. return;
  367. }
  368. m_fs.m_completion_blocker.try_unblock(*this);
  369. }
  370. bool Plan9FS::Blocker::is_completed() const
  371. {
  372. SpinlockLocker lock(m_completion->lock);
  373. return m_completion->completed;
  374. }
  375. bool Plan9FS::Plan9FSBlockCondition::should_add_blocker(Thread::Blocker& b, void*)
  376. {
  377. // NOTE: m_lock is held already!
  378. auto& blocker = static_cast<Blocker&>(b);
  379. return !blocker.is_completed();
  380. }
  381. void Plan9FS::Plan9FSBlockCondition::unblock_completed(u16 tag)
  382. {
  383. unblock([&](Thread::Blocker& b, void*, bool&) {
  384. VERIFY(b.blocker_type() == Thread::Blocker::Type::Plan9FS);
  385. auto& blocker = static_cast<Blocker&>(b);
  386. return blocker.unblock(tag);
  387. });
  388. }
  389. void Plan9FS::Plan9FSBlockCondition::unblock_all()
  390. {
  391. unblock([&](Thread::Blocker& b, void*, bool&) {
  392. VERIFY(b.blocker_type() == Thread::Blocker::Type::Plan9FS);
  393. auto& blocker = static_cast<Blocker&>(b);
  394. return blocker.unblock();
  395. });
  396. }
  397. void Plan9FS::Plan9FSBlockCondition::try_unblock(Plan9FS::Blocker& blocker)
  398. {
  399. if (m_fs.is_complete(*blocker.completion())) {
  400. SpinlockLocker lock(m_lock);
  401. blocker.unblock(blocker.completion()->tag);
  402. }
  403. }
  404. bool Plan9FS::is_complete(const ReceiveCompletion& completion)
  405. {
  406. MutexLocker locker(m_lock);
  407. if (m_completions.contains(completion.tag)) {
  408. // If it's still in the map then it can't be complete
  409. VERIFY(!completion.completed);
  410. return false;
  411. }
  412. // if it's not in the map anymore, it must be complete. But we MUST
  413. // hold m_lock to be able to check completion.completed!
  414. VERIFY(completion.completed);
  415. return true;
  416. }
  417. KResult Plan9FS::post_message(Message& message, RefPtr<ReceiveCompletion> completion)
  418. {
  419. auto& buffer = message.build();
  420. const u8* data = buffer.data();
  421. size_t size = buffer.size();
  422. auto& description = file_description();
  423. MutexLocker locker(m_send_lock);
  424. if (completion) {
  425. // Save the completion record *before* we send the message. This
  426. // ensures that it exists when the thread reads the response
  427. MutexLocker locker(m_lock);
  428. auto tag = completion->tag;
  429. m_completions.set(tag, completion.release_nonnull());
  430. // TODO: What if there is a collision? Do we need to wait until
  431. // the existing record with the tag completes before queueing
  432. // this one?
  433. }
  434. while (size > 0) {
  435. if (!description.can_write()) {
  436. auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
  437. if (Thread::current()->block<Thread::WriteBlocker>({}, description, unblock_flags).was_interrupted())
  438. return EINTR;
  439. }
  440. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(const_cast<u8*>(data));
  441. auto nwritten_or_error = description.write(data_buffer, size);
  442. if (nwritten_or_error.is_error())
  443. return nwritten_or_error.error();
  444. auto nwritten = nwritten_or_error.value();
  445. data += nwritten;
  446. size -= nwritten;
  447. }
  448. return KSuccess;
  449. }
  450. KResult Plan9FS::do_read(u8* data, size_t size)
  451. {
  452. auto& description = file_description();
  453. while (size > 0) {
  454. if (!description.can_read()) {
  455. auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
  456. if (Thread::current()->block<Thread::ReadBlocker>({}, description, unblock_flags).was_interrupted())
  457. return EINTR;
  458. }
  459. auto data_buffer = UserOrKernelBuffer::for_kernel_buffer(data);
  460. auto nread_or_error = description.read(data_buffer, size);
  461. if (nread_or_error.is_error())
  462. return nread_or_error.error();
  463. auto nread = nread_or_error.value();
  464. if (nread == 0)
  465. return EIO;
  466. data += nread;
  467. size -= nread;
  468. }
  469. return KSuccess;
  470. }
  471. KResult Plan9FS::read_and_dispatch_one_message()
  472. {
  473. struct [[gnu::packed]] Header {
  474. u32 size;
  475. u8 type;
  476. u16 tag;
  477. };
  478. Header header;
  479. KResult result = do_read(reinterpret_cast<u8*>(&header), sizeof(header));
  480. if (result.is_error())
  481. return result;
  482. auto buffer = KBuffer::try_create_with_size(header.size, Memory::Region::Access::ReadWrite);
  483. if (!buffer)
  484. return ENOMEM;
  485. // Copy the already read header into the buffer.
  486. memcpy(buffer->data(), &header, sizeof(header));
  487. result = do_read(buffer->data() + sizeof(header), header.size - sizeof(header));
  488. if (result.is_error())
  489. return result;
  490. MutexLocker locker(m_lock);
  491. auto optional_completion = m_completions.get(header.tag);
  492. if (optional_completion.has_value()) {
  493. auto completion = optional_completion.value();
  494. SpinlockLocker lock(completion->lock);
  495. completion->result = KSuccess;
  496. completion->message = adopt_own_if_nonnull(new (nothrow) Message { buffer.release_nonnull() });
  497. completion->completed = true;
  498. m_completions.remove(header.tag);
  499. m_completion_blocker.unblock_completed(header.tag);
  500. } else {
  501. dbgln("Received a 9p message of type {} with an unexpected tag {}, dropping", header.type, header.tag);
  502. }
  503. return KSuccess;
  504. }
  505. KResult Plan9FS::post_message_and_explicitly_ignore_reply(Message& message)
  506. {
  507. return post_message(message, {});
  508. }
  509. KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message)
  510. {
  511. auto request_type = message.type();
  512. auto tag = message.tag();
  513. auto completion = adopt_ref(*new ReceiveCompletion(tag));
  514. auto result = post_message(message, completion);
  515. if (result.is_error())
  516. return result;
  517. if (Thread::current()->block<Plan9FS::Blocker>({}, *this, message, completion).was_interrupted())
  518. return EINTR;
  519. if (completion->result.is_error()) {
  520. dbgln("Plan9FS: Message was aborted with error {}", completion->result.error());
  521. return EIO;
  522. }
  523. auto reply_type = message.type();
  524. if (reply_type == Message::Type::Rlerror) {
  525. // Contains a numerical Linux errno; hopefully our errno numbers match.
  526. u32 error_code;
  527. message >> error_code;
  528. return KResult((ErrnoCode)error_code);
  529. } else if (reply_type == Message::Type::Rerror) {
  530. // Contains an error message. We could attempt to parse it, but for now
  531. // we simply return EIO instead. In 9P200.u, it can also contain a
  532. // numerical errno in an unspecified encoding; we ignore those too.
  533. StringView error_name;
  534. message >> error_name;
  535. dbgln("Plan9FS: Received error name {}", error_name);
  536. return EIO;
  537. } else if ((u8)reply_type != (u8)request_type + 1) {
  538. // Other than those error messages. we only expect the matching reply
  539. // message type.
  540. dbgln("Plan9FS: Received unexpected message type {} in response to {}", (u8)reply_type, (u8)request_type);
  541. return EIO;
  542. } else {
  543. return KSuccess;
  544. }
  545. }
  546. size_t Plan9FS::adjust_buffer_size(size_t size) const
  547. {
  548. size_t max_size = m_max_message_size - Message::max_header_size;
  549. return min(size, max_size);
  550. }
  551. void Plan9FS::thread_main()
  552. {
  553. dbgln("Plan9FS: Thread running");
  554. do {
  555. auto result = read_and_dispatch_one_message();
  556. if (result.is_error()) {
  557. // If we fail to read, wake up everyone with an error.
  558. MutexLocker locker(m_lock);
  559. for (auto& it : m_completions) {
  560. it.value->result = result;
  561. it.value->completed = true;
  562. }
  563. m_completions.clear();
  564. m_completion_blocker.unblock_all();
  565. dbgln("Plan9FS: Thread terminating, error reading");
  566. return;
  567. }
  568. } while (!m_thread_shutdown);
  569. dbgln("Plan9FS: Thread terminating");
  570. }
  571. void Plan9FS::ensure_thread()
  572. {
  573. SpinlockLocker lock(m_thread_lock);
  574. if (!m_thread_running.exchange(true, AK::MemoryOrder::memory_order_acq_rel)) {
  575. Process::create_kernel_process(m_thread, "Plan9FS", [&]() {
  576. thread_main();
  577. m_thread_running.store(false, AK::MemoryOrder::memory_order_release);
  578. });
  579. }
  580. }
  581. Plan9FSInode::Plan9FSInode(Plan9FS& fs, u32 fid)
  582. : Inode(fs, fid)
  583. {
  584. }
  585. NonnullRefPtr<Plan9FSInode> Plan9FSInode::create(Plan9FS& fs, u32 fid)
  586. {
  587. return adopt_ref(*new Plan9FSInode(fs, fid));
  588. }
  589. Plan9FSInode::~Plan9FSInode()
  590. {
  591. Plan9FS::Message clunk_request { fs(), Plan9FS::Message::Type::Tclunk };
  592. clunk_request << fid();
  593. // FIXME: Should we observe this error somehow?
  594. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(clunk_request);
  595. }
  596. KResult Plan9FSInode::ensure_open_for_mode(int mode)
  597. {
  598. bool use_lopen = fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L;
  599. u32 l_mode = 0;
  600. u8 p9_mode = 0;
  601. {
  602. MutexLocker locker(m_inode_lock);
  603. // If it's already open in this mode, we're done.
  604. if ((m_open_mode & mode) == mode)
  605. return KSuccess;
  606. m_open_mode |= mode;
  607. if ((m_open_mode & O_RDWR) == O_RDWR) {
  608. l_mode |= 2;
  609. p9_mode |= 2;
  610. } else if (m_open_mode & O_WRONLY) {
  611. l_mode |= 1;
  612. p9_mode |= 1;
  613. } else if (m_open_mode & O_RDONLY) {
  614. // Leave the values at 0.
  615. }
  616. }
  617. if (use_lopen) {
  618. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tlopen };
  619. message << fid() << l_mode;
  620. return fs().post_message_and_wait_for_a_reply(message);
  621. } else {
  622. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Topen };
  623. message << fid() << p9_mode;
  624. return fs().post_message_and_wait_for_a_reply(message);
  625. }
  626. }
  627. KResultOr<size_t> Plan9FSInode::read_bytes(off_t offset, size_t size, UserOrKernelBuffer& buffer, FileDescription*) const
  628. {
  629. auto result = const_cast<Plan9FSInode&>(*this).ensure_open_for_mode(O_RDONLY);
  630. if (result.is_error())
  631. return result;
  632. size = fs().adjust_buffer_size(size);
  633. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treadlink };
  634. StringView data;
  635. // Try readlink first.
  636. bool readlink_succeded = false;
  637. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L && offset == 0) {
  638. message << fid();
  639. result = fs().post_message_and_wait_for_a_reply(message);
  640. if (result.is_success()) {
  641. readlink_succeded = true;
  642. message >> data;
  643. }
  644. }
  645. if (!readlink_succeded) {
  646. message = Plan9FS::Message { fs(), Plan9FS::Message::Type::Tread };
  647. message << fid() << (u64)offset << (u32)size;
  648. result = fs().post_message_and_wait_for_a_reply(message);
  649. if (result.is_error())
  650. return result.error();
  651. data = message.read_data();
  652. }
  653. // Guard against the server returning more data than requested.
  654. size_t nread = min(data.length(), size);
  655. if (!buffer.write(data.characters_without_null_termination(), nread))
  656. return EFAULT;
  657. return nread;
  658. }
  659. KResultOr<size_t> Plan9FSInode::write_bytes(off_t offset, size_t size, const UserOrKernelBuffer& data, FileDescription*)
  660. {
  661. auto result = ensure_open_for_mode(O_WRONLY);
  662. if (result.is_error())
  663. return result.error();
  664. size = fs().adjust_buffer_size(size);
  665. auto data_copy = data.copy_into_string(size); // FIXME: this seems ugly
  666. if (data_copy.is_null())
  667. return EFAULT;
  668. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twrite };
  669. message << fid() << (u64)offset;
  670. message.append_data(data_copy);
  671. result = fs().post_message_and_wait_for_a_reply(message);
  672. if (result.is_error())
  673. return result.error();
  674. u32 nwritten;
  675. message >> nwritten;
  676. return nwritten;
  677. }
  678. InodeMetadata Plan9FSInode::metadata() const
  679. {
  680. InodeMetadata metadata;
  681. metadata.inode = identifier();
  682. // 9P2000.L; TODO: 9P2000 & 9P2000.u
  683. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tgetattr };
  684. message << fid() << (u64)GetAttrMask::Basic;
  685. auto result = fs().post_message_and_wait_for_a_reply(message);
  686. if (result.is_error()) {
  687. // Just return blank metadata; hopefully that's enough to result in an
  688. // error at some upper layer. Ideally, there would be a way for
  689. // Inode::metadata() to return failure.
  690. return metadata;
  691. }
  692. u64 valid;
  693. Plan9FS::qid qid;
  694. u32 mode;
  695. u32 uid;
  696. u32 gid;
  697. u64 nlink;
  698. u64 rdev;
  699. u64 size;
  700. u64 blksize;
  701. u64 blocks;
  702. message >> valid >> qid >> mode >> uid >> gid >> nlink >> rdev >> size >> blksize >> blocks;
  703. // TODO: times...
  704. if (valid & (u64)GetAttrMask::Mode)
  705. metadata.mode = mode;
  706. if (valid & (u64)GetAttrMask::NLink)
  707. metadata.link_count = nlink;
  708. #if 0
  709. // FIXME: Map UID/GID somehow? Or what do we do?
  710. if (valid & (u64)GetAttrMask::UID)
  711. metadata.uid = uid;
  712. if (valid & (u64)GetAttrMask::GID)
  713. metadata.uid = gid;
  714. // FIXME: What about device nodes?
  715. if (valid & (u64)GetAttrMask::RDev)
  716. metadata.encoded_device = 0; // TODO
  717. #endif
  718. if (valid & (u64)GetAttrMask::Size)
  719. metadata.size = size;
  720. if (valid & (u64)GetAttrMask::Blocks) {
  721. metadata.block_size = blksize;
  722. metadata.block_count = blocks;
  723. }
  724. return metadata;
  725. }
  726. void Plan9FSInode::flush_metadata()
  727. {
  728. // Do nothing.
  729. }
  730. KResult Plan9FSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  731. {
  732. KResult result = KSuccess;
  733. // TODO: Should we synthesize "." and ".." here?
  734. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  735. // Start by cloning the fid and opening it.
  736. auto clone_fid = fs().allocate_fid();
  737. {
  738. Plan9FS::Message clone_message { fs(), Plan9FS::Message::Type::Twalk };
  739. clone_message << fid() << clone_fid << (u16)0;
  740. result = fs().post_message_and_wait_for_a_reply(clone_message);
  741. if (result.is_error())
  742. return result;
  743. Plan9FS::Message open_message { fs(), Plan9FS::Message::Type::Tlopen };
  744. open_message << clone_fid << (u32)0;
  745. result = fs().post_message_and_wait_for_a_reply(open_message);
  746. if (result.is_error()) {
  747. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  748. close_message << clone_fid;
  749. // FIXME: Should we observe this error?
  750. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(close_message);
  751. return result;
  752. }
  753. }
  754. u64 offset = 0;
  755. u32 count = fs().adjust_buffer_size(8 * MiB);
  756. while (true) {
  757. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treaddir };
  758. message << clone_fid << offset << count;
  759. result = fs().post_message_and_wait_for_a_reply(message);
  760. if (result.is_error())
  761. break;
  762. StringView data = message.read_data();
  763. if (data.is_empty()) {
  764. // We've reached the end.
  765. break;
  766. }
  767. for (Plan9FS::Message::Decoder decoder { data }; decoder.has_more_data();) {
  768. Plan9FS::qid qid;
  769. u8 type;
  770. StringView name;
  771. decoder >> qid >> offset >> type >> name;
  772. callback({ name, { fsid(), fs().allocate_fid() }, 0 });
  773. }
  774. }
  775. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  776. close_message << clone_fid;
  777. // FIXME: Should we observe this error?
  778. [[maybe_unused]] auto rc = fs().post_message_and_explicitly_ignore_reply(close_message);
  779. return result;
  780. } else {
  781. // TODO
  782. return ENOTIMPL;
  783. }
  784. }
  785. KResultOr<NonnullRefPtr<Inode>> Plan9FSInode::lookup(StringView name)
  786. {
  787. u32 newfid = fs().allocate_fid();
  788. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk };
  789. message << fid() << newfid << (u16)1 << name;
  790. auto result = fs().post_message_and_wait_for_a_reply(message);
  791. if (result.is_error())
  792. return result;
  793. return Plan9FSInode::create(fs(), newfid);
  794. }
  795. KResultOr<NonnullRefPtr<Inode>> Plan9FSInode::create_child(StringView, mode_t, dev_t, uid_t, gid_t)
  796. {
  797. // TODO
  798. return ENOTIMPL;
  799. }
  800. KResult Plan9FSInode::add_child(Inode&, const StringView&, mode_t)
  801. {
  802. // TODO
  803. return ENOTIMPL;
  804. }
  805. KResult Plan9FSInode::remove_child(const StringView&)
  806. {
  807. // TODO
  808. return ENOTIMPL;
  809. }
  810. KResult Plan9FSInode::chmod(mode_t)
  811. {
  812. // TODO
  813. return ENOTIMPL;
  814. }
  815. KResult Plan9FSInode::chown(uid_t, gid_t)
  816. {
  817. // TODO
  818. return ENOTIMPL;
  819. }
  820. KResult Plan9FSInode::truncate(u64 new_size)
  821. {
  822. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  823. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tsetattr };
  824. SetAttrMask valid = SetAttrMask::Size;
  825. u32 mode = 0;
  826. u32 uid = 0;
  827. u32 gid = 0;
  828. u64 atime_sec = 0;
  829. u64 atime_nsec = 0;
  830. u64 mtime_sec = 0;
  831. u64 mtime_nsec = 0;
  832. message << fid() << (u64)valid << mode << uid << gid << new_size << atime_sec << atime_nsec << mtime_sec << mtime_nsec;
  833. return fs().post_message_and_wait_for_a_reply(message);
  834. } else {
  835. // TODO: wstat version
  836. return KSuccess;
  837. }
  838. }
  839. }