SysFS.cpp 5.1 KB

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