Inode.cpp 6.8 KB

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