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 C18481FF09F for ; Tue, 01 Dec 2026 15:14:16 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 44253207CA; Tue, 01 Dec 2026 15:14:16 +0100 (CET) From: Christian Ebner 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 Message-ID: <20261201141401.438577-2-c.ebner@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261201141401.438577-1-c.ebner@proxmox.com> References: <20261201141401.438577-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: 1796134451848 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.340 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: 7O7FYGQDKKR2HKM3EAUPRMY36UYYL673 X-Message-ID-Hash: 7O7FYGQDKKR2HKM3EAUPRMY36UYYL673 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: 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 --- 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, } +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) -> Result { + 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>( } 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 { + 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 { + 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