summaryrefslogtreecommitdiff
path: root/fs/bcachefs/btree_gc.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/bcachefs/btree_gc.c')
-rw-r--r--fs/bcachefs/btree_gc.c197
1 files changed, 48 insertions, 149 deletions
diff --git a/fs/bcachefs/btree_gc.c b/fs/bcachefs/btree_gc.c
index a849f9e320b3..85fc181e76a8 100644
--- a/fs/bcachefs/btree_gc.c
+++ b/fs/bcachefs/btree_gc.c
@@ -112,137 +112,11 @@ static void btree_node_range_checks(struct bch_fs *c, struct btree *b,
/* marking of btree keys/nodes: */
-static void ptr_gen_recalc_oldest(struct bch_fs *c,
- const struct bch_extent_ptr *ptr,
- u8 *max_stale)
-{
- struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
- size_t b = PTR_BUCKET_NR(ca, ptr);
-
- if (gen_after(ca->oldest_gens[b], ptr->gen))
- ca->oldest_gens[b] = ptr->gen;
-
- *max_stale = max(*max_stale, ptr_stale(ca, ptr));
-}
-
-static void ptr_gens_recalc_oldest(struct bch_fs *c, enum bkey_type type,
- struct bkey_s_c k, u8 *max_stale)
-{
- const struct bch_extent_ptr *ptr;
-
- switch (type) {
- case BKEY_TYPE_BTREE:
- case BKEY_TYPE_EXTENTS:
- switch (k.k->type) {
- case BCH_EXTENT:
- case BCH_EXTENT_CACHED: {
- struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
-
- extent_for_each_ptr(e, ptr)
- ptr_gen_recalc_oldest(c, ptr, max_stale);
- break;
- }
- }
- break;
- case BKEY_TYPE_EC:
- switch (k.k->type) {
- case BCH_STRIPE: {
- struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
-
- for (ptr = s.v->ptrs;
- ptr < s.v->ptrs + s.v->nr_blocks;
- ptr++)
- ptr_gen_recalc_oldest(c, ptr, max_stale);
- }
- }
- default:
- break;
- }
-}
-
-static int ptr_gen_check(struct bch_fs *c,
- enum bkey_type type,
- const struct bch_extent_ptr *ptr)
-{
- struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
- size_t b = PTR_BUCKET_NR(ca, ptr);
- struct bucket *g = PTR_BUCKET(ca, ptr);
- int ret = 0;
-
- if (mustfix_fsck_err_on(!g->mark.gen_valid, c,
- "found ptr with missing gen in alloc btree,\n"
- "type %u gen %u",
- type, ptr->gen)) {
- g->_mark.gen = ptr->gen;
- g->_mark.gen_valid = 1;
- set_bit(b, ca->buckets_dirty);
- }
-
- if (mustfix_fsck_err_on(gen_cmp(ptr->gen, g->mark.gen) > 0, c,
- "%u ptr gen in the future: %u > %u",
- type, ptr->gen, g->mark.gen)) {
- g->_mark.gen = ptr->gen;
- g->_mark.gen_valid = 1;
- set_bit(b, ca->buckets_dirty);
- set_bit(BCH_FS_FIXED_GENS, &c->flags);
- }
-fsck_err:
- return ret;
-}
-
-static int ptr_gens_check(struct bch_fs *c, enum bkey_type type,
- struct bkey_s_c k)
-{
- const struct bch_extent_ptr *ptr;
- int ret = 0;
-
- switch (type) {
- case BKEY_TYPE_BTREE:
- case BKEY_TYPE_EXTENTS:
- switch (k.k->type) {
- case BCH_EXTENT:
- case BCH_EXTENT_CACHED: {
- struct bkey_s_c_extent e = bkey_s_c_to_extent(k);
-
- extent_for_each_ptr(e, ptr) {
- ret = ptr_gen_check(c, type, ptr);
- if (ret)
- return ret;
-
- }
- break;
- }
- }
- break;
- case BKEY_TYPE_EC:
- switch (k.k->type) {
- case BCH_STRIPE: {
- struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
-
- for (ptr = s.v->ptrs;
- ptr < s.v->ptrs + s.v->nr_blocks;
- ptr++) {
- ret = ptr_gen_check(c, type, ptr);
- if (ret)
- return ret;
- }
- }
- }
- break;
- default:
- break;
- }
-
- return ret;
-}
-
-/*
- * For runtime mark and sweep:
- */
-static int bch2_gc_mark_key(struct bch_fs *c, enum bkey_type type,
- struct bkey_s_c k,
+static int bch2_gc_mark_key(struct bch_fs *c, struct bkey_s_c k,
u8 *max_stale, bool initial)
{
+ struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
+ const struct bch_extent_ptr *ptr;
struct gc_pos pos = { 0 };
unsigned flags =
BCH_BUCKET_MARK_GC|
@@ -257,23 +131,50 @@ static int bch2_gc_mark_key(struct bch_fs *c, enum bkey_type type,
atomic64_set(&c->key_version, k.k->version.lo);
if (test_bit(BCH_FS_REBUILD_REPLICAS, &c->flags) ||
- fsck_err_on(!bch2_bkey_replicas_marked(c, type, k,
- false), c,
+ fsck_err_on(!bch2_bkey_replicas_marked(c, k, false), c,
"superblock not marked as containing replicas (type %u)",
- type)) {
- ret = bch2_mark_bkey_replicas(c, type, k);
+ k.k->type)) {
+ ret = bch2_mark_bkey_replicas(c, k);
if (ret)
return ret;
}
- ret = ptr_gens_check(c, type, k);
- if (ret)
- return ret;
+ bkey_for_each_ptr(ptrs, ptr) {
+ struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
+ size_t b = PTR_BUCKET_NR(ca, ptr);
+ struct bucket *g = PTR_BUCKET(ca, ptr);
+
+ if (mustfix_fsck_err_on(!g->mark.gen_valid, c,
+ "found ptr with missing gen in alloc btree,\n"
+ "type %u gen %u",
+ k.k->type, ptr->gen)) {
+ g->_mark.gen = ptr->gen;
+ g->_mark.gen_valid = 1;
+ set_bit(b, ca->buckets_dirty);
+ }
+
+ if (mustfix_fsck_err_on(gen_cmp(ptr->gen, g->mark.gen) > 0, c,
+ "%u ptr gen in the future: %u > %u",
+ k.k->type, ptr->gen, g->mark.gen)) {
+ g->_mark.gen = ptr->gen;
+ g->_mark.gen_valid = 1;
+ set_bit(b, ca->buckets_dirty);
+ set_bit(BCH_FS_FIXED_GENS, &c->flags);
+ }
+ }
}
- bch2_mark_key(c, type, k, true, k.k->size, pos, NULL, 0, flags);
+ bkey_for_each_ptr(ptrs, ptr) {
+ struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
+ size_t b = PTR_BUCKET_NR(ca, ptr);
+
+ if (gen_after(ca->oldest_gens[b], ptr->gen))
+ ca->oldest_gens[b] = ptr->gen;
+
+ *max_stale = max(*max_stale, ptr_stale(ca, ptr));
+ }
- ptr_gens_recalc_oldest(c, type, k, max_stale);
+ bch2_mark_key(c, k, true, k.k->size, pos, NULL, 0, flags);
fsck_err:
return ret;
}
@@ -281,7 +182,6 @@ fsck_err:
static int btree_gc_mark_node(struct bch_fs *c, struct btree *b,
u8 *max_stale, bool initial)
{
- enum bkey_type type = btree_node_type(b);
struct btree_node_iter iter;
struct bkey unpacked;
struct bkey_s_c k;
@@ -289,14 +189,14 @@ static int btree_gc_mark_node(struct bch_fs *c, struct btree *b,
*max_stale = 0;
- if (!bkey_type_needs_gc(type))
+ if (!btree_node_type_needs_gc(btree_node_type(b)))
return 0;
for_each_btree_node_key_unpack(b, k, &iter,
&unpacked) {
bch2_bkey_debugcheck(c, b, k);
- ret = bch2_gc_mark_key(c, type, k, max_stale, initial);
+ ret = bch2_gc_mark_key(c, k, max_stale, initial);
if (ret)
break;
}
@@ -310,7 +210,7 @@ static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id,
struct btree_iter iter;
struct btree *b;
struct range_checks r;
- unsigned depth = bkey_type_needs_gc(btree_id) ? 0 : 1;
+ unsigned depth = btree_node_type_needs_gc(btree_id) ? 0 : 1;
u8 max_stale;
int ret = 0;
@@ -364,7 +264,7 @@ static int bch2_gc_btree(struct bch_fs *c, enum btree_id btree_id,
b = c->btree_roots[btree_id].b;
if (!btree_node_fake(b))
- bch2_gc_mark_key(c, BKEY_TYPE_BTREE, bkey_i_to_s_c(&b->key),
+ bch2_gc_mark_key(c, bkey_i_to_s_c(&b->key),
&max_stale, initial);
gc_pos_set(c, gc_pos_btree_root(b->btree_id));
@@ -391,13 +291,13 @@ static int bch2_gc_btrees(struct bch_fs *c, struct list_head *journal,
for (i = 0; i < BTREE_ID_NR; i++) {
enum btree_id id = ids[i];
- enum bkey_type type = bkey_type(0, id);
+ enum btree_node_type type = __btree_node_type(0, id);
int ret = bch2_gc_btree(c, id, initial);
if (ret)
return ret;
- if (journal && bkey_type_needs_gc(type)) {
+ if (journal && btree_node_type_needs_gc(type)) {
struct bkey_i *k, *n;
struct jset_entry *j;
struct journal_replay *r;
@@ -405,8 +305,8 @@ static int bch2_gc_btrees(struct bch_fs *c, struct list_head *journal,
list_for_each_entry(r, journal, list)
for_each_jset_key(k, n, j, &r->j) {
- if (type == bkey_type(j->level, j->btree_id)) {
- ret = bch2_gc_mark_key(c, type,
+ if (type == __btree_node_type(j->level, j->btree_id)) {
+ ret = bch2_gc_mark_key(c,
bkey_i_to_s_c(k),
&max_stale, initial);
if (ret)
@@ -507,8 +407,7 @@ static void bch2_mark_pending_btree_node_frees(struct bch_fs *c)
for_each_pending_btree_node_free(c, as, d)
if (d->index_update_done)
- bch2_mark_key(c, BKEY_TYPE_BTREE,
- bkey_i_to_s_c(&d->key),
+ bch2_mark_key(c, bkey_i_to_s_c(&d->key),
true, 0,
pos, NULL, 0,
BCH_BUCKET_MARK_GC);