Plan9FileSystem.cpp 26 KB

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