linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: David Sterba <dsterba@suse.cz>, Qu Wenruo <quwenruo.btrfs@gmx.com>
Subject: [PATCH v5] btrfs: qgroup: Remove qgroup items along with subvolume deletion
Date: Thu, 9 Aug 2018 16:05:36 +0900	[thread overview]
Message-ID: <1b5d9d72-5e6b-a88b-0192-8676a19b9732@jp.fujitsu.com> (raw)
In-Reply-To: <055391ff-7137-4210-4eff-f2c3f70cf68b@jp.fujitsu.com>

When qgroup is on, subvolume deletion does not remove qgroup items
of the subvolume (qgroup info, limit, relation) from quota tree and
they need to get removed manually by "btrfs qgroup destroy".

Since level 0 qgroup cannot be used/inherited by any other subvolume,
let's remove them automatically when subvolume is deleted
(to be precise, when the subvolume root is dropped).

Signed-off-by: Misono Tomohiro <misono.tomohiro@jp.fujitsu.com>
---
v4 -> v5:
  Commit current transaction before calling btrfs_remove_qgroup() to
  keep qgroup consistency in all case. This resolves the concern in
  v4 path and there should be no demerit in this patch.

 fs/btrfs/extent-tree.c | 45 +++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 41 insertions(+), 4 deletions(-)

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 9e7b237b9547..ed052105e741 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -8871,12 +8871,13 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
 	struct btrfs_root_item *root_item = &root->root_item;
 	struct walk_control *wc;
 	struct btrfs_key key;
+	u64 objectid = root->root_key.objectid;
 	int err = 0;
 	int ret;
 	int level;
 	bool root_dropped = false;
 
-	btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid);
+	btrfs_debug(fs_info, "Drop subvolume %llu", objectid);
 
 	path = btrfs_alloc_path();
 	if (!path) {
@@ -9030,7 +9031,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
 		goto out_end_trans;
 	}
 
-	if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
+	if (objectid != BTRFS_TREE_RELOC_OBJECTID) {
 		ret = btrfs_find_root(tree_root, &root->root_key, path,
 				      NULL, NULL);
 		if (ret < 0) {
@@ -9043,8 +9044,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
 			 *
 			 * The most common failure here is just -ENOENT.
 			 */
-			btrfs_del_orphan_item(trans, tree_root,
-					      root->root_key.objectid);
+			btrfs_del_orphan_item(trans, tree_root, objectid);
 		}
 	}
 
@@ -9056,6 +9056,43 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
 		btrfs_put_fs_root(root);
 	}
 	root_dropped = true;
+
+	if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
+		/*
+		 * Remove level-0 qgroup items since no other subvolume can
+		 * use them.
+		 *
+		 * First, commit current transaction in order to make sure
+		 * this subvolume's excl == rfer == 0. Otherwise removing
+		 * qgroup relation causes qgroup inconsistency if excl != rfer.
+		 */
+		ret = btrfs_commit_transaction(trans);
+		if (ret)
+			goto out_free;
+
+		/* Start new transaction and remove qgroup items */
+		trans = btrfs_start_transaction(tree_root, 0);
+		if (IS_ERR(trans)) {
+			err = PTR_ERR(trans);
+			goto out_free;
+		}
+
+		ret = btrfs_remove_qgroup(trans, objectid);
+		if (ret == 1) {
+			/*
+			 * This means qgroup becomes inconsistent
+			 * (should not happen since we did transaction commit)
+			 */
+			btrfs_warn(fs_info,
+			"qgroup inconsistency found, need qgroup rescan");
+		} else if (ret == -EINVAL || ret == -ENOENT) {
+			/* qgroup is already removed, just ignore this */
+		} else if (ret) {
+			btrfs_abort_transaction(trans, ret);
+			err = ret;
+		}
+	}
+
 out_end_trans:
 	btrfs_end_transaction_throttle(trans);
 out_free:
-- 
2.14.4


  parent reply	other threads:[~2018-08-09  9:29 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <cover.1533783766.git.misono.tomohiro@jp.fujitsu.com>
2018-08-09  4:12 ` [PATCH v4] btrfs: qgroup: Remove qgroup items along with subvolume deletion Misono Tomohiro
2018-08-09  5:47   ` Qu Wenruo
2018-08-09  6:05     ` Misono Tomohiro
2018-08-09  6:14       ` Qu Wenruo
2018-08-09  7:02         ` Misono Tomohiro
2018-08-09  7:05   ` Misono Tomohiro [this message]
2018-08-15 13:06     ` [PATCH v5] " David Sterba
2018-08-15 14:22       ` Qu Wenruo
2018-08-15 14:33     ` Qu Wenruo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1b5d9d72-5e6b-a88b-0192-8676a19b9732@jp.fujitsu.com \
    --to=misono.tomohiro@jp.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=quwenruo.btrfs@gmx.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).