linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs: qgroup: Try our best to delete qgroup relations
@ 2019-08-03  6:45 Qu Wenruo
  2019-08-05 18:13 ` David Sterba
  0 siblings, 1 reply; 3+ messages in thread
From: Qu Wenruo @ 2019-08-03  6:45 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Andrei Borzenkov

When we try to delete qgroups, we're pretty cautious, we make sure both
qgroups exist and there is a relationship between them, then try to
delete the relation.

This behavior is OK, but the problem is we need to two relation items,
and if we failed the first item deletion, we error out, leaving the
other relation item in qgroup tree.

Sometimes the error from del_qgroup_relation_item() could just be
-ENOENT, thus we can ignore that error and continue without any problem.

Further more, such cautious behavior makes qgroup relation deletion
impossible for orphan relation items.

This patch will enhance __del_qgroup_relation():
- If both qgroups and their relation items exist
  Go the regular deletion routine and update their accounting if needed.

- If any qgroup or relation item doesn't exist
  Then we still try to delete the orphan items anyway, but don't trigger
  the accounting update.

By this, we try our best to remove relation items, and can handle orphan
relation items properly, while still keep the existing behavior for good
qgroup tree.

Reported-by: Andrei Borzenkov <arvidjaar@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/qgroup.c | 46 +++++++++++++++++++++++++++++-----------------
 1 file changed, 29 insertions(+), 17 deletions(-)

diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 3e6ffbbd8b0a..2b24d4a4430e 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -1312,8 +1312,9 @@ static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
 	struct btrfs_qgroup *member;
 	struct btrfs_qgroup_list *list;
 	struct ulist *tmp;
+	bool found = false;
 	int ret = 0;
-	int err;
+	int ret2;
 
 	tmp = ulist_alloc(GFP_KERNEL);
 	if (!tmp)
@@ -1327,28 +1328,39 @@ static int __del_qgroup_relation(struct btrfs_trans_handle *trans, u64 src,
 
 	member = find_qgroup_rb(fs_info, src);
 	parent = find_qgroup_rb(fs_info, dst);
-	if (!member || !parent) {
-		ret = -EINVAL;
-		goto out;
-	}
+	/*
+	 * The parent/member pair doesn't exist, then try to delete the dead
+	 * relation items only.
+	 */
+	if (!member && !parent)
+		goto delete_item;
 
 	/* check if such qgroup relation exist firstly */
 	list_for_each_entry(list, &member->groups, next_group) {
-		if (list->group == parent)
-			goto exist;
+		if (list->group == parent) {
+			found = true;
+			break;
+		}
 	}
-	ret = -ENOENT;
-	goto out;
-exist:
+
+delete_item:
 	ret = del_qgroup_relation_item(trans, src, dst);
-	err = del_qgroup_relation_item(trans, dst, src);
-	if (err && !ret)
-		ret = err;
+	if (ret < 0 && ret != -ENOENT)
+		goto out;
+	ret2 = del_qgroup_relation_item(trans, dst, src);
+	if (ret2 < 0 && ret2 != -ENOENT)
+		goto out;
 
-	spin_lock(&fs_info->qgroup_lock);
-	del_relation_rb(fs_info, src, dst);
-	ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
-	spin_unlock(&fs_info->qgroup_lock);
+	/* At least one deletion succeeded, return 0 */
+	if (!ret || !ret2)
+		ret = 0;
+
+	if (found) {
+		spin_lock(&fs_info->qgroup_lock);
+		del_relation_rb(fs_info, src, dst);
+		ret = quick_update_accounting(fs_info, tmp, src, dst, -1);
+		spin_unlock(&fs_info->qgroup_lock);
+	}
 out:
 	ulist_free(tmp);
 	return ret;
-- 
2.22.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: qgroup: Try our best to delete qgroup relations
  2019-08-03  6:45 [PATCH] btrfs: qgroup: Try our best to delete qgroup relations Qu Wenruo
@ 2019-08-05 18:13 ` David Sterba
  2019-08-06  0:04   ` Qu Wenruo
  0 siblings, 1 reply; 3+ messages in thread
From: David Sterba @ 2019-08-05 18:13 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs, Andrei Borzenkov

On Sat, Aug 03, 2019 at 02:45:59PM +0800, Qu Wenruo wrote:
> When we try to delete qgroups, we're pretty cautious, we make sure both
> qgroups exist and there is a relationship between them, then try to
> delete the relation.
> 
> This behavior is OK, but the problem is we need to two relation items,
> and if we failed the first item deletion, we error out, leaving the
> other relation item in qgroup tree.
> 
> Sometimes the error from del_qgroup_relation_item() could just be
> -ENOENT, thus we can ignore that error and continue without any problem.
> 
> Further more, such cautious behavior makes qgroup relation deletion
> impossible for orphan relation items.
> 
> This patch will enhance __del_qgroup_relation():
> - If both qgroups and their relation items exist
>   Go the regular deletion routine and update their accounting if needed.
> 
> - If any qgroup or relation item doesn't exist
>   Then we still try to delete the orphan items anyway, but don't trigger
>   the accounting update.
> 
> By this, we try our best to remove relation items, and can handle orphan
> relation items properly, while still keep the existing behavior for good
> qgroup tree.
> 
> Reported-by: Andrei Borzenkov <arvidjaar@gmail.com>
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Adding this to misc-next, please send a fstests testcase, thanks.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: qgroup: Try our best to delete qgroup relations
  2019-08-05 18:13 ` David Sterba
@ 2019-08-06  0:04   ` Qu Wenruo
  0 siblings, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2019-08-06  0:04 UTC (permalink / raw)
  To: dsterba, Qu Wenruo, linux-btrfs, Andrei Borzenkov


[-- Attachment #1.1: Type: text/plain, Size: 1669 bytes --]



On 2019/8/6 上午2:13, David Sterba wrote:
> On Sat, Aug 03, 2019 at 02:45:59PM +0800, Qu Wenruo wrote:
>> When we try to delete qgroups, we're pretty cautious, we make sure both
>> qgroups exist and there is a relationship between them, then try to
>> delete the relation.
>>
>> This behavior is OK, but the problem is we need to two relation items,
>> and if we failed the first item deletion, we error out, leaving the
>> other relation item in qgroup tree.
>>
>> Sometimes the error from del_qgroup_relation_item() could just be
>> -ENOENT, thus we can ignore that error and continue without any problem.
>>
>> Further more, such cautious behavior makes qgroup relation deletion
>> impossible for orphan relation items.
>>
>> This patch will enhance __del_qgroup_relation():
>> - If both qgroups and their relation items exist
>>   Go the regular deletion routine and update their accounting if needed.
>>
>> - If any qgroup or relation item doesn't exist
>>   Then we still try to delete the orphan items anyway, but don't trigger
>>   the accounting update.
>>
>> By this, we try our best to remove relation items, and can handle orphan
>> relation items properly, while still keep the existing behavior for good
>> qgroup tree.
>>
>> Reported-by: Andrei Borzenkov <arvidjaar@gmail.com>
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
> 
> Adding this to misc-next, please send a fstests testcase, thanks.
> 
That's the problem, I haven't found how the orphan relationship item is
left over.

Latest kernel will already delete both of them if nothing wrong happened.

So no idea how to write one test case.

Thanks,
Qu


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-08-06  0:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-03  6:45 [PATCH] btrfs: qgroup: Try our best to delete qgroup relations Qu Wenruo
2019-08-05 18:13 ` David Sterba
2019-08-06  0:04   ` Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).