Inode.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  4. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Singleton.h>
  9. #include <AK/StringView.h>
  10. #include <Kernel/API/InodeWatcherEvent.h>
  11. #include <Kernel/FileSystem/Custody.h>
  12. #include <Kernel/FileSystem/Inode.h>
  13. #include <Kernel/FileSystem/InodeWatcher.h>
  14. #include <Kernel/FileSystem/OpenFileDescription.h>
  15. #include <Kernel/FileSystem/VirtualFileSystem.h>
  16. #include <Kernel/Library/KBufferBuilder.h>
  17. #include <Kernel/Memory/SharedInodeVMObject.h>
  18. #include <Kernel/Net/LocalSocket.h>
  19. #include <Kernel/Tasks/Process.h>
  20. namespace Kernel {
  21. static Singleton<SpinlockProtected<Inode::AllInstancesList, LockRank::None>> s_all_instances;
  22. SpinlockProtected<Inode::AllInstancesList, LockRank::None>& Inode::all_instances()
  23. {
  24. return s_all_instances;
  25. }
  26. void Inode::sync_all()
  27. {
  28. Vector<NonnullRefPtr<Inode>, 32> inodes;
  29. Inode::all_instances().with([&](auto& all_inodes) {
  30. for (auto& inode : all_inodes) {
  31. if (inode.is_metadata_dirty())
  32. inodes.append(inode);
  33. }
  34. });
  35. for (auto& inode : inodes) {
  36. VERIFY(inode->is_metadata_dirty());
  37. (void)inode->flush_metadata();
  38. }
  39. }
  40. void Inode::sync()
  41. {
  42. if (is_metadata_dirty())
  43. (void)flush_metadata();
  44. auto result = fs().flush_writes();
  45. if (result.is_error()) {
  46. // TODO: Figure out how to propagate error to a higher function.
  47. }
  48. }
  49. ErrorOr<NonnullRefPtr<Custody>> Inode::resolve_as_link(Credentials const& credentials, Custody& base, RefPtr<Custody>* out_parent, int options, int symlink_recursion_level) const
  50. {
  51. // The default implementation simply treats the stored
  52. // contents as a path and resolves that. That is, it
  53. // behaves exactly how you would expect a symlink to work.
  54. // Make sure that our assumptions about the path length hold up.
  55. // Note that this doesn't mean that the reported size can be trusted, some inodes just report zero.
  56. VERIFY(size() <= MAXPATHLEN);
  57. Array<u8, MAXPATHLEN> contents;
  58. auto read_bytes = TRY(read_until_filled_or_end(0, contents.size(), UserOrKernelBuffer::for_kernel_buffer(contents.data()), nullptr));
  59. return VirtualFileSystem::the().resolve_path(credentials, StringView { contents.span().trim(read_bytes) }, base, out_parent, options, symlink_recursion_level);
  60. }
  61. Inode::Inode(FileSystem& fs, InodeIndex index)
  62. : m_file_system(fs)
  63. , m_index(index)
  64. {
  65. Inode::all_instances().with([&](auto& all_inodes) { all_inodes.append(*this); });
  66. }
  67. Inode::~Inode()
  68. {
  69. m_watchers.for_each([&](auto& watcher) {
  70. watcher->unregister_by_inode({}, identifier());
  71. });
  72. }
  73. void Inode::will_be_destroyed()
  74. {
  75. MutexLocker locker(m_inode_lock);
  76. if (m_metadata_dirty)
  77. (void)flush_metadata();
  78. }
  79. ErrorOr<size_t> Inode::write_bytes(off_t offset, size_t length, UserOrKernelBuffer const& target_buffer, OpenFileDescription* open_description)
  80. {
  81. MutexLocker locker(m_inode_lock);
  82. TRY(prepare_to_write_data());
  83. return write_bytes_locked(offset, length, target_buffer, open_description);
  84. }
  85. ErrorOr<size_t> Inode::read_bytes(off_t offset, size_t length, UserOrKernelBuffer& buffer, OpenFileDescription* open_description) const
  86. {
  87. MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
  88. return read_bytes_locked(offset, length, buffer, open_description);
  89. }
  90. ErrorOr<size_t> Inode::read_until_filled_or_end(off_t offset, size_t length, UserOrKernelBuffer buffer, OpenFileDescription* open_description) const
  91. {
  92. auto remaining_length = length;
  93. while (remaining_length > 0) {
  94. auto filled_bytes = TRY(read_bytes(offset, remaining_length, buffer, open_description));
  95. if (filled_bytes == 0)
  96. break;
  97. offset += filled_bytes;
  98. remaining_length -= filled_bytes;
  99. }
  100. return length - remaining_length;
  101. }
  102. ErrorOr<void> Inode::update_timestamps([[maybe_unused]] Optional<UnixDateTime> atime, [[maybe_unused]] Optional<UnixDateTime> ctime, [[maybe_unused]] Optional<UnixDateTime> mtime)
  103. {
  104. return ENOTIMPL;
  105. }
  106. ErrorOr<void> Inode::increment_link_count()
  107. {
  108. return ENOTIMPL;
  109. }
  110. ErrorOr<void> Inode::decrement_link_count()
  111. {
  112. return ENOTIMPL;
  113. }
  114. ErrorOr<void> Inode::set_shared_vmobject(Memory::SharedInodeVMObject& vmobject)
  115. {
  116. MutexLocker locker(m_inode_lock);
  117. m_shared_vmobject = TRY(vmobject.try_make_weak_ptr<Memory::SharedInodeVMObject>());
  118. return {};
  119. }
  120. LockRefPtr<LocalSocket> Inode::bound_socket() const
  121. {
  122. return m_bound_socket.strong_ref();
  123. }
  124. bool Inode::bind_socket(LocalSocket& socket)
  125. {
  126. MutexLocker locker(m_inode_lock);
  127. if (m_bound_socket)
  128. return false;
  129. m_bound_socket = socket;
  130. return true;
  131. }
  132. bool Inode::unbind_socket()
  133. {
  134. MutexLocker locker(m_inode_lock);
  135. if (!m_bound_socket)
  136. return false;
  137. m_bound_socket = nullptr;
  138. return true;
  139. }
  140. ErrorOr<void> Inode::register_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  141. {
  142. return m_watchers.with([&](auto& watchers) -> ErrorOr<void> {
  143. VERIFY(!watchers.contains(&watcher));
  144. TRY(watchers.try_set(&watcher));
  145. return {};
  146. });
  147. }
  148. void Inode::unregister_watcher(Badge<InodeWatcher>, InodeWatcher& watcher)
  149. {
  150. m_watchers.with([&](auto& watchers) {
  151. VERIFY(watchers.contains(&watcher));
  152. watchers.remove(&watcher);
  153. });
  154. }
  155. ErrorOr<NonnullRefPtr<FIFO>> Inode::fifo()
  156. {
  157. MutexLocker locker(m_inode_lock);
  158. VERIFY(metadata().is_fifo());
  159. // FIXME: Release m_fifo when it is closed by all readers and writers
  160. if (!m_fifo)
  161. m_fifo = TRY(FIFO::try_create(metadata().uid));
  162. return NonnullRefPtr { *m_fifo };
  163. }
  164. void Inode::set_metadata_dirty(bool metadata_dirty)
  165. {
  166. MutexLocker locker(m_inode_lock);
  167. if (metadata_dirty) {
  168. // Sanity check.
  169. VERIFY(!fs().is_readonly());
  170. }
  171. if (m_metadata_dirty == metadata_dirty)
  172. return;
  173. m_metadata_dirty = metadata_dirty;
  174. if (m_metadata_dirty) {
  175. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  176. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  177. m_watchers.for_each([&](auto& watcher) {
  178. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
  179. });
  180. }
  181. }
  182. void Inode::did_add_child(InodeIdentifier, StringView name)
  183. {
  184. m_watchers.for_each([&](auto& watcher) {
  185. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
  186. });
  187. }
  188. void Inode::did_remove_child(InodeIdentifier, StringView name)
  189. {
  190. if (name == "." || name == "..") {
  191. // These are just aliases and are not interesting to userspace.
  192. return;
  193. }
  194. m_watchers.for_each([&](auto& watcher) {
  195. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
  196. });
  197. }
  198. void Inode::did_modify_contents()
  199. {
  200. // FIXME: What happens if this fails?
  201. // ENOTIMPL would be a meaningless error to return here
  202. auto now = kgettimeofday();
  203. (void)update_timestamps({}, now, now);
  204. m_watchers.for_each([&](auto& watcher) {
  205. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
  206. });
  207. }
  208. void Inode::did_delete_self()
  209. {
  210. m_watchers.for_each([&](auto& watcher) {
  211. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
  212. });
  213. }
  214. ErrorOr<void> Inode::prepare_to_write_data()
  215. {
  216. VERIFY(m_inode_lock.is_locked());
  217. if (fs().is_readonly())
  218. return EROFS;
  219. auto metadata = this->metadata();
  220. if (metadata.is_setuid() || metadata.is_setgid()) {
  221. dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
  222. return chmod(metadata.mode & ~(04000 | 02000));
  223. }
  224. return {};
  225. }
  226. LockRefPtr<Memory::SharedInodeVMObject> Inode::shared_vmobject() const
  227. {
  228. MutexLocker locker(m_inode_lock);
  229. return m_shared_vmobject.strong_ref();
  230. }
  231. template<typename T>
  232. static inline bool range_overlap(T start1, T len1, T start2, T len2)
  233. {
  234. return ((start1 < start2 + len2) || len2 == 0) && ((start2 < start1 + len1) || len1 == 0);
  235. }
  236. static inline ErrorOr<void> normalize_flock(OpenFileDescription const& description, flock& lock)
  237. {
  238. off_t start;
  239. switch (lock.l_whence) {
  240. case SEEK_SET:
  241. start = lock.l_start;
  242. break;
  243. case SEEK_CUR:
  244. start = description.offset() + lock.l_start;
  245. break;
  246. case SEEK_END:
  247. // FIXME: Implement SEEK_END and negative lengths.
  248. return ENOTSUP;
  249. default:
  250. return EINVAL;
  251. }
  252. lock = { lock.l_type, SEEK_SET, start, lock.l_len, 0 };
  253. return {};
  254. }
  255. bool Inode::can_apply_flock(flock const& new_lock, Optional<OpenFileDescription const&> description) const
  256. {
  257. VERIFY(new_lock.l_whence == SEEK_SET);
  258. if (new_lock.l_type == F_UNLCK)
  259. return true;
  260. return m_flocks.with([&](auto& flocks) {
  261. for (auto const& lock : flocks) {
  262. if (!range_overlap(lock.start, lock.len, new_lock.l_start, new_lock.l_len))
  263. continue;
  264. // There are two cases where we can attempt downgrade:
  265. //
  266. // 1) We're the owner of this lock. The downgrade will immediately
  267. // succeed.
  268. // 2) We're not the owner of this lock. Our downgrade attempt will
  269. // fail, and the thread will start blocking on an FlockBlocker.
  270. //
  271. // For the first case, we get the description from try_apply_flock
  272. // below. For the second case, the check below would always be
  273. // false, so there is no need to store the description in the
  274. // blocker in the first place.
  275. if (new_lock.l_type == F_RDLCK && lock.type == F_WRLCK)
  276. return description.has_value() && lock.owner == &description.value() && lock.start == new_lock.l_start && lock.len == new_lock.l_len;
  277. if (new_lock.l_type == F_WRLCK)
  278. return false;
  279. }
  280. return true;
  281. });
  282. }
  283. ErrorOr<bool> Inode::try_apply_flock(Process const& process, OpenFileDescription const& description, flock const& new_lock)
  284. {
  285. return m_flocks.with([&](auto& flocks) -> ErrorOr<bool> {
  286. if (!can_apply_flock(new_lock, description))
  287. return false;
  288. bool did_manipulate_lock = false;
  289. for (size_t i = 0; i < flocks.size(); ++i) {
  290. auto const& lock = flocks[i];
  291. bool is_potential_downgrade = new_lock.l_type == F_RDLCK && lock.type == F_WRLCK;
  292. bool is_potential_unlock = new_lock.l_type == F_UNLCK;
  293. bool is_lock_owner = &description == lock.owner;
  294. bool lock_range_exactly_matches = lock.start == new_lock.l_start && lock.len == new_lock.l_len;
  295. bool can_manage_this_lock = is_lock_owner && lock_range_exactly_matches;
  296. if ((is_potential_downgrade || is_potential_unlock) && can_manage_this_lock) {
  297. flocks.remove(i);
  298. did_manipulate_lock = true;
  299. break;
  300. }
  301. }
  302. if (new_lock.l_type != F_UNLCK)
  303. TRY(flocks.try_append(Flock { new_lock.l_start, new_lock.l_len, &description, process.pid().value(), new_lock.l_type }));
  304. if (did_manipulate_lock)
  305. m_flock_blocker_set.unblock_all_blockers_whose_conditions_are_met();
  306. // Judging by the Linux implementation, unlocking a non-existent lock
  307. // also works.
  308. return true;
  309. });
  310. }
  311. ErrorOr<void> Inode::apply_flock(Process const& process, OpenFileDescription const& description, Userspace<flock const*> input_lock, ShouldBlock should_block)
  312. {
  313. auto new_lock = TRY(copy_typed_from_user(input_lock));
  314. TRY(normalize_flock(description, new_lock));
  315. while (true) {
  316. auto success = TRY(try_apply_flock(process, description, new_lock));
  317. if (success)
  318. return {};
  319. if (should_block == ShouldBlock::No)
  320. return EAGAIN;
  321. if (Thread::current()->block<Thread::FlockBlocker>({}, *this, new_lock).was_interrupted())
  322. return EINTR;
  323. }
  324. }
  325. ErrorOr<void> Inode::get_flock(OpenFileDescription const& description, Userspace<flock*> reference_lock) const
  326. {
  327. flock lookup = {};
  328. TRY(copy_from_user(&lookup, reference_lock));
  329. TRY(normalize_flock(description, lookup));
  330. return m_flocks.with([&](auto& flocks) {
  331. for (auto const& lock : flocks) {
  332. if (!range_overlap(lock.start, lock.len, lookup.l_start, lookup.l_len))
  333. continue;
  334. // Locks with the same owner can't conflict with each other.
  335. if (lock.pid == Process::current().pid())
  336. continue;
  337. if ((lookup.l_type == F_RDLCK && lock.type == F_WRLCK) || lookup.l_type == F_WRLCK) {
  338. lookup = { lock.type, SEEK_SET, lock.start, lock.len, lock.pid };
  339. return copy_to_user(reference_lock, &lookup);
  340. }
  341. }
  342. lookup.l_type = F_UNLCK;
  343. return copy_to_user(reference_lock, &lookup);
  344. });
  345. }
  346. void Inode::remove_flocks_for_description(OpenFileDescription const& description)
  347. {
  348. m_flocks.with([&](auto& flocks) {
  349. flocks.remove_all_matching([&](auto& entry) { return entry.owner == &description; });
  350. });
  351. }
  352. bool Inode::has_watchers() const
  353. {
  354. return !m_watchers.with([&](auto& watchers) { return watchers.is_empty(); });
  355. }
  356. }