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 32DFD69AA3 for ; Wed, 28 Jul 2021 10:29:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 2098C21DA5 for ; Wed, 28 Jul 2021 10:28:32 +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 A0A8121D9C for ; Wed, 28 Jul 2021 10:28:31 +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 73CB642A92; Wed, 28 Jul 2021 10:28:31 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 28 Jul 2021 10:28:29 +0200 Message-Id: <20210728082829.562447-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.789 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 Subject: [pbs-devel] [RFC proxmox-backup] tape: lock media_catalog file to to get a consistent view with load_catalog 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, 28 Jul 2021 08:29:02 -0000 --- src/tape/media_catalog.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/tape/media_catalog.rs b/src/tape/media_catalog.rs index 62c6acb3..1bd136f9 100644 --- a/src/tape/media_catalog.rs +++ b/src/tape/media_catalog.rs @@ -231,7 +231,12 @@ impl MediaCatalog { pending: Vec::new(), }; - let (found_magic_number, _) = me.load_catalog(&mut file, media_id.media_set_label.as_ref())?; + // Note: lock file, to get a consistent view with load_catalog + nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockExclusive)?; + let result = me.load_catalog(&mut file, media_id.media_set_label.as_ref()); + nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::Unlock)?; + + let (found_magic_number, _) = result?; if !found_magic_number { me.pending.extend(&Self::PROXMOX_BACKUP_MEDIA_CATALOG_MAGIC_1_1); @@ -372,9 +377,18 @@ impl MediaCatalog { match self.file { Some(ref mut file) => { - file.write_all(&self.pending)?; - file.flush()?; - file.sync_data()?; + let pending = &self.pending; + // Note: lock file, to get a consistent view with load_catalog + nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::LockExclusive)?; + let result: Result<(), Error> = proxmox::try_block!({ + file.write_all(pending)?; + file.flush()?; + file.sync_data()?; + Ok(()) + }); + nix::fcntl::flock(file.as_raw_fd(), nix::fcntl::FlockArg::Unlock)?; + + result?; } None => bail!("media catalog not writable (opened read only)"), } -- 2.30.2