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