Commit aae15aaf authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: New and improved topology repair code

This splits out btree topology repair into a separate pass, and makes
some improvements:
 - When we have to pick which of two overlapping nodes to drop keys
   from, we use the btree node header sequence number to preserve the
   newer node

 - the gc code has been changed so that it doesn't bail out if we're
   continuing/ignoring on fsck error - this way the dump tool can skip
   running the repair pass but still walk all reachable metadata

 - add a new superblock flag indicating when a filesystem is known to
   have btree topology issues, and the topology repair pass should be
   run

 - changing the start/end of a node might mean keys in that node have to
   be deleted: this patch handles that better by splitting it out into a
   separate function and running it explicitly in the topology repair
   code, previously those keys were only being dropped when the btree
   node was read in.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 4932e07e
......@@ -485,6 +485,7 @@ enum {
BCH_FS_ALLOCATOR_RUNNING,
BCH_FS_ALLOCATOR_STOPPING,
BCH_FS_INITIAL_GC_DONE,
BCH_FS_INITIAL_GC_UNFIXED,
BCH_FS_BTREE_INTERIOR_REPLAY_DONE,
BCH_FS_FSCK_DONE,
BCH_FS_STARTED,
......@@ -498,7 +499,9 @@ enum {
/* errors: */
BCH_FS_ERROR,
BCH_FS_TOPOLOGY_ERROR,
BCH_FS_ERRORS_FIXED,
BCH_FS_ERRORS_NOT_FIXED,
/* misc: */
BCH_FS_NEED_ANOTHER_GC,
......
......@@ -1317,12 +1317,10 @@ LE64_BITMASK(BCH_SB_GRPQUOTA, struct bch_sb, flags[0], 58, 59);
LE64_BITMASK(BCH_SB_PRJQUOTA, struct bch_sb, flags[0], 59, 60);
LE64_BITMASK(BCH_SB_HAS_ERRORS, struct bch_sb, flags[0], 60, 61);
LE64_BITMASK(BCH_SB_HAS_TOPOLOGY_ERRORS,struct bch_sb, flags[0], 61, 62);
/* bit 61 was reflink option */
LE64_BITMASK(BCH_SB_BIG_ENDIAN, struct bch_sb, flags[0], 62, 63);
/* 61-64 unused */
LE64_BITMASK(BCH_SB_STR_HASH_TYPE, struct bch_sb, flags[1], 0, 4);
LE64_BITMASK(BCH_SB_COMPRESSION_TYPE, struct bch_sb, flags[1], 4, 8);
LE64_BITMASK(BCH_SB_INODE_32BIT, struct bch_sb, flags[1], 8, 9);
......
This diff is collapsed.
......@@ -558,6 +558,55 @@ out: \
#define btree_err_on(cond, ...) ((cond) ? btree_err(__VA_ARGS__) : false)
/*
* When btree topology repair changes the start or end of a node, that might
* mean we have to drop keys that are no longer inside the node:
*/
__cold
void bch2_btree_node_drop_keys_outside_node(struct btree *b)
{
struct bset_tree *t;
struct bkey_s_c k;
struct bkey unpacked;
struct btree_node_iter iter;
for_each_bset(b, t) {
struct bset *i = bset(b, t);
struct bkey_packed *k;
for (k = i->start; k != vstruct_last(i); k = bkey_next(k))
if (bkey_cmp_left_packed(b, k, &b->data->min_key) >= 0)
break;
if (k != i->start) {
unsigned shift = (u64 *) k - (u64 *) i->start;
memmove_u64s_down(i->start, k,
(u64 *) vstruct_end(i) - (u64 *) k);
i->u64s = cpu_to_le16(le16_to_cpu(i->u64s) - shift);
set_btree_bset_end(b, t);
bch2_bset_set_no_aux_tree(b, t);
}
for (k = i->start; k != vstruct_last(i); k = bkey_next(k))
if (bkey_cmp_left_packed(b, k, &b->data->max_key) > 0)
break;
if (k != vstruct_last(i)) {
i->u64s = cpu_to_le16((u64 *) k - (u64 *) i->start);
set_btree_bset_end(b, t);
bch2_bset_set_no_aux_tree(b, t);
}
}
bch2_btree_build_aux_trees(b);
for_each_btree_node_key_unpack(b, k, &iter, &unpacked) {
BUG_ON(bpos_cmp(k.k->p, b->data->min_key) < 0);
BUG_ON(bpos_cmp(k.k->p, b->data->max_key) > 0);
}
}
static int validate_bset(struct bch_fs *c, struct bch_dev *ca,
struct btree *b, struct bset *i,
unsigned sectors, int write, bool have_retry)
......@@ -680,6 +729,8 @@ static int validate_bset_keys(struct bch_fs *c, struct btree *b,
{
unsigned version = le16_to_cpu(i->version);
struct bkey_packed *k, *prev = NULL;
bool updated_range = b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
BTREE_PTR_RANGE_UPDATED(&bkey_i_to_btree_ptr_v2(&b->key)->v);
int ret = 0;
for (k = i->start;
......@@ -713,7 +764,7 @@ static int validate_bset_keys(struct bch_fs *c, struct btree *b,
u = __bkey_disassemble(b, k, &tmp);
invalid = __bch2_bkey_invalid(c, u.s_c, btree_node_type(b)) ?:
bch2_bkey_in_btree_node(b, u.s_c) ?:
(!updated_range ? bch2_bkey_in_btree_node(b, u.s_c) : NULL) ?:
(write ? bch2_bkey_val_invalid(c, u.s_c) : NULL);
if (invalid) {
char buf[160];
......@@ -770,6 +821,8 @@ int bch2_btree_node_read_done(struct bch_fs *c, struct bch_dev *ca,
struct bch_extent_ptr *ptr;
struct bset *i;
bool used_mempool, blacklisted;
bool updated_range = b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
BTREE_PTR_RANGE_UPDATED(&bkey_i_to_btree_ptr_v2(&b->key)->v);
unsigned u64s;
int ret, retry_read = 0, write = READ;
......@@ -917,6 +970,9 @@ int bch2_btree_node_read_done(struct bch_fs *c, struct bch_dev *ca,
btree_bounce_free(c, btree_bytes(c), used_mempool, sorted);
if (updated_range)
bch2_btree_node_drop_keys_outside_node(b);
i = &b->data->keys;
for (k = i->start; k != vstruct_last(i);) {
struct bkey tmp;
......
......@@ -131,6 +131,8 @@ static inline void bset_encrypt(struct bch_fs *c, struct bset *i, unsigned offse
void bch2_btree_sort_into(struct bch_fs *, struct btree *, struct btree *);
void bch2_btree_node_drop_keys_outside_node(struct btree *);
void bch2_btree_build_aux_trees(struct btree *);
void bch2_btree_init_next(struct bch_fs *, struct btree *,
struct btree_iter *);
......
......@@ -1609,11 +1609,12 @@ int __bch2_foreground_maybe_merge(struct bch_fs *c,
bch2_bpos_to_text(&PBUF(buf1), prev->data->max_key);
bch2_bpos_to_text(&PBUF(buf2), next->data->min_key);
bch2_fs_inconsistent(c,
"btree topology error in btree merge:\n"
"prev ends at %s\n"
"next starts at %s\n",
buf1, buf2);
bch_err(c,
"btree topology error in btree merge:\n"
" prev ends at %s\n"
" next starts at %s",
buf1, buf2);
bch2_topology_error(c);
ret = -EIO;
goto err;
}
......
......@@ -25,6 +25,13 @@ bool bch2_inconsistent_error(struct bch_fs *c)
}
}
void bch2_topology_error(struct bch_fs *c)
{
set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags);
if (test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
bch2_inconsistent_error(c);
}
void bch2_fatal_error(struct bch_fs *c)
{
if (bch2_fs_emergency_read_only(c))
......@@ -74,9 +81,13 @@ enum fsck_err_ret bch2_fsck_err(struct bch_fs *c, unsigned flags,
vprintk(fmt, args);
va_end(args);
return bch2_inconsistent_error(c)
? FSCK_ERR_EXIT
: FSCK_ERR_FIX;
if (c->opts.errors == BCH_ON_ERROR_continue) {
bch_err(c, "fixing");
return FSCK_ERR_FIX;
} else {
bch2_inconsistent_error(c);
return FSCK_ERR_EXIT;
}
}
mutex_lock(&c->fsck_error_lock);
......@@ -146,6 +157,7 @@ enum fsck_err_ret bch2_fsck_err(struct bch_fs *c, unsigned flags,
set_bit(BCH_FS_ERRORS_FIXED, &c->flags);
return FSCK_ERR_FIX;
} else {
set_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags);
set_bit(BCH_FS_ERROR, &c->flags);
return c->opts.fix_errors == FSCK_OPT_EXIT ||
!(flags & FSCK_CAN_IGNORE)
......
......@@ -29,6 +29,8 @@ struct work_struct;
bool bch2_inconsistent_error(struct bch_fs *);
void bch2_topology_error(struct bch_fs *);
#define bch2_fs_inconsistent(c, ...) \
({ \
bch_err(c, __VA_ARGS__); \
......@@ -88,6 +90,7 @@ enum fsck_err_ret {
FSCK_ERR_IGNORE = 0,
FSCK_ERR_FIX = 1,
FSCK_ERR_EXIT = 2,
FSCK_ERR_START_TOPOLOGY_REPAIR = 3,
};
struct fsck_err_state {
......
......@@ -1241,8 +1241,9 @@ int bch2_fs_recovery(struct bch_fs *c)
if (c->opts.fsck &&
!test_bit(BCH_FS_ERROR, &c->flags) &&
BCH_SB_HAS_ERRORS(c->disk_sb.sb)) {
!test_bit(BCH_FS_ERRORS_NOT_FIXED, &c->flags)) {
SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 0);
SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 0);
write_sb = true;
}
......
......@@ -439,6 +439,11 @@ int bch2_sb_to_fs(struct bch_fs *c, struct bch_sb *src)
__copy_super(&c->disk_sb, src);
if (BCH_SB_HAS_ERRORS(c->disk_sb.sb))
set_bit(BCH_FS_ERROR, &c->flags);
if (BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb))
set_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags);
ret = bch2_sb_replicas_to_cpu_replicas(c);
if (ret)
return ret;
......@@ -715,6 +720,8 @@ int bch2_write_super(struct bch_fs *c)
if (test_bit(BCH_FS_ERROR, &c->flags))
SET_BCH_SB_HAS_ERRORS(c->disk_sb.sb, 1);
if (test_bit(BCH_FS_TOPOLOGY_ERROR, &c->flags))
SET_BCH_SB_HAS_TOPOLOGY_ERRORS(c->disk_sb.sb, 1);
SET_BCH_SB_BIG_ENDIAN(c->disk_sb.sb, CPU_BIG_ENDIAN);
......
......@@ -388,6 +388,11 @@ static int __bch2_fs_read_write(struct bch_fs *c, bool early)
unsigned i;
int ret;
if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
bch_err(c, "cannot go rw, unfixed btree errors");
return -EROFS;
}
if (test_bit(BCH_FS_RW, &c->flags))
return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment