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 fd
->bnode
= hfs_bnode_find(tree
, tree
->leaf_head
);
77 if (IS_ERR(fd
->bnode
))
78 return PTR_ERR(fd
->bnode
);
82 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
84 /* new record idx and complete record size */
86 size
= key_len
+ entry_len
;
91 end_rec_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
92 end_off
= hfs_bnode_read_u16(node
, end_rec_off
);
94 hfs_dbg(BNODE_MOD
, "insert_rec: %d, %d, %d, %d\n",
95 rec
, size
, end_off
, end_rec_off
);
96 if (size
> end_rec_off
- end_off
) {
98 panic("not enough room!\n");
99 new_node
= hfs_bnode_split(fd
);
100 if (IS_ERR(new_node
))
101 return PTR_ERR(new_node
);
104 if (node
->type
== HFS_NODE_LEAF
) {
106 mark_inode_dirty(tree
->inode
);
109 /* write new last offset */
110 hfs_bnode_write_u16(node
,
111 offsetof(struct hfs_bnode_desc
, num_recs
),
113 hfs_bnode_write_u16(node
, end_rec_off
, end_off
+ size
);
115 data_rec_off
= end_rec_off
+ 2;
116 idx_rec_off
= tree
->node_size
- (rec
+ 1) * 2;
117 if (idx_rec_off
== data_rec_off
)
119 /* move all following entries */
121 data_off
= hfs_bnode_read_u16(node
, data_rec_off
+ 2);
122 hfs_bnode_write_u16(node
, data_rec_off
, data_off
+ size
);
124 } while (data_rec_off
< idx_rec_off
);
127 hfs_bnode_move(node
, data_off
+ size
, data_off
,
131 hfs_bnode_write(node
, fd
->search_key
, data_off
, key_len
);
132 hfs_bnode_write(node
, entry
, data_off
+ key_len
, entry_len
);
133 hfs_bnode_dump(node
);
136 * update parent key if we inserted a key
137 * at the start of the node and it is not the new node
139 if (!rec
&& new_node
!= node
) {
140 hfs_bnode_read_key(node
, fd
->search_key
, data_off
+ size
);
141 hfs_brec_update_parent(fd
);
145 hfs_bnode_put(fd
->bnode
);
146 if (!new_node
->parent
) {
147 hfs_btree_inc_height(tree
);
148 new_node
->parent
= tree
->root
;
150 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
152 /* create index data entry */
153 cnid
= cpu_to_be32(new_node
->this);
155 entry_len
= sizeof(cnid
);
158 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
159 __hfs_brec_find(fd
->bnode
, fd
, hfs_find_rec_by_key
);
161 hfs_bnode_put(new_node
);
164 if ((tree
->attributes
& HFS_TREE_VARIDXKEYS
) ||
165 (tree
->cnid
== HFSPLUS_ATTR_CNID
))
166 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
168 fd
->search_key
->key_len
=
169 cpu_to_be16(tree
->max_key_len
);
170 key_len
= tree
->max_key_len
+ 2;
178 int hfs_brec_remove(struct hfs_find_data
*fd
)
180 struct hfs_btree
*tree
;
181 struct hfs_bnode
*node
, *parent
;
182 int end_off
, rec_off
, data_off
, size
;
187 rec_off
= tree
->node_size
- (fd
->record
+ 2) * 2;
188 end_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
190 if (node
->type
== HFS_NODE_LEAF
) {
192 mark_inode_dirty(tree
->inode
);
194 hfs_bnode_dump(node
);
195 hfs_dbg(BNODE_MOD
, "remove_rec: %d, %d\n",
196 fd
->record
, fd
->keylength
+ fd
->entrylength
);
197 if (!--node
->num_recs
) {
198 hfs_bnode_unlink(node
);
201 parent
= hfs_bnode_find(tree
, node
->parent
);
203 return PTR_ERR(parent
);
205 node
= fd
->bnode
= parent
;
207 __hfs_brec_find(node
, fd
, hfs_find_rec_by_key
);
210 hfs_bnode_write_u16(node
,
211 offsetof(struct hfs_bnode_desc
, num_recs
),
214 if (rec_off
== end_off
)
216 size
= fd
->keylength
+ fd
->entrylength
;
219 data_off
= hfs_bnode_read_u16(node
, rec_off
);
220 hfs_bnode_write_u16(node
, rec_off
+ 2, data_off
- size
);
222 } while (rec_off
>= end_off
);
225 hfs_bnode_move(node
, fd
->keyoffset
, fd
->keyoffset
+ size
,
226 data_off
- fd
->keyoffset
- size
);
228 hfs_bnode_dump(node
);
230 hfs_brec_update_parent(fd
);
234 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
)
236 struct hfs_btree
*tree
;
237 struct hfs_bnode
*node
, *new_node
, *next_node
;
238 struct hfs_bnode_desc node_desc
;
239 int num_recs
, new_rec_off
, new_off
, old_rec_off
;
240 int data_start
, data_end
, size
;
244 new_node
= hfs_bmap_alloc(tree
);
245 if (IS_ERR(new_node
))
248 hfs_dbg(BNODE_MOD
, "split_nodes: %d - %d - %d\n",
249 node
->this, new_node
->this, node
->next
);
250 new_node
->next
= node
->next
;
251 new_node
->prev
= node
->this;
252 new_node
->parent
= node
->parent
;
253 new_node
->type
= node
->type
;
254 new_node
->height
= node
->height
;
257 next_node
= hfs_bnode_find(tree
, node
->next
);
261 if (IS_ERR(next_node
)) {
263 hfs_bnode_put(new_node
);
267 size
= tree
->node_size
/ 2 - node
->num_recs
* 2 - 14;
268 old_rec_off
= tree
->node_size
- 4;
271 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
272 if (data_start
> size
)
275 if (++num_recs
< node
->num_recs
)
279 hfs_bnode_put(new_node
);
281 hfs_bnode_put(next_node
);
282 return ERR_PTR(-ENOSPC
);
285 if (fd
->record
+ 1 < num_recs
) {
286 /* new record is in the lower half,
287 * so leave some more space there
291 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
294 hfs_bnode_get(new_node
);
295 fd
->bnode
= new_node
;
296 fd
->record
-= num_recs
;
297 fd
->keyoffset
-= data_start
- 14;
298 fd
->entryoffset
-= data_start
- 14;
300 new_node
->num_recs
= node
->num_recs
- num_recs
;
301 node
->num_recs
= num_recs
;
303 new_rec_off
= tree
->node_size
- 2;
305 size
= data_start
- new_off
;
306 num_recs
= new_node
->num_recs
;
307 data_end
= data_start
;
309 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
312 data_end
= hfs_bnode_read_u16(node
, old_rec_off
);
313 new_off
= data_end
- size
;
316 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
317 hfs_bnode_copy(new_node
, 14, node
, data_start
, data_end
- data_start
);
319 /* update new bnode header */
320 node_desc
.next
= cpu_to_be32(new_node
->next
);
321 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
322 node_desc
.type
= new_node
->type
;
323 node_desc
.height
= new_node
->height
;
324 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
325 node_desc
.reserved
= 0;
326 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
328 /* update previous bnode header */
329 node
->next
= new_node
->this;
330 hfs_bnode_read(node
, &node_desc
, 0, sizeof(node_desc
));
331 node_desc
.next
= cpu_to_be32(node
->next
);
332 node_desc
.num_recs
= cpu_to_be16(node
->num_recs
);
333 hfs_bnode_write(node
, &node_desc
, 0, sizeof(node_desc
));
335 /* update next bnode header */
337 next_node
->prev
= new_node
->this;
338 hfs_bnode_read(next_node
, &node_desc
, 0, sizeof(node_desc
));
339 node_desc
.prev
= cpu_to_be32(next_node
->prev
);
340 hfs_bnode_write(next_node
, &node_desc
, 0, sizeof(node_desc
));
341 hfs_bnode_put(next_node
);
342 } else if (node
->this == tree
->leaf_tail
) {
343 /* if there is no next node, this might be the new tail */
344 tree
->leaf_tail
= new_node
->this;
345 mark_inode_dirty(tree
->inode
);
348 hfs_bnode_dump(node
);
349 hfs_bnode_dump(new_node
);
355 static int hfs_brec_update_parent(struct hfs_find_data
*fd
)
357 struct hfs_btree
*tree
;
358 struct hfs_bnode
*node
, *new_node
, *parent
;
360 int rec
, rec_off
, end_rec_off
;
361 int start_off
, end_off
;
370 parent
= hfs_bnode_find(tree
, node
->parent
);
372 return PTR_ERR(parent
);
373 __hfs_brec_find(parent
, fd
, hfs_find_rec_by_key
);
376 hfs_bnode_dump(parent
);
379 /* size difference between old and new key */
380 if ((tree
->attributes
& HFS_TREE_VARIDXKEYS
) ||
381 (tree
->cnid
== HFSPLUS_ATTR_CNID
))
382 newkeylen
= hfs_bnode_read_u16(node
, 14) + 2;
384 fd
->keylength
= newkeylen
= tree
->max_key_len
+ 2;
385 hfs_dbg(BNODE_MOD
, "update_rec: %d, %d, %d\n",
386 rec
, fd
->keylength
, newkeylen
);
388 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
389 end_rec_off
= tree
->node_size
- (parent
->num_recs
+ 1) * 2;
390 diff
= newkeylen
- fd
->keylength
;
394 end_off
= hfs_bnode_read_u16(parent
, end_rec_off
);
395 if (end_rec_off
- end_off
< diff
) {
397 hfs_dbg(BNODE_MOD
, "splitting index node\n");
399 new_node
= hfs_bnode_split(fd
);
400 if (IS_ERR(new_node
))
401 return PTR_ERR(new_node
);
404 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
405 end_rec_off
= tree
->node_size
-
406 (parent
->num_recs
+ 1) * 2;
410 end_off
= start_off
= hfs_bnode_read_u16(parent
, rec_off
);
411 hfs_bnode_write_u16(parent
, rec_off
, start_off
+ diff
);
412 start_off
-= 4; /* move previous cnid too */
414 while (rec_off
> end_rec_off
) {
416 end_off
= hfs_bnode_read_u16(parent
, rec_off
);
417 hfs_bnode_write_u16(parent
, rec_off
, end_off
+ diff
);
419 hfs_bnode_move(parent
, start_off
+ diff
, start_off
,
420 end_off
- start_off
);
422 hfs_bnode_copy(parent
, fd
->keyoffset
, node
, 14, newkeylen
);
423 hfs_bnode_dump(parent
);
431 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
432 /* create index key and entry */
433 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
434 cnid
= cpu_to_be32(new_node
->this);
436 __hfs_brec_find(fd
->bnode
, fd
, hfs_find_rec_by_key
);
437 hfs_brec_insert(fd
, &cnid
, sizeof(cnid
));
438 hfs_bnode_put(fd
->bnode
);
439 hfs_bnode_put(new_node
);
442 if (new_node
== node
)
444 /* restore search_key */
445 hfs_bnode_read_key(node
, fd
->search_key
, 14);
449 if (!rec
&& node
->parent
)
456 static int hfs_btree_inc_height(struct hfs_btree
*tree
)
458 struct hfs_bnode
*node
, *new_node
;
459 struct hfs_bnode_desc node_desc
;
465 node
= hfs_bnode_find(tree
, tree
->root
);
467 return PTR_ERR(node
);
469 new_node
= hfs_bmap_alloc(tree
);
470 if (IS_ERR(new_node
)) {
472 return PTR_ERR(new_node
);
475 tree
->root
= new_node
->this;
477 tree
->leaf_head
= tree
->leaf_tail
= new_node
->this;
478 new_node
->type
= HFS_NODE_LEAF
;
479 new_node
->num_recs
= 0;
481 new_node
->type
= HFS_NODE_INDEX
;
482 new_node
->num_recs
= 1;
484 new_node
->parent
= 0;
487 new_node
->height
= ++tree
->depth
;
489 node_desc
.next
= cpu_to_be32(new_node
->next
);
490 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
491 node_desc
.type
= new_node
->type
;
492 node_desc
.height
= new_node
->height
;
493 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
494 node_desc
.reserved
= 0;
495 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
497 rec
= tree
->node_size
- 2;
498 hfs_bnode_write_u16(new_node
, rec
, 14);
501 /* insert old root idx into new root */
502 node
->parent
= tree
->root
;
503 if (node
->type
== HFS_NODE_LEAF
||
504 tree
->attributes
& HFS_TREE_VARIDXKEYS
||
505 tree
->cnid
== HFSPLUS_ATTR_CNID
)
506 key_size
= hfs_bnode_read_u16(node
, 14) + 2;
508 key_size
= tree
->max_key_len
+ 2;
509 hfs_bnode_copy(new_node
, 14, node
, 14, key_size
);
511 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
) &&
512 (tree
->cnid
!= HFSPLUS_ATTR_CNID
)) {
513 key_size
= tree
->max_key_len
+ 2;
514 hfs_bnode_write_u16(new_node
, 14, tree
->max_key_len
);
516 cnid
= cpu_to_be32(node
->this);
517 hfs_bnode_write(new_node
, &cnid
, 14 + key_size
, 4);
520 hfs_bnode_write_u16(new_node
, rec
, 14 + key_size
+ 4);
524 hfs_bnode_put(new_node
);
525 mark_inode_dirty(tree
->inode
);