Inode.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/Net/LocalSocket.h>
  34. #include <Kernel/VM/SharedInodeVMObject.h>
  35. namespace Kernel {
  36. static SpinLock s_all_inodes_lock;
  37. InlineLinkedList<Inode>& all_inodes()
  38. {
  39. ASSERT(s_all_inodes_lock.is_locked());
  40. static InlineLinkedList<Inode>* list;
  41. if (!list)
  42. list = new InlineLinkedList<Inode>;
  43. return *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_inodes()) {
  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<ByteBuffer> Inode::read_entire(FileDescription* descriptor) const
  61. {
  62. size_t initial_size = metadata().size ? metadata().size : 4096;
  63. StringBuilder builder(initial_size);
  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 nullptr;
  80. }
  81. return builder.to_byte_buffer();
  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. if (!contents) {
  93. if (out_parent)
  94. *out_parent = nullptr;
  95. return KResult(-ENOENT);
  96. }
  97. auto path = StringView(contents.data(), contents.size());
  98. return VFS::the().resolve_path(path, base, out_parent, options, symlink_recursion_level);
  99. }
  100. Inode::Inode(FS& fs, unsigned index)
  101. : m_fs(fs)
  102. , m_index(index)
  103. {
  104. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  105. all_inodes().append(this);
  106. }
  107. Inode::~Inode()
  108. {
  109. ScopedSpinLock all_inodes_lock(s_all_inodes_lock);
  110. all_inodes().remove(this);
  111. }
  112. void Inode::will_be_destroyed()
  113. {
  114. if (m_metadata_dirty)
  115. flush_metadata();
  116. }
  117. void Inode::inode_contents_changed(off_t offset, ssize_t size, const u8* data)
  118. {
  119. if (m_shared_vmobject)
  120. m_shared_vmobject->inode_contents_changed({}, offset, size, data);
  121. }
  122. void Inode::inode_size_changed(size_t old_size, size_t new_size)
  123. {
  124. if (m_shared_vmobject)
  125. m_shared_vmobject->inode_size_changed({}, old_size, new_size);
  126. }
  127. int Inode::set_atime(time_t)
  128. {
  129. return -ENOTIMPL;
  130. }
  131. int Inode::set_ctime(time_t)
  132. {
  133. return -ENOTIMPL;
  134. }
  135. int Inode::set_mtime(time_t)
  136. {
  137. return -ENOTIMPL;
  138. }
  139. KResult Inode::increment_link_count()
  140. {
  141. return KResult(-ENOTIMPL);
  142. }
  143. KResult Inode::decrement_link_count()
  144. {
  145. return KResult(-ENOTIMPL);
  146. }
  147. void Inode::set_shared_vmobject(SharedInodeVMObject& vmobject)
  148. {
  149. m_shared_vmobject = vmobject.make_weak_ptr();
  150. }
  151. bool Inode::bind_socket(LocalSocket& socket)
  152. {
  153. LOCKER(m_lock);
  154. if (m_socket)
  155. return false;
  156. m_socket = socket;
  157. return true;
  158. }
  159. bool Inode::unbind_socket()
  160. {
  161. LOCKER(m_lock);
  162. if (!m_socket)
  163. return false;
  164. m_socket = nullptr;
  165. return true;
  166. }
  167. void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  168. {
  169. LOCKER(m_lock);
  170. ASSERT(!m_watchers.contains(&watcher));
  171. m_watchers.set(&watcher);
  172. }
  173. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  174. {
  175. LOCKER(m_lock);
  176. ASSERT(m_watchers.contains(&watcher));
  177. m_watchers.remove(&watcher);
  178. }
  179. void Inode::set_metadata_dirty(bool metadata_dirty)
  180. {
  181. if (m_metadata_dirty == metadata_dirty)
  182. return;
  183. m_metadata_dirty = metadata_dirty;
  184. if (m_metadata_dirty) {
  185. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  186. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  187. LOCKER(m_lock);
  188. for (auto& watcher : m_watchers) {
  189. watcher->notify_inode_event({}, InodeWatcher::Event::Type::Modified);
  190. }
  191. }
  192. }
  193. void Inode::did_add_child(const String& name)
  194. {
  195. LOCKER(m_lock);
  196. for (auto& watcher : m_watchers) {
  197. watcher->notify_child_added({}, name);
  198. }
  199. }
  200. void Inode::did_remove_child(const String& name)
  201. {
  202. LOCKER(m_lock);
  203. for (auto& watcher : m_watchers) {
  204. watcher->notify_child_removed({}, name);
  205. }
  206. }
  207. KResult Inode::prepare_to_write_data()
  208. {
  209. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  210. // We should funnel everything through an interface at the VFS layer so this can happen from a single place.
  211. LOCKER(m_lock);
  212. if (fs().is_readonly())
  213. return KResult(-EROFS);
  214. auto metadata = this->metadata();
  215. if (metadata.is_setuid() || metadata.is_setgid()) {
  216. dbg() << "Inode::prepare_to_write_data(): Stripping SUID/SGID bits from " << identifier();
  217. return chmod(metadata.mode & ~(04000 | 02000));
  218. }
  219. return KSuccess;
  220. }
  221. }