Inode.cpp 8.0 KB

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