2 * linux/fs/hfsplus/brec.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle individual btree records
11 #include "hfsplus_fs.h"
12 #include "hfsplus_raw.h"
14 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
);
15 static int hfs_brec_update_parent(struct hfs_find_data
*fd
);
16 static int hfs_btree_inc_height(struct hfs_btree
*);
18 /* Get the length and offset of the given record in the given node */
19 u16
hfs_brec_lenoff(struct hfs_bnode
*node
, u16 rec
, u16
*off
)
24 dataoff
= node
->tree
->node_size
- (rec
+ 2) * 2;
25 hfs_bnode_read(node
, retval
, dataoff
, 4);
26 *off
= be16_to_cpu(retval
[1]);
27 return be16_to_cpu(retval
[0]) - *off
;
30 /* Get the length of the key from a keyed record */
31 u16
hfs_brec_keylen(struct hfs_bnode
*node
, u16 rec
)
35 if (node
->type
!= HFS_NODE_INDEX
&& node
->type
!= HFS_NODE_LEAF
)
38 if ((node
->type
== HFS_NODE_INDEX
) &&
39 !(node
->tree
->attributes
& HFS_TREE_VARIDXKEYS
)) {
40 retval
= node
->tree
->max_key_len
+ 2;
42 recoff
= hfs_bnode_read_u16(node
,
43 node
->tree
->node_size
- (rec
+ 1) * 2);
46 if (recoff
> node
->tree
->node_size
- 2) {
47 printk(KERN_ERR
"hfs: recoff %d too large\n", recoff
);
51 retval
= hfs_bnode_read_u16(node
, recoff
) + 2;
52 if (retval
> node
->tree
->max_key_len
+ 2) {
53 printk(KERN_ERR
"hfs: keylen %d too large\n",
61 int hfs_brec_insert(struct hfs_find_data
*fd
, void *entry
, int entry_len
)
63 struct hfs_btree
*tree
;
64 struct hfs_bnode
*node
, *new_node
;
65 int size
, key_len
, rec
;
66 int data_off
, end_off
;
67 int idx_rec_off
, data_rec_off
, end_rec_off
;
73 hfs_btree_inc_height(tree
);
74 fd
->bnode
= hfs_bnode_find(tree
, tree
->leaf_head
);
75 if (IS_ERR(fd
->bnode
))
76 return PTR_ERR(fd
->bnode
);
80 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
82 /* new record idx and complete record size */
84 size
= key_len
+ entry_len
;
89 end_rec_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
90 end_off
= hfs_bnode_read_u16(node
, end_rec_off
);
92 dprint(DBG_BNODE_MOD
, "insert_rec: %d, %d, %d, %d\n",
93 rec
, size
, end_off
, end_rec_off
);
94 if (size
> end_rec_off
- end_off
) {
96 panic("not enough room!\n");
97 new_node
= hfs_bnode_split(fd
);
99 return PTR_ERR(new_node
);
102 if (node
->type
== HFS_NODE_LEAF
) {
104 mark_inode_dirty(tree
->inode
);
107 /* write new last offset */
108 hfs_bnode_write_u16(node
,
109 offsetof(struct hfs_bnode_desc
, num_recs
),
111 hfs_bnode_write_u16(node
, end_rec_off
, end_off
+ size
);
113 data_rec_off
= end_rec_off
+ 2;
114 idx_rec_off
= tree
->node_size
- (rec
+ 1) * 2;
115 if (idx_rec_off
== data_rec_off
)
117 /* move all following entries */
119 data_off
= hfs_bnode_read_u16(node
, data_rec_off
+ 2);
120 hfs_bnode_write_u16(node
, data_rec_off
, data_off
+ size
);
122 } while (data_rec_off
< idx_rec_off
);
125 hfs_bnode_move(node
, data_off
+ size
, data_off
,
129 hfs_bnode_write(node
, fd
->search_key
, data_off
, key_len
);
130 hfs_bnode_write(node
, entry
, data_off
+ key_len
, entry_len
);
131 hfs_bnode_dump(node
);
134 /* update parent key if we inserted a key
135 * at the start of the first node
137 if (!rec
&& new_node
!= node
)
138 hfs_brec_update_parent(fd
);
140 hfs_bnode_put(fd
->bnode
);
141 if (!new_node
->parent
) {
142 hfs_btree_inc_height(tree
);
143 new_node
->parent
= tree
->root
;
145 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
147 /* create index data entry */
148 cnid
= cpu_to_be32(new_node
->this);
150 entry_len
= sizeof(cnid
);
153 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
154 __hfs_brec_find(fd
->bnode
, fd
);
156 hfs_bnode_put(new_node
);
159 if (tree
->attributes
& HFS_TREE_VARIDXKEYS
)
160 key_len
= be16_to_cpu(fd
->search_key
->key_len
) + 2;
162 fd
->search_key
->key_len
=
163 cpu_to_be16(tree
->max_key_len
);
164 key_len
= tree
->max_key_len
+ 2;
170 hfs_brec_update_parent(fd
);
175 int hfs_brec_remove(struct hfs_find_data
*fd
)
177 struct hfs_btree
*tree
;
178 struct hfs_bnode
*node
, *parent
;
179 int end_off
, rec_off
, data_off
, size
;
184 rec_off
= tree
->node_size
- (fd
->record
+ 2) * 2;
185 end_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
187 if (node
->type
== HFS_NODE_LEAF
) {
189 mark_inode_dirty(tree
->inode
);
191 hfs_bnode_dump(node
);
192 dprint(DBG_BNODE_MOD
, "remove_rec: %d, %d\n",
193 fd
->record
, fd
->keylength
+ fd
->entrylength
);
194 if (!--node
->num_recs
) {
195 hfs_bnode_unlink(node
);
198 parent
= hfs_bnode_find(tree
, node
->parent
);
200 return PTR_ERR(parent
);
202 node
= fd
->bnode
= parent
;
204 __hfs_brec_find(node
, fd
);
207 hfs_bnode_write_u16(node
,
208 offsetof(struct hfs_bnode_desc
, num_recs
),
211 if (rec_off
== end_off
)
213 size
= fd
->keylength
+ fd
->entrylength
;
216 data_off
= hfs_bnode_read_u16(node
, rec_off
);
217 hfs_bnode_write_u16(node
, rec_off
+ 2, data_off
- size
);
219 } while (rec_off
>= end_off
);
222 hfs_bnode_move(node
, fd
->keyoffset
, fd
->keyoffset
+ size
,
223 data_off
- fd
->keyoffset
- size
);
225 hfs_bnode_dump(node
);
227 hfs_brec_update_parent(fd
);
231 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
)
233 struct hfs_btree
*tree
;
234 struct hfs_bnode
*node
, *new_node
, *next_node
;
235 struct hfs_bnode_desc node_desc
;
236 int num_recs
, new_rec_off
, new_off
, old_rec_off
;
237 int data_start
, data_end
, size
;
241 new_node
= hfs_bmap_alloc(tree
);
242 if (IS_ERR(new_node
))
245 dprint(DBG_BNODE_MOD
, "split_nodes: %d - %d - %d\n",
246 node
->this, new_node
->this, node
->next
);
247 new_node
->next
= node
->next
;
248 new_node
->prev
= node
->this;
249 new_node
->parent
= node
->parent
;
250 new_node
->type
= node
->type
;
251 new_node
->height
= node
->height
;
254 next_node
= hfs_bnode_find(tree
, node
->next
);
258 if (IS_ERR(next_node
)) {
260 hfs_bnode_put(new_node
);
264 size
= tree
->node_size
/ 2 - node
->num_recs
* 2 - 14;
265 old_rec_off
= tree
->node_size
- 4;
268 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
269 if (data_start
> size
)
272 if (++num_recs
< node
->num_recs
)
276 hfs_bnode_put(new_node
);
278 hfs_bnode_put(next_node
);
279 return ERR_PTR(-ENOSPC
);
282 if (fd
->record
+ 1 < num_recs
) {
283 /* new record is in the lower half,
284 * so leave some more space there
288 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
291 hfs_bnode_get(new_node
);
292 fd
->bnode
= new_node
;
293 fd
->record
-= num_recs
;
294 fd
->keyoffset
-= data_start
- 14;
295 fd
->entryoffset
-= data_start
- 14;
297 new_node
->num_recs
= node
->num_recs
- num_recs
;
298 node
->num_recs
= num_recs
;
300 new_rec_off
= tree
->node_size
- 2;
302 size
= data_start
- new_off
;
303 num_recs
= new_node
->num_recs
;
304 data_end
= data_start
;
306 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
309 data_end
= hfs_bnode_read_u16(node
, old_rec_off
);
310 new_off
= data_end
- size
;
313 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
314 hfs_bnode_copy(new_node
, 14, node
, data_start
, data_end
- data_start
);
316 /* update new bnode header */
317 node_desc
.next
= cpu_to_be32(new_node
->next
);
318 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
319 node_desc
.type
= new_node
->type
;
320 node_desc
.height
= new_node
->height
;
321 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
322 node_desc
.reserved
= 0;
323 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
325 /* update previous bnode header */
326 node
->next
= new_node
->this;
327 hfs_bnode_read(node
, &node_desc
, 0, sizeof(node_desc
));
328 node_desc
.next
= cpu_to_be32(node
->next
);
329 node_desc
.num_recs
= cpu_to_be16(node
->num_recs
);
330 hfs_bnode_write(node
, &node_desc
, 0, sizeof(node_desc
));
332 /* update next bnode header */
334 next_node
->prev
= new_node
->this;
335 hfs_bnode_read(next_node
, &node_desc
, 0, sizeof(node_desc
));
336 node_desc
.prev
= cpu_to_be32(next_node
->prev
);
337 hfs_bnode_write(next_node
, &node_desc
, 0, sizeof(node_desc
));
338 hfs_bnode_put(next_node
);
339 } else if (node
->this == tree
->leaf_tail
) {
340 /* if there is no next node, this might be the new tail */
341 tree
->leaf_tail
= new_node
->this;
342 mark_inode_dirty(tree
->inode
);
345 hfs_bnode_dump(node
);
346 hfs_bnode_dump(new_node
);
352 static int hfs_brec_update_parent(struct hfs_find_data
*fd
)
354 struct hfs_btree
*tree
;
355 struct hfs_bnode
*node
, *new_node
, *parent
;
357 int rec
, rec_off
, end_rec_off
;
358 int start_off
, end_off
;
367 parent
= hfs_bnode_find(tree
, node
->parent
);
369 return PTR_ERR(parent
);
370 __hfs_brec_find(parent
, fd
);
371 hfs_bnode_dump(parent
);
374 /* size difference between old and new key */
375 if (tree
->attributes
& HFS_TREE_VARIDXKEYS
)
376 newkeylen
= hfs_bnode_read_u16(node
, 14) + 2;
378 fd
->keylength
= newkeylen
= tree
->max_key_len
+ 2;
379 dprint(DBG_BNODE_MOD
, "update_rec: %d, %d, %d\n",
380 rec
, fd
->keylength
, newkeylen
);
382 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
383 end_rec_off
= tree
->node_size
- (parent
->num_recs
+ 1) * 2;
384 diff
= newkeylen
- fd
->keylength
;
388 end_off
= hfs_bnode_read_u16(parent
, end_rec_off
);
389 if (end_rec_off
- end_off
< diff
) {
391 dprint(DBG_BNODE_MOD
, "hfs: splitting index node.\n");
393 new_node
= hfs_bnode_split(fd
);
394 if (IS_ERR(new_node
))
395 return PTR_ERR(new_node
);
398 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
399 end_rec_off
= tree
->node_size
-
400 (parent
->num_recs
+ 1) * 2;
404 end_off
= start_off
= hfs_bnode_read_u16(parent
, rec_off
);
405 hfs_bnode_write_u16(parent
, rec_off
, start_off
+ diff
);
406 start_off
-= 4; /* move previous cnid too */
408 while (rec_off
> end_rec_off
) {
410 end_off
= hfs_bnode_read_u16(parent
, rec_off
);
411 hfs_bnode_write_u16(parent
, rec_off
, end_off
+ diff
);
413 hfs_bnode_move(parent
, start_off
+ diff
, start_off
,
414 end_off
- start_off
);
416 hfs_bnode_copy(parent
, fd
->keyoffset
, node
, 14, newkeylen
);
417 hfs_bnode_dump(parent
);
425 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
426 /* create index key and entry */
427 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
428 cnid
= cpu_to_be32(new_node
->this);
430 __hfs_brec_find(fd
->bnode
, fd
);
431 hfs_brec_insert(fd
, &cnid
, sizeof(cnid
));
432 hfs_bnode_put(fd
->bnode
);
433 hfs_bnode_put(new_node
);
436 if (new_node
== node
)
438 /* restore search_key */
439 hfs_bnode_read_key(node
, fd
->search_key
, 14);
443 if (!rec
&& node
->parent
)
450 static int hfs_btree_inc_height(struct hfs_btree
*tree
)
452 struct hfs_bnode
*node
, *new_node
;
453 struct hfs_bnode_desc node_desc
;
459 node
= hfs_bnode_find(tree
, tree
->root
);
461 return PTR_ERR(node
);
463 new_node
= hfs_bmap_alloc(tree
);
464 if (IS_ERR(new_node
)) {
466 return PTR_ERR(new_node
);
469 tree
->root
= new_node
->this;
471 tree
->leaf_head
= tree
->leaf_tail
= new_node
->this;
472 new_node
->type
= HFS_NODE_LEAF
;
473 new_node
->num_recs
= 0;
475 new_node
->type
= HFS_NODE_INDEX
;
476 new_node
->num_recs
= 1;
478 new_node
->parent
= 0;
481 new_node
->height
= ++tree
->depth
;
483 node_desc
.next
= cpu_to_be32(new_node
->next
);
484 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
485 node_desc
.type
= new_node
->type
;
486 node_desc
.height
= new_node
->height
;
487 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
488 node_desc
.reserved
= 0;
489 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
491 rec
= tree
->node_size
- 2;
492 hfs_bnode_write_u16(new_node
, rec
, 14);
495 /* insert old root idx into new root */
496 node
->parent
= tree
->root
;
497 if (node
->type
== HFS_NODE_LEAF
||
498 tree
->attributes
& HFS_TREE_VARIDXKEYS
)
499 key_size
= hfs_bnode_read_u16(node
, 14) + 2;
501 key_size
= tree
->max_key_len
+ 2;
502 hfs_bnode_copy(new_node
, 14, node
, 14, key_size
);
504 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
)) {
505 key_size
= tree
->max_key_len
+ 2;
506 hfs_bnode_write_u16(new_node
, 14, tree
->max_key_len
);
508 cnid
= cpu_to_be32(node
->this);
509 hfs_bnode_write(new_node
, &cnid
, 14 + key_size
, 4);
512 hfs_bnode_write_u16(new_node
, rec
, 14 + key_size
+ 4);
516 hfs_bnode_put(new_node
);
517 mark_inode_dirty(tree
->inode
);