Skip to content

Commit 38ad9dc

Browse files
author
Kent Overstreet
committed
bcachefs: Initialize gc buckets in alloc trigger
Needed for online fsck; we need the trigger to initialize newly allocated buckets and generation number changes while gc is running. Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
1 parent 9ab55df commit 38ad9dc

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,10 +831,9 @@ int bch2_trigger_alloc(struct btree_trans *trans,
831831

832832
struct bch_alloc_v4 old_a_convert;
833833
const struct bch_alloc_v4 *old_a = bch2_alloc_to_v4(old, &old_a_convert);
834+
struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
834835

835836
if (flags & BTREE_TRIGGER_transactional) {
836-
struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
837-
838837
alloc_data_type_set(new_a, new_a->data_type);
839838

840839
if (bch2_bucket_sectors_total(*new_a) > bch2_bucket_sectors_total(*old_a)) {
@@ -906,7 +905,6 @@ int bch2_trigger_alloc(struct btree_trans *trans,
906905
}
907906

908907
if ((flags & BTREE_TRIGGER_atomic) && (flags & BTREE_TRIGGER_insert)) {
909-
struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
910908
u64 journal_seq = trans->journal_res.seq;
911909
u64 bucket_journal_seq = new_a->journal_seq;
912910

@@ -935,11 +933,9 @@ int bch2_trigger_alloc(struct btree_trans *trans,
935933
c->journal.flushed_seq_ondisk,
936934
new.k->p.inode, new.k->p.offset,
937935
bucket_journal_seq);
938-
if (ret) {
939-
bch2_fs_fatal_error(c,
940-
"setting bucket_needs_journal_commit: %s", bch2_err_str(ret));
936+
if (bch2_fs_fatal_err_on(ret, c,
937+
"setting bucket_needs_journal_commit: %s", bch2_err_str(ret)))
941938
goto err;
942-
}
943939
}
944940

945941
if (new_a->gen != old_a->gen) {
@@ -974,6 +970,18 @@ int bch2_trigger_alloc(struct btree_trans *trans,
974970
if (statechange(a->data_type == BCH_DATA_need_gc_gens))
975971
bch2_gc_gens_async(c);
976972
}
973+
974+
if ((flags & BTREE_TRIGGER_gc) && (flags & BTREE_TRIGGER_insert)) {
975+
rcu_read_lock();
976+
struct bucket *g = gc_bucket(ca, new.k->p.offset);
977+
if (unlikely(!g)) {
978+
rcu_read_unlock();
979+
goto invalid_bucket;
980+
}
981+
g->gen_valid = 1;
982+
g->gen = new_a->gen;
983+
rcu_read_unlock();
984+
}
977985
err:
978986
printbuf_exit(&buf);
979987
bch2_dev_put(ca);

fs/bcachefs/btree_gc.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ static int bch2_gc_mark_key(struct btree_trans *trans, enum btree_id btree_id,
624624
}
625625

626626
ret = bch2_key_trigger(trans, btree_id, level, old, unsafe_bkey_s_c_to_s(k),
627-
BTREE_TRIGGER_gc|flags);
627+
BTREE_TRIGGER_gc|BTREE_TRIGGER_insert|flags);
628628
out:
629629
fsck_err:
630630
printbuf_exit(&buf);
@@ -891,14 +891,16 @@ static int bch2_gc_alloc_done(struct bch_fs *c)
891891

892892
static int bch2_gc_alloc_start(struct bch_fs *c)
893893
{
894+
int ret = 0;
895+
894896
for_each_member_device(c, ca) {
895897
struct bucket_array *buckets = kvmalloc(sizeof(struct bucket_array) +
896898
ca->mi.nbuckets * sizeof(struct bucket),
897899
GFP_KERNEL|__GFP_ZERO);
898900
if (!buckets) {
899901
bch2_dev_put(ca);
900-
bch_err(c, "error allocating ca->buckets[gc]");
901-
return -BCH_ERR_ENOMEM_gc_alloc_start;
902+
ret = -BCH_ERR_ENOMEM_gc_alloc_start;
903+
break;
902904
}
903905

904906
buckets->first_bucket = ca->mi.first_bucket;
@@ -908,27 +910,6 @@ static int bch2_gc_alloc_start(struct bch_fs *c)
908910
rcu_assign_pointer(ca->buckets_gc, buckets);
909911
}
910912

911-
struct bch_dev *ca = NULL;
912-
int ret = bch2_trans_run(c,
913-
for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
914-
BTREE_ITER_prefetch, k, ({
915-
ca = bch2_dev_iterate(c, ca, k.k->p.inode);
916-
if (!ca) {
917-
bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
918-
continue;
919-
}
920-
921-
if (bucket_valid(ca, k.k->p.offset)) {
922-
struct bch_alloc_v4 a_convert;
923-
const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &a_convert);
924-
925-
struct bucket *g = gc_bucket(ca, k.k->p.offset);
926-
g->gen_valid = 1;
927-
g->gen = a->gen;
928-
}
929-
0;
930-
})));
931-
bch2_dev_put(ca);
932913
bch_err_fn(c, ret);
933914
return ret;
934915
}

0 commit comments

Comments
 (0)