bcachefs: x-macro-ify bch_data_ops enum
authorKent Overstreet <kent.overstreet@linux.dev>
Mon, 20 Nov 2023 23:43:48 +0000 (18:43 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Mon, 1 Jan 2024 16:47:38 +0000 (11:47 -0500)
This will let us add an enum -> string table for a to_text() fn.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/bcachefs_ioctl.h
fs/bcachefs/move.c
fs/bcachefs/move.h

index 934da1b0e37b2c05086b803da5187424dc7a7236..d40ebfe05b0b5809e26c73a5e34747404845e863 100644 (file)
@@ -173,12 +173,17 @@ struct bch_ioctl_disk_set_state {
        __u64                   dev;
 };
 
+#define BCH_DATA_OPS()                 \
+       x(scrub,                0)      \
+       x(rereplicate,          1)      \
+       x(migrate,              2)      \
+       x(rewrite_old_nodes,    3)
+
 enum bch_data_ops {
-       BCH_DATA_OP_SCRUB               = 0,
-       BCH_DATA_OP_REREPLICATE         = 1,
-       BCH_DATA_OP_MIGRATE             = 2,
-       BCH_DATA_OP_REWRITE_OLD_NODES   = 3,
-       BCH_DATA_OP_NR                  = 4,
+#define x(t, n) BCH_DATA_OP_##t = n,
+       BCH_DATA_OPS()
+#undef x
+       BCH_DATA_OP_NR
 };
 
 /*
index 54830ee0ed886795233e939158d9b4f417d11f85..b47c0d28c6d5215374d8018dbcb18fc25df669ca 100644 (file)
 #include <linux/ioprio.h>
 #include <linux/kthread.h>
 
+const char * const bch2_data_ops_strs[] = {
+#define x(t, n, ...) [n] = #t,
+       BCH_DATA_OPS()
+#undef x
+       NULL
+};
+
 static void trace_move_extent2(struct bch_fs *c, struct bkey_s_c k)
 {
        if (trace_move_extent_enabled()) {
@@ -211,7 +218,7 @@ void bch2_move_stats_exit(struct bch_move_stats *stats, struct bch_fs *c)
        trace_move_data(c, stats);
 }
 
-void bch2_move_stats_init(struct bch_move_stats *stats, char *name)
+void bch2_move_stats_init(struct bch_move_stats *stats, const char *name)
 {
        memset(stats, 0, sizeof(*stats));
        stats->data_type = BCH_DATA_user;
@@ -1012,9 +1019,13 @@ int bch2_data_job(struct bch_fs *c,
 {
        int ret = 0;
 
+       if (op.op >= BCH_DATA_OP_NR)
+               return -EINVAL;
+
+       bch2_move_stats_init(stats, bch2_data_ops_strs[op.op]);
+
        switch (op.op) {
-       case BCH_DATA_OP_REREPLICATE:
-               bch2_move_stats_init(stats, "rereplicate");
+       case BCH_DATA_OP_rereplicate:
                stats->data_type = BCH_DATA_journal;
                ret = bch2_journal_flush_device_pins(&c->journal, -1);
 
@@ -1033,14 +1044,11 @@ int bch2_data_job(struct bch_fs *c,
                                     true,
                                     rereplicate_pred, c) ?: ret;
                ret = bch2_replicas_gc2(c) ?: ret;
-
-               bch2_move_stats_exit(stats, c);
                break;
-       case BCH_DATA_OP_MIGRATE:
+       case BCH_DATA_OP_migrate:
                if (op.migrate.dev >= c->sb.nr_devices)
                        return -EINVAL;
 
-               bch2_move_stats_init(stats, "migrate");
                stats->data_type = BCH_DATA_journal;
                ret = bch2_journal_flush_device_pins(&c->journal, op.migrate.dev);
 
@@ -1059,18 +1067,15 @@ int bch2_data_job(struct bch_fs *c,
                                     true,
                                     migrate_pred, &op) ?: ret;
                ret = bch2_replicas_gc2(c) ?: ret;
-
-               bch2_move_stats_exit(stats, c);
                break;
-       case BCH_DATA_OP_REWRITE_OLD_NODES:
-               bch2_move_stats_init(stats, "rewrite_old_nodes");
+       case BCH_DATA_OP_rewrite_old_nodes:
                ret = bch2_scan_old_btree_nodes(c, stats);
-               bch2_move_stats_exit(stats, c);
                break;
        default:
                ret = -EINVAL;
        }
 
+       bch2_move_stats_exit(stats, c);
        return ret;
 }
 
index 0906aa2d1de29c328fbbe9a43ca877eb7fc02471..531965674a31cb562ea5f7476caeecac6a8d12e1 100644 (file)
@@ -75,6 +75,8 @@ do {                                                                  \
 typedef bool (*move_pred_fn)(struct bch_fs *, void *, struct bkey_s_c,
                             struct bch_io_opts *, struct data_update_opts *);
 
+extern const char * const bch2_data_ops_strs[];
+
 void bch2_moving_ctxt_exit(struct moving_context *);
 void bch2_moving_ctxt_init(struct moving_context *, struct bch_fs *,
                           struct bch_ratelimit *, struct bch_move_stats *,
@@ -149,7 +151,7 @@ int bch2_data_job(struct bch_fs *,
 
 void bch2_move_stats_to_text(struct printbuf *, struct bch_move_stats *);
 void bch2_move_stats_exit(struct bch_move_stats *, struct bch_fs *);
-void bch2_move_stats_init(struct bch_move_stats *, char *);
+void bch2_move_stats_init(struct bch_move_stats *, const char *);
 
 void bch2_fs_moving_ctxts_to_text(struct printbuf *, struct bch_fs *);