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