InodeFile.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <Kernel/API/POSIX/errno.h>
  8. #include <Kernel/FileSystem/Inode.h>
  9. #include <Kernel/FileSystem/InodeFile.h>
  10. #include <Kernel/FileSystem/OpenFileDescription.h>
  11. #include <Kernel/FileSystem/VirtualFileSystem.h>
  12. #include <Kernel/Memory/PrivateInodeVMObject.h>
  13. #include <Kernel/Memory/SharedInodeVMObject.h>
  14. #include <Kernel/Process.h>
  15. #include <LibC/sys/ioctl_numbers.h>
  16. namespace Kernel {
  17. InodeFile::InodeFile(NonnullLockRefPtr<Inode>&& inode)
  18. : m_inode(move(inode))
  19. {
  20. }
  21. InodeFile::~InodeFile() = default;
  22. ErrorOr<size_t> InodeFile::read(OpenFileDescription& description, u64 offset, UserOrKernelBuffer& buffer, size_t count)
  23. {
  24. if (Checked<off_t>::addition_would_overflow(offset, count))
  25. return EOVERFLOW;
  26. auto nread = TRY(m_inode->read_bytes(offset, count, buffer, &description));
  27. if (nread > 0) {
  28. Thread::current()->did_file_read(nread);
  29. evaluate_block_conditions();
  30. }
  31. return nread;
  32. }
  33. ErrorOr<size_t> InodeFile::write(OpenFileDescription& description, u64 offset, UserOrKernelBuffer const& data, size_t count)
  34. {
  35. if (Checked<off_t>::addition_would_overflow(offset, count))
  36. return EOVERFLOW;
  37. size_t nwritten = 0;
  38. {
  39. MutexLocker locker(m_inode->m_inode_lock);
  40. TRY(m_inode->prepare_to_write_data());
  41. nwritten = TRY(m_inode->write_bytes(offset, count, data, &description));
  42. }
  43. if (nwritten > 0) {
  44. auto mtime_result = m_inode->set_mtime(kgettimeofday().to_truncated_seconds());
  45. Thread::current()->did_file_write(nwritten);
  46. evaluate_block_conditions();
  47. if (mtime_result.is_error())
  48. return mtime_result.release_error();
  49. }
  50. return nwritten;
  51. }
  52. ErrorOr<void> InodeFile::ioctl(OpenFileDescription& description, unsigned request, Userspace<void*> arg)
  53. {
  54. switch (request) {
  55. case FIBMAP: {
  56. auto current_process_credentials = Process::current().credentials();
  57. if (!current_process_credentials->is_superuser())
  58. return EPERM;
  59. auto user_block_number = static_ptr_cast<int*>(arg);
  60. int block_number = 0;
  61. TRY(copy_from_user(&block_number, user_block_number));
  62. if (block_number < 0)
  63. return EINVAL;
  64. auto block_address = TRY(inode().get_block_address(block_number));
  65. return copy_to_user(user_block_number, &block_address);
  66. }
  67. case FIONREAD: {
  68. int remaining_bytes = inode().size() - description.offset();
  69. return copy_to_user(static_ptr_cast<int*>(arg), &remaining_bytes);
  70. }
  71. default:
  72. return EINVAL;
  73. }
  74. }
  75. ErrorOr<Memory::Region*> InodeFile::mmap(Process& process, OpenFileDescription& description, Memory::VirtualRange const& range, u64 offset, int prot, bool shared)
  76. {
  77. // FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
  78. LockRefPtr<Memory::InodeVMObject> vmobject;
  79. if (shared)
  80. vmobject = TRY(Memory::SharedInodeVMObject::try_create_with_inode(inode()));
  81. else
  82. vmobject = TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode()));
  83. auto path = TRY(description.pseudo_path());
  84. return process.address_space().allocate_region_with_vmobject(range, vmobject.release_nonnull(), offset, path->view(), prot, shared);
  85. }
  86. ErrorOr<NonnullOwnPtr<KString>> InodeFile::pseudo_path(OpenFileDescription const&) const
  87. {
  88. // If it has an inode, then it has a path, and therefore the caller should have been able to get a custody at some point.
  89. VERIFY_NOT_REACHED();
  90. }
  91. ErrorOr<void> InodeFile::truncate(u64 size)
  92. {
  93. TRY(m_inode->truncate(size));
  94. TRY(m_inode->set_mtime(kgettimeofday().to_truncated_seconds()));
  95. return {};
  96. }
  97. ErrorOr<void> InodeFile::sync()
  98. {
  99. m_inode->sync();
  100. return {};
  101. }
  102. ErrorOr<void> InodeFile::chown(Credentials const& credentials, OpenFileDescription& description, UserID uid, GroupID gid)
  103. {
  104. VERIFY(description.inode() == m_inode);
  105. VERIFY(description.custody());
  106. return VirtualFileSystem::the().chown(credentials, *description.custody(), uid, gid);
  107. }
  108. ErrorOr<void> InodeFile::chmod(Credentials const& credentials, OpenFileDescription& description, mode_t mode)
  109. {
  110. VERIFY(description.inode() == m_inode);
  111. VERIFY(description.custody());
  112. return VirtualFileSystem::the().chmod(credentials, *description.custody(), mode);
  113. }
  114. }