Skip to contentSkip to conversation

fragmention_lru validation and repair code

PATCH New Daniel Hill 1 participant 4 messages Feb 19, 2024

lore
Share & tools
lore
#0 lore raw

cover fragmention_lru validation and repair code

Spotted bogus looking alloc entries that were also being incorrectly repaired.

Turns out bcachefs has no validation or repair code in the fragmentation portion of the LRU btree.

I don't know how your perfectly rational mind missed some basic repair code Kent.

copygc_get_buckets() was taking a full 60s to finish, Not sure if this is the issue, but it's a good start.

Now can you review this code like I asked you to on Tuesday? Mr "I look at your code when you ask me to" Kent?

Daniel Hill (3):
bcachefs: alloc_v4_invalid() now checks fragmenation for consistency
bcachefs: update fragmenation_lru in alloc_write_key()
bcachefs: add check for missing fragmentation in
check_alloc_to_lru_ref()

 fs/bcachefs/alloc_background.c | 44 ++++++++++++++++++++++++++++++++++
 fs/bcachefs/btree_gc.c         |  5 +++-
 fs/bcachefs/sb-errors_types.h  |  3 ++-
 3 files changed, 50 insertions(+), 2 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 bcachefs: alloc_v4_invalid() now checks fragmenation for consistency

We need to validate fragmenation_lru is consistent with sectors counts and bucket type.

---
 fs/bcachefs/alloc_background.c | 18 ++++++++++++++++++
 fs/bcachefs/sb-errors_types.h  |  3 ++-
 2 files changed, 20 insertions(+), 1 deletion(-)
fs/bcachefs/alloc_background.c +18 -0
index 4de4036ded06..3bf2a2daad93 100644
@@ -292,6 +292,24 @@ int bch2_alloc_v4_invalid(struct bch_fs *c, struct bkey_s_c k,
 	case BCH_DATA_stripe:
 		break;
 	}
+
+	/* this check fails to work during device removal evacuate etc,
+	 * so fall back to something less robust
+	 */
+	if (bch2_dev_exists2(c, a.k->p.inode)) {
+		u64 fragmentation_lru = alloc_lru_idx_fragmentation(*a.v,
+							    bch_dev_bkey_exists(c, a.k->p.inode));
+
+		bkey_fsck_err_on(a.v->fragmentation_lru != fragmentation_lru, c, err,
+				 alloc_lru_fragmentation_wrong,
+				 "bad fragmentation_lru (got %llu should be %llu)",
+				 a.v->fragmentation_lru, fragmentation_lru);
+	}
+	else
+		bkey_fsck_err_on(a.v->fragmentation_lru && !data_type_movable(a.v->data_type),
+				 c, err, alloc_lru_fragmentation_wrong,
+				 "alloc type %s is unmovabled but fragmentation_lru not 0 (%llu)",
+				 bch2_data_type_str(a.v->data_type), a.v->fragmentation_lru);
 fsck_err:
 	return ret;
 }
fs/bcachefs/sb-errors_types.h +2 -1
index 0df4b0e7071a..cc70a93dfd0d 100644
@@ -264,7 +264,8 @@
 	x(subvol_children_not_set,				256)	\
 	x(subvol_children_bad,					257)	\
 	x(subvol_loop,						258)	\
-	x(subvol_unreachable,					259)
+	x(subvol_unreachable,					259)	\
+	x(alloc_lru_fragmentation_wrong,			260)
 
 enum bch_sb_error_id {
 #define x(t, n) BCH_FSCK_ERR_##t = n,
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/3 bcachefs: update fragmenation_lru in alloc_write_key()

In reply to Daniel Hill

The alloc trigger normally makes sure fragmentation_lru is correct, but we don't run triggers here, so we have to manually construct it.

---
 fs/bcachefs/btree_gc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/bcachefs/btree_gc.c +4 -1
index 6c52f116098f..43ada059b109 100644
@@ -1456,12 +1456,15 @@ static int bch2_alloc_write_key(struct btree_trans *trans,
 	a->v = new;
 
 	/*
-	 * The trigger normally makes sure this is set, but we're not running
+	 * The trigger normally makes sure these are set, but we're not running
 	 * triggers:
 	 */
 	if (a->v.data_type == BCH_DATA_cached && !a->v.io_time[READ])
 		a->v.io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
 
+	a->v.fragmentation_lru = alloc_lru_idx_fragmentation(a->v,
+						bch_dev_bkey_exists(c, a->k.p.inode));
+
 	ret = bch2_trans_update(trans, iter, &a->k_i, BTREE_TRIGGER_NORUN);
 fsck_err:
 	return ret;
Signature · 1 line
2.43.0
#3 lore raw

PATCH 3/3 bcachefs: add check for missing fragmentation in check_alloc_to_lru_ref()

In reply to Daniel Hill

We need to make sure we're not missing any fragmenation entries in the LRU BTREE after repairing ALLOC BTREE

---
 fs/bcachefs/alloc_background.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
fs/bcachefs/alloc_background.c +26 -0
index 3bf2a2daad93..c0894680ac4c 100644
@@ -1566,6 +1566,32 @@ static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
 
 	a = bch2_alloc_to_v4(alloc_k, &a_convert);
 
+	if (a->fragmentation_lru) {
+		lru_k = bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru,
+					   lru_pos(BCH_LRU_FRAGMENTATION_START,
+						   bucket_to_u64(alloc_k.k->p),
+						   a->fragmentation_lru), 0);
+		ret = bkey_err(lru_k);
+
+		if (ret)
+			return ret;
+
+		if (fsck_err_on(lru_k.k->type != KEY_TYPE_set, c,
+				alloc_key_to_missing_lru_entry,
+				"missing fragmentation_lru entry\n"
+				"  %s",
+				(printbuf_reset(&buf),
+				bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
+			ret = bch2_lru_set(trans,
+					BCH_LRU_FRAGMENTATION_START,
+					bucket_to_u64(alloc_k.k->p),
+					a->fragmentation_lru);
+			if (ret)
+				goto err;
+		}
+		bch2_trans_iter_exit(trans, &lru_iter);
+	}
+
 	if (a->data_type != BCH_DATA_cached)
 		return 0;
 
Signature · 1 line
2.43.0