Plan9FileSystem.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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>(description) != Thread::BlockResult::WokeNormally)
  364. return KResult(-EINTR);
  365. }
  366. ssize_t nwritten = description.write(data, size);
  367. if (nwritten < 0)
  368. return KResult(nwritten);
  369. data += nwritten;
  370. size -= nwritten;
  371. }
  372. return KSuccess;
  373. }
  374. KResult Plan9FS::do_read(u8* data, size_t size)
  375. {
  376. auto& description = file_description();
  377. while (size > 0) {
  378. if (!description.can_read()) {
  379. if (Thread::current()->block<Thread::ReadBlocker>(description) != Thread::BlockResult::WokeNormally)
  380. return KResult(-EINTR);
  381. }
  382. ssize_t nread = description.read(data, size);
  383. if (nread < 0)
  384. return KResult(nread);
  385. if (nread == 0)
  386. return KResult(-EIO);
  387. data += nread;
  388. size -= nread;
  389. }
  390. return KSuccess;
  391. }
  392. KResult Plan9FS::read_and_dispatch_one_message()
  393. {
  394. ASSERT(m_someone_is_reading);
  395. // That someone is us.
  396. struct [[gnu::packed]] Header
  397. {
  398. u32 size;
  399. u8 type;
  400. u16 tag;
  401. };
  402. Header header;
  403. KResult result = do_read(reinterpret_cast<u8*>(&header), sizeof(header));
  404. if (result.is_error())
  405. return result;
  406. auto buffer = KBuffer::create_with_size(header.size, Region::Access::Read | Region::Access::Write);
  407. // Copy the already read header into the buffer.
  408. memcpy(buffer.data(), &header, sizeof(header));
  409. result = do_read(buffer.data() + sizeof(header), header.size - sizeof(header));
  410. if (result.is_error())
  411. return result;
  412. LOCKER(m_lock);
  413. auto optional_completion = m_completions.get(header.tag);
  414. if (!optional_completion.has_value()) {
  415. if (m_tags_to_ignore.contains(header.tag)) {
  416. m_tags_to_ignore.remove(header.tag);
  417. } else {
  418. dbg() << "Received a 9p message of type " << header.type << " with an unexpected tag " << header.tag << ", dropping";
  419. }
  420. return KSuccess;
  421. }
  422. ReceiveCompletion& completion = *optional_completion.value();
  423. completion.result = KSuccess;
  424. completion.message = Message { move(buffer) };
  425. completion.completed = true;
  426. m_completions.remove(header.tag);
  427. return KSuccess;
  428. }
  429. bool Plan9FS::Blocker::should_unblock(Thread&, time_t, long)
  430. {
  431. if (m_completion.completed)
  432. return true;
  433. bool someone_else_is_reading = m_completion.fs.m_someone_is_reading.exchange(true);
  434. if (!someone_else_is_reading) {
  435. // We're gonna start reading ourselves; unblock.
  436. return true;
  437. }
  438. return false;
  439. }
  440. KResult Plan9FS::wait_for_specific_message(u16 tag, Message& out_message)
  441. {
  442. KResult result = KSuccess;
  443. ReceiveCompletion completion { *this, out_message, result, false };
  444. {
  445. LOCKER(m_lock);
  446. m_completions.set(tag, &completion);
  447. }
  448. // Block until either:
  449. // * Someone else reads the message we're waiting for, and hands it to us;
  450. // * Or we become the one to read and dispatch messages.
  451. auto block_result = Thread::current()->block<Plan9FS::Blocker>(completion);
  452. if (block_result != Thread::BlockResult::WokeNormally) {
  453. LOCKER(m_lock);
  454. m_completions.remove(tag);
  455. return KResult(-EINTR);
  456. }
  457. // See for which reason we woke up.
  458. if (completion.completed) {
  459. // Somebody else completed it for us; nothing further to do.
  460. return result;
  461. }
  462. while (!completion.completed && result.is_success()) {
  463. result = read_and_dispatch_one_message();
  464. }
  465. if (result.is_error()) {
  466. // If we fail to read, wake up everyone with an error.
  467. LOCKER(m_lock);
  468. for (auto& it : m_completions) {
  469. it.value->result = result;
  470. it.value->completed = true;
  471. }
  472. m_completions.clear();
  473. }
  474. // Wake up someone else, if anyone is interested...
  475. m_someone_is_reading = false;
  476. // ...and return.
  477. return result;
  478. }
  479. KResult Plan9FS::post_message_and_explicitly_ignore_reply(Message& message)
  480. {
  481. auto tag = message.tag();
  482. {
  483. LOCKER(m_lock);
  484. m_tags_to_ignore.set(tag);
  485. }
  486. auto result = post_message(message);
  487. if (result.is_error()) {
  488. LOCKER(m_lock);
  489. m_tags_to_ignore.remove(tag);
  490. }
  491. return result;
  492. }
  493. KResult Plan9FS::post_message_and_wait_for_a_reply(Message& message, bool auto_convert_error_reply_to_error)
  494. {
  495. auto request_type = message.type();
  496. auto tag = message.tag();
  497. auto result = post_message(message);
  498. if (result.is_error())
  499. return result;
  500. result = wait_for_specific_message(tag, message);
  501. if (result.is_error())
  502. return result;
  503. if (!auto_convert_error_reply_to_error)
  504. return KSuccess;
  505. auto reply_type = message.type();
  506. if (reply_type == Message::Type::Rlerror) {
  507. // Contains a numerical Linux errno; hopefully our errno numbers match.
  508. u32 error_code;
  509. message >> error_code;
  510. return KResult(-error_code);
  511. } else if (reply_type == Message::Type::Rerror) {
  512. // Contains an error message. We could attempt to parse it, but for now
  513. // we simply return -EIO instead. In 9P200.u, it can also contain a
  514. // numerical errno in an unspecified encoding; we ignore those too.
  515. StringView error_name;
  516. message >> error_name;
  517. dbg() << "Plan9FS: Received error name " << error_name;
  518. return KResult(-EIO);
  519. } else if ((u8)reply_type != (u8)request_type + 1) {
  520. // Other than those error messages. we only expect the matching reply
  521. // message type.
  522. dbg() << "Plan9FS: Received unexpected message type " << (u8)reply_type
  523. << " in response to " << (u8)request_type;
  524. return KResult(-EIO);
  525. } else {
  526. return KSuccess;
  527. }
  528. }
  529. ssize_t Plan9FS::adjust_buffer_size(ssize_t size) const
  530. {
  531. ssize_t max_size = m_max_message_size - Message::max_header_size;
  532. return min(size, max_size);
  533. }
  534. Plan9FSInode::Plan9FSInode(Plan9FS& fs, u32 fid)
  535. : Inode(fs, fid)
  536. {
  537. }
  538. NonnullRefPtr<Plan9FSInode> Plan9FSInode::create(Plan9FS& fs, u32 fid)
  539. {
  540. return adopt(*new Plan9FSInode(fs, fid));
  541. }
  542. Plan9FSInode::~Plan9FSInode()
  543. {
  544. Plan9FS::Message clunk_request { fs(), Plan9FS::Message::Type::Tclunk };
  545. clunk_request << fid();
  546. fs().post_message_and_explicitly_ignore_reply(clunk_request);
  547. }
  548. KResult Plan9FSInode::ensure_open_for_mode(int mode)
  549. {
  550. bool use_lopen = fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L;
  551. u32 l_mode = 0;
  552. u8 p9_mode = 0;
  553. {
  554. LOCKER(m_lock);
  555. // If it's already open in this mode, we're done.
  556. if ((m_open_mode & mode) == mode)
  557. return KSuccess;
  558. m_open_mode |= mode;
  559. if ((m_open_mode & O_RDWR) == O_RDWR) {
  560. l_mode |= 2;
  561. p9_mode |= 2;
  562. } else if (m_open_mode & O_WRONLY) {
  563. l_mode |= 1;
  564. p9_mode |= 1;
  565. } else if (m_open_mode & O_RDONLY) {
  566. // Leave the values at 0.
  567. }
  568. }
  569. if (use_lopen) {
  570. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tlopen };
  571. message << fid() << l_mode;
  572. return fs().post_message_and_wait_for_a_reply(message);
  573. } else {
  574. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Topen };
  575. message << fid() << p9_mode;
  576. return fs().post_message_and_wait_for_a_reply(message);
  577. }
  578. }
  579. ssize_t Plan9FSInode::read_bytes(off_t offset, ssize_t size, u8* buffer, FileDescription*) const
  580. {
  581. auto result = const_cast<Plan9FSInode&>(*this).ensure_open_for_mode(O_RDONLY);
  582. if (result.is_error())
  583. return result;
  584. size = fs().adjust_buffer_size(size);
  585. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treadlink };
  586. StringView data;
  587. // Try readlink first.
  588. bool readlink_succeded = false;
  589. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L && offset == 0) {
  590. message << fid();
  591. result = fs().post_message_and_wait_for_a_reply(message);
  592. if (result.is_success()) {
  593. readlink_succeded = true;
  594. message >> data;
  595. }
  596. }
  597. if (!readlink_succeded) {
  598. message = Plan9FS::Message { fs(), Plan9FS::Message::Type::Tread };
  599. message << fid() << (u64)offset << (u32)size;
  600. result = fs().post_message_and_wait_for_a_reply(message);
  601. if (result.is_error())
  602. return result.error();
  603. data = message.read_data();
  604. }
  605. // Guard against the server returning more data than requested.
  606. size_t nread = min(data.length(), (size_t)size);
  607. memcpy(buffer, data.characters_without_null_termination(), nread);
  608. return nread;
  609. }
  610. ssize_t Plan9FSInode::write_bytes(off_t offset, ssize_t size, const u8* data, FileDescription*)
  611. {
  612. auto result = ensure_open_for_mode(O_WRONLY);
  613. if (result.is_error())
  614. return result;
  615. size = fs().adjust_buffer_size(size);
  616. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twrite };
  617. message << fid() << (u64)offset;
  618. message.append_data({ data, (size_t)size });
  619. result = fs().post_message_and_wait_for_a_reply(message);
  620. if (result.is_error())
  621. return result.error();
  622. u32 nwritten;
  623. message >> nwritten;
  624. return nwritten;
  625. }
  626. InodeMetadata Plan9FSInode::metadata() const
  627. {
  628. InodeMetadata metadata;
  629. metadata.inode = identifier();
  630. // 9P2000.L; TODO: 9P2000 & 9P2000.u
  631. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tgetattr };
  632. message << fid() << (u64)GetAttrMask::Basic;
  633. auto result = fs().post_message_and_wait_for_a_reply(message);
  634. if (result.is_error()) {
  635. // Just return blank metadata; hopefully that's enough to result in an
  636. // error at some upper layer. Ideally, there would be a way for
  637. // Inode::metadata() to return failure.
  638. return metadata;
  639. }
  640. u64 valid;
  641. Plan9FS::qid qid;
  642. u32 mode;
  643. u32 uid;
  644. u32 gid;
  645. u64 nlink;
  646. u64 rdev;
  647. u64 size;
  648. u64 blksize;
  649. u64 blocks;
  650. message >> valid >> qid >> mode >> uid >> gid >> nlink >> rdev >> size >> blksize >> blocks;
  651. // TODO: times...
  652. if (valid & (u64)GetAttrMask::Mode)
  653. metadata.mode = mode;
  654. if (valid & (u64)GetAttrMask::NLink)
  655. metadata.link_count = nlink;
  656. #if 0
  657. // FIXME: Map UID/GID somehow? Or what do we do?
  658. if (valid & (u64)GetAttrMask::UID)
  659. metadata.uid = uid;
  660. if (valid & (u64)GetAttrMask::GID)
  661. metadata.uid = gid;
  662. // FIXME: What about device nodes?
  663. if (valid & (u64)GetAttrMask::RDev)
  664. metadata.encoded_device = 0; // TODO
  665. #endif
  666. if (valid & (u64)GetAttrMask::Size)
  667. metadata.size = size;
  668. if (valid & (u64)GetAttrMask::Blocks) {
  669. metadata.block_size = blksize;
  670. metadata.block_count = blocks;
  671. }
  672. return metadata;
  673. }
  674. void Plan9FSInode::flush_metadata()
  675. {
  676. // Do nothing.
  677. }
  678. size_t Plan9FSInode::directory_entry_count() const
  679. {
  680. size_t count = 0;
  681. traverse_as_directory([&count](const FS::DirectoryEntry&) {
  682. count++;
  683. return true;
  684. });
  685. return count;
  686. }
  687. KResult Plan9FSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)> callback) const
  688. {
  689. KResult result = KSuccess;
  690. // TODO: Should we synthesize "." and ".." here?
  691. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  692. // Start by cloning the fid and opening it.
  693. auto clone_fid = fs().allocate_fid();
  694. {
  695. Plan9FS::Message clone_message { fs(), Plan9FS::Message::Type::Twalk };
  696. clone_message << fid() << clone_fid << (u16)0;
  697. result = fs().post_message_and_wait_for_a_reply(clone_message);
  698. if (result.is_error())
  699. return result;
  700. Plan9FS::Message open_message { fs(), Plan9FS::Message::Type::Tlopen };
  701. open_message << clone_fid << (u32)0;
  702. result = fs().post_message_and_wait_for_a_reply(open_message);
  703. if (result.is_error()) {
  704. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  705. close_message << clone_fid;
  706. fs().post_message_and_explicitly_ignore_reply(close_message);
  707. return result;
  708. }
  709. }
  710. u64 offset = 0;
  711. u32 count = fs().adjust_buffer_size(8 * MB);
  712. while (true) {
  713. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Treaddir };
  714. message << clone_fid << offset << count;
  715. result = fs().post_message_and_wait_for_a_reply(message);
  716. if (result.is_error())
  717. break;
  718. StringView data = message.read_data();
  719. if (data.is_empty()) {
  720. // We've reached the end.
  721. break;
  722. }
  723. for (Plan9FS::Message::Decoder decoder { data }; decoder.has_more_data();) {
  724. Plan9FS::qid qid;
  725. u8 type;
  726. StringView name;
  727. decoder >> qid >> offset >> type >> name;
  728. FS::DirectoryEntry entry {
  729. "",
  730. name.length(),
  731. { fsid(), fs().allocate_fid() },
  732. 0
  733. };
  734. size_t size_to_copy = min(sizeof(entry.name) - 1, name.length());
  735. memcpy(entry.name, name.characters_without_null_termination(), size_to_copy);
  736. entry.name[size_to_copy] = 0;
  737. callback(entry);
  738. }
  739. }
  740. Plan9FS::Message close_message { fs(), Plan9FS::Message::Type::Tclunk };
  741. close_message << clone_fid;
  742. fs().post_message_and_explicitly_ignore_reply(close_message);
  743. return result;
  744. } else {
  745. // TODO
  746. return KResult(-ENOTIMPL);
  747. }
  748. }
  749. RefPtr<Inode> Plan9FSInode::lookup(StringView name)
  750. {
  751. u32 newfid = fs().allocate_fid();
  752. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Twalk };
  753. message << fid() << newfid << (u16)1 << name;
  754. auto result = fs().post_message_and_wait_for_a_reply(message);
  755. if (result.is_error())
  756. return nullptr;
  757. return Plan9FSInode::create(fs(), newfid);
  758. }
  759. KResultOr<NonnullRefPtr<Inode>> Plan9FSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  760. {
  761. // TODO
  762. return KResult(-ENOTIMPL);
  763. }
  764. KResult Plan9FSInode::add_child(Inode&, const StringView&, mode_t)
  765. {
  766. // TODO
  767. return KResult(-ENOTIMPL);
  768. }
  769. KResult Plan9FSInode::remove_child(const StringView&)
  770. {
  771. // TODO
  772. return KResult(-ENOTIMPL);
  773. }
  774. KResult Plan9FSInode::chmod(mode_t)
  775. {
  776. // TODO
  777. return KResult(-ENOTIMPL);
  778. }
  779. KResult Plan9FSInode::chown(uid_t, gid_t)
  780. {
  781. // TODO
  782. return KResult(-ENOTIMPL);
  783. }
  784. KResult Plan9FSInode::truncate(u64 new_size)
  785. {
  786. if (fs().m_remote_protocol_version >= Plan9FS::ProtocolVersion::v9P2000L) {
  787. Plan9FS::Message message { fs(), Plan9FS::Message::Type::Tsetattr };
  788. SetAttrMask valid = SetAttrMask::Size;
  789. u32 mode = 0;
  790. u32 uid = 0;
  791. u32 gid = 0;
  792. u64 atime_sec = 0;
  793. u64 atime_nsec = 0;
  794. u64 mtime_sec = 0;
  795. u64 mtime_nsec = 0;
  796. message << fid() << (u64)valid << mode << uid << gid << new_size << atime_sec << atime_nsec << mtime_sec << mtime_nsec;
  797. return fs().post_message_and_wait_for_a_reply(message);
  798. } else {
  799. // TODO: wstat version
  800. return KSuccess;
  801. }
  802. }
  803. }