Inode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (c) 2018-2020, 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 SpinLock s_all_inodes_lock;
  23. static 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(Memory::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.set(&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::try_create(metadata().uid);
  162. // FIXME: We need to be able to observe OOM here.
  163. VERIFY(!m_fifo.is_null());
  164. }
  165. VERIFY(m_fifo);
  166. return *m_fifo;
  167. }
  168. void Inode::set_metadata_dirty(bool metadata_dirty)
  169. {
  170. MutexLocker locker(m_inode_lock);
  171. if (metadata_dirty) {
  172. // Sanity check.
  173. VERIFY(!fs().is_readonly());
  174. }
  175. if (m_metadata_dirty == metadata_dirty)
  176. return;
  177. m_metadata_dirty = metadata_dirty;
  178. if (m_metadata_dirty) {
  179. // FIXME: Maybe we should hook into modification events somewhere else, I'm not sure where.
  180. // We don't always end up on this particular code path, for instance when writing to an ext2fs file.
  181. for (auto& watcher : m_watchers) {
  182. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::MetadataModified);
  183. }
  184. }
  185. }
  186. void Inode::did_add_child(InodeIdentifier const&, String const& name)
  187. {
  188. MutexLocker locker(m_inode_lock);
  189. for (auto& watcher : m_watchers) {
  190. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildCreated, name);
  191. }
  192. }
  193. void Inode::did_remove_child(InodeIdentifier const&, String const& name)
  194. {
  195. MutexLocker locker(m_inode_lock);
  196. if (name == "." || name == "..") {
  197. // These are just aliases and are not interesting to userspace.
  198. return;
  199. }
  200. for (auto& watcher : m_watchers) {
  201. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ChildDeleted, name);
  202. }
  203. }
  204. void Inode::did_modify_contents()
  205. {
  206. MutexLocker locker(m_inode_lock);
  207. for (auto& watcher : m_watchers) {
  208. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::ContentModified);
  209. }
  210. }
  211. void Inode::did_delete_self()
  212. {
  213. MutexLocker locker(m_inode_lock);
  214. for (auto& watcher : m_watchers) {
  215. watcher->notify_inode_event({}, identifier(), InodeWatcherEvent::Type::Deleted);
  216. }
  217. }
  218. KResult Inode::prepare_to_write_data()
  219. {
  220. // FIXME: It's a poor design that filesystems are expected to call this before writing out data.
  221. // We should funnel everything through an interface at the VirtualFileSystem layer so this can happen from a single place.
  222. MutexLocker locker(m_inode_lock);
  223. if (fs().is_readonly())
  224. return EROFS;
  225. auto metadata = this->metadata();
  226. if (metadata.is_setuid() || metadata.is_setgid()) {
  227. dbgln("Inode::prepare_to_write_data(): Stripping SUID/SGID bits from {}", identifier());
  228. return chmod(metadata.mode & ~(04000 | 02000));
  229. }
  230. return KSuccess;
  231. }
  232. RefPtr<Memory::SharedInodeVMObject> Inode::shared_vmobject() const
  233. {
  234. MutexLocker locker(m_inode_lock);
  235. return m_shared_vmobject.strong_ref();
  236. }
  237. template<typename T>
  238. static inline bool range_overlap(T start1, T len1, T start2, T len2)
  239. {
  240. return ((start1 < start2 + len2) || len2 == 0) && ((start2 < start1 + len1) || len1 == 0);
  241. }
  242. static inline KResult normalize_flock(FileDescription const& description, flock& lock)
  243. {
  244. off_t start;
  245. switch (lock.l_whence) {
  246. case SEEK_SET:
  247. start = lock.l_start;
  248. break;
  249. case SEEK_CUR:
  250. start = description.offset() + lock.l_start;
  251. break;
  252. case SEEK_END:
  253. // FIXME: Implement SEEK_END and negative lengths.
  254. return ENOTSUP;
  255. default:
  256. return EINVAL;
  257. }
  258. lock = { lock.l_type, SEEK_SET, start, lock.l_len, 0 };
  259. return KSuccess;
  260. }
  261. KResult Inode::can_apply_flock(FileDescription const& description, flock const& new_lock) const
  262. {
  263. VERIFY(new_lock.l_whence == SEEK_SET);
  264. MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
  265. if (new_lock.l_type == F_UNLCK) {
  266. for (auto& lock : m_flocks) {
  267. if (&description == lock.owner && lock.start == new_lock.l_start && lock.len == new_lock.l_len)
  268. return KSuccess;
  269. }
  270. return EINVAL;
  271. }
  272. for (auto& lock : m_flocks) {
  273. if (!range_overlap(lock.start, lock.len, new_lock.l_start, new_lock.l_len))
  274. continue;
  275. if (new_lock.l_type == F_RDLCK && lock.type == F_WRLCK)
  276. return EAGAIN;
  277. if (new_lock.l_type == F_WRLCK)
  278. return EAGAIN;
  279. }
  280. return KSuccess;
  281. }
  282. KResult Inode::apply_flock(Process const& process, FileDescription const& description, Userspace<flock const*> input_lock)
  283. {
  284. flock new_lock;
  285. if (!copy_from_user(&new_lock, input_lock))
  286. return EFAULT;
  287. auto rc = normalize_flock(description, new_lock);
  288. if (rc.is_error())
  289. return rc;
  290. MutexLocker locker(m_inode_lock);
  291. rc = can_apply_flock(description, new_lock);
  292. if (rc.is_error())
  293. return rc;
  294. if (new_lock.l_type == F_UNLCK) {
  295. for (size_t i = 0; i < m_flocks.size(); ++i) {
  296. if (&description == m_flocks[i].owner && m_flocks[i].start == new_lock.l_start && m_flocks[i].len == new_lock.l_len) {
  297. m_flocks.remove(i);
  298. return KSuccess;
  299. }
  300. }
  301. return EINVAL;
  302. }
  303. m_flocks.append(Flock { new_lock.l_type, new_lock.l_start, new_lock.l_len, &description, process.pid().value() });
  304. return KSuccess;
  305. }
  306. KResult Inode::get_flock(FileDescription const& description, Userspace<flock*> reference_lock) const
  307. {
  308. flock lookup;
  309. if (!copy_from_user(&lookup, reference_lock))
  310. return EFAULT;
  311. auto rc = normalize_flock(description, lookup);
  312. if (rc.is_error())
  313. return rc;
  314. MutexLocker locker(m_inode_lock, Mutex::Mode::Shared);
  315. for (auto& lock : m_flocks) {
  316. if (!range_overlap(lock.start, lock.len, lookup.l_start, lookup.l_len))
  317. continue;
  318. if ((lookup.l_type == F_RDLCK && lock.type == F_WRLCK) || lookup.l_type == F_WRLCK) {
  319. lookup = { lock.type, SEEK_SET, lock.start, lock.len, lock.pid };
  320. if (!copy_to_user(reference_lock, &lookup))
  321. return EFAULT;
  322. return KSuccess;
  323. }
  324. }
  325. lookup.l_type = F_UNLCK;
  326. if (!copy_to_user(reference_lock, &lookup))
  327. return EFAULT;
  328. return KSuccess;
  329. }
  330. void Inode::remove_flocks_for_description(FileDescription const& description)
  331. {
  332. MutexLocker locker(m_inode_lock);
  333. for (size_t i = 0; i < m_flocks.size(); ++i) {
  334. if (&description == m_flocks[i].owner)
  335. m_flocks.remove(i--);
  336. }
  337. }
  338. }