Skip to content

Commit 9abb6dd

Browse files
author
Kent Overstreet
committed
bcachefs: Standardize helpers for printing enum strs with bounds checks
Signed-off-by: Kent Overstreet <[email protected]>
1 parent ba8ed36 commit 9abb6dd

File tree

10 files changed

+69
-56
lines changed

10 files changed

+69
-56
lines changed

fs/bcachefs/bcachefs_format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ static inline __u64 __bset_magic(struct bch_sb *sb)
13141314
x(write_buffer_keys, 11) \
13151315
x(datetime, 12)
13161316

1317-
enum {
1317+
enum bch_jset_entry_type {
13181318
#define x(f, nr) BCH_JSET_ENTRY_##f = nr,
13191319
BCH_JSET_ENTRY_TYPES()
13201320
#undef x
@@ -1360,7 +1360,7 @@ struct jset_entry_blacklist_v2 {
13601360
x(inodes, 1) \
13611361
x(key_version, 2)
13621362

1363-
enum {
1363+
enum bch_fs_usage_type {
13641364
#define x(f, nr) BCH_FS_USAGE_##f = nr,
13651365
BCH_FS_USAGE_TYPES()
13661366
#undef x

fs/bcachefs/buckets.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,6 @@ static inline const char *bch2_data_type_str(enum bch_data_type type)
395395
: "(invalid data type)";
396396
}
397397

398-
static inline void bch2_prt_data_type(struct printbuf *out, enum bch_data_type type)
399-
{
400-
if (type < BCH_DATA_NR)
401-
prt_str(out, __bch2_data_types[type]);
402-
else
403-
prt_printf(out, "(invalid data type %u)", type);
404-
}
405-
406398
/* disk reservations: */
407399

408400
static inline void bch2_disk_reservation_put(struct bch_fs *c,

fs/bcachefs/checksum.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,20 @@ int bch2_rechecksum_bio(struct bch_fs *c, struct bio *bio,
429429
extent_nonce(version, crc_old), bio);
430430

431431
if (bch2_crc_cmp(merged, crc_old.csum) && !c->opts.no_data_io) {
432-
bch_err(c, "checksum error in %s() (memory corruption or bug?)\n"
433-
"expected %0llx:%0llx got %0llx:%0llx (old type %s new type %s)",
434-
__func__,
435-
crc_old.csum.hi,
436-
crc_old.csum.lo,
437-
merged.hi,
438-
merged.lo,
439-
bch2_csum_types[crc_old.csum_type],
440-
bch2_csum_types[new_csum_type]);
432+
struct printbuf buf = PRINTBUF;
433+
prt_printf(&buf, "checksum error in %s() (memory corruption or bug?)\n"
434+
"expected %0llx:%0llx got %0llx:%0llx (old type ",
435+
__func__,
436+
crc_old.csum.hi,
437+
crc_old.csum.lo,
438+
merged.hi,
439+
merged.lo);
440+
bch2_prt_csum_type(&buf, crc_old.csum_type);
441+
prt_str(&buf, " new type ");
442+
bch2_prt_csum_type(&buf, new_csum_type);
443+
prt_str(&buf, ")");
444+
bch_err(c, "%s", buf.buf);
445+
printbuf_exit(&buf);
441446
return -EIO;
442447
}
443448

fs/bcachefs/checksum.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ static inline void bch2_csum_err_msg(struct printbuf *out,
6161
struct bch_csum expected,
6262
struct bch_csum got)
6363
{
64-
prt_printf(out, "checksum error: got ");
64+
prt_str(out, "checksum error, type ");
65+
bch2_prt_csum_type(out, type);
66+
prt_str(out, ": got ");
6567
bch2_csum_to_text(out, type, got);
6668
prt_str(out, " should be ");
6769
bch2_csum_to_text(out, type, expected);
68-
prt_printf(out, " type %s", bch2_csum_types[type]);
6970
}
7071

7172
int bch2_chacha_encrypt_key(struct bch_key *, struct nonce, void *, size_t);

fs/bcachefs/compress.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ static inline enum bch_compression_type bch2_compression_opt_to_type(unsigned v)
4747
return __bch2_compression_opt_to_type[bch2_compression_decode(v).type];
4848
}
4949

50-
static inline void bch2_prt_compression_type(struct printbuf *out, enum bch_compression_type type)
51-
{
52-
if (type < BCH_COMPRESSION_TYPE_NR)
53-
prt_str(out, __bch2_compression_types[type]);
54-
else
55-
prt_printf(out, "(invalid compression type %u)", type);
56-
}
57-
5850
int bch2_bio_uncompress_inplace(struct bch_fs *, struct bio *,
5951
struct bch_extent_crc_unpacked *);
6052
int bch2_bio_uncompress(struct bch_fs *, struct bio *, struct bio *,

fs/bcachefs/ec.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ void bch2_stripe_to_text(struct printbuf *out, struct bch_fs *c,
138138

139139
unsigned nr_data = s.nr_blocks - s.nr_redundant;
140140

141-
prt_printf(out, "algo %u sectors %u blocks %u:%u csum %u gran %u",
141+
prt_printf(out, "algo %u sectors %u blocks %u:%u csum ",
142142
s.algorithm,
143143
le16_to_cpu(s.sectors),
144144
nr_data,
145-
s.nr_redundant,
146-
s.csum_type,
147-
1U << s.csum_granularity_bits);
145+
s.nr_redundant);
146+
bch2_prt_csum_type(out, s.csum_type);
147+
prt_printf(out, " gran %u", 1U << s.csum_granularity_bits);
148148

149149
for (unsigned i = 0; i < s.nr_blocks; i++) {
150150
const struct bch_extent_ptr *ptr = sp->ptrs + i;
@@ -611,10 +611,8 @@ static void ec_validate_checksums(struct bch_fs *c, struct ec_stripe_buf *buf)
611611
struct printbuf err = PRINTBUF;
612612
struct bch_dev *ca = bch_dev_bkey_exists(c, v->ptrs[i].dev);
613613

614-
prt_printf(&err, "stripe checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)\n",
615-
want.hi, want.lo,
616-
got.hi, got.lo,
617-
bch2_csum_types[v->csum_type]);
614+
prt_str(&err, "stripe ");
615+
bch2_csum_err_msg(&err, v->csum_type, want, got);
618616
prt_printf(&err, " for %ps at %u of\n ", (void *) _RET_IP_, i);
619617
bch2_bkey_val_to_text(&err, c, bkey_i_to_s_c(&buf->key));
620618
bch_err_ratelimited(ca, "%s", err.buf);

fs/bcachefs/extents.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,11 +1030,12 @@ void bch2_bkey_ptrs_to_text(struct printbuf *out, struct bch_fs *c,
10301030
struct bch_extent_crc_unpacked crc =
10311031
bch2_extent_crc_unpack(k.k, entry_to_crc(entry));
10321032

1033-
prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum %s compress ",
1033+
prt_printf(out, "crc: c_size %u size %u offset %u nonce %u csum ",
10341034
crc.compressed_size,
10351035
crc.uncompressed_size,
1036-
crc.offset, crc.nonce,
1037-
bch2_csum_types[crc.csum_type]);
1036+
crc.offset, crc.nonce);
1037+
bch2_prt_csum_type(out, crc.csum_type);
1038+
prt_str(out, " compress ");
10381039
bch2_prt_compression_type(out, crc.compression_type);
10391040
break;
10401041
}

fs/bcachefs/journal_io.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void journal_entry_err_msg(struct printbuf *out,
247247

248248
if (entry) {
249249
prt_str(out, " type=");
250-
prt_str(out, bch2_jset_entry_types[entry->type]);
250+
bch2_prt_jset_entry_type(out, entry->type);
251251
}
252252

253253
if (!jset) {
@@ -403,7 +403,8 @@ static void journal_entry_btree_keys_to_text(struct printbuf *out, struct bch_fs
403403
jset_entry_for_each_key(entry, k) {
404404
if (!first) {
405405
prt_newline(out);
406-
prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
406+
bch2_prt_jset_entry_type(out, entry->type);
407+
prt_str(out, ": ");
407408
}
408409
prt_printf(out, "btree=%s l=%u ", bch2_btree_id_str(entry->btree_id), entry->level);
409410
bch2_bkey_val_to_text(out, c, bkey_i_to_s_c(k));
@@ -563,9 +564,9 @@ static void journal_entry_usage_to_text(struct printbuf *out, struct bch_fs *c,
563564
struct jset_entry_usage *u =
564565
container_of(entry, struct jset_entry_usage, entry);
565566

566-
prt_printf(out, "type=%s v=%llu",
567-
bch2_fs_usage_types[u->entry.btree_id],
568-
le64_to_cpu(u->v));
567+
prt_str(out, "type=");
568+
bch2_prt_fs_usage_type(out, u->entry.btree_id);
569+
prt_printf(out, " v=%llu", le64_to_cpu(u->v));
569570
}
570571

571572
static int journal_entry_data_usage_validate(struct bch_fs *c,
@@ -827,11 +828,11 @@ int bch2_journal_entry_validate(struct bch_fs *c,
827828
void bch2_journal_entry_to_text(struct printbuf *out, struct bch_fs *c,
828829
struct jset_entry *entry)
829830
{
831+
bch2_prt_jset_entry_type(out, entry->type);
832+
830833
if (entry->type < BCH_JSET_ENTRY_NR) {
831-
prt_printf(out, "%s: ", bch2_jset_entry_types[entry->type]);
834+
prt_str(out, ": ");
832835
bch2_jset_entry_ops[entry->type].to_text(out, c, entry);
833-
} else {
834-
prt_printf(out, "(unknown type %u)", entry->type);
835836
}
836837
}
837838

fs/bcachefs/opts.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const char * const __bch2_btree_ids[] = {
4343
NULL
4444
};
4545

46-
const char * const bch2_csum_types[] = {
46+
static const char * const __bch2_csum_types[] = {
4747
BCH_CSUM_TYPES()
4848
NULL
4949
};
@@ -53,7 +53,7 @@ const char * const bch2_csum_opts[] = {
5353
NULL
5454
};
5555

56-
const char * const __bch2_compression_types[] = {
56+
static const char * const __bch2_compression_types[] = {
5757
BCH_COMPRESSION_TYPES()
5858
NULL
5959
};
@@ -83,18 +83,39 @@ const char * const bch2_member_states[] = {
8383
NULL
8484
};
8585

86-
const char * const bch2_jset_entry_types[] = {
86+
static const char * const __bch2_jset_entry_types[] = {
8787
BCH_JSET_ENTRY_TYPES()
8888
NULL
8989
};
9090

91-
const char * const bch2_fs_usage_types[] = {
91+
static const char * const __bch2_fs_usage_types[] = {
9292
BCH_FS_USAGE_TYPES()
9393
NULL
9494
};
9595

9696
#undef x
9797

98+
static void prt_str_opt_boundscheck(struct printbuf *out, const char * const opts[],
99+
unsigned nr, const char *type, unsigned idx)
100+
{
101+
if (idx < nr)
102+
prt_str(out, opts[idx]);
103+
else
104+
prt_printf(out, "(unknown %s %u)", type, idx);
105+
}
106+
107+
#define PRT_STR_OPT_BOUNDSCHECKED(name, type) \
108+
void bch2_prt_##name(struct printbuf *out, type t) \
109+
{ \
110+
prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
111+
}
112+
113+
PRT_STR_OPT_BOUNDSCHECKED(jset_entry_type, enum bch_jset_entry_type);
114+
PRT_STR_OPT_BOUNDSCHECKED(fs_usage_type, enum bch_fs_usage_type);
115+
PRT_STR_OPT_BOUNDSCHECKED(data_type, enum bch_data_type);
116+
PRT_STR_OPT_BOUNDSCHECKED(csum_type, enum bch_csum_type);
117+
PRT_STR_OPT_BOUNDSCHECKED(compression_type, enum bch_compression_type);
118+
98119
static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
99120
struct printbuf *err)
100121
{

fs/bcachefs/opts.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@ extern const char * const bch2_version_upgrade_opts[];
1616
extern const char * const bch2_sb_features[];
1717
extern const char * const bch2_sb_compat[];
1818
extern const char * const __bch2_btree_ids[];
19-
extern const char * const bch2_csum_types[];
2019
extern const char * const bch2_csum_opts[];
21-
extern const char * const __bch2_compression_types[];
2220
extern const char * const bch2_compression_opts[];
2321
extern const char * const bch2_str_hash_types[];
2422
extern const char * const bch2_str_hash_opts[];
2523
extern const char * const __bch2_data_types[];
2624
extern const char * const bch2_member_states[];
27-
extern const char * const bch2_jset_entry_types[];
28-
extern const char * const bch2_fs_usage_types[];
2925
extern const char * const bch2_d_types[];
3026

27+
void bch2_prt_jset_entry_type(struct printbuf *, enum bch_jset_entry_type);
28+
void bch2_prt_fs_usage_type(struct printbuf *, enum bch_fs_usage_type);
29+
void bch2_prt_data_type(struct printbuf *, enum bch_data_type);
30+
void bch2_prt_csum_type(struct printbuf *, enum bch_csum_type);
31+
void bch2_prt_compression_type(struct printbuf *, enum bch_compression_type);
32+
3133
static inline const char *bch2_d_type_str(unsigned d_type)
3234
{
3335
return (d_type < BCH_DT_MAX ? bch2_d_types[d_type] : NULL) ?: "(bad d_type)";

0 commit comments

Comments
 (0)