Plan9FileSystem.cpp 27 KB

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