MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / fs / hfs / brec.c
blob10e986c2fcddfa8bf1e46033f592d8ee67e8b9b5
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 /* Get the length and offset of the given record in the given node */
14 u16 hfs_brec_lenoff(struct hfs_bnode *node, u16 rec, u16 *off)
16 __be16 retval[2];
17 u16 dataoff;
19 dataoff = node->tree->node_size - (rec + 2) * 2;
20 hfs_bnode_read(node, retval, dataoff, 4);
21 *off = be16_to_cpu(retval[1]);
22 return be16_to_cpu(retval[0]) - *off;
25 /* Get the length of the key from a keyed record */
26 u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec)
28 u16 retval, recoff;
30 if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF)
31 return 0;
33 if ((node->type == HFS_NODE_INDEX) &&
34 !(node->tree->attributes & HFS_TREE_VARIDXKEYS)) {
35 if (node->tree->attributes & HFS_TREE_BIGKEYS)
36 retval = node->tree->max_key_len + 2;
37 else
38 retval = node->tree->max_key_len + 1;
39 } else {
40 recoff = hfs_bnode_read_u16(node, node->tree->node_size - (rec + 1) * 2);
41 if (!recoff)
42 return 0;
43 if (node->tree->attributes & HFS_TREE_BIGKEYS)
44 retval = hfs_bnode_read_u16(node, recoff) + 2;
45 else
46 retval = (hfs_bnode_read_u8(node, recoff) | 1) + 1;
48 return retval;
51 int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len)
53 struct hfs_btree *tree;
54 struct hfs_bnode *node, *new_node;
55 int size, key_len, rec;
56 int data_off, end_off;
57 int idx_rec_off, data_rec_off, end_rec_off;
58 __be32 cnid;
60 tree = fd->tree;
61 if (!fd->bnode) {
62 if (!tree->root)
63 hfs_btree_inc_height(tree);
64 fd->bnode = hfs_bnode_find(tree, tree->leaf_head);
65 if (IS_ERR(fd->bnode))
66 return PTR_ERR(fd->bnode);
67 fd->record = -1;
69 new_node = NULL;
70 key_len = (fd->search_key->key_len | 1) + 1;
71 again:
72 /* new record idx and complete record size */
73 rec = fd->record + 1;
74 size = key_len + entry_len;
76 node = fd->bnode;
77 hfs_bnode_dump(node);
78 /* get last offset */
79 end_rec_off = tree->node_size - (node->num_recs + 1) * 2;
80 end_off = hfs_bnode_read_u16(node, end_rec_off);
81 end_rec_off -= 2;
82 dprint(DBG_BNODE_MOD, "insert_rec: %d, %d, %d, %d\n", rec, size, end_off, end_rec_off);
83 if (size > end_rec_off - end_off) {
84 if (new_node)
85 panic("not enough room!\n");
86 new_node = hfs_bnode_split(fd);
87 if (IS_ERR(new_node))
88 return PTR_ERR(new_node);
89 goto again;
91 if (node->type == HFS_NODE_LEAF) {
92 tree->leaf_count++;
93 mark_inode_dirty(tree->inode);
95 node->num_recs++;
96 /* write new last offset */
97 hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
98 hfs_bnode_write_u16(node, end_rec_off, end_off + size);
99 data_off = end_off;
100 data_rec_off = end_rec_off + 2;
101 idx_rec_off = tree->node_size - (rec + 1) * 2;
102 if (idx_rec_off == data_rec_off)
103 goto skip;
104 /* move all following entries */
105 do {
106 data_off = hfs_bnode_read_u16(node, data_rec_off + 2);
107 hfs_bnode_write_u16(node, data_rec_off, data_off + size);
108 data_rec_off += 2;
109 } while (data_rec_off < idx_rec_off);
111 /* move data away */
112 hfs_bnode_move(node, data_off + size, data_off,
113 end_off - data_off);
115 skip:
116 hfs_bnode_write(node, fd->search_key, data_off, key_len);
117 hfs_bnode_write(node, entry, data_off + key_len, entry_len);
118 hfs_bnode_dump(node);
120 if (new_node) {
121 /* update parent key if we inserted a key
122 * at the start of the first node
124 if (!rec && new_node != node)
125 hfs_brec_update_parent(fd);
127 hfs_bnode_put(fd->bnode);
128 if (!new_node->parent) {
129 hfs_btree_inc_height(tree);
130 new_node->parent = tree->root;
132 fd->bnode = hfs_bnode_find(tree, new_node->parent);
134 /* create index data entry */
135 cnid = cpu_to_be32(new_node->this);
136 entry = &cnid;
137 entry_len = sizeof(cnid);
139 /* get index key */
140 hfs_bnode_read_key(new_node, fd->search_key, 14);
141 __hfs_brec_find(fd->bnode, fd);
143 hfs_bnode_put(new_node);
144 new_node = NULL;
146 if (tree->attributes & HFS_TREE_VARIDXKEYS)
147 key_len = fd->search_key->key_len + 1;
148 else {
149 fd->search_key->key_len = tree->max_key_len;
150 key_len = tree->max_key_len + 1;
152 goto again;
155 if (!rec)
156 hfs_brec_update_parent(fd);
158 return 0;
161 int hfs_brec_remove(struct hfs_find_data *fd)
163 struct hfs_btree *tree;
164 struct hfs_bnode *node, *parent;
165 int end_off, rec_off, data_off, size;
167 tree = fd->tree;
168 node = fd->bnode;
169 again:
170 rec_off = tree->node_size - (fd->record + 2) * 2;
171 end_off = tree->node_size - (node->num_recs + 1) * 2;
173 if (node->type == HFS_NODE_LEAF) {
174 tree->leaf_count--;
175 mark_inode_dirty(tree->inode);
177 hfs_bnode_dump(node);
178 dprint(DBG_BNODE_MOD, "remove_rec: %d, %d\n", fd->record, fd->keylength + fd->entrylength);
179 if (!--node->num_recs) {
180 hfs_bnode_unlink(node);
181 if (!node->parent)
182 return 0;
183 parent = hfs_bnode_find(tree, node->parent);
184 if (IS_ERR(parent))
185 return PTR_ERR(parent);
186 hfs_bnode_put(node);
187 node = fd->bnode = parent;
189 __hfs_brec_find(node, fd);
190 goto again;
192 hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs);
194 if (rec_off == end_off)
195 goto skip;
196 size = fd->keylength + fd->entrylength;
198 do {
199 data_off = hfs_bnode_read_u16(node, rec_off);
200 hfs_bnode_write_u16(node, rec_off + 2, data_off - size);
201 rec_off -= 2;
202 } while (rec_off >= end_off);
204 /* fill hole */
205 hfs_bnode_move(node, fd->keyoffset, fd->keyoffset + size,
206 data_off - fd->keyoffset - size);
207 skip:
208 hfs_bnode_dump(node);
209 if (!fd->record)
210 hfs_brec_update_parent(fd);
211 return 0;
214 struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd)
216 struct hfs_btree *tree;
217 struct hfs_bnode *node, *new_node;
218 struct hfs_bnode_desc node_desc;
219 int num_recs, new_rec_off, new_off, old_rec_off;
220 int data_start, data_end, size;
222 tree = fd->tree;
223 node = fd->bnode;
224 new_node = hfs_bmap_alloc(tree);
225 if (IS_ERR(new_node))
226 return new_node;
227 hfs_bnode_get(node);
228 dprint(DBG_BNODE_MOD, "split_nodes: %d - %d - %d\n",
229 node->this, new_node->this, node->next);
230 new_node->next = node->next;
231 new_node->prev = node->this;
232 new_node->parent = node->parent;
233 new_node->type = node->type;
234 new_node->height = node->height;
236 size = tree->node_size / 2 - node->num_recs * 2 - 14;
237 old_rec_off = tree->node_size - 4;
238 num_recs = 1;
239 for (;;) {
240 data_start = hfs_bnode_read_u16(node, old_rec_off);
241 if (data_start > size)
242 break;
243 old_rec_off -= 2;
244 if (++num_recs < node->num_recs)
245 continue;
246 /* panic? */
247 hfs_bnode_put(node);
248 hfs_bnode_put(new_node);
249 return ERR_PTR(-ENOSPC);
252 if (fd->record + 1 < num_recs) {
253 /* new record is in the lower half,
254 * so leave some more space there
256 old_rec_off += 2;
257 num_recs--;
258 data_start = hfs_bnode_read_u16(node, old_rec_off);
259 } else {
260 hfs_bnode_put(node);
261 hfs_bnode_get(new_node);
262 fd->bnode = new_node;
263 fd->record -= num_recs;
264 fd->keyoffset -= data_start - 14;
265 fd->entryoffset -= data_start - 14;
267 new_node->num_recs = node->num_recs - num_recs;
268 node->num_recs = num_recs;
270 new_rec_off = tree->node_size - 2;
271 new_off = 14;
272 size = data_start - new_off;
273 num_recs = new_node->num_recs;
274 data_end = data_start;
275 while (num_recs) {
276 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
277 old_rec_off -= 2;
278 new_rec_off -= 2;
279 data_end = hfs_bnode_read_u16(node, old_rec_off);
280 new_off = data_end - size;
281 num_recs--;
283 hfs_bnode_write_u16(new_node, new_rec_off, new_off);
284 hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start);
286 /* update new bnode header */
287 node_desc.next = cpu_to_be32(new_node->next);
288 node_desc.prev = cpu_to_be32(new_node->prev);
289 node_desc.type = new_node->type;
290 node_desc.height = new_node->height;
291 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
292 node_desc.reserved = 0;
293 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
295 /* update previous bnode header */
296 node->next = new_node->this;
297 hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc));
298 node_desc.next = cpu_to_be32(node->next);
299 node_desc.num_recs = cpu_to_be16(node->num_recs);
300 hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc));
302 /* update next bnode header */
303 if (new_node->next) {
304 struct hfs_bnode *next_node = hfs_bnode_find(tree, new_node->next);
305 next_node->prev = new_node->this;
306 hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc));
307 node_desc.prev = cpu_to_be32(next_node->prev);
308 hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc));
309 hfs_bnode_put(next_node);
310 } else if (node->this == tree->leaf_tail) {
311 /* if there is no next node, this might be the new tail */
312 tree->leaf_tail = new_node->this;
313 mark_inode_dirty(tree->inode);
316 hfs_bnode_dump(node);
317 hfs_bnode_dump(new_node);
318 hfs_bnode_put(node);
320 return new_node;
323 int hfs_brec_update_parent(struct hfs_find_data *fd)
325 struct hfs_btree *tree;
326 struct hfs_bnode *node, *new_node, *parent;
327 int newkeylen, diff;
328 int rec, rec_off, end_rec_off;
329 int start_off, end_off;
331 tree = fd->tree;
332 node = fd->bnode;
333 new_node = NULL;
334 if (!node->parent)
335 return 0;
337 again:
338 parent = hfs_bnode_find(tree, node->parent);
339 if (IS_ERR(parent))
340 return PTR_ERR(parent);
341 __hfs_brec_find(parent, fd);
342 hfs_bnode_dump(parent);
343 rec = fd->record;
345 /* size difference between old and new key */
346 if (tree->attributes & HFS_TREE_VARIDXKEYS)
347 newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1;
348 else
349 fd->keylength = newkeylen = tree->max_key_len + 1;
350 dprint(DBG_BNODE_MOD, "update_rec: %d, %d, %d\n", rec, fd->keylength, newkeylen);
352 rec_off = tree->node_size - (rec + 2) * 2;
353 end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
354 diff = newkeylen - fd->keylength;
355 if (!diff)
356 goto skip;
357 if (diff > 0) {
358 end_off = hfs_bnode_read_u16(parent, end_rec_off);
359 if (end_rec_off - end_off < diff) {
361 printk("splitting index node...\n");
362 fd->bnode = parent;
363 new_node = hfs_bnode_split(fd);
364 if (IS_ERR(new_node))
365 return PTR_ERR(new_node);
366 parent = fd->bnode;
367 rec = fd->record;
368 rec_off = tree->node_size - (rec + 2) * 2;
369 end_rec_off = tree->node_size - (parent->num_recs + 1) * 2;
373 end_off = start_off = hfs_bnode_read_u16(parent, rec_off);
374 hfs_bnode_write_u16(parent, rec_off, start_off + diff);
375 start_off -= 4; /* move previous cnid too */
377 while (rec_off > end_rec_off) {
378 rec_off -= 2;
379 end_off = hfs_bnode_read_u16(parent, rec_off);
380 hfs_bnode_write_u16(parent, rec_off, end_off + diff);
382 hfs_bnode_move(parent, start_off + diff, start_off,
383 end_off - start_off);
384 skip:
385 hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen);
386 if (!(tree->attributes & HFS_TREE_VARIDXKEYS))
387 hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1);
388 hfs_bnode_dump(parent);
390 hfs_bnode_put(node);
391 node = parent;
393 if (new_node) {
394 __be32 cnid;
396 fd->bnode = hfs_bnode_find(tree, new_node->parent);
397 /* create index key and entry */
398 hfs_bnode_read_key(new_node, fd->search_key, 14);
399 cnid = cpu_to_be32(new_node->this);
401 __hfs_brec_find(fd->bnode, fd);
402 hfs_brec_insert(fd, &cnid, sizeof(cnid));
403 hfs_bnode_put(fd->bnode);
404 hfs_bnode_put(new_node);
406 if (!rec) {
407 if (new_node == node)
408 goto out;
409 /* restore search_key */
410 hfs_bnode_read_key(node, fd->search_key, 14);
414 if (!rec && node->parent)
415 goto again;
416 out:
417 fd->bnode = node;
418 return 0;
421 int hfs_btree_inc_height(struct hfs_btree *tree)
423 struct hfs_bnode *node, *new_node;
424 struct hfs_bnode_desc node_desc;
425 int key_size, rec;
426 __be32 cnid;
428 node = NULL;
429 if (tree->root) {
430 node = hfs_bnode_find(tree, tree->root);
431 if (IS_ERR(node))
432 return PTR_ERR(node);
434 new_node = hfs_bmap_alloc(tree);
435 if (IS_ERR(new_node)) {
436 hfs_bnode_put(node);
437 return PTR_ERR(new_node);
440 tree->root = new_node->this;
441 if (!tree->depth) {
442 tree->leaf_head = tree->leaf_tail = new_node->this;
443 new_node->type = HFS_NODE_LEAF;
444 new_node->num_recs = 0;
445 } else {
446 new_node->type = HFS_NODE_INDEX;
447 new_node->num_recs = 1;
449 new_node->parent = 0;
450 new_node->next = 0;
451 new_node->prev = 0;
452 new_node->height = ++tree->depth;
454 node_desc.next = cpu_to_be32(new_node->next);
455 node_desc.prev = cpu_to_be32(new_node->prev);
456 node_desc.type = new_node->type;
457 node_desc.height = new_node->height;
458 node_desc.num_recs = cpu_to_be16(new_node->num_recs);
459 node_desc.reserved = 0;
460 hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc));
462 rec = tree->node_size - 2;
463 hfs_bnode_write_u16(new_node, rec, 14);
465 if (node) {
466 /* insert old root idx into new root */
467 node->parent = tree->root;
468 if (node->type == HFS_NODE_LEAF ||
469 tree->attributes & HFS_TREE_VARIDXKEYS)
470 key_size = hfs_bnode_read_u8(node, 14) + 1;
471 else
472 key_size = tree->max_key_len + 1;
473 hfs_bnode_copy(new_node, 14, node, 14, key_size);
475 if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) {
476 key_size = tree->max_key_len + 1;
477 hfs_bnode_write_u8(new_node, 14, tree->max_key_len);
479 key_size = (key_size + 1) & -2;
480 cnid = cpu_to_be32(node->this);
481 hfs_bnode_write(new_node, &cnid, 14 + key_size, 4);
483 rec -= 2;
484 hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4);
486 hfs_bnode_put(node);
488 hfs_bnode_put(new_node);
489 mark_inode_dirty(tree->inode);
491 return 0;