mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key()
       [not found] <20261127045416.1160-1-ozgunkultekin@gmail.com>
@ 2026-12-01  9:42 ` Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 1/3] hfsplus: " Ozgun Kultekin
                     ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ozgun Kultekin @ 2026-12-01  9:42 UTC (permalink / raw)
  To: slava, glaubitz, frank.li
  Cc: linux-fsdevel, yangguikun19, stable, linux-kernel, kovalev,
	brauner, cengiz.can, Ozgun Kultekin

hfs_bnode_read_key() checks the on-disk key length against the size of
the key union, but the buffers from hfs_find_init() are only
max_key_len + 2 bytes each. A crafted extents leaf key overflows them.

Changes in v5, following Viacheslav's review of v4:

- One allocation or two: two kzalloc() calls would not be measurably
  slower, since both buffers are small. But every hfs_find_init() would
  allocate twice, and the error path and hfs_find_exit() would need a
  second kfree(). I kept the single allocation so the fixes stay small
  for stable, and wrote it the way you suggested:
  kzalloc(fd->key_capacity * 2, ...) with a comment saying that
  search_key and key share it. HFS_FIND_KEY_BUFS is gone. If you would
  rather have two allocations, I can send that as a separate patch.

- HFSPLUS_KEY_LEN_SIZE is gone. hfs_find_init() now has the local
  constant you proposed:
  const size_t key_len_size = sizeof_field(hfsplus_btree_key, key_len);
  HFS_KEY_LEN_MAX_SIZE is gone from hfs as well. Its local is
  sizeof(u16), because the key length is a u16 in HFS_TREE_BIGKEYS
  trees, and a comment says so. The allocation size is unchanged in
  both drivers.

- key_len < 1: it came in with bb5e07cb9277 ("hfs/hfsplus: fix
  slab-out-of-bounds in hfs_bnode_read_key"), and neither the commit
  message nor its thread says why. As far as I can see it is never
  true. In hfsplus key_len is either hfs_bnode_read_u16() + 2 (2 to
  65537) or max_key_len + 2. In hfs it is hfs_bnode_read_u8() + 1 or
  max_key_len + 1. hfs_bnode_read() zeroes the buffer before it checks
  the offset, so even a bad offset gives a length of 2 (1 in hfs). The
  original patch printed key_len with %d, so it was probably an int at
  the time, but even then 0 and negative values could not reach the
  check. Now that key_len is a u32, a negative value is not possible at
  all. I left the check as it is so the fixes change only what they
  need to. If you agree, it can go in a separate cleanup.

- 3/3, fd->key_capacity in hfs_btree_inc_height(): no, that function
  only gets the tree, while its callers have the hfs_find_data. The
  key_size it reads from disk is used only to copy the key from the old
  root to the new root node and to place the child pointer.
  hfs_bnode_copy() and hfs_bnode_write() clamp that to the node through
  check_and_correct_requested_length(), and nothing is copied into
  search_key or key there. The tree-level bound would be
  max_key_len + 2, the same value as key_capacity. Rejecting a longer
  key there changes behaviour, so it belongs in its own fix, not in
  this no-functional-change cleanup. I can send that separately.

- 3/3, map_node_bits in hfsplus/xattr.c is now
  (node_size - HFS_BNODE_DESC_SIZE - (sizeof(u16) * 2) - 2) * 8.

- for-next: v4 did conflict with "hfs: fix orphaned b-tree node in
  hfs_bnode_split()". v5 is based on for-next (d536283b12c4). 1/3 and
  2/3 applied unchanged. In 3/3, hfs_bnode_split() in hfs now has the
  node_desc_size local from that commit, and it is converted the same
  way as the hfsplus one.

Tested on for-next d536283b12c4 (7.4-rc1) with KASAN in QEMU, with and
without the series:
- hfsplus crafted images: without the series, the mount of the first
  image fails with EINVAL after the overflow has corrupted the heap.
  KASAN then reports a use-after-free in rcu_cblist_dequeue() and the
  kernel oopses on a NULL pointer. With the series the key is rejected
  with "Invalid key length: 516", there is no KASAN report, and the two
  other images mount.
- hfsplus image made by mkfs.hfsplus: create, rename, delete, fill to
  ENOSPC, xattrs, appends and remount work, and fsck.hfsplus reports it
  clean, with and without the series.
- hfs image made by hformat: 400 creates, 200 deletes, fragmented
  appends and remount work, and hfsutils reads the result, with and
  without the series.

checkpatch --strict shows no errors. The remaining items are the
"Unknown commit id" warning for 1da177e4c3f4, which my shallow clone
does not have, and the extern CHECK on the hfs prototype, which is kept
to match its neighbours. Each patch builds on its own with W=1 and no
warnings. 1/3 and 2/3 still apply to 6.12.y and 6.6.y, except for the
same context conflict in hfs_find_init() as in v4.

v4: https://lore.kernel.org/all/20261127045416.1160-1-ozgunkultekin@gmail.com/
v3: https://lore.kernel.org/all/20261119005943.37851-1-ozgunkultekin@gmail.com/
v2: https://lore.kernel.org/all/20261118013337.67813-1-ozgunkultekin@gmail.com/
v1: https://lore.kernel.org/all/20261114043522.48764-1-ozgunkultekin@gmail.com/

Ozgun Kultekin (3):
  hfsplus: pass the key buffer size to hfs_bnode_read_key()
  hfs: pass the key buffer size to hfs_bnode_read_key()
  hfs/hfsplus: name the node descriptor size and the first record offset

 fs/hfs/bfind.c             | 15 ++++++++--
 fs/hfs/bnode.c             |  9 +++---
 fs/hfs/brec.c              | 49 +++++++++++++++++++-------------
 fs/hfs/btree.c             |  9 +++---
 fs/hfs/btree.h             |  4 ++-
 fs/hfsplus/bfind.c         | 11 ++++++--
 fs/hfsplus/bnode.c         |  9 +++---
 fs/hfsplus/brec.c          | 57 +++++++++++++++++++++++---------------
 fs/hfsplus/btree.c         |  9 +++---
 fs/hfsplus/hfsplus_fs.h    | 19 +++++++------
 fs/hfsplus/xattr.c         |  8 +++---
 include/linux/hfs_common.h |  6 ++++
 12 files changed, 129 insertions(+), 76 deletions(-)


base-commit: d536283b12c4597f8fc8cc65a034edb8753a9880
-- 
2.39.5


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

* [PATCH v5 1/3] hfsplus: pass the key buffer size to hfs_bnode_read_key()
  2026-12-01  9:42 ` [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key() Ozgun Kultekin
@ 2026-12-01  9:42   ` Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 2/3] hfs: " Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 3/3] hfs/hfsplus: name the node descriptor size and the first record offset Ozgun Kultekin
  2 siblings, 0 replies; 4+ messages in thread
From: Ozgun Kultekin @ 2026-12-01  9:42 UTC (permalink / raw)
  To: slava, glaubitz, frank.li
  Cc: linux-fsdevel, yangguikun19, stable, linux-kernel, kovalev,
	brauner, cengiz.can, Ozgun Kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all
records of variable-key and attributes trees) from disk and only checks
it against sizeof(hfsplus_btree_key), 518 bytes. The destination is
always one of the two key buffers hfs_find_init() allocates, each
tree->max_key_len + 2 bytes long: max_key_len in the b-tree header
record does not count the __be16 key_len field, as hfs_btree_open()
checks (HFSPLUS_EXT_KEYLEN - sizeof(u16) and so on). For the extents
tree max_key_len is fixed at 10, so the whole allocation is 24 bytes and
a crafted leaf key writes up to 516 bytes from the image into
kmalloc-32. The error path clears 518 bytes of the same buffer.

hfs_brec_insert() reaches this with a key that was never validated:
when the tree root is 0 it reads the key of record 0 of the leaf it
inserts into, and after a split it reads the first key of the new node.
A read-write mount that extends the catalog, or an append to a file
whose extents spill into the extents tree, is enough.

Do not let hfs_bnode_read_key() assume the size of the buffer. Store
the capacity of each key buffer in struct hfs_find_data as key_capacity
in hfs_find_init(), allocate the two buffers from it, and pass it to
every hfs_bnode_read_key() call. Keys longer than the buffer are
rejected as before, and the error path clears only the buffer.

The allocation size does not change: max_key_len * 2 + 4 is now
key_capacity * 2, where key_capacity is max_key_len plus the size of the
key_len field. key_capacity goes after bnode, where it pairs with
record, so struct hfs_find_data stays 56 bytes on 64-bit.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")
Reported-by: Guikun Yang <yangguikun19@qq.com>
Closes: https://lore.kernel.org/all/tencent_F85118A9AFDF9B230D4E8FA672510A3E2F08@qq.com/
Suggested-by: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: stable@vger.kernel.org
Signed-off-by: Ozgun Kultekin <ozgunkultekin@gmail.com>
---

Notes:
    v5: search_key and key stay in one allocation, now written as
        key_capacity * 2 with a comment, and the size of key_len is a local
        constant; HFS_FIND_KEY_BUFS and HFSPLUS_KEY_LEN_SIZE are gone.
        Rebased on the hfs tree for-next branch. As suggested by
        Viacheslav.
    v4: https://lore.kernel.org/all/20261127045416.1160-2-ozgunkultekin@gmail.com/
    v3: https://lore.kernel.org/all/20261119005943.37851-3-ozgunkultekin@gmail.com/
    v2: https://lore.kernel.org/all/20261118013337.67813-1-ozgunkultekin@gmail.com/
    v1: https://lore.kernel.org/all/20261114043522.48764-1-ozgunkultekin@gmail.com/

 fs/hfsplus/bfind.c      | 11 +++++++++--
 fs/hfsplus/bnode.c      |  7 ++++---
 fs/hfsplus/brec.c       | 12 ++++++++----
 fs/hfsplus/hfsplus_fs.h | 11 +++++++----
 4 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/fs/hfsplus/bfind.c b/fs/hfsplus/bfind.c
index ca9813f58a..657f22df1a 100644
--- a/fs/hfsplus/bfind.c
+++ b/fs/hfsplus/bfind.c
@@ -14,16 +14,23 @@
 
 int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
+	const size_t key_len_size = sizeof_field(hfsplus_btree_key, key_len);
 	void *ptr;
 
 	fd->tree = tree;
 	fd->bnode = NULL;
 	hfs_find_result_init(fd);
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/*
+	 * max_key_len does not count the key_len field at the start of
+	 * every key, so each key buffer needs max_key_len + key_len_size
+	 * bytes. search_key and key share one allocation of two buffers.
+	 */
+	fd->key_capacity = tree->max_key_len + key_len_size;
+	ptr = kzalloc(fd->key_capacity * 2, GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 	fd->search_key = ptr;
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_capacity;
 	hfs_dbg("cnid %d, caller %ps\n",
 		tree->cnid, __builtin_return_address(0));
 	mutex_lock_nested(&tree->tree_lock,
diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
index 3a1a1fa6f2..144348bdbf 100644
--- a/fs/hfsplus/bnode.c
+++ b/fs/hfsplus/bnode.c
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 	return data;
 }
 
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_capacity)
 {
 	struct hfs_btree *tree;
 	u32 key_len;
@@ -84,8 +85,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 2;
 
-	if (key_len > sizeof(hfsplus_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfsplus_btree_key));
+	if (key_len > key_capacity || key_len < 1) {
+		memset(key, 0, key_capacity);
 		pr_err("hfsplus: Invalid key length: %u\n", key_len);
 		return;
 	}
diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
index f416e562d4..c9fb6873ac 100644
--- a/fs/hfsplus/brec.c
+++ b/fs/hfsplus/brec.c
@@ -148,7 +148,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 	 * at the start of the node and it is not the new node
 	 */
 	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off + size);
+		hfs_bnode_read_key(node, fd->search_key, data_off + size,
+				   fd->key_capacity);
 		hfs_brec_update_parent(fd);
 	}
 
@@ -166,7 +167,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14,
+				   fd->key_capacity);
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
 
 		hfs_bnode_put(new_node);
@@ -464,7 +466,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14,
+				   fd->key_capacity);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
@@ -476,7 +479,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14);
+			hfs_bnode_read_key(node, fd->search_key, 14,
+					   fd->key_capacity);
 		}
 		new_node = NULL;
 	}
diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
index dd8f717943..3d33f2df0b 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -286,13 +286,15 @@ static inline void hfsplus_mark_inode_dirty(struct inode *inode,
 }
 
 struct hfs_find_data {
-	/* filled by caller */
+	/* set by hfs_find_init() */
 	hfsplus_btree_key *search_key;
 	hfsplus_btree_key *key;
-	/* filled by find */
 	struct hfs_btree *tree;
+	/* filled by find */
 	struct hfs_bnode *bnode;
-	/* filled by findrec */
+	/* set by hfs_find_init(): size of each of search_key and key */
+	u32 key_capacity;
+	/* filled by findrec, reset by hfs_find_result_init() */
 	int record;
 	int keyoffset, keylength;
 	int entryoffset, entrylength;
@@ -403,7 +405,8 @@ void hfs_bmap_free(struct hfs_bnode *node);
 void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
 u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_capacity);
 void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
 void hfs_bnode_clear(struct hfs_bnode *node, u32 off, u32 len);
-- 
2.39.5


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

* [PATCH v5 2/3] hfs: pass the key buffer size to hfs_bnode_read_key()
  2026-12-01  9:42 ` [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key() Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 1/3] hfsplus: " Ozgun Kultekin
@ 2026-12-01  9:42   ` Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 3/3] hfs/hfsplus: name the node descriptor size and the first record offset Ozgun Kultekin
  2 siblings, 0 replies; 4+ messages in thread
From: Ozgun Kultekin @ 2026-12-01  9:42 UTC (permalink / raw)
  To: slava, glaubitz, frank.li
  Cc: linux-fsdevel, yangguikun19, stable, linux-kernel, kovalev,
	brauner, cengiz.can, Ozgun Kultekin

hfs_bnode_read_key() reads the key length of leaf records (and of all
records of variable-key trees) from disk and only checks it against
sizeof(hfs_btree_key). The destination is always one of the two key
buffers hfs_find_init() allocates, each tree->max_key_len + 2 bytes
long: max_key_len in the b-tree header record does not count the key
length field, as hfs_btree_open() checks (HFS_MAX_EXT_KEYLEN is
sizeof(struct hfs_ext_key) - sizeof(u8)). For the extents tree
max_key_len is 7, so the whole allocation is 18 bytes and a crafted leaf
key writes up to 256 bytes from the image past it. The error path
clears sizeof(hfs_btree_key) bytes of the same buffer.

hfs_brec_insert() and hfs_brec_update_parent() reach this with keys
that were never validated, the same way as in hfsplus.

Store the capacity of each key buffer in struct hfs_find_data as
key_capacity in hfs_find_init(), allocate the two buffers from it, and
pass it to every hfs_bnode_read_key() call. Keys longer than the buffer
are rejected as before, and the error path clears only the buffer.

The allocation size does not change: max_key_len * 2 + 4 is now
key_capacity * 2, where key_capacity is max_key_len plus sizeof(u16),
room for the 16-bit key length of HFS_TREE_BIGKEYS trees. key_capacity
goes after bnode, where it pairs with record, so struct hfs_find_data
stays 56 bytes on 64-bit.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes: bb5e07cb9277 ("hfs/hfsplus: fix slab-out-of-bounds in hfs_bnode_read_key")
Reported-by: Guikun Yang <yangguikun19@qq.com>
Closes: https://lore.kernel.org/all/tencent_F85118A9AFDF9B230D4E8FA672510A3E2F08@qq.com/
Suggested-by: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: stable@vger.kernel.org
Signed-off-by: Ozgun Kultekin <ozgunkultekin@gmail.com>
---

Notes:
    v5: the same changes as in the hfsplus patch: key_capacity * 2 with a
        comment, a local key_len_size, HFS_FIND_KEY_BUFS and
        HFS_KEY_LEN_MAX_SIZE are gone. Rebased on the hfs tree for-next
        branch. As suggested by Viacheslav.
    v4: https://lore.kernel.org/all/20261127045416.1160-3-ozgunkultekin@gmail.com/
    v3: https://lore.kernel.org/all/20261119005943.37851-4-ozgunkultekin@gmail.com/
    v2: https://lore.kernel.org/all/20261118013337.67813-1-ozgunkultekin@gmail.com/
    v1: https://lore.kernel.org/all/20261114043522.48764-1-ozgunkultekin@gmail.com/

 fs/hfs/bfind.c | 15 +++++++++++++--
 fs/hfs/bnode.c |  7 ++++---
 fs/hfs/brec.c  | 12 ++++++++----
 fs/hfs/btree.h |  4 +++-
 4 files changed, 28 insertions(+), 10 deletions(-)

diff --git a/fs/hfs/bfind.c b/fs/hfs/bfind.c
index d56e47bdc5..98e2774fd6 100644
--- a/fs/hfs/bfind.c
+++ b/fs/hfs/bfind.c
@@ -14,6 +14,11 @@
 
 int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 {
+	/*
+	 * The key length field is a u8, or a u16 in HFS_TREE_BIGKEYS trees
+	 * (see hfs_brec_keylen()); leave room for the larger one.
+	 */
+	const size_t key_len_size = sizeof(u16);
 	void *ptr;
 
 	if (!tree || !fd)
@@ -21,11 +26,17 @@ int hfs_find_init(struct hfs_btree *tree, struct hfs_find_data *fd)
 
 	fd->tree = tree;
 	fd->bnode = NULL;
-	ptr = kzalloc(tree->max_key_len * 2 + 4, GFP_KERNEL);
+	/*
+	 * max_key_len does not count the key length field at the start of
+	 * every key, so each key buffer needs max_key_len + key_len_size
+	 * bytes. search_key and key share one allocation of two buffers.
+	 */
+	fd->key_capacity = tree->max_key_len + key_len_size;
+	ptr = kzalloc(fd->key_capacity * 2, GFP_KERNEL);
 	if (!ptr)
 		return -ENOMEM;
 	fd->search_key = ptr;
-	fd->key = ptr + tree->max_key_len + 2;
+	fd->key = ptr + fd->key_capacity;
 	hfs_dbg("cnid %d, caller %ps\n",
 		tree->cnid, __builtin_return_address(0));
 	switch (tree->cnid) {
diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c
index ec1b0189a4..f2781afea7 100644
--- a/fs/hfs/bnode.c
+++ b/fs/hfs/bnode.c
@@ -71,7 +71,8 @@ u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off)
 	return data;
 }
 
-void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
+void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			u32 key_capacity)
 {
 	struct hfs_btree *tree;
 	u32 key_len;
@@ -83,8 +84,8 @@ void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off)
 	else
 		key_len = tree->max_key_len + 1;
 
-	if (key_len > sizeof(hfs_btree_key) || key_len < 1) {
-		memset(key, 0, sizeof(hfs_btree_key));
+	if (key_len > key_capacity || key_len < 1) {
+		memset(key, 0, key_capacity);
 		pr_err("hfs: Invalid key length: %u\n", key_len);
 		return;
 	}
diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c
index 1ca1ee9f73..15520b2b15 100644
--- a/fs/hfs/brec.c
+++ b/fs/hfs/brec.c
@@ -138,7 +138,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 	 * at the start of the node and it is not the new node
 	 */
 	if (!rec && new_node != node) {
-		hfs_bnode_read_key(node, fd->search_key, data_off + size);
+		hfs_bnode_read_key(node, fd->search_key, data_off + size,
+				   fd->key_capacity);
 		hfs_brec_update_parent(fd);
 	}
 
@@ -156,7 +157,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14,
+				   fd->key_capacity);
 		__hfs_brec_find(fd->bnode, fd);
 
 		hfs_bnode_put(new_node);
@@ -458,7 +460,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14);
+		hfs_bnode_read_key(new_node, fd->search_key, 14,
+				   fd->key_capacity);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd);
@@ -470,7 +473,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14);
+			hfs_bnode_read_key(node, fd->search_key, 14,
+					   fd->key_capacity);
 		}
 		new_node = NULL;
 	}
diff --git a/fs/hfs/btree.h b/fs/hfs/btree.h
index 1f703f2c53..b42c47b212 100644
--- a/fs/hfs/btree.h
+++ b/fs/hfs/btree.h
@@ -79,6 +79,7 @@ struct hfs_find_data {
 	btree_key *search_key;
 	struct hfs_btree *tree;
 	struct hfs_bnode *bnode;
+	u32 key_capacity;
 	int record;
 	int keyoffset, keylength;
 	int entryoffset, entrylength;
@@ -117,7 +118,8 @@ extern void hfs_bmap_free(struct hfs_bnode *node);
 extern void hfs_bnode_read(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern u16 hfs_bnode_read_u16(struct hfs_bnode *node, u32 off);
 extern u8 hfs_bnode_read_u8(struct hfs_bnode *node, u32 off);
-extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off);
+extern void hfs_bnode_read_key(struct hfs_bnode *node, void *key, u32 off,
+			       u32 key_capacity);
 extern void hfs_bnode_write(struct hfs_bnode *node, void *buf, u32 off, u32 len);
 extern void hfs_bnode_write_u16(struct hfs_bnode *node, u32 off, u16 data);
 extern void hfs_bnode_write_u8(struct hfs_bnode *node, u32 off, u8 data);
-- 
2.39.5


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

* [PATCH v5 3/3] hfs/hfsplus: name the node descriptor size and the first record offset
  2026-12-01  9:42 ` [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key() Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 1/3] hfsplus: " Ozgun Kultekin
  2026-12-01  9:42   ` [PATCH v5 2/3] hfs: " Ozgun Kultekin
@ 2026-12-01  9:42   ` Ozgun Kultekin
  2 siblings, 0 replies; 4+ messages in thread
From: Ozgun Kultekin @ 2026-12-01  9:42 UTC (permalink / raw)
  To: slava, glaubitz, frank.li
  Cc: linux-fsdevel, yangguikun19, stable, linux-kernel, kovalev,
	brauner, cengiz.can, Ozgun Kultekin

Both drivers spell the size of struct hfs_bnode_desc, which is also the
offset of record 0 in a node, in several ways: the literal 14 in brec.c
and btree.c, a local node_desc_size in hfs_bnode_split() of both
drivers, local desc_size variables in hfsplus_fs.h, and
sizeof(struct hfs_bnode_desc) in bnode.c, btree.c and hfsplus/xattr.c.

Add two names next to the structure: HFS_BNODE_DESC_SIZE for the size
and HFS_BNODE_FIRST_REC_OFF for the offset of record 0, and use them
everywhere. In hfs_btree_inc_height() keep the offset of the child
pointer in a local variable and write its length as sizeof(cnid)
instead of 4. Align the conditions next to the changed lines.

No functional change.

Suggested-by: Viacheslav Dubeyko <slava@dubeyko.com>
Signed-off-by: Ozgun Kultekin <ozgunkultekin@gmail.com>
---

Notes:
    v5: rebased on the hfs tree for-next branch; hfs_bnode_split() in hfs
        now has the node_desc_size local from "hfs: fix orphaned b-tree
        node in hfs_bnode_split()", converted like the hfsplus one.
        map_node_bits in hfsplus/xattr.c reordered to
        (node_size - ...) * 8, as suggested by Viacheslav.
    v4: https://lore.kernel.org/all/20261127045416.1160-4-ozgunkultekin@gmail.com/
    v3: https://lore.kernel.org/all/20261119005943.37851-2-ozgunkultekin@gmail.com/

 fs/hfs/bnode.c             |  2 +-
 fs/hfs/brec.c              | 47 ++++++++++++++++++--------------
 fs/hfs/btree.c             |  9 ++++---
 fs/hfsplus/bnode.c         |  2 +-
 fs/hfsplus/brec.c          | 55 +++++++++++++++++++++-----------------
 fs/hfsplus/btree.c         |  9 ++++---
 fs/hfsplus/hfsplus_fs.h    |  8 +++---
 fs/hfsplus/xattr.c         |  8 +++---
 include/linux/hfs_common.h |  6 +++++
 9 files changed, 83 insertions(+), 63 deletions(-)

diff --git a/fs/hfs/bnode.c b/fs/hfs/bnode.c
index f2781afea7..f976e4092a 100644
--- a/fs/hfs/bnode.c
+++ b/fs/hfs/bnode.c
@@ -429,7 +429,7 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
 
 	rec_off = tree->node_size - 2;
 	off = hfs_bnode_read_u16(node, rec_off);
-	if (off != sizeof(struct hfs_bnode_desc))
+	if (off != HFS_BNODE_FIRST_REC_OFF)
 		goto node_error;
 	for (i = 1; i <= node->num_recs; off = next_off, i++) {
 		rec_off -= 2;
diff --git a/fs/hfs/brec.c b/fs/hfs/brec.c
index 15520b2b15..d6ba3722ec 100644
--- a/fs/hfs/brec.c
+++ b/fs/hfs/brec.c
@@ -157,8 +157,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14,
-				   fd->key_capacity);
+		hfs_bnode_read_key(new_node, fd->search_key,
+				   HFS_BNODE_FIRST_REC_OFF, fd->key_capacity);
 		__hfs_brec_find(fd->bnode, fd);
 
 		hfs_bnode_put(new_node);
@@ -257,7 +257,6 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 	int num_recs, new_rec_off, new_off, old_rec_off;
 	int data_start, data_end, size;
 	size_t rec_off_tbl_size;
-	size_t node_desc_size = sizeof(struct hfs_bnode_desc);
 	size_t rec_size = sizeof(__be16);
 
 	tree = fd->tree;
@@ -287,7 +286,7 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 
 	rec_off_tbl_size = node->num_recs * rec_size;
 	size = tree->node_size / 2;
-	size -= node_desc_size;
+	size -= HFS_BNODE_DESC_SIZE;
 	size -= rec_off_tbl_size;
 	old_rec_off = tree->node_size - (2 * rec_size);
 
@@ -319,14 +318,14 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		hfs_bnode_get(new_node);
 		fd->bnode = new_node;
 		fd->record -= num_recs;
-		fd->keyoffset -= data_start - node_desc_size;
-		fd->entryoffset -= data_start - node_desc_size;
+		fd->keyoffset -= data_start - HFS_BNODE_FIRST_REC_OFF;
+		fd->entryoffset -= data_start - HFS_BNODE_FIRST_REC_OFF;
 	}
 	new_node->num_recs = node->num_recs - num_recs;
 	node->num_recs = num_recs;
 
 	new_rec_off = tree->node_size - rec_size;
-	new_off = node_desc_size;
+	new_off = HFS_BNODE_FIRST_REC_OFF;
 	size = data_start - new_off;
 	num_recs = new_node->num_recs;
 	data_end = data_start;
@@ -339,8 +338,8 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		num_recs--;
 	}
 	hfs_bnode_write_u16(new_node, new_rec_off, new_off);
-	hfs_bnode_copy(new_node, node_desc_size,
-			node, data_start, data_end - data_start);
+	hfs_bnode_copy(new_node, HFS_BNODE_FIRST_REC_OFF,
+		       node, data_start, data_end - data_start);
 
 	/* update new bnode header */
 	node_desc.next = cpu_to_be32(new_node->next);
@@ -404,7 +403,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 
 	/* size difference between old and new key */
 	if (tree->attributes & HFS_TREE_VARIDXKEYS)
-		newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1;
+		newkeylen =
+			(hfs_bnode_read_u8(node, HFS_BNODE_FIRST_REC_OFF) | 1) + 1;
 	else
 		fd->keylength = newkeylen = tree->max_key_len + 1;
 	hfs_dbg("rec %d, keylength %d, newkeylen %d\n",
@@ -443,7 +443,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 	hfs_bnode_move(parent, start_off + diff, start_off,
 		       end_off - start_off);
 skip:
-	hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
+	hfs_bnode_copy(parent, fd->keyoffset, node, HFS_BNODE_FIRST_REC_OFF,
+		       newkeylen);
 	if (!(tree->attributes & HFS_TREE_VARIDXKEYS))
 		hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1);
 	hfs_bnode_dump(parent);
@@ -460,8 +461,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14,
-				   fd->key_capacity);
+		hfs_bnode_read_key(new_node, fd->search_key,
+				   HFS_BNODE_FIRST_REC_OFF, fd->key_capacity);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd);
@@ -473,7 +474,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14,
+			hfs_bnode_read_key(node, fd->search_key,
+					   HFS_BNODE_FIRST_REC_OFF,
 					   fd->key_capacity);
 		}
 		new_node = NULL;
@@ -491,6 +493,7 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)
 	struct hfs_bnode *node, *new_node;
 	struct hfs_bnode_desc node_desc;
 	int key_size, rec;
+	u32 cnid_off;
 	__be32 cnid;
 
 	node = NULL;
@@ -528,28 +531,32 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)
 	hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
 
 	rec = tree->node_size - 2;
-	hfs_bnode_write_u16(new_node, rec, 14);
+	hfs_bnode_write_u16(new_node, rec, HFS_BNODE_FIRST_REC_OFF);
 
 	if (node) {
 		/* insert old root idx into new root */
 		node->parent = tree->root;
 		if (node->type == HFS_NODE_LEAF ||
 		    tree->attributes & HFS_TREE_VARIDXKEYS)
-			key_size = hfs_bnode_read_u8(node, 14) + 1;
+			key_size =
+				hfs_bnode_read_u8(node, HFS_BNODE_FIRST_REC_OFF) + 1;
 		else
 			key_size = tree->max_key_len + 1;
-		hfs_bnode_copy(new_node, 14, node, 14, key_size);
+		hfs_bnode_copy(new_node, HFS_BNODE_FIRST_REC_OFF,
+			       node, HFS_BNODE_FIRST_REC_OFF, key_size);
 
 		if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
 			key_size = tree->max_key_len + 1;
-			hfs_bnode_write_u8(new_node, 14, tree->max_key_len);
+			hfs_bnode_write_u8(new_node, HFS_BNODE_FIRST_REC_OFF,
+					   tree->max_key_len);
 		}
 		key_size = (key_size + 1) & -2;
 		cnid = cpu_to_be32(node->this);
-		hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
+		cnid_off = HFS_BNODE_FIRST_REC_OFF + key_size;
+		hfs_bnode_write(new_node, &cnid, cnid_off, sizeof(cnid));
 
 		rec -= 2;
-		hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
+		hfs_bnode_write_u16(new_node, rec, cnid_off + sizeof(cnid));
 
 		hfs_bnode_put(node);
 	}
diff --git a/fs/hfs/btree.c b/fs/hfs/btree.c
index f070384031..c3cd94f219 100644
--- a/fs/hfs/btree.c
+++ b/fs/hfs/btree.c
@@ -216,7 +216,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id, btree_keycmp ke
 
 	/* Load the header */
 	head = (struct hfs_btree_header_rec *)(kmap_local_folio(folio, 0) +
-					       sizeof(struct hfs_bnode_desc));
+					       HFS_BNODE_FIRST_REC_OFF);
 	tree->root = be32_to_cpu(head->root);
 	tree->leaf_count = be32_to_cpu(head->leaf_count);
 	tree->leaf_head = be32_to_cpu(head->leaf_head);
@@ -312,7 +312,7 @@ void hfs_btree_write(struct hfs_btree *tree)
 	/* Load the header */
 	page = node->page[0];
 	head = (struct hfs_btree_header_rec *)(kmap_local_page(page) +
-					       sizeof(struct hfs_bnode_desc));
+					       HFS_BNODE_FIRST_REC_OFF);
 
 	head->root = cpu_to_be32(tree->root);
 	head->leaf_count = cpu_to_be32(tree->leaf_count);
@@ -356,8 +356,9 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
 	desc.num_recs = cpu_to_be16(1);
 	desc.reserved = 0;
 	hfs_bnode_write(node, &desc, 0, sizeof(desc));
-	hfs_bnode_write_u16(node, 14, 0x8000);
-	hfs_bnode_write_u16(node, tree->node_size - 2, 14);
+	hfs_bnode_write_u16(node, HFS_BNODE_FIRST_REC_OFF, 0x8000);
+	hfs_bnode_write_u16(node, tree->node_size - 2,
+			    HFS_BNODE_FIRST_REC_OFF);
 	hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
 
 	return node;
diff --git a/fs/hfsplus/bnode.c b/fs/hfsplus/bnode.c
index 144348bdbf..d91714e97b 100644
--- a/fs/hfsplus/bnode.c
+++ b/fs/hfsplus/bnode.c
@@ -592,7 +592,7 @@ struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
 
 	rec_off = tree->node_size - 2;
 	off = hfs_bnode_read_u16(node, rec_off);
-	if (off != sizeof(struct hfs_bnode_desc))
+	if (off != HFS_BNODE_FIRST_REC_OFF)
 		goto node_error;
 	for (i = 1; i <= node->num_recs; off = next_off, i++) {
 		rec_off -= 2;
diff --git a/fs/hfsplus/brec.c b/fs/hfsplus/brec.c
index c9fb6873ac..c56ca7a852 100644
--- a/fs/hfsplus/brec.c
+++ b/fs/hfsplus/brec.c
@@ -167,8 +167,8 @@ int hfs_brec_insert(struct hfs_find_data *fd, void *entry, u32 entry_len)
 		entry_len = sizeof(cnid);
 
 		/* get index key */
-		hfs_bnode_read_key(new_node, fd->search_key, 14,
-				   fd->key_capacity);
+		hfs_bnode_read_key(new_node, fd->search_key,
+				   HFS_BNODE_FIRST_REC_OFF, fd->key_capacity);
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
 
 		hfs_bnode_put(new_node);
@@ -260,7 +260,6 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 	int num_recs, new_rec_off, new_off, old_rec_off;
 	int data_start, data_end, size;
 	size_t rec_off_tbl_size;
-	size_t node_desc_size = sizeof(struct hfs_bnode_desc);
 	size_t rec_size = sizeof(__be16);
 
 	tree = fd->tree;
@@ -290,7 +289,7 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 
 	rec_off_tbl_size = node->num_recs * rec_size;
 	size = tree->node_size / 2;
-	size -= node_desc_size;
+	size -= HFS_BNODE_DESC_SIZE;
 	size -= rec_off_tbl_size;
 	old_rec_off = tree->node_size - (2 * rec_size);
 
@@ -322,14 +321,14 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		hfs_bnode_get(new_node);
 		fd->bnode = new_node;
 		fd->record -= num_recs;
-		fd->keyoffset -= data_start - node_desc_size;
-		fd->entryoffset -= data_start - node_desc_size;
+		fd->keyoffset -= data_start - HFS_BNODE_FIRST_REC_OFF;
+		fd->entryoffset -= data_start - HFS_BNODE_FIRST_REC_OFF;
 	}
 	new_node->num_recs = node->num_recs - num_recs;
 	node->num_recs = num_recs;
 
 	new_rec_off = tree->node_size - rec_size;
-	new_off = node_desc_size;
+	new_off = HFS_BNODE_FIRST_REC_OFF;
 	size = data_start - new_off;
 	num_recs = new_node->num_recs;
 	data_end = data_start;
@@ -342,8 +341,8 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		num_recs--;
 	}
 	hfs_bnode_write_u16(new_node, new_rec_off, new_off);
-	hfs_bnode_copy(new_node, node_desc_size,
-			node, data_start, data_end - data_start);
+	hfs_bnode_copy(new_node, HFS_BNODE_FIRST_REC_OFF,
+		       node, data_start, data_end - data_start);
 
 	/* update new bnode header */
 	node_desc.next = cpu_to_be32(new_node->next);
@@ -410,8 +409,9 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 
 	/* size difference between old and new key */
 	if ((tree->attributes & HFS_TREE_VARIDXKEYS) ||
-				(tree->cnid == HFSPLUS_ATTR_CNID))
-		newkeylen = hfs_bnode_read_u16(node, 14) + 2;
+	    (tree->cnid == HFSPLUS_ATTR_CNID))
+		newkeylen =
+			hfs_bnode_read_u16(node, HFS_BNODE_FIRST_REC_OFF) + 2;
 	else
 		fd->keylength = newkeylen = tree->max_key_len + 2;
 	hfs_dbg("rec %d, keylength %d, newkeylen %d\n",
@@ -451,7 +451,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 	hfs_bnode_move(parent, start_off + diff, start_off,
 		       end_off - start_off);
 skip:
-	hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
+	hfs_bnode_copy(parent, fd->keyoffset, node, HFS_BNODE_FIRST_REC_OFF,
+		       newkeylen);
 	hfs_bnode_dump(parent);
 
 	hfs_bnode_put(node);
@@ -466,8 +467,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 		}
 		fd->bnode = hfs_bnode_find(tree, new_node->parent);
 		/* create index key and entry */
-		hfs_bnode_read_key(new_node, fd->search_key, 14,
-				   fd->key_capacity);
+		hfs_bnode_read_key(new_node, fd->search_key,
+				   HFS_BNODE_FIRST_REC_OFF, fd->key_capacity);
 		cnid = cpu_to_be32(new_node->this);
 
 		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
@@ -479,7 +480,8 @@ static int hfs_brec_update_parent(struct hfs_find_data *fd)
 			if (new_node == node)
 				goto out;
 			/* restore search_key */
-			hfs_bnode_read_key(node, fd->search_key, 14,
+			hfs_bnode_read_key(node, fd->search_key,
+					   HFS_BNODE_FIRST_REC_OFF,
 					   fd->key_capacity);
 		}
 		new_node = NULL;
@@ -497,6 +499,7 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)
 	struct hfs_bnode *node, *new_node;
 	struct hfs_bnode_desc node_desc;
 	int key_size, rec;
+	u32 cnid_off;
 	__be32 cnid;
 
 	node = NULL;
@@ -534,29 +537,33 @@ static int hfs_btree_inc_height(struct hfs_btree *tree)
 	hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
 
 	rec = tree->node_size - 2;
-	hfs_bnode_write_u16(new_node, rec, 14);
+	hfs_bnode_write_u16(new_node, rec, HFS_BNODE_FIRST_REC_OFF);
 
 	if (node) {
 		/* insert old root idx into new root */
 		node->parent = tree->root;
 		if (node->type == HFS_NODE_LEAF ||
-				tree->attributes & HFS_TREE_VARIDXKEYS ||
-				tree->cnid == HFSPLUS_ATTR_CNID)
-			key_size = hfs_bnode_read_u16(node, 14) + 2;
+		    tree->attributes & HFS_TREE_VARIDXKEYS ||
+		    tree->cnid == HFSPLUS_ATTR_CNID)
+			key_size =
+				hfs_bnode_read_u16(node, HFS_BNODE_FIRST_REC_OFF) + 2;
 		else
 			key_size = tree->max_key_len + 2;
-		hfs_bnode_copy(new_node, 14, node, 14, key_size);
+		hfs_bnode_copy(new_node, HFS_BNODE_FIRST_REC_OFF,
+			       node, HFS_BNODE_FIRST_REC_OFF, key_size);
 
 		if (!(tree->attributes & HFS_TREE_VARIDXKEYS) &&
-				(tree->cnid != HFSPLUS_ATTR_CNID)) {
+		    (tree->cnid != HFSPLUS_ATTR_CNID)) {
 			key_size = tree->max_key_len + 2;
-			hfs_bnode_write_u16(new_node, 14, tree->max_key_len);
+			hfs_bnode_write_u16(new_node, HFS_BNODE_FIRST_REC_OFF,
+					    tree->max_key_len);
 		}
 		cnid = cpu_to_be32(node->this);
-		hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
+		cnid_off = HFS_BNODE_FIRST_REC_OFF + key_size;
+		hfs_bnode_write(new_node, &cnid, cnid_off, sizeof(cnid));
 
 		rec -= 2;
-		hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
+		hfs_bnode_write_u16(new_node, rec, cnid_off + sizeof(cnid));
 
 		hfs_bnode_put(node);
 	}
diff --git a/fs/hfsplus/btree.c b/fs/hfsplus/btree.c
index 0a9b32d5c5..c42f655e71 100644
--- a/fs/hfsplus/btree.c
+++ b/fs/hfsplus/btree.c
@@ -322,7 +322,7 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)
 
 	/* Load the header */
 	head = (struct hfs_btree_header_rec *)(kmap_local_page(page) +
-		sizeof(struct hfs_bnode_desc));
+		HFS_BNODE_FIRST_REC_OFF);
 	tree->root = be32_to_cpu(head->root);
 	tree->leaf_count = be32_to_cpu(head->leaf_count);
 	tree->leaf_head = be32_to_cpu(head->leaf_head);
@@ -454,7 +454,7 @@ int hfs_btree_write(struct hfs_btree *tree)
 	/* Load the header */
 	page = node->page[0];
 	head = (struct hfs_btree_header_rec *)(kmap_local_page(page) +
-		sizeof(struct hfs_bnode_desc));
+		HFS_BNODE_FIRST_REC_OFF);
 
 	head->root = cpu_to_be32(tree->root);
 	head->leaf_count = cpu_to_be32(tree->leaf_count);
@@ -497,8 +497,9 @@ static struct hfs_bnode *hfs_bmap_new_bmap(struct hfs_bnode *prev, u32 idx)
 	desc.num_recs = cpu_to_be16(1);
 	desc.reserved = 0;
 	hfs_bnode_write(node, &desc, 0, sizeof(desc));
-	hfs_bnode_write_u16(node, 14, 0x8000);
-	hfs_bnode_write_u16(node, tree->node_size - 2, 14);
+	hfs_bnode_write_u16(node, HFS_BNODE_FIRST_REC_OFF, 0x8000);
+	hfs_bnode_write_u16(node, tree->node_size - 2,
+			    HFS_BNODE_FIRST_REC_OFF);
 	hfs_bnode_write_u16(node, tree->node_size - 4, tree->node_size - 6);
 
 	return node;
diff --git a/fs/hfsplus/hfsplus_fs.h b/fs/hfsplus/hfsplus_fs.h
index 3d33f2df0b..43062f056f 100644
--- a/fs/hfsplus/hfsplus_fs.h
+++ b/fs/hfsplus/hfsplus_fs.h
@@ -659,16 +659,15 @@ bool hfs_bnode_num_recs_invalid(struct hfs_bnode *node)
 	u32 table_size;
 	u32 area_size;
 	u32 rec_size = sizeof(__be16);
-	u32 desc_size = sizeof(struct hfs_bnode_desc);
 
 	if (!node || !node->tree)
 		return true;
 
 	node_size = node->tree->node_size;
-	if (node_size < desc_size)
+	if (node_size < HFS_BNODE_DESC_SIZE)
 		return true;
 
-	area_size = node_size - desc_size;
+	area_size = node_size - HFS_BNODE_DESC_SIZE;
 	table_size = ((u32)node->num_recs + 1) * rec_size;
 
 	return table_size > area_size;
@@ -691,12 +690,11 @@ bool hfs_brec_offsets_invalid(struct hfs_bnode *node, u16 off, u16 next_off)
 	u32 table_size;
 	u32 table_start;
 	u32 rec_size = sizeof(__be16);
-	u32 desc_size = sizeof(struct hfs_bnode_desc);
 
 	if (!node || !node->tree)
 		return true;
 
-	if (off < desc_size || (off & 1))
+	if (off < HFS_BNODE_FIRST_REC_OFF || (off & 1))
 		return true;
 
 	if (next_off <= off ||
diff --git a/fs/hfsplus/xattr.c b/fs/hfsplus/xattr.c
index 3aeca4d177..37e04fa253 100644
--- a/fs/hfsplus/xattr.c
+++ b/fs/hfsplus/xattr.c
@@ -80,7 +80,7 @@ static u32 hfsplus_init_header_node(struct inode *attr_file,
 	desc = (struct hfs_bnode_desc *)buf;
 	desc->type = HFS_NODE_HEADER;
 	desc->num_recs = cpu_to_be16(HFSPLUS_BTREE_HDR_NODE_RECS_COUNT);
-	offset = sizeof(struct hfs_bnode_desc);
+	offset = HFS_BNODE_FIRST_REC_OFF;
 	*--rec_offsets = cpu_to_be16(offset);
 
 	head = (struct hfs_btree_header_rec *)(buf + offset);
@@ -102,8 +102,8 @@ static u32 hfsplus_init_header_node(struct inode *attr_file,
 		u32 map_node_bits;
 
 		desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
-		map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
-					(2 * sizeof(u16)) - 2);
+		map_node_bits = (node_size - HFS_BNODE_DESC_SIZE -
+				 (sizeof(u16) * 2) - 2) * 8;
 		map_nodes = (be32_to_cpu(head->node_count) -
 				hdr_node_map_rec_bits +
 				(map_node_bits - 1)) / map_node_bits;
@@ -163,7 +163,7 @@ static void hfsplus_init_map_node(u8 *buf, u16 node_size, u32 next_node)
 	rec_offsets = (__be16 *)(buf + node_size);
 
 	/* record #1 */
-	offset = sizeof(struct hfs_bnode_desc);
+	offset = HFS_BNODE_FIRST_REC_OFF;
 	*--rec_offsets = cpu_to_be16(offset);
 
 	/* record #2 */
diff --git a/include/linux/hfs_common.h b/include/linux/hfs_common.h
index eb119a1c56..10324627f9 100644
--- a/include/linux/hfs_common.h
+++ b/include/linux/hfs_common.h
@@ -495,6 +495,12 @@ struct hfs_bnode_desc {
 	u16 reserved;
 } __packed;
 
+/* Size of the node descriptor at the start of every node */
+#define HFS_BNODE_DESC_SIZE	sizeof(struct hfs_bnode_desc)
+
+/* Offset of record 0, which follows the node descriptor */
+#define HFS_BNODE_FIRST_REC_OFF	HFS_BNODE_DESC_SIZE
+
 /* HFS/HFS+ BTree node types */
 #define HFS_NODE_INDEX	0x00	/* An internal (index) node */
 #define HFS_NODE_HEADER	0x01	/* The tree header node (node 0) */
-- 
2.39.5


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

end of thread, other threads:[~2026-12-01  9:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20261127045416.1160-1-ozgunkultekin@gmail.com>
2026-12-01  9:42 ` [PATCH v5 0/3] hfs/hfsplus: pass the key buffer size to hfs_bnode_read_key() Ozgun Kultekin
2026-12-01  9:42   ` [PATCH v5 1/3] hfsplus: " Ozgun Kultekin
2026-12-01  9:42   ` [PATCH v5 2/3] hfs: " Ozgun Kultekin
2026-12-01  9:42   ` [PATCH v5 3/3] hfs/hfsplus: name the node descriptor size and the first record offset Ozgun Kultekin

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®