Skip to contentSkip to conversation

hfsplus: fix b-tree logic issues

PATCH New Viacheslav Dubeyko 1 participant 6 messages Apr 3

Subsystems (3)
lore
Share & tools
lore

Viacheslav Dubeyko

author maintainer
#0 lore raw

cover hfsplus: fix b-tree logic issues

The generic/642 failure uncovered multiple issues
in b-tree functionality. This patchset fixes potential race conditions in b-tree functionality, error processing issue in hfs_bmap_free(), logic of alloc/free b-tree node. It reworks hfsplus_create_attributes_file() logic by introducing a specialized method of hfsplus_init_map_node() and writing the allocated map b-tree's nodes by means of hfsplus_write_attributes_file_node() method. And this patchset adds calling of hfs_bnode_unlink() before hfs_bnode_put() for the case if new node cannot be used for spliting the existing node.

Viacheslav Dubeyko (5):
hfsplus: fix potential race conditions in b-tree functionality
hfsplus: fix error processing issue in hfs_bmap_free()
hfsplus: fix logic of alloc/free b-tree node
hfsplus: rework logic of map nodes creation in xattr b-tree
hfsplus: fix generic/642 failure

 fs/hfsplus/bnode.c         |   3 +
 fs/hfsplus/brec.c          |  32 ++++++----
 fs/hfsplus/btree.c         |  22 +++++--
 fs/hfsplus/inode.c         |  15 ++---
 fs/hfsplus/super.c         |   3 +
 fs/hfsplus/xattr.c         | 127 ++++++++++++++++++++++++++++++-------
 include/linux/hfs_common.h |   2 +
 7 files changed, 156 insertions(+), 48 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/5 hfsplus: fix potential race conditions in b-tree functionality

The HFS_BNODE_DELETED flag is checked in hfs_bnode_put() under locked tree->hash_lock. This patch adds locking for the case of setting the HFS_BNODE_DELETED flag in hfs_bnode_unlink() with the goal to avoid potential race conditions.

The hfs_btree_write() method should be called under tree->tree_lock. This patch reworks logic by adding locking the tree->tree_lock for the calls of hfs_btree_write() in hfsplus_cat_write_inode() and hfsplus_system_write_inode().

This patch adds also the lockdep_assert_held() in hfs_bmap_reserve(), hfs_bmap_alloc(), and hfs_bmap_free().

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/bnode.c |  3 +++
 fs/hfsplus/btree.c |  5 +++++
 fs/hfsplus/inode.c | 15 +++++++--------
 fs/hfsplus/super.c |  3 +++
 4 files changed, 18 insertions(+), 8 deletions(-)
fs/hfsplus/bnode.c +3 -0
index 250a226336ea..f8b5a8ae58ff 100644
@@ -420,7 +420,10 @@ void hfs_bnode_unlink(struct hfs_bnode *node)
 		tree->root = 0;
 		tree->depth = 0;
 	}
+
+	spin_lock(&tree->hash_lock);
 	set_bit(HFS_BNODE_DELETED, &node->flags);
+	spin_unlock(&tree->hash_lock);
 }
 
 static inline int hfs_bnode_hash(u32 num)
fs/hfsplus/btree.c +5 -0
index 04304f952f6b..f1ed7c336593 100644
@@ -500,6 +500,8 @@ int hfs_bmap_reserve(struct hfs_btree *tree, u32 rsvd_nodes)
 	u32 count;
 	int res;
 
+	lockdep_assert_held(&tree->tree_lock);
+
 	if (rsvd_nodes <= 0)
 		return 0;
 
@@ -529,6 +531,8 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
 	u8 *data, byte, m;
 	int i, res;
 
+	lockdep_assert_held(&tree->tree_lock);
+
 	res = hfs_bmap_reserve(tree, 1);
 	if (res)
 		return ERR_PTR(res);
@@ -607,6 +611,7 @@ void hfs_bmap_free(struct hfs_bnode *node)
 	hfs_dbg("node %u\n", node->this);
 	BUG_ON(!node->this);
 	tree = node->tree;
+	lockdep_assert_held(&tree->tree_lock);
 	nidx = node->this;
 	node = hfs_bnode_find(tree, 0);
 	if (IS_ERR(node))
fs/hfsplus/inode.c +7 -8
index cdf08393de44..e8f359d69328 100644
@@ -720,20 +720,19 @@ int hfsplus_cat_write_inode(struct inode *inode)
 					 sizeof(struct hfsplus_cat_file));
 	}
 
+	res = hfs_btree_write(tree);
+	if (res) {
+		pr_err("b-tree write err: %d, ino %lu\n",
+		       res, inode->i_ino);
+		goto out;
+	}
+
 	set_bit(HFSPLUS_I_CAT_DIRTY,
 		&HFSPLUS_I(HFSPLUS_CAT_TREE_I(inode->i_sb))->flags);
 	set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
 out:
 	hfs_find_exit(&fd);
 
-	if (!res) {
-		res = hfs_btree_write(tree);
-		if (res) {
-			pr_err("b-tree write err: %d, ino %lu\n",
-			       res, inode->i_ino);
-		}
-	}
-
 	return res;
 }
 
fs/hfsplus/super.c +3 -0
index 8bd1627ffc9c..44635f92ada9 100644
@@ -153,7 +153,10 @@ static int hfsplus_system_write_inode(struct inode *inode)
 	}
 	hfsplus_inode_write_fork(inode, fork);
 	if (tree) {
+		mutex_lock_nested(&tree->tree_lock,
+				  hfsplus_btree_lock_class(tree));
 		int err = hfs_btree_write(tree);
+		mutex_unlock(&tree->tree_lock);
 
 		if (err) {
 			pr_err("b-tree write err: %d, ino %lu\n",
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/5 hfsplus: fix error processing issue in hfs_bmap_free()

In reply to Viacheslav Dubeyko

Currently, we check only -EINVAL error code in hfs_bmap_free() after calling the hfs_bmap_clear_bit(). It means that other error codes will be silently ignored. This patch adds the checking of all other error codes.

cc Shardul Bankar <[email protected]>
cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/btree.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
fs/hfsplus/btree.c +9 -5
index f1ed7c336593..80aa816ae1c0 100644
@@ -146,8 +146,9 @@ struct hfs_bmap_ctx {
  * Returns the struct page pointer, or an ERR_PTR on failure.
  * Note: The caller is responsible for mapping/unmapping the returned page.
  */
-static struct page *hfs_bmap_get_map_page(struct hfs_bnode *node, struct hfs_bmap_ctx *ctx,
-				u32 byte_offset)
+static struct page *hfs_bmap_get_map_page(struct hfs_bnode *node,
+					  struct hfs_bmap_ctx *ctx,
+					  u32 byte_offset)
 {
 	u16 rec_idx, off16;
 	unsigned int page_off;
@@ -647,9 +648,12 @@ void hfs_bmap_free(struct hfs_bnode *node)
 
 	res = hfs_bmap_clear_bit(node, nidx);
 	if (res == -EINVAL) {
-		pr_crit("trying to free free bnode %u(%d)\n",
-				node->this, node->type);
-	} else if (!res) {
+		pr_crit("trying to free the freed bnode %u(%d)\n",
+			nidx, node->type);
+	} else if (res) {
+		pr_crit("fail to free bnode %u(%d)\n",
+			nidx, node->type);
+	} else {
 		tree->free_nodes++;
 		mark_inode_dirty(tree->inode);
 	}
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/5 hfsplus: fix logic of alloc/free b-tree node

In reply to Viacheslav Dubeyko

The hfs_bmap_alloc() and hfs_bmap_free() modify the b-tree's counters and nodes' bitmap of b-tree. However, hfs_btree_write() synchronizes the state of in-core b-tree's counters and node's bitmap with b-tree's descriptor in header node. Postponing this synchronization could result in inconsistent state of file system volume. This patch adds calling of hfs_btree_write() in hfs_bmap_alloc() and hfs_bmap_free() methods.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/btree.c | 3 +++
 1 file changed, 3 insertions(+)
fs/hfsplus/btree.c +3 -0
index 80aa816ae1c0..761c74ccd653 100644
@@ -564,6 +564,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
 						set_page_dirty(page);
 						kunmap_local(data);
 						tree->free_nodes--;
+						hfs_btree_write(tree);
 						mark_inode_dirty(tree->inode);
 						hfs_bnode_put(node);
 						return hfs_bnode_create(tree,
@@ -585,6 +586,7 @@ struct hfs_bnode *hfs_bmap_alloc(struct hfs_btree *tree)
 		if (!nidx) {
 			hfs_dbg("create new bmap node\n");
 			next_node = hfs_bmap_new_bmap(node, idx);
+			hfs_btree_write(tree);
 		} else
 			next_node = hfs_bnode_find(tree, nidx);
 		hfs_bnode_put(node);
@@ -655,6 +657,7 @@ void hfs_bmap_free(struct hfs_bnode *node)
 			nidx, node->type);
 	} else {
 		tree->free_nodes++;
+		hfs_btree_write(tree);
 		mark_inode_dirty(tree->inode);
 	}
 
Signature · 1 line
2.43.0
author maintainer
#4 lore raw

PATCH 4/5 hfsplus: rework logic of map nodes creation in xattr b-tree

In reply to Viacheslav Dubeyko

In hfsplus_init_header_node() when node_count > 63488 (header bitmap capacity), the code calculates map_nodes, subtracts them from free_nodes, and marks their positions used in the bitmap. However, it doesn't write the actual map node structure (type, record offsets, bitmap) for those physical positions, only node 0 is written.

This patch reworks hfsplus_create_attributes_file() logic by introducing a specialized method of hfsplus_init_map_node() and writing the allocated map b-tree's nodes by means of hfsplus_write_attributes_file_node() method.

cc John Paul Adrian Glaubitz <[email protected]>
cc Yangtao Li <[email protected]>
---
 fs/hfsplus/xattr.c         | 127 ++++++++++++++++++++++++++++++-------
 include/linux/hfs_common.h |   2 +
 2 files changed, 106 insertions(+), 23 deletions(-)
fs/hfsplus/xattr.c +104 -23
index 89e2e7e46e96..75cb74466738 100644
@@ -50,7 +50,7 @@ static bool is_known_namespace(const char *name)
 	return true;
 }
 
-static void hfsplus_init_header_node(struct inode *attr_file,
+static u32 hfsplus_init_header_node(struct inode *attr_file,
 					u32 clump_size,
 					char *buf, u16 node_size)
 {
@@ -59,6 +59,7 @@ static void hfsplus_init_header_node(struct inode *attr_file,
 	u16 offset;
 	__be16 *rec_offsets;
 	u32 hdr_node_map_rec_bits;
+	u32 map_nodes = 0;
 	char *bmp;
 	u32 used_nodes;
 	u32 used_bmp_bytes;
@@ -93,7 +94,6 @@ static void hfsplus_init_header_node(struct inode *attr_file,
 	hdr_node_map_rec_bits = 8 * (node_size - offset - (4 * sizeof(u16)));
 	if (be32_to_cpu(head->node_count) > hdr_node_map_rec_bits) {
 		u32 map_node_bits;
-		u32 map_nodes;
 
 		desc->next = cpu_to_be32(be32_to_cpu(head->leaf_tail) + 1);
 		map_node_bits = 8 * (node_size - sizeof(struct hfs_bnode_desc) -
@@ -116,21 +116,100 @@ static void hfsplus_init_header_node(struct inode *attr_file,
 	*bmp = ~(0xFF >> used_nodes);
 	offset += hdr_node_map_rec_bits / 8;
 	*--rec_offsets = cpu_to_be16(offset);
+
+	return map_nodes;
+}
+
+/*
+ * Initialize a map node buffer. Map nodes have a single bitmap record.
+ */
+static void hfsplus_init_map_node(u8 *buf, u16 node_size, u32 next_node)
+{
+	struct hfs_bnode_desc *desc;
+	__be16 *rec_offsets;
+	size_t rec_size = sizeof(__be16);
+	u16 offset;
+
+	memset(buf, 0, node_size);
+
+	desc = (struct hfs_bnode_desc *)buf;
+	desc->type = HFS_NODE_MAP;
+	desc->num_recs = cpu_to_be16(1);
+	desc->next = cpu_to_be32(next_node);
+
+	/*
+	 * A map node consists of the node descriptor and a single
+	 * map record. The map record is a continuation of the map
+	 * record contained in the header node. The size of the map
+	 * record is the size of the node, minus the size of
+	 * the node descriptor (14 bytes), minus the size of two offsets
+	 * (4 bytes), minus two bytes of free space. That is, the size of
+	 * the map record is the size of the node minus 20 bytes;
+	 * this keeps the length of the map record an even multiple of 4 bytes.
+	 * The start of the map record is not aligned to a 4-byte boundary:
+	 * it starts immediately after the node descriptor
+	 * (at an offset of 14 bytes).
+	 *
+	 * Two record offsets stored at the end of the node:
+	 *   record[1] = start of record 0 -> sizeof(hfs_bnode_desc)
+	 *   record[2] = start of free space
+	 */
+	rec_offsets = (__be16 *)(buf + node_size);
+
+	/* record #1 */
+	offset = sizeof(struct hfs_bnode_desc);
+	*--rec_offsets = cpu_to_be16(offset);
+
+	/* record #2 */
+	offset = node_size;
+	offset -= (u16)HFSPLUS_BTREE_MAP_NODE_RECS_COUNT * rec_size;
+	offset -= HFSPLUS_BTREE_MAP_NODE_RESERVED_BYTES;
+	*--rec_offsets = cpu_to_be16(offset);
+}
+
+static inline
+int hfsplus_write_attributes_file_node(struct inode *attr_file, char *buf,
+					u16 node_size, int *index)
+{
+	struct address_space *mapping;
+	struct page *page;
+	void *kaddr;
+	u32 written = 0;
+
+	mapping = attr_file->i_mapping;
+
+	for (; written < node_size; (*index)++, written += PAGE_SIZE) {
+		page = read_mapping_page(mapping, *index, NULL);
+		if (IS_ERR(page))
+			return PTR_ERR(page);
+
+		kaddr = kmap_local_page(page);
+		memcpy(kaddr, buf + written,
+			min_t(size_t, PAGE_SIZE, node_size - written));
+		kunmap_local(kaddr);
+
+		set_page_dirty(page);
+		put_page(page);
+	}
+
+	return 0;
 }
 
 static int hfsplus_create_attributes_file(struct super_block *sb)
 {
-	int err = 0;
 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
 	struct inode *attr_file;
 	struct hfsplus_inode_info *hip;
+	struct hfs_bnode_desc *desc;
 	u32 clump_size;
 	u16 node_size = HFSPLUS_ATTR_TREE_NODE_SIZE;
+	u32 next_node;
+	u32 map_node_idx;
+	u32 map_nodes;
 	char *buf;
-	int index, written;
-	struct address_space *mapping;
-	struct page *page;
+	int index;
 	int old_state = HFSPLUS_EMPTY_ATTR_TREE;
+	int err = 0;
 
 	hfs_dbg("ino %d\n", HFSPLUS_ATTR_CNID);
 
@@ -195,7 +274,7 @@ static int hfsplus_create_attributes_file(struct super_block *sb)
 	}
 
 	while (hip->alloc_blocks < hip->clump_blocks) {
-		err = hfsplus_file_extend(attr_file, false);
+		err = hfsplus_file_extend(attr_file, true);
 		if (unlikely(err)) {
 			pr_err("failed to extend attributes file\n");
 			goto end_attr_file_creation;
@@ -212,28 +291,30 @@ static int hfsplus_create_attributes_file(struct super_block *sb)
 		goto end_attr_file_creation;
 	}
 
-	hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
+	map_nodes = hfsplus_init_header_node(attr_file, clump_size, buf, node_size);
 
-	mapping = attr_file->i_mapping;
+	desc = (struct hfs_bnode_desc *)buf;
+	next_node = be32_to_cpu(desc->next);
 
 	index = 0;
-	written = 0;
-	for (; written < node_size; index++, written += PAGE_SIZE) {
-		void *kaddr;
 
-		page = read_mapping_page(mapping, index, NULL);
-		if (IS_ERR(page)) {
-			err = PTR_ERR(page);
-			goto failed_header_node_init;
-		}
+	err = hfsplus_write_attributes_file_node(attr_file, buf,
+						 node_size, &index);
+	if (unlikely(err))
+		goto failed_header_node_init;
 
-		kaddr = kmap_atomic(page);
-		memcpy(kaddr, buf + written,
-			min_t(size_t, PAGE_SIZE, node_size - written));
-		kunmap_atomic(kaddr);
+	for (map_node_idx = 0; map_node_idx < map_nodes; map_node_idx++) {
+		if (next_node >= map_nodes)
+			next_node = 0;
 
-		set_page_dirty(page);
-		put_page(page);
+		hfsplus_init_map_node(buf, node_size, next_node);
+
+		err = hfsplus_write_attributes_file_node(attr_file, buf,
+							 node_size, &index);
+		if (unlikely(err))
+			goto failed_header_node_init;
+
+		next_node++;
 	}
 
 	hfsplus_mark_inode_dirty(HFSPLUS_ATTR_TREE_I(sb), HFSPLUS_I_ATTR_DIRTY);
include/linux/hfs_common.h +2 -0
index 9e71b9a03b60..07dfc39630ab 100644
@@ -518,6 +518,8 @@ struct hfs_btree_header_rec {
 #define HFSPLUS_BTREE_HDR_MAP_REC_INDEX		2	/* Map (bitmap) record in Header node */
 #define HFSPLUS_BTREE_MAP_NODE_REC_INDEX	0	/* Map record in Map Node */
 #define HFSPLUS_BTREE_HDR_USER_BYTES		128
+#define HFSPLUS_BTREE_MAP_NODE_RECS_COUNT	2
+#define HFSPLUS_BTREE_MAP_NODE_RESERVED_BYTES	2
 
 /* btree key type */
 #define HFSPLUS_KEY_CASEFOLDING		0xCF	/* case-insensitive */
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH 5/5 hfsplus: fix generic/642 failure

In reply to Viacheslav Dubeyko

The xfstests' test-case generic/642 finishes with
corrupted HFS+ volume:

sudo ./check generic/642
[sudo] password for slavad:
FSTYP -- hfsplus
PLATFORM -- Linux/x86_64 hfsplus-testing-0001 7.0.0-rc1+ #26 SMP PREEMPT_DYNAMIC Mon Mar 23 17:24:32 PDT 2026 MKFS_OPTIONS -- /dev/loop51
MOUNT_OPTIONS -- /dev/loop51 /mnt/scratch

generic/642 6s ... _check_generic_filesystem: filesystem on /dev/loop51 is inconsistent (see xfstests-dev/results//generic/642.full for details)

Ran: generic/642
Failures: generic/642
Failed 1 of 1 tests

sudo fsck.hfs -d /dev/loop51
** /dev/loop51
Using cacheBlockSize=32K cacheTotalBlock=1024 cacheSize=32768K. Executing fsck_hfs (version 540.1-Linux).
** Checking non-journaled HFS Plus Volume.
The volume name is untitled
** Checking extents overflow file.
** Checking catalog file.
** Checking multi-linked files.
** Checking catalog hierarchy.
** Checking extended attributes file.
invalid free nodes - calculated 1637 header 1260 Invalid B-tree header
Invalid map node
(8, 0)
** Checking volume bitmap.
** Checking volume information.
Verify Status: VIStat = 0x0000, ABTStat = 0xc000 EBTStat = 0x0000 CBTStat = 0x0000 CatStat = 0x00000000
** Repairing volume.
** Rechecking volume.
** Checking non-journaled HFS Plus Volume.
The volume name is untitled
** Checking extents overflow file.
** Checking catalog file.
** Checking multi-linked files.
** Checking catalog hierarchy.
** Checking extended attributes file.
** Checking volume bitmap.
** Checking volume information.
** The volume untitled was repaired successfully.

The fsck tool detected that Extended Attributes b-tree is corrupted. Namely, the free nodes number is incorrect and map node bitmap has inconsistent state. Analysis has shown that during b-tree closing there are still some lost b-tree's nodes in the hash out of b-tree structure. But this orphaned b-tree nodes are still accounted as used in map node bitmap:

tree_cnid 8, nidx 0, node_count 1408, free_nodes 1403
tree_cnid 8, nidx 1, node_count 1408, free_nodes 1403
tree_cnid 8, nidx 3, node_count 1408, free_nodes 1403
tree_cnid 8, nidx 54, node_count 1408, free_nodes 1403
tree_cnid 8, nidx 67, node_count 1408, free_nodes 1403
tree_cnid 8, nidx 0, prev 0, next 0, parent 0, num_recs 3, type 0x1, height 0 tree_cnid 8, nidx 1, prev 0, next 0, parent 3, num_recs 1, type 0xff, height 1 tree_cnid 8, nidx 3, prev 0, next 0, parent 0, num_recs 1, type 0x0, height 2 tree_cnid 8, nidx 54, prev 29, next 46, parent 3, num_recs 0, type 0xff, height 1 tree_cnid 8, nidx 67, prev 8, next 14, parent 3, num_recs 0, type 0xff, height 1

This issue happens in hfs_bnode_split() logic during detection the possibility of moving half ot the records out of the node. The hfs_bnode_split() contains a loop that implements
a roughly 50/50 split of the B-tree node's records by scanning the offset table to find where the data crosses the node's midpoint. If this logic detects the incapability of spliting the node, then it simply calls hfs_bnode_put() for newly created node. However, node is not set as HFS_BNODE_DELETED and real deletion of node doesn't happen. As a result, the empty node becomes orphaned but it is still accounted as used. Finally, fsck tool detects this inconsistency of HFS+ volume.

This patch adds call of hfs_bnode_unlink() before hfs_bnode_put() for the case if new node cannot be used for spliting the existing node.

sudo ./check generic/642

FSTYP         -- hfsplus
PLATFORM      -- Linux/x86_64 hfsplus-testing-0001 7.0.0-rc1+ #26 SMP PREEMPT_DYNAMIC Fri Apr  3 12:39:13 PDT 2026
MKFS_OPTIONS  -- /dev/loop51

MOUNT_OPTIONS -- /dev/loop51 /mnt/scratch

generic/642 40s ... 39s
Ran: generic/642
Passed all 1 tests

---
 fs/hfsplus/brec.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)
fs/hfsplus/brec.c +20 -12
index 6796c1a80e99..e3df89284079 100644
@@ -239,6 +239,9 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 	struct hfs_bnode_desc node_desc;
 	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;
 	node = fd->bnode;
@@ -265,18 +268,22 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		return next_node;
 	}
 
-	size = tree->node_size / 2 - node->num_recs * 2 - 14;
-	old_rec_off = tree->node_size - 4;
+	rec_off_tbl_size = node->num_recs * rec_size;
+	size = tree->node_size / 2;
+	size -= node_desc_size;
+	size -= rec_off_tbl_size;
+	old_rec_off = tree->node_size - (2 * rec_size);
+
 	num_recs = 1;
 	for (;;) {
 		data_start = hfs_bnode_read_u16(node, old_rec_off);
 		if (data_start > size)
 			break;
-		old_rec_off -= 2;
+		old_rec_off -= rec_size;
 		if (++num_recs < node->num_recs)
 			continue;
-		/* panic? */
 		hfs_bnode_put(node);
+		hfs_bnode_unlink(new_node);
 		hfs_bnode_put(new_node);
 		if (next_node)
 			hfs_bnode_put(next_node);
@@ -287,7 +294,7 @@ static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
 		/* new record is in the lower half,
 		 * so leave some more space there
 		 */
-		old_rec_off += 2;
+		old_rec_off += rec_size;
 		num_recs--;
 		data_start = hfs_bnode_read_u16(node, old_rec_off);
 	} else {
@@ -295,27 +302,28 @@ 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 - 14;
-		fd->entryoffset -= data_start - 14;
+		fd->keyoffset -= data_start - node_desc_size;
+		fd->entryoffset -= data_start - node_desc_size;
 	}
 	new_node->num_recs = node->num_recs - num_recs;
 	node->num_recs = num_recs;
 
-	new_rec_off = tree->node_size - 2;
-	new_off = 14;
+	new_rec_off = tree->node_size - rec_size;
+	new_off = node_desc_size;
 	size = data_start - new_off;
 	num_recs = new_node->num_recs;
 	data_end = data_start;
 	while (num_recs) {
 		hfs_bnode_write_u16(new_node, new_rec_off, new_off);
-		old_rec_off -= 2;
-		new_rec_off -= 2;
+		old_rec_off -= rec_size;
+		new_rec_off -= rec_size;
 		data_end = hfs_bnode_read_u16(node, old_rec_off);
 		new_off = data_end - size;
 		num_recs--;
 	}
 	hfs_bnode_write_u16(new_node, new_rec_off, new_off);
-	hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
+	hfs_bnode_copy(new_node, node_desc_size,
+			node, data_start, data_end - data_start);
 
 	/* update new bnode header */
 	node_desc.next = cpu_to_be32(new_node->next);
Signature · 1 line
2.43.0