public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] add fsync parameter to replace_file and atomic_open_or_create
Date: Wed, 20 Oct 2021 15:00:43 +0200	[thread overview]
Message-ID: <20211020130045.2020043-1-dietmar@proxmox.com> (raw)

The fsync is required for consistency after power failure, so it should
be set when writing config files or otherwise important data.
---
 proxmox/src/tools/fs.rs | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/proxmox/src/tools/fs.rs b/proxmox/src/tools/fs.rs
index 19e549d..29233dd 100644
--- a/proxmox/src/tools/fs.rs
+++ b/proxmox/src/tools/fs.rs
@@ -163,10 +163,15 @@ pub fn make_tmp_file<P: AsRef<Path>>(
 /// Atomically replace a file.
 ///
 /// This first creates a temporary file and then rotates it in place.
+///
+/// `fsync`: use `fsync(2)` sycall to synchronize a file's in-core
+/// state with storage device. This makes sure the is consistent even
+/// aftert a power loss.
 pub fn replace_file<P: AsRef<Path>>(
     path: P,
     data: &[u8],
     options: CreateOptions,
+    fsync: bool,
 ) -> Result<(), Error> {
     let (fd, tmp_path) = make_tmp_file(&path, options)?;
 
@@ -177,6 +182,11 @@ pub fn replace_file<P: AsRef<Path>>(
         bail!("write failed: {}", err);
     }
 
+    if fsync {
+        // make sure data is on disk
+        nix::unistd::fsync(file.as_raw_fd())?;
+    }
+
     if let Err(err) = std::fs::rename(&tmp_path, &path) {
         let _ = unistd::unlink(&tmp_path);
         bail!(
@@ -194,11 +204,16 @@ pub fn replace_file<P: AsRef<Path>>(
 /// Since we need to initialize the file, we also need a solid slow
 /// path where we create the file. In order to avoid races, we create
 /// it in a temporary location and rotate it in place.
+///
+/// `fsync`: use `fsync(2)` sycall to synchronize the `initial_data`
+/// to the storage device. This options has no effect it the `initial_data`
+/// is empty or the file already exists.
 pub fn atomic_open_or_create_file<P: AsRef<Path>>(
     path: P,
     mut oflag: OFlag,
     initial_data: &[u8],
     options: CreateOptions,
+    fsync: bool,
 ) -> Result<File, Error> {
     let path = path.as_ref();
 
@@ -244,6 +259,10 @@ pub fn atomic_open_or_create_file<P: AsRef<Path>>(
                 err,
             )
         })?;
+        if fsync {
+            // make sure the initial_data is on disk
+            nix::unistd::fsync(file.as_raw_fd())?;
+        }
     }
 
     // rotate the file into place, but use `RENAME_NOREPLACE`, so in case 2 processes race against
@@ -623,6 +642,7 @@ pub fn open_file_locked<P: AsRef<Path>>(
         OFlag::O_RDWR | OFlag::O_CLOEXEC | OFlag::O_APPEND,
         &[],
         options,
+        false,
     )?;
 
     match lock_file(&mut file, exclusive, Some(timeout)) {
-- 
2.30.2





             reply	other threads:[~2021-10-20 13:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-20 13:00 Dietmar Maurer [this message]
2021-10-20 13:00 ` [pbs-devel] [PATCH proxmox-opendid] add fsync parameter to replace_file Dietmar Maurer
2021-10-20 13:00 ` [pbs-devel] [PATCH proxmox-backup] use new fsync parameter to replace_file and atomic_open_or_create Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211020130045.2020043-1-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal