5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle individual btree records
13 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
);
14 static int hfs_brec_update_parent(struct hfs_find_data
*fd
);
15 static int hfs_btree_inc_height(struct hfs_btree
*tree
);
17 /* Get the length and offset of the given record in the given node */
18 u16
hfs_brec_lenoff(struct hfs_bnode
*node
, u16 rec
, u16
*off
)
23 dataoff
= node
->tree
->node_size
- (rec
+ 2) * 2;
24 hfs_bnode_read(node
, retval
, dataoff
, 4);
25 *off
= be16_to_cpu(retval
[1]);
26 return be16_to_cpu(retval
[0]) - *off
;
29 /* Get the length of the key from a keyed record */
30 u16
hfs_brec_keylen(struct hfs_bnode
*node
, u16 rec
)
34 if (node
->type
!= HFS_NODE_INDEX
&& node
->type
!= HFS_NODE_LEAF
)
37 if ((node
->type
== HFS_NODE_INDEX
) &&
38 !(node
->tree
->attributes
& HFS_TREE_VARIDXKEYS
)) {
39 if (node
->tree
->attributes
& HFS_TREE_BIGKEYS
)
40 retval
= node
->tree
->max_key_len
+ 2;
42 retval
= node
->tree
->max_key_len
+ 1;
44 recoff
= hfs_bnode_read_u16(node
, node
->tree
->node_size
- (rec
+ 1) * 2);
47 if (node
->tree
->attributes
& HFS_TREE_BIGKEYS
)
48 retval
= hfs_bnode_read_u16(node
, recoff
) + 2;
50 retval
= (hfs_bnode_read_u8(node
, recoff
) | 1) + 1;
55 int hfs_brec_insert(struct hfs_find_data
*fd
, void *entry
, int entry_len
)
57 struct hfs_btree
*tree
;
58 struct hfs_bnode
*node
, *new_node
;
59 int size
, key_len
, rec
;
60 int data_off
, end_off
;
61 int idx_rec_off
, data_rec_off
, end_rec_off
;
67 hfs_btree_inc_height(tree
);
68 fd
->bnode
= hfs_bnode_find(tree
, tree
->leaf_head
);
69 if (IS_ERR(fd
->bnode
))
70 return PTR_ERR(fd
->bnode
);
74 key_len
= (fd
->search_key
->key_len
| 1) + 1;
76 /* new record idx and complete record size */
78 size
= key_len
+ entry_len
;
83 end_rec_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
84 end_off
= hfs_bnode_read_u16(node
, end_rec_off
);
86 dprint(DBG_BNODE_MOD
, "insert_rec: %d, %d, %d, %d\n", rec
, size
, end_off
, end_rec_off
);
87 if (size
> end_rec_off
- end_off
) {
89 panic("not enough room!\n");
90 new_node
= hfs_bnode_split(fd
);
92 return PTR_ERR(new_node
);
95 if (node
->type
== HFS_NODE_LEAF
) {
97 mark_inode_dirty(tree
->inode
);
100 /* write new last offset */
101 hfs_bnode_write_u16(node
, offsetof(struct hfs_bnode_desc
, num_recs
), node
->num_recs
);
102 hfs_bnode_write_u16(node
, end_rec_off
, end_off
+ size
);
104 data_rec_off
= end_rec_off
+ 2;
105 idx_rec_off
= tree
->node_size
- (rec
+ 1) * 2;
106 if (idx_rec_off
== data_rec_off
)
108 /* move all following entries */
110 data_off
= hfs_bnode_read_u16(node
, data_rec_off
+ 2);
111 hfs_bnode_write_u16(node
, data_rec_off
, data_off
+ size
);
113 } while (data_rec_off
< idx_rec_off
);
116 hfs_bnode_move(node
, data_off
+ size
, data_off
,
120 hfs_bnode_write(node
, fd
->search_key
, data_off
, key_len
);
121 hfs_bnode_write(node
, entry
, data_off
+ key_len
, entry_len
);
122 hfs_bnode_dump(node
);
125 /* update parent key if we inserted a key
126 * at the start of the first node
128 if (!rec
&& new_node
!= node
)
129 hfs_brec_update_parent(fd
);
131 hfs_bnode_put(fd
->bnode
);
132 if (!new_node
->parent
) {
133 hfs_btree_inc_height(tree
);
134 new_node
->parent
= tree
->root
;
136 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
138 /* create index data entry */
139 cnid
= cpu_to_be32(new_node
->this);
141 entry_len
= sizeof(cnid
);
144 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
145 __hfs_brec_find(fd
->bnode
, fd
);
147 hfs_bnode_put(new_node
);
150 if (tree
->attributes
& HFS_TREE_VARIDXKEYS
)
151 key_len
= fd
->search_key
->key_len
+ 1;
153 fd
->search_key
->key_len
= tree
->max_key_len
;
154 key_len
= tree
->max_key_len
+ 1;
160 hfs_brec_update_parent(fd
);
165 int hfs_brec_remove(struct hfs_find_data
*fd
)
167 struct hfs_btree
*tree
;
168 struct hfs_bnode
*node
, *parent
;
169 int end_off
, rec_off
, data_off
, size
;
174 rec_off
= tree
->node_size
- (fd
->record
+ 2) * 2;
175 end_off
= tree
->node_size
- (node
->num_recs
+ 1) * 2;
177 if (node
->type
== HFS_NODE_LEAF
) {
179 mark_inode_dirty(tree
->inode
);
181 hfs_bnode_dump(node
);
182 dprint(DBG_BNODE_MOD
, "remove_rec: %d, %d\n", fd
->record
, fd
->keylength
+ fd
->entrylength
);
183 if (!--node
->num_recs
) {
184 hfs_bnode_unlink(node
);
187 parent
= hfs_bnode_find(tree
, node
->parent
);
189 return PTR_ERR(parent
);
191 node
= fd
->bnode
= parent
;
193 __hfs_brec_find(node
, fd
);
196 hfs_bnode_write_u16(node
, offsetof(struct hfs_bnode_desc
, num_recs
), node
->num_recs
);
198 if (rec_off
== end_off
)
200 size
= fd
->keylength
+ fd
->entrylength
;
203 data_off
= hfs_bnode_read_u16(node
, rec_off
);
204 hfs_bnode_write_u16(node
, rec_off
+ 2, data_off
- size
);
206 } while (rec_off
>= end_off
);
209 hfs_bnode_move(node
, fd
->keyoffset
, fd
->keyoffset
+ size
,
210 data_off
- fd
->keyoffset
- size
);
212 hfs_bnode_dump(node
);
214 hfs_brec_update_parent(fd
);
218 static struct hfs_bnode
*hfs_bnode_split(struct hfs_find_data
*fd
)
220 struct hfs_btree
*tree
;
221 struct hfs_bnode
*node
, *new_node
;
222 struct hfs_bnode_desc node_desc
;
223 int num_recs
, new_rec_off
, new_off
, old_rec_off
;
224 int data_start
, data_end
, size
;
228 new_node
= hfs_bmap_alloc(tree
);
229 if (IS_ERR(new_node
))
232 dprint(DBG_BNODE_MOD
, "split_nodes: %d - %d - %d\n",
233 node
->this, new_node
->this, node
->next
);
234 new_node
->next
= node
->next
;
235 new_node
->prev
= node
->this;
236 new_node
->parent
= node
->parent
;
237 new_node
->type
= node
->type
;
238 new_node
->height
= node
->height
;
240 size
= tree
->node_size
/ 2 - node
->num_recs
* 2 - 14;
241 old_rec_off
= tree
->node_size
- 4;
244 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
245 if (data_start
> size
)
248 if (++num_recs
< node
->num_recs
)
252 hfs_bnode_put(new_node
);
253 return ERR_PTR(-ENOSPC
);
256 if (fd
->record
+ 1 < num_recs
) {
257 /* new record is in the lower half,
258 * so leave some more space there
262 data_start
= hfs_bnode_read_u16(node
, old_rec_off
);
265 hfs_bnode_get(new_node
);
266 fd
->bnode
= new_node
;
267 fd
->record
-= num_recs
;
268 fd
->keyoffset
-= data_start
- 14;
269 fd
->entryoffset
-= data_start
- 14;
271 new_node
->num_recs
= node
->num_recs
- num_recs
;
272 node
->num_recs
= num_recs
;
274 new_rec_off
= tree
->node_size
- 2;
276 size
= data_start
- new_off
;
277 num_recs
= new_node
->num_recs
;
278 data_end
= data_start
;
280 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
283 data_end
= hfs_bnode_read_u16(node
, old_rec_off
);
284 new_off
= data_end
- size
;
287 hfs_bnode_write_u16(new_node
, new_rec_off
, new_off
);
288 hfs_bnode_copy(new_node
, 14, node
, data_start
, data_end
- data_start
);
290 /* update new bnode header */
291 node_desc
.next
= cpu_to_be32(new_node
->next
);
292 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
293 node_desc
.type
= new_node
->type
;
294 node_desc
.height
= new_node
->height
;
295 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
296 node_desc
.reserved
= 0;
297 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
299 /* update previous bnode header */
300 node
->next
= new_node
->this;
301 hfs_bnode_read(node
, &node_desc
, 0, sizeof(node_desc
));
302 node_desc
.next
= cpu_to_be32(node
->next
);
303 node_desc
.num_recs
= cpu_to_be16(node
->num_recs
);
304 hfs_bnode_write(node
, &node_desc
, 0, sizeof(node_desc
));
306 /* update next bnode header */
307 if (new_node
->next
) {
308 struct hfs_bnode
*next_node
= hfs_bnode_find(tree
, new_node
->next
);
309 next_node
->prev
= new_node
->this;
310 hfs_bnode_read(next_node
, &node_desc
, 0, sizeof(node_desc
));
311 node_desc
.prev
= cpu_to_be32(next_node
->prev
);
312 hfs_bnode_write(next_node
, &node_desc
, 0, sizeof(node_desc
));
313 hfs_bnode_put(next_node
);
314 } else if (node
->this == tree
->leaf_tail
) {
315 /* if there is no next node, this might be the new tail */
316 tree
->leaf_tail
= new_node
->this;
317 mark_inode_dirty(tree
->inode
);
320 hfs_bnode_dump(node
);
321 hfs_bnode_dump(new_node
);
327 static int hfs_brec_update_parent(struct hfs_find_data
*fd
)
329 struct hfs_btree
*tree
;
330 struct hfs_bnode
*node
, *new_node
, *parent
;
332 int rec
, rec_off
, end_rec_off
;
333 int start_off
, end_off
;
342 parent
= hfs_bnode_find(tree
, node
->parent
);
344 return PTR_ERR(parent
);
345 __hfs_brec_find(parent
, fd
);
346 hfs_bnode_dump(parent
);
349 /* size difference between old and new key */
350 if (tree
->attributes
& HFS_TREE_VARIDXKEYS
)
351 newkeylen
= (hfs_bnode_read_u8(node
, 14) | 1) + 1;
353 fd
->keylength
= newkeylen
= tree
->max_key_len
+ 1;
354 dprint(DBG_BNODE_MOD
, "update_rec: %d, %d, %d\n", rec
, fd
->keylength
, newkeylen
);
356 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
357 end_rec_off
= tree
->node_size
- (parent
->num_recs
+ 1) * 2;
358 diff
= newkeylen
- fd
->keylength
;
362 end_off
= hfs_bnode_read_u16(parent
, end_rec_off
);
363 if (end_rec_off
- end_off
< diff
) {
365 printk("splitting index node...\n");
367 new_node
= hfs_bnode_split(fd
);
368 if (IS_ERR(new_node
))
369 return PTR_ERR(new_node
);
372 rec_off
= tree
->node_size
- (rec
+ 2) * 2;
373 end_rec_off
= tree
->node_size
- (parent
->num_recs
+ 1) * 2;
377 end_off
= start_off
= hfs_bnode_read_u16(parent
, rec_off
);
378 hfs_bnode_write_u16(parent
, rec_off
, start_off
+ diff
);
379 start_off
-= 4; /* move previous cnid too */
381 while (rec_off
> end_rec_off
) {
383 end_off
= hfs_bnode_read_u16(parent
, rec_off
);
384 hfs_bnode_write_u16(parent
, rec_off
, end_off
+ diff
);
386 hfs_bnode_move(parent
, start_off
+ diff
, start_off
,
387 end_off
- start_off
);
389 hfs_bnode_copy(parent
, fd
->keyoffset
, node
, 14, newkeylen
);
390 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
))
391 hfs_bnode_write_u8(parent
, fd
->keyoffset
, newkeylen
- 1);
392 hfs_bnode_dump(parent
);
400 fd
->bnode
= hfs_bnode_find(tree
, new_node
->parent
);
401 /* create index key and entry */
402 hfs_bnode_read_key(new_node
, fd
->search_key
, 14);
403 cnid
= cpu_to_be32(new_node
->this);
405 __hfs_brec_find(fd
->bnode
, fd
);
406 hfs_brec_insert(fd
, &cnid
, sizeof(cnid
));
407 hfs_bnode_put(fd
->bnode
);
408 hfs_bnode_put(new_node
);
411 if (new_node
== node
)
413 /* restore search_key */
414 hfs_bnode_read_key(node
, fd
->search_key
, 14);
418 if (!rec
&& node
->parent
)
425 static int hfs_btree_inc_height(struct hfs_btree
*tree
)
427 struct hfs_bnode
*node
, *new_node
;
428 struct hfs_bnode_desc node_desc
;
434 node
= hfs_bnode_find(tree
, tree
->root
);
436 return PTR_ERR(node
);
438 new_node
= hfs_bmap_alloc(tree
);
439 if (IS_ERR(new_node
)) {
441 return PTR_ERR(new_node
);
444 tree
->root
= new_node
->this;
446 tree
->leaf_head
= tree
->leaf_tail
= new_node
->this;
447 new_node
->type
= HFS_NODE_LEAF
;
448 new_node
->num_recs
= 0;
450 new_node
->type
= HFS_NODE_INDEX
;
451 new_node
->num_recs
= 1;
453 new_node
->parent
= 0;
456 new_node
->height
= ++tree
->depth
;
458 node_desc
.next
= cpu_to_be32(new_node
->next
);
459 node_desc
.prev
= cpu_to_be32(new_node
->prev
);
460 node_desc
.type
= new_node
->type
;
461 node_desc
.height
= new_node
->height
;
462 node_desc
.num_recs
= cpu_to_be16(new_node
->num_recs
);
463 node_desc
.reserved
= 0;
464 hfs_bnode_write(new_node
, &node_desc
, 0, sizeof(node_desc
));
466 rec
= tree
->node_size
- 2;
467 hfs_bnode_write_u16(new_node
, rec
, 14);
470 /* insert old root idx into new root */
471 node
->parent
= tree
->root
;
472 if (node
->type
== HFS_NODE_LEAF
||
473 tree
->attributes
& HFS_TREE_VARIDXKEYS
)
474 key_size
= hfs_bnode_read_u8(node
, 14) + 1;
476 key_size
= tree
->max_key_len
+ 1;
477 hfs_bnode_copy(new_node
, 14, node
, 14, key_size
);
479 if (!(tree
->attributes
& HFS_TREE_VARIDXKEYS
)) {
480 key_size
= tree
->max_key_len
+ 1;
481 hfs_bnode_write_u8(new_node
, 14, tree
->max_key_len
);
483 key_size
= (key_size
+ 1) & -2;
484 cnid
= cpu_to_be32(node
->this);
485 hfs_bnode_write(new_node
, &cnid
, 14 + key_size
, 4);
488 hfs_bnode_write_u16(new_node
, rec
, 14 + key_size
+ 4);
492 hfs_bnode_put(new_node
);
493 mark_inode_dirty(tree
->inode
);