Inode.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/NonnullRefPtrVector.h>
  7. #include <AK/Singleton.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/API/InodeWatcherEvent.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/Inode.h>
  13. #include <Kernel/FileSystem/InodeWatcher.h>
  14. #include <Kernel/FileSystem/VirtualFileSystem.h>
  15. #include <Kernel/KBufferBuilder.h>
  16. #include <Kernel/Net/LocalSocket.h>
  17. #include <Kernel/VM/SharedInodeVMObject.h>
  18. namespace Kernel {
  19. static SpinLock s_all_inodes_lock;
  20. static AK::Singleton<InlineLinkedList<Inode>> s_list;
  21. SpinLock<u32>& Inode::all_inodes_lock()
  22. {
  23. return s_all_inodes_lock;
  24. }
  25. InlineLinkedList<Inode>& Inode::all_with_lock()
  26. {
  27. VERIFY(s_all_inodes_lock.is_locked());
  28. return *s_list;
  29. }
  30. void Inode::sync()
  31. {
  32. NonnullRefPtrVector<Inode, 32> inodes;
  33. {
  34. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  35. for (auto& inode : all_with_lock()) {
  36. if (inode.is_metadata_dirty())
  37. inodes.append(inode);
  38. }
  39. }
  40. for (auto& inode : inodes) {
  41. VERIFY(inode.is_metadata_dirty());
  42. inode.flush_metadata();
  43. }
  44. }
  45. KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(FileDescription* description) const
  46. {
  47. KBufferBuilder builder;
  48. ssize_t nread;
  49. u8 buffer[4096];
  50. off_t offset = 0;
  51. for (;;) {
  52. auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
  53. nread = read_bytes(offset, sizeof(buffer), buf, description);
  54. if (nread < 0)
  55. return KResult((ErrnoCode)-nread);
  56. VERIFY(nread <= (ssize_t)sizeof(buffer));
  57. if (nread <= 0)
  58. break;
  59. builder.append((const char*)buffer, nread);
  60. offset += nread;
  61. if (nread < (ssize_t)sizeof(buffer))
  62. break;
  63. }
  64. if (nread < 0) {
  65. dmesgln("Inode::read_entire: Error: {}", nread);
  66. return KResult((ErrnoCode)-nread);
  67. }
  68. auto entire_file = builder.build();
  69. if (!entire_file)
  70. return ENOMEM;
  71. return entire_file.release_nonnull();
  72. }
  73. KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  74. {
  75. // The default implementation simply treats the stored
  76. // contents as a path and resolves that. That is, it
  77. // behaves exactly how you would expect a symlink to work.
  78. auto contents_or = read_entire();
  79. if (contents_or.is_error())
  80. return contents_or.error();
  81. auto& contents = contents_or.value();
  82. auto path = StringView(contents->data(), contents->size());
  83. return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
  84. }
  85. Inode::Inode(FS& fs, InodeIndex index)
  86. : m_fs(fs)
  87. , m_index(index)
  88. {
  89. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  90. all_with_lock().append(this);
  91. }
  92. Inode::~Inode()
  93. {
  94. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  95. all_with_lock().remove(this);
  96. }
  97. void Inode::will_be_destroyed()
  98. {
  99. Locker locker(m_lock);
  100. if (m_metadata_dirty)
  101. flush_metadata();
  102. }
  103. KResult Inode::set_atime(time_t)
  104. {
  105. return ENOTIMPL;
  106. }
  107. KResult Inode::set_ctime(time_t)
  108. {
  109. return ENOTIMPL;
  110. }
  111. KResult Inode::set_mtime(time_t)
  112. {
  113. return ENOTIMPL;
  114. }
  115. KResult Inode::increment_link_count()
  116. {
  117. return ENOTIMPL;
  118. }
  119. KResult Inode::decrement_link_count()
  120. {
  121. return ENOTIMPL;
  122. }
  123. void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
  124. {
  125. Locker locker(m_lock);
  126. m_shared_vmobject = vmobject;
  127. }
  128. bool Inode::bind_socket(LocalSocket& socket)
  129. {
  130. Locker locker(m_lock);
  131. if (m_socket)
  132. return false;
  133. m_socket = socket;
  134. return true;
  135. }
  136. bool Inode::unbind_socket()
  137. {
  138. Locker locker(m_lock);
  139. if (!m_socket)
  140. return false;
  141. m_socket = nullptr;
  142. return true;
  143. }
  144. void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  145. {
  146. Locker locker(m_lock);
  147. VERIFY(!m_watchers.contains(&watcher));
  148. m_watchers.set(&watcher);
  149. }
  150. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  151. {
  152. Locker locker(m_lock);
  153. VERIFY(m_watchers.contains(&watcher));
  154. m_watchers.remove(&watcher);
  155. }
  156. NonnullRefPtr<FIFO> Inode::fifo()
  157. {
  158. Locker locker(m_lock);
  159. VERIFY(metadata().is_fifo());
  160. // FIXME: Release m_fifo when it is closed by all readers and writers
  161. if (!m_fifo)
  162. m_fifo = FIFO::create(metadata().uid);
  163. VERIFY(m_fifo);
  164. return *m_fifo;
  165. }
  166. void Inode::set_metadata_dirty(bool metadata_dirty)
  167. {
  168. Locker locker(m_lock);
  169. if (metadata_dirty) {
  170. // Sanity check.
  171. VERIFY(!fs().is_readonly());
  172. }
  173. if (m_metadata_dirty == metadata_dirty)
  174. return;
  175. m_metadata_dirty = metadata_dirty;
  176. if (m_metadata_dirty) {
  177. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  178. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  179. for (auto& watcher : m_watchers) {
  180. watcher->notify_inode_event({}, InodeWatcherEvent::Type::Modified);
  181. }
  182. }
  183. }
  184. void Inode::did_add_child(const InodeIdentifier& child_id)
  185. {
  186. Locker locker(m_lock);
  187. for (auto& watcher : m_watchers) {
  188. watcher->notify_child_added({}, child_id);
  189. }
  190. }
  191. void Inode::did_remove_child(const InodeIdentifier& child_id)
  192. {
  193. Locker locker(m_lock);
  194. for (auto& watcher : m_watchers) {
  195. watcher->notify_child_removed({}, child_id);
  196. }
  197. }
  198. KResult Inode::prepare_to_write_data()
  199. {
  200. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  201. // We should funnel everything through an interface at the VFS layer so this can happen from a single place.
  202. Locker locker(m_lock);
  203. if (fs().is_readonly())
  204. return EROFS;
  205. auto metadata = this->metadata();
  206. if (metadata.is_setuid() || metadata.is_setgid()) {
  207. dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
  208. return chmod(metadata.mode & ~(04000 | 02000));
  209. }
  210. return KSuccess;
  211. }
  212. RefPtr<SharedInodeVMObject> Inode::shared_vmobject() const
  213. {
  214. Locker locker(m_lock);
  215. return m_shared_vmobject.strong_ref();
  216. }
  217. bool Inode::is_shared_vmobject(const SharedInodeVMObject& other) const
  218. {
  219. Locker locker(m_lock);
  220. return m_shared_vmobject.unsafe_ptr() == &other;
  221. }
  222. }