Skip to content

Commit e58f963

Browse files
author
Kent Overstreet
committed
bcachefs: helpers for printing data types
We need bounds checking since new versions may introduce new data types. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 38c23fb commit e58f963

14 files changed

Lines changed: 64 additions & 64 deletions

File tree

fs/bcachefs/alloc_background.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ int bch2_alloc_v4_invalid(struct bch_fs *c, struct bkey_s_c k,
273273
bkey_fsck_err_on(!bch2_bucket_sectors_dirty(*a.v),
274274
c, err, alloc_key_dirty_sectors_0,
275275
"data_type %s but dirty_sectors==0",
276-
bch2_data_types[a.v->data_type]);
276+
bch2_data_type_str(a.v->data_type));
277277
break;
278278
case BCH_DATA_cached:
279279
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
325325
prt_newline(out);
326326
printbuf_indent_add(out, 2);
327327

328-
prt_printf(out, "gen %u oldest_gen %u data_type %s",
329-
a->gen, a->oldest_gen,
330-
a->data_type < BCH_DATA_NR
331-
? bch2_data_types[a->data_type]
332-
: "(invalid data type)");
328+
prt_printf(out, "gen %u oldest_gen %u data_type ", a->gen, a->oldest_gen);
329+
bch2_prt_data_type(out, a->data_type);
333330
prt_newline(out);
334331
prt_printf(out, "journal_seq %llu", a->journal_seq);
335332
prt_newline(out);

fs/bcachefs/alloc_foreground.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,10 +1525,11 @@ static void bch2_open_bucket_to_text(struct printbuf *out, struct bch_fs *c, str
15251525
unsigned data_type = ob->data_type;
15261526
barrier(); /* READ_ONCE() doesn't work on bitfields */
15271527

1528-
prt_printf(out, "%zu ref %u %s %u:%llu gen %u allocated %u/%u",
1528+
prt_printf(out, "%zu ref %u ",
15291529
ob - c->open_buckets,
1530-
atomic_read(&ob->pin),
1531-
data_type < BCH_DATA_NR ? bch2_data_types[data_type] : "invalid data type",
1530+
atomic_read(&ob->pin));
1531+
bch2_prt_data_type(out, data_type);
1532+
prt_printf(out, " %u:%llu gen %u allocated %u/%u",
15321533
ob->dev, ob->bucket, ob->gen,
15331534
ca->mi.bucket_size - ob->sectors_free, ca->mi.bucket_size);
15341535
if (ob->ec)

fs/bcachefs/btree_gc.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ static int bch2_check_fix_ptrs(struct btree_trans *trans, enum btree_id btree_id
597597
"bucket %u:%zu data type %s ptr gen %u missing in alloc btree\n"
598598
"while marking %s",
599599
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
600-
bch2_data_types[ptr_data_type(k->k, &p.ptr)],
600+
bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
601601
p.ptr.gen,
602602
(printbuf_reset(&buf),
603603
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
615615
"bucket %u:%zu data type %s ptr gen in the future: %u > %u\n"
616616
"while marking %s",
617617
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
618-
bch2_data_types[ptr_data_type(k->k, &p.ptr)],
618+
bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
619619
p.ptr.gen, g->gen,
620620
(printbuf_reset(&buf),
621621
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
637637
"bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
638638
"while marking %s",
639639
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr), g->gen,
640-
bch2_data_types[ptr_data_type(k->k, &p.ptr)],
640+
bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
641641
p.ptr.gen,
642642
(printbuf_reset(&buf),
643643
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
649649
"bucket %u:%zu data type %s stale dirty ptr: %u < %u\n"
650650
"while marking %s",
651651
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
652-
bch2_data_types[ptr_data_type(k->k, &p.ptr)],
652+
bch2_data_type_str(ptr_data_type(k->k, &p.ptr)),
653653
p.ptr.gen, g->gen,
654654
(printbuf_reset(&buf),
655655
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
664664
"bucket %u:%zu different types of data in same bucket: %s, %s\n"
665665
"while marking %s",
666666
p.ptr.dev, PTR_BUCKET_NR(ca, &p.ptr),
667-
bch2_data_types[g->data_type],
668-
bch2_data_types[data_type],
667+
bch2_data_type_str(g->data_type),
668+
bch2_data_type_str(data_type),
669669
(printbuf_reset(&buf),
670670
bch2_bkey_val_to_text(&buf, c, *k), buf.buf))) {
671671
if (data_type == BCH_DATA_btree) {
@@ -1238,11 +1238,11 @@ static int bch2_gc_done(struct bch_fs *c,
12381238

12391239
for (i = 0; i < BCH_DATA_NR; i++) {
12401240
copy_dev_field(dev_usage_buckets_wrong,
1241-
d[i].buckets, "%s buckets", bch2_data_types[i]);
1241+
d[i].buckets, "%s buckets", bch2_data_type_str(i));
12421242
copy_dev_field(dev_usage_sectors_wrong,
1243-
d[i].sectors, "%s sectors", bch2_data_types[i]);
1243+
d[i].sectors, "%s sectors", bch2_data_type_str(i));
12441244
copy_dev_field(dev_usage_fragmented_wrong,
1245-
d[i].fragmented, "%s fragmented", bch2_data_types[i]);
1245+
d[i].fragmented, "%s fragmented", bch2_data_type_str(i));
12461246
}
12471247
}
12481248

@@ -1417,8 +1417,8 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
14171417
": got %s, should be %s",
14181418
iter->pos.inode, iter->pos.offset,
14191419
gc.gen,
1420-
bch2_data_types[new.data_type],
1421-
bch2_data_types[gc.data_type]))
1420+
bch2_data_type_str(new.data_type),
1421+
bch2_data_type_str(gc.data_type)))
14221422
new.data_type = gc.data_type;
14231423

14241424
#define copy_bucket_field(_errtype, _f) \
@@ -1428,7 +1428,7 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
14281428
": got %u, should be %u", \
14291429
iter->pos.inode, iter->pos.offset, \
14301430
gc.gen, \
1431-
bch2_data_types[gc.data_type], \
1431+
bch2_data_type_str(gc.data_type), \
14321432
new._f, gc._f)) \
14331433
new._f = gc._f; \
14341434

fs/bcachefs/buckets.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void bch2_dev_usage_to_text(struct printbuf *out, struct bch_dev_usage *usage)
284284
prt_newline(out);
285285

286286
for (unsigned i = 0; i < BCH_DATA_NR; i++) {
287-
prt_str(out, bch2_data_types[i]);
287+
bch2_prt_data_type(out, i);
288288
prt_tab(out);
289289
prt_u64(out, usage->d[i].buckets);
290290
prt_tab_rjust(out);
@@ -523,16 +523,16 @@ int bch2_mark_metadata_bucket(struct bch_fs *c, struct bch_dev *ca,
523523
if (bch2_fs_inconsistent_on(g->data_type &&
524524
g->data_type != data_type, c,
525525
"different types of data in same bucket: %s, %s",
526-
bch2_data_types[g->data_type],
527-
bch2_data_types[data_type])) {
526+
bch2_data_type_str(g->data_type),
527+
bch2_data_type_str(data_type))) {
528528
ret = -EIO;
529529
goto err;
530530
}
531531

532532
if (bch2_fs_inconsistent_on((u64) g->dirty_sectors + sectors > ca->mi.bucket_size, c,
533533
"bucket %u:%zu gen %u data type %s sector count overflow: %u + %u > bucket size",
534534
ca->dev_idx, b, g->gen,
535-
bch2_data_types[g->data_type ?: data_type],
535+
bch2_data_type_str(g->data_type ?: data_type),
536536
g->dirty_sectors, sectors)) {
537537
ret = -EIO;
538538
goto err;
@@ -575,7 +575,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
575575
"bucket %u:%zu gen %u data type %s: ptr gen %u newer than bucket gen\n"
576576
"while marking %s",
577577
ptr->dev, bucket_nr, b_gen,
578-
bch2_data_types[bucket_data_type ?: ptr_data_type],
578+
bch2_data_type_str(bucket_data_type ?: ptr_data_type),
579579
ptr->gen,
580580
(bch2_bkey_val_to_text(&buf, c, k), buf.buf));
581581
ret = -EIO;
@@ -588,7 +588,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
588588
"bucket %u:%zu gen %u data type %s: ptr gen %u too stale\n"
589589
"while marking %s",
590590
ptr->dev, bucket_nr, b_gen,
591-
bch2_data_types[bucket_data_type ?: ptr_data_type],
591+
bch2_data_type_str(bucket_data_type ?: ptr_data_type),
592592
ptr->gen,
593593
(printbuf_reset(&buf),
594594
bch2_bkey_val_to_text(&buf, c, k), buf.buf));
@@ -603,7 +603,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
603603
"while marking %s",
604604
ptr->dev, bucket_nr, b_gen,
605605
*bucket_gen(ca, bucket_nr),
606-
bch2_data_types[bucket_data_type ?: ptr_data_type],
606+
bch2_data_type_str(bucket_data_type ?: ptr_data_type),
607607
ptr->gen,
608608
(printbuf_reset(&buf),
609609
bch2_bkey_val_to_text(&buf, c, k), buf.buf));
@@ -624,8 +624,8 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
624624
"bucket %u:%zu gen %u different types of data in same bucket: %s, %s\n"
625625
"while marking %s",
626626
ptr->dev, bucket_nr, b_gen,
627-
bch2_data_types[bucket_data_type],
628-
bch2_data_types[ptr_data_type],
627+
bch2_data_type_str(bucket_data_type),
628+
bch2_data_type_str(ptr_data_type),
629629
(printbuf_reset(&buf),
630630
bch2_bkey_val_to_text(&buf, c, k), buf.buf));
631631
ret = -EIO;
@@ -638,7 +638,7 @@ int bch2_check_bucket_ref(struct btree_trans *trans,
638638
"bucket %u:%zu gen %u data type %s sector count overflow: %u + %lli > U32_MAX\n"
639639
"while marking %s",
640640
ptr->dev, bucket_nr, b_gen,
641-
bch2_data_types[bucket_data_type ?: ptr_data_type],
641+
bch2_data_type_str(bucket_data_type ?: ptr_data_type),
642642
bucket_sectors, sectors,
643643
(printbuf_reset(&buf),
644644
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,
11301130
"bucket %llu:%llu gen %u different types of data in same bucket: %s, %s\n"
11311131
"while marking %s",
11321132
iter.pos.inode, iter.pos.offset, a->v.gen,
1133-
bch2_data_types[a->v.data_type],
1134-
bch2_data_types[type],
1135-
bch2_data_types[type]);
1133+
bch2_data_type_str(a->v.data_type),
1134+
bch2_data_type_str(type),
1135+
bch2_data_type_str(type));
11361136
ret = -EIO;
11371137
goto err;
11381138
}

fs/bcachefs/buckets.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,21 @@ static inline bool is_superblock_bucket(struct bch_dev *ca, u64 b)
385385
return false;
386386
}
387387

388+
static inline const char *bch2_data_type_str(enum bch_data_type type)
389+
{
390+
return type < BCH_DATA_NR
391+
? __bch2_data_types[type]
392+
: "(invalid data type)";
393+
}
394+
395+
static inline void bch2_prt_data_type(struct printbuf *out, enum bch_data_type type)
396+
{
397+
if (type < BCH_DATA_NR)
398+
prt_str(out, __bch2_data_types[type]);
399+
else
400+
prt_printf(out, "(invalid data type %u)", type);
401+
}
402+
388403
/* disk reservations: */
389404

390405
static inline void bch2_disk_reservation_put(struct bch_fs *c,

fs/bcachefs/ec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
190190
a->v.stripe_redundancy, trans,
191191
"bucket %llu:%llu gen %u data type %s dirty_sectors %u: multiple stripes using same bucket (%u, %llu)",
192192
iter.pos.inode, iter.pos.offset, a->v.gen,
193-
bch2_data_types[a->v.data_type],
193+
bch2_data_type_str(a->v.data_type),
194194
a->v.dirty_sectors,
195195
a->v.stripe, s.k->p.offset)) {
196196
ret = -EIO;
@@ -200,7 +200,7 @@ static int bch2_trans_mark_stripe_bucket(struct btree_trans *trans,
200200
if (bch2_trans_inconsistent_on(data_type && a->v.dirty_sectors, trans,
201201
"bucket %llu:%llu gen %u data type %s dirty_sectors %u: data already in stripe bucket %llu",
202202
iter.pos.inode, iter.pos.offset, a->v.gen,
203-
bch2_data_types[a->v.data_type],
203+
bch2_data_type_str(a->v.data_type),
204204
a->v.dirty_sectors,
205205
s.k->p.offset)) {
206206
ret = -EIO;

fs/bcachefs/journal_io.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,10 +683,7 @@ static void journal_entry_dev_usage_to_text(struct printbuf *out, struct bch_fs
683683
prt_printf(out, "dev=%u", le32_to_cpu(u->dev));
684684

685685
for (i = 0; i < nr_types; i++) {
686-
if (i < BCH_DATA_NR)
687-
prt_printf(out, " %s", bch2_data_types[i]);
688-
else
689-
prt_printf(out, " (unknown data type %u)", i);
686+
bch2_prt_data_type(out, i);
690687
prt_printf(out, ": buckets=%llu sectors=%llu fragmented=%llu",
691688
le64_to_cpu(u->d[i].buckets),
692689
le64_to_cpu(u->d[i].sectors),

fs/bcachefs/move.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,9 @@ int bch2_data_job(struct bch_fs *c,
10831083

10841084
void bch2_move_stats_to_text(struct printbuf *out, struct bch_move_stats *stats)
10851085
{
1086-
prt_printf(out, "%s: data type=%s pos=",
1087-
stats->name,
1088-
bch2_data_types[stats->data_type]);
1086+
prt_printf(out, "%s: data type==", stats->name);
1087+
bch2_prt_data_type(out, stats->data_type);
1088+
prt_str(out, " pos=");
10891089
bch2_bbpos_to_text(out, stats->pos);
10901090
prt_newline(out);
10911091
printbuf_indent_add(out, 2);

fs/bcachefs/opts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const char * const bch2_str_hash_opts[] = {
7272
NULL
7373
};
7474

75-
const char * const bch2_data_types[] = {
75+
const char * const __bch2_data_types[] = {
7676
BCH_DATA_TYPES()
7777
NULL
7878
};

fs/bcachefs/opts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern const char * const bch2_compression_types[];
2222
extern const char * const bch2_compression_opts[];
2323
extern const char * const bch2_str_hash_types[];
2424
extern const char * const bch2_str_hash_opts[];
25-
extern const char * const bch2_data_types[];
25+
extern const char * const __bch2_data_types[];
2626
extern const char * const bch2_member_states[];
2727
extern const char * const bch2_jset_entry_types[];
2828
extern const char * const bch2_fs_usage_types[];

0 commit comments

Comments
 (0)