From 0556a4b72c6a3617daf174d83d5009bd4b6bf3a0 Mon Sep 17 00:00:00 2001 From: John Spray Date: Tue, 2 May 2023 11:21:02 +0100 Subject: [PATCH] file: wrap syncfs syscall It is prudent to syncfs on startup if relying on all writes from previous instances of one's program to be persistent. --- include/seastar/core/file.hh | 5 +++++ include/seastar/core/reactor.hh | 1 + src/core/file-impl.hh | 1 + src/core/file.cc | 13 +++++++++++++ src/core/reactor.cc | 10 ++++++++++ 5 files changed, 30 insertions(+) diff --git a/include/seastar/core/file.hh b/include/seastar/core/file.hh index 69b976f6cb4..b860af50767 100644 --- a/include/seastar/core/file.hh +++ b/include/seastar/core/file.hh @@ -135,6 +135,7 @@ public: } virtual future<> flush(void) = 0; + virtual future<> syncfs(void) = 0; virtual future stat(void) = 0; virtual future<> truncate(uint64_t length) = 0; virtual future<> discard(uint64_t offset, uint64_t length) = 0; @@ -374,6 +375,10 @@ public: /// a flush, data is guaranteed to be on disk. future<> flush() noexcept; + /// Causes any dirty data on any file on the same filesystem as this file to + /// be flushed to disk. + future<> syncfs() noexcept; + /// Returns \c stat information about the file. future stat() noexcept; diff --git a/include/seastar/core/reactor.hh b/include/seastar/core/reactor.hh index a076f411f96..d41f764252a 100644 --- a/include/seastar/core/reactor.hh +++ b/include/seastar/core/reactor.hh @@ -685,6 +685,7 @@ private: future fstat(int fd) noexcept; future fstatfs(int fd) noexcept; + future<> syncfs(int fd) noexcept; friend future> make_file_impl(int fd, file_open_options options, int flags) noexcept; public: future<> readable(pollable_fd_state& fd); diff --git a/src/core/file-impl.hh b/src/core/file-impl.hh index 30b5932af94..ffbe5da5028 100644 --- a/src/core/file-impl.hh +++ b/src/core/file-impl.hh @@ -90,6 +90,7 @@ protected: public: virtual ~posix_file_impl() override; future<> flush(void) noexcept override; + future<> syncfs(void) noexcept override; future stat(void) noexcept override; future<> truncate(uint64_t length) noexcept override; future<> discard(uint64_t offset, uint64_t length) noexcept override; diff --git a/src/core/file.cc b/src/core/file.cc index c23b77f3eb8..a23ce311369 100644 --- a/src/core/file.cc +++ b/src/core/file.cc @@ -185,6 +185,11 @@ posix_file_impl::flush(void) noexcept { return engine().fdatasync(_fd); } +future<> +posix_file_impl::syncfs(void) noexcept { + return engine().syncfs(_fd); +} + future posix_file_impl::stat() noexcept { return engine().fstat(_fd); @@ -1220,6 +1225,14 @@ future<> file::flush() noexcept { } } +future<> file::syncfs() noexcept { + try { + return _file_impl->syncfs(); + } catch (...) { + return current_exception_as_future(); + } +} + future file::dma_write(uint64_t pos, std::vector iov, const io_priority_class& pc, io_intent* intent) noexcept { try { return _file_impl->write_dma(pos, std::move(iov), pc, intent); diff --git a/src/core/reactor.cc b/src/core/reactor.cc index ae952b1ad79..e333aeed778 100644 --- a/src/core/reactor.cc +++ b/src/core/reactor.cc @@ -2225,6 +2225,16 @@ reactor::fstatfs(int fd) noexcept { }); } +future<> +reactor::syncfs(int fd) noexcept { + return _thread_pool->submit>([fd] { + auto ret = ::syncfs(fd); + return wrap_syscall(ret); + }).then([] (syscall_result sr) { + sr.throw_if_error(); + }); +} + future reactor::statvfs(std::string_view pathname) noexcept { // Allocating memory for a sstring can throw, hence the futurize_invoke