From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pbs-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 45E891FF165 for <inbox@lore.proxmox.com>; Thu, 8 May 2025 15:06:32 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B4608FE02; Thu, 8 May 2025 15:06:50 +0200 (CEST) From: Christian Ebner <c.ebner@proxmox.com> To: pbs-devel@lists.proxmox.com Date: Thu, 8 May 2025 15:05:48 +0200 Message-Id: <20250508130555.494782-15-c.ebner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250508130555.494782-1-c.ebner@proxmox.com> References: <20250508130555.494782-1-c.ebner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.029 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [RFC v2 proxmox-backup 14/21] datastore: GC: clean-up trashed snapshots, groups and namespaces X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion <pbs-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/> List-Post: <mailto:pbs-devel@lists.proxmox.com> List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox Backup Server development discussion <pbs-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pbs-devel-bounces@lists.proxmox.com Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com> Cleanup trashed items during phase 1 of garbage collection. If encountered, index files located within trashed snapshots are touched as well, deferring chunk cleanup to the next run Signed-off-by: Christian Ebner <c.ebner@proxmox.com> --- pbs-datastore/src/datastore.rs | 84 +++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/pbs-datastore/src/datastore.rs b/pbs-datastore/src/datastore.rs index ca05e1bea..d88af4c68 100644 --- a/pbs-datastore/src/datastore.rs +++ b/pbs-datastore/src/datastore.rs @@ -574,6 +574,18 @@ impl DataStore { !path.exists() } + /// Checks if the namespace trash marker file exists, + /// does not imply that the namespace itself exists. + pub fn namespace_is_trashed(&self, namespace: &BackupNamespace) -> bool { + if namespace.is_root() { + return false; + } + let mut path = self.base_path(); + path.push(namespace.path()); + path.push(TRASH_MARKER_FILENAME); + path.exists() + } + /// Remove the namespace and all it's parent components from the trash by removing the trash or /// trash-pending marker file for each namespace level from deepest to shallowest. Missing files /// are ignored. @@ -1322,7 +1334,7 @@ impl DataStore { .context("creating namespace iterator failed")? { let namespace = namespace.context("iterating namespaces failed")?; - for group in arc_self.iter_backup_groups(namespace)? { + for group in arc_self.iter_backup_groups(namespace.clone())? { let group = group.context("iterating backup groups failed")?; // Avoid race between listing/marking of snapshots by GC and pruning the last @@ -1403,10 +1415,80 @@ impl DataStore { } processed_index_files += 1; } + + // Only try to lock a trashed snapshots and continue if that is not + // possible, as then most likely this is in the process of being untrashed. + // Check trash state before and after locking to avoid otherwise possible + // races. + if snapshot.backup_dir.is_trashed() { + if let Ok(_lock) = snapshot.backup_dir.lock() { + if snapshot.backup_dir.is_trashed() { + let path = snapshot.backup_dir.full_path(); + log::info!("removing trashed backup snapshot {path:?}"); + std::fs::remove_dir_all(&path).with_context(|| { + format!("removing trashed backup snapshot {path:?} failed") + })?; + } + } else { + let path = snapshot.backup_dir.full_path(); + warn!("failed to lock trashed backup snapshot can {path:?}"); + } + } } break; } + if group.is_trashed() { + if let Ok(_lock) = group.lock() { + if group.is_trashed() { + let trash_path = group.full_group_path().join(".trashed"); + std::fs::remove_file(&trash_path).map_err(|err| { + format_err!( + "removing the trash file '{trash_path:?}' failed - {err}" + ) + })?; + + let owner_path = group.full_group_path().join("owner"); + std::fs::remove_file(&owner_path).map_err(|err| { + format_err!( + "removing the owner file '{owner_path:?}' failed - {err}" + ) + })?; + + let path = group.full_group_path(); + + std::fs::remove_dir(&path).map_err(|err| { + format_err!("removing group directory {path:?} failed - {err}") + })?; + + // Remove any now empty backup type directory + let base_file = std::fs::File::open(self.base_path())?; + let base_fd = base_file.as_raw_fd(); + for ty in BackupType::iter() { + let mut ty_dir = namespace.path(); + ty_dir.push(ty.to_string()); + match unlinkat(Some(base_fd), &ty_dir, UnlinkatFlags::RemoveDir) { + Ok(_) => (), + Err(nix::errno::Errno::ENOENT) | + Err(nix::errno::Errno::ENOTEMPTY) => (), + Err(err) => info!("failed to remove backup type directory for {namespace} - {err}"), + } + } + } else { + let path = group.full_group_path(); + warn!("failed to lock trashed backup group {path:?}"); + } + } + } + } + if self.namespace_is_trashed(&namespace) { + // Remove the namespace, but only if it was empty (as the GC already cleared child + // items and no new ones have been created since). + match arc_self.destroy_namespace_recursive(&namespace, false) { + Ok(true) => info!("removed trashed namespace {namespace}"), + Ok(false) => info!("failed to remove trashed namespace {namespace}, not empty"), + Err(err) => warn!("removing trashed namespace failed: {err:#}"), + } } } -- 2.39.5 _______________________________________________ pbs-devel mailing list pbs-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel