]> git.dujemihanovic.xyz Git - linux.git/commitdiff
btrfs: skip subtree scan if it's too high to avoid low stall in btrfs_commit_transact...
authorQu Wenruo <wqu@suse.com>
Wed, 24 Aug 2022 01:14:09 +0000 (09:14 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 26 Sep 2022 10:28:01 +0000 (12:28 +0200)
Btrfs qgroup has a long history of bringing performance penalty in
btrfs_commit_transaction().

Although we tried our best to migrate such impact, there is still an
unsolved call site, btrfs_drop_snapshot().

This function will find the highest shared tree block and modify its
extent ownership to do a subvolume/snapshot dropping.

Such change will affect the whole subtree, and cause tons of qgroup
dirty extents and stall btrfs_commit_transaction().

To avoid such problem, here we introduce a new sysfs interface,
/sys/fs/btrfs/<uuid>/qgroups/drop_subptree_threshold, to determine at
whether and at which level we should skip qgroup accounting for subtree
dropping.

The default value is BTRFS_MAX_LEVEL, thus every subtree drop will go
through qgroup accounting, to ensure qgroup numbers are kept as
consistent as possible.

While for performance sensitive cases, add a way to change the values to
more reasonable values like 3, to make any subtree, which is at or higher
than level 3, to mark qgroup inconsistent and skip the accounting.

The cost is obvious, the qgroup number is no longer consistent, but at
least performance is more reasonable, and users have the control.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/disk-io.c
fs/btrfs/qgroup.c
fs/btrfs/sysfs.c

index 05df502c3c9d29fa1bde34d689288ffcdb0190ef..0108585d838dbcf5346fa39db68cb414cfd9b6aa 100644 (file)
@@ -1002,6 +1002,7 @@ struct btrfs_fs_info {
        struct completion qgroup_rescan_completion;
        struct btrfs_work qgroup_rescan_work;
        bool qgroup_rescan_running;     /* protected by qgroup_rescan_lock */
+       u8 qgroup_drop_subtree_thres;
 
        /* filesystem state */
        unsigned long fs_state;
index 120b7697ab4287f0c21cbdb9dbca54e8a1a25321..0d33d771cee3cf562ed1f66c0f7816bcf6836b99 100644 (file)
@@ -2260,6 +2260,7 @@ static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
        fs_info->qgroup_seq = 1;
        fs_info->qgroup_ulist = NULL;
        fs_info->qgroup_rescan_running = false;
+       fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
        mutex_init(&fs_info->qgroup_rescan_lock);
 }
 
index 89df16dcd83fc79621d83ce6c42ae9280314a900..f4dc5aa3f7003c15683ae07d067a544b9b481ee9 100644 (file)
@@ -1279,6 +1279,7 @@ int btrfs_quota_disable(struct btrfs_fs_info *fs_info)
        quota_root = fs_info->quota_root;
        fs_info->quota_root = NULL;
        fs_info->qgroup_flags &= ~BTRFS_QGROUP_STATUS_FLAG_ON;
+       fs_info->qgroup_drop_subtree_thres = BTRFS_MAX_LEVEL;
        spin_unlock(&fs_info->qgroup_lock);
 
        btrfs_free_qgroup_config(fs_info);
@@ -2307,6 +2308,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
        struct btrfs_fs_info *fs_info = trans->fs_info;
        int ret = 0;
        int level;
+       u8 drop_subptree_thres;
        struct extent_buffer *eb = root_eb;
        struct btrfs_path *path = NULL;
 
@@ -2316,6 +2318,23 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
        if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
                return 0;
 
+       spin_lock(&fs_info->qgroup_lock);
+       drop_subptree_thres = fs_info->qgroup_drop_subtree_thres;
+       spin_unlock(&fs_info->qgroup_lock);
+
+       /*
+        * This function only gets called for snapshot drop, if we hit a high
+        * node here, it means we are going to change ownership for quite a lot
+        * of extents, which will greatly slow down btrfs_commit_transaction().
+        *
+        * So here if we find a high tree here, we just skip the accounting and
+        * mark qgroup inconsistent.
+        */
+       if (root_level >= drop_subptree_thres) {
+               qgroup_mark_inconsistent(fs_info);
+               return 0;
+       }
+
        if (!extent_buffer_uptodate(root_eb)) {
                ret = btrfs_read_extent_buffer(root_eb, root_gen, root_level, NULL);
                if (ret)
index 736d81d911f9edc89091feba131048f962e422b1..699b54b3acaae0b6e8e31f69e34066631b1aaa9c 100644 (file)
@@ -2045,6 +2045,44 @@ static ssize_t qgroup_inconsistent_show(struct kobject *qgroups_kobj,
 }
 BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
 
+static ssize_t qgroup_drop_subtree_thres_show(struct kobject *qgroups_kobj,
+                                             struct kobj_attribute *a,
+                                             char *buf)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
+       u8 result;
+
+       spin_lock(&fs_info->qgroup_lock);
+       result = fs_info->qgroup_drop_subtree_thres;
+       spin_unlock(&fs_info->qgroup_lock);
+
+       return sysfs_emit(buf, "%d\n", result);
+}
+
+static ssize_t qgroup_drop_subtree_thres_store(struct kobject *qgroups_kobj,
+                                              struct kobj_attribute *a,
+                                              const char *buf, size_t len)
+{
+       struct btrfs_fs_info *fs_info = to_fs_info(qgroups_kobj->parent);
+       u8 new_thres;
+       int ret;
+
+       ret = kstrtou8(buf, 10, &new_thres);
+       if (ret)
+               return -EINVAL;
+
+       if (new_thres > BTRFS_MAX_LEVEL)
+               return -EINVAL;
+
+       spin_lock(&fs_info->qgroup_lock);
+       fs_info->qgroup_drop_subtree_thres = new_thres;
+       spin_unlock(&fs_info->qgroup_lock);
+
+       return len;
+}
+BTRFS_ATTR_RW(qgroups, drop_subtree_threshold, qgroup_drop_subtree_thres_show,
+             qgroup_drop_subtree_thres_store);
+
 /*
  * Qgroups global info
  *
@@ -2053,6 +2091,7 @@ BTRFS_ATTR(qgroups, inconsistent, qgroup_inconsistent_show);
 static struct attribute *qgroups_attrs[] = {
        BTRFS_ATTR_PTR(qgroups, enabled),
        BTRFS_ATTR_PTR(qgroups, inconsistent),
+       BTRFS_ATTR_PTR(qgroups, drop_subtree_threshold),
        NULL
 };
 ATTRIBUTE_GROUPS(qgroups);