Inode.cpp 7.1 KB

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