Inode.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <AK/Singleton.h>
  9. #include <AK/StringBuilder.h>
  10. #include <AK/StringView.h>
  11. #include <Kernel/API/InodeWatcherEvent.h>
  12. #include <Kernel/FileSystem/Custody.h>
  13. #include <Kernel/FileSystem/Inode.h>
  14. #include <Kernel/FileSystem/InodeWatcher.h>
  15. #include <Kernel/FileSystem/OpenFileDescription.h>
  16. #include <Kernel/FileSystem/VirtualFileSystem.h>
  17. #include <Kernel/KBufferBuilder.h>
  18. #include <Kernel/Memory/SharedInodeVMObject.h>
  19. #include <Kernel/Net/LocalSocket.h>
  20. #include <Kernel/Process.h>
  21. namespace Kernel {
  22. static Singleton<SpinlockProtected<Inode::AllInstancesList>> s_all_instances;
  23. SpinlockProtected<Inode::AllInstancesList>& Inode::all_instances()
  24. {
  25. return s_all_instances;
  26. }
  27. void Inode::sync()
  28. {
  29. NonnullRefPtrVector<Inode, 32> inodes;
  30. Inode::all_instances().with([&](auto& all_inodes) {
  31. for (auto& inode : all_inodes) {
  32. if (inode.is_metadata_dirty())
  33. inodes.append(inode);
  34. }
  35. });
  36. for (auto& inode : inodes) {
  37. VERIFY(inode.is_metadata_dirty());
  38. inode.flush_metadata();
  39. }
  40. }
  41. KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(OpenFileDescription* description) const
  42. {
  43. auto builder = TRY(KBufferBuilder::try_create());
  44. u8 buffer[4096];
  45. off_t offset = 0;
  46. for (;;) {
  47. auto buf = UserOrKernelBuffer::for_kernel_buffer(buffer);
  48. auto nread = TRY(read_bytes(offset, sizeof(buffer), buf, description));
  49. VERIFY(nread <= sizeof(buffer));
  50. if (nread == 0)
  51. break;
  52. TRY(builder.append((const char*)buffer, nread));
  53. offset += nread;
  54. if (nread < sizeof(buffer))
  55. break;
  56. }
  57. auto entire_file = builder.build();
  58. if (!entire_file)
  59. return ENOMEM;
  60. return entire_file.release_nonnull();
  61. }
  62. KResultOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  63. {
  64. // The default implementation simply treats the stored
  65. // contents as a path and resolves that. That is, it
  66. // behaves exactly how you would expect a symlink to work.
  67. auto contents = TRY(read_entire());
  68. return VirtualFileSystem::the().resolve_path(StringView { contents->bytes() }, base, out_parent, options, symlink_recursion_level);
  69. }
  70. Inode::Inode(FileSystem& fs, InodeIndex index)
  71. : m_file_system(fs)
  72. , m_index(index)
  73. {
  74. Inode::all_instances().with([&](auto& all_inodes) { all_inodes.append(*this); });
  75. }
  76. Inode::~Inode()
  77. {
  78. for (auto& watcher : m_watchers) {
  79. watcher->unregister_by_inode({}, identifier());
  80. }
  81. }
  82. void Inode::will_be_destroyed()
  83. {
  84. MutexLocker locker(m_inode_lock);
  85. if (m_metadata_dirty)
  86. flush_metadata();
  87. }
  88. KResult Inode::set_atime(time_t)
  89. {
  90. return ENOTIMPL;
  91. }
  92. KResult Inode::set_ctime(time_t)
  93. {
  94. return ENOTIMPL;
  95. }
  96. KResult Inode::set_mtime(time_t)
  97. {
  98. return ENOTIMPL;
  99. }
  100. KResult Inode::increment_link_count()
  101. {
  102. return ENOTIMPL;
  103. }
  104. KResult Inode::decrement_link_count()
  105. {
  106. return ENOTIMPL;
  107. }
  108. void Inode::set_shared_vmobject(Memory::SharedInodeVMObject& vmobject)
  109. {
  110. MutexLocker locker(m_inode_lock);
  111. m_shared_vmobject = vmobject;
  112. }
  113. bool Inode::bind_socket(LocalSocket& socket)
  114. {
  115. MutexLocker locker(m_inode_lock);
  116. if (m_socket)
  117. return false;
  118. m_socket = socket;
  119. return true;
  120. }
  121. bool Inode::unbind_socket()
  122. {
  123. MutexLocker locker(m_inode_lock);
  124. if (!m_socket)
  125. return false;
  126. m_socket = nullptr;
  127. return true;
  128. }
  129. void Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  130. {
  131. MutexLocker locker(m_inode_lock);
  132. VERIFY(!m_watchers.contains(&watcher));
  133. m_watchers.set(&watcher);
  134. }
  135. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  136. {
  137. MutexLocker locker(m_inode_lock);
  138. VERIFY(m_watchers.contains(&watcher));
  139. m_watchers.remove(&watcher);
  140. }
  141. KResultOr<NonnullRefPtr<FIFO>> Inode::fifo()
  142. {
  143. MutexLocker locker(m_inode_lock);
  144. VERIFY(metadata().is_fifo());
  145. // FIXME: Release m_fifo when it is closed by all readers and writers
  146. if (!m_fifo)
  147. m_fifo = TRY(FIFO::try_create(metadata().uid));
  148. return *m_fifo;
  149. }
  150. void Inode::set_metadata_dirty(bool metadata_dirty)
  151. {
  152. MutexLocker locker(m_inode_lock);
  153. if (metadata_dirty) {
  154. // Sanity check.
  155. VERIFY(!fs().is_readonly());
  156. }
  157. if (m_metadata_dirty == metadata_dirty)
  158. return;
  159. m_metadata_dirty = metadata_dirty;
  160. if (m_metadata_dirty) {
  161. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  162. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  163. for (auto& watcher : m_watchers) {
  164. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
  165. }
  166. }
  167. }
  168. void Inode::did_add_child(InodeIdentifier const&, String const& name)
  169. {
  170. MutexLocker locker(m_inode_lock);
  171. for (auto& watcher : m_watchers) {
  172. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
  173. }
  174. }
  175. void Inode::did_remove_child(InodeIdentifier const&, String const& name)
  176. {
  177. MutexLocker locker(m_inode_lock);
  178. if (name == "." || name == "..") {
  179. // These are just aliases and are not interesting to userspace.
  180. return;
  181. }
  182. for (auto& watcher : m_watchers) {
  183. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
  184. }
  185. }
  186. void Inode::did_modify_contents()
  187. {
  188. MutexLocker locker(m_inode_lock);
  189. for (auto& watcher : m_watchers) {
  190. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
  191. }
  192. }
  193. void Inode::did_delete_self()
  194. {
  195. MutexLocker locker(m_inode_lock);
  196. for (auto& watcher : m_watchers) {
  197. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
  198. }
  199. }
  200. KResult Inode::prepare_to_write_data()
  201. {
  202. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  203. // We should funnel everything through an interface at the VirtualFileSystem layer so this can happen from a single place.
  204. MutexLocker locker(m_inode_lock);
  205. if (fs().is_readonly())
  206. return EROFS;
  207. auto metadata = this->metadata();
  208. if (metadata.is_setuid() || metadata.is_setgid()) {
  209. dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
  210. return chmod(metadata.mode & ~(04000 | 02000));
  211. }
  212. return KSuccess;
  213. }
  214. RefPtr<Memory::SharedInodeVMObject> Inode::shared_vmobject() const
  215. {
  216. MutexLocker locker(m_inode_lock);
  217. return m_shared_vmobject.strong_ref();
  218. }
  219. template<typename T>
  220. static inline bool range_overlap(T start1, T len1, T start2, T len2)
  221. {
  222. return ((start1 < start2 + len2) || len2 == 0) && ((start2 < start1 + len1) || len1 == 0);
  223. }
  224. static inline KResult normalize_flock(OpenFileDescription const& description, flock& lock)
  225. {
  226. off_t start;
  227. switch (lock.l_whence) {
  228. case SEEK_SET:
  229. start = lock.l_start;
  230. break;
  231. case SEEK_CUR:
  232. start = description.offset() + lock.l_start;
  233. break;
  234. case SEEK_END:
  235. // FIXME: Implement SEEK_END and negative lengths.
  236. return ENOTSUP;
  237. default:
  238. return EINVAL;
  239. }
  240. lock = { lock.l_type, SEEK_SET, start, lock.l_len, 0 };
  241. return KSuccess;
  242. }
  243. KResult Inode::can_apply_flock(OpenFileDescription const& description, flock const& new_lock) const
  244. {
  245. VERIFY(new_lock.l_whence == SEEK_SET);
  246. MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
  247. if (new_lock.l_type == F_UNLCK) {
  248. for (auto& lock : m_flocks) {
  249. if (&description == lock.owner && lock.start == new_lock.l_start && lock.len == new_lock.l_len)
  250. return KSuccess;
  251. }
  252. return EINVAL;
  253. }
  254. for (auto& lock : m_flocks) {
  255. if (!range_overlap(lock.start, lock.len, new_lock.l_start, new_lock.l_len))
  256. continue;
  257. if (new_lock.l_type == F_RDLCK && lock.type == F_WRLCK)
  258. return EAGAIN;
  259. if (new_lock.l_type == F_WRLCK)
  260. return EAGAIN;
  261. }
  262. return KSuccess;
  263. }
  264. KResult Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace<flock const*> input_lock)
  265. {
  266. flock new_lock = {};
  267. TRY(copy_from_user(&new_lock, input_lock));
  268. TRY(normalize_flock(description, new_lock));
  269. MutexLocker locker(m_inode_lock);
  270. TRY(can_apply_flock(description, new_lock));
  271. if (new_lock.l_type == F_UNLCK) {
  272. for (size_t i = 0; i < m_flocks.size(); ++i) {
  273. if (&description == m_flocks[i].owner && m_flocks[i].start == new_lock.l_start && m_flocks[i].len == new_lock.l_len) {
  274. m_flocks.remove(i);
  275. return KSuccess;
  276. }
  277. }
  278. return EINVAL;
  279. }
  280. m_flocks.append(Flock { new_lock.l_type, new_lock.l_start, new_lock.l_len, &description, process.pid().value() });
  281. return KSuccess;
  282. }
  283. KResult Inode::get_flock(OpenFileDescription const& description, Userspace<flock*> reference_lock) const
  284. {
  285. flock lookup = {};
  286. TRY(copy_from_user(&lookup, reference_lock));
  287. TRY(normalize_flock(description, lookup));
  288. MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
  289. for (auto& lock : m_flocks) {
  290. if (!range_overlap(lock.start, lock.len, lookup.l_start, lookup.l_len))
  291. continue;
  292. if ((lookup.l_type == F_RDLCK && lock.type == F_WRLCK) || lookup.l_type == F_WRLCK) {
  293. lookup = { lock.type, SEEK_SET, lock.start, lock.len, lock.pid };
  294. return copy_to_user(reference_lock, &lookup);
  295. }
  296. }
  297. lookup.l_type = F_UNLCK;
  298. return copy_to_user(reference_lock, &lookup);
  299. }
  300. void Inode::remove_flocks_for_description(OpenFileDescription const& description)
  301. {
  302. MutexLocker locker(m_inode_lock);
  303. for (size_t i = 0; i < m_flocks.size(); ++i) {
  304. if (&description == m_flocks[i].owner)
  305. m_flocks.remove(i--);
  306. }
  307. }
  308. }