Plan9FileSystem.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /*
  2. * Copyright (c) 2020, Sergey Bugaev <bugaevc@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/FileSystem/Plan9FileSystem.h>
  27. #include <Kernel/Process.h>
  28. namespace Kernel {
  29. NonnullRefPtr<Plan9FS> Plan9FS::create(FileDescription& file_description)
  30. {
  31. return adopt(*new Plan9FS(file_description));
  32. }
  33. Plan9FS::Plan9FS(FileDescription& file_description)
  34. : FileBackedFS(file_description)
  35. {
  36. }
  37. Plan9FS::~Plan9FS()
  38. {
  39. // Make sure to destroy the root inode before the FS gets destroyed.
  40. if (m_root_inode) {
  41. ASSERT(m_root_inode->ref_count() == 1);
  42. m_root_inode = nullptr;
  43. }
  44. }
  45. class Plan9FS::Message {
  46. public:
  47. enum class Type : u8 {
  48. // 9P2000.L
  49. Tlerror = 6,
  50. Rlerror = 7,
  51. Tstatfs = 8,
  52. Rstatfs = 9,
  53. Tlopen = 12,
  54. Rlopen = 13,
  55. Tlcreate = 14,
  56. Rlcreate = 15,
  57. Tsymlink = 16,
  58. Rsymlink = 17,
  59. Tmknod = 18,
  60. Rmknod = 19,
  61. Trename = 20,
  62. Rrename = 21,
  63. Treadlink = 22,
  64. Rreadlink = 23,
  65. Tgetattr = 24,
  66. Rgetattr = 25,
  67. Tsetattr = 26,
  68. Rsetattr = 27,
  69. Txattrwalk = 30,
  70. Rxattrwalk = 31,
  71. Txattrcreate = 32,
  72. Rxattrcreate = 33,
  73. Treaddir = 40,
  74. Rreaddir = 41,
  75. Tfsync = 50,
  76. Rfsync = 51,
  77. Tlock = 52,
  78. Rlock = 53,
  79. Tgetlock = 54,
  80. Rgetlock = 55,
  81. Tlink = 70,
  82. Rlink = 71,
  83. Tmkdir = 72,
  84. Rmkdir = 73,
  85. Trenameat = 74,
  86. Rrenameat = 75,
  87. Tunlinkat = 76,
  88. Runlinkat = 77,
  89. // 9P2000
  90. Tversion = 100,
  91. Rversion = 101,
  92. Tauth = 102,
  93. Rauth = 103,
  94. Tattach = 104,
  95. Rattach = 105,
  96. Terror = 106,
  97. Rerror = 107,
  98. Tflush = 108,
  99. Rflush = 109,
  100. Twalk = 110,
  101. Rwalk = 111,
  102. Topen = 112,
  103. Ropen = 113,
  104. Tcreate = 114,
  105. Rcreate = 115,
  106. Tread = 116,
  107. Rread = 117,
  108. Twrite = 118,
  109. Rwrite = 119,
  110. Tclunk = 120,
  111. Rclunk = 121,
  112. Tremove = 122,
  113. Rremove = 123,
  114. Tstat = 124,
  115. Rstat = 125,
  116. Twstat = 126,
  117. Rwstat = 127
  118. };
  119. class Decoder {
  120. public:
  121. explicit Decoder(const StringView& data)
  122. : m_data(data)
  123. {
  124. }
  125. Decoder& operator>>(u8&);
  126. Decoder& operator>>(u16&);
  127. Decoder& operator>>(u32&);
  128. Decoder& operator>>(u64&);
  129. Decoder& operator>>(StringView&);
  130. Decoder& operator>>(qid&);
  131. StringView read_data();
  132. bool has_more_data() const { return !m_data.is_empty(); }
  133. private:
  134. StringView m_data;
  135. template<typename N>
  136. Decoder& read_number(N& number)
  137. {
  138. ASSERT(sizeof(number) <= m_data.length());
  139. memcpy(&number, m_data.characters_without_null_termination(), sizeof(number));
  140. m_data = m_data.substring_view(sizeof(number), m_data.length() - sizeof(number));
  141. return *this;
  142. }
  143. };
  144. Message& operator<<(u8);
  145. Message& operator<<(u16);
  146. Message& operator<<(u32);
  147. Message& operator<<(u64);
  148. Message& operator<<(const StringView&);
  149. void append_data(const StringView&);
  150. template<typename T>
  151. Message& operator>>(T& t)
  152. {
  153. ASSERT(m_have_been_built);
  154. m_built.decoder >> t;
  155. return *this;
  156. }
  157. StringView read_data()
  158. {
  159. ASSERT(m_have_been_built);
  160. return m_built.decoder.read_data();
  161. }
  162. Type type() const { return m_type; }
  163. u16 tag() const { return m_tag; }
  164. Message(Plan9FS&, Type);
  165. Message(KBuffer&&);
  166. ~Message();
  167. Message& operator=(Message&&);
  168. const KBuffer& build();
  169. static constexpr ssize_t max_header_size = 24;
  170. private:
  171. template<typename N>
  172. Message& append_number(N number)
  173. {
  174. ASSERT(!m_have_been_built);
  175. m_builder.append(reinterpret_cast<const char*>(&number), sizeof(number));
  176. return *this;
  177. }
  178. union {
  179. KBufferBuilder m_builder;
  180. struct {
  181. KBuffer buffer;
  182. Decoder decoder;
  183. } m_built;
  184. };
  185. u16 m_tag { 0 };
  186. Type m_type { 0 };
  187. bool m_have_been_built { false };
  188. };
  189. bool Plan9FS::initialize()
  190. {
  191. Message version_message { *this, Message::Type::Tversion };
  192. version_message << (u32)m_max_message_size << "9P2000.L";
  193. auto result = post_message_and_wait_for_a_reply(version_message);
  194. if (result.is_error())
  195. return false;
  196. u32 msize;
  197. StringView remote_protocol_version;
  198. version_message >> msize >> remote_protocol_version;
  199. dbg() << "Remote supports msize=" << msize << " and protocol version " << remote_protocol_version;
  200. m_remote_protocol_version = parse_protocol_version(remote_protocol_version);
  201. m_max_message_size = min(m_max_message_size, (size_t)msize);
  202. // TODO: auth
  203. u32 root_fid = allocate_fid();
  204. Message attach_message { *this, Message::Type::Tattach };
  205. // FIXME: This needs a user name and an "export" name; but how do we get them?
  206. // Perhaps initialize() should accept a string of FS-specific options...
  207. attach_message << root_fid << (u32)-1 << "sergey"
  208. << "/";
  209. if (m_remote_protocol_version >= ProtocolVersion::v9P2000u)
  210. attach_message << (u32)-1;
  211. result = post_message_and_wait_for_a_reply(attach_message);
  212. if (result.is_error()) {
  213. dbg() << "Attaching failed";
  214. return false;
  215. }
  216. m_root_inode = Plan9FSInode::create(*this, root_fid);
  217. return true;
  218. }
  219. Plan9FS::ProtocolVersion Plan9FS::parse_protocol_version(const StringView& s) const
  220. {
  221. if (s == "9P2000.L")
  222. return ProtocolVersion::v9P2000L;
  223. if (s == "9P2000.u")
  224. return ProtocolVersion::v9P2000u;
  225. return ProtocolVersion::v9P2000;
  226. }
  227. NonnullRefPtr<Inode> Plan9FS::root_inode() const
  228. {
  229. return *m_root_inode;
  230. }
  231. Plan9FS::Message& Plan9FS::Message::operator<<(u8 number)
  232. {
  233. return append_number(number);
  234. }
  235. Plan9FS::Message& Plan9FS::Message::operator<<(u16 number)
  236. {
  237. return append_number(number);
  238. }
  239. Plan9FS::Message& Plan9FS::Message::operator<<(u32 number)
  240. {
  241. return append_number(number);
  242. }
  243. Plan9FS::Message& Plan9FS::Message::operator<<(u64 number)
  244. {
  245. return append_number(number);
  246. }
  247. Plan9FS::Message& Plan9FS::Message::operator<<(const StringView& string)
  248. {
  249. *this << static_cast<u16>(string.length());
  250. m_builder.append(string);
  251. return *this;
  252. }
  253. void Plan9FS::Message::append_data(const StringView& data)
  254. {
  255. *this << static_cast<u32>(data.length());
  256. m_builder.append(data);
  257. }
  258. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u8& number)
  259. {
  260. return read_number(number);
  261. }
  262. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u16& number)
  263. {
  264. return read_number(number);
  265. }
  266. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u32& number)
  267. {
  268. return read_number(number);
  269. }
  270. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(u64& number)
  271. {
  272. return read_number(number);
  273. }
  274. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(qid& qid)
  275. {
  276. return *this >> qid.type >> qid.version >> qid.path;
  277. }
  278. Plan9FS::Message::Decoder& Plan9FS::Message::Decoder::operator>>(StringView& string)
  279. {
  280. u16 length;
  281. *this >> length;
  282. ASSERT(length <= m_data.length());
  283. string = m_data.substring_view(0, length);
  284. m_data = m_data.substring_view_starting_after_substring(string);
  285. return *this;
  286. }
  287. StringView Plan9FS::Message::Decoder::read_data()
  288. {
  289. u32 length;
  290. *this >> length;
  291. ASSERT(length <= m_data.length());
  292. auto data = m_data.substring_view(0, length);
  293. m_data = m_data.substring_view_starting_after_substring(data);
  294. return data;
  295. }
  296. Plan9FS::Message::Message(Plan9FS& fs, Type type)
  297. : m_builder()
  298. , m_tag(fs.allocate_tag())
  299. , m_type(type)
  300. , m_have_been_built(false)
  301. {
  302. u32 size_placeholder = 0;
  303. *this << size_placeholder << (u8)type << m_tag;
  304. }
  305. Plan9FS::Message::Message(KBuffer&& buffer)
  306. : m_built { buffer, Decoder({ buffer.data(), buffer.size() }) }
  307. , m_have_been_built(true)
  308. {
  309. u32 size;
  310. u8 raw_type;
  311. *this >> size >> raw_type >> m_tag;
  312. m_type = (Type)raw_type;
  313. }
  314. Plan9FS::Message::~Message()
  315. {
  316. if (m_have_been_built) {
  317. m_built.buffer.~KBuffer();
  318. m_built.decoder.~Decoder();
  319. } else {
  320. m_builder.~KBufferBuilder();
  321. }
  322. }
  323. Plan9FS::Message& Plan9FS::Message::operator=(Message&& message)
  324. {
  325. m_tag = message.m_tag;
  326. m_type = message.m_type;
  327. if (m_have_been_built) {
  328. m_built.buffer.~KBuffer();
  329. m_built.decoder.~Decoder();
  330. } else {
  331. m_builder.~KBufferBuilder();
  332. }
  333. m_have_been_built = message.m_have_been_built;
  334. if (m_have_been_built) {
  335. new (&m_built.buffer) KBuffer(move(message.m_built.buffer));
  336. new (&m_built.decoder) Decoder(move(message.m_built.decoder));
  337. } else {
  338. new (&m_builder) KBufferBuilder(move(message.m_builder));
  339. }
  340. return *this;
  341. }
  342. const KBuffer& Plan9FS::Message::build()
  343. {
  344. ASSERT(!m_have_been_built);
  345. auto tmp_buffer = m_builder.build();
  346. m_have_been_built = true;
  347. m_builder.~KBufferBuilder();
  348. new (&m_built.buffer) KBuffer(move(tmp_buffer));
  349. new (&m_built.decoder) Decoder({ m_built.buffer.data(), m_built.buffer.size() });
  350. u32* size = reinterpret_cast<u32*>(m_built.buffer.data());
  351. *size = m_built.buffer.size();
  352. return m_built.buffer;
  353. }
  354. KResult Plan9FS::post_message(Message& message)
  355. {
  356. auto& buffer = message.build();
  357. const u8* data = buffer.data();
  358. size_t size = buffer.size();
  359. auto& description = file_description();
  360. LOCKER(m_send_lock);
  361. while (size > 0) {
  362. if (!description.can_write()) {
  363. if (Thread::current()->block<Thread::WriteBlocker>(nullptr, description).was_interrupted())
  364. return KResult(-EINTR);
  365. }
  366. auto nwritten_or_error = description.write(data, size);
  367. if (nwritten_or_error.is_error())
  368. return nwritten_or_error.error();
  369. auto nwritten = nwritten_or_error.value();
  370. data += nwritten;
  371. size -= nwritten;
  372. }
  373. return KSuccess;
  374. }
  375. KResult Plan9FS::do_read(u8* data, size_t size)
  376. {
  377. auto& description = file_description();
  378. while (size > 0) {
  379. if (!description.can_read()) {
  380. if (Thread::current()->block<Thread::ReadBlocker>(nullptr, description).was_interrupted())
  381. return KResult(-EINTR);
  382. }
  383. auto nread_or_error = description.read(data, size);
  384. if (nread_or_error.is_error())
  385. return nread_or_error.error();
  386. auto nread = nread_or_error.value();
  387. if (nread == 0)
  388. return KResult(-EIO);
  389. data += nread;
  390. size -= nread;
  391. }
  392. return KSuccess;
  393. }
  394. KResult Plan9FS::read_and_dispatch_one_message()
  395. {
  396. ASSERT(m_someone_is_reading);
  397. // That someone is us.
  398. struct [[gnu::packed]] Header
  399. {
  400. u32 size;
  401. u8 type;
  402. u16 tag;
  403. };
  404. Header header;
  405. KResult result = do_read(reinterpret_cast<u8*>(&header), sizeof(header));
  406. if (result.is_error())
  407. return result;
  408. auto buffer = KBuffer::create_with_size(header.size, Region::Access::Read | Region::Access::Write);
  409. // Copy the already read header into the buffer.
  410. memcpy(buffer.data(), &header, sizeof(header));
  411. result = do_read(buffer.data() + sizeof(header), header.size - sizeof(header));
  412. if (result.is_error())
  413. return result;
  414. LOCKER(m_lock);
  415. auto optional_completion = m_completions.get(header.tag);
  416. if (!optional_completion.has_value()) {
  417. if (m_tags_to_ignore.contains(header.tag)) {
  418. m_tags_to_ignore.remove(header.tag);
  419. } else {
  420. dbg() << "Received a 9p message of type " << header.type << " with an unexpected tag " << header.tag << ", dropping";
  421. }
  422. return KSuccess;
  423. }
  424. ReceiveCompletion& completion = *optional_completion.value();
  425. completion.result = KSuccess;
  426. completion.message = Message { move(buffer) };
  427. completion.completed = true;
  428. m_completions.remove(header.tag);
  429. return KSuccess;
  430. }
  431. bool Plan9FS::Blocker::should_unblock(Thread&)
  432. {
  433. if (m_completion.completed)
  434. return true;
  435. bool someone_else_is_reading = m_completion.fs.m_someone_is_reading.exchange(true);
  436. if (!someone_else_is_reading) {
  437. // We're gonna start reading ourselves; unblock.
  438. return true;
  439. }
  440. return false;
  441. }
  442. KResult Plan9FS::wait_for_specific_message(u16 tag, Message& out_message)
  443. {
  444. KResult result = KSuccess;
  445. ReceiveCompletion completion { *this, out_message, result, false };
  446. {
  447. LOCKER(m_lock);
  448. m_completions.set(tag, &completion);
  449. }
  450. // Block until either:
  451. // * Someone else reads the message we're waiting for, and hands it to us;
  452. // * Or we become the one to read and dispatch messages.
  453. if (Thread::current()->block<Plan9FS::Blocker>(nullptr, completion).was_interrupted()) {
  454. LOCKER(m_lock);
  455. m_completions.remove(tag);
  456. return KResult(-EINTR);
  457. }
  458. // See for which reason we woke up.
  459. if (completion.completed) {
  460. // Somebody else completed it for us; nothing further to do.
  461. return result;
  462. }
  463. while (!completion.completed && result.is_success()) {
  464. result = read_and_dispatch_one_message();
  465. }
  466. if (result.is_error()) {
  467. // If we fail to read, wake up everyone with an error.
  468. LOCKER(m_lock);
  469. for (auto& it : m_completions) {
  470. it.value->result = result;
  471. it.value->completed = true;
  472. }
  473. m_completions.clear();
  474. }
  475. // Wake up someone else, if anyone is interested...
  476. m_someone_is_reading = false;
  477. // ...and return.
  478. return result;
  479. }
  480. KResult Plan9FS::post_message_and_explicitly_ignore_reply(Message& message)
  481. {
  482. auto tag = message.tag();
  483. {
  484. LOCKER(m_lock);
  485. m_tags_to_ignore.set(tag);
  486. }
  487. auto result = post_message(message);
  488. if (result.is_error()) {
  489. LOCKER(m_lock);
  490. m_tags_to_ignore.remove(tag);
  491. }
  492. return result;
  493. }
  494. KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message, bool auto_convert_error_reply_to_error)
  495. {
  496. auto request_type = message.type();
  497. auto tag = message.tag();
  498. auto result = post_message(message);
  499. if (result.is_error())
  500. return result;
  501. result = wait_for_specific_message(tag, message);
  502. if (result.is_error())
  503. return result;
  504. if (!auto_convert_error_reply_to_error)
  505. return KSuccess;
  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 KResult(-error_code);
  512. } else if (reply_type == Message::Type::Rerror) {
  513. // Contains an error message. We could attempt to parse it, but for now
  514. // we simply return -EIO instead. In 9P200.u, it can also contain a
  515. // numerical errno in an unspecified encoding; we ignore those too.
  516. StringView error_name;
  517. message >> error_name;
  518. dbg() << "Plan9FS: Received error name " << error_name;
  519. return KResult(-EIO);
  520. } else if ((u8)reply_type != (u8)request_type + 1) {
  521. // Other than those error messages. we only expect the matching reply
  522. // message type.
  523. dbg() << "Plan9FS: Received unexpected message type " << (u8)reply_type
  524. << " in response to " << (u8)request_type;
  525. return KResult(-EIO);
  526. } else {
  527. return KSuccess;
  528. }
  529. }
  530. ssize_t Plan9FS::adjust_buffer_size(ssize_t size) const
  531. {
  532. ssize_t max_size = m_max_message_size - Message::max_header_size;
  533. return min(size, max_size);
  534. }
  535. Plan9FSInode::Plan9FSInode(Plan9FS& fs, u32 fid)
  536. : Inode(fs, fid)
  537. {
  538. }
  539. NonnullRefPtr<Plan9FSInode> Plan9FSInode::create(Plan9FS& fs, u32 fid)
  540. {
  541. return adopt(*new Plan9FSInode(fs, fid));
  542. }
  543. Plan9FSInode::~Plan9FSInode()
  544. {
  545. Plan9FS::Message clunk_request { fs(), Plan9FS::Message::Type::Tclunk };
  546. clunk_request << fid();
  547. // FIXME: Should we observe this error somehow?
  548. (void)fs().post_message_and_explicitly_ignore_reply(clunk_request);
  549. }
  550. KResult Plan9FSInode::ensure_open_for_mode(int mode)
  551. {
  552. bool use_lopen = fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L;
  553. u32 l_mode = 0;
  554. u8 p9_mode = 0;
  555. {
  556. LOCKER(m_lock);
  557. // If it's already open in this mode, we're done.
  558. if ((m_open_mode & mode) == mode)
  559. return KSuccess;
  560. m_open_mode |= mode;
  561. if ((m_open_mode & O_RDWR) == O_RDWR) {
  562. l_mode |= 2;
  563. p9_mode |= 2;
  564. } else if (m_open_mode & O_WRONLY) {
  565. l_mode |= 1;
  566. p9_mode |= 1;
  567. } else if (m_open_mode & O_RDONLY) {
  568. // Leave the values at 0.
  569. }
  570. }
  571. if (use_lopen) {
  572. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tlopen };
  573. message << fid() << l_mode;
  574. return fs().post_message_and_wait_for_a_reply(message);
  575. } else {
  576. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Topen };
  577. message << fid() << p9_mode;
  578. return fs().post_message_and_wait_for_a_reply(message);
  579. }
  580. }
  581. ssize_t Plan9FSInode::read_bytes(off_t offset, ssize_t size, u8* buffer, FileDescription*) const
  582. {
  583. auto result = const_cast<Plan9FSInode&>(*this).ensure_open_for_mode(O_RDONLY);
  584. if (result.is_error())
  585. return result;
  586. size = fs().adjust_buffer_size(size);
  587. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treadlink };
  588. StringView data;
  589. // Try readlink first.
  590. bool readlink_succeded = false;
  591. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L && offset == 0) {
  592. message << fid();
  593. result = fs().post_message_and_wait_for_a_reply(message);
  594. if (result.is_success()) {
  595. readlink_succeded = true;
  596. message >> data;
  597. }
  598. }
  599. if (!readlink_succeded) {
  600. message = Plan9FS::Message { fs(), Plan9FS::Message::Type::Tread };
  601. message << fid() << (u64)offset << (u32)size;
  602. result = fs().post_message_and_wait_for_a_reply(message);
  603. if (result.is_error())
  604. return result.error();
  605. data = message.read_data();
  606. }
  607. // Guard against the server returning more data than requested.
  608. size_t nread = min(data.length(), (size_t)size);
  609. memcpy(buffer, data.characters_without_null_termination(), nread);
  610. return nread;
  611. }
  612. ssize_t Plan9FSInode::write_bytes(off_t offset, ssize_t size, const u8* data, FileDescription*)
  613. {
  614. auto result = ensure_open_for_mode(O_WRONLY);
  615. if (result.is_error())
  616. return result;
  617. size = fs().adjust_buffer_size(size);
  618. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twrite };
  619. message << fid() << (u64)offset;
  620. message.append_data({ data, (size_t)size });
  621. result = fs().post_message_and_wait_for_a_reply(message);
  622. if (result.is_error())
  623. return result.error();
  624. u32 nwritten;
  625. message >> nwritten;
  626. return nwritten;
  627. }
  628. InodeMetadata Plan9FSInode::metadata() const
  629. {
  630. InodeMetadata metadata;
  631. metadata.inode = identifier();
  632. // 9P2000.L; TODO: 9P2000 & 9P2000.u
  633. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tgetattr };
  634. message << fid() << (u64)GetAttrMask::Basic;
  635. auto result = fs().post_message_and_wait_for_a_reply(message);
  636. if (result.is_error()) {
  637. // Just return blank metadata; hopefully that's enough to result in an
  638. // error at some upper layer. Ideally, there would be a way for
  639. // Inode::metadata() to return failure.
  640. return metadata;
  641. }
  642. u64 valid;
  643. Plan9FS::qid qid;
  644. u32 mode;
  645. u32 uid;
  646. u32 gid;
  647. u64 nlink;
  648. u64 rdev;
  649. u64 size;
  650. u64 blksize;
  651. u64 blocks;
  652. message >> valid >> qid >> mode >> uid >> gid >> nlink >> rdev >> size >> blksize >> blocks;
  653. // TODO: times...
  654. if (valid & (u64)GetAttrMask::Mode)
  655. metadata.mode = mode;
  656. if (valid & (u64)GetAttrMask::NLink)
  657. metadata.link_count = nlink;
  658. #if 0
  659. // FIXME: Map UID/GID somehow? Or what do we do?
  660. if (valid & (u64)GetAttrMask::UID)
  661. metadata.uid = uid;
  662. if (valid & (u64)GetAttrMask::GID)
  663. metadata.uid = gid;
  664. // FIXME: What about device nodes?
  665. if (valid & (u64)GetAttrMask::RDev)
  666. metadata.encoded_device = 0; // TODO
  667. #endif
  668. if (valid & (u64)GetAttrMask::Size)
  669. metadata.size = size;
  670. if (valid & (u64)GetAttrMask::Blocks) {
  671. metadata.block_size = blksize;
  672. metadata.block_count = blocks;
  673. }
  674. return metadata;
  675. }
  676. void Plan9FSInode::flush_metadata()
  677. {
  678. // Do nothing.
  679. }
  680. KResultOr<size_t> Plan9FSInode::directory_entry_count() const
  681. {
  682. size_t count = 0;
  683. KResult result = traverse_as_directory([&count](const FS::DirectoryEntry&) {
  684. count++;
  685. return true;
  686. });
  687. if (result.is_error())
  688. return result;
  689. return count;
  690. }
  691. KResult Plan9FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  692. {
  693. KResult result = KSuccess;
  694. // TODO: Should we synthesize "." and ".." here?
  695. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  696. // Start by cloning the fid and opening it.
  697. auto clone_fid = fs().allocate_fid();
  698. {
  699. Plan9FS::Message clone_message { fs(), Plan9FS::Message::Type::Twalk };
  700. clone_message << fid() << clone_fid << (u16)0;
  701. result = fs().post_message_and_wait_for_a_reply(clone_message);
  702. if (result.is_error())
  703. return result;
  704. Plan9FS::Message open_message { fs(), Plan9FS::Message::Type::Tlopen };
  705. open_message << clone_fid << (u32)0;
  706. result = fs().post_message_and_wait_for_a_reply(open_message);
  707. if (result.is_error()) {
  708. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  709. close_message << clone_fid;
  710. // FIXME: Should we observe this error?
  711. (void)fs().post_message_and_explicitly_ignore_reply(close_message);
  712. return result;
  713. }
  714. }
  715. u64 offset = 0;
  716. u32 count = fs().adjust_buffer_size(8 * MB);
  717. while (true) {
  718. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treaddir };
  719. message << clone_fid << offset << count;
  720. result = fs().post_message_and_wait_for_a_reply(message);
  721. if (result.is_error())
  722. break;
  723. StringView data = message.read_data();
  724. if (data.is_empty()) {
  725. // We've reached the end.
  726. break;
  727. }
  728. for (Plan9FS::Message::Decoder decoder { data }; decoder.has_more_data();) {
  729. Plan9FS::qid qid;
  730. u8 type;
  731. StringView name;
  732. decoder >> qid >> offset >> type >> name;
  733. FS::DirectoryEntry entry {
  734. "",
  735. name.length(),
  736. { fsid(), fs().allocate_fid() },
  737. 0
  738. };
  739. size_t size_to_copy = min(sizeof(entry.name) - 1, name.length());
  740. memcpy(entry.name, name.characters_without_null_termination(), size_to_copy);
  741. entry.name[size_to_copy] = 0;
  742. callback(entry);
  743. }
  744. }
  745. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  746. close_message << clone_fid;
  747. // FIXME: Should we observe this error?
  748. (void)fs().post_message_and_explicitly_ignore_reply(close_message);
  749. return result;
  750. } else {
  751. // TODO
  752. return KResult(-ENOTIMPL);
  753. }
  754. }
  755. RefPtr<Inode> Plan9FSInode::lookup(StringView name)
  756. {
  757. u32 newfid = fs().allocate_fid();
  758. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk };
  759. message << fid() << newfid << (u16)1 << name;
  760. auto result = fs().post_message_and_wait_for_a_reply(message);
  761. if (result.is_error())
  762. return nullptr;
  763. return Plan9FSInode::create(fs(), newfid);
  764. }
  765. KResultOr<NonnullRefPtr<Inode>> Plan9FSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  766. {
  767. // TODO
  768. return KResult(-ENOTIMPL);
  769. }
  770. KResult Plan9FSInode::add_child(Inode&, const StringView&, mode_t)
  771. {
  772. // TODO
  773. return KResult(-ENOTIMPL);
  774. }
  775. KResult Plan9FSInode::remove_child(const StringView&)
  776. {
  777. // TODO
  778. return KResult(-ENOTIMPL);
  779. }
  780. KResult Plan9FSInode::chmod(mode_t)
  781. {
  782. // TODO
  783. return KResult(-ENOTIMPL);
  784. }
  785. KResult Plan9FSInode::chown(uid_t, gid_t)
  786. {
  787. // TODO
  788. return KResult(-ENOTIMPL);
  789. }
  790. KResult Plan9FSInode::truncate(u64 new_size)
  791. {
  792. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  793. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tsetattr };
  794. SetAttrMask valid = SetAttrMask::Size;
  795. u32 mode = 0;
  796. u32 uid = 0;
  797. u32 gid = 0;
  798. u64 atime_sec = 0;
  799. u64 atime_nsec = 0;
  800. u64 mtime_sec = 0;
  801. u64 mtime_nsec = 0;
  802. message << fid() << (u64)valid << mode << uid << gid << new_size << atime_sec << atime_nsec << mtime_sec << mtime_nsec;
  803. return fs().post_message_and_wait_for_a_reply(message);
  804. } else {
  805. // TODO: wstat version
  806. return KSuccess;
  807. }
  808. }
  809. }