From: Christian Ebner <c.ebner@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup 1/4] pbs-config: add helper to try upgrade shared lock to exclusive lock
Date: Tue, 1 Dec 2026 15:13:58 +0100 [thread overview]
Message-ID: <20261201141401.438577-2-c.ebner@proxmox.com> (raw)
In-Reply-To: <20261201141401.438577-1-c.ebner@proxmox.com>
Allow callers holding a shared BackupLockGuard to upgrade it in place
to an exclusive lock with a specified timeou, supporting both
file-based and legacy directory locks. On failure (for example, if
another holder still holds a shared lock), the guard is dropped and
any held lock is released.
Add unit tests covering contended and uncontended upgrades for both
file-based and legacy directory locks.
Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
---
pbs-config/Cargo.toml | 3 ++
pbs-config/src/lib.rs | 80 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 83 insertions(+)
diff --git a/pbs-config/Cargo.toml b/pbs-config/Cargo.toml
index 81d9c1650..808079416 100644
--- a/pbs-config/Cargo.toml
+++ b/pbs-config/Cargo.toml
@@ -36,3 +36,6 @@ proxmox-uuid.workspace = true
pbs-api-types.workspace = true
pbs-buildcfg.workspace = true
pbs-key-config.workspace = true
+
+[dev-dependencies]
+tempfile.workspace = true
diff --git a/pbs-config/src/lib.rs b/pbs-config/src/lib.rs
index b50062866..2bab9f018 100644
--- a/pbs-config/src/lib.rs
+++ b/pbs-config/src/lib.rs
@@ -1,4 +1,5 @@
use std::os::unix::prelude::AsRawFd;
+use std::time::Duration;
use anyhow::{Error, bail, format_err};
use hex::FromHex;
@@ -59,6 +60,24 @@ pub struct BackupLockGuard {
_legacy_dir: Option<DirLockGuard>,
}
+impl BackupLockGuard {
+ /// Upgrade of a shared lock to an exclusive lock on the same underlying lock descriptor.
+ ///
+ /// Blocking if timeout is `None`, otherwise blocks for duration if lock cannot be upgraded.
+ /// Non-blocking if timeout is set to 0. Not guaranteed to be atomic. On failure (for example,
+ /// if another holder still holds a shared lock), the guard is dropped, releasing any held lock.
+ pub fn try_upgrade(mut self, timeout: Option<Duration>) -> Result<Self, Error> {
+ if let Some(ref mut file) = self.file {
+ proxmox_sys::fs::lock_file(file, true, timeout)
+ .map_err(|err| format_err!("unable to upgrade lock to exclusive - {err}"))?;
+ } else if let Some(ref mut dir) = self._legacy_dir {
+ proxmox_sys::fs::lock_file(dir, true, timeout)
+ .map_err(|err| format_err!("unable to upgrade lock to exclusive - {err}"))?;
+ }
+ Ok(self)
+ }
+}
+
impl AsRawFd for BackupLockGuard {
fn as_raw_fd(&self) -> i32 {
self.file.as_ref().map_or(-1, |f| f.as_raw_fd())
@@ -124,3 +143,64 @@ pub fn detect_modified_configuration_file<T: AsRef<str>>(
}
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ use proxmox_sys::fs::{CreateOptions, lock_dir_noblock_shared, open_file_locked};
+ use tempfile::TempDir;
+
+ #[test]
+ fn test_backup_lock_guard_try_upgrade_file() {
+ let temp_dir = TempDir::new().unwrap();
+ let lock_path = temp_dir.path().join("test.lck");
+ let timeout = Some(Duration::ZERO);
+
+ let lock_shared = || -> Result<BackupLockGuard, Error> {
+ let file = open_file_locked(&lock_path, Duration::ZERO, false, CreateOptions::new())?;
+ Ok(BackupLockGuard {
+ file: Some(file),
+ _legacy_dir: None,
+ })
+ };
+
+ let guard1 = lock_shared().unwrap();
+ let guard2 = lock_shared().unwrap();
+
+ // Contended upgrade must fail and drop guard1.
+ assert!(guard1.try_upgrade(timeout).is_err());
+
+ // With guard1 dropped, guard2 is the sole holder and can upgrade.
+ let exclusive = guard2.try_upgrade(timeout).unwrap();
+
+ // Exclusive lock must block new shared locks.
+ assert!(lock_shared().is_err());
+
+ drop(exclusive);
+
+ // Once released, shared locks succeed again.
+ let _guard3 = lock_shared().unwrap();
+ }
+
+ #[test]
+ fn test_backup_lock_guard_try_upgrade_legacy_dir() {
+ let temp_dir = TempDir::new().unwrap();
+ let timeout = Some(Duration::ZERO);
+
+ let lock_shared = || -> Result<BackupLockGuard, Error> {
+ lock_dir_noblock_shared(&temp_dir.path(), "test", "busy").map(BackupLockGuard::from)
+ };
+
+ let guard1 = lock_shared().unwrap();
+ let guard2 = lock_shared().unwrap();
+
+ assert!(guard1.try_upgrade(timeout).is_err());
+
+ let exclusive = guard2.try_upgrade(timeout).unwrap();
+ assert!(lock_shared().is_err());
+
+ drop(exclusive);
+ let _guard3 = lock_shared().unwrap();
+ }
+}
--
2.47.3
next prev parent reply other threads:[~2026-12-01 14:14 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 14:13 [PATCH proxmox-backup 0/4] server: pull: reduce lock contention on unchanged snapshots Christian Ebner
2026-12-01 14:13 ` Christian Ebner [this message]
2026-12-01 14:13 ` [PATCH proxmox-backup 2/4] server: pull: load source manifest into memory instead of a temp file Christian Ebner
2026-12-01 14:14 ` [PATCH proxmox-backup 3/4] server: pull: log snapshot sync start in pull_snapshot_from Christian Ebner
2026-12-01 14:14 ` [PATCH proxmox-backup 4/4] server: pull: check unchanged snapshots under a shared lock 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=20261201141401.438577-2-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