From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 71A031FF0E3 for ; Wed, 02 Dec 2026 10:27:25 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 1B1AF209A6; Wed, 02 Dec 2026 10:27:25 +0100 (CET) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Wed, 02 Dec 2026 10:27:19 +0100 Message-Id: Subject: Re: [PATCH proxmox-backup 1/4] pbs-config: add helper to try upgrade shared lock to exclusive lock From: "Jakob Klocker" To: "Christian Ebner" , X-Mailer: aerc 0.20.0 References: <20261201141401.438577-1-c.ebner@proxmox.com> <20261201141401.438577-2-c.ebner@proxmox.com> In-Reply-To: <20261201141401.438577-2-c.ebner@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796203639792 X-SPAM-LEVEL: Spam detection results: 0 AWL 1.054 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: Z62OKNAWKIDK7T6WAQV4CPS5NGUPWCUZ X-Message-ID-Hash: Z62OKNAWKIDK7T6WAQV4CPS5NGUPWCUZ X-MailFrom: j.klocker@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: On Tue Dec 1, 2026 at 3:13 PM CET, Christian Ebner wrote: > Allow callers holding a shared BackupLockGuard to upgrade it in place > to an exclusive lock with a specified timeou, supporting both /s/timeou/timeout > 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 =3D true > pbs-api-types.workspace =3D true > pbs-buildcfg.workspace =3D true > pbs-key-config.workspace =3D true > + > +[dev-dependencies] > +tempfile.workspace =3D 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; > =20 > use anyhow::{Error, bail, format_err}; > use hex::FromHex; > @@ -59,6 +60,24 @@ pub struct BackupLockGuard { > _legacy_dir: Option, since `_legacy_dir` is now read, we might want to drop the leading underscore. > } > =20 > +impl BackupLockGuard { > + /// Upgrade of a shared lock to an exclusive lock on the same underl= ying 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 dropp= ed, releasing any held lock. > + pub fn try_upgrade(mut self, timeout: Option) -> Result { > + if let Some(ref mut file) =3D self.file { > + proxmox_sys::fs::lock_file(file, true, timeout) > + .map_err(|err| format_err!("unable to upgrade lock to ex= clusive - {err}"))?; > + } else if let Some(ref mut dir) =3D self._legacy_dir { > + proxmox_sys::fs::lock_file(dir, true, timeout) > + .map_err(|err| format_err!("unable to upgrade lock to ex= clusive - {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_f= ile_locked}; > + use tempfile::TempDir; > + > + #[test] > + fn test_backup_lock_guard_try_upgrade_file() { > + let temp_dir =3D TempDir::new().unwrap(); > + let lock_path =3D temp_dir.path().join("test.lck"); > + let timeout =3D Some(Duration::ZERO); > + > + let lock_shared =3D || -> Result { > + let file =3D open_file_locked(&lock_path, Duration::ZERO, fa= lse, CreateOptions::new())?; > + Ok(BackupLockGuard { > + file: Some(file), > + _legacy_dir: None, > + }) > + }; > + > + let guard1 =3D lock_shared().unwrap(); > + let guard2 =3D 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 upgrad= e. > + let exclusive =3D 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 =3D lock_shared().unwrap(); > + } > + > + #[test] > + fn test_backup_lock_guard_try_upgrade_legacy_dir() { > + let temp_dir =3D TempDir::new().unwrap(); > + let timeout =3D Some(Duration::ZERO); > + > + let lock_shared =3D || -> Result { > + lock_dir_noblock_shared(&temp_dir.path(), "test", "busy").ma= p(BackupLockGuard::from) `TempDir::path()` already returns a reference, so the extra borrow isn't needed afaict. > + }; > + > + let guard1 =3D lock_shared().unwrap(); > + let guard2 =3D lock_shared().unwrap(); > + > + assert!(guard1.try_upgrade(timeout).is_err()); > + > + let exclusive =3D guard2.try_upgrade(timeout).unwrap(); > + assert!(lock_shared().is_err()); > + > + drop(exclusive); > + let _guard3 =3D lock_shared().unwrap(); > + } > +}