]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/rebalance.c
Update bcachefs sources to c887148ebf99 thread_with_file: add f_ops.flush
[bcachefs-tools-debian] / libbcachefs / rebalance.c
index 6ee4d2e02073851a07ad1ef28eb02150e2a430e1..56336f3dd1d0771b94c30aaa78d2c6b0d00617e7 100644 (file)
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include "bcachefs.h"
+#include "alloc_background.h"
 #include "alloc_foreground.h"
 #include "btree_iter.h"
 #include "btree_update.h"
@@ -68,7 +69,7 @@ err:
 
 int bch2_set_rebalance_needs_scan(struct bch_fs *c, u64 inum)
 {
-       int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
+       int ret = bch2_trans_do(c, NULL, NULL, BCH_TRANS_COMMIT_no_enospc|BCH_TRANS_COMMIT_lazy_rw,
                            __bch2_set_rebalance_needs_scan(trans, inum));
        rebalance_wakeup(c);
        return ret;
@@ -124,7 +125,7 @@ static int bch2_bkey_clear_needs_rebalance(struct btree_trans *trans,
 
        extent_entry_drop(bkey_i_to_s(n),
                          (void *) bch2_bkey_rebalance_opts(bkey_i_to_s_c(n)));
-       return bch2_trans_commit(trans, NULL, NULL, BTREE_INSERT_NOFAIL);
+       return bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
 }
 
 static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
@@ -170,6 +171,20 @@ static struct bkey_s_c next_rebalance_extent(struct btree_trans *trans,
                return bkey_s_c_null;
        }
 
+       if (trace_rebalance_extent_enabled()) {
+               struct printbuf buf = PRINTBUF;
+
+               prt_str(&buf, "target=");
+               bch2_target_to_text(&buf, c, r->target);
+               prt_str(&buf, " compression=");
+               bch2_compression_opt_to_text(&buf, r->compression);
+               prt_str(&buf, " ");
+               bch2_bkey_val_to_text(&buf, c, k);
+
+               trace_rebalance_extent(c, buf.buf);
+               printbuf_exit(&buf);
+       }
+
        return k;
 }
 
@@ -238,13 +253,12 @@ static bool rebalance_pred(struct bch_fs *c, void *arg,
 
        if (k.k->p.inode) {
                target          = io_opts->background_target;
-               compression     = io_opts->background_compression ?: io_opts->compression;
+               compression     = background_compression(*io_opts);
        } else {
                const struct bch_extent_rebalance *r = bch2_bkey_rebalance_opts(k);
 
                target          = r ? r->target : io_opts->background_target;
-               compression     = r ? r->compression :
-                       (io_opts->background_compression ?: io_opts->compression);
+               compression     = r ? r->compression : background_compression(*io_opts);
        }
 
        data_opts->rewrite_ptrs         = bch2_bkey_ptrs_need_rebalance(c, k, target, compression);
@@ -272,7 +286,7 @@ static int do_rebalance_scan(struct moving_context *ctxt, u64 inum, u64 cookie)
        r->state = BCH_REBALANCE_scanning;
 
        ret = __bch2_move_data(ctxt, r->scan_start, r->scan_end, rebalance_pred, NULL) ?:
-               commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
+               commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
                          bch2_clear_rebalance_needs_scan(trans, inum, cookie));
 
        bch2_move_stats_exit(&r->scan_stats, trans->c);
@@ -282,15 +296,12 @@ static int do_rebalance_scan(struct moving_context *ctxt, u64 inum, u64 cookie)
 static void rebalance_wait(struct bch_fs *c)
 {
        struct bch_fs_rebalance *r = &c->rebalance;
-       struct bch_dev *ca;
        struct io_clock *clock = &c->io_clock[WRITE];
        u64 now = atomic64_read(&clock->now);
-       u64 min_member_capacity = 128 * 2048;
-       unsigned i;
+       u64 min_member_capacity = bch2_min_rw_member_capacity(c);
 
-       for_each_rw_member(ca, c, i)
-               min_member_capacity = min(min_member_capacity,
-                                         ca->mi.nbuckets * ca->mi.bucket_size);
+       if (min_member_capacity == U64_MAX)
+               min_member_capacity = 128 * 2048;
 
        r->wait_iotime_end              = now + (min_member_capacity >> 6);
 
@@ -319,8 +330,16 @@ static int do_rebalance(struct moving_context *ctxt)
                             BTREE_ID_rebalance_work, POS_MIN,
                             BTREE_ITER_ALL_SNAPSHOTS);
 
-       while (!bch2_move_ratelimit(ctxt) &&
-              !kthread_wait_freezable(r->enabled)) {
+       while (!bch2_move_ratelimit(ctxt)) {
+               if (!r->enabled) {
+                       bch2_moving_ctxt_flush_all(ctxt);
+                       kthread_wait_freezable(r->enabled ||
+                                              kthread_should_stop());
+               }
+
+               if (kthread_should_stop())
+                       break;
+
                bch2_trans_begin(trans);
 
                ret = bkey_err(k = next_rebalance_entry(trans, &rebalance_work_iter));
@@ -350,7 +369,8 @@ static int do_rebalance(struct moving_context *ctxt)
            !kthread_should_stop() &&
            !atomic64_read(&r->work_stats.sectors_seen) &&
            !atomic64_read(&r->scan_stats.sectors_seen)) {
-               bch2_trans_unlock(trans);
+               bch2_moving_ctxt_flush_all(ctxt);
+               bch2_trans_unlock_long(trans);
                rebalance_wait(c);
        }
 
@@ -364,7 +384,6 @@ static int bch2_rebalance_thread(void *arg)
        struct bch_fs *c = arg;
        struct bch_fs_rebalance *r = &c->rebalance;
        struct moving_context ctxt;
-       int ret;
 
        set_freezable();
 
@@ -372,8 +391,7 @@ static int bch2_rebalance_thread(void *arg)
                              writepoint_ptr(&c->rebalance_write_point),
                              true);
 
-       while (!kthread_should_stop() &&
-              !(ret = do_rebalance(&ctxt)))
+       while (!kthread_should_stop() && !do_rebalance(&ctxt))
                ;
 
        bch2_moving_ctxt_exit(&ctxt);
@@ -394,11 +412,11 @@ void bch2_rebalance_status_to_text(struct printbuf *out, struct bch_fs *c)
                u64 now = atomic64_read(&c->io_clock[WRITE].now);
 
                prt_str(out, "io wait duration:  ");
-               bch2_prt_human_readable_s64(out, r->wait_iotime_end - r->wait_iotime_start);
+               bch2_prt_human_readable_s64(out, (r->wait_iotime_end - r->wait_iotime_start) << 9);
                prt_newline(out);
 
                prt_str(out, "io wait remaining: ");
-               bch2_prt_human_readable_s64(out, r->wait_iotime_end - now);
+               bch2_prt_human_readable_s64(out, (r->wait_iotime_end - now) << 9);
                prt_newline(out);
 
                prt_str(out, "duration waited:   ");
@@ -449,10 +467,9 @@ int bch2_rebalance_start(struct bch_fs *c)
 
        p = kthread_create(bch2_rebalance_thread, c, "bch-rebalance/%s", c->name);
        ret = PTR_ERR_OR_ZERO(p);
-       if (ret) {
-               bch_err_msg(c, ret, "creating rebalance thread");
+       bch_err_msg(c, ret, "creating rebalance thread");
+       if (ret)
                return ret;
-       }
 
        get_task_struct(p);
        rcu_assign_pointer(c->rebalance.thread, p);