Inode.cpp 11 KB

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