DevFS.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  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 <AK/Singleton.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/StringView.h>
  29. #include <Kernel/FileSystem/DevFS.h>
  30. #include <Kernel/FileSystem/VirtualFileSystem.h>
  31. namespace Kernel {
  32. NonnullRefPtr<DevFS> DevFS::create()
  33. {
  34. return adopt(*new DevFS);
  35. }
  36. DevFS::DevFS()
  37. : m_root_inode(adopt(*new DevFSRootDirectoryInode(*this)))
  38. {
  39. LOCKER(m_lock);
  40. Device::for_each([&](Device& device) {
  41. // FIXME: Find a better way to not add MasterPTYs or SlavePTYs!
  42. if (device.is_master_pty() || (device.is_character_device() && device.major() == 201))
  43. return;
  44. notify_new_device(device);
  45. });
  46. }
  47. void DevFS::notify_new_device(Device& device)
  48. {
  49. LOCKER(m_lock);
  50. auto new_device_inode = adopt(*new DevFSDeviceInode(*this, device));
  51. m_nodes.append(new_device_inode);
  52. m_root_inode->m_devices.append(new_device_inode);
  53. }
  54. size_t DevFS::get_new_inode_index()
  55. {
  56. LOCKER(m_lock);
  57. return 1 + (++m_inode_index);
  58. }
  59. void DevFS::notify_device_removal(Device&)
  60. {
  61. TODO();
  62. }
  63. DevFS::~DevFS()
  64. {
  65. }
  66. bool DevFS::initialize()
  67. {
  68. return true;
  69. }
  70. NonnullRefPtr<Inode> DevFS::root_inode() const
  71. {
  72. return *m_root_inode;
  73. }
  74. RefPtr<Inode> DevFS::get_inode(InodeIdentifier inode_id) const
  75. {
  76. LOCKER(m_lock);
  77. if (inode_id.index() == 1)
  78. return m_root_inode;
  79. for (auto& node : m_nodes) {
  80. if (inode_id.index() == node.index())
  81. return node;
  82. }
  83. return nullptr;
  84. }
  85. DevFSInode::DevFSInode(DevFS& fs)
  86. : Inode(fs, fs.get_new_inode_index())
  87. {
  88. }
  89. ssize_t DevFSInode::read_bytes(off_t, ssize_t, UserOrKernelBuffer&, FileDescription*) const
  90. {
  91. ASSERT_NOT_REACHED();
  92. }
  93. KResult DevFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const
  94. {
  95. ASSERT_NOT_REACHED();
  96. }
  97. RefPtr<Inode> DevFSInode::lookup(StringView)
  98. {
  99. ASSERT_NOT_REACHED();
  100. }
  101. void DevFSInode::flush_metadata()
  102. {
  103. }
  104. ssize_t DevFSInode::write_bytes(off_t, ssize_t, const UserOrKernelBuffer&, FileDescription*)
  105. {
  106. ASSERT_NOT_REACHED();
  107. }
  108. KResultOr<NonnullRefPtr<Inode>> DevFSInode::create_child(const String&, mode_t, dev_t, uid_t, gid_t)
  109. {
  110. return KResult(-EROFS);
  111. }
  112. KResult DevFSInode::add_child(Inode&, const StringView&, mode_t)
  113. {
  114. return KResult(-EROFS);
  115. }
  116. KResult DevFSInode::remove_child(const StringView&)
  117. {
  118. return KResult(-EROFS);
  119. }
  120. KResultOr<size_t> DevFSInode::directory_entry_count() const
  121. {
  122. ASSERT_NOT_REACHED();
  123. }
  124. KResult DevFSInode::chmod(mode_t)
  125. {
  126. return KResult(-EPERM);
  127. }
  128. KResult DevFSInode::chown(uid_t, gid_t)
  129. {
  130. return KResult(-EPERM);
  131. }
  132. KResult DevFSInode::truncate(u64)
  133. {
  134. return KResult(-EPERM);
  135. }
  136. String DevFSLinkInode::name() const
  137. {
  138. return m_name;
  139. }
  140. DevFSLinkInode::~DevFSLinkInode()
  141. {
  142. }
  143. DevFSLinkInode::DevFSLinkInode(DevFS& fs, String name)
  144. : DevFSInode(fs)
  145. , m_name(name)
  146. {
  147. }
  148. ssize_t DevFSLinkInode::read_bytes(off_t offset, ssize_t, UserOrKernelBuffer& buffer, FileDescription*) const
  149. {
  150. LOCKER(m_lock);
  151. ASSERT(offset == 0);
  152. ASSERT(!m_link.is_null());
  153. if (!buffer.write(((const u8*)m_link.substring_view(0).characters_without_null_termination()) + offset, m_link.length()))
  154. return -EFAULT;
  155. return m_link.length();
  156. }
  157. InodeMetadata DevFSLinkInode::metadata() const
  158. {
  159. LOCKER(m_lock);
  160. InodeMetadata metadata;
  161. metadata.inode = { fsid(), index() };
  162. metadata.mode = S_IFLNK | 0555;
  163. metadata.uid = 0;
  164. metadata.gid = 0;
  165. metadata.size = 0;
  166. metadata.mtime = mepoch;
  167. return metadata;
  168. }
  169. ssize_t DevFSLinkInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& buffer, FileDescription*)
  170. {
  171. LOCKER(m_lock);
  172. ASSERT(offset == 0);
  173. ASSERT(buffer.is_kernel_buffer());
  174. m_link = buffer.copy_into_string(count);
  175. return count;
  176. }
  177. DevFSDirectoryInode::DevFSDirectoryInode(DevFS& fs)
  178. : DevFSInode(fs)
  179. {
  180. }
  181. DevFSDirectoryInode::~DevFSDirectoryInode()
  182. {
  183. }
  184. InodeMetadata DevFSDirectoryInode::metadata() const
  185. {
  186. LOCKER(m_lock);
  187. InodeMetadata metadata;
  188. metadata.inode = { fsid(), 1 };
  189. metadata.mode = 0040555;
  190. metadata.uid = 0;
  191. metadata.gid = 0;
  192. metadata.size = 0;
  193. metadata.mtime = mepoch;
  194. return metadata;
  195. }
  196. KResult DevFSDirectoryInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)>) const
  197. {
  198. LOCKER(m_lock);
  199. return KResult(-EINVAL);
  200. }
  201. RefPtr<Inode> DevFSDirectoryInode::lookup(StringView)
  202. {
  203. LOCKER(m_lock);
  204. return nullptr;
  205. }
  206. KResultOr<size_t> DevFSDirectoryInode::directory_entry_count() const
  207. {
  208. LOCKER(m_lock);
  209. return m_devices.size();
  210. }
  211. DevFSRootDirectoryInode::DevFSRootDirectoryInode(DevFS& fs)
  212. : DevFSDirectoryInode(fs)
  213. , m_parent_fs(fs)
  214. {
  215. }
  216. KResult DevFSRootDirectoryInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  217. {
  218. LOCKER(m_parent_fs.m_lock);
  219. callback({ ".", identifier(), 0 });
  220. callback({ "..", identifier(), 0 });
  221. for (auto& folder : m_subfolders) {
  222. InodeIdentifier identifier = { fsid(), folder.index() };
  223. callback({ folder.name(), identifier, 0 });
  224. }
  225. for (auto& link : m_links) {
  226. InodeIdentifier identifier = { fsid(), link.index() };
  227. callback({ link.name(), identifier, 0 });
  228. }
  229. for (auto& device_node : m_devices) {
  230. InodeIdentifier identifier = { fsid(), device_node.index() };
  231. callback({ device_node.name(), identifier, 0 });
  232. }
  233. return KSuccess;
  234. }
  235. RefPtr<Inode> DevFSRootDirectoryInode::lookup(StringView name)
  236. {
  237. LOCKER(m_parent_fs.m_lock);
  238. for (auto& subfolder : m_subfolders) {
  239. if (subfolder.name() == name)
  240. return subfolder;
  241. }
  242. for (auto& link : m_links) {
  243. if (link.name() == name)
  244. return link;
  245. }
  246. for (auto& device_node : m_devices) {
  247. if (device_node.name() == name) {
  248. return device_node;
  249. }
  250. }
  251. return nullptr;
  252. }
  253. KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(const String& name, mode_t mode, dev_t, uid_t, gid_t)
  254. {
  255. LOCKER(m_parent_fs.m_lock);
  256. InodeMetadata metadata;
  257. metadata.mode = mode;
  258. if (metadata.is_directory()) {
  259. for (auto& folder : m_subfolders) {
  260. if (folder.name() == name)
  261. return KResult(-EEXIST);
  262. }
  263. if (name != "pts")
  264. return KResult(-EROFS);
  265. auto new_directory_inode = adopt(*new DevFSPtsDirectoryInode(m_parent_fs));
  266. m_subfolders.append(new_directory_inode);
  267. m_parent_fs.m_nodes.append(new_directory_inode);
  268. return KResult(KSuccess);
  269. }
  270. if (metadata.is_symlink()) {
  271. for (auto& link : m_links) {
  272. if (link.name() == name)
  273. return KResult(-EEXIST);
  274. }
  275. dbgln("DevFS: Success on create new symlink");
  276. auto new_link_inode = adopt(*new DevFSLinkInode(m_parent_fs, name));
  277. m_links.append(new_link_inode);
  278. m_parent_fs.m_nodes.append(new_link_inode);
  279. return new_link_inode;
  280. }
  281. return KResult(-EROFS);
  282. }
  283. DevFSRootDirectoryInode::~DevFSRootDirectoryInode()
  284. {
  285. }
  286. InodeMetadata DevFSRootDirectoryInode::metadata() const
  287. {
  288. LOCKER(m_parent_fs.m_lock);
  289. InodeMetadata metadata;
  290. metadata.inode = { fsid(), 1 };
  291. metadata.mode = 0040555;
  292. metadata.uid = 0;
  293. metadata.gid = 0;
  294. metadata.size = 0;
  295. metadata.mtime = mepoch;
  296. return metadata;
  297. }
  298. KResultOr<size_t> DevFSRootDirectoryInode::directory_entry_count() const
  299. {
  300. LOCKER(m_parent_fs.m_lock);
  301. return m_devices.size() + DevFSDirectoryInode::directory_entry_count().value();
  302. }
  303. DevFSDeviceInode::DevFSDeviceInode(DevFS& fs, const Device& device)
  304. : DevFSInode(fs)
  305. , m_attached_device(device)
  306. {
  307. }
  308. DevFSDeviceInode::~DevFSDeviceInode()
  309. {
  310. }
  311. KResult DevFSDeviceInode::chown(uid_t uid, gid_t gid)
  312. {
  313. LOCKER(m_lock);
  314. m_uid = uid;
  315. m_gid = gid;
  316. return KSuccess;
  317. }
  318. String DevFSDeviceInode::name() const
  319. {
  320. if (m_cached_name.is_null() || m_cached_name.is_empty())
  321. const_cast<DevFSDeviceInode&>(*this).m_cached_name = determine_name();
  322. return m_cached_name;
  323. }
  324. String DevFSDeviceInode::determine_name() const
  325. {
  326. LOCKER(m_lock);
  327. if (m_attached_device->is_character_device()) {
  328. switch (m_attached_device->major()) {
  329. case 85:
  330. if (m_attached_device->minor() == 1)
  331. return "keyboard";
  332. ASSERT_NOT_REACHED();
  333. case 10:
  334. if (m_attached_device->minor() == 1)
  335. return "mouse";
  336. ASSERT_NOT_REACHED();
  337. case 42:
  338. if (m_attached_device->minor() == 42)
  339. return "audio";
  340. ASSERT_NOT_REACHED();
  341. case 1:
  342. switch (m_attached_device->minor()) {
  343. case 8:
  344. return "random";
  345. case 3:
  346. return "null";
  347. case 5:
  348. return "zero";
  349. case 7:
  350. return "full";
  351. default:
  352. ASSERT_NOT_REACHED();
  353. }
  354. case 5:
  355. if (m_attached_device->minor() == 1)
  356. return "console";
  357. if (m_attached_device->minor() == 2)
  358. return "ptmx";
  359. ASSERT_NOT_REACHED();
  360. case 4:
  361. if (m_attached_device->minor() >= 64)
  362. return String::formatted("ttyS{}", m_attached_device->minor() - 64);
  363. return String::formatted("tty{}", m_attached_device->minor());
  364. default:
  365. ASSERT_NOT_REACHED();
  366. }
  367. } else {
  368. switch (m_attached_device->major()) {
  369. case 29:
  370. return String::formatted("fb{}", m_attached_device->minor());
  371. case 3: {
  372. size_t drive_index = (u8)'a' + m_attached_device->minor();
  373. char drive_letter = (u8)drive_index;
  374. return String::format("hd%c", drive_letter);
  375. }
  376. case 100:
  377. // FIXME: Try to not hardcode a maximum of 16 partitions per drive!
  378. size_t drive_index = (u8)'a' + (m_attached_device->minor() / 16);
  379. char drive_letter = (u8)drive_index;
  380. return String::formatted("hd{:c}{}", drive_letter, m_attached_device->minor() + 1);
  381. }
  382. }
  383. ASSERT_NOT_REACHED();
  384. }
  385. ssize_t DevFSDeviceInode::read_bytes(off_t offset, ssize_t count, UserOrKernelBuffer& buffer, FileDescription* description) const
  386. {
  387. LOCKER(m_lock);
  388. ASSERT(!!description);
  389. if (!m_attached_device->can_read(*description, offset))
  390. return -EIO;
  391. auto nread = const_cast<Device&>(*m_attached_device).read(*description, offset, buffer, count);
  392. if (nread.is_error())
  393. return -EIO;
  394. return nread.value();
  395. }
  396. InodeMetadata DevFSDeviceInode::metadata() const
  397. {
  398. LOCKER(m_lock);
  399. InodeMetadata metadata;
  400. metadata.inode = { fsid(), index() };
  401. metadata.mode = (m_attached_device->is_block_device() ? S_IFBLK : S_IFCHR) | m_attached_device->required_mode();
  402. metadata.uid = m_uid;
  403. metadata.gid = m_gid;
  404. metadata.size = 0;
  405. metadata.mtime = mepoch;
  406. metadata.major_device = m_attached_device->major();
  407. metadata.minor_device = m_attached_device->minor();
  408. return metadata;
  409. }
  410. ssize_t DevFSDeviceInode::write_bytes(off_t offset, ssize_t count, const UserOrKernelBuffer& buffer, FileDescription* description)
  411. {
  412. LOCKER(m_lock);
  413. ASSERT(!!description);
  414. if (!m_attached_device->can_read(*description, offset))
  415. return -EIO;
  416. auto nread = const_cast<Device&>(*m_attached_device).write(*description, offset, buffer, count);
  417. if (nread.is_error())
  418. return -EIO;
  419. return nread.value();
  420. }
  421. DevFSPtsDirectoryInode::DevFSPtsDirectoryInode(DevFS& fs)
  422. : DevFSDirectoryInode(fs)
  423. {
  424. }
  425. KResult DevFSPtsDirectoryInode::traverse_as_directory(Function<bool(const FS::DirectoryEntryView&)> callback) const
  426. {
  427. LOCKER(m_lock);
  428. callback({ ".", identifier(), 0 });
  429. callback({ "..", identifier(), 0 });
  430. return KSuccess;
  431. }
  432. RefPtr<Inode> DevFSPtsDirectoryInode::lookup(StringView)
  433. {
  434. return nullptr;
  435. }
  436. DevFSPtsDirectoryInode::~DevFSPtsDirectoryInode()
  437. {
  438. }
  439. InodeMetadata DevFSPtsDirectoryInode::metadata() const
  440. {
  441. LOCKER(m_lock);
  442. InodeMetadata metadata;
  443. metadata.inode = { fsid(), index() };
  444. metadata.mode = 0040555;
  445. metadata.uid = 0;
  446. metadata.gid = 0;
  447. metadata.size = 0;
  448. metadata.mtime = mepoch;
  449. return metadata;
  450. }
  451. KResultOr<size_t> DevFSPtsDirectoryInode::directory_entry_count() const
  452. {
  453. return 0;
  454. }
  455. }