Inode.cpp 7.0 KB

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