diff --git a/Kernel/API/Syscall.h b/Kernel/API/Syscall.h index 90b9d85c1e8..d590ab5125c 100644 --- a/Kernel/API/Syscall.h +++ b/Kernel/API/Syscall.h @@ -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) \ diff --git a/Kernel/CMakeLists.txt b/Kernel/CMakeLists.txt index ea2487ebc1a..bce235026f8 100644 --- a/Kernel/CMakeLists.txt +++ b/Kernel/CMakeLists.txt @@ -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 diff --git a/Kernel/FileSystem/File.h b/Kernel/FileSystem/File.h index 27405160000..303549fdc3f 100644 --- a/Kernel/FileSystem/File.h +++ b/Kernel/FileSystem/File.h @@ -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; } diff --git a/Kernel/FileSystem/FileSystem.cpp b/Kernel/FileSystem/FileSystem.cpp index 75247ef6478..b98840fd0a8 100644 --- a/Kernel/FileSystem/FileSystem.cpp +++ b/Kernel/FileSystem/FileSystem.cpp @@ -51,7 +51,7 @@ FileSystem::DirectoryEntryView::DirectoryEntryView(const StringView& n, InodeIde void FileSystem::sync() { - Inode::sync(); + Inode::sync_all(); NonnullRefPtrVector file_systems; { diff --git a/Kernel/FileSystem/Inode.cpp b/Kernel/FileSystem/Inode.cpp index 1261c0ef689..4dd78267bb7 100644 --- a/Kernel/FileSystem/Inode.cpp +++ b/Kernel/FileSystem/Inode.cpp @@ -29,7 +29,7 @@ SpinlockProtected& Inode::all_instances() return s_all_instances; } -void Inode::sync() +void Inode::sync_all() { NonnullRefPtrVector 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> Inode::read_entire(OpenFileDescription* description) const { auto builder = TRY(KBufferBuilder::try_create()); diff --git a/Kernel/FileSystem/Inode.h b/Kernel/FileSystem/Inode.h index d0e0b927b40..c078d836082 100644 --- a/Kernel/FileSystem/Inode.h +++ b/Kernel/FileSystem/Inode.h @@ -87,7 +87,8 @@ public: void set_shared_vmobject(Memory::SharedInodeVMObject&); RefPtr shared_vmobject() const; - static void sync(); + static void sync_all(); + void sync(); bool has_watchers() const { return !m_watchers.is_empty(); } diff --git a/Kernel/FileSystem/InodeFile.cpp b/Kernel/FileSystem/InodeFile.cpp index 499dee0b7fd..f037504f034 100644 --- a/Kernel/FileSystem/InodeFile.cpp +++ b/Kernel/FileSystem/InodeFile.cpp @@ -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); diff --git a/Kernel/FileSystem/InodeFile.h b/Kernel/FileSystem/InodeFile.h index 8e7dd7cd8d1..88588b28e5e 100644 --- a/Kernel/FileSystem/InodeFile.h +++ b/Kernel/FileSystem/InodeFile.h @@ -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; diff --git a/Kernel/FileSystem/OpenFileDescription.cpp b/Kernel/FileSystem/OpenFileDescription.cpp index 955b429fa56..eb90c3450b9 100644 --- a/Kernel/FileSystem/OpenFileDescription.cpp +++ b/Kernel/FileSystem/OpenFileDescription.cpp @@ -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(); diff --git a/Kernel/FileSystem/OpenFileDescription.h b/Kernel/FileSystem/OpenFileDescription.h index 2c3f1b1cfab..67413d4e744 100644 --- a/Kernel/FileSystem/OpenFileDescription.h +++ b/Kernel/FileSystem/OpenFileDescription.h @@ -122,6 +122,7 @@ public: void set_original_custody(Badge, Custody& custody); KResult truncate(u64); + KResult sync(); off_t offset() const { return m_current_offset; } diff --git a/Kernel/Process.h b/Kernel/Process.h index 180a1d07d1a..9c0ab672da7 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -363,6 +363,7 @@ public: KResultOr sys$fchmod(int fd, mode_t); KResultOr sys$chown(Userspace); KResultOr sys$fchown(int fd, UserID, GroupID); + KResultOr sys$fsync(int fd); KResultOr sys$socket(int domain, int type, int protocol); KResultOr sys$bind(int sockfd, Userspace addr, socklen_t); KResultOr sys$listen(int sockfd, int backlog); diff --git a/Kernel/Syscalls/fsync.cpp b/Kernel/Syscalls/fsync.cpp new file mode 100644 index 00000000000..3e1445de777 --- /dev/null +++ b/Kernel/Syscalls/fsync.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2021, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace Kernel { + +KResultOr 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(); +} + +} diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index b4f7c76e363..7f7aa566c4c 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -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()