Plan9FileSystem.cpp 29 KB

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