1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hfsplus/brec.c
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handle individual btree records
12 #include "hfsplus_fs.h"
13 #include "hfsplus_raw.h"
15 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
);
16 static int hfs_brec_update_parent(struct hfs_find_data
*fd
);
17 static int hfs_btree_inc_height(struct hfs_btree
*);
19 /* Get the length and offset of the given record in the given node */
20 u16
hfs_brec_lenoff(struct hfs_bnode
*node
, u16 rec
, u16
*off
)
25 dataoff
= node
->tree
->node_size
- (rec
+ 2) * 2;
26 hfs_bnode_read(node
, retval
, dataoff
, 4);
27 *off
= be16_to_cpu(retval
[1]);
28 return be16_to_cpu(retval
[0]) - *off
;
31 /* Get the length of the key from a keyed record */
32 u16
hfs_brec_keylen(struct hfs_bnode
*node
, u16 rec
)
36 if (node
->type
!= HFS_NODE_INDEX
&& node
->type
!= HFS_NODE_LEAF
)
39 if ((node
->type
== HFS_NODE_INDEX
) &&
40 !(node
->tree
->attributes
& HFS_TREE_VARIDXKEYS
) &&
41 (node
->tree
->cnid
!= HFSPLUS_ATTR_CNID
)) {
42 retval
= node
->tree
->max_key_len
+ 2;
44 recoff
= hfs_bnode_read_u16(node
,
45 node
->tree
->node_size
- (rec
+ 1) * 2);
48 if (recoff
> node
->tree
->node_size
- 2) {
49 pr_err("recoff %d too large\n", recoff
);
53 retval
= hfs_bnode_read_u16(node
, recoff
) + 2;
54 if (retval
> node
->tree
->max_key_len
+ 2) {
55 pr_err("keylen %d too large\n",
63 int hfs_brec_insert(struct hfs_find_data
*fd
, void *entry
, int entry_len
)
65 struct hfs_btree
*tree
;
66 struct hfs_bnode
*node
, *new_node
;
67 int size
, key_len
, rec
;
68 int data_off
, end_off
;
69 int idx_rec_off
, data_rec_off
, end_rec_off
;
75 hfs_btree_inc_height(tree
);
76 node
= hfs_bnode_find(tree
, tree
->leaf_head
);
83 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
85 /* new record idx and complete record size */
87 size
= key_len
+ entry_len
;
92 end_rec_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
93 end_off
= hfs_bnode_read_u16(node
, end_rec_off
);
95 hfs_dbg(BNODE_MOD
, "insert_rec: %d, %d, %d, %d\n",
96 rec
, size
, end_off
, end_rec_off
);
97 if (size
> end_rec_off
- end_off
) {
99 panic("not enough room!\n");
100 new_node
= hfs_bnode_split(fd
);
101 if (IS_ERR(new_node
))
102 return PTR_ERR(new_node
);
105 if (node
->type
== HFS_NODE_LEAF
) {
107 mark_inode_dirty(tree
->inode
);
110 /* write new last offset */
111 hfs_bnode_write_u16(node
,
112 offsetof(struct hfs_bnode_desc
, num_recs
),
114 hfs_bnode_write_u16(node
, end_rec_off
, end_off
+ size
);
116 data_rec_off
= end_rec_off
+ 2;
117 idx_rec_off
= tree
->node_size
- (rec
+ 1) * 2;
118 if (idx_rec_off
== data_rec_off
)
120 /* move all following entries */
122 data_off
= hfs_bnode_read_u16(node
, data_rec_off
+ 2);
123 hfs_bnode_write_u16(node
, data_rec_off
, data_off
+ size
);
125 } while (data_rec_off
< idx_rec_off
);
128 hfs_bnode_move(node
, data_off
+ size
, data_off
,
132 hfs_bnode_write(node
, fd
->search_key
, data_off
, key_len
);
133 hfs_bnode_write(node
, entry
, data_off
+ key_len
, entry_len
);
134 hfs_bnode_dump(node
);
137 * update parent key if we inserted a key
138 * at the start of the node and it is not the new node
140 if (!rec
&& new_node
!= node
) {
141 hfs_bnode_read_key(node
, fd
->search_key
, data_off
+ size
);
142 hfs_brec_update_parent(fd
);
146 hfs_bnode_put(fd
->bnode
);
147 if (!new_node
->parent
) {
148 hfs_btree_inc_height(tree
);
149 new_node
->parent
= tree
->root
;
151 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
153 /* create index data entry */
154 cnid
= cpu_to_be32(new_node
->this);
156 entry_len
= sizeof(cnid
);
159 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
160 __hfs_brec_find(fd
->bnode
, fd
, hfs_find_rec_by_key
);
162 hfs_bnode_put(new_node
);
165 if ((tree
->attributes
& HFS_TREE_VARIDXKEYS
) ||
166 (tree
->cnid
== HFSPLUS_ATTR_CNID
))
167 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
169 fd
->search_key
->key_len
=
170 cpu_to_be16(tree
->max_key_len
);
171 key_len
= tree
->max_key_len
+ 2;
179 int hfs_brec_remove(struct hfs_find_data
*fd
)
181 struct hfs_btree
*tree
;
182 struct hfs_bnode
*node
, *parent
;
183 int end_off
, rec_off
, data_off
, size
;
188 rec_off
= tree
->node_size
- (fd
->record
+ 2) * 2;
189 end_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
191 if (node
->type
== HFS_NODE_LEAF
) {
193 mark_inode_dirty(tree
->inode
);
195 hfs_bnode_dump(node
);
196 hfs_dbg(BNODE_MOD
, "remove_rec: %d, %d\n",
197 fd
->record
, fd
->keylength
+ fd
->entrylength
);
198 if (!--node
->num_recs
) {
199 hfs_bnode_unlink(node
);
202 parent
= hfs_bnode_find(tree
, node
->parent
);
204 return PTR_ERR(parent
);
206 node
= fd
->bnode
= parent
;
208 __hfs_brec_find(node
, fd
, hfs_find_rec_by_key
);
211 hfs_bnode_write_u16(node
,
212 offsetof(struct hfs_bnode_desc
, num_recs
),
215 if (rec_off
== end_off
)
217 size
= fd
->keylength
+ fd
->entrylength
;
220 data_off
= hfs_bnode_read_u16(node
, rec_off
);
221 hfs_bnode_write_u16(node
, rec_off
+ 2, data_off
- size
);
223 } while (rec_off
>= end_off
);
226 hfs_bnode_move(node
, fd
->keyoffset
, fd
->keyoffset
+ size
,
227 data_off
- fd
->keyoffset
- size
);
229 hfs_bnode_dump(node
);
231 hfs_brec_update_parent(fd
);
235 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
)
237 struct hfs_btree
*tree
;
238 struct hfs_bnode
*node
, *new_node
, *next_node
;
239 struct hfs_bnode_desc node_desc
;
240 int num_recs
, new_rec_off
, new_off
, old_rec_off
;
241 int data_start
, data_end
, size
;
245 new_node
= hfs_bmap_alloc(tree
);
246 if (IS_ERR(new_node
))
249 hfs_dbg(BNODE_MOD
, "split_nodes: %d - %d - %d\n",
250 node
->this, new_node
->this, node
->next
);
251 new_node
->next
= node
->next
;
252 new_node
->prev
= node
->this;
253 new_node
->parent
= node
->parent
;
254 new_node
->type
= node
->type
;
255 new_node
->height
= node
->height
;
258 next_node
= hfs_bnode_find(tree
, node
->next
);
262 if (IS_ERR(next_node
)) {
264 hfs_bnode_put(new_node
);
268 size
= tree
->node_size
/ 2 - node
->num_recs
* 2 - 14;
269 old_rec_off
= tree
->node_size
- 4;
272 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
273 if (data_start
> size
)
276 if (++num_recs
< node
->num_recs
)
280 hfs_bnode_put(new_node
);
282 hfs_bnode_put(next_node
);
283 return ERR_PTR(-ENOSPC
);
286 if (fd
->record
+ 1 < num_recs
) {
287 /* new record is in the lower half,
288 * so leave some more space there
292 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
295 hfs_bnode_get(new_node
);
296 fd
->bnode
= new_node
;
297 fd
->record
-= num_recs
;
298 fd
->keyoffset
-= data_start
- 14;
299 fd
->entryoffset
-= data_start
- 14;
301 new_node
->num_recs
= node
->num_recs
- num_recs
;
302 node
->num_recs
= num_recs
;
304 new_rec_off
= tree
->node_size
- 2;
306 size
= data_start
- new_off
;
307 num_recs
= new_node
->num_recs
;
308 data_end
= data_start
;
310 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
313 data_end
= hfs_bnode_read_u16(node
, old_rec_off
);
314 new_off
= data_end
- size
;
317 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
318 hfs_bnode_copy(new_node
, 14, node
, data_start
, data_end
- data_start
);
320 /* update new bnode header */
321 node_desc
.next
= cpu_to_be32(new_node
->next
);
322 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
323 node_desc
.type
= new_node
->type
;
324 node_desc
.height
= new_node
->height
;
325 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
326 node_desc
.reserved
= 0;
327 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
329 /* update previous bnode header */
330 node
->next
= new_node
->this;
331 hfs_bnode_read(node
, &node_desc
, 0, sizeof(node_desc
));
332 node_desc
.next
= cpu_to_be32(node
->next
);
333 node_desc
.num_recs
= cpu_to_be16(node
->num_recs
);
334 hfs_bnode_write(node
, &node_desc
, 0, sizeof(node_desc
));
336 /* update next bnode header */
338 next_node
->prev
= new_node
->this;
339 hfs_bnode_read(next_node
, &node_desc
, 0, sizeof(node_desc
));
340 node_desc
.prev
= cpu_to_be32(next_node
->prev
);
341 hfs_bnode_write(next_node
, &node_desc
, 0, sizeof(node_desc
));
342 hfs_bnode_put(next_node
);
343 } else if (node
->this == tree
->leaf_tail
) {
344 /* if there is no next node, this might be the new tail */
345 tree
->leaf_tail
= new_node
->this;
346 mark_inode_dirty(tree
->inode
);
349 hfs_bnode_dump(node
);
350 hfs_bnode_dump(new_node
);
356 static int hfs_brec_update_parent(struct hfs_find_data
*fd
)
358 struct hfs_btree
*tree
;
359 struct hfs_bnode
*node
, *new_node
, *parent
;
361 int rec
, rec_off
, end_rec_off
;
362 int start_off
, end_off
;
371 parent
= hfs_bnode_find(tree
, node
->parent
);
373 return PTR_ERR(parent
);
374 __hfs_brec_find(parent
, fd
, hfs_find_rec_by_key
);
377 hfs_bnode_dump(parent
);
380 /* size difference between old and new key */
381 if ((tree
->attributes
& HFS_TREE_VARIDXKEYS
) ||
382 (tree
->cnid
== HFSPLUS_ATTR_CNID
))
383 newkeylen
= hfs_bnode_read_u16(node
, 14) + 2;
385 fd
->keylength
= newkeylen
= tree
->max_key_len
+ 2;
386 hfs_dbg(BNODE_MOD
, "update_rec: %d, %d, %d\n",
387 rec
, fd
->keylength
, newkeylen
);
389 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
390 end_rec_off
= tree
->node_size
- (parent
->num_recs
+ 1) * 2;
391 diff
= newkeylen
- fd
->keylength
;
395 end_off
= hfs_bnode_read_u16(parent
, end_rec_off
);
396 if (end_rec_off
- end_off
< diff
) {
398 hfs_dbg(BNODE_MOD
, "splitting index node\n");
400 new_node
= hfs_bnode_split(fd
);
401 if (IS_ERR(new_node
))
402 return PTR_ERR(new_node
);
405 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
406 end_rec_off
= tree
->node_size
-
407 (parent
->num_recs
+ 1) * 2;
411 end_off
= start_off
= hfs_bnode_read_u16(parent
, rec_off
);
412 hfs_bnode_write_u16(parent
, rec_off
, start_off
+ diff
);
413 start_off
-= 4; /* move previous cnid too */
415 while (rec_off
> end_rec_off
) {
417 end_off
= hfs_bnode_read_u16(parent
, rec_off
);
418 hfs_bnode_write_u16(parent
, rec_off
, end_off
+ diff
);
420 hfs_bnode_move(parent
, start_off
+ diff
, start_off
,
421 end_off
- start_off
);
423 hfs_bnode_copy(parent
, fd
->keyoffset
, node
, 14, newkeylen
);
424 hfs_bnode_dump(parent
);
432 if (!new_node
->parent
) {
433 hfs_btree_inc_height(tree
);
434 new_node
->parent
= tree
->root
;
436 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
437 /* create index key and entry */
438 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
439 cnid
= cpu_to_be32(new_node
->this);
441 __hfs_brec_find(fd
->bnode
, fd
, hfs_find_rec_by_key
);
442 hfs_brec_insert(fd
, &cnid
, sizeof(cnid
));
443 hfs_bnode_put(fd
->bnode
);
444 hfs_bnode_put(new_node
);
447 if (new_node
== node
)
449 /* restore search_key */
450 hfs_bnode_read_key(node
, fd
->search_key
, 14);
455 if (!rec
&& node
->parent
)
462 static int hfs_btree_inc_height(struct hfs_btree
*tree
)
464 struct hfs_bnode
*node
, *new_node
;
465 struct hfs_bnode_desc node_desc
;
471 node
= hfs_bnode_find(tree
, tree
->root
);
473 return PTR_ERR(node
);
475 new_node
= hfs_bmap_alloc(tree
);
476 if (IS_ERR(new_node
)) {
478 return PTR_ERR(new_node
);
481 tree
->root
= new_node
->this;
483 tree
->leaf_head
= tree
->leaf_tail
= new_node
->this;
484 new_node
->type
= HFS_NODE_LEAF
;
485 new_node
->num_recs
= 0;
487 new_node
->type
= HFS_NODE_INDEX
;
488 new_node
->num_recs
= 1;
490 new_node
->parent
= 0;
493 new_node
->height
= ++tree
->depth
;
495 node_desc
.next
= cpu_to_be32(new_node
->next
);
496 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
497 node_desc
.type
= new_node
->type
;
498 node_desc
.height
= new_node
->height
;
499 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
500 node_desc
.reserved
= 0;
501 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
503 rec
= tree
->node_size
- 2;
504 hfs_bnode_write_u16(new_node
, rec
, 14);
507 /* insert old root idx into new root */
508 node
->parent
= tree
->root
;
509 if (node
->type
== HFS_NODE_LEAF
||
510 tree
->attributes
& HFS_TREE_VARIDXKEYS
||
511 tree
->cnid
== HFSPLUS_ATTR_CNID
)
512 key_size
= hfs_bnode_read_u16(node
, 14) + 2;
514 key_size
= tree
->max_key_len
+ 2;
515 hfs_bnode_copy(new_node
, 14, node
, 14, key_size
);
517 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
) &&
518 (tree
->cnid
!= HFSPLUS_ATTR_CNID
)) {
519 key_size
= tree
->max_key_len
+ 2;
520 hfs_bnode_write_u16(new_node
, 14, tree
->max_key_len
);
522 cnid
= cpu_to_be32(node
->this);
523 hfs_bnode_write(new_node
, &cnid
, 14 + key_size
, 4);
526 hfs_bnode_write_u16(new_node
, rec
, 14 + key_size
+ 4);
530 hfs_bnode_put(new_node
);
531 mark_inode_dirty(tree
->inode
);