FileDescription.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/MemoryStream.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/Devices/BlockDevice.h>
  10. #include <Kernel/FileSystem/Custody.h>
  11. #include <Kernel/FileSystem/FIFO.h>
  12. #include <Kernel/FileSystem/FileDescription.h>
  13. #include <Kernel/FileSystem/FileSystem.h>
  14. #include <Kernel/FileSystem/InodeFile.h>
  15. #include <Kernel/FileSystem/InodeWatcher.h>
  16. #include <Kernel/Net/Socket.h>
  17. #include <Kernel/Process.h>
  18. #include <Kernel/TTY/MasterPTY.h>
  19. #include <Kernel/TTY/TTY.h>
  20. #include <Kernel/UnixTypes.h>
  21. #include <Kernel/VM/MemoryManager.h>
  22. #include <LibC/errno_numbers.h>
  23. namespace Kernel {
  24. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custody)
  25. {
  26. auto inode_file = InodeFile::create(custody.inode());
  27. if (inode_file.is_error())
  28. return inode_file.error();
  29. auto description = adopt_ref_if_nonnull(new FileDescription(*inode_file.release_value()));
  30. if (!description)
  31. return ENOMEM;
  32. description->m_custody = custody;
  33. auto result = description->attach();
  34. if (result.is_error()) {
  35. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for custody: {}", result);
  36. return result;
  37. }
  38. return description.release_nonnull();
  39. }
  40. KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(File& file)
  41. {
  42. auto description = adopt_ref_if_nonnull(new FileDescription(file));
  43. if (!description)
  44. return ENOMEM;
  45. auto result = description->attach();
  46. if (result.is_error()) {
  47. dbgln_if(FILEDESCRIPTION_DEBUG, "Failed to create file description for file: {}", result);
  48. return result;
  49. }
  50. return description.release_nonnull();
  51. }
  52. FileDescription::FileDescription(File& file)
  53. : m_file(file)
  54. {
  55. if (file.is_inode())
  56. m_inode = static_cast<InodeFile&>(file).inode();
  57. m_is_directory = metadata().is_directory();
  58. }
  59. FileDescription::~FileDescription()
  60. {
  61. m_file->detach(*this);
  62. if (is_fifo())
  63. static_cast<FIFO*>(m_file.ptr())->detach(m_fifo_direction);
  64. // FIXME: Should this error path be observed somehow?
  65. (void)m_file->close();
  66. if (m_inode)
  67. m_inode->detach(*this);
  68. }
  69. KResult FileDescription::attach()
  70. {
  71. if (m_inode) {
  72. auto result = m_inode->attach(*this);
  73. if (result.is_error())
  74. return result;
  75. }
  76. return m_file->attach(*this);
  77. }
  78. Thread::FileBlocker::BlockFlags FileDescription::should_unblock(Thread::FileBlocker::BlockFlags block_flags) const
  79. {
  80. using BlockFlags = Thread::FileBlocker::BlockFlags;
  81. BlockFlags unblock_flags = BlockFlags::None;
  82. if (has_flag(block_flags, BlockFlags::Read) && can_read())
  83. unblock_flags |= BlockFlags::Read;
  84. if (has_flag(block_flags, BlockFlags::Write) && can_write())
  85. unblock_flags |= BlockFlags::Write;
  86. // TODO: Implement Thread::FileBlocker::BlockFlags::Exception
  87. if (has_flag(block_flags, BlockFlags::SocketFlags)) {
  88. auto* sock = socket();
  89. VERIFY(sock);
  90. if (has_flag(block_flags, BlockFlags::Accept) && sock->can_accept())
  91. unblock_flags |= BlockFlags::Accept;
  92. if (has_flag(block_flags, BlockFlags::Connect) && sock->setup_state() == Socket::SetupState::Completed)
  93. unblock_flags |= BlockFlags::Connect;
  94. }
  95. return unblock_flags;
  96. }
  97. KResult FileDescription::stat(::stat& buffer)
  98. {
  99. Locker locker(m_lock);
  100. // FIXME: This is due to the Device class not overriding File::stat().
  101. if (m_inode)
  102. return m_inode->metadata().stat(buffer);
  103. return m_file->stat(buffer);
  104. }
  105. KResultOr<off_t> FileDescription::seek(off_t offset, int whence)
  106. {
  107. Locker locker(m_lock);
  108. if (!m_file->is_seekable())
  109. return ESPIPE;
  110. off_t new_offset;
  111. switch (whence) {
  112. case SEEK_SET:
  113. new_offset = offset;
  114. break;
  115. case SEEK_CUR:
  116. if (Checked<off_t>::addition_would_overflow(m_current_offset, offset))
  117. return EOVERFLOW;
  118. new_offset = m_current_offset + offset;
  119. break;
  120. case SEEK_END:
  121. if (!metadata().is_valid())
  122. return EIO;
  123. if (Checked<off_t>::addition_would_overflow(metadata().size, offset))
  124. return EOVERFLOW;
  125. new_offset = metadata().size + offset;
  126. break;
  127. default:
  128. return EINVAL;
  129. }
  130. if (new_offset < 0)
  131. return EINVAL;
  132. // FIXME: Return EINVAL if attempting to seek past the end of a seekable device.
  133. m_current_offset = new_offset;
  134. m_file->did_seek(*this, new_offset);
  135. if (m_inode)
  136. m_inode->did_seek(*this, new_offset);
  137. evaluate_block_conditions();
  138. return m_current_offset;
  139. }
  140. KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
  141. {
  142. Locker locker(m_lock);
  143. if (Checked<off_t>::addition_would_overflow(m_current_offset, count))
  144. return EOVERFLOW;
  145. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  146. if (!nread_or_error.is_error()) {
  147. if (m_file->is_seekable())
  148. m_current_offset += nread_or_error.value();
  149. evaluate_block_conditions();
  150. }
  151. return nread_or_error;
  152. }
  153. KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
  154. {
  155. Locker locker(m_lock);
  156. if (Checked<off_t>::addition_would_overflow(m_current_offset, size))
  157. return EOVERFLOW;
  158. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  159. if (!nwritten_or_error.is_error()) {
  160. if (m_file->is_seekable())
  161. m_current_offset += nwritten_or_error.value();
  162. evaluate_block_conditions();
  163. }
  164. return nwritten_or_error;
  165. }
  166. bool FileDescription::can_write() const
  167. {
  168. return m_file->can_write(*this, offset());
  169. }
  170. bool FileDescription::can_read() const
  171. {
  172. return m_file->can_read(*this, offset());
  173. }
  174. KResultOr<NonnullOwnPtr<KBuffer>> FileDescription::read_entire_file()
  175. {
  176. // HACK ALERT: (This entire function)
  177. VERIFY(m_file->is_inode());
  178. VERIFY(m_inode);
  179. return m_inode->read_entire(this);
  180. }
  181. KResultOr<size_t> FileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, size_t size)
  182. {
  183. Locker locker(m_lock, Lock::Mode::Shared);
  184. if (!is_directory())
  185. return ENOTDIR;
  186. auto metadata = this->metadata();
  187. if (!metadata.is_valid())
  188. return EIO;
  189. size_t remaining = size;
  190. KResult error = KSuccess;
  191. u8 stack_buffer[PAGE_SIZE];
  192. Bytes temp_buffer(stack_buffer, sizeof(stack_buffer));
  193. OutputMemoryStream stream { temp_buffer };
  194. auto flush_stream_to_output_buffer = [&error, &stream, &remaining, &output_buffer]() -> bool {
  195. if (error != 0)
  196. return false;
  197. if (stream.size() == 0)
  198. return true;
  199. if (remaining < stream.size()) {
  200. error = EINVAL;
  201. return false;
  202. } else if (!output_buffer.write(stream.bytes())) {
  203. error = EFAULT;
  204. return false;
  205. }
  206. output_buffer = output_buffer.offset(stream.size());
  207. remaining -= stream.size();
  208. stream.reset();
  209. return true;
  210. };
  211. KResult result = VFS::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &error, &remaining, &output_buffer, &stream, this](auto& entry) {
  212. size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length();
  213. if (serialized_size > stream.remaining()) {
  214. if (!flush_stream_to_output_buffer()) {
  215. return false;
  216. }
  217. }
  218. stream << (u32)entry.inode.index().value();
  219. stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
  220. stream << (u32)entry.name.length();
  221. stream << entry.name.bytes();
  222. return true;
  223. });
  224. flush_stream_to_output_buffer();
  225. if (result.is_error()) {
  226. // We should only return -EFAULT when the userspace buffer is too small,
  227. // so that userspace can reliably use it as a signal to increase its
  228. // buffer size.
  229. VERIFY(result != -EFAULT);
  230. return result;
  231. }
  232. if (error) {
  233. return error;
  234. }
  235. return size - remaining;
  236. }
  237. bool FileDescription::is_device() const
  238. {
  239. return m_file->is_device();
  240. }
  241. const Device* FileDescription::device() const
  242. {
  243. if (!is_device())
  244. return nullptr;
  245. return static_cast<const Device*>(m_file.ptr());
  246. }
  247. Device* FileDescription::device()
  248. {
  249. if (!is_device())
  250. return nullptr;
  251. return static_cast<Device*>(m_file.ptr());
  252. }
  253. bool FileDescription::is_tty() const
  254. {
  255. return m_file->is_tty();
  256. }
  257. const TTY* FileDescription::tty() const
  258. {
  259. if (!is_tty())
  260. return nullptr;
  261. return static_cast<const TTY*>(m_file.ptr());
  262. }
  263. TTY* FileDescription::tty()
  264. {
  265. if (!is_tty())
  266. return nullptr;
  267. return static_cast<TTY*>(m_file.ptr());
  268. }
  269. bool FileDescription::is_inode_watcher() const
  270. {
  271. return m_file->is_inode_watcher();
  272. }
  273. const InodeWatcher* FileDescription::inode_watcher() const
  274. {
  275. if (!is_inode_watcher())
  276. return nullptr;
  277. return static_cast<const InodeWatcher*>(m_file.ptr());
  278. }
  279. InodeWatcher* FileDescription::inode_watcher()
  280. {
  281. if (!is_inode_watcher())
  282. return nullptr;
  283. return static_cast<InodeWatcher*>(m_file.ptr());
  284. }
  285. bool FileDescription::is_master_pty() const
  286. {
  287. return m_file->is_master_pty();
  288. }
  289. const MasterPTY* FileDescription::master_pty() const
  290. {
  291. if (!is_master_pty())
  292. return nullptr;
  293. return static_cast<const MasterPTY*>(m_file.ptr());
  294. }
  295. MasterPTY* FileDescription::master_pty()
  296. {
  297. if (!is_master_pty())
  298. return nullptr;
  299. return static_cast<MasterPTY*>(m_file.ptr());
  300. }
  301. KResult FileDescription::close()
  302. {
  303. if (m_file->attach_count() > 0)
  304. return KSuccess;
  305. return m_file->close();
  306. }
  307. String FileDescription::absolute_path() const
  308. {
  309. if (m_custody)
  310. return m_custody->absolute_path();
  311. return m_file->absolute_path(*this);
  312. }
  313. InodeMetadata FileDescription::metadata() const
  314. {
  315. if (m_inode)
  316. return m_inode->metadata();
  317. return {};
  318. }
  319. KResultOr<Region*> FileDescription::mmap(Process& process, const Range& range, u64 offset, int prot, bool shared)
  320. {
  321. Locker locker(m_lock);
  322. return m_file->mmap(process, *this, range, offset, prot, shared);
  323. }
  324. KResult FileDescription::truncate(u64 length)
  325. {
  326. Locker locker(m_lock);
  327. return m_file->truncate(length);
  328. }
  329. bool FileDescription::is_fifo() const
  330. {
  331. return m_file->is_fifo();
  332. }
  333. FIFO* FileDescription::fifo()
  334. {
  335. if (!is_fifo())
  336. return nullptr;
  337. return static_cast<FIFO*>(m_file.ptr());
  338. }
  339. bool FileDescription::is_socket() const
  340. {
  341. return m_file->is_socket();
  342. }
  343. Socket* FileDescription::socket()
  344. {
  345. if (!is_socket())
  346. return nullptr;
  347. return static_cast<Socket*>(m_file.ptr());
  348. }
  349. const Socket* FileDescription::socket() const
  350. {
  351. if (!is_socket())
  352. return nullptr;
  353. return static_cast<const Socket*>(m_file.ptr());
  354. }
  355. void FileDescription::set_file_flags(u32 flags)
  356. {
  357. Locker locker(m_lock);
  358. m_is_blocking = !(flags & O_NONBLOCK);
  359. m_should_append = flags & O_APPEND;
  360. m_direct = flags & O_DIRECT;
  361. m_file_flags = flags;
  362. }
  363. KResult FileDescription::chmod(mode_t mode)
  364. {
  365. Locker locker(m_lock);
  366. return m_file->chmod(*this, mode);
  367. }
  368. KResult FileDescription::chown(uid_t uid, gid_t gid)
  369. {
  370. Locker locker(m_lock);
  371. return m_file->chown(*this, uid, gid);
  372. }
  373. FileBlockCondition& FileDescription::block_condition()
  374. {
  375. return m_file->block_condition();
  376. }
  377. }