Inode.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. ASSERT(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. ASSERT(inode.is_metadata_dirty());
  62. inode.flush_metadata();
  63. }
  64. }
  65. KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(FileDescription* descriptor) 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, descriptor);
  74. if (nread < 0)
  75. return KResult(nread);
  76. ASSERT(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. klog() << "Inode::read_entire: ERROR: " << nread;
  86. return KResult(nread);
  87. }
  88. auto entire_file = builder.build();
  89. if (!entire_file)
  90. return KResult(-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, unsigned 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. if (m_metadata_dirty)
  120. flush_metadata();
  121. }
  122. void Inode::inode_contents_changed(off_t offset, ssize_t size, const UserOrKernelBuffer& data)
  123. {
  124. if (auto shared_vmobject = this->shared_vmobject())
  125. shared_vmobject->inode_contents_changed({}, offset, size, data);
  126. }
  127. void Inode::inode_size_changed(size_t old_size, size_t new_size)
  128. {
  129. if (auto shared_vmobject = this->shared_vmobject())
  130. shared_vmobject->inode_size_changed({}, old_size, new_size);
  131. }
  132. int Inode::set_atime(time_t)
  133. {
  134. return -ENOTIMPL;
  135. }
  136. int Inode::set_ctime(time_t)
  137. {
  138. return -ENOTIMPL;
  139. }
  140. int Inode::set_mtime(time_t)
  141. {
  142. return -ENOTIMPL;
  143. }
  144. KResult Inode::increment_link_count()
  145. {
  146. return KResult(-ENOTIMPL);
  147. }
  148. KResult Inode::decrement_link_count()
  149. {
  150. return KResult(-ENOTIMPL);
  151. }
  152. void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
  153. {
  154. m_shared_vmobject = vmobject;
  155. }
  156. bool Inode::bind_socket(LocalSocket& socket)
  157. {
  158. LOCKER(m_lock);
  159. if (m_socket)
  160. return false;
  161. m_socket = socket;
  162. return true;
  163. }
  164. bool Inode::unbind_socket()
  165. {
  166. LOCKER(m_lock);
  167. if (!m_socket)
  168. return false;
  169. m_socket = nullptr;
  170. return true;
  171. }
  172. void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  173. {
  174. LOCKER(m_lock);
  175. ASSERT(!m_watchers.contains(&watcher));
  176. m_watchers.set(&watcher);
  177. }
  178. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  179. {
  180. LOCKER(m_lock);
  181. ASSERT(m_watchers.contains(&watcher));
  182. m_watchers.remove(&watcher);
  183. }
  184. FIFO& Inode::fifo()
  185. {
  186. ASSERT(metadata().is_fifo());
  187. // FIXME: Release m_fifo when it is closed by all readers and writers
  188. if (!m_fifo)
  189. m_fifo = FIFO::create(metadata().uid);
  190. ASSERT(m_fifo);
  191. return *m_fifo;
  192. }
  193. void Inode::set_metadata_dirty(bool metadata_dirty)
  194. {
  195. if (m_metadata_dirty == metadata_dirty)
  196. return;
  197. m_metadata_dirty = metadata_dirty;
  198. if (m_metadata_dirty) {
  199. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  200. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  201. LOCKER(m_lock);
  202. for (auto& watcher : m_watchers) {
  203. watcher->notify_inode_event({}, InodeWatcherEvent::Type::Modified);
  204. }
  205. }
  206. }
  207. void Inode::did_add_child(const InodeIdentifier& child_id)
  208. {
  209. LOCKER(m_lock);
  210. for (auto& watcher : m_watchers) {
  211. watcher->notify_child_added({}, child_id);
  212. }
  213. }
  214. void Inode::did_remove_child(const InodeIdentifier& child_id)
  215. {
  216. LOCKER(m_lock);
  217. for (auto& watcher : m_watchers) {
  218. watcher->notify_child_removed({}, child_id);
  219. }
  220. }
  221. KResult Inode::prepare_to_write_data()
  222. {
  223. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  224. // We should funnel everything through an interface at the VFS layer so this can happen from a single place.
  225. LOCKER(m_lock);
  226. if (fs().is_readonly())
  227. return KResult(-EROFS);
  228. auto metadata = this->metadata();
  229. if (metadata.is_setuid() || metadata.is_setgid()) {
  230. dbg() << "Inode::prepare_to_write_data(): Stripping SUID/SGID bits from " << identifier();
  231. return chmod(metadata.mode & ~(04000 | 02000));
  232. }
  233. return KSuccess;
  234. }
  235. RefPtr<SharedInodeVMObject> Inode::shared_vmobject()
  236. {
  237. return m_shared_vmobject.strong_ref();
  238. }
  239. RefPtr<SharedInodeVMObject> Inode::shared_vmobject() const
  240. {
  241. return m_shared_vmobject.strong_ref();
  242. }
  243. bool Inode::is_shared_vmobject(const SharedInodeVMObject& other) const
  244. {
  245. return m_shared_vmobject.unsafe_ptr() == &other;
  246. }
  247. }