Skip to content

Commit

Permalink
bcachefs: Plumb bkey_validate_context to journal_entry_validate
Browse files Browse the repository at this point in the history
This lets us print the exact location in the journal if it was found in
the journal, or correctly print if it was found in the superblock.

Signed-off-by: Kent Overstreet <[email protected]>
  • Loading branch information
Kent Overstreet committed Dec 8, 2024
1 parent be12880 commit f1d736e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 84 deletions.
12 changes: 7 additions & 5 deletions fs/bcachefs/bkey_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,27 +213,29 @@ BCH_BKEY_TYPES();
enum bch_validate_flags {
BCH_VALIDATE_write = BIT(0),
BCH_VALIDATE_commit = BIT(1),
BCH_VALIDATE_journal = BIT(2),
BCH_VALIDATE_silent = BIT(3),
BCH_VALIDATE_silent = BIT(2),
};

#define BKEY_VALIDATE_CONTEXTS() \
x(unknown) \
x(commit) \
x(superblock) \
x(journal) \
x(btree_root) \
x(btree_node)
x(btree_node) \
x(commit)

struct bkey_validate_context {
enum {
#define x(n) BKEY_VALIDATE_##n,
BKEY_VALIDATE_CONTEXTS()
#undef x
} from:8;
enum bch_validate_flags flags:8;
u8 level;
enum btree_id btree;
bool root:1;
enum bch_validate_flags flags:8;
unsigned journal_offset;
u64 journal_seq;
};

#endif /* _BCACHEFS_BKEY_TYPES_H */
44 changes: 18 additions & 26 deletions fs/bcachefs/btree_trans_commit.c
Original file line number Diff line number Diff line change
Expand Up @@ -719,45 +719,37 @@ bch2_trans_commit_write_locked(struct btree_trans *trans, unsigned flags,
goto fatal_err;
}

trans_for_each_update(trans, i) {
enum bch_validate_flags invalid_flags = 0;

if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
invalid_flags |= BCH_VALIDATE_write|BCH_VALIDATE_commit;

ret = bch2_bkey_validate(c, bkey_i_to_s_c(i->k),
(struct bkey_validate_context) {
.from = BKEY_VALIDATE_commit,
.level = i->level,
.btree = i->btree_id,
.flags = invalid_flags,
});
if (unlikely(ret)){
bch2_trans_inconsistent(trans, "invalid bkey on insert from %s -> %ps\n",
trans->fn, (void *) i->ip_allocated);
goto fatal_err;
}
btree_insert_entry_checks(trans, i);
}
struct bkey_validate_context validate_context = { .from = BKEY_VALIDATE_commit };

if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
validate_context.flags = BCH_VALIDATE_write|BCH_VALIDATE_commit;

for (struct jset_entry *i = trans->journal_entries;
i != (void *) ((u64 *) trans->journal_entries + trans->journal_entries_u64s);
i = vstruct_next(i)) {
enum bch_validate_flags invalid_flags = 0;

if (!(flags & BCH_TRANS_COMMIT_no_journal_res))
invalid_flags |= BCH_VALIDATE_write|BCH_VALIDATE_commit;

ret = bch2_journal_entry_validate(c, NULL, i,
bcachefs_metadata_version_current,
CPU_BIG_ENDIAN, invalid_flags);
CPU_BIG_ENDIAN, validate_context);
if (unlikely(ret)) {
bch2_trans_inconsistent(trans, "invalid journal entry on insert from %s\n",
trans->fn);
goto fatal_err;
}
}

trans_for_each_update(trans, i) {
validate_context.level = i->level;
validate_context.btree = i->btree_id;

ret = bch2_bkey_validate(c, bkey_i_to_s_c(i->k), validate_context);
if (unlikely(ret)){
bch2_trans_inconsistent(trans, "invalid bkey on insert from %s -> %ps\n",
trans->fn, (void *) i->ip_allocated);
goto fatal_err;
}
btree_insert_entry_checks(trans, i);
}

if (likely(!(flags & BCH_TRANS_COMMIT_no_journal_res))) {
struct journal *j = &c->journal;
struct jset_entry *entry;
Expand Down
9 changes: 7 additions & 2 deletions fs/bcachefs/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,14 @@ int __bch2_bkey_fsck_err(struct bch_fs *c,
fsck_flags |= fsck_flags_extra[err];

struct printbuf buf = PRINTBUF;

prt_printf(&buf, "invalid bkey in %s btree=",
prt_printf(&buf, "invalid bkey in %s",
bch2_bkey_validate_contexts[from.from]);

if (from.from == BKEY_VALIDATE_journal)
prt_printf(&buf, " journal seq=%llu offset=%u",
from.journal_seq, from.journal_offset);

prt_str(&buf, " btree=");
bch2_btree_id_to_text(&buf, from.btree);
prt_printf(&buf, " level=%u: ", from.level);

Expand Down
13 changes: 6 additions & 7 deletions fs/bcachefs/extents.c
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,12 @@ static int extent_ptr_validate(struct bch_fs *c,
{
int ret = 0;

struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
bkey_for_each_ptr(ptrs, ptr2)
bkey_fsck_err_on(ptr != ptr2 && ptr->dev == ptr2->dev,
c, ptr_to_duplicate_device,
"multiple pointers to same device (%u)", ptr->dev);

/* bad pointers are repaired by check_fix_ptrs(): */
rcu_read_lock();
struct bch_dev *ca = bch2_dev_rcu_noerror(c, ptr->dev);
Expand All @@ -1252,13 +1258,6 @@ static int extent_ptr_validate(struct bch_fs *c,
unsigned bucket_size = ca->mi.bucket_size;
rcu_read_unlock();

struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
bkey_for_each_ptr(ptrs, ptr2)
bkey_fsck_err_on(ptr != ptr2 && ptr->dev == ptr2->dev,
c, ptr_to_duplicate_device,
"multiple pointers to same device (%u)", ptr->dev);


bkey_fsck_err_on(bucket >= nbuckets,
c, ptr_after_last_bucket,
"pointer past last bucket (%llu > %llu)", bucket, nbuckets);
Expand Down
Loading

0 comments on commit f1d736e

Please sign in to comment.