InodeFile.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringView.h>
  27. #include <Kernel/FileSystem/FileDescription.h>
  28. #include <Kernel/FileSystem/Inode.h>
  29. #include <Kernel/FileSystem/InodeFile.h>
  30. #include <Kernel/FileSystem/VirtualFileSystem.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/VM/PrivateInodeVMObject.h>
  33. #include <Kernel/VM/SharedInodeVMObject.h>
  34. #include <LibC/errno_numbers.h>
  35. #include <LibC/sys/ioctl_numbers.h>
  36. namespace Kernel {
  37. InodeFile::InodeFile(NonnullRefPtr<Inode>&& inode)
  38. : m_inode(move(inode))
  39. {
  40. }
  41. InodeFile::~InodeFile()
  42. {
  43. }
  44. KResultOr<size_t> InodeFile::read(FileDescription& description, size_t offset, UserOrKernelBuffer& buffer, size_t count)
  45. {
  46. if (Checked<off_t>::addition_would_overflow(offset, count))
  47. return EOVERFLOW;
  48. ssize_t nread = m_inode->read_bytes(offset, count, buffer, &description);
  49. if (nread > 0) {
  50. Thread::current()->did_file_read(nread);
  51. evaluate_block_conditions();
  52. }
  53. if (nread < 0)
  54. return KResult((ErrnoCode)-nread);
  55. return nread;
  56. }
  57. KResultOr<size_t> InodeFile::write(FileDescription& description, size_t offset, const UserOrKernelBuffer& data, size_t count)
  58. {
  59. if (Checked<off_t>::addition_would_overflow(offset, count))
  60. return EOVERFLOW;
  61. ssize_t nwritten = m_inode->write_bytes(offset, count, data, &description);
  62. if (nwritten > 0) {
  63. m_inode->set_mtime(kgettimeofday().tv_sec);
  64. Thread::current()->did_file_write(nwritten);
  65. evaluate_block_conditions();
  66. }
  67. if (nwritten < 0)
  68. return KResult((ErrnoCode)-nwritten);
  69. return nwritten;
  70. }
  71. int InodeFile::ioctl(FileDescription& description, unsigned request, FlatPtr arg)
  72. {
  73. (void)description;
  74. switch (request) {
  75. case FIBMAP: {
  76. if (!Process::current()->is_superuser())
  77. return -EPERM;
  78. int block_number = 0;
  79. if (!copy_from_user(&block_number, (int*)arg))
  80. return -EFAULT;
  81. if (block_number < 0)
  82. return -EINVAL;
  83. auto block_address = inode().get_block_address(block_number);
  84. if (block_address.is_error())
  85. return block_address.error();
  86. if (!copy_to_user((int*)arg, &block_address.value()))
  87. return -EFAULT;
  88. return 0;
  89. }
  90. default:
  91. return -EINVAL;
  92. }
  93. }
  94. KResultOr<Region*> InodeFile::mmap(Process& process, FileDescription& description, const Range& range, size_t offset, int prot, bool shared)
  95. {
  96. // FIXME: If PROT_EXEC, check that the underlying file system isn't mounted noexec.
  97. RefPtr<InodeVMObject> vmobject;
  98. if (shared)
  99. vmobject = SharedInodeVMObject::create_with_inode(inode());
  100. else
  101. vmobject = PrivateInodeVMObject::create_with_inode(inode());
  102. if (!vmobject)
  103. return ENOMEM;
  104. return process.allocate_region_with_vmobject(range, vmobject.release_nonnull(), offset, description.absolute_path(), prot, shared);
  105. }
  106. String InodeFile::absolute_path(const FileDescription& description) const
  107. {
  108. ASSERT_NOT_REACHED();
  109. ASSERT(description.custody());
  110. return description.absolute_path();
  111. }
  112. KResult InodeFile::truncate(u64 size)
  113. {
  114. auto truncate_result = m_inode->truncate(size);
  115. if (truncate_result.is_error())
  116. return truncate_result;
  117. int mtime_result = m_inode->set_mtime(kgettimeofday().tv_sec);
  118. if (mtime_result < 0)
  119. return KResult((ErrnoCode)-mtime_result);
  120. return KSuccess;
  121. }
  122. KResult InodeFile::chown(FileDescription& description, uid_t uid, gid_t gid)
  123. {
  124. ASSERT(description.inode() == m_inode);
  125. ASSERT(description.custody());
  126. return VFS::the().chown(*description.custody(), uid, gid);
  127. }
  128. KResult InodeFile::chmod(FileDescription& description, mode_t mode)
  129. {
  130. ASSERT(description.inode() == m_inode);
  131. ASSERT(description.custody());
  132. return VFS::the().chmod(*description.custody(), mode);
  133. }
  134. }