Skip to contentSkip to conversation

bcachefs: Fix subtraction underflow

PATCH In review Alan Huang 2 participants 2 messages Jan 27, 2025 – Jan 28, 2025

lore
Share & tools
lore

Alan Huang

author
#0 lore raw

PATCH 1/1 bcachefs: Fix subtraction underflow

When ancestor is less than IS_ANCESTOR_BITMAP, we would get an incorrect result.

---
 fs/bcachefs/snapshot.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/bcachefs/snapshot.c +3 -2
index c54091a28909..ede0b480e7d4 100644
@@ -146,8 +146,9 @@ bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
 		goto out;
 	}
 
-	while (id && id < ancestor - IS_ANCESTOR_BITMAP)
-		id = get_ancestor_below(t, id, ancestor);
+	if (likely(ancestor >= IS_ANCESTOR_BITMAP))
+		while (id && id < ancestor - IS_ANCESTOR_BITMAP)
+			id = get_ancestor_below(t, id, ancestor);
 
 	ret = id && id < ancestor
 		? test_ancestor_bitmap(t, id, ancestor)
Signature · 1 line
2.47.0
#1 lore raw
Quoted 13 lines · Alan Huang

When ancestor is less than IS_ANCESTOR_BITMAP, we would get an incorrect result.

---
 fs/bcachefs/snapshot.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/bcachefs/snapshot.c +0 -0
index c54091a28909..ede0b480e7d4 100644

fs/bcachefs/snapshot.c bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor) · line 151

Quoted 8 lines
fs/bcachefs/snapshot.c +3 -2
@@ -146,8 +146,9 @@ bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
 		goto out;
 	}
 
-	while (id && id < ancestor - IS_ANCESTOR_BITMAP)
-		id = get_ancestor_below(t, id, ancestor);
+	if (likely(ancestor >= IS_ANCESTOR_BITMAP))
+		while (id && id < ancestor - IS_ANCESTOR_BITMAP)
+			id = get_ancestor_below(t, id, ancestor);

That's not right, if ancestor < IS_ANCESTOR_BITMAP the bitmap would still need to be initialized.