SysFS.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_directory(SysFSRootDirectory::create())
  23. {
  24. }
  25. UNMAP_AFTER_INIT void SysFSComponentRegistry::register_new_component(SysFSComponent& component)
  26. {
  27. Locker locker(m_lock);
  28. m_root_directory->m_components.append(component);
  29. }
  30. NonnullRefPtr<SysFSRootDirectory> SysFSRootDirectory::create()
  31. {
  32. return adopt_ref(*new (nothrow) SysFSRootDirectory);
  33. }
  34. KResult SysFSRootDirectory::traverse_as_directory(unsigned fsid, Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  35. {
  36. Locker locker(SysFSComponentRegistry::the().get_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. SysFSRootDirectory::SysFSRootDirectory()
  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().root_directory().to_inode(*this))
  55. {
  56. }
  57. SysFS::~SysFS()
  58. {
  59. }
  60. bool SysFS::initialize()
  61. {
  62. return true;
  63. }
  64. NonnullRefPtr<Inode> SysFS::root_inode() const
  65. {
  66. return *m_root_inode;
  67. }
  68. NonnullRefPtr<SysFSInode> SysFSInode::create(SysFS const& fs, SysFSComponent const& component)
  69. {
  70. return adopt_ref(*new (nothrow) SysFSInode(fs, component));
  71. }
  72. SysFSInode::SysFSInode(SysFS const& fs, SysFSComponent const& component)
  73. : Inode(const_cast<SysFS&>(fs), component.component_index())
  74. , m_associated_component(component)
  75. {
  76. }
  77. KResultOr<size_t> SysFSInode::read_bytes(off_t offset, size_t count, UserOrKernelBuffer& buffer, FileDescription* fd) const
  78. {
  79. return m_associated_component->read_bytes(offset, count, buffer, fd);
  80. }
  81. KResult SysFSInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)>) const
  82. {
  83. VERIFY_NOT_REACHED();
  84. }
  85. RefPtr<Inode> SysFSInode::lookup(StringView)
  86. {
  87. VERIFY_NOT_REACHED();
  88. }
  89. InodeMetadata SysFSInode::metadata() const
  90. {
  91. Locker locker(m_inode_lock);
  92. InodeMetadata metadata;
  93. metadata.inode = { fsid(), m_associated_component->component_index() };
  94. metadata.mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH;
  95. metadata.uid = 0;
  96. metadata.gid = 0;
  97. metadata.size = m_associated_component->size();
  98. metadata.mtime = mepoch;
  99. return metadata;
  100. }
  101. void SysFSInode::flush_metadata()
  102. {
  103. }
  104. KResultOr<size_t> SysFSInode::write_bytes(off_t offset, size_t count, UserOrKernelBuffer const& buffer, FileDescription* fd)
  105. {
  106. return m_associated_component->write_bytes(offset, count, buffer, fd);
  107. }
  108. KResultOr<NonnullRefPtr<Inode>> SysFSInode::create_child(StringView, mode_t, dev_t, uid_t, gid_t)
  109. {
  110. return EROFS;
  111. }
  112. KResult SysFSInode::add_child(Inode&, StringView const&, mode_t)
  113. {
  114. return EROFS;
  115. }
  116. KResult SysFSInode::remove_child(StringView const&)
  117. {
  118. return EROFS;
  119. }
  120. KResult SysFSInode::chmod(mode_t)
  121. {
  122. return EPERM;
  123. }
  124. KResult SysFSInode::chown(uid_t, gid_t)
  125. {
  126. return EPERM;
  127. }
  128. KResult SysFSInode::truncate(u64)
  129. {
  130. return EPERM;
  131. }
  132. NonnullRefPtr<SysFSDirectoryInode> SysFSDirectoryInode::create(SysFS const& sysfs, SysFSComponent const& component)
  133. {
  134. return adopt_ref(*new (nothrow) SysFSDirectoryInode(sysfs, component));
  135. }
  136. SysFSDirectoryInode::SysFSDirectoryInode(SysFS const& fs, SysFSComponent const& component)
  137. : SysFSInode(fs, component)
  138. , m_parent_fs(const_cast<SysFS&>(fs))
  139. {
  140. }
  141. SysFSDirectoryInode::~SysFSDirectoryInode()
  142. {
  143. }
  144. InodeMetadata SysFSDirectoryInode::metadata() const
  145. {
  146. Locker locker(m_inode_lock);
  147. InodeMetadata metadata;
  148. metadata.inode = { fsid(), m_associated_component->component_index() };
  149. metadata.mode = S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH | S_IXOTH;
  150. metadata.uid = 0;
  151. metadata.gid = 0;
  152. metadata.size = 0;
  153. metadata.mtime = mepoch;
  154. return metadata;
  155. }
  156. KResult SysFSDirectoryInode::traverse_as_directory(Function<bool(FileSystem::DirectoryEntryView const&)> callback) const
  157. {
  158. Locker locker(m_parent_fs.m_lock);
  159. return m_associated_component->traverse_as_directory(m_parent_fs.fsid(), move(callback));
  160. }
  161. RefPtr<Inode> SysFSDirectoryInode::lookup(StringView name)
  162. {
  163. Locker locker(m_parent_fs.m_lock);
  164. auto component = m_associated_component->lookup(name);
  165. if (!component)
  166. return {};
  167. return component->to_inode(m_parent_fs);
  168. }
  169. }