1

bcachefs: bch2_accounting_invalid()

Implement bch2_accounting_invalid(); check for junk at the end, and
replicas accounting entries in particular need to be checked or we'll
pop asserts later.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-08-08 23:19:59 -04:00
parent f39bae2e02
commit 077e473723
4 changed files with 73 additions and 8 deletions

View File

@ -114,11 +114,74 @@ int bch2_mod_dev_cached_sectors(struct btree_trans *trans,
return bch2_disk_accounting_mod(trans, &acc, &sectors, 1, gc);
}
static inline bool is_zero(char *start, char *end)
{
BUG_ON(start > end);
for (; start < end; start++)
if (*start)
return false;
return true;
}
#define field_end(p, member) (((void *) (&p.member)) + sizeof(p.member))
int bch2_accounting_invalid(struct bch_fs *c, struct bkey_s_c k,
enum bch_validate_flags flags,
struct printbuf *err)
{
return 0;
struct disk_accounting_pos acc_k;
bpos_to_disk_accounting_pos(&acc_k, k.k->p);
void *end = &acc_k + 1;
int ret = 0;
switch (acc_k.type) {
case BCH_DISK_ACCOUNTING_nr_inodes:
end = field_end(acc_k, nr_inodes);
break;
case BCH_DISK_ACCOUNTING_persistent_reserved:
end = field_end(acc_k, persistent_reserved);
break;
case BCH_DISK_ACCOUNTING_replicas:
bkey_fsck_err_on(!acc_k.replicas.nr_devs,
c, err, accounting_key_replicas_nr_devs_0,
"accounting key replicas entry with nr_devs=0");
bkey_fsck_err_on(acc_k.replicas.nr_required > acc_k.replicas.nr_devs ||
(acc_k.replicas.nr_required > 1 &&
acc_k.replicas.nr_required == acc_k.replicas.nr_devs),
c, err, accounting_key_replicas_nr_required_bad,
"accounting key replicas entry with bad nr_required");
for (unsigned i = 0; i + 1 < acc_k.replicas.nr_devs; i++)
bkey_fsck_err_on(acc_k.replicas.devs[i] > acc_k.replicas.devs[i + 1],
c, err, accounting_key_replicas_devs_unsorted,
"accounting key replicas entry with unsorted devs");
end = (void *) &acc_k.replicas + replicas_entry_bytes(&acc_k.replicas);
break;
case BCH_DISK_ACCOUNTING_dev_data_type:
end = field_end(acc_k, dev_data_type);
break;
case BCH_DISK_ACCOUNTING_compression:
end = field_end(acc_k, compression);
break;
case BCH_DISK_ACCOUNTING_snapshot:
end = field_end(acc_k, snapshot);
break;
case BCH_DISK_ACCOUNTING_btree:
end = field_end(acc_k, btree);
break;
case BCH_DISK_ACCOUNTING_rebalance_work:
end = field_end(acc_k, rebalance_work);
break;
}
bkey_fsck_err_on(!is_zero(end, (void *) (&acc_k + 1)),
c, err, accounting_key_junk_at_end,
"junk at end of accounting key");
fsck_err:
return ret;
}
void bch2_accounting_key_to_text(struct printbuf *out, struct disk_accounting_pos *k)

View File

@ -124,10 +124,6 @@ struct bch_dev_data_type {
__u8 data_type;
};
struct bch_dev_stripe_buckets {
__u8 dev;
};
struct bch_acct_compression {
__u8 type;
};
@ -140,6 +136,9 @@ struct bch_acct_btree {
__u32 id;
};
struct bch_acct_rebalance_work {
};
struct disk_accounting_pos {
union {
struct {
@ -149,10 +148,10 @@ struct disk_accounting_pos {
struct bch_persistent_reserved persistent_reserved;
struct bch_replicas_entry_v1 replicas;
struct bch_dev_data_type dev_data_type;
struct bch_dev_stripe_buckets dev_stripe_buckets;
struct bch_acct_compression compression;
struct bch_acct_snapshot snapshot;
struct bch_acct_btree btree;
struct bch_acct_rebalance_work rebalance_work;
};
};
struct bpos _pad;

View File

@ -24,7 +24,6 @@ static int bch2_memcmp(const void *l, const void *r, const void *priv)
static void verify_replicas_entry(struct bch_replicas_entry_v1 *e)
{
#ifdef CONFIG_BCACHEFS_DEBUG
BUG_ON(e->data_type >= BCH_DATA_NR);
BUG_ON(!e->nr_devs);
BUG_ON(e->nr_required > 1 &&
e->nr_required >= e->nr_devs);

View File

@ -287,7 +287,11 @@ enum bch_fsck_flags {
x(accounting_replicas_not_marked, 273, 0) \
x(invalid_btree_id, 274, 0) \
x(alloc_key_io_time_bad, 275, 0) \
x(alloc_key_fragmentation_lru_wrong, 276, FSCK_AUTOFIX)
x(alloc_key_fragmentation_lru_wrong, 276, FSCK_AUTOFIX) \
x(accounting_key_junk_at_end, 277, 0) \
x(accounting_key_replicas_nr_devs_0, 278, 0) \
x(accounting_key_replicas_nr_required_bad, 279, 0) \
x(accounting_key_replicas_devs_unsorted, 280, 0) \
enum bch_sb_error_id {
#define x(t, n, ...) BCH_FSCK_ERR_##t = n,