Inode.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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/FileSystem/Custody.h>
  31. #include <Kernel/FileSystem/Inode.h>
  32. #include <Kernel/FileSystem/InodeWatcher.h>
  33. #include <Kernel/FileSystem/VirtualFileSystem.h>
  34. #include <Kernel/KBufferBuilder.h>
  35. #include <Kernel/Net/LocalSocket.h>
  36. #include <Kernel/VM/SharedInodeVMObject.h>
  37. namespace Kernel {
  38. static SpinLock s_all_inodes_lock;
  39. static AK::Singleton<InlineLinkedList<Inode>> s_list;
  40. InlineLinkedList<Inode>& Inode::all_with_lock()
  41. {
  42. ASSERT(s_all_inodes_lock.is_locked());
  43. return *s_list;
  44. }
  45. void Inode::sync()
  46. {
  47. NonnullRefPtrVector<Inode, 32> inodes;
  48. {
  49. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  50. for (auto& inode : all_with_lock()) {
  51. if (inode.is_metadata_dirty())
  52. inodes.append(inode);
  53. }
  54. }
  55. for (auto& inode : inodes) {
  56. ASSERT(inode.is_metadata_dirty());
  57. inode.flush_metadata();
  58. }
  59. }
  60. KResultOr<KBuffer> Inode::read_entire(FileDescription* descriptor) const
  61. {
  62. KBufferBuilder builder;
  63. ssize_t nread;
  64. u8 buffer[4096];
  65. off_t offset = 0;
  66. for (;;) {
  67. auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
  68. nread = read_bytes(offset, sizeof(buffer), buf, descriptor);
  69. if (nread < 0)
  70. return KResult(nread);
  71. ASSERT(nread <= (ssize_t)sizeof(buffer));
  72. if (nread <= 0)
  73. break;
  74. builder.append((const char*)buffer, nread);
  75. offset += nread;
  76. if (nread < (ssize_t)sizeof(buffer))
  77. break;
  78. }
  79. if (nread < 0) {
  80. klog() << "Inode::read_entire: ERROR: " << nread;
  81. return KResult(nread);
  82. }
  83. return builder.build();
  84. }
  85. KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  86. {
  87. // The default implementation simply treats the stored
  88. // contents as a path and resolves that. That is, it
  89. // behaves exactly how you would expect a symlink to work.
  90. auto contents_or = read_entire();
  91. if (contents_or.is_error())
  92. return contents_or.error();
  93. auto& contents = contents_or.value();
  94. auto path = StringView(contents.data(), contents.size());
  95. return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
  96. }
  97. Inode::Inode(FS& fs, unsigned index)
  98. : m_fs(fs)
  99. , m_index(index)
  100. {
  101. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  102. all_with_lock().append(this);
  103. }
  104. Inode::~Inode()
  105. {
  106. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  107. all_with_lock().remove(this);
  108. }
  109. void Inode::will_be_destroyed()
  110. {
  111. if (m_metadata_dirty)
  112. flush_metadata();
  113. }
  114. void Inode::inode_contents_changed(off_t offset, ssize_t size, const UserOrKernelBuffer& data)
  115. {
  116. if (m_shared_vmobject)
  117. m_shared_vmobject->inode_contents_changed({}, offset, size, data);
  118. }
  119. void Inode::inode_size_changed(size_t old_size, size_t new_size)
  120. {
  121. if (m_shared_vmobject)
  122. m_shared_vmobject->inode_size_changed({}, old_size, new_size);
  123. }
  124. int Inode::set_atime(time_t)
  125. {
  126. return -ENOTIMPL;
  127. }
  128. int Inode::set_ctime(time_t)
  129. {
  130. return -ENOTIMPL;
  131. }
  132. int Inode::set_mtime(time_t)
  133. {
  134. return -ENOTIMPL;
  135. }
  136. KResult Inode::increment_link_count()
  137. {
  138. return KResult(-ENOTIMPL);
  139. }
  140. KResult Inode::decrement_link_count()
  141. {
  142. return KResult(-ENOTIMPL);
  143. }
  144. void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
  145. {
  146. m_shared_vmobject = vmobject.make_weak_ptr();
  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. ASSERT(!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. ASSERT(m_watchers.contains(&watcher));
  174. m_watchers.remove(&watcher);
  175. }
  176. FIFO& Inode::fifo()
  177. {
  178. ASSERT(metadata().is_fifo());
  179. // FIXME: Release m_fifo when it is closed by all readers and writers
  180. if (!m_fifo)
  181. m_fifo = FIFO::create(metadata().uid);
  182. ASSERT(m_fifo);
  183. return *m_fifo;
  184. }
  185. void Inode::set_metadata_dirty(bool metadata_dirty)
  186. {
  187. if (m_metadata_dirty == metadata_dirty)
  188. return;
  189. m_metadata_dirty = metadata_dirty;
  190. if (m_metadata_dirty) {
  191. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  192. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  193. LOCKER(m_lock);
  194. for (auto& watcher : m_watchers) {
  195. watcher->notify_inode_event({}, InodeWatcher::Event::Type::Modified);
  196. }
  197. }
  198. }
  199. void Inode::did_add_child(const String& name)
  200. {
  201. LOCKER(m_lock);
  202. for (auto& watcher : m_watchers) {
  203. watcher->notify_child_added({}, name);
  204. }
  205. }
  206. void Inode::did_remove_child(const String& name)
  207. {
  208. LOCKER(m_lock);
  209. for (auto& watcher : m_watchers) {
  210. watcher->notify_child_removed({}, name);
  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(m_lock);
  218. if (fs().is_readonly())
  219. return KResult(-EROFS);
  220. auto metadata = this->metadata();
  221. if (metadata.is_setuid() || metadata.is_setgid()) {
  222. dbg() << "Inode::prepare_to_write_data(): Stripping SUID/SGID bits from " << identifier();
  223. return chmod(metadata.mode & ~(04000 | 02000));
  224. }
  225. return KSuccess;
  226. }
  227. }