Inode.cpp 10 KB

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