Inode.cpp 13 KB

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