2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2022-08-23 13:51:18 -03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-03-23 09:45:10 -03:00
|
|
|
#include <AK/StringView.h>
|
2023-01-07 17:52:06 -03:00
|
|
|
#include <Kernel/API/Ioctl.h>
|
2021-09-12 08:29:28 -03:00
|
|
|
#include <Kernel/API/POSIX/errno.h>
|
2019-06-07 06:43:58 -03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
|
#include <Kernel/FileSystem/InodeFile.h>
|
2021-09-07 08:39:11 -03:00
|
|
|
#include <Kernel/FileSystem/OpenFileDescription.h>
|
2019-05-30 08:39:17 -03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2021-08-06 05:45:34 -03:00
|
|
|
#include <Kernel/Memory/PrivateInodeVMObject.h>
|
|
|
|
|
#include <Kernel/Memory/SharedInodeVMObject.h>
|
2019-05-30 08:39:17 -03:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
namespace Kernel {
|
|
|
|
|
|
2023-03-07 08:25:00 -03:00
|
|
|
InodeFile::InodeFile(NonnullRefPtr<Inode> inode)
|
2019-05-30 08:39:17 -03:00
|
|
|
: m_inode(move(inode))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 16:15:15 -03:00
|
|
|
InodeFile::~InodeFile() = default;
|
2019-05-30 08:39:17 -03:00
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<size_t> InodeFile::read(OpenFileDescription& description, u64 offset, UserOrKernelBuffer& buffer, size_t count)
|
2019-05-30 08:39:17 -03:00
|
|
|
{
|
2021-02-03 06:51:37 -03:00
|
|
|
if (Checked<off_t>::addition_would_overflow(offset, count))
|
|
|
|
|
return EOVERFLOW;
|
|
|
|
|
|
2021-09-05 11:03:54 -03:00
|
|
|
auto nread = TRY(m_inode->read_bytes(offset, count, buffer, &description));
|
2020-11-29 20:05:27 -03:00
|
|
|
if (nread > 0) {
|
2020-06-28 18:34:31 -03:00
|
|
|
Thread::current()->did_file_read(nread);
|
2020-11-29 20:05:27 -03:00
|
|
|
evaluate_block_conditions();
|
|
|
|
|
}
|
2019-12-01 13:36:06 -03:00
|
|
|
return nread;
|
2019-05-30 08:39:17 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
ErrorOr<size_t> InodeFile::write(OpenFileDescription& description, u64 offset, UserOrKernelBuffer const& data, size_t count)
|
2019-05-30 08:39:17 -03:00
|
|
|
{
|
2021-02-03 06:51:37 -03:00
|
|
|
if (Checked<off_t>::addition_would_overflow(offset, count))
|
|
|
|
|
return EOVERFLOW;
|
|
|
|
|
|
2022-08-05 22:22:20 -03:00
|
|
|
size_t nwritten = TRY(m_inode->write_bytes(offset, count, data, &description));
|
2019-12-01 13:36:06 -03:00
|
|
|
if (nwritten > 0) {
|
2022-11-22 17:01:45 -03:00
|
|
|
auto mtime_result = m_inode->update_timestamps({}, {}, kgettimeofday());
|
2020-06-28 18:34:31 -03:00
|
|
|
Thread::current()->did_file_write(nwritten);
|
2020-11-29 20:05:27 -03:00
|
|
|
evaluate_block_conditions();
|
2021-04-30 10:51:06 -03:00
|
|
|
if (mtime_result.is_error())
|
2021-11-07 20:51:39 -03:00
|
|
|
return mtime_result.release_error();
|
2019-12-01 13:36:06 -03:00
|
|
|
}
|
|
|
|
|
return nwritten;
|
2019-05-30 08:39:17 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<void> InodeFile::ioctl(OpenFileDescription& description, unsigned request, Userspace<void*> arg)
|
2021-01-30 17:12:49 -03:00
|
|
|
{
|
|
|
|
|
switch (request) {
|
|
|
|
|
case FIBMAP: {
|
2022-08-20 19:21:01 -03:00
|
|
|
auto current_process_credentials = Process::current().credentials();
|
|
|
|
|
if (!current_process_credentials->is_superuser())
|
2021-07-26 07:47:25 -03:00
|
|
|
return EPERM;
|
2021-01-30 17:12:49 -03:00
|
|
|
|
2021-07-26 06:47:00 -03:00
|
|
|
auto user_block_number = static_ptr_cast<int*>(arg);
|
2021-01-30 17:12:49 -03:00
|
|
|
int block_number = 0;
|
2021-09-05 12:38:37 -03:00
|
|
|
TRY(copy_from_user(&block_number, user_block_number));
|
2021-01-30 17:12:49 -03:00
|
|
|
|
|
|
|
|
if (block_number < 0)
|
2021-07-26 07:47:25 -03:00
|
|
|
return EINVAL;
|
2021-01-30 17:12:49 -03:00
|
|
|
|
2021-09-05 11:03:54 -03:00
|
|
|
auto block_address = TRY(inode().get_block_address(block_number));
|
2021-09-05 12:38:37 -03:00
|
|
|
return copy_to_user(user_block_number, &block_address);
|
2021-01-30 17:12:49 -03:00
|
|
|
}
|
2021-07-27 02:06:22 -03:00
|
|
|
case FIONREAD: {
|
|
|
|
|
int remaining_bytes = inode().size() - description.offset();
|
2021-11-14 19:43:43 -03:00
|
|
|
return copy_to_user(static_ptr_cast<int*>(arg), &remaining_bytes);
|
2021-07-27 02:06:22 -03:00
|
|
|
}
|
2021-01-30 17:12:49 -03:00
|
|
|
default:
|
2021-07-26 07:47:25 -03:00
|
|
|
return EINVAL;
|
2021-01-30 17:12:49 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-06 15:05:48 -03:00
|
|
|
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> InodeFile::vmobject_for_mmap(Process&, Memory::VirtualRange const& range, u64& offset, bool shared)
|
2019-05-30 08:39:17 -03:00
|
|
|
{
|
2020-02-28 16:47:27 -03:00
|
|
|
if (shared)
|
2022-08-06 15:05:48 -03:00
|
|
|
return TRY(Memory::SharedInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size()));
|
|
|
|
|
return TRY(Memory::PrivateInodeVMObject::try_create_with_inode_and_range(inode(), offset, range.size()));
|
2019-05-30 08:39:17 -03:00
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:58:27 -03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> InodeFile::pseudo_path(OpenFileDescription const&) const
|
2019-05-30 08:39:17 -03:00
|
|
|
{
|
2021-10-29 19:45:23 -03:00
|
|
|
// 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.
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-05-30 08:39:17 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<void> InodeFile::truncate(u64 size)
|
2019-05-30 08:39:17 -03:00
|
|
|
{
|
2021-09-05 11:03:54 -03:00
|
|
|
TRY(m_inode->truncate(size));
|
2022-11-22 17:01:45 -03:00
|
|
|
TRY(m_inode->update_timestamps({}, {}, kgettimeofday()));
|
2021-11-07 20:51:39 -03:00
|
|
|
return {};
|
2019-05-30 08:39:17 -03:00
|
|
|
}
|
2020-01-03 16:14:56 -03:00
|
|
|
|
2021-11-07 20:51:39 -03:00
|
|
|
ErrorOr<void> InodeFile::sync()
|
2021-09-12 00:28:59 -03:00
|
|
|
{
|
|
|
|
|
m_inode->sync();
|
2021-11-07 20:51:39 -03:00
|
|
|
return {};
|
2021-09-12 00:28:59 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 11:15:29 -03:00
|
|
|
ErrorOr<void> InodeFile::chown(Credentials const& credentials, OpenFileDescription& description, UserID uid, GroupID gid)
|
2020-01-03 16:14:56 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(description.inode() == m_inode);
|
|
|
|
|
VERIFY(description.custody());
|
2022-08-21 11:15:29 -03:00
|
|
|
return VirtualFileSystem::the().chown(credentials, *description.custody(), uid, gid);
|
2020-01-03 16:14:56 -03:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 11:15:29 -03:00
|
|
|
ErrorOr<void> InodeFile::chmod(Credentials const& credentials, OpenFileDescription& description, mode_t mode)
|
2020-01-03 16:14:56 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(description.inode() == m_inode);
|
|
|
|
|
VERIFY(description.custody());
|
2022-08-21 11:15:29 -03:00
|
|
|
return VirtualFileSystem::the().chmod(credentials, *description.custody(), mode);
|
2020-01-03 16:14:56 -03:00
|
|
|
}
|
2020-02-15 21:27:42 -03:00
|
|
|
|
2022-11-27 19:50:28 -03:00
|
|
|
bool InodeFile::is_regular_file() const
|
|
|
|
|
{
|
|
|
|
|
return inode().metadata().is_regular_file();
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-15 21:27:42 -03:00
|
|
|
}
|