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>
|
2019-06-07 10:36:51 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/Inode.h>
|
|
|
|
#include <Kernel/FileSystem/InodeFile.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/errno_numbers.h>
|
|
|
|
#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))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
InodeFile::~InodeFile()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:18:51 +03:00
|
|
|
KResultOr<size_t> InodeFile::read(FileDescription& 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-05-02 00:29:39 +03:00
|
|
|
auto result = m_inode->read_bytes(offset, count, buffer, &description);
|
|
|
|
if (result.is_error())
|
|
|
|
return result.error();
|
|
|
|
auto nread = result.value();
|
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-03-17 15:18:51 +03:00
|
|
|
KResultOr<size_t> InodeFile::write(FileDescription& 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-05-02 00:29:39 +03: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 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())
|
|
|
|
return mtime_result;
|
2019-12-01 19:36:06 +03:00
|
|
|
}
|
|
|
|
return nwritten;
|
2019-05-30 14:39:17 +03:00
|
|
|
}
|
|
|
|
|
2021-07-26 13:47:25 +03:00
|
|
|
KResult InodeFile::ioctl(FileDescription& description, unsigned request, Userspace<void*> arg)
|
2021-01-30 23:12:49 +03:00
|
|
|
{
|
|
|
|
(void)description;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case FIBMAP: {
|
|
|
|
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-07-26 12:47:00 +03:00
|
|
|
if (!copy_from_user(&block_number, user_block_number))
|
2021-07-26 13:47:25 +03:00
|
|
|
return EFAULT;
|
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
|
|
|
|
|
|
|
auto block_address = inode().get_block_address(block_number);
|
|
|
|
if (block_address.is_error())
|
|
|
|
return block_address.error();
|
|
|
|
|
2021-07-26 12:47:00 +03:00
|
|
|
if (!copy_to_user(user_block_number, &block_address.value()))
|
2021-07-26 13:47:25 +03:00
|
|
|
return EFAULT;
|
2021-01-30 23:12:49 +03:00
|
|
|
|
2021-07-26 13:47:25 +03:00
|
|
|
return KSuccess;
|
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-08-06 14:54:48 +03:00
|
|
|
KResultOr<Memory::Region*> InodeFile::mmap(Process& process, FileDescription& 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-08-06 14:49:36 +03:00
|
|
|
vmobject = Memory::SharedInodeVMObject::try_create_with_inode(inode());
|
2020-02-28 22:47:27 +03:00
|
|
|
else
|
2021-08-06 14:49:36 +03:00
|
|
|
vmobject = Memory::PrivateInodeVMObject::try_create_with_inode(inode());
|
2020-02-28 22:47:27 +03:00
|
|
|
if (!vmobject)
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOMEM;
|
2021-02-08 17:45:40 +03:00
|
|
|
return process.space().allocate_region_with_vmobject(range, vmobject.release_nonnull(), offset, description.absolute_path(), prot, shared);
|
2019-05-30 14:39:17 +03:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
String InodeFile::absolute_path(const FileDescription& description) const
|
2019-05-30 14:39:17 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
VERIFY(description.custody());
|
2019-06-13 23:03:04 +03:00
|
|
|
return description.absolute_path();
|
2019-05-30 14:39:17 +03:00
|
|
|
}
|
|
|
|
|
2020-02-08 14:07:04 +03:00
|
|
|
KResult InodeFile::truncate(u64 size)
|
2019-05-30 14:39:17 +03:00
|
|
|
{
|
2021-04-30 16:51:06 +03: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 15:56:49 +03:00
|
|
|
return KSuccess;
|
2019-05-30 14:39:17 +03:00
|
|
|
}
|
2020-01-03 22:14:56 +03:00
|
|
|
|
2020-05-28 17:32:20 +03:00
|
|
|
KResult InodeFile::chown(FileDescription& description, uid_t uid, gid_t 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
|
|
|
}
|
|
|
|
|
2020-05-28 17:32:20 +03:00
|
|
|
KResult InodeFile::chmod(FileDescription& 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
|
|
|
|
|
|
|
}
|