[libc-commits] [libc] [libc] Implement fdatasync (PR #225613)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Wed Sep 23 00:16:09 PDT 2026
https://github.com/labath created https://github.com/llvm/llvm-project/pull/225613
This patch implements the fdatasync entry point for Linux, matching the existing fsync implementation. I'm adding it to all supported Linux architectures (x86_64, aarch64, arm, and riscv), where it's backed by the SYS_fdatasync syscall.
Assisted-by: Gemini
>From 27076c96d24d3ce2d4fcae5a99f0f905193113af Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Tue, 22 Sep 2026 15:14:23 +0000
Subject: [PATCH] [libc] Implement fdatasync
This patch implements the fdatasync entry point for Linux, matching the
existing fsync implementation. I'm adding it to all supported Linux
architectures (x86_64, aarch64, arm, and riscv), where it's backed by
the SYS_fdatasync syscall.
Assisted-by: Gemini
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/include/unistd.yaml | 6 +++
.../linux/syscall_wrappers/CMakeLists.txt | 12 +++++
.../OSUtil/linux/syscall_wrappers/fdatasync.h | 33 +++++++++++++
libc/src/unistd/CMakeLists.txt | 7 +++
libc/src/unistd/fdatasync.h | 25 ++++++++++
libc/src/unistd/linux/CMakeLists.txt | 14 ++++++
libc/src/unistd/linux/fdatasync.cpp | 32 ++++++++++++
libc/test/src/unistd/CMakeLists.txt | 19 +++++++
libc/test/src/unistd/fdatasync_test.cpp | 49 +++++++++++++++++++
13 files changed, 201 insertions(+)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h
create mode 100644 libc/src/unistd/fdatasync.h
create mode 100644 libc/src/unistd/linux/fdatasync.cpp
create mode 100644 libc/test/src/unistd/fdatasync_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index c6e8dc4e69fb49..4e5025e22ac2bb 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -435,6 +435,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 1602255c6559cc..53ad6c42b65675 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -249,6 +249,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fsync
libc.src.unistd.ftruncate
libc.src.unistd.getcwd
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index c56fbf7974adec..f5367add50a6d4 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -460,6 +460,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index b7c05bc6bc6c52..0274770d049d4b 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -474,6 +474,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.faccessat
libc.src.unistd.fchdir
libc.src.unistd.fchown
+ libc.src.unistd.fdatasync
libc.src.unistd.fpathconf
libc.src.unistd.fsync
libc.src.unistd.ftruncate
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 890a4ae21f1919..50863aef0e2ba5 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -250,6 +250,12 @@ functions:
- type: int
- type: uid_t
- type: gid_t
+ - name: fdatasync
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
- name: fpathconf
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f5f28f51f49f60..316caccf4306ae 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -928,6 +928,18 @@ add_header_library(
libc.include.sys_syscall
)
+add_header_library(
+ fdatasync
+ HDRS
+ fdatasync.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.include.sys_syscall
+)
+
add_header_library(
fsync
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h
new file mode 100644
index 00000000000000..f59edfc1a239b0
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
+
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> fdatasync(int fd) {
+ return syscall_checked<int>(SYS_fdatasync, fd);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FDATASYNC_H
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 97bae9a643f345..26c9d13680c199 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -119,6 +119,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.fchown
)
+add_entrypoint_object(
+ fdatasync
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.fdatasync
+)
+
add_entrypoint_object(
fork
ALIAS
diff --git a/libc/src/unistd/fdatasync.h b/libc/src/unistd/fdatasync.h
new file mode 100644
index 00000000000000..8dc456cd49f1fc
--- /dev/null
+++ b/libc/src/unistd/fdatasync.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
+#define LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int fdatasync(int fd);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_FDATASYNC_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index d4e03819612b92..3c48ab56177eb8 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -140,6 +140,20 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ fdatasync
+ SRCS
+ fdatasync.cpp
+ HDRS
+ ../fdatasync.h
+ DEPENDS
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.__support.OSUtil.linux.syscall_wrappers.fdatasync
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
fork
SRCS
diff --git a/libc/src/unistd/linux/fdatasync.cpp b/libc/src/unistd/linux/fdatasync.cpp
new file mode 100644
index 00000000000000..8001c87d2ac17d
--- /dev/null
+++ b/libc/src/unistd/linux/fdatasync.cpp
@@ -0,0 +1,32 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/fdatasync.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/fdatasync.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, fdatasync, (int fd)) {
+ ErrorOr<int> ret = linux_syscalls::fdatasync(fd);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 17960278bd3f75..74eb93d8635c30 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -204,6 +204,25 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ fdatasync_test
+ SUITE
+ libc_unistd_unittests
+ SRCS
+ fdatasync_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.fcntl_macros
+ libc.hdr.sys_stat_macros
+ libc.src.fcntl.open
+ libc.src.unistd.close
+ libc.src.unistd.fdatasync
+ libc.src.unistd.unlink
+ libc.src.unistd.write
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
ftruncate_test
SUITE
diff --git a/libc/test/src/unistd/fdatasync_test.cpp b/libc/test/src/unistd/fdatasync_test.cpp
new file mode 100644
index 00000000000000..2b336286191965
--- /dev/null
+++ b/libc/test/src/unistd/fdatasync_test.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for fdatasync.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/fcntl/open.h"
+#include "src/unistd/close.h"
+#include "src/unistd/fdatasync.h"
+#include "src/unistd/unlink.h"
+#include "src/unistd/write.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcFdatasyncTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcFdatasyncTest, BasicSync) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+ constexpr const char *FILENAME = "fdatasync.test";
+ auto test_file = libc_make_test_file_path(FILENAME);
+ constexpr const char WRITE_DATA[] = "hello, fdatasync";
+ constexpr ssize_t WRITE_SIZE = sizeof(WRITE_DATA);
+
+ int fd = LIBC_NAMESPACE::open(test_file, O_WRONLY | O_CREAT, S_IRWXU);
+ ASSERT_ERRNO_SUCCESS();
+ ASSERT_GT(fd, 0);
+
+ ASSERT_THAT(LIBC_NAMESPACE::write(fd, WRITE_DATA, WRITE_SIZE),
+ Succeeds(WRITE_SIZE));
+ ASSERT_THAT(LIBC_NAMESPACE::fdatasync(fd), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::close(fd), Succeeds(0));
+ ASSERT_THAT(LIBC_NAMESPACE::unlink(test_file), Succeeds(0));
+}
+
+TEST_F(LlvmLibcFdatasyncTest, BadFd) {
+ using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+ ASSERT_THAT(LIBC_NAMESPACE::fdatasync(-1), Fails(EBADF));
+}
More information about the libc-commits
mailing list