18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  linux/fs/hfsplus/brec.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2001
68c2ecf20Sopenharmony_ci * Brad Boyer (flar@allandria.com)
78c2ecf20Sopenharmony_ci * (C) 2003 Ardis Technologies <roman@ardistech.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Handle individual btree records
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "hfsplus_fs.h"
138c2ecf20Sopenharmony_ci#include "hfsplus_raw.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cistatic struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd);
168c2ecf20Sopenharmony_cistatic int hfs_brec_update_parent(struct hfs_find_data *fd);
178c2ecf20Sopenharmony_cistatic int hfs_btree_inc_height(struct hfs_btree *);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Get the length and offset of the given record in the given node */
208c2ecf20Sopenharmony_ciu16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	__be16 retval[2];
238c2ecf20Sopenharmony_ci	u16 dataoff;
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci	dataoff = node->tree->node_size - (rec + 2) * 2;
268c2ecf20Sopenharmony_ci	hfs_bnode_read(node, retval, dataoff, 4);
278c2ecf20Sopenharmony_ci	*off = be16_to_cpu(retval[1]);
288c2ecf20Sopenharmony_ci	return be16_to_cpu(retval[0]) - *off;
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* Get the length of the key from a keyed record */
328c2ecf20Sopenharmony_ciu16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	u16 retval, recoff;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF)
378c2ecf20Sopenharmony_ci		return 0;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	if ((node->type == HFS_NODE_INDEX) &&
408c2ecf20Sopenharmony_ci	   !(node->tree->attributes & HFS_TREE_VARIDXKEYS) &&
418c2ecf20Sopenharmony_ci	   (node->tree->cnid != HFSPLUS_ATTR_CNID)) {
428c2ecf20Sopenharmony_ci		retval = node->tree->max_key_len + 2;
438c2ecf20Sopenharmony_ci	} else {
448c2ecf20Sopenharmony_ci		recoff = hfs_bnode_read_u16(node,
458c2ecf20Sopenharmony_ci			node->tree->node_size - (rec + 1) * 2);
468c2ecf20Sopenharmony_ci		if (!recoff)
478c2ecf20Sopenharmony_ci			return 0;
488c2ecf20Sopenharmony_ci		if (recoff > node->tree->node_size - 2) {
498c2ecf20Sopenharmony_ci			pr_err("recoff %d too large\n", recoff);
508c2ecf20Sopenharmony_ci			return 0;
518c2ecf20Sopenharmony_ci		}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci		retval = hfs_bnode_read_u16(node, recoff) + 2;
548c2ecf20Sopenharmony_ci		if (retval > node->tree->max_key_len + 2) {
558c2ecf20Sopenharmony_ci			pr_err("keylen %d too large\n",
568c2ecf20Sopenharmony_ci				retval);
578c2ecf20Sopenharmony_ci			retval = 0;
588c2ecf20Sopenharmony_ci		}
598c2ecf20Sopenharmony_ci	}
608c2ecf20Sopenharmony_ci	return retval;
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciint hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct hfs_btree *tree;
668c2ecf20Sopenharmony_ci	struct hfs_bnode *node, *new_node;
678c2ecf20Sopenharmony_ci	int size, key_len, rec;
688c2ecf20Sopenharmony_ci	int data_off, end_off;
698c2ecf20Sopenharmony_ci	int idx_rec_off, data_rec_off, end_rec_off;
708c2ecf20Sopenharmony_ci	__be32 cnid;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	tree = fd->tree;
738c2ecf20Sopenharmony_ci	if (!fd->bnode) {
748c2ecf20Sopenharmony_ci		if (!tree->root)
758c2ecf20Sopenharmony_ci			hfs_btree_inc_height(tree);
768c2ecf20Sopenharmony_ci		node = hfs_bnode_find(tree, tree->leaf_head);
778c2ecf20Sopenharmony_ci		if (IS_ERR(node))
788c2ecf20Sopenharmony_ci			return PTR_ERR(node);
798c2ecf20Sopenharmony_ci		fd->bnode = node;
808c2ecf20Sopenharmony_ci		fd->record = -1;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci	new_node = NULL;
838c2ecf20Sopenharmony_ci	key_len = be16_to_cpu(fd->search_key->key_len) + 2;
848c2ecf20Sopenharmony_ciagain:
858c2ecf20Sopenharmony_ci	/* new record idx and complete record size */
868c2ecf20Sopenharmony_ci	rec = fd->record + 1;
878c2ecf20Sopenharmony_ci	size = key_len + entry_len;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	node = fd->bnode;
908c2ecf20Sopenharmony_ci	hfs_bnode_dump(node);
918c2ecf20Sopenharmony_ci	/* get last offset */
928c2ecf20Sopenharmony_ci	end_rec_off = tree->node_size - (node->num_recs + 1) * 2;
938c2ecf20Sopenharmony_ci	end_off = hfs_bnode_read_u16(node, end_rec_off);
948c2ecf20Sopenharmony_ci	end_rec_off -= 2;
958c2ecf20Sopenharmony_ci	hfs_dbg(BNODE_MOD, "insert_rec: %d, %d, %d, %d\n",
968c2ecf20Sopenharmony_ci		rec, size, end_off, end_rec_off);
978c2ecf20Sopenharmony_ci	if (size > end_rec_off - end_off) {
988c2ecf20Sopenharmony_ci		if (new_node)
998c2ecf20Sopenharmony_ci			panic("not enough room!\n");
1008c2ecf20Sopenharmony_ci		new_node = hfs_bnode_split(fd);
1018c2ecf20Sopenharmony_ci		if (IS_ERR(new_node))
1028c2ecf20Sopenharmony_ci			return PTR_ERR(new_node);
1038c2ecf20Sopenharmony_ci		goto again;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci	if (node->type == HFS_NODE_LEAF) {
1068c2ecf20Sopenharmony_ci		tree->leaf_count++;
1078c2ecf20Sopenharmony_ci		mark_inode_dirty(tree->inode);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci	node->num_recs++;
1108c2ecf20Sopenharmony_ci	/* write new last offset */
1118c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(node,
1128c2ecf20Sopenharmony_ci		offsetof(struct hfs_bnode_desc, num_recs),
1138c2ecf20Sopenharmony_ci		node->num_recs);
1148c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(node, end_rec_off, end_off + size);
1158c2ecf20Sopenharmony_ci	data_off = end_off;
1168c2ecf20Sopenharmony_ci	data_rec_off = end_rec_off + 2;
1178c2ecf20Sopenharmony_ci	idx_rec_off = tree->node_size - (rec + 1) * 2;
1188c2ecf20Sopenharmony_ci	if (idx_rec_off == data_rec_off)
1198c2ecf20Sopenharmony_ci		goto skip;
1208c2ecf20Sopenharmony_ci	/* move all following entries */
1218c2ecf20Sopenharmony_ci	do {
1228c2ecf20Sopenharmony_ci		data_off = hfs_bnode_read_u16(node, data_rec_off + 2);
1238c2ecf20Sopenharmony_ci		hfs_bnode_write_u16(node, data_rec_off, data_off + size);
1248c2ecf20Sopenharmony_ci		data_rec_off += 2;
1258c2ecf20Sopenharmony_ci	} while (data_rec_off < idx_rec_off);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	/* move data away */
1288c2ecf20Sopenharmony_ci	hfs_bnode_move(node, data_off + size, data_off,
1298c2ecf20Sopenharmony_ci		       end_off - data_off);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ciskip:
1328c2ecf20Sopenharmony_ci	hfs_bnode_write(node, fd->search_key, data_off, key_len);
1338c2ecf20Sopenharmony_ci	hfs_bnode_write(node, entry, data_off + key_len, entry_len);
1348c2ecf20Sopenharmony_ci	hfs_bnode_dump(node);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/*
1378c2ecf20Sopenharmony_ci	 * update parent key if we inserted a key
1388c2ecf20Sopenharmony_ci	 * at the start of the node and it is not the new node
1398c2ecf20Sopenharmony_ci	 */
1408c2ecf20Sopenharmony_ci	if (!rec && new_node != node) {
1418c2ecf20Sopenharmony_ci		hfs_bnode_read_key(node, fd->search_key, data_off + size);
1428c2ecf20Sopenharmony_ci		hfs_brec_update_parent(fd);
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (new_node) {
1468c2ecf20Sopenharmony_ci		hfs_bnode_put(fd->bnode);
1478c2ecf20Sopenharmony_ci		if (!new_node->parent) {
1488c2ecf20Sopenharmony_ci			hfs_btree_inc_height(tree);
1498c2ecf20Sopenharmony_ci			new_node->parent = tree->root;
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci		fd->bnode = hfs_bnode_find(tree, new_node->parent);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		/* create index data entry */
1548c2ecf20Sopenharmony_ci		cnid = cpu_to_be32(new_node->this);
1558c2ecf20Sopenharmony_ci		entry = &cnid;
1568c2ecf20Sopenharmony_ci		entry_len = sizeof(cnid);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		/* get index key */
1598c2ecf20Sopenharmony_ci		hfs_bnode_read_key(new_node, fd->search_key, 14);
1608c2ecf20Sopenharmony_ci		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		hfs_bnode_put(new_node);
1638c2ecf20Sopenharmony_ci		new_node = NULL;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		if ((tree->attributes & HFS_TREE_VARIDXKEYS) ||
1668c2ecf20Sopenharmony_ci				(tree->cnid == HFSPLUS_ATTR_CNID))
1678c2ecf20Sopenharmony_ci			key_len = be16_to_cpu(fd->search_key->key_len) + 2;
1688c2ecf20Sopenharmony_ci		else {
1698c2ecf20Sopenharmony_ci			fd->search_key->key_len =
1708c2ecf20Sopenharmony_ci				cpu_to_be16(tree->max_key_len);
1718c2ecf20Sopenharmony_ci			key_len = tree->max_key_len + 2;
1728c2ecf20Sopenharmony_ci		}
1738c2ecf20Sopenharmony_ci		goto again;
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return 0;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciint hfs_brec_remove(struct hfs_find_data *fd)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct hfs_btree *tree;
1828c2ecf20Sopenharmony_ci	struct hfs_bnode *node, *parent;
1838c2ecf20Sopenharmony_ci	int end_off, rec_off, data_off, size;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	tree = fd->tree;
1868c2ecf20Sopenharmony_ci	node = fd->bnode;
1878c2ecf20Sopenharmony_ciagain:
1888c2ecf20Sopenharmony_ci	rec_off = tree->node_size - (fd->record + 2) * 2;
1898c2ecf20Sopenharmony_ci	end_off = tree->node_size - (node->num_recs + 1) * 2;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	if (node->type == HFS_NODE_LEAF) {
1928c2ecf20Sopenharmony_ci		tree->leaf_count--;
1938c2ecf20Sopenharmony_ci		mark_inode_dirty(tree->inode);
1948c2ecf20Sopenharmony_ci	}
1958c2ecf20Sopenharmony_ci	hfs_bnode_dump(node);
1968c2ecf20Sopenharmony_ci	hfs_dbg(BNODE_MOD, "remove_rec: %d, %d\n",
1978c2ecf20Sopenharmony_ci		fd->record, fd->keylength + fd->entrylength);
1988c2ecf20Sopenharmony_ci	if (!--node->num_recs) {
1998c2ecf20Sopenharmony_ci		hfs_bnode_unlink(node);
2008c2ecf20Sopenharmony_ci		if (!node->parent)
2018c2ecf20Sopenharmony_ci			return 0;
2028c2ecf20Sopenharmony_ci		parent = hfs_bnode_find(tree, node->parent);
2038c2ecf20Sopenharmony_ci		if (IS_ERR(parent))
2048c2ecf20Sopenharmony_ci			return PTR_ERR(parent);
2058c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
2068c2ecf20Sopenharmony_ci		node = fd->bnode = parent;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		__hfs_brec_find(node, fd, hfs_find_rec_by_key);
2098c2ecf20Sopenharmony_ci		goto again;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(node,
2128c2ecf20Sopenharmony_ci		offsetof(struct hfs_bnode_desc, num_recs),
2138c2ecf20Sopenharmony_ci		node->num_recs);
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	if (rec_off == end_off)
2168c2ecf20Sopenharmony_ci		goto skip;
2178c2ecf20Sopenharmony_ci	size = fd->keylength + fd->entrylength;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	do {
2208c2ecf20Sopenharmony_ci		data_off = hfs_bnode_read_u16(node, rec_off);
2218c2ecf20Sopenharmony_ci		hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
2228c2ecf20Sopenharmony_ci		rec_off -= 2;
2238c2ecf20Sopenharmony_ci	} while (rec_off >= end_off);
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* fill hole */
2268c2ecf20Sopenharmony_ci	hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
2278c2ecf20Sopenharmony_ci		       data_off - fd->keyoffset - size);
2288c2ecf20Sopenharmony_ciskip:
2298c2ecf20Sopenharmony_ci	hfs_bnode_dump(node);
2308c2ecf20Sopenharmony_ci	if (!fd->record)
2318c2ecf20Sopenharmony_ci		hfs_brec_update_parent(fd);
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct hfs_btree *tree;
2388c2ecf20Sopenharmony_ci	struct hfs_bnode *node, *new_node, *next_node;
2398c2ecf20Sopenharmony_ci	struct hfs_bnode_desc node_desc;
2408c2ecf20Sopenharmony_ci	int num_recs, new_rec_off, new_off, old_rec_off;
2418c2ecf20Sopenharmony_ci	int data_start, data_end, size;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	tree = fd->tree;
2448c2ecf20Sopenharmony_ci	node = fd->bnode;
2458c2ecf20Sopenharmony_ci	new_node = hfs_bmap_alloc(tree);
2468c2ecf20Sopenharmony_ci	if (IS_ERR(new_node))
2478c2ecf20Sopenharmony_ci		return new_node;
2488c2ecf20Sopenharmony_ci	hfs_bnode_get(node);
2498c2ecf20Sopenharmony_ci	hfs_dbg(BNODE_MOD, "split_nodes: %d - %d - %d\n",
2508c2ecf20Sopenharmony_ci		node->this, new_node->this, node->next);
2518c2ecf20Sopenharmony_ci	new_node->next = node->next;
2528c2ecf20Sopenharmony_ci	new_node->prev = node->this;
2538c2ecf20Sopenharmony_ci	new_node->parent = node->parent;
2548c2ecf20Sopenharmony_ci	new_node->type = node->type;
2558c2ecf20Sopenharmony_ci	new_node->height = node->height;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (node->next)
2588c2ecf20Sopenharmony_ci		next_node = hfs_bnode_find(tree, node->next);
2598c2ecf20Sopenharmony_ci	else
2608c2ecf20Sopenharmony_ci		next_node = NULL;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (IS_ERR(next_node)) {
2638c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
2648c2ecf20Sopenharmony_ci		hfs_bnode_put(new_node);
2658c2ecf20Sopenharmony_ci		return next_node;
2668c2ecf20Sopenharmony_ci	}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	size = tree->node_size / 2 - node->num_recs * 2 - 14;
2698c2ecf20Sopenharmony_ci	old_rec_off = tree->node_size - 4;
2708c2ecf20Sopenharmony_ci	num_recs = 1;
2718c2ecf20Sopenharmony_ci	for (;;) {
2728c2ecf20Sopenharmony_ci		data_start = hfs_bnode_read_u16(node, old_rec_off);
2738c2ecf20Sopenharmony_ci		if (data_start > size)
2748c2ecf20Sopenharmony_ci			break;
2758c2ecf20Sopenharmony_ci		old_rec_off -= 2;
2768c2ecf20Sopenharmony_ci		if (++num_recs < node->num_recs)
2778c2ecf20Sopenharmony_ci			continue;
2788c2ecf20Sopenharmony_ci		/* panic? */
2798c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
2808c2ecf20Sopenharmony_ci		hfs_bnode_put(new_node);
2818c2ecf20Sopenharmony_ci		if (next_node)
2828c2ecf20Sopenharmony_ci			hfs_bnode_put(next_node);
2838c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOSPC);
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (fd->record + 1 < num_recs) {
2878c2ecf20Sopenharmony_ci		/* new record is in the lower half,
2888c2ecf20Sopenharmony_ci		 * so leave some more space there
2898c2ecf20Sopenharmony_ci		 */
2908c2ecf20Sopenharmony_ci		old_rec_off += 2;
2918c2ecf20Sopenharmony_ci		num_recs--;
2928c2ecf20Sopenharmony_ci		data_start = hfs_bnode_read_u16(node, old_rec_off);
2938c2ecf20Sopenharmony_ci	} else {
2948c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
2958c2ecf20Sopenharmony_ci		hfs_bnode_get(new_node);
2968c2ecf20Sopenharmony_ci		fd->bnode = new_node;
2978c2ecf20Sopenharmony_ci		fd->record -= num_recs;
2988c2ecf20Sopenharmony_ci		fd->keyoffset -= data_start - 14;
2998c2ecf20Sopenharmony_ci		fd->entryoffset -= data_start - 14;
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci	new_node->num_recs = node->num_recs - num_recs;
3028c2ecf20Sopenharmony_ci	node->num_recs = num_recs;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	new_rec_off = tree->node_size - 2;
3058c2ecf20Sopenharmony_ci	new_off = 14;
3068c2ecf20Sopenharmony_ci	size = data_start - new_off;
3078c2ecf20Sopenharmony_ci	num_recs = new_node->num_recs;
3088c2ecf20Sopenharmony_ci	data_end = data_start;
3098c2ecf20Sopenharmony_ci	while (num_recs) {
3108c2ecf20Sopenharmony_ci		hfs_bnode_write_u16(new_node, new_rec_off, new_off);
3118c2ecf20Sopenharmony_ci		old_rec_off -= 2;
3128c2ecf20Sopenharmony_ci		new_rec_off -= 2;
3138c2ecf20Sopenharmony_ci		data_end = hfs_bnode_read_u16(node, old_rec_off);
3148c2ecf20Sopenharmony_ci		new_off = data_end - size;
3158c2ecf20Sopenharmony_ci		num_recs--;
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(new_node, new_rec_off, new_off);
3188c2ecf20Sopenharmony_ci	hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	/* update new bnode header */
3218c2ecf20Sopenharmony_ci	node_desc.next = cpu_to_be32(new_node->next);
3228c2ecf20Sopenharmony_ci	node_desc.prev = cpu_to_be32(new_node->prev);
3238c2ecf20Sopenharmony_ci	node_desc.type = new_node->type;
3248c2ecf20Sopenharmony_ci	node_desc.height = new_node->height;
3258c2ecf20Sopenharmony_ci	node_desc.num_recs = cpu_to_be16(new_node->num_recs);
3268c2ecf20Sopenharmony_ci	node_desc.reserved = 0;
3278c2ecf20Sopenharmony_ci	hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	/* update previous bnode header */
3308c2ecf20Sopenharmony_ci	node->next = new_node->this;
3318c2ecf20Sopenharmony_ci	hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
3328c2ecf20Sopenharmony_ci	node_desc.next = cpu_to_be32(node->next);
3338c2ecf20Sopenharmony_ci	node_desc.num_recs = cpu_to_be16(node->num_recs);
3348c2ecf20Sopenharmony_ci	hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	/* update next bnode header */
3378c2ecf20Sopenharmony_ci	if (next_node) {
3388c2ecf20Sopenharmony_ci		next_node->prev = new_node->this;
3398c2ecf20Sopenharmony_ci		hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
3408c2ecf20Sopenharmony_ci		node_desc.prev = cpu_to_be32(next_node->prev);
3418c2ecf20Sopenharmony_ci		hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
3428c2ecf20Sopenharmony_ci		hfs_bnode_put(next_node);
3438c2ecf20Sopenharmony_ci	} else if (node->this == tree->leaf_tail) {
3448c2ecf20Sopenharmony_ci		/* if there is no next node, this might be the new tail */
3458c2ecf20Sopenharmony_ci		tree->leaf_tail = new_node->this;
3468c2ecf20Sopenharmony_ci		mark_inode_dirty(tree->inode);
3478c2ecf20Sopenharmony_ci	}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	hfs_bnode_dump(node);
3508c2ecf20Sopenharmony_ci	hfs_bnode_dump(new_node);
3518c2ecf20Sopenharmony_ci	hfs_bnode_put(node);
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	return new_node;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int hfs_brec_update_parent(struct hfs_find_data *fd)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	struct hfs_btree *tree;
3598c2ecf20Sopenharmony_ci	struct hfs_bnode *node, *new_node, *parent;
3608c2ecf20Sopenharmony_ci	int newkeylen, diff;
3618c2ecf20Sopenharmony_ci	int rec, rec_off, end_rec_off;
3628c2ecf20Sopenharmony_ci	int start_off, end_off;
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	tree = fd->tree;
3658c2ecf20Sopenharmony_ci	node = fd->bnode;
3668c2ecf20Sopenharmony_ci	new_node = NULL;
3678c2ecf20Sopenharmony_ci	if (!node->parent)
3688c2ecf20Sopenharmony_ci		return 0;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ciagain:
3718c2ecf20Sopenharmony_ci	parent = hfs_bnode_find(tree, node->parent);
3728c2ecf20Sopenharmony_ci	if (IS_ERR(parent))
3738c2ecf20Sopenharmony_ci		return PTR_ERR(parent);
3748c2ecf20Sopenharmony_ci	__hfs_brec_find(parent, fd, hfs_find_rec_by_key);
3758c2ecf20Sopenharmony_ci	if (fd->record < 0)
3768c2ecf20Sopenharmony_ci		return -ENOENT;
3778c2ecf20Sopenharmony_ci	hfs_bnode_dump(parent);
3788c2ecf20Sopenharmony_ci	rec = fd->record;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	/* size difference between old and new key */
3818c2ecf20Sopenharmony_ci	if ((tree->attributes & HFS_TREE_VARIDXKEYS) ||
3828c2ecf20Sopenharmony_ci				(tree->cnid == HFSPLUS_ATTR_CNID))
3838c2ecf20Sopenharmony_ci		newkeylen = hfs_bnode_read_u16(node, 14) + 2;
3848c2ecf20Sopenharmony_ci	else
3858c2ecf20Sopenharmony_ci		fd->keylength = newkeylen = tree->max_key_len + 2;
3868c2ecf20Sopenharmony_ci	hfs_dbg(BNODE_MOD, "update_rec: %d, %d, %d\n",
3878c2ecf20Sopenharmony_ci		rec, fd->keylength, newkeylen);
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	rec_off = tree->node_size - (rec + 2) * 2;
3908c2ecf20Sopenharmony_ci	end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
3918c2ecf20Sopenharmony_ci	diff = newkeylen - fd->keylength;
3928c2ecf20Sopenharmony_ci	if (!diff)
3938c2ecf20Sopenharmony_ci		goto skip;
3948c2ecf20Sopenharmony_ci	if (diff > 0) {
3958c2ecf20Sopenharmony_ci		end_off = hfs_bnode_read_u16(parent, end_rec_off);
3968c2ecf20Sopenharmony_ci		if (end_rec_off - end_off < diff) {
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci			hfs_dbg(BNODE_MOD, "splitting index node\n");
3998c2ecf20Sopenharmony_ci			fd->bnode = parent;
4008c2ecf20Sopenharmony_ci			new_node = hfs_bnode_split(fd);
4018c2ecf20Sopenharmony_ci			if (IS_ERR(new_node))
4028c2ecf20Sopenharmony_ci				return PTR_ERR(new_node);
4038c2ecf20Sopenharmony_ci			parent = fd->bnode;
4048c2ecf20Sopenharmony_ci			rec = fd->record;
4058c2ecf20Sopenharmony_ci			rec_off = tree->node_size - (rec + 2) * 2;
4068c2ecf20Sopenharmony_ci			end_rec_off = tree->node_size -
4078c2ecf20Sopenharmony_ci				(parent->num_recs + 1) * 2;
4088c2ecf20Sopenharmony_ci		}
4098c2ecf20Sopenharmony_ci	}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
4128c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(parent, rec_off, start_off + diff);
4138c2ecf20Sopenharmony_ci	start_off -= 4;	/* move previous cnid too */
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	while (rec_off > end_rec_off) {
4168c2ecf20Sopenharmony_ci		rec_off -= 2;
4178c2ecf20Sopenharmony_ci		end_off = hfs_bnode_read_u16(parent, rec_off);
4188c2ecf20Sopenharmony_ci		hfs_bnode_write_u16(parent, rec_off, end_off + diff);
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci	hfs_bnode_move(parent, start_off + diff, start_off,
4218c2ecf20Sopenharmony_ci		       end_off - start_off);
4228c2ecf20Sopenharmony_ciskip:
4238c2ecf20Sopenharmony_ci	hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
4248c2ecf20Sopenharmony_ci	hfs_bnode_dump(parent);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	hfs_bnode_put(node);
4278c2ecf20Sopenharmony_ci	node = parent;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if (new_node) {
4308c2ecf20Sopenharmony_ci		__be32 cnid;
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci		if (!new_node->parent) {
4338c2ecf20Sopenharmony_ci			hfs_btree_inc_height(tree);
4348c2ecf20Sopenharmony_ci			new_node->parent = tree->root;
4358c2ecf20Sopenharmony_ci		}
4368c2ecf20Sopenharmony_ci		fd->bnode = hfs_bnode_find(tree, new_node->parent);
4378c2ecf20Sopenharmony_ci		/* create index key and entry */
4388c2ecf20Sopenharmony_ci		hfs_bnode_read_key(new_node, fd->search_key, 14);
4398c2ecf20Sopenharmony_ci		cnid = cpu_to_be32(new_node->this);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci		__hfs_brec_find(fd->bnode, fd, hfs_find_rec_by_key);
4428c2ecf20Sopenharmony_ci		hfs_brec_insert(fd, &cnid, sizeof(cnid));
4438c2ecf20Sopenharmony_ci		hfs_bnode_put(fd->bnode);
4448c2ecf20Sopenharmony_ci		hfs_bnode_put(new_node);
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci		if (!rec) {
4478c2ecf20Sopenharmony_ci			if (new_node == node)
4488c2ecf20Sopenharmony_ci				goto out;
4498c2ecf20Sopenharmony_ci			/* restore search_key */
4508c2ecf20Sopenharmony_ci			hfs_bnode_read_key(node, fd->search_key, 14);
4518c2ecf20Sopenharmony_ci		}
4528c2ecf20Sopenharmony_ci		new_node = NULL;
4538c2ecf20Sopenharmony_ci	}
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	if (!rec && node->parent)
4568c2ecf20Sopenharmony_ci		goto again;
4578c2ecf20Sopenharmony_ciout:
4588c2ecf20Sopenharmony_ci	fd->bnode = node;
4598c2ecf20Sopenharmony_ci	return 0;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic int hfs_btree_inc_height(struct hfs_btree *tree)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	struct hfs_bnode *node, *new_node;
4658c2ecf20Sopenharmony_ci	struct hfs_bnode_desc node_desc;
4668c2ecf20Sopenharmony_ci	int key_size, rec;
4678c2ecf20Sopenharmony_ci	__be32 cnid;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	node = NULL;
4708c2ecf20Sopenharmony_ci	if (tree->root) {
4718c2ecf20Sopenharmony_ci		node = hfs_bnode_find(tree, tree->root);
4728c2ecf20Sopenharmony_ci		if (IS_ERR(node))
4738c2ecf20Sopenharmony_ci			return PTR_ERR(node);
4748c2ecf20Sopenharmony_ci	}
4758c2ecf20Sopenharmony_ci	new_node = hfs_bmap_alloc(tree);
4768c2ecf20Sopenharmony_ci	if (IS_ERR(new_node)) {
4778c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
4788c2ecf20Sopenharmony_ci		return PTR_ERR(new_node);
4798c2ecf20Sopenharmony_ci	}
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	tree->root = new_node->this;
4828c2ecf20Sopenharmony_ci	if (!tree->depth) {
4838c2ecf20Sopenharmony_ci		tree->leaf_head = tree->leaf_tail = new_node->this;
4848c2ecf20Sopenharmony_ci		new_node->type = HFS_NODE_LEAF;
4858c2ecf20Sopenharmony_ci		new_node->num_recs = 0;
4868c2ecf20Sopenharmony_ci	} else {
4878c2ecf20Sopenharmony_ci		new_node->type = HFS_NODE_INDEX;
4888c2ecf20Sopenharmony_ci		new_node->num_recs = 1;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci	new_node->parent = 0;
4918c2ecf20Sopenharmony_ci	new_node->next = 0;
4928c2ecf20Sopenharmony_ci	new_node->prev = 0;
4938c2ecf20Sopenharmony_ci	new_node->height = ++tree->depth;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	node_desc.next = cpu_to_be32(new_node->next);
4968c2ecf20Sopenharmony_ci	node_desc.prev = cpu_to_be32(new_node->prev);
4978c2ecf20Sopenharmony_ci	node_desc.type = new_node->type;
4988c2ecf20Sopenharmony_ci	node_desc.height = new_node->height;
4998c2ecf20Sopenharmony_ci	node_desc.num_recs = cpu_to_be16(new_node->num_recs);
5008c2ecf20Sopenharmony_ci	node_desc.reserved = 0;
5018c2ecf20Sopenharmony_ci	hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	rec = tree->node_size - 2;
5048c2ecf20Sopenharmony_ci	hfs_bnode_write_u16(new_node, rec, 14);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	if (node) {
5078c2ecf20Sopenharmony_ci		/* insert old root idx into new root */
5088c2ecf20Sopenharmony_ci		node->parent = tree->root;
5098c2ecf20Sopenharmony_ci		if (node->type == HFS_NODE_LEAF ||
5108c2ecf20Sopenharmony_ci				tree->attributes & HFS_TREE_VARIDXKEYS ||
5118c2ecf20Sopenharmony_ci				tree->cnid == HFSPLUS_ATTR_CNID)
5128c2ecf20Sopenharmony_ci			key_size = hfs_bnode_read_u16(node, 14) + 2;
5138c2ecf20Sopenharmony_ci		else
5148c2ecf20Sopenharmony_ci			key_size = tree->max_key_len + 2;
5158c2ecf20Sopenharmony_ci		hfs_bnode_copy(new_node, 14, node, 14, key_size);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		if (!(tree->attributes & HFS_TREE_VARIDXKEYS) &&
5188c2ecf20Sopenharmony_ci				(tree->cnid != HFSPLUS_ATTR_CNID)) {
5198c2ecf20Sopenharmony_ci			key_size = tree->max_key_len + 2;
5208c2ecf20Sopenharmony_ci			hfs_bnode_write_u16(new_node, 14, tree->max_key_len);
5218c2ecf20Sopenharmony_ci		}
5228c2ecf20Sopenharmony_ci		cnid = cpu_to_be32(node->this);
5238c2ecf20Sopenharmony_ci		hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci		rec -= 2;
5268c2ecf20Sopenharmony_ci		hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci		hfs_bnode_put(node);
5298c2ecf20Sopenharmony_ci	}
5308c2ecf20Sopenharmony_ci	hfs_bnode_put(new_node);
5318c2ecf20Sopenharmony_ci	mark_inode_dirty(tree->inode);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	return 0;
5348c2ecf20Sopenharmony_ci}
535