Skip to content

Commit 9c893fa

Browse files
author
Kent Overstreet
committed
bcachefs: Validate number of counters for accounting keys
We weren't checking that accounting keys have the expected number of accounters. Originally we probably wanted to be flexible on this, but it doesn't look like that will be required - accounting is extended by adding new counter types, not more counters to an existing type. This means we can drop a BUG_ON() that popped once in automated testing, and the new validation will make that bug easier to track down. Signed-off-by: Kent Overstreet <[email protected]>
1 parent e1e50a6 commit 9c893fa

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

fs/bcachefs/disk_accounting.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ static inline bool is_zero(char *start, char *end)
135135

136136
#define field_end(p, member) (((void *) (&p.member)) + sizeof(p.member))
137137

138+
static const unsigned bch2_accounting_type_nr_counters[] = {
139+
#define x(f, id, nr) [BCH_DISK_ACCOUNTING_##f] = nr,
140+
BCH_DISK_ACCOUNTING_TYPES()
141+
#undef x
142+
};
143+
138144
int bch2_accounting_validate(struct bch_fs *c, struct bkey_s_c k,
139145
struct bkey_validate_context from)
140146
{
@@ -193,6 +199,11 @@ int bch2_accounting_validate(struct bch_fs *c, struct bkey_s_c k,
193199
bkey_fsck_err_on(!is_zero(end, (void *) (&acc_k + 1)),
194200
c, accounting_key_junk_at_end,
195201
"junk at end of accounting key");
202+
203+
bkey_fsck_err_on(bch2_accounting_counters(k.k) != bch2_accounting_type_nr_counters[acc_k.type],
204+
c, accounting_key_nr_counters_wrong,
205+
"accounting key with %u counters, should be %u",
206+
bch2_accounting_counters(k.k), bch2_accounting_type_nr_counters[acc_k.type]);
196207
fsck_err:
197208
return ret;
198209
}

fs/bcachefs/disk_accounting.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ static inline bool bch2_accounting_key_is_zero(struct bkey_s_c_accounting a)
3333
static inline void bch2_accounting_accumulate(struct bkey_i_accounting *dst,
3434
struct bkey_s_c_accounting src)
3535
{
36-
EBUG_ON(dst->k.u64s != src.k->u64s);
37-
38-
for (unsigned i = 0; i < bch2_accounting_counters(&dst->k); i++)
36+
for (unsigned i = 0;
37+
i < min(bch2_accounting_counters(&dst->k),
38+
bch2_accounting_counters(src.k));
39+
i++)
3940
dst->v.d[i] += src.v->d[i];
41+
4042
if (bversion_cmp(dst->k.bversion, src.k->bversion) < 0)
4143
dst->k.bversion = src.k->bversion;
4244
}

fs/bcachefs/disk_accounting_format.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,25 @@ static inline bool data_type_is_hidden(enum bch_data_type type)
9595
}
9696
}
9797

98+
/*
99+
* field 1: name
100+
* field 2: id
101+
* field 3: number of counters (max 3)
102+
*/
103+
98104
#define BCH_DISK_ACCOUNTING_TYPES() \
99-
x(nr_inodes, 0) \
100-
x(persistent_reserved, 1) \
101-
x(replicas, 2) \
102-
x(dev_data_type, 3) \
103-
x(compression, 4) \
104-
x(snapshot, 5) \
105-
x(btree, 6) \
106-
x(rebalance_work, 7) \
107-
x(inum, 8)
105+
x(nr_inodes, 0, 1) \
106+
x(persistent_reserved, 1, 1) \
107+
x(replicas, 2, 1) \
108+
x(dev_data_type, 3, 3) \
109+
x(compression, 4, 3) \
110+
x(snapshot, 5, 1) \
111+
x(btree, 6, 1) \
112+
x(rebalance_work, 7, 1) \
113+
x(inum, 8, 3)
108114

109115
enum disk_accounting_type {
110-
#define x(f, nr) BCH_DISK_ACCOUNTING_##f = nr,
116+
#define x(f, nr, ...) BCH_DISK_ACCOUNTING_##f = nr,
111117
BCH_DISK_ACCOUNTING_TYPES()
112118
#undef x
113119
BCH_DISK_ACCOUNTING_TYPE_NR,

fs/bcachefs/sb-errors_format.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,14 @@ enum bch_fsck_flags {
311311
x(accounting_key_replicas_nr_required_bad, 279, FSCK_AUTOFIX) \
312312
x(accounting_key_replicas_devs_unsorted, 280, FSCK_AUTOFIX) \
313313
x(accounting_key_version_0, 282, FSCK_AUTOFIX) \
314+
x(accounting_key_nr_counters_wrong, 307, FSCK_AUTOFIX) \
314315
x(logged_op_but_clean, 283, FSCK_AUTOFIX) \
315316
x(compression_opt_not_marked_in_sb, 295, FSCK_AUTOFIX) \
316317
x(compression_type_not_marked_in_sb, 296, FSCK_AUTOFIX) \
317318
x(directory_size_mismatch, 303, FSCK_AUTOFIX) \
318319
x(dirent_cf_name_too_big, 304, 0) \
319320
x(dirent_stray_data_after_cf_name, 305, 0) \
320-
x(MAX, 307, 0)
321+
x(MAX, 308, 0)
321322

322323
enum bch_sb_error_id {
323324
#define x(t, n, ...) BCH_FSCK_ERR_##t = n,

0 commit comments

Comments
 (0)