]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/hfs/brec.c | |
3 | * | |
4 | * Copyright (C) 2001 | |
5 | * Brad Boyer (flar@allandria.com) | |
6 | * (C) 2003 Ardis Technologies <roman@ardistech.com> | |
7 | * | |
8 | * Handle individual btree records | |
9 | */ | |
10 | ||
11 | #include "btree.h" | |
12 | ||
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); | |
16 | ||
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) | |
19 | { | |
20 | __be16 retval[2]; | |
21 | u16 dataoff; | |
22 | ||
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; | |
27 | } | |
28 | ||
29 | /* Get the length of the key from a keyed record */ | |
30 | u16 hfs_brec_keylen(struct hfs_bnode *node, u16 rec) | |
31 | { | |
32 | u16 retval, recoff; | |
33 | ||
34 | if (node->type != HFS_NODE_INDEX && node->type != HFS_NODE_LEAF) | |
35 | return 0; | |
36 | ||
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; | |
cf059462 | 47 | if (node->tree->attributes & HFS_TREE_BIGKEYS) { |
1da177e4 | 48 | retval = hfs_bnode_read_u16(node, recoff) + 2; |
cf059462 ES |
49 | if (retval > node->tree->max_key_len + 2) { |
50 | printk(KERN_ERR "hfs: keylen %d too large\n", | |
51 | retval); | |
55581d01 | 52 | retval = 0; |
cf059462 ES |
53 | } |
54 | } else { | |
1da177e4 | 55 | retval = (hfs_bnode_read_u8(node, recoff) | 1) + 1; |
cf059462 ES |
56 | if (retval > node->tree->max_key_len + 1) { |
57 | printk(KERN_ERR "hfs: keylen %d too large\n", | |
58 | retval); | |
55581d01 | 59 | retval = 0; |
cf059462 ES |
60 | } |
61 | } | |
1da177e4 LT |
62 | } |
63 | return retval; | |
64 | } | |
65 | ||
66 | int hfs_brec_insert(struct hfs_find_data *fd, void *entry, int entry_len) | |
67 | { | |
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; | |
74 | ||
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; | |
83 | } | |
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; | |
90 | ||
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; | |
105 | } | |
106 | if (node->type == HFS_NODE_LEAF) { | |
107 | tree->leaf_count++; | |
108 | mark_inode_dirty(tree->inode); | |
109 | } | |
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); | |
125 | ||
126 | /* move data away */ | |
127 | hfs_bnode_move(node, data_off + size, data_off, | |
128 | end_off - data_off); | |
129 | ||
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); | |
134 | ||
135 | if (new_node) { | |
136 | /* update parent key if we inserted a key | |
137 | * at the start of the first node | |
138 | */ | |
139 | if (!rec && new_node != node) | |
140 | hfs_brec_update_parent(fd); | |
141 | ||
142 | hfs_bnode_put(fd->bnode); | |
143 | if (!new_node->parent) { | |
144 | hfs_btree_inc_height(tree); | |
145 | new_node->parent = tree->root; | |
146 | } | |
147 | fd->bnode = hfs_bnode_find(tree, new_node->parent); | |
148 | ||
149 | /* create index data entry */ | |
150 | cnid = cpu_to_be32(new_node->this); | |
151 | entry = &cnid; | |
152 | entry_len = sizeof(cnid); | |
153 | ||
154 | /* get index key */ | |
155 | hfs_bnode_read_key(new_node, fd->search_key, 14); | |
156 | __hfs_brec_find(fd->bnode, fd); | |
157 | ||
158 | hfs_bnode_put(new_node); | |
159 | new_node = NULL; | |
160 | ||
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; | |
166 | } | |
167 | goto again; | |
168 | } | |
169 | ||
170 | if (!rec) | |
171 | hfs_brec_update_parent(fd); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | int hfs_brec_remove(struct hfs_find_data *fd) | |
177 | { | |
178 | struct hfs_btree *tree; | |
179 | struct hfs_bnode *node, *parent; | |
180 | int end_off, rec_off, data_off, size; | |
181 | ||
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; | |
187 | ||
188 | if (node->type == HFS_NODE_LEAF) { | |
189 | tree->leaf_count--; | |
190 | mark_inode_dirty(tree->inode); | |
191 | } | |
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; | |
203 | ||
204 | __hfs_brec_find(node, fd); | |
205 | goto again; | |
206 | } | |
207 | hfs_bnode_write_u16(node, offsetof(struct hfs_bnode_desc, num_recs), node->num_recs); | |
208 | ||
209 | if (rec_off == end_off) | |
210 | goto skip; | |
211 | size = fd->keylength + fd->entrylength; | |
212 | ||
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); | |
218 | ||
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; | |
227 | } | |
228 | ||
229 | static struct hfs_bnode *hfs_bnode_split(struct hfs_find_data *fd) | |
230 | { | |
231 | struct hfs_btree *tree; | |
232 | struct hfs_bnode *node, *new_node; | |
233 | struct hfs_bnode_desc node_desc; | |
234 | int num_recs, new_rec_off, new_off, old_rec_off; | |
235 | int data_start, data_end, size; | |
236 | ||
237 | tree = fd->tree; | |
238 | node = fd->bnode; | |
239 | new_node = hfs_bmap_alloc(tree); | |
240 | if (IS_ERR(new_node)) | |
241 | return new_node; | |
242 | hfs_bnode_get(node); | |
243 | dprint(DBG_BNODE_MOD, "split_nodes: %d - %d - %d\n", | |
244 | node->this, new_node->this, node->next); | |
245 | new_node->next = node->next; | |
246 | new_node->prev = node->this; | |
247 | new_node->parent = node->parent; | |
248 | new_node->type = node->type; | |
249 | new_node->height = node->height; | |
250 | ||
251 | size = tree->node_size / 2 - node->num_recs * 2 - 14; | |
252 | old_rec_off = tree->node_size - 4; | |
253 | num_recs = 1; | |
254 | for (;;) { | |
255 | data_start = hfs_bnode_read_u16(node, old_rec_off); | |
256 | if (data_start > size) | |
257 | break; | |
258 | old_rec_off -= 2; | |
259 | if (++num_recs < node->num_recs) | |
260 | continue; | |
261 | /* panic? */ | |
262 | hfs_bnode_put(node); | |
263 | hfs_bnode_put(new_node); | |
264 | return ERR_PTR(-ENOSPC); | |
265 | } | |
266 | ||
267 | if (fd->record + 1 < num_recs) { | |
268 | /* new record is in the lower half, | |
269 | * so leave some more space there | |
270 | */ | |
271 | old_rec_off += 2; | |
272 | num_recs--; | |
273 | data_start = hfs_bnode_read_u16(node, old_rec_off); | |
274 | } else { | |
275 | hfs_bnode_put(node); | |
276 | hfs_bnode_get(new_node); | |
277 | fd->bnode = new_node; | |
278 | fd->record -= num_recs; | |
279 | fd->keyoffset -= data_start - 14; | |
280 | fd->entryoffset -= data_start - 14; | |
281 | } | |
282 | new_node->num_recs = node->num_recs - num_recs; | |
283 | node->num_recs = num_recs; | |
284 | ||
285 | new_rec_off = tree->node_size - 2; | |
286 | new_off = 14; | |
287 | size = data_start - new_off; | |
288 | num_recs = new_node->num_recs; | |
289 | data_end = data_start; | |
290 | while (num_recs) { | |
291 | hfs_bnode_write_u16(new_node, new_rec_off, new_off); | |
292 | old_rec_off -= 2; | |
293 | new_rec_off -= 2; | |
294 | data_end = hfs_bnode_read_u16(node, old_rec_off); | |
295 | new_off = data_end - size; | |
296 | num_recs--; | |
297 | } | |
298 | hfs_bnode_write_u16(new_node, new_rec_off, new_off); | |
299 | hfs_bnode_copy(new_node, 14, node, data_start, data_end - data_start); | |
300 | ||
301 | /* update new bnode header */ | |
302 | node_desc.next = cpu_to_be32(new_node->next); | |
303 | node_desc.prev = cpu_to_be32(new_node->prev); | |
304 | node_desc.type = new_node->type; | |
305 | node_desc.height = new_node->height; | |
306 | node_desc.num_recs = cpu_to_be16(new_node->num_recs); | |
307 | node_desc.reserved = 0; | |
308 | hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc)); | |
309 | ||
310 | /* update previous bnode header */ | |
311 | node->next = new_node->this; | |
312 | hfs_bnode_read(node, &node_desc, 0, sizeof(node_desc)); | |
313 | node_desc.next = cpu_to_be32(node->next); | |
314 | node_desc.num_recs = cpu_to_be16(node->num_recs); | |
315 | hfs_bnode_write(node, &node_desc, 0, sizeof(node_desc)); | |
316 | ||
317 | /* update next bnode header */ | |
318 | if (new_node->next) { | |
319 | struct hfs_bnode *next_node = hfs_bnode_find(tree, new_node->next); | |
320 | next_node->prev = new_node->this; | |
321 | hfs_bnode_read(next_node, &node_desc, 0, sizeof(node_desc)); | |
322 | node_desc.prev = cpu_to_be32(next_node->prev); | |
323 | hfs_bnode_write(next_node, &node_desc, 0, sizeof(node_desc)); | |
324 | hfs_bnode_put(next_node); | |
325 | } else if (node->this == tree->leaf_tail) { | |
326 | /* if there is no next node, this might be the new tail */ | |
327 | tree->leaf_tail = new_node->this; | |
328 | mark_inode_dirty(tree->inode); | |
329 | } | |
330 | ||
331 | hfs_bnode_dump(node); | |
332 | hfs_bnode_dump(new_node); | |
333 | hfs_bnode_put(node); | |
334 | ||
335 | return new_node; | |
336 | } | |
337 | ||
338 | static int hfs_brec_update_parent(struct hfs_find_data *fd) | |
339 | { | |
340 | struct hfs_btree *tree; | |
341 | struct hfs_bnode *node, *new_node, *parent; | |
342 | int newkeylen, diff; | |
343 | int rec, rec_off, end_rec_off; | |
344 | int start_off, end_off; | |
345 | ||
346 | tree = fd->tree; | |
347 | node = fd->bnode; | |
348 | new_node = NULL; | |
349 | if (!node->parent) | |
350 | return 0; | |
351 | ||
352 | again: | |
353 | parent = hfs_bnode_find(tree, node->parent); | |
354 | if (IS_ERR(parent)) | |
355 | return PTR_ERR(parent); | |
356 | __hfs_brec_find(parent, fd); | |
357 | hfs_bnode_dump(parent); | |
358 | rec = fd->record; | |
359 | ||
360 | /* size difference between old and new key */ | |
361 | if (tree->attributes & HFS_TREE_VARIDXKEYS) | |
362 | newkeylen = (hfs_bnode_read_u8(node, 14) | 1) + 1; | |
363 | else | |
364 | fd->keylength = newkeylen = tree->max_key_len + 1; | |
365 | dprint(DBG_BNODE_MOD, "update_rec: %d, %d, %d\n", rec, fd->keylength, newkeylen); | |
366 | ||
367 | rec_off = tree->node_size - (rec + 2) * 2; | |
368 | end_rec_off = tree->node_size - (parent->num_recs + 1) * 2; | |
369 | diff = newkeylen - fd->keylength; | |
370 | if (!diff) | |
371 | goto skip; | |
372 | if (diff > 0) { | |
373 | end_off = hfs_bnode_read_u16(parent, end_rec_off); | |
374 | if (end_rec_off - end_off < diff) { | |
375 | ||
7cf3cc30 | 376 | printk(KERN_DEBUG "hfs: splitting index node...\n"); |
1da177e4 LT |
377 | fd->bnode = parent; |
378 | new_node = hfs_bnode_split(fd); | |
379 | if (IS_ERR(new_node)) | |
380 | return PTR_ERR(new_node); | |
381 | parent = fd->bnode; | |
382 | rec = fd->record; | |
383 | rec_off = tree->node_size - (rec + 2) * 2; | |
384 | end_rec_off = tree->node_size - (parent->num_recs + 1) * 2; | |
385 | } | |
386 | } | |
387 | ||
388 | end_off = start_off = hfs_bnode_read_u16(parent, rec_off); | |
389 | hfs_bnode_write_u16(parent, rec_off, start_off + diff); | |
390 | start_off -= 4; /* move previous cnid too */ | |
391 | ||
392 | while (rec_off > end_rec_off) { | |
393 | rec_off -= 2; | |
394 | end_off = hfs_bnode_read_u16(parent, rec_off); | |
395 | hfs_bnode_write_u16(parent, rec_off, end_off + diff); | |
396 | } | |
397 | hfs_bnode_move(parent, start_off + diff, start_off, | |
398 | end_off - start_off); | |
399 | skip: | |
400 | hfs_bnode_copy(parent, fd->keyoffset, node, 14, newkeylen); | |
401 | if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) | |
402 | hfs_bnode_write_u8(parent, fd->keyoffset, newkeylen - 1); | |
403 | hfs_bnode_dump(parent); | |
404 | ||
405 | hfs_bnode_put(node); | |
406 | node = parent; | |
407 | ||
408 | if (new_node) { | |
409 | __be32 cnid; | |
410 | ||
411 | fd->bnode = hfs_bnode_find(tree, new_node->parent); | |
412 | /* create index key and entry */ | |
413 | hfs_bnode_read_key(new_node, fd->search_key, 14); | |
414 | cnid = cpu_to_be32(new_node->this); | |
415 | ||
416 | __hfs_brec_find(fd->bnode, fd); | |
417 | hfs_brec_insert(fd, &cnid, sizeof(cnid)); | |
418 | hfs_bnode_put(fd->bnode); | |
419 | hfs_bnode_put(new_node); | |
420 | ||
421 | if (!rec) { | |
422 | if (new_node == node) | |
423 | goto out; | |
424 | /* restore search_key */ | |
425 | hfs_bnode_read_key(node, fd->search_key, 14); | |
426 | } | |
427 | } | |
428 | ||
429 | if (!rec && node->parent) | |
430 | goto again; | |
431 | out: | |
432 | fd->bnode = node; | |
433 | return 0; | |
434 | } | |
435 | ||
436 | static int hfs_btree_inc_height(struct hfs_btree *tree) | |
437 | { | |
438 | struct hfs_bnode *node, *new_node; | |
439 | struct hfs_bnode_desc node_desc; | |
440 | int key_size, rec; | |
441 | __be32 cnid; | |
442 | ||
443 | node = NULL; | |
444 | if (tree->root) { | |
445 | node = hfs_bnode_find(tree, tree->root); | |
446 | if (IS_ERR(node)) | |
447 | return PTR_ERR(node); | |
448 | } | |
449 | new_node = hfs_bmap_alloc(tree); | |
450 | if (IS_ERR(new_node)) { | |
451 | hfs_bnode_put(node); | |
452 | return PTR_ERR(new_node); | |
453 | } | |
454 | ||
455 | tree->root = new_node->this; | |
456 | if (!tree->depth) { | |
457 | tree->leaf_head = tree->leaf_tail = new_node->this; | |
458 | new_node->type = HFS_NODE_LEAF; | |
459 | new_node->num_recs = 0; | |
460 | } else { | |
461 | new_node->type = HFS_NODE_INDEX; | |
462 | new_node->num_recs = 1; | |
463 | } | |
464 | new_node->parent = 0; | |
465 | new_node->next = 0; | |
466 | new_node->prev = 0; | |
467 | new_node->height = ++tree->depth; | |
468 | ||
469 | node_desc.next = cpu_to_be32(new_node->next); | |
470 | node_desc.prev = cpu_to_be32(new_node->prev); | |
471 | node_desc.type = new_node->type; | |
472 | node_desc.height = new_node->height; | |
473 | node_desc.num_recs = cpu_to_be16(new_node->num_recs); | |
474 | node_desc.reserved = 0; | |
475 | hfs_bnode_write(new_node, &node_desc, 0, sizeof(node_desc)); | |
476 | ||
477 | rec = tree->node_size - 2; | |
478 | hfs_bnode_write_u16(new_node, rec, 14); | |
479 | ||
480 | if (node) { | |
481 | /* insert old root idx into new root */ | |
482 | node->parent = tree->root; | |
483 | if (node->type == HFS_NODE_LEAF || | |
484 | tree->attributes & HFS_TREE_VARIDXKEYS) | |
485 | key_size = hfs_bnode_read_u8(node, 14) + 1; | |
486 | else | |
487 | key_size = tree->max_key_len + 1; | |
488 | hfs_bnode_copy(new_node, 14, node, 14, key_size); | |
489 | ||
490 | if (!(tree->attributes & HFS_TREE_VARIDXKEYS)) { | |
491 | key_size = tree->max_key_len + 1; | |
492 | hfs_bnode_write_u8(new_node, 14, tree->max_key_len); | |
493 | } | |
494 | key_size = (key_size + 1) & -2; | |
495 | cnid = cpu_to_be32(node->this); | |
496 | hfs_bnode_write(new_node, &cnid, 14 + key_size, 4); | |
497 | ||
498 | rec -= 2; | |
499 | hfs_bnode_write_u16(new_node, rec, 14 + key_size + 4); | |
500 | ||
501 | hfs_bnode_put(node); | |
502 | } | |
503 | hfs_bnode_put(new_node); | |
504 | mark_inode_dirty(tree->inode); | |
505 | ||
506 | return 0; | |
507 | } |