Inode.cpp 11 KB

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