Re: [PATCH 0/3] hfs: fix writes to b-tree nodes larger than a page

From: Viacheslav Dubeyko

Date: Fri Dec 04 2026 - 14:32:02 EST


On Fri, 2026-12-04 at 05:36 +0000, Mar Dominguez wrote:
> 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

Please, correct me if I wrong. Have you implemented support of node
size bigger than 512 bytes in HFS?

Inside Macintosh: Files (the original HFS spec)

Chapter 2, "Data Organization on Volumes", in the B*-Trees section
(Files-104):

- Under Nodes:
▎ "A B*-tree file consists entirely of objects called nodes, each of
which is 512 bytes long."
- Under Header Nodes, describing the BTHdrRec field bthNodeSize:
Integer; {size of a node}:
▎ "The size (in bytes) of a node. Currently, this is always 512."
- Under Map Nodes, the map record size is worked out from the 512-byte
node:
▎ "occupies 494 bytes (512 bytes in the node, less 14 bytes for the
node descriptor and 2 bytes for each of the two record offsets at the
end of the node)."

TN1150 "HFS Plus Volume Format"

TN1150 is Apple's HFS+ spec, and it also restates the HFS rule
(tn1150).

- In B-Trees › HFS and HFS Plus B-Trees Compared › Node Sizes:
▎ "In an HFS B-tree, nodes always have a fixed size of 512 bytes."

▎ "In an HFS Plus B-tree, the node size is determined by a field
(nodeSize) in the header node. The node size must be a power from 512
through 32,768. An implementation must use the nodeSize field to
determine the actual node size."
- Table 1, "HFS and HFS Plus Compared", lists the catalog node size as
512 bytes (HFS) against 4 KB (HFS Plus).
- In the Header Record section, nodeSize is described as "a power of
two, from 512 through 32,768, inclusive". That describes the HFS+
header.

- The format has one valid node size for HFS: 512. TN1150 says it
without any qualifier.
- bthNodeSize is still a field in the header, and Inside Macintosh says
"currently" always 512. So the format left room for other sizes, but no
Apple HFS implementation ever used them.

So, we should support only 512 bytes node size. This a difference
between HFS and HFS+. HFS supports only 512 bytes b-tree nodes and
nodes share memory page. For the case of HFS+, b-tree node cannot be
smaller than 4K, if I remember correctly. I assume that we could have
issue in HFS b-tree nodes management code. But node size should be 512
bytes always for the case of HFS.

Thanks,
Slava.


> 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