bcachefs: helpers for printing data types
authorKent Overstreet <kent.overstreet@linux.dev>
Sun, 7 Jan 2024 01:57:43 +0000 (20:57 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 21 Jan 2024 11:01:45 +0000 (06:01 -0500)
We need bounds checking since new versions may introduce new data types.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
14 files changed:
fs/bcachefs/alloc_background.c
fs/bcachefs/alloc_foreground.c
fs/bcachefs/btree_gc.c
fs/bcachefs/buckets.c
fs/bcachefs/buckets.h
fs/bcachefs/ec.c
fs/bcachefs/journal_io.c
fs/bcachefs/move.c
fs/bcachefs/opts.c
fs/bcachefs/opts.h
fs/bcachefs/replicas.c
fs/bcachefs/sb-members.c
fs/bcachefs/super.c
fs/bcachefs/sysfs.c

index bebaaf8dbeea8bcad2c6b62da65ec5f9c36acd7e..614da759226b553fb3bdf15769d700b9e6d24293 100644 (file)
@@ -273,7 +273,7 @@ int bch2_alloc_v4_invalid(struct bch_fs *c, struct bkey_s_c k,
                bkey_fsck_err_on(!bch2_bucket_sectors_dirty(*a.v),
                                 c, err, alloc_key_dirty_sectors_0,
                                 "data_type %s but dirty_sectors==0",
-                                bch2_data_types[a.v->data_type]);
+                                bch2_data_type_str(a.v->data_type));
                break;
        case BCH_DATA_cached:
                bkey_fsck_err_on(!a.v->cached_sectors ||
@@ -325,11 +325,8 @@ void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c
        prt_newline(out);
        printbuf_indent_add(out, 2);
 
-       prt_printf(out, "gen %u oldest_gen %u data_type %s",
-              a->gen, a->oldest_gen,
-              a->data_type < BCH_DATA_NR
-              ? bch2_data_types[a->data_type]
-              : "(invalid data type)");
+       prt_printf(out, "gen %u oldest_gen %u data_type ", a->gen, a->oldest_gen);
+       bch2_prt_data_type(out, a->data_type);
        prt_newline(out);
        prt_printf(out, "journal_seq       %llu",       a->journal_seq);
        prt_newline(out);
index b0ff47998a9440912f940dc09e27b34e6341cb9e..633d3223b353f83e83501601024dd262952236c6 100644 (file)
@@ -1525,10 +1525,11 @@ static void bch2_open_bucket_to_text(struct printbuf *out, struct bch_fs *c, str
        unsigned data_type = ob->data_type;
        barrier(); /* READ_ONCE() doesn't work on bitfields */
 
-       prt_printf(out, "%zu ref %u %s %u:%llu gen %u allocated %u/%u",
+       prt_printf(out, "%zu ref %u ",
                   ob - c->open_buckets,
-                  atomic_read(&ob->pin),
-                  data_type < BCH_DATA_NR ? bch2_data_types[data_type] : "invalid data type",
+                  atomic_read(&ob->pin));
+       bch2_prt_data_type(out, data_type);
+       prt_printf(out, " %u:%llu gen %u allocated %u/%u",
                   ob->dev, ob->bucket, ob->gen,
                   ca->mi.bucket_size - ob->sectors_free, ca->mi.bucket_size);
        if (ob->ec)
index 49b4ade758c3623ed35557a02a00afd31b0bec52..523e9b1069cd22cc1c4dee95d91972be434d74ce 100644 (file)
@@ -597,7 +597,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
                              "bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n"
                              "while marking %s",
                              p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
-                             bch2_data_types[ptr_data_type(k->k, &p.ptr)],
+                             bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
                              p.ptr.gen,
                              (printbuf_reset(&buf),
                               bch2_bkey_val_to_text(&buf, c, *k), buf.buf)))) {
@@ -615,7 +615,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
                              "bucket %u:%zu data type %s ptr gen in the future: %u > %u\n"
                              "while marking %s",
                              p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
-                             bch2_data_types[ptr_data_type(k->k, &p.ptr)],
+                             bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
                              p.ptr.gen, g->gen,
                              (printbuf_reset(&buf),
                               bch2_bkey_val_to_text(&buf, c, *k), buf.buf)))) {
@@ -637,7 +637,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
                              "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
                              "while marking %s",
                              p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen,
-                             bch2_data_types[ptr_data_type(k->k, &p.ptr)],
+                             bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
                              p.ptr.gen,
                              (printbuf_reset(&buf),
                               bch2_bkey_val_to_text(&buf, c, *k), buf.buf))))
@@ -649,7 +649,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
                              "bucket %u:%zu data type %s stale dirty ptr: %u < %u\n"
                              "while marking %s",
                              p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
-                             bch2_data_types[ptr_data_type(k->k, &p.ptr)],
+                             bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
                              p.ptr.gen, g->gen,
                              (printbuf_reset(&buf),
                               bch2_bkey_val_to_text(&buf, c, *k), buf.buf))))
@@ -664,8 +664,8 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
                                "bucket %u:%zu different types of data in same bucket: %s, %s\n"
                                "while marking %s",
                                p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
-                               bch2_data_types[g->data_type],
-                               bch2_data_types[data_type],
+                               bch2_data_type_str(g->data_type),
+                               bch2_data_type_str(data_type),
                                (printbuf_reset(&buf),
                                 bch2_bkey_val_to_text(&buf, c, *k), buf.buf))) {
                        if (data_type == BCH_DATA_btree) {
@@ -1238,11 +1238,11 @@ static int bch2_gc_done(struct bch_fs *c,
 
                for (i = 0; i < BCH_DATA_NR; i++) {
                        copy_dev_field(dev_usage_buckets_wrong,
-                                      d[i].buckets,    "%s buckets", bch2_data_types[i]);
+                                      d[i].buckets,    "%s buckets", bch2_data_type_str(i));
                        copy_dev_field(dev_usage_sectors_wrong,
-                                      d[i].sectors,    "%s sectors", bch2_data_types[i]);
+                                      d[i].sectors,    "%s sectors", bch2_data_type_str(i));
                        copy_dev_field(dev_usage_fragmented_wrong,
-                                      d[i].fragmented, "%s fragmented", bch2_data_types[i]);
+                                      d[i].fragmented, "%s fragmented", bch2_data_type_str(i));
                }
        }
 
@@ -1417,8 +1417,8 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
                        ": got %s, should be %s",
                        iter->pos.inode, iter->pos.offset,
                        gc.gen,
-                       bch2_data_types[new.data_type],
-                       bch2_data_types[gc.data_type]))
+                       bch2_data_type_str(new.data_type),
+                       bch2_data_type_str(gc.data_type)))
                new.data_type = gc.data_type;
 
 #define copy_bucket_field(_errtype, _f)                                        \
@@ -1428,7 +1428,7 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
                        ": got %u, should be %u",                       \
                        iter->pos.inode, iter->pos.offset,              \
                        gc.gen,                                         \
-                       bch2_data_types[gc.data_type],                  \
+                       bch2_data_type_str(gc.data_type),               \
                        new._f, gc._f))                                 \
                new._f = gc._f;                                         \
 
index d83ea0e53df3f36f8476cd096ca4cc6948145cc3..5dc19363bb9fea3f70622e0f8758e2478d213602 100644 (file)
@@ -284,7 +284,7 @@ void bch2_dev_usage_to_text(struct printbuf *out, struct bch_dev_usage *usage)
        prt_newline(out);
 
        for (unsigned i = 0; i < BCH_DATA_NR; i++) {
-               prt_str(out, bch2_data_types[i]);
+               bch2_prt_data_type(out, i);
                prt_tab(out);
                prt_u64(out, usage->d[i].buckets);
                prt_tab_rjust(out);
@@ -523,8 +523,8 @@ int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
        if (bch2_fs_inconsistent_on(g->data_type &&
                        g->data_type != data_type, c,
                        "different types of data in same bucket: %s, %s",
-                       bch2_data_types[g->data_type],
-                       bch2_data_types[data_type])) {
+                       bch2_data_type_str(g->data_type),
+                       bch2_data_type_str(data_type))) {
                ret = -EIO;
                goto err;
        }
@@ -532,7 +532,7 @@ int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
        if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
                        "bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
                        ca->dev_idx, b, g->gen,
-                       bch2_data_types[g->data_type ?: data_type],
+                       bch2_data_type_str(g->data_type ?: data_type),
                        g->dirty_sectors, sectors)) {
                ret = -EIO;
                goto err;
@@ -575,7 +575,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
                        "bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
-                       bch2_data_types[bucket_data_type ?: ptr_data_type],
+                       bch2_data_type_str(bucket_data_type ?: ptr_data_type),
                        ptr->gen,
                        (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
                ret = -EIO;
@@ -588,7 +588,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
                        "bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
-                       bch2_data_types[bucket_data_type ?: ptr_data_type],
+                       bch2_data_type_str(bucket_data_type ?: ptr_data_type),
                        ptr->gen,
                        (printbuf_reset(&buf),
                         bch2_bkey_val_to_text(&buf, c, k), buf.buf));
@@ -603,7 +603,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
                        *bucket_gen(ca, bucket_nr),
-                       bch2_data_types[bucket_data_type ?: ptr_data_type],
+                       bch2_data_type_str(bucket_data_type ?: ptr_data_type),
                        ptr->gen,
                        (printbuf_reset(&buf),
                         bch2_bkey_val_to_text(&buf, c, k), buf.buf));
@@ -624,8 +624,8 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
                        "bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
-                       bch2_data_types[bucket_data_type],
-                       bch2_data_types[ptr_data_type],
+                       bch2_data_type_str(bucket_data_type),
+                       bch2_data_type_str(ptr_data_type),
                        (printbuf_reset(&buf),
                         bch2_bkey_val_to_text(&buf, c, k), buf.buf));
                ret = -EIO;
@@ -638,7 +638,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
                        "bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U32_MAX\n"
                        "while marking %s",
                        ptr->dev, bucket_nr, b_gen,
-                       bch2_data_types[bucket_data_type ?: ptr_data_type],
+                       bch2_data_type_str(bucket_data_type ?: ptr_data_type),
                        bucket_sectors, sectors,
                        (printbuf_reset(&buf),
                         bch2_bkey_val_to_text(&buf, c, k), buf.buf));
@@ -1130,9 +1130,9 @@ static int __bch2_trans_mark_metadata_bucket(struct btree_trans *trans,
                        "bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
                        "while marking %s",
                        iter.pos.inode, iter.pos.offset, a->v.gen,
-                       bch2_data_types[a->v.data_type],
-                       bch2_data_types[type],
-                       bch2_data_types[type]);
+                       bch2_data_type_str(a->v.data_type),
+                       bch2_data_type_str(type),
+                       bch2_data_type_str(type));
                ret = -EIO;
                goto err;
        }
index 2c95cc5d86be661c6d6a0783d366d5d8b8b919d7..2b1e907f2acada63ba31d3c100ac6b5f59d778ea 100644 (file)
@@ -385,6 +385,21 @@ static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b)
        return false;
 }
 
+static inline const char *bch2_data_type_str(enum bch_data_type type)
+{
+       return type < BCH_DATA_NR
+               ? __bch2_data_types[type]
+               : "(invalid data type)";
+}
+
+static inline void bch2_prt_data_type(struct printbuf *out, enum bch_data_type type)
+{
+       if (type < BCH_DATA_NR)
+               prt_str(out, __bch2_data_types[type]);
+       else
+               prt_printf(out, "(invalid data type %u)", type);
+}
+
 /* disk reservations: */
 
 static inline void bch2_disk_reservation_put(struct bch_fs *c,
index b29b8a20bb8b459dfbfa3a2ca8a05f24a4ce64d5..d503af2700247d8aa1257962c37df9b042ee55ec 100644 (file)
@@ -190,7 +190,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
                                               a->v.stripe_redundancy, trans,
                                "bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
                                iter.pos.inode, iter.pos.offset, a->v.gen,
-                               bch2_data_types[a->v.data_type],
+                               bch2_data_type_str(a->v.data_type),
                                a->v.dirty_sectors,
                                a->v.stripe, s.k->p.offset)) {
                        ret = -EIO;
@@ -200,7 +200,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
                if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
                                "bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
                                iter.pos.inode, iter.pos.offset, a->v.gen,
-                               bch2_data_types[a->v.data_type],
+                               bch2_data_type_str(a->v.data_type),
                                a->v.dirty_sectors,
                                s.k->p.offset)) {
                        ret = -EIO;
index b0f4dd491e1205d28c6af528fb59696cdbc4dc9c..04a1e79a5ed392cd8ebaac922a2516b374a6d094 100644 (file)
@@ -683,10 +683,7 @@ static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs
        prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
 
        for (i = 0; i < nr_types; i++) {
-               if (i < BCH_DATA_NR)
-                       prt_printf(out, " %s", bch2_data_types[i]);
-               else
-                       prt_printf(out, " (unknown data type %u)", i);
+               bch2_prt_data_type(out, i);
                prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
                       le64_to_cpu(u->d[i].buckets),
                       le64_to_cpu(u->d[i].sectors),
index 7a33319dcd168001594f6532bafe0caf92f83c22..a9e0920b34f32122f1d3e65220f8237ab09acfdf 100644 (file)
@@ -1083,9 +1083,9 @@ int bch2_data_job(struct bch_fs *c,
 
 void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
 {
-       prt_printf(out, "%s: data type=%s pos=",
-                  stats->name,
-                  bch2_data_types[stats->data_type]);
+       prt_printf(out, "%s: data type==", stats->name);
+       bch2_prt_data_type(out, stats->data_type);
+       prt_str(out, " pos=");
        bch2_bbpos_to_text(out, stats->pos);
        prt_newline(out);
        printbuf_indent_add(out, 2);
index 8e6f230eac38155bf5d048367d6ebde35a4a15bd..6aaf78de88457812ae254a056335a6b65e1b74f3 100644 (file)
@@ -72,7 +72,7 @@ const char * const bch2_str_hash_opts[] = {
        NULL
 };
 
-const char * const bch2_data_types[] = {
+const char * const __bch2_data_types[] = {
        BCH_DATA_TYPES()
        NULL
 };
index 93a24fef42148488cdddb391cd291dd0e0168063..67e98e00e93727dc18a266453d4ee2e1e3736cb0 100644 (file)
@@ -22,7 +22,7 @@ extern const char * const bch2_compression_types[];
 extern const char * const bch2_compression_opts[];
 extern const char * const bch2_str_hash_types[];
 extern const char * const bch2_str_hash_opts[];
-extern const char * const bch2_data_types[];
+extern const char * const __bch2_data_types[];
 extern const char * const bch2_member_states[];
 extern const char * const bch2_jset_entry_types[];
 extern const char * const bch2_fs_usage_types[];
index 92ba56ef1fc89690656e9625871ecd7ee38b5f9b..1c3900da4c77008b36d6d4d14841707012c5aa36 100644 (file)
@@ -39,15 +39,10 @@ static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
 static void bch2_replicas_entry_v0_to_text(struct printbuf *out,
                                           struct bch_replicas_entry_v0 *e)
 {
-       unsigned i;
-
-       if (e->data_type < BCH_DATA_NR)
-               prt_printf(out, "%s", bch2_data_types[e->data_type]);
-       else
-               prt_printf(out, "(invalid data type %u)", e->data_type);
+       bch2_prt_data_type(out, e->data_type);
 
        prt_printf(out, ": %u [", e->nr_devs);
-       for (i = 0; i < e->nr_devs; i++)
+       for (unsigned i = 0; i < e->nr_devs; i++)
                prt_printf(out, i ? " %u" : "%u", e->devs[i]);
        prt_printf(out, "]");
 }
@@ -55,15 +50,10 @@ static void bch2_replicas_entry_v0_to_text(struct printbuf *out,
 void bch2_replicas_entry_to_text(struct printbuf *out,
                                 struct bch_replicas_entry_v1 *e)
 {
-       unsigned i;
-
-       if (e->data_type < BCH_DATA_NR)
-               prt_printf(out, "%s", bch2_data_types[e->data_type]);
-       else
-               prt_printf(out, "(invalid data type %u)", e->data_type);
+       bch2_prt_data_type(out, e->data_type);
 
        prt_printf(out, ": %u/%u [", e->nr_required, e->nr_devs);
-       for (i = 0; i < e->nr_devs; i++)
+       for (unsigned i = 0; i < e->nr_devs; i++)
                prt_printf(out, i ? " %u" : "%u", e->devs[i]);
        prt_printf(out, "]");
 }
index a44a238bf8b5550023226844734424b1211c812a..a45354d2acde9f3ad0b149247c8ff4c7c869fb15 100644 (file)
@@ -251,7 +251,7 @@ static void member_to_text(struct printbuf *out,
        prt_printf(out, "Data allowed:");
        prt_tab(out);
        if (BCH_MEMBER_DATA_ALLOWED(&m))
-               prt_bitflags(out, bch2_data_types, BCH_MEMBER_DATA_ALLOWED(&m));
+               prt_bitflags(out, __bch2_data_types, BCH_MEMBER_DATA_ALLOWED(&m));
        else
                prt_printf(out, "(none)");
        prt_newline(out);
@@ -259,7 +259,7 @@ static void member_to_text(struct printbuf *out,
        prt_printf(out, "Has data:");
        prt_tab(out);
        if (data_have)
-               prt_bitflags(out, bch2_data_types, data_have);
+               prt_bitflags(out, __bch2_data_types, data_have);
        else
                prt_printf(out, "(none)");
        prt_newline(out);
index 9dbc35940197f1c55c1bc48746bc23a3983ac203..a3ec21f229ed6ccabd68f5326b76b762b410aff1 100644 (file)
@@ -1625,7 +1625,7 @@ int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
        if (data) {
                struct printbuf data_has = PRINTBUF;
 
-               prt_bitflags(&data_has, bch2_data_types, data);
+               prt_bitflags(&data_has, __bch2_data_types, data);
                bch_err(ca, "Remove failed, still has data (%s)", data_has.buf);
                printbuf_exit(&data_has);
                ret = -EBUSY;
index 8ed52319ff68d2b93194970b7da51218a579b0dd..434961e400e24e831e989f5748c349971cf72508 100644 (file)
@@ -883,7 +883,7 @@ static void dev_io_done_to_text(struct printbuf *out, struct bch_dev *ca)
 
                for (i = 1; i < BCH_DATA_NR; i++)
                        prt_printf(out, "%-12s:%12llu\n",
-                              bch2_data_types[i],
+                              bch2_data_type_str(i),
                               percpu_u64_get(&ca->io_done->sectors[rw][i]) << 9);
        }
 }
@@ -908,7 +908,7 @@ SHOW(bch2_dev)
        }
 
        if (attr == &sysfs_has_data) {
-               prt_bitflags(out, bch2_data_types, bch2_dev_has_data(c, ca));
+               prt_bitflags(out, __bch2_data_types, bch2_dev_has_data(c, ca));
                prt_char(out, '\n');
        }