Kernel+LibC: Implement fsync

This commit is contained in:
TheFightingCatfish 2021-09-11 23:28:59 -04:00 committed by Andreas Kling
parent fd3735199b
commit a81b21c1a7
Notes: sideshowbarker 2024-07-18 04:12:18 +09:00
13 changed files with 51 additions and 6 deletions

View File

@ -79,6 +79,7 @@ enum class NeedsBigProcessLock {
S(fork, NeedsBigProcessLock::Yes) \
S(fstat, NeedsBigProcessLock::Yes) \
S(fstatvfs, NeedsBigProcessLock::Yes) \
S(fsync, NeedsBigProcessLock::Yes) \
S(ftruncate, NeedsBigProcessLock::Yes) \
S(futex, NeedsBigProcessLock::Yes) \
S(get_dir_entries, NeedsBigProcessLock::Yes) \

View File

@ -204,6 +204,7 @@ set(KERNEL_SOURCES
Syscalls/exit.cpp
Syscalls/fcntl.cpp
Syscalls/fork.cpp
Syscalls/fsync.cpp
Syscalls/ftruncate.cpp
Syscalls/futex.cpp
Syscalls/get_dir_entries.cpp

View File

@ -96,6 +96,7 @@ public:
virtual String absolute_path(const OpenFileDescription&) const = 0;
virtual KResult truncate(u64) { return EINVAL; }
virtual KResult sync() { return EINVAL; }
virtual KResult chown(OpenFileDescription&, UserID, GroupID) { return EBADF; }
virtual KResult chmod(OpenFileDescription&, mode_t) { return EBADF; }

View File

@ -51,7 +51,7 @@ FileSystem::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIde
void FileSystem::sync()
{
Inode::sync();
Inode::sync_all();
NonnullRefPtrVector<FileSystem, 32> file_systems;
{

View File

@ -29,7 +29,7 @@ SpinlockProtected<Inode::AllInstancesList>& Inode::all_instances()
return s_all_instances;
}
void Inode::sync()
void Inode::sync_all()
{
NonnullRefPtrVector<Inode, 32> inodes;
Inode::all_instances().with([&](auto& all_inodes) {
@ -45,6 +45,13 @@ void Inode::sync()
}
}
void Inode::sync()
{
if (is_metadata_dirty())
flush_metadata();
fs().flush_writes();
}
KResultOr<NonnullOwnPtr<KBuffer>> Inode::read_entire(OpenFileDescription* description) const
{
auto builder = TRY(KBufferBuilder::try_create());

View File

@ -87,7 +87,8 @@ public:
void set_shared_vmobject(Memory::SharedInodeVMObject&);
RefPtr<Memory::SharedInodeVMObject> shared_vmobject() const;
static void sync();
static void sync_all();
void sync();
bool has_watchers() const { return !m_watchers.is_empty(); }

View File

@ -107,6 +107,12 @@ KResult InodeFile::truncate(u64 size)
return KSuccess;
}
KResult InodeFile::sync()
{
m_inode->sync();
return KSuccess;
}
KResult InodeFile::chown(OpenFileDescription& description, UserID uid, GroupID gid)
{
VERIFY(description.inode() == m_inode);

View File

@ -39,6 +39,7 @@ public:
virtual String absolute_path(const OpenFileDescription&) const override;
virtual KResult truncate(u64) override;
virtual KResult sync() override;
virtual KResult chown(OpenFileDescription&, UserID, GroupID) override;
virtual KResult chmod(OpenFileDescription&, mode_t) override;

View File

@ -383,6 +383,12 @@ KResult OpenFileDescription::truncate(u64 length)
return m_file->truncate(length);
}
KResult OpenFileDescription::sync()
{
MutexLocker locker(m_lock);
return m_file->sync();
}
bool OpenFileDescription::is_fifo() const
{
return m_file->is_fifo();

View File

@ -122,6 +122,7 @@ public:
void set_original_custody(Badge<VirtualFileSystem>, Custody& custody);
KResult truncate(u64);
KResult sync();
off_t offset() const { return m_current_offset; }

View File

@ -363,6 +363,7 @@ public:
KResultOr<FlatPtr> sys$fchmod(int fd, mode_t);
KResultOr<FlatPtr> sys$chown(Userspace<const Syscall::SC_chown_params*>);
KResultOr<FlatPtr> sys$fchown(int fd, UserID, GroupID);
KResultOr<FlatPtr> sys$fsync(int fd);
KResultOr<FlatPtr> sys$socket(int domain, int type, int protocol);
KResultOr<FlatPtr> sys$bind(int sockfd, Userspace<const sockaddr*> addr, socklen_t);
KResultOr<FlatPtr> sys$listen(int sockfd, int backlog);

19
Kernel/Syscalls/fsync.cpp Normal file
View File

@ -0,0 +1,19 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Process.h>
namespace Kernel {
KResultOr<FlatPtr> Process::sys$fsync(int fd)
{
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
REQUIRE_PROMISE(stdio);
auto description = TRY(fds().open_file_description(fd));
return description->sync();
}
}

View File

@ -709,10 +709,10 @@ void sysbeep()
syscall(SC_beep);
}
int fsync([[maybe_unused]] int fd)
int fsync(int fd)
{
dbgln("FIXME: Implement fsync()");
return 0;
int rc = syscall(SC_fsync, fd);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
int halt()