From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 18F7D1FF0C2 for ; Thu, 03 Dec 2026 16:05:13 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 208992178D; Thu, 03 Dec 2026 16:05:10 +0100 (CET) From: Christian Ebner To: pbs-devel@lists.proxmox.com Subject: [PATCH v2 proxmox-backup 7/9] pbs-datastore: add create_locked_backup_dir_with_timeout() Date: Thu, 3 Dec 2026 16:04:46 +0100 Message-ID: <20261203150448.412515-8-c.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203150448.412515-1-c.ebner@proxmox.com> References: <20261203150448.412515-1-c.ebner@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796310301971 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.332 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 6X7OHUP2X7GPBYTVLIB3VDJY4MLSSWCS X-Message-ID-Hash: 6X7OHUP2X7GPBYTVLIB3VDJY4MLSSWCS X-MailFrom: c.ebner@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add a timeout-aware variant of create_locked_backup_dir() using BackupDir::lock_with_timeout(). Verify the snapshot directory still exists once the exclusive lock is held and bail if it vanished concurrently. This is now also checked for the case without timeout. Signed-off-by: Christian Ebner --- pbs-datastore/src/datastore.rs | 37 ++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs index e6766a3b9..c8e4bc598 100644 --- a/pbs-datastore/src/datastore.rs +++ b/pbs-datastore/src/datastore.rs @@ -1873,6 +1873,19 @@ impl DataStore { self: &Arc, ns: &BackupNamespace, backup_dir: &pbs_api_types::BackupDir, + ) -> Result<(PathBuf, bool, BackupLockGuard), Error> { + self.create_locked_backup_dir_with_timeout(ns, backup_dir, Duration::ZERO) + } + + /// Creates a new backup snapshot inside a BackupGroup, or locks an existing one, waiting up + /// to `timeout` for the exclusive snapshot lock. + /// + /// The BackupGroup directory needs to exist. + pub fn create_locked_backup_dir_with_timeout( + self: &Arc, + ns: &BackupNamespace, + backup_dir: &pbs_api_types::BackupDir, + timeout: Duration, ) -> Result<(PathBuf, bool, BackupLockGuard), Error> { if backup_dir.time > proxmox_time::epoch_i64().saturating_add(self.inner.max_snapshot_time_skew) @@ -1885,22 +1898,34 @@ impl DataStore { let backup_dir = self.backup_dir(ns.clone(), backup_dir.clone())?; let relative_path = backup_dir.relative_path(); + let full_path = backup_dir.full_path(); - match std::fs::create_dir(backup_dir.full_path()) { + let (is_new, guard) = match std::fs::create_dir(&full_path) { Ok(_) => { - let guard = backup_dir.lock().with_context(|| { + let guard = backup_dir.lock_with_timeout(timeout).with_context(|| { format!("while creating new locked snapshot {relative_path:?}") })?; - Ok((relative_path, true, guard)) + (true, guard) } Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => { let guard = backup_dir - .lock() + .lock_with_timeout(timeout) .with_context(|| format!("while creating locked snapshot {relative_path:?}"))?; - Ok((relative_path, false, guard)) + (false, guard) } - Err(e) => Err(e.into()), + Err(e) => return Err(e.into()), + }; + + // Ensure the snapshot directory was not removed between create_dir() and acquiring the + // lock (under file-based locking, open_backup_lockfile() can create a fresh lock file + // after a concurrent destroy() unlinked the old one). + if !full_path.exists() { + bail!( + "while creating locked snapshot {relative_path:?}: snapshot vanished concurrently" + ); } + + Ok((relative_path, is_new, guard)) } /// Get a streaming iter over single-level backup namespaces of a datatstore -- 2.47.3