FileDescription.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 (nothrow) 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 (nothrow) 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_any_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, u64 offset, size_t count)
  141. {
  142. if (Checked<u64>::addition_would_overflow(offset, count))
  143. return EOVERFLOW;
  144. return m_file->read(*this, offset, buffer, count);
  145. }
  146. KResultOr<size_t> FileDescription::write(u64 offset, UserOrKernelBuffer const& data, size_t data_size)
  147. {
  148. if (Checked<u64>::addition_would_overflow(offset, data_size))
  149. return EOVERFLOW;
  150. return m_file->write(*this, offset, data, data_size);
  151. }
  152. KResultOr<size_t> FileDescription::read(UserOrKernelBuffer& buffer, size_t count)
  153. {
  154. Locker locker(m_lock);
  155. if (Checked<off_t>::addition_would_overflow(m_current_offset, count))
  156. return EOVERFLOW;
  157. auto nread_or_error = m_file->read(*this, offset(), buffer, count);
  158. if (!nread_or_error.is_error()) {
  159. if (m_file->is_seekable())
  160. m_current_offset += nread_or_error.value();
  161. evaluate_block_conditions();
  162. }
  163. return nread_or_error;
  164. }
  165. KResultOr<size_t> FileDescription::write(const UserOrKernelBuffer& data, size_t size)
  166. {
  167. Locker locker(m_lock);
  168. if (Checked<off_t>::addition_would_overflow(m_current_offset, size))
  169. return EOVERFLOW;
  170. auto nwritten_or_error = m_file->write(*this, offset(), data, size);
  171. if (!nwritten_or_error.is_error()) {
  172. if (m_file->is_seekable())
  173. m_current_offset += nwritten_or_error.value();
  174. evaluate_block_conditions();
  175. }
  176. return nwritten_or_error;
  177. }
  178. bool FileDescription::can_write() const
  179. {
  180. return m_file->can_write(*this, offset());
  181. }
  182. bool FileDescription::can_read() const
  183. {
  184. return m_file->can_read(*this, offset());
  185. }
  186. KResultOr<NonnullOwnPtr<KBuffer>> FileDescription::read_entire_file()
  187. {
  188. // HACK ALERT: (This entire function)
  189. VERIFY(m_file->is_inode());
  190. VERIFY(m_inode);
  191. return m_inode->read_entire(this);
  192. }
  193. KResultOr<size_t> FileDescription::get_dir_entries(UserOrKernelBuffer& output_buffer, size_t size)
  194. {
  195. Locker locker(m_lock, Mutex::Mode::Shared);
  196. if (!is_directory())
  197. return ENOTDIR;
  198. auto metadata = this->metadata();
  199. if (!metadata.is_valid())
  200. return EIO;
  201. size_t remaining = size;
  202. KResult error = KSuccess;
  203. u8 stack_buffer[PAGE_SIZE];
  204. Bytes temp_buffer(stack_buffer, sizeof(stack_buffer));
  205. OutputMemoryStream stream { temp_buffer };
  206. auto flush_stream_to_output_buffer = [&error, &stream, &remaining, &output_buffer]() -> bool {
  207. if (error != 0)
  208. return false;
  209. if (stream.size() == 0)
  210. return true;
  211. if (remaining < stream.size()) {
  212. error = EINVAL;
  213. return false;
  214. } else if (!output_buffer.write(stream.bytes())) {
  215. error = EFAULT;
  216. return false;
  217. }
  218. output_buffer = output_buffer.offset(stream.size());
  219. remaining -= stream.size();
  220. stream.reset();
  221. return true;
  222. };
  223. KResult result = VirtualFileSystem::the().traverse_directory_inode(*m_inode, [&flush_stream_to_output_buffer, &stream, this](auto& entry) {
  224. size_t serialized_size = sizeof(ino_t) + sizeof(u8) + sizeof(size_t) + sizeof(char) * entry.name.length();
  225. if (serialized_size > stream.remaining()) {
  226. if (!flush_stream_to_output_buffer()) {
  227. return false;
  228. }
  229. }
  230. stream << (u32)entry.inode.index().value();
  231. stream << m_inode->fs().internal_file_type_to_directory_entry_type(entry);
  232. stream << (u32)entry.name.length();
  233. stream << entry.name.bytes();
  234. return true;
  235. });
  236. flush_stream_to_output_buffer();
  237. if (result.is_error()) {
  238. // We should only return -EFAULT when the userspace buffer is too small,
  239. // so that userspace can reliably use it as a signal to increase its
  240. // buffer size.
  241. VERIFY(result != -EFAULT);
  242. return result;
  243. }
  244. if (error) {
  245. return error;
  246. }
  247. return size - remaining;
  248. }
  249. bool FileDescription::is_device() const
  250. {
  251. return m_file->is_device();
  252. }
  253. const Device* FileDescription::device() const
  254. {
  255. if (!is_device())
  256. return nullptr;
  257. return static_cast<const Device*>(m_file.ptr());
  258. }
  259. Device* FileDescription::device()
  260. {
  261. if (!is_device())
  262. return nullptr;
  263. return static_cast<Device*>(m_file.ptr());
  264. }
  265. bool FileDescription::is_tty() const
  266. {
  267. return m_file->is_tty();
  268. }
  269. const TTY* FileDescription::tty() const
  270. {
  271. if (!is_tty())
  272. return nullptr;
  273. return static_cast<const TTY*>(m_file.ptr());
  274. }
  275. TTY* FileDescription::tty()
  276. {
  277. if (!is_tty())
  278. return nullptr;
  279. return static_cast<TTY*>(m_file.ptr());
  280. }
  281. bool FileDescription::is_inode_watcher() const
  282. {
  283. return m_file->is_inode_watcher();
  284. }
  285. const InodeWatcher* FileDescription::inode_watcher() const
  286. {
  287. if (!is_inode_watcher())
  288. return nullptr;
  289. return static_cast<const InodeWatcher*>(m_file.ptr());
  290. }
  291. InodeWatcher* FileDescription::inode_watcher()
  292. {
  293. if (!is_inode_watcher())
  294. return nullptr;
  295. return static_cast<InodeWatcher*>(m_file.ptr());
  296. }
  297. bool FileDescription::is_master_pty() const
  298. {
  299. return m_file->is_master_pty();
  300. }
  301. const MasterPTY* FileDescription::master_pty() const
  302. {
  303. if (!is_master_pty())
  304. return nullptr;
  305. return static_cast<const MasterPTY*>(m_file.ptr());
  306. }
  307. MasterPTY* FileDescription::master_pty()
  308. {
  309. if (!is_master_pty())
  310. return nullptr;
  311. return static_cast<MasterPTY*>(m_file.ptr());
  312. }
  313. KResult FileDescription::close()
  314. {
  315. if (m_file->attach_count() > 0)
  316. return KSuccess;
  317. return m_file->close();
  318. }
  319. String FileDescription::absolute_path() const
  320. {
  321. if (m_custody)
  322. return m_custody->absolute_path();
  323. return m_file->absolute_path(*this);
  324. }
  325. InodeMetadata FileDescription::metadata() const
  326. {
  327. if (m_inode)
  328. return m_inode->metadata();
  329. return {};
  330. }
  331. KResultOr<Region*> FileDescription::mmap(Process& process, const Range& range, u64 offset, int prot, bool shared)
  332. {
  333. Locker locker(m_lock);
  334. return m_file->mmap(process, *this, range, offset, prot, shared);
  335. }
  336. KResult FileDescription::truncate(u64 length)
  337. {
  338. Locker locker(m_lock);
  339. return m_file->truncate(length);
  340. }
  341. bool FileDescription::is_fifo() const
  342. {
  343. return m_file->is_fifo();
  344. }
  345. FIFO* FileDescription::fifo()
  346. {
  347. if (!is_fifo())
  348. return nullptr;
  349. return static_cast<FIFO*>(m_file.ptr());
  350. }
  351. bool FileDescription::is_socket() const
  352. {
  353. return m_file->is_socket();
  354. }
  355. Socket* FileDescription::socket()
  356. {
  357. if (!is_socket())
  358. return nullptr;
  359. return static_cast<Socket*>(m_file.ptr());
  360. }
  361. const Socket* FileDescription::socket() const
  362. {
  363. if (!is_socket())
  364. return nullptr;
  365. return static_cast<const Socket*>(m_file.ptr());
  366. }
  367. void FileDescription::set_file_flags(u32 flags)
  368. {
  369. Locker locker(m_lock);
  370. m_is_blocking = !(flags & O_NONBLOCK);
  371. m_should_append = flags & O_APPEND;
  372. m_direct = flags & O_DIRECT;
  373. m_file_flags = flags;
  374. }
  375. KResult FileDescription::chmod(mode_t mode)
  376. {
  377. Locker locker(m_lock);
  378. return m_file->chmod(*this, mode);
  379. }
  380. KResult FileDescription::chown(uid_t uid, gid_t gid)
  381. {
  382. Locker locker(m_lock);
  383. return m_file->chown(*this, uid, gid);
  384. }
  385. FileBlockCondition& FileDescription::block_condition()
  386. {
  387. return m_file->block_condition();
  388. }
  389. }