2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2022-08-23 19:51:18 +03:00
|
|
|
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
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 {
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
InodeFile::InodeFile(NonnullLockRefPtr<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
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<size_t> InodeFile::write(OpenFileDescription& description, u64 offset, UserOrKernelBuffer const& 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;
|
|
|
|
|
2022-08-06 04:22:20 +03:00
|
|
|
size_t nwritten = TRY(m_inode->write_bytes(offset, count, data, &description));
|
2019-12-01 19:36:06 +03:00
|
|
|
if (nwritten > 0) {
|
2022-08-22 14:34:22 +03:00
|
|
|
auto mtime_result = m_inode->update_timestamps({}, {}, 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: {
|
2022-08-21 01:21:01 +03:00
|
|
|
auto current_process_credentials = Process::current().credentials();
|
|
|
|
if (!current_process_credentials->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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 19:51:18 +03:00
|
|
|
ErrorOr<NonnullLockRefPtr<Memory::VMObject>> InodeFile::vmobject_for_mmap(Process&, Memory::VirtualRange const&, u64&, bool shared)
|
2019-05-30 14:39:17 +03:00
|
|
|
{
|
2020-02-28 22:47:27 +03:00
|
|
|
if (shared)
|
2022-08-23 19:51:18 +03:00
|
|
|
return TRY(Memory::SharedInodeVMObject::try_create_with_inode(inode()));
|
|
|
|
return TRY(Memory::PrivateInodeVMObject::try_create_with_inode(inode()));
|
2019-05-30 14:39:17 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> InodeFile::pseudo_path(OpenFileDescription const&) 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));
|
2022-08-22 14:34:22 +03:00
|
|
|
TRY(m_inode->update_timestamps({}, {}, 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
|
|
|
}
|
|
|
|
|
2022-08-21 17:15:29 +03:00
|
|
|
ErrorOr<void> InodeFile::chown(Credentials const& credentials, 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());
|
2022-08-21 17:15:29 +03:00
|
|
|
return VirtualFileSystem::the().chown(credentials, *description.custody(), uid, gid);
|
2020-01-03 22:14:56 +03:00
|
|
|
}
|
|
|
|
|
2022-08-21 17:15:29 +03:00
|
|
|
ErrorOr<void> InodeFile::chmod(Credentials const& credentials, 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());
|
2022-08-21 17:15:29 +03:00
|
|
|
return VirtualFileSystem::the().chmod(credentials, *description.custody(), mode);
|
2020-01-03 22:14:56 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|