]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bcachefs: Factor out progress.[ch]
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 6 Feb 2025 20:59:28 +0000 (15:59 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sat, 15 Mar 2025 01:02:12 +0000 (21:02 -0400)
the backpointers code has progress indicators; these aren't great, since
they print to the dmesg console and we much prefer to have progress
indicators reporting to a specific userspace program so they're not
spamming the system console.

But not all codepaths that need progress indicators support that yet,
and we don't want users to think "this is hung".

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/Makefile
fs/bcachefs/backpointers.c
fs/bcachefs/backpointers.h
fs/bcachefs/progress.c [new file with mode: 0644]
fs/bcachefs/progress.h [new file with mode: 0644]

index d2689388d5e88e0aae78fc7cc23c193a9e0727bd..1cf17a16af9f91b83ba91487ab35996a6c711c12 100644 (file)
@@ -67,6 +67,7 @@ bcachefs-y            :=      \
        nocow_locking.o         \
        opts.o                  \
        printbuf.o              \
+       progress.o              \
        quota.o                 \
        rebalance.o             \
        rcu_pending.o           \
index 3aff2b24de4aa79aca68466607102bcb285de382..bb799b86aa699a54ace9faafd711735ecc9470c7 100644 (file)
@@ -11,6 +11,7 @@
 #include "checksum.h"
 #include "disk_accounting.h"
 #include "error.h"
+#include "progress.h"
 
 #include <linux/mm.h>
 
@@ -735,71 +736,6 @@ static int bch2_get_btree_in_memory_pos(struct btree_trans *trans,
        return ret;
 }
 
-struct progress_indicator_state {
-       unsigned long           next_print;
-       u64                     nodes_seen;
-       u64                     nodes_total;
-       struct btree            *last_node;
-};
-
-static inline void progress_init(struct progress_indicator_state *s,
-                                struct bch_fs *c,
-                                u64 btree_id_mask)
-{
-       memset(s, 0, sizeof(*s));
-
-       s->next_print = jiffies + HZ * 10;
-
-       for (unsigned i = 0; i < BTREE_ID_NR; i++) {
-               if (!(btree_id_mask & BIT_ULL(i)))
-                       continue;
-
-               struct disk_accounting_pos acc = {
-                       .type           = BCH_DISK_ACCOUNTING_btree,
-                       .btree.id       = i,
-               };
-
-               u64 v;
-               bch2_accounting_mem_read(c, disk_accounting_pos_to_bpos(&acc), &v, 1);
-               s->nodes_total += div64_ul(v, btree_sectors(c));
-       }
-}
-
-static inline bool progress_update_p(struct progress_indicator_state *s)
-{
-       bool ret = time_after_eq(jiffies, s->next_print);
-
-       if (ret)
-               s->next_print = jiffies + HZ * 10;
-       return ret;
-}
-
-static void progress_update_iter(struct btree_trans *trans,
-                                struct progress_indicator_state *s,
-                                struct btree_iter *iter,
-                                const char *msg)
-{
-       struct bch_fs *c = trans->c;
-       struct btree *b = path_l(btree_iter_path(trans, iter))->b;
-
-       s->nodes_seen += b != s->last_node;
-       s->last_node = b;
-
-       if (progress_update_p(s)) {
-               struct printbuf buf = PRINTBUF;
-               unsigned percent = s->nodes_total
-                       ? div64_u64(s->nodes_seen * 100, s->nodes_total)
-                       : 0;
-
-               prt_printf(&buf, "%s: %d%%, done %llu/%llu nodes, at ",
-                          msg, percent, s->nodes_seen, s->nodes_total);
-               bch2_bbpos_to_text(&buf, BBPOS(iter->btree_id, iter->pos));
-
-               bch_info(c, "%s", buf.buf);
-               printbuf_exit(&buf);
-       }
-}
-
 static int bch2_check_extents_to_backpointers_pass(struct btree_trans *trans,
                                                   struct extents_to_bp_state *s)
 {
@@ -807,7 +743,7 @@ static int bch2_check_extents_to_backpointers_pass(struct btree_trans *trans,
        struct progress_indicator_state progress;
        int ret = 0;
 
-       progress_init(&progress, trans->c, BIT_ULL(BTREE_ID_extents)|BIT_ULL(BTREE_ID_reflink));
+       bch2_progress_init(&progress, trans->c, BIT_ULL(BTREE_ID_extents)|BIT_ULL(BTREE_ID_reflink));
 
        for (enum btree_id btree_id = 0;
             btree_id < btree_id_nr_alive(c);
@@ -826,7 +762,7 @@ static int bch2_check_extents_to_backpointers_pass(struct btree_trans *trans,
                                                  BTREE_ITER_prefetch);
 
                        ret = for_each_btree_key_continue(trans, iter, 0, k, ({
-                               progress_update_iter(trans, &progress, &iter, "extents_to_backpointers");
+                               bch2_progress_update_iter(trans, &progress, &iter, "extents_to_backpointers");
                                check_extent_to_backpointers(trans, s, btree_id, level, k) ?:
                                bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
                        }));
@@ -1226,11 +1162,11 @@ static int bch2_check_backpointers_to_extents_pass(struct btree_trans *trans,
 
        bch2_bkey_buf_init(&last_flushed);
        bkey_init(&last_flushed.k->k);
-       progress_init(&progress, trans->c, BIT_ULL(BTREE_ID_backpointers));
+       bch2_progress_init(&progress, trans->c, BIT_ULL(BTREE_ID_backpointers));
 
        int ret = for_each_btree_key(trans, iter, BTREE_ID_backpointers,
                                     POS_MIN, BTREE_ITER_prefetch, k, ({
-                       progress_update_iter(trans, &progress, &iter, "backpointers_to_extents");
+                       bch2_progress_update_iter(trans, &progress, &iter, "backpointers_to_extents");
                        check_one_backpointer(trans, start, end, k, &last_flushed);
        }));
 
index 060dad1521ee37801b7ed1dd4afa60c9032d6d69..5c6a17c21769cec81a00737f63ea6e167633743c 100644 (file)
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
-#ifndef _BCACHEFS_BACKPOINTERS_BACKGROUND_H
-#define _BCACHEFS_BACKPOINTERS_BACKGROUND_H
+#ifndef _BCACHEFS_BACKPOINTERS_H
+#define _BCACHEFS_BACKPOINTERS_H
 
 #include "btree_cache.h"
 #include "btree_iter.h"
diff --git a/fs/bcachefs/progress.c b/fs/bcachefs/progress.c
new file mode 100644 (file)
index 0000000..bafd1c9
--- /dev/null
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "bcachefs.h"
+#include "bbpos.h"
+#include "disk_accounting.h"
+#include "progress.h"
+
+void bch2_progress_init(struct progress_indicator_state *s,
+                       struct bch_fs *c,
+                       u64 btree_id_mask)
+{
+       memset(s, 0, sizeof(*s));
+
+       s->next_print = jiffies + HZ * 10;
+
+       for (unsigned i = 0; i < BTREE_ID_NR; i++) {
+               if (!(btree_id_mask & BIT_ULL(i)))
+                       continue;
+
+               struct disk_accounting_pos acc = {
+                       .type           = BCH_DISK_ACCOUNTING_btree,
+                       .btree.id       = i,
+               };
+
+               u64 v;
+               bch2_accounting_mem_read(c, disk_accounting_pos_to_bpos(&acc), &v, 1);
+               s->nodes_total += div64_ul(v, btree_sectors(c));
+       }
+}
+
+static inline bool progress_update_p(struct progress_indicator_state *s)
+{
+       bool ret = time_after_eq(jiffies, s->next_print);
+
+       if (ret)
+               s->next_print = jiffies + HZ * 10;
+       return ret;
+}
+
+void bch2_progress_update_iter(struct btree_trans *trans,
+                              struct progress_indicator_state *s,
+                              struct btree_iter *iter,
+                              const char *msg)
+{
+       struct bch_fs *c = trans->c;
+       struct btree *b = path_l(btree_iter_path(trans, iter))->b;
+
+       s->nodes_seen += b != s->last_node;
+       s->last_node = b;
+
+       if (progress_update_p(s)) {
+               struct printbuf buf = PRINTBUF;
+               unsigned percent = s->nodes_total
+                       ? div64_u64(s->nodes_seen * 100, s->nodes_total)
+                       : 0;
+
+               prt_printf(&buf, "%s: %d%%, done %llu/%llu nodes, at ",
+                          msg, percent, s->nodes_seen, s->nodes_total);
+               bch2_bbpos_to_text(&buf, BBPOS(iter->btree_id, iter->pos));
+
+               bch_info(c, "%s", buf.buf);
+               printbuf_exit(&buf);
+       }
+}
diff --git a/fs/bcachefs/progress.h b/fs/bcachefs/progress.h
new file mode 100644 (file)
index 0000000..23fb181
--- /dev/null
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _BCACHEFS_PROGRESS_H
+#define _BCACHEFS_PROGRESS_H
+
+/*
+ * Lame progress indicators
+ *
+ * We don't like to use these because they print to the dmesg console, which is
+ * spammy - we much prefer to be wired up to a userspace programm (e.g. via
+ * thread_with_file) and have it print the progress indicator.
+ *
+ * But some code is old and doesn't support that, or runs in a context where
+ * that's not yet practical (mount).
+ */
+
+struct progress_indicator_state {
+       unsigned long           next_print;
+       u64                     nodes_seen;
+       u64                     nodes_total;
+       struct btree            *last_node;
+};
+
+void bch2_progress_init(struct progress_indicator_state *, struct bch_fs *, u64);
+void bch2_progress_update_iter(struct btree_trans *,
+                              struct progress_indicator_state *,
+                              struct btree_iter *,
+                              const char *);
+
+#endif /* _BCACHEFS_PROGRESS_H */