mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page
@ 2026-12-04  5:36 Mar Dominguez
  2026-12-04  5:38 ` [PATCH 1/3] hfs: fix b-tree node writes past the first page Mar Dominguez
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Mar Dominguez @ 2026-12-04  5:36 UTC (permalink / raw)
  To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li,
	linux-fsdevel, linux-kernel

hfs_btree_open() accepts b-tree node sizes up to 32 KiB, and the read
side handles nodes that span several pages, but hfs_bnode_write(),
hfs_bnode_clear(), hfs_bnode_copy() and hfs_bnode_move() only use the
node's first page.  Any change to a catalog or extents tree whose nodes
are larger than PAGE_SIZE then writes past that page.

Patch 1 makes the four helpers walk the node's pages.  Patch 2 rejects
node sizes below 512 bytes, as hfsplus does.  Patch 3 adds KUnit tests
for the four helpers.

Patch 1 fixes the write helpers instead of refusing nodes larger than
512 bytes: hfs_btree_open() has accepted them since 2.6.12, and the read
and node creation paths already support them.  With patch 1 the write
helpers do too, as the hfsplus ones do.

Tested with x86_64 KASAN builds under QEMU: hfs/for-next (a2c27efbbd43)
with CONFIG_DEBUG_VM, and mainline (007722b8211f) without it.  The test
volumes were made with hformat, with the catalog tree rewritten to use
8 KiB or 32 KiB nodes.  Without the series, mkdir and rmdir on them hit
the VM_BUG_ON in memcpy_to_page() with CONFIG_DEBUG_VM.  Without
CONFIG_DEBUG_VM, the volume can't be mounted again after mkdir (8 KiB
nodes), and rmdir (32 KiB nodes) crashes init through corrupted
memory.  With the series, both work and the volume mounts again with
the expected directories.  A volume with the usual 512-byte nodes
behaves the same with and without the series.  The hfs KUnit suites
pass on UML with KASAN; without patch 1 the four new cases fail.

I have not tested on 32-bit HIGHMEM, on architectures with pages larger
than 4 KiB, or with volumes written by Mac OS, and I have not run
xfstests.  Patches 1 and 2 also apply to mainline; patch 3 needs the
KUnit setup in hfs/for-next.  I can send the test images privately.

Found and written with an LLM coding assistant.

Mar Dominguez (3):
  hfs: fix b-tree node writes past the first page
  hfs: check the b-tree node size range in hfs_btree_open()
  hfs: add KUnit tests for b-tree node operations

 fs/hfs/Makefile            |   2 +-
 fs/hfs/bnode.c             | 132 ++++++++++++++---
 fs/hfs/bnode_test.c        | 293 +++++++++++++++++++++++++++++++++++++
 fs/hfs/btree.c             |   5 +-
 include/linux/hfs_common.h |   6 +-
 5 files changed, 416 insertions(+), 22 deletions(-)
 create mode 100644 fs/hfs/bnode_test.c


base-commit: a2c27efbbd43d2e3756860ebd1bf891d7481f2c5
-- 
2.53.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] hfs: fix b-tree node writes past the first page
  2026-12-04  5:36 [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page Mar Dominguez
@ 2026-12-04  5:38 ` Mar Dominguez
  2026-12-04  5:41 ` [PATCH 2/3] hfs: check the b-tree node size range in hfs_btree_open() Mar Dominguez
  2026-12-04  5:43 ` [PATCH 3/3] hfs: add KUnit tests for b-tree node operations Mar Dominguez
  2 siblings, 0 replies; 4+ messages in thread
From: Mar Dominguez @ 2026-12-04  5:38 UTC (permalink / raw)
  To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li,
	linux-fsdevel, linux-kernel

hfs_btree_open() accepts any power-of-two node size from the b-tree
header, so a catalog or extents tree can have 8, 16 or 32 KiB nodes,
which span several pages on 4 KiB page systems.  hfs_bnode_find() and
hfs_bnode_read() handle such nodes: commit 54a5ead6f5e2 ("hfs: fix high
memory mapping in hfs_bnode_read") made the read side walk
node->page[].  hfs_bnode_write(), hfs_bnode_clear(), hfs_bnode_copy()
and hfs_bnode_move() still use only node->page[0], with offsets and
lengths that are bounded by the node size, not by the page size.  Every
change to such a tree then writes up to 28 KiB past the end of that
page, into whatever memory follows it.  For example, hfs_brec_insert()
updates the record offset table at the end of the node, and
hfs_bnode_put() clears the whole node when a node is deleted.  The
node's other pages are never updated, so the tree on disk is left
inconsistent too.

With CONFIG_DEBUG_VM, mkdir on a volume whose catalog tree has 8 KiB
nodes trips the check in memcpy_to_page():

  kernel BUG at include/linux/highmem.h:552!
  RIP: 0010:hfs_bnode_write+0x4c5/0x560
  Call Trace:
   hfs_bnode_write_u16+0x6d/0xa0
   hfs_brec_insert+0x3c4/0xb60
   hfs_cat_create+0x3bd/0xb00
   hfs_mkdir+0x59/0xc0
   vfs_mkdir+0x2b4/0x710

Without CONFIG_DEBUG_VM the pages after node->page[0] are silently
overwritten or zeroed.

Walk the node's pages in all four helpers, as hfs_bnode_read() and the
hfsplus helpers do.  hfs_bnode_move() copies backwards when the
destination is above the source, because the ranges may overlap across
pages.

Found and written with an LLM coding assistant.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Mar Dominguez <mar@talkjune.com>
---
 fs/hfs/bnode.c | 124 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 106 insertions(+), 18 deletions(-)

diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c
index ec1b0189a4..d12be972ce 100644
--- a/fs/hfs/bnode.c
+++ b/fs/hfs/bnode.c
@@ -95,6 +95,9 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 {
 	struct page *page;
+	u32 pagenum;
+	u32 bytes_written;
+	u32 bytes_to_write;
 
 	if (!is_bnode_offset_valid(node, off))
 		return;
@@ -111,10 +114,22 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 	len = check_and_correct_requested_length(node, off, len);
 
 	off += node->page_offset;
-	page = node->page[0];
+	pagenum = off >> PAGE_SHIFT;
+	off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+	for (bytes_written = 0; bytes_written < len;
+	     bytes_written += bytes_to_write) {
+		if (pagenum >= node->tree->pages_per_bnode)
+			break;
+		page = node->page[pagenum];
+		bytes_to_write = min_t(u32, len - bytes_written, PAGE_SIZE - off);
+
+		memcpy_to_page(page, off, buf + bytes_written, bytes_to_write);
+		set_page_dirty(page);
 
-	memcpy_to_page(page, off, buf, len);
-	set_page_dirty(page);
+		pagenum++;
+		off = 0; /* page offset only applies to the first page */
+	}
 }
 
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
@@ -133,6 +148,9 @@ void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data)
 void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 {
 	struct page *page;
+	u32 pagenum;
+	u32 bytes_cleared;
+	u32 bytes_to_clear;
 
 	if (!is_bnode_offset_valid(node, off))
 		return;
@@ -149,16 +167,65 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 	len = check_and_correct_requested_length(node, off, len);
 
 	off += node->page_offset;
-	page = node->page[0];
+	pagenum = off >> PAGE_SHIFT;
+	off &= ~PAGE_MASK; /* compute page offset for the first page */
+
+	for (bytes_cleared = 0; bytes_cleared < len;
+	     bytes_cleared += bytes_to_clear) {
+		if (pagenum >= node->tree->pages_per_bnode)
+			break;
+		page = node->page[pagenum];
+		bytes_to_clear = min_t(u32, len - bytes_cleared, PAGE_SIZE - off);
+
+		memzero_page(page, off, bytes_to_clear);
+		set_page_dirty(page);
+
+		pagenum++;
+		off = 0; /* page offset only applies to the first page */
+	}
+}
 
-	memzero_page(page, off, len);
-	set_page_dirty(page);
+/* Number of bytes from @off to the end of the page that contains @off */
+static inline u32 hfs_bnode_page_tail(u32 off)
+{
+	return PAGE_SIZE - (off & ~PAGE_MASK);
+}
+
+/* Number of bytes before @end in the page that contains byte @end - 1 */
+static inline u32 hfs_bnode_page_head(u32 end)
+{
+	return ((end - 1) & ~PAGE_MASK) + 1;
+}
+
+/*
+ * Copy @len bytes from offset @src of @src_node to offset @dst of @dst_node.
+ * The offsets include the nodes' page_offset, and neither range may cross
+ * a page boundary.
+ */
+static void hfs_bnode_copy_chunk(struct hfs_bnode *dst_node, u32 dst,
+				 struct hfs_bnode *src_node, u32 src, u32 len)
+{
+	struct page *src_page = src_node->page[src >> PAGE_SHIFT];
+	struct page *dst_page = dst_node->page[dst >> PAGE_SHIFT];
+	void *ptr;
+
+	src &= ~PAGE_MASK;
+	dst &= ~PAGE_MASK;
+
+	if (src_page == dst_page) {
+		ptr = kmap_local_page(dst_page);
+		memmove(ptr + dst, ptr + src, len);
+		kunmap_local(ptr);
+	} else {
+		memcpy_page(dst_page, dst, src_page, src, len);
+	}
+	set_page_dirty(dst_page);
 }
 
 void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 		    struct hfs_bnode *src_node, u32 src, u32 len)
 {
-	struct page *src_page, *dst_page;
+	u32 chunk;
 
 	hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
 	if (!len)
@@ -169,17 +236,20 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 
 	src += src_node->page_offset;
 	dst += dst_node->page_offset;
-	src_page = src_node->page[0];
-	dst_page = dst_node->page[0];
 
-	memcpy_page(dst_page, dst, src_page, src, len);
-	set_page_dirty(dst_page);
+	while (len) {
+		chunk = min3(len, hfs_bnode_page_tail(src),
+			     hfs_bnode_page_tail(dst));
+		hfs_bnode_copy_chunk(dst_node, dst, src_node, src, chunk);
+		src += chunk;
+		dst += chunk;
+		len -= chunk;
+	}
 }
 
 void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 {
-	struct page *page;
-	void *ptr;
+	u32 chunk;
 
 	hfs_dbg("dst %u, src %u, len %u\n", dst, src, len);
 	if (!len)
@@ -190,11 +260,29 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 
 	src += node->page_offset;
 	dst += node->page_offset;
-	page = node->page[0];
-	ptr = kmap_local_page(page);
-	memmove(ptr + dst, ptr + src, len);
-	kunmap_local(ptr);
-	set_page_dirty(page);
+
+	if (dst > src) {
+		/* The ranges may overlap: move the end first */
+		src += len;
+		dst += len;
+		while (len) {
+			chunk = min3(len, hfs_bnode_page_head(src),
+				     hfs_bnode_page_head(dst));
+			src -= chunk;
+			dst -= chunk;
+			len -= chunk;
+			hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+		}
+	} else {
+		while (len) {
+			chunk = min3(len, hfs_bnode_page_tail(src),
+				     hfs_bnode_page_tail(dst));
+			hfs_bnode_copy_chunk(node, dst, node, src, chunk);
+			src += chunk;
+			dst += chunk;
+			len -= chunk;
+		}
+	}
 }
 
 void hfs_bnode_dump(struct hfs_bnode *node)
-- 
2.53.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/3] hfs: check the b-tree node size range in hfs_btree_open()
  2026-12-04  5:36 [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page Mar Dominguez
  2026-12-04  5:38 ` [PATCH 1/3] hfs: fix b-tree node writes past the first page Mar Dominguez
@ 2026-12-04  5:41 ` Mar Dominguez
  2026-12-04  5:43 ` [PATCH 3/3] hfs: add KUnit tests for b-tree node operations Mar Dominguez
  2 siblings, 0 replies; 4+ messages in thread
From: Mar Dominguez @ 2026-12-04  5:41 UTC (permalink / raw)
  To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li,
	linux-fsdevel, linux-kernel

hfs_btree_open() only checks that the node size in the b-tree header is
a power of two, so it also accepts nodes of 1 to 256 bytes, which can't
hold a node descriptor and the header node's records.  HFS b-tree nodes
are 512 bytes, and hfsplus already rejects node sizes outside 512 to
32768 bytes.  Apply the same range to HFS, with the constants moved to
hfs_common.h and shared by both, and log the size that is rejected.

Found and written with an LLM coding assistant.

Signed-off-by: Mar Dominguez <mar@talkjune.com>
---
 fs/hfs/btree.c             | 5 ++++-
 include/linux/hfs_common.h | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/hfs/btree.c b/fs/hfs/btree.c
index f070384031..62c9094ab4 100644
--- a/fs/hfs/btree.c
+++ b/fs/hfs/btree.c
@@ -229,8 +229,11 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke
 	tree->depth = be16_to_cpu(head->depth);
 
 	size = tree->node_size;
-	if (!is_power_of_2(size))
+	if (size < HFS_NODE_MINSZ || size > HFS_NODE_MXSZ ||
+	    !is_power_of_2(size)) {
+		pr_err("invalid node size %u\n", size);
 		goto fail_folio;
+	}
 	if (!tree->node_count)
 		goto fail_folio;
 	switch (id) {
diff --git a/include/linux/hfs_common.h b/include/linux/hfs_common.h
index eb119a1c56..3502552d75 100644
--- a/include/linux/hfs_common.h
+++ b/include/linux/hfs_common.h
@@ -532,13 +532,15 @@ struct hfs_btree_header_rec {
 
 /* HFS BTree misc info */
 #define HFS_TREE_HEAD				0
+#define HFS_NODE_MINSZ				512
+#define HFS_NODE_MXSZ				32768
 #define HFS_BTREE_HDR_MAP_REC_INDEX		2	/* Map (bitmap) record in Header node */
 #define HFS_BTREE_MAP_NODE_REC_INDEX		0	/* Map record in Map Node */
 
 /* HFS+ BTree misc info */
 #define HFSPLUS_TREE_HEAD			HFS_TREE_HEAD
-#define HFSPLUS_NODE_MXSZ			32768
-#define HFSPLUS_NODE_MINSZ			512
+#define HFSPLUS_NODE_MXSZ			HFS_NODE_MXSZ
+#define HFSPLUS_NODE_MINSZ			HFS_NODE_MINSZ
 #define HFSPLUS_ATTR_TREE_NODE_SIZE		8192
 #define HFSPLUS_BTREE_HDR_NODE_RECS_COUNT	3
 /* Map (bitmap) record in Header node */
-- 
2.53.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 3/3] hfs: add KUnit tests for b-tree node operations
  2026-12-04  5:36 [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page Mar Dominguez
  2026-12-04  5:38 ` [PATCH 1/3] hfs: fix b-tree node writes past the first page Mar Dominguez
  2026-12-04  5:41 ` [PATCH 2/3] hfs: check the b-tree node size range in hfs_btree_open() Mar Dominguez
@ 2026-12-04  5:43 ` Mar Dominguez
  2 siblings, 0 replies; 4+ messages in thread
From: Mar Dominguez @ 2026-12-04  5:43 UTC (permalink / raw)
  To: Viacheslav Dubeyko, John Paul Adrian Glaubitz, Yangtao Li,
	linux-fsdevel, linux-kernel

Add a KUnit suite for hfs_bnode_write(), hfs_bnode_clear(),
hfs_bnode_copy() and hfs_bnode_move() on 32 KiB nodes, with ranges
that cross page boundaries and reach the end of the node, checking the
node's contents page by page.  The first page of each test node is the
start of a higher-order allocation, so an operation that runs off the
end of node->page[0] lands in the test's own guard pages, which are
checked, instead of in other memory.  Without the multi-page fix all
four cases fail on systems with 4 KiB pages.

Export the helpers with EXPORT_SYMBOL_IF_KUNIT(), as the existing HFS
tests do for string.c and inode.c.

Found and written with an LLM coding assistant.

Signed-off-by: Mar Dominguez <mar@talkjune.com>
---
 fs/hfs/Makefile     |   2 +-
 fs/hfs/bnode.c      |   8 ++
 fs/hfs/bnode_test.c | 293 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 302 insertions(+), 1 deletion(-)
 create mode 100644 fs/hfs/bnode_test.c

diff --git a/fs/hfs/Makefile b/fs/hfs/Makefile
index be68c5305f..c19138944f 100644
--- a/fs/hfs/Makefile
+++ b/fs/hfs/Makefile
@@ -10,4 +10,4 @@ hfs-objs := bitmap.o bfind.o bnode.o brec.o btree.o \
             part_tbl.o string.o super.o sysdep.o trans.o
 
 # KUnit tests
-obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o
+obj-$(CONFIG_HFS_KUNIT_TEST) += string_test.o hfs_time_test.o bnode_test.o
diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c
index d12be972ce..d1d61d7f46 100644
--- a/fs/hfs/bnode.c
+++ b/fs/hfs/bnode.c
@@ -12,6 +12,7 @@
 #include <linux/pagemap.h>
 #include <linux/slab.h>
 #include <linux/swap.h>
+#include <kunit/visibility.h>
 
 #include "btree.h"
 
@@ -54,6 +55,7 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read);
 
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off)
 {
@@ -62,6 +64,7 @@ u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off)
 	hfs_bnode_read(node, &data, off, 2);
 	return be16_to_cpu(data);
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_read_u16);
 
 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 {
@@ -131,6 +134,7 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write);
 
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
 {
@@ -138,6 +142,7 @@ void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data)
 	// optimize later...
 	hfs_bnode_write(node, &v, off, 2);
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_write_u16);
 
 void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data)
 {
@@ -184,6 +189,7 @@ void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len)
 		off = 0; /* page offset only applies to the first page */
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_clear);
 
 /* Number of bytes from @off to the end of the page that contains @off */
 static inline u32 hfs_bnode_page_tail(u32 off)
@@ -246,6 +252,7 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, u32 dst,
 		len -= chunk;
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_copy);
 
 void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 {
@@ -284,6 +291,7 @@ void hfs_bnode_move(struct hfs_bnode *node, u32 dst, u32 src, u32 len)
 		}
 	}
 }
+EXPORT_SYMBOL_IF_KUNIT(hfs_bnode_move);
 
 void hfs_bnode_dump(struct hfs_bnode *node)
 {
diff --git a/fs/hfs/bnode_test.c b/fs/hfs/bnode_test.c
new file mode 100644
index 0000000000..9c34df76a0
--- /dev/null
+++ b/fs/hfs/bnode_test.c
@@ -0,0 +1,293 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for HFS b-tree node operations
+ *
+ * A b-tree node can be larger than a page (up to HFS_NODE_MXSZ bytes).
+ * The tests check that reads, writes, clears, copies and moves reach the
+ * right page of such a node.  The first page of every test node is the
+ * first page of a higher-order allocation, so an operation that runs off
+ * the end of node->page[0] lands in the test's own guard pages, which are
+ * checked, instead of corrupting other memory.
+ */
+
+#include <kunit/test.h>
+#include <linux/gfp.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
+#include <linux/overflow.h>
+#include <linux/string.h>
+#include "btree.h"
+
+#define TEST_NODE_SIZE		HFS_NODE_MXSZ
+
+struct hfs_bnode_test_node {
+	struct hfs_bnode *node;
+	struct page *block;
+	unsigned int order;
+};
+
+static void hfs_bnode_test_free(void *data)
+{
+	struct hfs_bnode_test_node *t = data;
+	u32 i;
+
+	for (i = 1; i < t->node->tree->pages_per_bnode; i++)
+		if (t->node->page[i])
+			__free_page(t->node->page[i]);
+	__free_pages(t->block, t->order);
+}
+
+static struct hfs_btree *hfs_bnode_test_tree(struct kunit *test)
+{
+	struct hfs_btree *tree;
+
+	tree = kunit_kzalloc(test, sizeof(*tree), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, tree);
+	tree->node_size = TEST_NODE_SIZE;
+	tree->pages_per_bnode = DIV_ROUND_UP(TEST_NODE_SIZE, PAGE_SIZE);
+
+	return tree;
+}
+
+static struct hfs_bnode_test_node *hfs_bnode_test_alloc(struct kunit *test,
+							struct hfs_btree *tree)
+{
+	struct hfs_bnode_test_node *t;
+	struct hfs_bnode *node;
+	u32 i;
+
+	t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, t);
+	node = kunit_kzalloc(test, struct_size(node, page, tree->pages_per_bnode),
+			     GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, node);
+	node->tree = tree;
+	t->node = node;
+
+	t->order = get_order(TEST_NODE_SIZE);
+	t->block = alloc_pages(GFP_KERNEL | __GFP_ZERO, t->order);
+	KUNIT_ASSERT_NOT_NULL(test, t->block);
+	node->page[0] = t->block;
+	for (i = 1; i < tree->pages_per_bnode; i++) {
+		node->page[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!node->page[i])
+			break;
+	}
+	KUNIT_ASSERT_EQ(test, 0,
+			kunit_add_action_or_reset(test, hfs_bnode_test_free, t));
+	KUNIT_ASSERT_EQ(test, i, tree->pages_per_bnode);
+
+	return t;
+}
+
+/* Offset of the first byte that differs, or @len */
+static u32 hfs_bnode_test_diff(const u8 *a, const u8 *b, u32 len)
+{
+	u32 i;
+
+	for (i = 0; i < len; i++)
+		if (a[i] != b[i])
+			break;
+
+	return i;
+}
+
+/* Check the node's contents page by page, and that the guard pages are untouched */
+static void hfs_bnode_test_check(struct kunit *test, struct hfs_bnode_test_node *t,
+				 const u8 *expected)
+{
+	struct hfs_bnode *node = t->node;
+	u32 size = node->tree->node_size;
+	u32 i, len, written;
+	u8 *buf;
+	void *p;
+
+	buf = kunit_kzalloc(test, size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+	for (i = 0; i * PAGE_SIZE < size; i++) {
+		len = min_t(u32, size - i * PAGE_SIZE, PAGE_SIZE);
+		memcpy_from_page(buf + i * PAGE_SIZE, node->page[i], 0, len);
+	}
+	i = hfs_bnode_test_diff(buf, expected, size);
+	KUNIT_EXPECT_EQ_MSG(test, i, size, "node contents differ at offset %u", i);
+
+	for (i = 1, written = 0; i < (1U << t->order); i++) {
+		p = kmap_local_page(t->block + i);
+		if (memchr_inv(p, 0, PAGE_SIZE))
+			written++;
+		kunmap_local(p);
+	}
+	KUNIT_EXPECT_EQ_MSG(test, written, 0, "%u guard pages were written", written);
+	kunit_kfree(test, buf);
+}
+
+/*
+ * An offset in the node that is a page boundary when nodes span several
+ * pages, and a valid offset otherwise
+ */
+static u32 hfs_bnode_test_boundary(struct hfs_btree *tree)
+{
+	return min_t(u32, PAGE_SIZE, tree->node_size / 2);
+}
+
+static u8 *hfs_bnode_test_pattern(struct kunit *test, u32 size, u8 seed)
+{
+	u8 *buf;
+	u32 i;
+
+	buf = kunit_kmalloc(test, size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+	for (i = 0; i < size; i++)
+		buf[i] = seed + i * 7 + (i >> 8);
+
+	return buf;
+}
+
+/* Write a few ranges, including ones crossing page boundaries and the node's end */
+static void hfs_bnode_write_test(struct kunit *test)
+{
+	struct hfs_btree *tree = hfs_bnode_test_tree(test);
+	struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree);
+	u32 size = tree->node_size;
+	u32 pb = hfs_bnode_test_boundary(tree);
+	const struct { u32 off, len; } ranges[] = {
+		{ 0, sizeof(struct hfs_bnode_desc) },
+		{ pb - 3, 7 },
+		{ size / 2 - 100, 300 },
+		{ size - 6, 6 },
+	};
+	u8 *expected, *data, *buf;
+	u32 i;
+
+	expected = kunit_kzalloc(test, size, GFP_KERNEL);
+	buf = kunit_kzalloc(test, size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, expected);
+	KUNIT_ASSERT_NOT_NULL(test, buf);
+	data = hfs_bnode_test_pattern(test, size, 0x11);
+
+	for (i = 0; i < ARRAY_SIZE(ranges); i++) {
+		hfs_bnode_write(t->node, data, ranges[i].off, ranges[i].len);
+		memcpy(expected + ranges[i].off, data, ranges[i].len);
+
+		hfs_bnode_read(t->node, buf, ranges[i].off, ranges[i].len);
+		KUNIT_EXPECT_MEMEQ_MSG(test, buf, data, ranges[i].len,
+				       "read back at offset %u", ranges[i].off);
+	}
+
+	/* the record offset at the end of the node */
+	hfs_bnode_write_u16(t->node, size - 2, 0x1234);
+	expected[size - 2] = 0x12;
+	expected[size - 1] = 0x34;
+	KUNIT_EXPECT_EQ(test, 0x1234, hfs_bnode_read_u16(t->node, size - 2));
+
+	hfs_bnode_test_check(test, t, expected);
+}
+
+static void hfs_bnode_clear_test(struct kunit *test)
+{
+	struct hfs_btree *tree = hfs_bnode_test_tree(test);
+	struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree);
+	u32 size = tree->node_size;
+	u32 pb = hfs_bnode_test_boundary(tree);
+	u8 *expected;
+
+	expected = hfs_bnode_test_pattern(test, size, 0x5a);
+	hfs_bnode_write(t->node, expected, 0, size);
+	hfs_bnode_test_check(test, t, expected);
+
+	hfs_bnode_clear(t->node, pb - 5, 10);
+	memset(expected + pb - 5, 0, 10);
+	hfs_bnode_test_check(test, t, expected);
+
+	hfs_bnode_clear(t->node, size / 4 + 1, size / 2);
+	memset(expected + size / 4 + 1, 0, size / 2);
+	hfs_bnode_test_check(test, t, expected);
+
+	/* what hfs_bnode_put() does for a deleted node */
+	hfs_bnode_clear(t->node, 0, size);
+	memset(expected, 0, size);
+	hfs_bnode_test_check(test, t, expected);
+}
+
+static void hfs_bnode_copy_test(struct kunit *test)
+{
+	struct hfs_btree *tree = hfs_bnode_test_tree(test);
+	struct hfs_bnode_test_node *src = hfs_bnode_test_alloc(test, tree);
+	struct hfs_bnode_test_node *dst = hfs_bnode_test_alloc(test, tree);
+	u32 size = tree->node_size;
+	u32 pb = hfs_bnode_test_boundary(tree);
+	const struct { u32 dst, src, len; } copies[] = {
+		{ 14, pb - 10, 300 },
+		{ size - 600, 100, 600 },
+		{ pb + 1, 3, size / 2 - 2 },
+		{ 3, 5, size - 8 },
+	};
+	u8 *data, *expected;
+	u32 i;
+
+	data = hfs_bnode_test_pattern(test, size, 0x21);
+	hfs_bnode_write(src->node, data, 0, size);
+	hfs_bnode_test_check(test, src, data);
+
+	expected = kunit_kzalloc(test, size, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, expected);
+	for (i = 0; i < ARRAY_SIZE(copies); i++) {
+		hfs_bnode_copy(dst->node, copies[i].dst, src->node, copies[i].src,
+			       copies[i].len);
+		memcpy(expected + copies[i].dst, data + copies[i].src, copies[i].len);
+		hfs_bnode_test_check(test, dst, expected);
+	}
+	hfs_bnode_test_check(test, src, data);
+}
+
+static void hfs_bnode_move_test(struct kunit *test)
+{
+	struct hfs_btree *tree = hfs_bnode_test_tree(test);
+	struct hfs_bnode_test_node *t = hfs_bnode_test_alloc(test, tree);
+	u32 size = tree->node_size;
+	u32 pb = hfs_bnode_test_boundary(tree);
+	const struct { u32 dst, src, len; } moves[] = {
+		/* overlapping, towards the end (hfs_brec_insert()) */
+		{ 102, 100, size - 200 },
+		{ pb + 6, pb - 6, size / 2 - 6 },
+		/* overlapping, towards the start (hfs_brec_remove()) */
+		{ 100, 102, size - 200 },
+		{ pb - 6, pb + 6, size / 2 - 6 },
+		/* disjoint */
+		{ size / 2 + 3, 14, size / 2 - 20 },
+		{ 14, size / 2 + 3, size / 2 - 20 },
+		/* within one page */
+		{ 16, 14, 2 },
+	};
+	u8 *expected;
+	u32 i;
+
+	expected = hfs_bnode_test_pattern(test, size, 0x37);
+	hfs_bnode_write(t->node, expected, 0, size);
+
+	for (i = 0; i < ARRAY_SIZE(moves); i++) {
+		hfs_bnode_move(t->node, moves[i].dst, moves[i].src, moves[i].len);
+		memmove(expected + moves[i].dst, expected + moves[i].src,
+			moves[i].len);
+		hfs_bnode_test_check(test, t, expected);
+	}
+}
+
+static struct kunit_case hfs_bnode_test_cases[] = {
+	KUNIT_CASE(hfs_bnode_write_test),
+	KUNIT_CASE(hfs_bnode_clear_test),
+	KUNIT_CASE(hfs_bnode_copy_test),
+	KUNIT_CASE(hfs_bnode_move_test),
+	{}
+};
+
+static struct kunit_suite hfs_bnode_test_suite = {
+	.name = "hfs_bnode",
+	.test_cases = hfs_bnode_test_cases,
+};
+
+kunit_test_suite(hfs_bnode_test_suite);
+
+MODULE_DESCRIPTION("KUnit tests for HFS b-tree node operations");
+MODULE_LICENSE("GPL");
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
-- 
2.53.0



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04  5:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04  5:36 [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page Mar Dominguez
2026-12-04  5:38 ` [PATCH 1/3] hfs: fix b-tree node writes past the first page Mar Dominguez
2026-12-04  5:41 ` [PATCH 2/3] hfs: check the b-tree node size range in hfs_btree_open() Mar Dominguez
2026-12-04  5:43 ` [PATCH 3/3] hfs: add KUnit tests for b-tree node operations Mar Dominguez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®