[pve-devel] [PATCH storage 2/3] btrfs: cleanup after qgroups when deleting subvolumes

Wolfgang Bumiller w.bumiller at proxmox.com
Thu Jun 24 12:01:41 CEST 2021


Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
They literally just get leaked...

 PVE/Storage/BTRFSPlugin.pm | 34 +++++++++++++++++++++++++++++-----
 1 file changed, 29 insertions(+), 5 deletions(-)

diff --git a/PVE/Storage/BTRFSPlugin.pm b/PVE/Storage/BTRFSPlugin.pm
index 179186b..ccdf3c8 100644
--- a/PVE/Storage/BTRFSPlugin.pm
+++ b/PVE/Storage/BTRFSPlugin.pm
@@ -312,6 +312,30 @@ sub clone_image {
     return $newvolname;
 }
 
+# We need to cleanup after qgroups as they just get leaked...
+my sub subvolume_delete($$@) {
+    my ($class, $scfg, @subvols) = @_;
+
+    for my $path (@subvols) {
+	my $qid = undef;
+	if ($scfg->{quotas}) {
+	    eval {
+		$qid = '0/' . $class->btrfs_get_subvol_id($path);
+	    };
+	    warn "failed to get qgroup id for subvolume: $@\n" if $@;
+	}
+
+	$class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
+
+	if (defined($qid)) {
+	    eval {
+		$class->btrfs_cmd(['qgroup', 'destroy', '--', $qid, $scfg->{path}]);
+	    };
+	    warn "failed to destroy qgroup: $@\n" if $@;
+	}
+    }
+}
+
 sub alloc_image {
     my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
 
@@ -372,7 +396,7 @@ sub alloc_image {
     };
 
     if (my $err = $@) {
-	eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $subvol]); };
+	eval { subvolume_delete($class, $scfg, $subvol); };
 	warn $@ if $@;
 	die $err;
     }
@@ -428,7 +452,7 @@ sub free_image {
 	push @snapshot_vols, "$dir/$volume";
     });
 
-    $class->btrfs_cmd(['subvolume', 'delete', '--', @snapshot_vols, $subvol]);
+    subvolume_delete($class, $scfg, @snapshot_vols, $subvol);
     # try to cleanup directory to not clutter storage with empty $vmid dirs if
     # all images from a guest got deleted
     rmdir($dir);
@@ -550,7 +574,7 @@ sub volume_snapshot_rollback {
     # The paths are absolute, so pass -1 as file descriptors.
     my $ok = PVE::Tools::renameat2(-1, $tmp_path, -1, $path, &PVE::Tools::RENAME_EXCHANGE);
 
-    eval { $class->btrfs_cmd(['subvolume', 'delete', '--', $tmp_path]) };
+    eval { subvolume_delete($class, $scfg, $tmp_path) };
     warn "failed to remove '$tmp_path' subvolume: $@" if $@;
 
     if (!$ok) {
@@ -575,7 +599,7 @@ sub volume_snapshot_delete {
 	$path = raw_file_to_subvol($path);
     }
 
-    $class->btrfs_cmd(['subvolume', 'delete', '--', $path]);
+    subvolume_delete($class, $scfg, $path);
 
     return undef;
 }
@@ -910,7 +934,7 @@ sub volume_import {
 	    $dh->rewind;
 	    while (defined(my $entry = $dh->read)) {
 		next if $entry eq '.' || $entry eq '..';
-		eval { $class->btrfs_cmd(['subvolume', 'delete', '--', "$tmppath/$entry"]) };
+		eval { subvolume_delete($class, $scfg, "$tmppath/$entry") };
 		warn $@ if $@;
 	    }
 	    $dh->close; undef $dh;
-- 
2.30.2






More information about the pve-devel mailing list