From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id C08036A01C for ; Tue, 11 Aug 2020 10:51:30 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B9EE01B1C6 for ; Tue, 11 Aug 2020 10:51:00 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 5E7871B198 for ; Tue, 11 Aug 2020 10:50:58 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id E9688445AD for ; Tue, 11 Aug 2020 10:50:57 +0200 (CEST) From: Stefan Reiter To: pbs-devel@lists.proxmox.com Date: Tue, 11 Aug 2020 10:50:37 +0200 Message-Id: <20200811085042.30686-3-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200811085042.30686-1-s.reiter@proxmox.com> References: <20200811085042.30686-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.073 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [datastore.rs] Subject: [pbs-devel] [PATCH v2 proxmox-backup 2/7] src/backup/backup_info.rs: remove BackupGroup lock() X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 11 Aug 2020 08:51:30 -0000 From: Dietmar Maurer Signed-off-by: Stefan Reiter --- src/backup/backup_info.rs | 41 --------------------------------------- src/backup/datastore.rs | 9 +++++---- 2 files changed, 5 insertions(+), 45 deletions(-) diff --git a/src/backup/backup_info.rs b/src/backup/backup_info.rs index ea917d3c..26b57fae 100644 --- a/src/backup/backup_info.rs +++ b/src/backup/backup_info.rs @@ -3,16 +3,12 @@ use crate::tools; use anyhow::{bail, format_err, Error}; use regex::Regex; use std::os::unix::io::RawFd; -use nix::dir::Dir; -use std::time::Duration; use chrono::{DateTime, TimeZone, SecondsFormat, Utc}; use std::path::{PathBuf, Path}; use lazy_static::lazy_static; -use proxmox::sys::error::SysError; - use super::manifest::MANIFEST_BLOB_NAME; macro_rules! BACKUP_ID_RE { () => (r"[A-Za-z0-9][A-Za-z0-9_-]+") } @@ -40,9 +36,6 @@ lazy_static!{ } -/// Opaque type releasing the corresponding flock when dropped -pub type BackupGroupGuard = Dir; - /// BackupGroup is a directory containing a list of BackupDir #[derive(Debug, Eq, PartialEq, Hash, Clone)] pub struct BackupGroup { @@ -137,40 +130,6 @@ impl BackupGroup { Ok(last) } - pub fn lock(&self, base_path: &Path) -> Result { - use nix::fcntl::OFlag; - use nix::sys::stat::Mode; - - let mut path = base_path.to_owned(); - path.push(self.group_path()); - - let mut handle = Dir::open(&path, OFlag::O_RDONLY, Mode::empty()) - .map_err(|err| { - format_err!( - "unable to open backup group directory {:?} for locking - {}", - self.group_path(), - err, - ) - })?; - - // acquire in non-blocking mode, no point in waiting here since other - // backups could still take a very long time - proxmox::tools::fs::lock_file(&mut handle, true, Some(Duration::from_nanos(0))) - .map_err(|err| { - format_err!( - "unable to acquire lock on backup group {:?} - {}", - self.group_path(), - if err.would_block() { - String::from("another backup is already running") - } else { - err.to_string() - } - ) - })?; - - Ok(handle) - } - pub fn list_groups(base_path: &Path) -> Result, Error> { let mut list = Vec::new(); diff --git a/src/backup/datastore.rs b/src/backup/datastore.rs index afdff224..01695f48 100644 --- a/src/backup/datastore.rs +++ b/src/backup/datastore.rs @@ -11,7 +11,7 @@ use serde_json::Value; use proxmox::tools::fs::{replace_file, CreateOptions}; -use super::backup_info::{BackupGroup, BackupGroupGuard, BackupDir, BackupInfo}; +use super::backup_info::{BackupGroup, BackupDir, BackupInfo}; use super::chunk_store::ChunkStore; use super::dynamic_index::{DynamicIndexReader, DynamicIndexWriter}; use super::fixed_index::{FixedIndexReader, FixedIndexWriter}; @@ -21,6 +21,7 @@ use super::{DataBlob, ArchiveType, archive_type}; use crate::config::datastore; use crate::server::WorkerTask; use crate::tools; +use crate::tools::fs::{lock_dir_noblock, DirLockGuard}; use crate::api2::types::{GarbageCollectionStatus, Userid}; lazy_static! { @@ -335,7 +336,7 @@ impl DataStore { &self, backup_group: &BackupGroup, userid: &Userid, - ) -> Result<(Userid, BackupGroupGuard), Error> { + ) -> Result<(Userid, DirLockGuard), Error> { // create intermediate path first: let base_path = self.base_path(); @@ -348,13 +349,13 @@ impl DataStore { // create the last component now match std::fs::create_dir(&full_path) { Ok(_) => { - let guard = backup_group.lock(&base_path)?; + let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?; self.set_owner(backup_group, userid, false)?; let owner = self.get_owner(backup_group)?; // just to be sure Ok((owner, guard)) } Err(ref err) if err.kind() == io::ErrorKind::AlreadyExists => { - let guard = backup_group.lock(&base_path)?; + let guard = lock_dir_noblock(&full_path, "backup group", "another backup is already running")?; let owner = self.get_owner(backup_group)?; // just to be sure Ok((owner, guard)) } -- 2.20.1