SysFS.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Singleton.h>
  7. #include <AK/StringView.h>
  8. #include <Kernel/FileSystem/SysFS.h>
  9. #include <Kernel/FileSystem/VirtualFileSystem.h>
  10. #include <Kernel/Sections.h>
  11. namespace Kernel {
  12. static AK::Singleton<SystemRegistrar> s_the;
  13. SystemRegistrar& SystemRegistrar::the()
  14. {
  15. return *s_the;
  16. }
  17. UNMAP_AFTER_INIT void SystemRegistrar::initialize()
  18. {
  19. VERIFY(!s_the.is_initialized());
  20. s_the.ensure_instance();
  21. }
  22. UNMAP_AFTER_INIT SystemRegistrar::SystemRegistrar()
  23. : m_root_folder(SysFSRootFolder::create())
  24. {
  25. }
  26. UNMAP_AFTER_INIT void SystemRegistrar::register_new_component(SystemExposedComponent& component)
  27. {
  28. Locker locker(m_lock);
  29. m_root_folder->m_components.append(component);
  30. }
  31. NonnullRefPtr<SysFSRootFolder> SysFSRootFolder::create()
  32. {
  33. return adopt_ref(*new (nothrow) SysFSRootFolder);
  34. }
  35. KResult SysFSRootFolder::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  36. {
  37. Locker locker(SystemRegistrar::the().m_lock);
  38. callback({ ".", { fsid, component_index() }, 0 });
  39. callback({ "..", { fsid, 0 }, 0 });
  40. for (auto& component : m_components) {
  41. InodeIdentifier identifier = { fsid, component.component_index() };
  42. callback({ component.name(), identifier, 0 });
  43. }
  44. return KSuccess;
  45. }
  46. SysFSRootFolder::SysFSRootFolder()
  47. : SystemExposedFolder(".")
  48. {
  49. }
  50. NonnullRefPtr<SysFS> SysFS::create()
  51. {
  52. return adopt_ref(*new (nothrow) SysFS);
  53. }
  54. SysFS::SysFS()
  55. : m_root_inode(SystemRegistrar::the().m_root_folder->to_inode(*this))
  56. {
  57. Locker locker(m_lock);
  58. }
  59. SysFS::~SysFS()
  60. {
  61. }
  62. bool SysFS::initialize()
  63. {
  64. return true;
  65. }
  66. NonnullRefPtr<Inode> SysFS::root_inode() const
  67. {
  68. return *m_root_inode;
  69. }
  70. NonnullRefPtr<SysFSInode> SysFSInode::create(SysFS const& fs, SystemExposedComponent const& component)
  71. {
  72. return adopt_ref(*new (nothrow) SysFSInode(fs, component));
  73. }
  74. SysFSInode::SysFSInode(SysFS const& fs, SystemExposedComponent const& component)
  75. : Inode(const_cast<SysFS&>(fs), component.component_index())
  76. , m_associated_component(component)
  77. {
  78. }
  79. KResultOr<size_t> SysFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* fd) const
  80. {
  81. return m_associated_component->read_bytes(offset, count, buffer, fd);
  82. }
  83. KResult SysFSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const
  84. {
  85. VERIFY_NOT_REACHED();
  86. }
  87. RefPtr<Inode> SysFSInode::lookup(StringView)
  88. {
  89. VERIFY_NOT_REACHED();
  90. }
  91. InodeMetadata SysFSInode::metadata() const
  92. {
  93. Locker locker(m_lock);
  94. InodeMetadata metadata;
  95. metadata.inode = { fsid(), m_associated_component->component_index() };
  96. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  97. metadata.uid = 0;
  98. metadata.gid = 0;
  99. metadata.size = m_associated_component->size();
  100. metadata.mtime = mepoch;
  101. return metadata;
  102. }
  103. void SysFSInode::flush_metadata()
  104. {
  105. }
  106. KResultOr<size_t> SysFSInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, FileDescription* fd)
  107. {
  108. return m_associated_component->write_bytes(offset, count, buffer, fd);
  109. }
  110. KResultOr<NonnullRefPtr<Inode>> SysFSInode::create_child(String const&, mode_t, dev_t, uid_t, gid_t)
  111. {
  112. return EROFS;
  113. }
  114. KResult SysFSInode::add_child(Inode&, StringView const&, mode_t)
  115. {
  116. return EROFS;
  117. }
  118. KResult SysFSInode::remove_child(StringView const&)
  119. {
  120. return EROFS;
  121. }
  122. KResultOr<size_t> SysFSInode::directory_entry_count() const
  123. {
  124. VERIFY_NOT_REACHED();
  125. }
  126. KResult SysFSInode::chmod(mode_t)
  127. {
  128. return EPERM;
  129. }
  130. KResult SysFSInode::chown(uid_t, gid_t)
  131. {
  132. return EPERM;
  133. }
  134. KResult SysFSInode::truncate(u64)
  135. {
  136. return EPERM;
  137. }
  138. NonnullRefPtr<SysFSDirectoryInode> SysFSDirectoryInode::create(SysFS const& sysfs, SystemExposedComponent const& component)
  139. {
  140. return adopt_ref(*new (nothrow) SysFSDirectoryInode(sysfs, component));
  141. }
  142. SysFSDirectoryInode::SysFSDirectoryInode(SysFS const& fs, SystemExposedComponent const& component)
  143. : SysFSInode(fs, component)
  144. , m_parent_fs(const_cast<SysFS&>(fs))
  145. {
  146. }
  147. SysFSDirectoryInode::~SysFSDirectoryInode()
  148. {
  149. }
  150. InodeMetadata SysFSDirectoryInode::metadata() const
  151. {
  152. Locker locker(m_lock);
  153. InodeMetadata metadata;
  154. metadata.inode = { fsid(), m_associated_component->component_index() };
  155. metadata.mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXOTH;
  156. metadata.uid = 0;
  157. metadata.gid = 0;
  158. metadata.size = 0;
  159. metadata.mtime = mepoch;
  160. return metadata;
  161. }
  162. KResult SysFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  163. {
  164. Locker locker(m_parent_fs.m_lock);
  165. return m_associated_component->traverse_as_directory(m_parent_fs.fsid(), move(callback));
  166. }
  167. RefPtr<Inode> SysFSDirectoryInode::lookup(StringView name)
  168. {
  169. Locker locker(m_parent_fs.m_lock);
  170. auto component = m_associated_component->lookup(name);
  171. if (!component)
  172. return {};
  173. return component->to_inode(m_parent_fs);
  174. }
  175. KResultOr<size_t> SysFSDirectoryInode::directory_entry_count() const
  176. {
  177. Locker locker(m_lock);
  178. return m_associated_component->entries_count();
  179. }
  180. }