Skip to content

Commit 260af15

Browse files
author
Kent Overstreet
committed
bcachefs: Kill alloc_v4.fragmentation_lru
The fragmentation_lru field hasn't been needed since we reworked the LRU btrees to use the btree write buffer; previously it was used to resolve collisions, but the revised LRU btree uses the backpointer (the bucket) as part of the key. It should have been deleted at the time of the LRU rework; since it wasn't, that left places for bugs to hide, in check/repair. This fixes LRU fsck on a filesystem image helpfully provided by a user who disappeared before I could get his name for the reported-by. Signed-off-by: Kent Overstreet <[email protected]>
1 parent 01bf5e3 commit 260af15

File tree

7 files changed

+38
-22
lines changed

7 files changed

+38
-22
lines changed

fs/bcachefs/alloc_background.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ void bch2_alloc_v4_swab(struct bkey_s k)
332332
a->io_time[1] = swab64(a->io_time[1]);
333333
a->stripe = swab32(a->stripe);
334334
a->nr_external_backpointers = swab32(a->nr_external_backpointers);
335-
a->fragmentation_lru = swab64(a->fragmentation_lru);
336335
a->stripe_sectors = swab32(a->stripe_sectors);
337336

338337
bps = alloc_v4_backpointers(a);
@@ -347,6 +346,7 @@ void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c
347346
{
348347
struct bch_alloc_v4 _a;
349348
const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &_a);
349+
struct bch_dev *ca = c ? bch2_dev_bucket_tryget_noerror(c, k.k->p) : NULL;
350350

351351
prt_newline(out);
352352
printbuf_indent_add(out, 2);
@@ -364,9 +364,13 @@ void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c
364364
prt_printf(out, "stripe_redundancy %u\n", a->stripe_redundancy);
365365
prt_printf(out, "io_time[READ] %llu\n", a->io_time[READ]);
366366
prt_printf(out, "io_time[WRITE] %llu\n", a->io_time[WRITE]);
367-
prt_printf(out, "fragmentation %llu\n", a->fragmentation_lru);
367+
368+
if (ca)
369+
prt_printf(out, "fragmentation %llu\n", alloc_lru_idx_fragmentation(*a, ca));
368370
prt_printf(out, "bp_start %llu\n", BCH_ALLOC_V4_BACKPOINTERS_START(a));
369371
printbuf_indent_sub(out, 2);
372+
373+
bch2_dev_put(ca);
370374
}
371375

372376
void __bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
@@ -882,12 +886,13 @@ int bch2_trigger_alloc(struct btree_trans *trans,
882886
goto err;
883887
}
884888

885-
new_a->fragmentation_lru = alloc_lru_idx_fragmentation(*new_a, ca);
886-
if (old_a->fragmentation_lru != new_a->fragmentation_lru) {
889+
old_lru = alloc_lru_idx_fragmentation(*old_a, ca);
890+
new_lru = alloc_lru_idx_fragmentation(*new_a, ca);
891+
if (old_lru != new_lru) {
887892
ret = bch2_lru_change(trans,
888893
BCH_LRU_FRAGMENTATION_START,
889894
bucket_to_u64(new.k->p),
890-
old_a->fragmentation_lru, new_a->fragmentation_lru);
895+
old_lru, new_lru);
891896
if (ret)
892897
goto err;
893898
}
@@ -1629,18 +1634,22 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
16291634
if (ret)
16301635
return ret;
16311636

1637+
struct bch_dev *ca = bch2_dev_tryget_noerror(c, alloc_k.k->p.inode);
1638+
if (!ca)
1639+
return 0;
1640+
16321641
a = bch2_alloc_to_v4(alloc_k, &a_convert);
16331642

1634-
if (a->fragmentation_lru) {
1643+
u64 lru_idx = alloc_lru_idx_fragmentation(*a, ca);
1644+
if (lru_idx) {
16351645
ret = bch2_lru_check_set(trans, BCH_LRU_FRAGMENTATION_START,
1636-
a->fragmentation_lru,
1637-
alloc_k, last_flushed);
1646+
lru_idx, alloc_k, last_flushed);
16381647
if (ret)
1639-
return ret;
1648+
goto err;
16401649
}
16411650

16421651
if (a->data_type != BCH_DATA_cached)
1643-
return 0;
1652+
goto err;
16441653

16451654
if (fsck_err_on(!a->io_time[READ],
16461655
trans, alloc_key_cached_but_read_time_zero,
@@ -1669,6 +1678,7 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
16691678
goto err;
16701679
err:
16711680
fsck_err:
1681+
bch2_dev_put(ca);
16721682
printbuf_exit(&buf);
16731683
return ret;
16741684
}

fs/bcachefs/alloc_background_format.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ struct bch_alloc_v4 {
7070
__u32 stripe;
7171
__u32 nr_external_backpointers;
7272
/* end of fields in original version of alloc_v4 */
73-
__u64 fragmentation_lru;
73+
__u64 _fragmentation_lru; /* obsolete */
7474
__u32 stripe_sectors;
7575
__u32 pad;
7676
} __packed __aligned(8);

fs/bcachefs/btree_gc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,6 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
828828
return ret;
829829
}
830830

831-
gc.fragmentation_lru = alloc_lru_idx_fragmentation(gc, ca);
832-
833831
if (fsck_err_on(new.data_type != gc.data_type,
834832
trans, alloc_key_data_type_wrong,
835833
"bucket %llu:%llu gen %u has wrong data_type"
@@ -857,7 +855,6 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
857855
copy_bucket_field(alloc_key_cached_sectors_wrong, cached_sectors);
858856
copy_bucket_field(alloc_key_stripe_wrong, stripe);
859857
copy_bucket_field(alloc_key_stripe_redundancy_wrong, stripe_redundancy);
860-
copy_bucket_field(alloc_key_fragmentation_lru_wrong, fragmentation_lru);
861858
#undef copy_bucket_field
862859

863860
if (!bch2_alloc_v4_cmp(*old, new))

fs/bcachefs/lru.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ static int bch2_check_lru_key(struct btree_trans *trans,
133133
u64 idx;
134134
int ret;
135135

136-
if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_pos),
136+
struct bch_dev *ca = bch2_dev_bucket_tryget_noerror(c, alloc_pos);
137+
138+
if (fsck_err_on(!ca,
137139
trans, lru_entry_to_invalid_bucket,
138140
"lru key points to nonexistent device:bucket %llu:%llu",
139141
alloc_pos.inode, alloc_pos.offset))
@@ -151,7 +153,7 @@ static int bch2_check_lru_key(struct btree_trans *trans,
151153
idx = alloc_lru_idx_read(*a);
152154
break;
153155
case BCH_LRU_fragmentation:
154-
idx = a->fragmentation_lru;
156+
idx = alloc_lru_idx_fragmentation(*a, ca);
155157
break;
156158
}
157159

@@ -174,6 +176,7 @@ static int bch2_check_lru_key(struct btree_trans *trans,
174176
err:
175177
fsck_err:
176178
bch2_trans_iter_exit(trans, &iter);
179+
bch2_dev_put(ca);
177180
printbuf_exit(&buf2);
178181
printbuf_exit(&buf1);
179182
return ret;

fs/bcachefs/move.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ int bch2_evacuate_bucket(struct moving_context *ctxt,
692692
a = bch2_alloc_to_v4(k, &a_convert);
693693
dirty_sectors = bch2_bucket_sectors_dirty(*a);
694694
bucket_size = ca->mi.bucket_size;
695-
fragmentation = a->fragmentation_lru;
695+
fragmentation = alloc_lru_idx_fragmentation(*a, ca);
696696

697697
ret = bch2_btree_write_buffer_tryflush(trans);
698698
bch_err_msg(c, ret, "flushing btree write buffer");

fs/bcachefs/movinggc.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ move_bucket_in_flight_add(struct buckets_in_flight *list, struct move_bucket b)
7373
static int bch2_bucket_is_movable(struct btree_trans *trans,
7474
struct move_bucket *b, u64 time)
7575
{
76+
struct bch_fs *c = trans->c;
7677
struct btree_iter iter;
7778
struct bkey_s_c k;
7879
struct bch_alloc_v4 _a;
@@ -90,14 +91,19 @@ static int bch2_bucket_is_movable(struct btree_trans *trans,
9091
if (ret)
9192
return ret;
9293

94+
struct bch_dev *ca = bch2_dev_tryget(c, k.k->p.inode);
95+
if (!ca)
96+
goto out;
97+
9398
a = bch2_alloc_to_v4(k, &_a);
9499
b->k.gen = a->gen;
95100
b->sectors = bch2_bucket_sectors_dirty(*a);
101+
u64 lru_idx = alloc_lru_idx_fragmentation(*a, ca);
96102

97-
ret = data_type_movable(a->data_type) &&
98-
a->fragmentation_lru &&
99-
a->fragmentation_lru <= time;
103+
ret = lru_idx && lru_idx <= time;
100104

105+
bch2_dev_put(ca);
106+
out:
101107
bch2_trans_iter_exit(trans, &iter);
102108
return ret;
103109
}

fs/bcachefs/sb-errors_format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ enum bch_fsck_flags {
115115
x(alloc_key_data_type_inconsistency, 101, 0) \
116116
x(alloc_key_to_missing_dev_bucket, 102, 0) \
117117
x(alloc_key_cached_inconsistency, 103, 0) \
118-
x(alloc_key_cached_but_read_time_zero, 104, 0) \
119-
x(alloc_key_to_missing_lru_entry, 105, 0) \
118+
x(alloc_key_cached_but_read_time_zero, 104, FSCK_AUTOFIX) \
119+
x(alloc_key_to_missing_lru_entry, 105, FSCK_AUTOFIX) \
120120
x(alloc_key_data_type_wrong, 106, FSCK_AUTOFIX) \
121121
x(alloc_key_gen_wrong, 107, FSCK_AUTOFIX) \
122122
x(alloc_key_dirty_sectors_wrong, 108, FSCK_AUTOFIX) \

0 commit comments

Comments
 (0)