Plan9FileSystem.cpp 26 KB

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