Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / fs / hfs / brec.c
blob6d7c42d9672516c67572d9e364f5d5e6f1833605
1 /*
2 * linux/fs/hfs/brec.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle individual btree records
9 */
11 #include "btree.h"
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)
20 __be16 retval[2];
21 u16 dataoff;
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)
32 u16 retval, recoff;
34 if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF)
35 return 0;
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;
41 else
42 retval = node->tree->max_key_len + 1;
43 } else {
44 recoff = hfs_bnode_read_u16(node, node->tree->node_size - (rec + 1) * 2);
45 if (!recoff)
46 return 0;
47 if (node->tree->attributes & HFS_TREE_BIGKEYS) {
48 retval = hfs_bnode_read_u16(node, recoff) + 2;
49 if (retval > node->tree->max_key_len + 2) {
50 printk(KERN_ERR "hfs: keylen %d too large\n",
51 retval);
52 retval = 0;
54 } else {
55 retval = (hfs_bnode_read_u8(node, recoff) | 1) + 1;
56 if (retval > node->tree->max_key_len + 1) {
57 printk(KERN_ERR "hfs: keylen %d too large\n",
58 retval);
59 retval = 0;
63 return retval;
66 int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
68 struct hfs_btree *tree;
69 struct hfs_bnode *node, *new_node;
70 int size, key_len, rec;
71 int data_off, end_off;
72 int idx_rec_off, data_rec_off, end_rec_off;
73 __be32 cnid;
75 tree = fd->tree;
76 if (!fd->bnode) {
77 if (!tree->root)
78 hfs_btree_inc_height(tree);
79 fd->bnode = hfs_bnode_find(tree, tree->leaf_head);
80 if (IS_ERR(fd->bnode))
81 return PTR_ERR(fd->bnode);
82 fd->record = -1;
84 new_node = NULL;
85 key_len = (fd->search_key->key_len | 1) + 1;
86 again:
87 /* new record idx and complete record size */
88 rec = fd->record + 1;
89 size = key_len + entry_len;
91 node = fd->bnode;
92 hfs_bnode_dump(node);
93 /* get last offset */
94 end_rec_off = tree->node_size - (node->num_recs + 1) * 2;
95 end_off = hfs_bnode_read_u16(node, end_rec_off);
96 end_rec_off -= 2;
97 dprint(DBG_BNODE_MOD, "insert_rec: %d, %d, %d, %d\n", rec, size, end_off, end_rec_off);
98 if (size > end_rec_off - end_off) {
99 if (new_node)
100 panic("not enough room!\n");
101 new_node = hfs_bnode_split(fd);
102 if (IS_ERR(new_node))
103 return PTR_ERR(new_node);
104 goto again;
106 if (node->type == HFS_NODE_LEAF) {
107 tree->leaf_count++;
108 mark_inode_dirty(tree->inode);
110 node->num_recs++;
111 /* write new last offset */
112 hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
113 hfs_bnode_write_u16(node, end_rec_off, end_off + size);
114 data_off = end_off;
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)
118 goto skip;
119 /* move all following entries */
120 do {
121 data_off = hfs_bnode_read_u16(node, data_rec_off + 2);
122 hfs_bnode_write_u16(node, data_rec_off, data_off + size);
123 data_rec_off += 2;
124 } while (data_rec_off < idx_rec_off);
126 /* move data away */
127 hfs_bnode_move(node, data_off + size, data_off,
128 end_off - data_off);
130 skip:
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);
135 if (new_node) {
136 /* update parent key if we inserted a key
137 * at the start of the first node
139 if (!rec && new_node != node)
140 hfs_brec_update_parent(fd);
142 hfs_bnode_put(fd->bnode);
143 if (!new_node->parent) {
144 hfs_btree_inc_height(tree);
145 new_node->parent = tree->root;
147 fd->bnode = hfs_bnode_find(tree, new_node->parent);
149 /* create index data entry */
150 cnid = cpu_to_be32(new_node->this);
151 entry = &cnid;
152 entry_len = sizeof(cnid);
154 /* get index key */
155 hfs_bnode_read_key(new_node, fd->search_key, 14);
156 __hfs_brec_find(fd->bnode, fd);
158 hfs_bnode_put(new_node);
159 new_node = NULL;
161 if (tree->attributes & HFS_TREE_VARIDXKEYS)
162 key_len = fd->search_key->key_len + 1;
163 else {
164 fd->search_key->key_len = tree->max_key_len;
165 key_len = tree->max_key_len + 1;
167 goto again;
170 if (!rec)
171 hfs_brec_update_parent(fd);
173 return 0;
176 int hfs_brec_remove(struct hfs_find_data *fd)
178 struct hfs_btree *tree;
179 struct hfs_bnode *node, *parent;
180 int end_off, rec_off, data_off, size;
182 tree = fd->tree;
183 node = fd->bnode;
184 again:
185 rec_off = tree->node_size - (fd->record + 2) * 2;
186 end_off = tree->node_size - (node->num_recs + 1) * 2;
188 if (node->type == HFS_NODE_LEAF) {
189 tree->leaf_count--;
190 mark_inode_dirty(tree->inode);
192 hfs_bnode_dump(node);
193 dprint(DBG_BNODE_MOD, "remove_rec: %d, %d\n", fd->record, fd->keylength + fd->entrylength);
194 if (!--node->num_recs) {
195 hfs_bnode_unlink(node);
196 if (!node->parent)
197 return 0;
198 parent = hfs_bnode_find(tree, node->parent);
199 if (IS_ERR(parent))
200 return PTR_ERR(parent);
201 hfs_bnode_put(node);
202 node = fd->bnode = parent;
204 __hfs_brec_find(node, fd);
205 goto again;
207 hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
209 if (rec_off == end_off)
210 goto skip;
211 size = fd->keylength + fd->entrylength;
213 do {
214 data_off = hfs_bnode_read_u16(node, rec_off);
215 hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
216 rec_off -= 2;
217 } while (rec_off >= end_off);
219 /* fill hole */
220 hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
221 data_off - fd->keyoffset - size);
222 skip:
223 hfs_bnode_dump(node);
224 if (!fd->record)
225 hfs_brec_update_parent(fd);
226 return 0;
229 static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
231 struct hfs_btree *tree;
232 <<<<<<< HEAD:fs/hfs/brec.c
233 struct hfs_bnode *node, *new_node;
234 =======
235 struct hfs_bnode *node, *new_node, *next_node;
236 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/hfs/brec.c
237 struct hfs_bnode_desc node_desc;
238 int num_recs, new_rec_off, new_off, old_rec_off;
239 int data_start, data_end, size;
241 tree = fd->tree;
242 node = fd->bnode;
243 new_node = hfs_bmap_alloc(tree);
244 if (IS_ERR(new_node))
245 return new_node;
246 hfs_bnode_get(node);
247 dprint(DBG_BNODE_MOD, "split_nodes: %d - %d - %d\n",
248 node->this, new_node->this, node->next);
249 new_node->next = node->next;
250 new_node->prev = node->this;
251 new_node->parent = node->parent;
252 new_node->type = node->type;
253 new_node->height = node->height;
255 <<<<<<< HEAD:fs/hfs/brec.c
256 =======
257 if (node->next)
258 next_node = hfs_bnode_find(tree, node->next);
259 else
260 next_node = NULL;
262 if (IS_ERR(next_node)) {
263 hfs_bnode_put(node);
264 hfs_bnode_put(new_node);
265 return next_node;
268 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/hfs/brec.c
269 size = tree->node_size / 2 - node->num_recs * 2 - 14;
270 old_rec_off = tree->node_size - 4;
271 num_recs = 1;
272 for (;;) {
273 data_start = hfs_bnode_read_u16(node, old_rec_off);
274 if (data_start > size)
275 break;
276 old_rec_off -= 2;
277 if (++num_recs < node->num_recs)
278 continue;
279 /* panic? */
280 hfs_bnode_put(node);
281 hfs_bnode_put(new_node);
282 <<<<<<< HEAD:fs/hfs/brec.c
283 =======
284 if (next_node)
285 hfs_bnode_put(next_node);
286 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/hfs/brec.c
287 return ERR_PTR(-ENOSPC);
290 if (fd->record + 1 < num_recs) {
291 /* new record is in the lower half,
292 * so leave some more space there
294 old_rec_off += 2;
295 num_recs--;
296 data_start = hfs_bnode_read_u16(node, old_rec_off);
297 } else {
298 hfs_bnode_put(node);
299 hfs_bnode_get(new_node);
300 fd->bnode = new_node;
301 fd->record -= num_recs;
302 fd->keyoffset -= data_start - 14;
303 fd->entryoffset -= data_start - 14;
305 new_node->num_recs = node->num_recs - num_recs;
306 node->num_recs = num_recs;
308 new_rec_off = tree->node_size - 2;
309 new_off = 14;
310 size = data_start - new_off;
311 num_recs = new_node->num_recs;
312 data_end = data_start;
313 while (num_recs) {
314 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
315 old_rec_off -= 2;
316 new_rec_off -= 2;
317 data_end = hfs_bnode_read_u16(node, old_rec_off);
318 new_off = data_end - size;
319 num_recs--;
321 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
322 hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
324 /* update new bnode header */
325 node_desc.next = cpu_to_be32(new_node->next);
326 node_desc.prev = cpu_to_be32(new_node->prev);
327 node_desc.type = new_node->type;
328 node_desc.height = new_node->height;
329 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
330 node_desc.reserved = 0;
331 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
333 /* update previous bnode header */
334 node->next = new_node->this;
335 hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
336 node_desc.next = cpu_to_be32(node->next);
337 node_desc.num_recs = cpu_to_be16(node->num_recs);
338 hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
340 /* update next bnode header */
341 <<<<<<< HEAD:fs/hfs/brec.c
342 if (new_node->next) {
343 struct hfs_bnode *next_node = hfs_bnode_find(tree, new_node->next);
344 =======
345 if (next_node) {
346 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:fs/hfs/brec.c
347 next_node->prev = new_node->this;
348 hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
349 node_desc.prev = cpu_to_be32(next_node->prev);
350 hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
351 hfs_bnode_put(next_node);
352 } else if (node->this == tree->leaf_tail) {
353 /* if there is no next node, this might be the new tail */
354 tree->leaf_tail = new_node->this;
355 mark_inode_dirty(tree->inode);
358 hfs_bnode_dump(node);
359 hfs_bnode_dump(new_node);
360 hfs_bnode_put(node);
362 return new_node;
365 static int hfs_brec_update_parent(struct hfs_find_data *fd)
367 struct hfs_btree *tree;
368 struct hfs_bnode *node, *new_node, *parent;
369 int newkeylen, diff;
370 int rec, rec_off, end_rec_off;
371 int start_off, end_off;
373 tree = fd->tree;
374 node = fd->bnode;
375 new_node = NULL;
376 if (!node->parent)
377 return 0;
379 again:
380 parent = hfs_bnode_find(tree, node->parent);
381 if (IS_ERR(parent))
382 return PTR_ERR(parent);
383 __hfs_brec_find(parent, fd);
384 hfs_bnode_dump(parent);
385 rec = fd->record;
387 /* size difference between old and new key */
388 if (tree->attributes & HFS_TREE_VARIDXKEYS)
389 newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1;
390 else
391 fd->keylength = newkeylen = tree->max_key_len + 1;
392 dprint(DBG_BNODE_MOD, "update_rec: %d, %d, %d\n", rec, fd->keylength, newkeylen);
394 rec_off = tree->node_size - (rec + 2) * 2;
395 end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
396 diff = newkeylen - fd->keylength;
397 if (!diff)
398 goto skip;
399 if (diff > 0) {
400 end_off = hfs_bnode_read_u16(parent, end_rec_off);
401 if (end_rec_off - end_off < diff) {
403 printk(KERN_DEBUG "hfs: splitting index node...\n");
404 fd->bnode = parent;
405 new_node = hfs_bnode_split(fd);
406 if (IS_ERR(new_node))
407 return PTR_ERR(new_node);
408 parent = fd->bnode;
409 rec = fd->record;
410 rec_off = tree->node_size - (rec + 2) * 2;
411 end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
415 end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
416 hfs_bnode_write_u16(parent, rec_off, start_off + diff);
417 start_off -= 4; /* move previous cnid too */
419 while (rec_off > end_rec_off) {
420 rec_off -= 2;
421 end_off = hfs_bnode_read_u16(parent, rec_off);
422 hfs_bnode_write_u16(parent, rec_off, end_off + diff);
424 hfs_bnode_move(parent, start_off + diff, start_off,
425 end_off - start_off);
426 skip:
427 hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
428 if (!(tree->attributes & HFS_TREE_VARIDXKEYS))
429 hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1);
430 hfs_bnode_dump(parent);
432 hfs_bnode_put(node);
433 node = parent;
435 if (new_node) {
436 __be32 cnid;
438 fd->bnode = hfs_bnode_find(tree, new_node->parent);
439 /* create index key and entry */
440 hfs_bnode_read_key(new_node, fd->search_key, 14);
441 cnid = cpu_to_be32(new_node->this);
443 __hfs_brec_find(fd->bnode, fd);
444 hfs_brec_insert(fd, &cnid, sizeof(cnid));
445 hfs_bnode_put(fd->bnode);
446 hfs_bnode_put(new_node);
448 if (!rec) {
449 if (new_node == node)
450 goto out;
451 /* restore search_key */
452 hfs_bnode_read_key(node, fd->search_key, 14);
456 if (!rec && node->parent)
457 goto again;
458 out:
459 fd->bnode = node;
460 return 0;
463 static int hfs_btree_inc_height(struct hfs_btree *tree)
465 struct hfs_bnode *node, *new_node;
466 struct hfs_bnode_desc node_desc;
467 int key_size, rec;
468 __be32 cnid;
470 node = NULL;
471 if (tree->root) {
472 node = hfs_bnode_find(tree, tree->root);
473 if (IS_ERR(node))
474 return PTR_ERR(node);
476 new_node = hfs_bmap_alloc(tree);
477 if (IS_ERR(new_node)) {
478 hfs_bnode_put(node);
479 return PTR_ERR(new_node);
482 tree->root = new_node->this;
483 if (!tree->depth) {
484 tree->leaf_head = tree->leaf_tail = new_node->this;
485 new_node->type = HFS_NODE_LEAF;
486 new_node->num_recs = 0;
487 } else {
488 new_node->type = HFS_NODE_INDEX;
489 new_node->num_recs = 1;
491 new_node->parent = 0;
492 new_node->next = 0;
493 new_node->prev = 0;
494 new_node->height = ++tree->depth;
496 node_desc.next = cpu_to_be32(new_node->next);
497 node_desc.prev = cpu_to_be32(new_node->prev);
498 node_desc.type = new_node->type;
499 node_desc.height = new_node->height;
500 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
501 node_desc.reserved = 0;
502 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
504 rec = tree->node_size - 2;
505 hfs_bnode_write_u16(new_node, rec, 14);
507 if (node) {
508 /* insert old root idx into new root */
509 node->parent = tree->root;
510 if (node->type == HFS_NODE_LEAF ||
511 tree->attributes & HFS_TREE_VARIDXKEYS)
512 key_size = hfs_bnode_read_u8(node, 14) + 1;
513 else
514 key_size = tree->max_key_len + 1;
515 hfs_bnode_copy(new_node, 14, node, 14, key_size);
517 if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
518 key_size = tree->max_key_len + 1;
519 hfs_bnode_write_u8(new_node, 14, tree->max_key_len);
521 key_size = (key_size + 1) & -2;
522 cnid = cpu_to_be32(node->this);
523 hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
525 rec -= 2;
526 hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
528 hfs_bnode_put(node);
530 hfs_bnode_put(new_node);
531 mark_inode_dirty(tree->inode);
533 return 0;