public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christian Ebner <c.ebner@proxmox.com>
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	[thread overview]
Message-ID: <20261203150448.412515-8-c.ebner@proxmox.com> (raw)
In-Reply-To: <20261203150448.412515-1-c.ebner@proxmox.com>

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 <c.ebner@proxmox.com>
---
 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<Self>,
         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<Self>,
+        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





  parent reply	other threads:[~2026-12-03 15:05 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:04 [PATCH v2 proxmox-backup 0/9] pull: avoid lock contention with verify on unchanged snapshots Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 1/9] server: pull: refactor snapshot sync start logging Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 2/9] server: pull: defer sync stats instantiation until needed Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 3/9] server: pull: load source manifest into memory instead of a temp file Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 4/9] server: pull: move target manifest loading logic into reusable helper Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 5/9] server: pull: decouple decryption key gathering from logging Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 6/9] pbs-datastore: add BackupDir::lock_with_timeout() Christian Ebner
2026-12-03 15:04 ` Christian Ebner [this message]
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 8/9] server: pull: check for unchanged snapshots under a shared lock Christian Ebner
2026-12-03 15:04 ` [PATCH v2 proxmox-backup 9/9] server: pull: skip exclusive lock when source has no client log Christian Ebner

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=20261203150448.412515-8-c.ebner@proxmox.com \
    --to=c.ebner@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