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