btrfs: qgroup: Search commit root for rescan to avoid missing extent
[sfrench/cifs-2.6.git] / fs / btrfs / qgroup.c
index 09c7e4fd550f4d10b8febcc745e0ba4138d9f35b..641ef2679bb1832891990fdf75e17afc92a28c38 100644 (file)
@@ -11,6 +11,7 @@
 #include <linux/slab.h>
 #include <linux/workqueue.h>
 #include <linux/btrfs.h>
+#include <linux/sizes.h>
 
 #include "ctree.h"
 #include "transaction.h"
@@ -1881,8 +1882,8 @@ static int qgroup_update_counters(struct btrfs_fs_info *fs_info,
                cur_old_count = btrfs_qgroup_get_old_refcnt(qg, seq);
                cur_new_count = btrfs_qgroup_get_new_refcnt(qg, seq);
 
-               trace_qgroup_update_counters(fs_info, qg->qgroupid,
-                                            cur_old_count, cur_new_count);
+               trace_qgroup_update_counters(fs_info, qg, cur_old_count,
+                                            cur_new_count);
 
                /* Rfer update part */
                if (cur_old_count == 0 && cur_new_count > 0) {
@@ -2013,8 +2014,8 @@ btrfs_qgroup_account_extent(struct btrfs_trans_handle *trans,
 
        BUG_ON(!fs_info->quota_root);
 
-       trace_btrfs_qgroup_account_extent(fs_info, bytenr, num_bytes,
-                                         nr_old_roots, nr_new_roots);
+       trace_btrfs_qgroup_account_extent(fs_info, trans->transid, bytenr,
+                                       num_bytes, nr_old_roots, nr_new_roots);
 
        qgroups = ulist_alloc(GFP_NOFS);
        if (!qgroups) {
@@ -2375,8 +2376,21 @@ out:
        return ret;
 }
 
-static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
+/*
+ * Two limits to commit transaction in advance.
+ *
+ * For RATIO, it will be 1/RATIO of the remaining limit
+ * (excluding data and prealloc meta) as threshold.
+ * For SIZE, it will be in byte unit as threshold.
+ */
+#define QGROUP_PERTRANS_RATIO          32
+#define QGROUP_PERTRANS_SIZE           SZ_32M
+static bool qgroup_check_limits(struct btrfs_fs_info *fs_info,
+                               const struct btrfs_qgroup *qg, u64 num_bytes)
 {
+       u64 limit;
+       u64 threshold;
+
        if ((qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_RFER) &&
            qgroup_rsv_total(qg) + (s64)qg->rfer + num_bytes > qg->max_rfer)
                return false;
@@ -2385,6 +2399,31 @@ static bool qgroup_check_limits(const struct btrfs_qgroup *qg, u64 num_bytes)
            qgroup_rsv_total(qg) + (s64)qg->excl + num_bytes > qg->max_excl)
                return false;
 
+       /*
+        * Even if we passed the check, it's better to check if reservation
+        * for meta_pertrans is pushing us near limit.
+        * If there is too much pertrans reservation or it's near the limit,
+        * let's try commit transaction to free some, using transaction_kthread
+        */
+       if ((qg->lim_flags & (BTRFS_QGROUP_LIMIT_MAX_RFER |
+                             BTRFS_QGROUP_LIMIT_MAX_EXCL))) {
+               if (qg->lim_flags & BTRFS_QGROUP_LIMIT_MAX_EXCL)
+                       limit = qg->max_excl;
+               else
+                       limit = qg->max_rfer;
+               threshold = (limit - qg->rsv.values[BTRFS_QGROUP_RSV_DATA] -
+                           qg->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC]) /
+                           QGROUP_PERTRANS_RATIO;
+               threshold = min_t(u64, threshold, QGROUP_PERTRANS_SIZE);
+
+               /*
+                * Use transaction_kthread to commit transaction, so we no
+                * longer need to bother nested transaction nor lock context.
+                */
+               if (qg->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS] > threshold)
+                       btrfs_commit_transaction_locksafe(fs_info);
+       }
+
        return true;
 }
 
@@ -2434,7 +2473,7 @@ static int qgroup_reserve(struct btrfs_root *root, u64 num_bytes, bool enforce,
 
                qg = unode_aux_to_qgroup(unode);
 
-               if (enforce && !qgroup_check_limits(qg, num_bytes)) {
+               if (enforce && !qgroup_check_limits(fs_info, qg, num_bytes)) {
                        ret = -EDQUOT;
                        goto out;
                }
@@ -2551,7 +2590,6 @@ qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
        struct btrfs_key found;
        struct extent_buffer *scratch_leaf = NULL;
        struct ulist *roots = NULL;
-       struct seq_list tree_mod_seq_elem = SEQ_LIST_INIT(tree_mod_seq_elem);
        u64 num_bytes;
        int slot;
        int ret;
@@ -2586,7 +2624,6 @@ qgroup_rescan_leaf(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
                              btrfs_header_nritems(path->nodes[0]) - 1);
        fs_info->qgroup_rescan_progress.objectid = found.objectid + 1;
 
-       btrfs_get_tree_mod_seq(fs_info, &tree_mod_seq_elem);
        scratch_leaf = btrfs_clone_extent_buffer(path->nodes[0]);
        if (!scratch_leaf) {
                ret = -ENOMEM;
@@ -2625,7 +2662,6 @@ out:
                btrfs_tree_read_unlock_blocking(scratch_leaf);
                free_extent_buffer(scratch_leaf);
        }
-       btrfs_put_tree_mod_seq(fs_info, &tree_mod_seq_elem);
 
        return ret;
 }
@@ -2642,6 +2678,12 @@ static void btrfs_qgroup_rescan_worker(struct btrfs_work *work)
        path = btrfs_alloc_path();
        if (!path)
                goto out;
+       /*
+        * Rescan should only search for commit root, and any later difference
+        * should be recorded by qgroup
+        */
+       path->search_commit_root = 1;
+       path->skip_locking = 1;
 
        err = 0;
        while (!err && !btrfs_fs_closing(fs_info)) {