From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 2850076BFE for ; Wed, 20 Oct 2021 15:01:19 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1FC74123A2 for ; Wed, 20 Oct 2021 15:00:49 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id E46DE12394 for ; Wed, 20 Oct 2021 15:00:47 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BD1984690C; Wed, 20 Oct 2021 15:00:47 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 20 Oct 2021 15:00:43 +0200 Message-Id: <20211020130045.2020043-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.532 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [fs.rs] Subject: [pbs-devel] [PATCH proxmox] add fsync parameter to replace_file and atomic_open_or_create X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Oct 2021 13:01:19 -0000 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>( /// 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>( 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>( 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>( /// 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>( path: P, mut oflag: OFlag, initial_data: &[u8], options: CreateOptions, + fsync: bool, ) -> Result { let path = path.as_ref(); @@ -244,6 +259,10 @@ pub fn atomic_open_or_create_file>( 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>( OFlag::O_RDWR | OFlag::O_CLOEXEC | OFlag::O_APPEND, &[], options, + false, )?; match lock_file(&mut file, exclusive, Some(timeout)) { -- 2.30.2