Inode.cpp 10 KB

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