[PATCH 14/21] Fix free/alloc reversal of btree block merge condition

Valerie Aurora val at versity.com
Tue Feb 11 13:19:11 PST 2025


To achieve the goal of merging two btree blocks when the merged block
would be 80% full, we want to merge two blocks when their _used_ space
is 40% or lower, not when their _free_ space is 40% or lower. Add some
BUG_ON()'s to catch total_free wrapping around to negative, which was
the visible symptom in this case.

Signed-off-by: Valerie Aurora <val at versity.com>
---
 shared/btree.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/shared/btree.c b/shared/btree.c
index a55da6e..727eca1 100644
--- a/shared/btree.c
+++ b/shared/btree.c
@@ -49,7 +49,7 @@
  * deletion of a few items doesn't bounce a pair of blocks between
  * splitting and merging.
  */
-#define NGNFS_BTREE_MERGE_FREE_THRESH	(NGNFS_BTREE_MAX_FREE * 40 / 100)
+#define NGNFS_BTREE_MERGE_FREE_THRESH	(NGNFS_BTREE_MAX_FREE * (100 - 40) / 100)
 
 /*
  * If an insertion could be performed after compacting free space, but
@@ -260,7 +260,9 @@ static struct ngnfs_btree_item *insert_item(struct ngnfs_txn_block *tblk,
 
 	memmove_item_headers(tblk, bt, ind, 1);
 	ngnfs_tblk_le16_add_cpu(tblk, &bt->tail_free, -bytes);
+	BUG_ON(le16_to_cpu(bt->tail_free) > NGNFS_BTREE_MAX_FREE);
 	ngnfs_tblk_le16_add_cpu(tblk, &bt->total_free, -bytes);
+	BUG_ON(le16_to_cpu(bt->total_free) > NGNFS_BTREE_MAX_FREE);
 	ngnfs_tblk_le16_add_cpu(tblk, &bt->nr_items, 1);
 	ngnfs_tblk_assign(tblk, bt->ihdrs[ind].off, cpu_to_le16(off));
 	ngnfs_tblk_assign(tblk, bt->ihdrs[ind].val_size, cpu_to_le16(val_size));
@@ -297,7 +299,9 @@ static void delete_item(struct ngnfs_txn_block *tblk, struct ngnfs_btree_block *
 
 	if (off == NGNFS_BLOCK_SIZE - le16_to_cpu(bt->tail_free) - bytes)
 		ngnfs_tblk_le16_add_cpu(tblk, &bt->tail_free, bytes);
+	BUG_ON(le16_to_cpu(bt->tail_free) > NGNFS_BTREE_MAX_FREE);
 	ngnfs_tblk_le16_add_cpu(tblk, &bt->total_free, bytes);
+	BUG_ON(le16_to_cpu(bt->total_free) > NGNFS_BTREE_MAX_FREE);
 
 	memmove_item_headers(tblk, bt, ind + 1, -1);
 	ngnfs_tblk_le16_add_cpu(tblk, &bt->nr_items, -1);
-- 
2.48.1




More information about the ngnfs-devel mailing list