From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <w.bumiller@proxmox.com>
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))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id DB108704E4
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 12:02:17 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id D4E04966F
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 12:01:47 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id E76689652
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 12:01:46 +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 B91BD46789
 for <pve-devel@lists.proxmox.com>; Thu, 24 Jun 2021 12:01:46 +0200 (CEST)
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 24 Jun 2021 12:01:41 +0200
Message-Id: <20210624100142.108334-2-w.bumiller@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20210624100142.108334-1-w.bumiller@proxmox.com>
References: <20210624100142.108334-1-w.bumiller@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.486 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 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
 URI_NOVOWEL               0.5 URI hostname has long non-vowel sequence
Subject: [pve-devel] [PATCH storage 2/3] btrfs: cleanup after qgroups when
 deleting subvolumes
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 24 Jun 2021 10:02:17 -0000

Signed-off-by: Wolfgang Bumiller <w.bumiller@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