Inode.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/NonnullRefPtrVector.h>
  27. #include <AK/Singleton.h>
  28. #include <AK/StringBuilder.h>
  29. #include <AK/StringView.h>
  30. #include <Kernel/API/InodeWatcherEvent.h>
  31. #include <Kernel/FileSystem/Custody.h>
  32. #include <Kernel/FileSystem/Inode.h>
  33. #include <Kernel/FileSystem/InodeWatcher.h>
  34. #include <Kernel/FileSystem/VirtualFileSystem.h>
  35. #include <Kernel/KBufferBuilder.h>
  36. #include <Kernel/Net/LocalSocket.h>
  37. #include <Kernel/VM/SharedInodeVMObject.h>
  38. namespace Kernel {
  39. static SpinLock s_all_inodes_lock;
  40. static AK::Singleton<InlineLinkedList<Inode>> s_list;
  41. SpinLock<u32>& Inode::all_inodes_lock()
  42. {
  43. return s_all_inodes_lock;
  44. }
  45. InlineLinkedList<Inode>& Inode::all_with_lock()
  46. {
  47. VERIFY(s_all_inodes_lock.is_locked());
  48. return *s_list;
  49. }
  50. void Inode::sync()
  51. {
  52. NonnullRefPtrVector<Inode, 32> inodes;
  53. {
  54. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  55. for (auto& inode : all_with_lock()) {
  56. if (inode.is_metadata_dirty())
  57. inodes.append(inode);
  58. }
  59. }
  60. for (auto& inode : inodes) {
  61. VERIFY(inode.is_metadata_dirty());
  62. inode.flush_metadata();
  63. }
  64. }
  65. KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(FileDescription* description) const
  66. {
  67. KBufferBuilder builder;
  68. ssize_t nread;
  69. u8 buffer[4096];
  70. off_t offset = 0;
  71. for (;;) {
  72. auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
  73. nread = read_bytes(offset, sizeof(buffer), buf, description);
  74. if (nread < 0)
  75. return KResult((ErrnoCode)-nread);
  76. VERIFY(nread <= (ssize_t)sizeof(buffer));
  77. if (nread <= 0)
  78. break;
  79. builder.append((const char*)buffer, nread);
  80. offset += nread;
  81. if (nread < (ssize_t)sizeof(buffer))
  82. break;
  83. }
  84. if (nread < 0) {
  85. dmesgln("Inode::read_entire: Error: {}", nread);
  86. return KResult((ErrnoCode)-nread);
  87. }
  88. auto entire_file = builder.build();
  89. if (!entire_file)
  90. return ENOMEM;
  91. return entire_file.release_nonnull();
  92. }
  93. KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  94. {
  95. // The default implementation simply treats the stored
  96. // contents as a path and resolves that. That is, it
  97. // behaves exactly how you would expect a symlink to work.
  98. auto contents_or = read_entire();
  99. if (contents_or.is_error())
  100. return contents_or.error();
  101. auto& contents = contents_or.value();
  102. auto path = StringView(contents->data(), contents->size());
  103. return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
  104. }
  105. Inode::Inode(FS& fs, InodeIndex index)
  106. : m_fs(fs)
  107. , m_index(index)
  108. {
  109. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  110. all_with_lock().append(this);
  111. }
  112. Inode::~Inode()
  113. {
  114. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  115. all_with_lock().remove(this);
  116. }
  117. void Inode::will_be_destroyed()
  118. {
  119. LOCKER(m_lock);
  120. if (m_metadata_dirty)
  121. flush_metadata();
  122. }
  123. int Inode::set_atime(time_t)
  124. {
  125. return -ENOTIMPL;
  126. }
  127. int Inode::set_ctime(time_t)
  128. {
  129. return -ENOTIMPL;
  130. }
  131. int Inode::set_mtime(time_t)
  132. {
  133. return -ENOTIMPL;
  134. }
  135. KResult Inode::increment_link_count()
  136. {
  137. return ENOTIMPL;
  138. }
  139. KResult Inode::decrement_link_count()
  140. {
  141. return ENOTIMPL;
  142. }
  143. void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
  144. {
  145. LOCKER(m_lock);
  146. m_shared_vmobject = vmobject;
  147. }
  148. bool Inode::bind_socket(LocalSocket& socket)
  149. {
  150. LOCKER(m_lock);
  151. if (m_socket)
  152. return false;
  153. m_socket = socket;
  154. return true;
  155. }
  156. bool Inode::unbind_socket()
  157. {
  158. LOCKER(m_lock);
  159. if (!m_socket)
  160. return false;
  161. m_socket = nullptr;
  162. return true;
  163. }
  164. void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  165. {
  166. LOCKER(m_lock);
  167. VERIFY(!m_watchers.contains(&watcher));
  168. m_watchers.set(&watcher);
  169. }
  170. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  171. {
  172. LOCKER(m_lock);
  173. VERIFY(m_watchers.contains(&watcher));
  174. m_watchers.remove(&watcher);
  175. }
  176. NonnullRefPtr<FIFO> Inode::fifo()
  177. {
  178. LOCKER(m_lock);
  179. VERIFY(metadata().is_fifo());
  180. // FIXME: Release m_fifo when it is closed by all readers and writers
  181. if (!m_fifo)
  182. m_fifo = FIFO::create(metadata().uid);
  183. VERIFY(m_fifo);
  184. return *m_fifo;
  185. }
  186. void Inode::set_metadata_dirty(bool metadata_dirty)
  187. {
  188. LOCKER(m_lock);
  189. if (metadata_dirty) {
  190. // Sanity check.
  191. VERIFY(!fs().is_readonly());
  192. }
  193. if (m_metadata_dirty == metadata_dirty)
  194. return;
  195. m_metadata_dirty = metadata_dirty;
  196. if (m_metadata_dirty) {
  197. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  198. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  199. for (auto& watcher : m_watchers) {
  200. watcher->notify_inode_event({}, InodeWatcherEvent::Type::Modified);
  201. }
  202. }
  203. }
  204. void Inode::did_add_child(const InodeIdentifier& child_id)
  205. {
  206. LOCKER(m_lock);
  207. for (auto& watcher : m_watchers) {
  208. watcher->notify_child_added({}, child_id);
  209. }
  210. }
  211. void Inode::did_remove_child(const InodeIdentifier& child_id)
  212. {
  213. LOCKER(m_lock);
  214. for (auto& watcher : m_watchers) {
  215. watcher->notify_child_removed({}, child_id);
  216. }
  217. }
  218. KResult Inode::prepare_to_write_data()
  219. {
  220. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  221. // We should funnel everything through an interface at the VFS layer so this can happen from a single place.
  222. LOCKER(m_lock);
  223. if (fs().is_readonly())
  224. return EROFS;
  225. auto metadata = this->metadata();
  226. if (metadata.is_setuid() || metadata.is_setgid()) {
  227. dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
  228. return chmod(metadata.mode & ~(04000 | 02000));
  229. }
  230. return KSuccess;
  231. }
  232. RefPtr<SharedInodeVMObject> Inode::shared_vmobject() const
  233. {
  234. LOCKER(m_lock);
  235. return m_shared_vmobject.strong_ref();
  236. }
  237. bool Inode::is_shared_vmobject(const SharedInodeVMObject& other) const
  238. {
  239. LOCKER(m_lock);
  240. return m_shared_vmobject.unsafe_ptr() == &other;
  241. }
  242. }