blob: 7645ab3259ea129cf86880e74d8b6c673c533d4c [file] [log] [blame]
Chris Masonbe0e5c02007-01-26 15:51:26 -05001#include <stdio.h>
2#include <stdlib.h>
3#include "kerncompat.h"
Chris Masoneb60cea2007-02-02 09:18:22 -05004#include "radix-tree.h"
5#include "ctree.h"
6#include "disk-io.h"
Chris Mason5de08d72007-02-24 06:24:44 -05007#include "print-tree.h"
Chris Mason9a8dd152007-02-23 08:38:36 -05008
Chris Mason5c680ed2007-02-22 11:39:13 -05009int split_node(struct ctree_root *root, struct ctree_path *path, int level);
10int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size);
Chris Mason5de08d72007-02-24 06:24:44 -050011int push_node_left(struct ctree_root *root, struct ctree_path *path, int level);
12int push_node_right(struct ctree_root *root,
13 struct ctree_path *path, int level);
14int del_ptr(struct ctree_root *root, struct ctree_path *path, int level);
Chris Masond97e63b2007-02-20 16:40:44 -050015
Chris Mason5de08d72007-02-24 06:24:44 -050016inline void init_path(struct ctree_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050017{
18 memset(p, 0, sizeof(*p));
19}
20
Chris Mason5de08d72007-02-24 06:24:44 -050021void release_path(struct ctree_root *root, struct ctree_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050022{
23 int i;
24 for (i = 0; i < MAX_LEVEL; i++) {
25 if (!p->nodes[i])
26 break;
27 tree_block_release(root, p->nodes[i]);
28 }
29}
30
Chris Mason74123bd2007-02-02 11:05:29 -050031/*
32 * The leaf data grows from end-to-front in the node.
33 * this returns the address of the start of the last item,
34 * which is the stop of the leaf data stack
35 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050036static inline unsigned int leaf_data_end(struct leaf *leaf)
37{
38 unsigned int nr = leaf->header.nritems;
39 if (nr == 0)
Chris Masond97e63b2007-02-20 16:40:44 -050040 return sizeof(leaf->data);
Chris Masonbe0e5c02007-01-26 15:51:26 -050041 return leaf->items[nr-1].offset;
42}
43
Chris Mason74123bd2007-02-02 11:05:29 -050044/*
45 * The space between the end of the leaf items and
46 * the start of the leaf data. IOW, how much room
47 * the leaf has left for both items and data
48 */
Chris Mason5de08d72007-02-24 06:24:44 -050049int leaf_free_space(struct leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050050{
51 int data_end = leaf_data_end(leaf);
52 int nritems = leaf->header.nritems;
53 char *items_end = (char *)(leaf->items + nritems + 1);
54 return (char *)(leaf->data + data_end) - (char *)items_end;
55}
56
Chris Mason74123bd2007-02-02 11:05:29 -050057/*
58 * compare two keys in a memcmp fashion
59 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050060int comp_keys(struct key *k1, struct key *k2)
61{
62 if (k1->objectid > k2->objectid)
63 return 1;
64 if (k1->objectid < k2->objectid)
65 return -1;
66 if (k1->flags > k2->flags)
67 return 1;
68 if (k1->flags < k2->flags)
69 return -1;
70 if (k1->offset > k2->offset)
71 return 1;
72 if (k1->offset < k2->offset)
73 return -1;
74 return 0;
75}
Chris Mason74123bd2007-02-02 11:05:29 -050076
77/*
78 * search for key in the array p. items p are item_size apart
79 * and there are 'max' items in p
80 * the slot in the array is returned via slot, and it points to
81 * the place where you would insert key if it is not found in
82 * the array.
83 *
84 * slot may point to max if the key is bigger than all of the keys
85 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050086int generic_bin_search(char *p, int item_size, struct key *key,
87 int max, int *slot)
88{
89 int low = 0;
90 int high = max;
91 int mid;
92 int ret;
93 struct key *tmp;
94
95 while(low < high) {
96 mid = (low + high) / 2;
97 tmp = (struct key *)(p + mid * item_size);
98 ret = comp_keys(tmp, key);
99
100 if (ret < 0)
101 low = mid + 1;
102 else if (ret > 0)
103 high = mid;
104 else {
105 *slot = mid;
106 return 0;
107 }
108 }
109 *slot = low;
110 return 1;
111}
112
Chris Mason97571fd2007-02-24 13:39:08 -0500113/*
114 * simple bin_search frontend that does the right thing for
115 * leaves vs nodes
116 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500117int bin_search(struct node *c, struct key *key, int *slot)
118{
119 if (is_leaf(c->header.flags)) {
120 struct leaf *l = (struct leaf *)c;
121 return generic_bin_search((void *)l->items, sizeof(struct item),
122 key, c->header.nritems, slot);
123 } else {
124 return generic_bin_search((void *)c->keys, sizeof(struct key),
125 key, c->header.nritems, slot);
126 }
127 return -1;
128}
129
Chris Mason74123bd2007-02-02 11:05:29 -0500130/*
131 * look for key in the tree. path is filled in with nodes along the way
132 * if key is found, we return zero and you can find the item in the leaf
133 * level of the path (level 0)
134 *
135 * If the key isn't found, the path points to the slot where it should
136 * be inserted.
Chris Mason97571fd2007-02-24 13:39:08 -0500137 *
138 * if ins_len > 0, nodes and leaves will be split as we walk down the
139 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
140 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500141 */
Chris Mason5de08d72007-02-24 06:24:44 -0500142int search_slot(struct ctree_root *root, struct key *key,
143 struct ctree_path *p, int ins_len)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500144{
Chris Masoneb60cea2007-02-02 09:18:22 -0500145 struct tree_buffer *b = root->node;
146 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500147 int slot;
148 int ret;
149 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500150
Chris Masoneb60cea2007-02-02 09:18:22 -0500151 b->count++;
152 while (b) {
153 c = &b->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500154 level = node_level(c->header.flags);
Chris Masoneb60cea2007-02-02 09:18:22 -0500155 p->nodes[level] = b;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500156 ret = bin_search(c, key, &slot);
157 if (!is_leaf(c->header.flags)) {
158 if (ret && slot > 0)
159 slot -= 1;
160 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500161 if (ins_len > 0 &&
162 c->header.nritems == NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500163 int sret = split_node(root, p, level);
164 BUG_ON(sret > 0);
165 if (sret)
166 return sret;
167 b = p->nodes[level];
168 c = &b->node;
169 slot = p->slots[level];
Chris Mason5de08d72007-02-24 06:24:44 -0500170 } else if (ins_len < 0 &&
171 c->header.nritems <= NODEPTRS_PER_BLOCK/4) {
172 u64 blocknr = b->blocknr;
173 slot = p->slots[level +1];
174 b->count++;
175 if (push_node_left(root, p, level))
176 push_node_right(root, p, level);
177 if (c->header.nritems == 0 &&
178 level < MAX_LEVEL - 1 &&
179 p->nodes[level + 1]) {
180 int tslot = p->slots[level + 1];
181
182 p->slots[level + 1] = slot;
183 del_ptr(root, p, level + 1);
184 p->slots[level + 1] = tslot;
185 tree_block_release(root, b);
186 free_extent(root, blocknr, 1);
187 } else {
188 tree_block_release(root, b);
189 }
190 b = p->nodes[level];
191 c = &b->node;
192 slot = p->slots[level];
Chris Mason5c680ed2007-02-22 11:39:13 -0500193 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500194 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500195 continue;
196 } else {
Chris Mason5c680ed2007-02-22 11:39:13 -0500197 struct leaf *l = (struct leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500198 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500199 if (ins_len > 0 && leaf_free_space(l) <
200 sizeof(struct item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500201 int sret = split_leaf(root, p, ins_len);
202 BUG_ON(sret > 0);
203 if (sret)
204 return sret;
205 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500206 return ret;
207 }
208 }
209 return -1;
210}
211
Chris Mason74123bd2007-02-02 11:05:29 -0500212/*
213 * adjust the pointers going up the tree, starting at level
214 * making sure the right key of each node is points to 'key'.
215 * This is used after shifting pointers to the left, so it stops
216 * fixing up pointers when a given leaf/node is not in slot 0 of the
217 * higher levels
218 */
Chris Masoneb60cea2007-02-02 09:18:22 -0500219static void fixup_low_keys(struct ctree_root *root,
220 struct ctree_path *path, struct key *key,
221 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500222{
223 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500224 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500225 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500226 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500227 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500228 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500229 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500230 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Masoneb60cea2007-02-02 09:18:22 -0500231 write_tree_block(root, path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500232 if (tslot != 0)
233 break;
234 }
235}
236
Chris Mason74123bd2007-02-02 11:05:29 -0500237/*
238 * try to push data from one node into the next node left in the
239 * tree. The src node is found at specified level in the path.
240 * If some bytes were pushed, return 0, otherwise return 1.
241 *
242 * Lower nodes/leaves in the path are not touched, higher nodes may
243 * be modified to reflect the push.
244 *
245 * The path is altered to reflect the push.
246 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500247int push_node_left(struct ctree_root *root, struct ctree_path *path, int level)
248{
249 int slot;
250 struct node *left;
251 struct node *right;
252 int push_items = 0;
253 int left_nritems;
254 int right_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500255 struct tree_buffer *t;
256 struct tree_buffer *right_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500257
258 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
259 return 1;
260 slot = path->slots[level + 1];
261 if (slot == 0)
262 return 1;
263
Chris Masoneb60cea2007-02-02 09:18:22 -0500264 t = read_tree_block(root,
265 path->nodes[level + 1]->node.blockptrs[slot - 1]);
266 left = &t->node;
267 right_buf = path->nodes[level];
268 right = &right_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500269 left_nritems = left->header.nritems;
270 right_nritems = right->header.nritems;
271 push_items = NODEPTRS_PER_BLOCK - (left_nritems + 1);
Chris Masoneb60cea2007-02-02 09:18:22 -0500272 if (push_items <= 0) {
273 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500274 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500275 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500276
277 if (right_nritems < push_items)
278 push_items = right_nritems;
279 memcpy(left->keys + left_nritems, right->keys,
280 push_items * sizeof(struct key));
281 memcpy(left->blockptrs + left_nritems, right->blockptrs,
282 push_items * sizeof(u64));
283 memmove(right->keys, right->keys + push_items,
284 (right_nritems - push_items) * sizeof(struct key));
285 memmove(right->blockptrs, right->blockptrs + push_items,
286 (right_nritems - push_items) * sizeof(u64));
287 right->header.nritems -= push_items;
288 left->header.nritems += push_items;
289
290 /* adjust the pointers going up the tree */
Chris Masoneb60cea2007-02-02 09:18:22 -0500291 fixup_low_keys(root, path, right->keys, level + 1);
292
293 write_tree_block(root, t);
294 write_tree_block(root, right_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500295
296 /* then fixup the leaf pointer in the path */
297 if (path->slots[level] < push_items) {
298 path->slots[level] += left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500299 tree_block_release(root, path->nodes[level]);
300 path->nodes[level] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500301 path->slots[level + 1] -= 1;
302 } else {
303 path->slots[level] -= push_items;
Chris Masoneb60cea2007-02-02 09:18:22 -0500304 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500305 }
306 return 0;
307}
308
Chris Mason74123bd2007-02-02 11:05:29 -0500309/*
310 * try to push data from one node into the next node right in the
311 * tree. The src node is found at specified level in the path.
312 * If some bytes were pushed, return 0, otherwise return 1.
313 *
314 * Lower nodes/leaves in the path are not touched, higher nodes may
315 * be modified to reflect the push.
316 *
317 * The path is altered to reflect the push.
318 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500319int push_node_right(struct ctree_root *root, struct ctree_path *path, int level)
320{
321 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500322 struct tree_buffer *t;
323 struct tree_buffer *src_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500324 struct node *dst;
325 struct node *src;
326 int push_items = 0;
327 int dst_nritems;
328 int src_nritems;
329
Chris Mason74123bd2007-02-02 11:05:29 -0500330 /* can't push from the root */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500331 if (level == MAX_LEVEL - 1 || path->nodes[level + 1] == 0)
332 return 1;
Chris Mason74123bd2007-02-02 11:05:29 -0500333
334 /* only try to push inside the node higher up */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500335 slot = path->slots[level + 1];
336 if (slot == NODEPTRS_PER_BLOCK - 1)
337 return 1;
338
Chris Masoneb60cea2007-02-02 09:18:22 -0500339 if (slot >= path->nodes[level + 1]->node.header.nritems -1)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500340 return 1;
341
Chris Masoneb60cea2007-02-02 09:18:22 -0500342 t = read_tree_block(root,
343 path->nodes[level + 1]->node.blockptrs[slot + 1]);
344 dst = &t->node;
345 src_buffer = path->nodes[level];
346 src = &src_buffer->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500347 dst_nritems = dst->header.nritems;
348 src_nritems = src->header.nritems;
349 push_items = NODEPTRS_PER_BLOCK - (dst_nritems + 1);
Chris Masoneb60cea2007-02-02 09:18:22 -0500350 if (push_items <= 0) {
351 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500352 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500353 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500354
355 if (src_nritems < push_items)
356 push_items = src_nritems;
357 memmove(dst->keys + push_items, dst->keys,
358 dst_nritems * sizeof(struct key));
359 memcpy(dst->keys, src->keys + src_nritems - push_items,
360 push_items * sizeof(struct key));
361
362 memmove(dst->blockptrs + push_items, dst->blockptrs,
363 dst_nritems * sizeof(u64));
364 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
365 push_items * sizeof(u64));
366
367 src->header.nritems -= push_items;
368 dst->header.nritems += push_items;
369
370 /* adjust the pointers going up the tree */
Chris Masoneb60cea2007-02-02 09:18:22 -0500371 memcpy(path->nodes[level + 1]->node.keys + path->slots[level + 1] + 1,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500372 dst->keys, sizeof(struct key));
Chris Masoneb60cea2007-02-02 09:18:22 -0500373
374 write_tree_block(root, path->nodes[level + 1]);
375 write_tree_block(root, t);
376 write_tree_block(root, src_buffer);
377
Chris Mason74123bd2007-02-02 11:05:29 -0500378 /* then fixup the pointers in the path */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500379 if (path->slots[level] >= src->header.nritems) {
380 path->slots[level] -= src->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500381 tree_block_release(root, path->nodes[level]);
382 path->nodes[level] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500383 path->slots[level + 1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500384 } else {
385 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500386 }
387 return 0;
388}
389
Chris Mason97571fd2007-02-24 13:39:08 -0500390/*
391 * helper function to insert a new root level in the tree.
392 * A new node is allocated, and a single item is inserted to
393 * point to the existing root
394 */
Chris Mason5de08d72007-02-24 06:24:44 -0500395static int insert_new_root(struct ctree_root *root,
396 struct ctree_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500397{
398 struct tree_buffer *t;
399 struct node *lower;
400 struct node *c;
401 struct key *lower_key;
402
403 BUG_ON(path->nodes[level]);
404 BUG_ON(path->nodes[level-1] != root->node);
405
406 t = alloc_free_block(root);
407 c = &t->node;
408 memset(c, 0, sizeof(c));
409 c->header.nritems = 1;
410 c->header.flags = node_level(level);
411 c->header.blocknr = t->blocknr;
412 c->header.parentid = root->node->node.header.parentid;
413 lower = &path->nodes[level-1]->node;
414 if (is_leaf(lower->header.flags))
415 lower_key = &((struct leaf *)lower)->items[0].key;
416 else
417 lower_key = lower->keys;
418 memcpy(c->keys, lower_key, sizeof(struct key));
419 c->blockptrs[0] = path->nodes[level-1]->blocknr;
420 /* the super has an extra ref to root->node */
421 tree_block_release(root, root->node);
422 root->node = t;
423 t->count++;
424 write_tree_block(root, t);
425 path->nodes[level] = t;
426 path->slots[level] = 0;
427 return 0;
428}
429
Chris Mason74123bd2007-02-02 11:05:29 -0500430/*
431 * worker function to insert a single pointer in a node.
432 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500433 *
Chris Mason74123bd2007-02-02 11:05:29 -0500434 * slot and level indicate where you want the key to go, and
435 * blocknr is the block the key points to.
436 */
Chris Mason5c680ed2007-02-22 11:39:13 -0500437int insert_ptr(struct ctree_root *root,
Chris Mason74123bd2007-02-02 11:05:29 -0500438 struct ctree_path *path, struct key *key,
439 u64 blocknr, int slot, int level)
440{
Chris Mason74123bd2007-02-02 11:05:29 -0500441 struct node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500442 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500443
444 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500445 lower = &path->nodes[level]->node;
446 nritems = lower->header.nritems;
447 if (slot > nritems)
448 BUG();
449 if (nritems == NODEPTRS_PER_BLOCK)
450 BUG();
451 if (slot != nritems) {
452 memmove(lower->keys + slot + 1, lower->keys + slot,
453 (nritems - slot) * sizeof(struct key));
454 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
455 (nritems - slot) * sizeof(u64));
456 }
457 memcpy(lower->keys + slot, key, sizeof(struct key));
458 lower->blockptrs[slot] = blocknr;
459 lower->header.nritems++;
460 if (lower->keys[1].objectid == 0)
461 BUG();
462 write_tree_block(root, path->nodes[level]);
463 return 0;
464}
465
Chris Mason97571fd2007-02-24 13:39:08 -0500466/*
467 * split the node at the specified level in path in two.
468 * The path is corrected to point to the appropriate node after the split
469 *
470 * Before splitting this tries to make some room in the node by pushing
471 * left and right, if either one works, it returns right away.
472 */
Chris Mason5c680ed2007-02-22 11:39:13 -0500473int split_node(struct ctree_root *root, struct ctree_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500474{
Chris Mason5c680ed2007-02-22 11:39:13 -0500475 struct tree_buffer *t;
476 struct node *c;
477 struct tree_buffer *split_buffer;
478 struct node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500479 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500480 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500481
Chris Mason5c680ed2007-02-22 11:39:13 -0500482 ret = push_node_left(root, path, level);
483 if (!ret)
484 return 0;
485 ret = push_node_right(root, path, level);
486 if (!ret)
487 return 0;
488 t = path->nodes[level];
489 c = &t->node;
490 if (t == root->node) {
491 /* trying to split the root, lets make a new one */
492 ret = insert_new_root(root, path, level + 1);
493 if (ret)
494 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495 }
Chris Mason5c680ed2007-02-22 11:39:13 -0500496 split_buffer = alloc_free_block(root);
497 split = &split_buffer->node;
498 split->header.flags = c->header.flags;
499 split->header.blocknr = split_buffer->blocknr;
500 split->header.parentid = root->node->node.header.parentid;
501 mid = (c->header.nritems + 1) / 2;
502 memcpy(split->keys, c->keys + mid,
503 (c->header.nritems - mid) * sizeof(struct key));
504 memcpy(split->blockptrs, c->blockptrs + mid,
505 (c->header.nritems - mid) * sizeof(u64));
506 split->header.nritems = c->header.nritems - mid;
507 c->header.nritems = mid;
508 write_tree_block(root, t);
509 write_tree_block(root, split_buffer);
510 insert_ptr(root, path, split->keys, split_buffer->blocknr,
511 path->slots[level + 1] + 1, level + 1);
Chris Mason5de08d72007-02-24 06:24:44 -0500512 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500513 path->slots[level] -= mid;
514 tree_block_release(root, t);
515 path->nodes[level] = split_buffer;
516 path->slots[level + 1] += 1;
517 } else {
518 tree_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500519 }
Chris Mason5c680ed2007-02-22 11:39:13 -0500520 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500521}
522
Chris Mason74123bd2007-02-02 11:05:29 -0500523/*
524 * how many bytes are required to store the items in a leaf. start
525 * and nr indicate which items in the leaf to check. This totals up the
526 * space used both by the item structs and the item data
527 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500528int leaf_space_used(struct leaf *l, int start, int nr)
529{
530 int data_len;
531 int end = start + nr - 1;
532
533 if (!nr)
534 return 0;
535 data_len = l->items[start].offset + l->items[start].size;
536 data_len = data_len - l->items[end].offset;
537 data_len += sizeof(struct item) * nr;
538 return data_len;
539}
540
Chris Mason74123bd2007-02-02 11:05:29 -0500541/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500542 * push some data in the path leaf to the right, trying to free up at
543 * least data_size bytes. returns zero if the push worked, nonzero otherwise
544 */
545int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
546 int data_size)
547{
548 struct tree_buffer *left_buf = path->nodes[0];
549 struct leaf *left = &left_buf->leaf;
550 struct leaf *right;
551 struct tree_buffer *right_buf;
552 struct tree_buffer *upper;
553 int slot;
554 int i;
555 int free_space;
556 int push_space = 0;
557 int push_items = 0;
558 struct item *item;
559
560 slot = path->slots[1];
561 if (!path->nodes[1]) {
562 return 1;
563 }
564 upper = path->nodes[1];
565 if (slot >= upper->node.header.nritems - 1) {
566 return 1;
567 }
568 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
569 right = &right_buf->leaf;
570 free_space = leaf_free_space(right);
571 if (free_space < data_size + sizeof(struct item)) {
572 tree_block_release(root, right_buf);
573 return 1;
574 }
575 for (i = left->header.nritems - 1; i >= 0; i--) {
576 item = left->items + i;
577 if (path->slots[0] == i)
578 push_space += data_size + sizeof(*item);
579 if (item->size + sizeof(*item) + push_space > free_space)
580 break;
581 push_items++;
582 push_space += item->size + sizeof(*item);
583 }
584 if (push_items == 0) {
585 tree_block_release(root, right_buf);
586 return 1;
587 }
588 /* push left to right */
589 push_space = left->items[left->header.nritems - push_items].offset +
590 left->items[left->header.nritems - push_items].size;
591 push_space -= leaf_data_end(left);
592 /* make room in the right data area */
593 memmove(right->data + leaf_data_end(right) - push_space,
594 right->data + leaf_data_end(right),
595 LEAF_DATA_SIZE - leaf_data_end(right));
596 /* copy from the left data area */
597 memcpy(right->data + LEAF_DATA_SIZE - push_space,
598 left->data + leaf_data_end(left),
599 push_space);
600 memmove(right->items + push_items, right->items,
601 right->header.nritems * sizeof(struct item));
602 /* copy the items from left to right */
603 memcpy(right->items, left->items + left->header.nritems - push_items,
604 push_items * sizeof(struct item));
605
606 /* update the item pointers */
607 right->header.nritems += push_items;
608 push_space = LEAF_DATA_SIZE;
609 for (i = 0; i < right->header.nritems; i++) {
610 right->items[i].offset = push_space - right->items[i].size;
611 push_space = right->items[i].offset;
612 }
613 left->header.nritems -= push_items;
614
615 write_tree_block(root, left_buf);
616 write_tree_block(root, right_buf);
617 memcpy(upper->node.keys + slot + 1,
618 &right->items[0].key, sizeof(struct key));
619 write_tree_block(root, upper);
620 /* then fixup the leaf pointer in the path */
621 // FIXME use nritems in here somehow
622 if (path->slots[0] >= left->header.nritems) {
623 path->slots[0] -= left->header.nritems;
624 tree_block_release(root, path->nodes[0]);
625 path->nodes[0] = right_buf;
626 path->slots[1] += 1;
627 } else {
628 tree_block_release(root, right_buf);
629 }
630 return 0;
631}
632/*
Chris Mason74123bd2007-02-02 11:05:29 -0500633 * push some data in the path leaf to the left, trying to free up at
634 * least data_size bytes. returns zero if the push worked, nonzero otherwise
635 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500636int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
637 int data_size)
638{
Chris Masoneb60cea2007-02-02 09:18:22 -0500639 struct tree_buffer *right_buf = path->nodes[0];
640 struct leaf *right = &right_buf->leaf;
641 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500642 struct leaf *left;
643 int slot;
644 int i;
645 int free_space;
646 int push_space = 0;
647 int push_items = 0;
648 struct item *item;
649 int old_left_nritems;
650
651 slot = path->slots[1];
652 if (slot == 0) {
653 return 1;
654 }
655 if (!path->nodes[1]) {
656 return 1;
657 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500658 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
659 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500660 free_space = leaf_free_space(left);
661 if (free_space < data_size + sizeof(struct item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500662 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500663 return 1;
664 }
665 for (i = 0; i < right->header.nritems; i++) {
666 item = right->items + i;
667 if (path->slots[0] == i)
668 push_space += data_size + sizeof(*item);
669 if (item->size + sizeof(*item) + push_space > free_space)
670 break;
671 push_items++;
672 push_space += item->size + sizeof(*item);
673 }
674 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500675 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500676 return 1;
677 }
678 /* push data from right to left */
679 memcpy(left->items + left->header.nritems,
680 right->items, push_items * sizeof(struct item));
681 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
682 memcpy(left->data + leaf_data_end(left) - push_space,
683 right->data + right->items[push_items - 1].offset,
684 push_space);
685 old_left_nritems = left->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500686 BUG_ON(old_left_nritems < 0);
687
Chris Masonbe0e5c02007-01-26 15:51:26 -0500688 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
689 left->items[i].offset -= LEAF_DATA_SIZE -
690 left->items[old_left_nritems -1].offset;
691 }
692 left->header.nritems += push_items;
693
694 /* fixup right node */
695 push_space = right->items[push_items-1].offset - leaf_data_end(right);
696 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
697 leaf_data_end(right), push_space);
698 memmove(right->items, right->items + push_items,
699 (right->header.nritems - push_items) * sizeof(struct item));
700 right->header.nritems -= push_items;
701 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500702
Chris Masonbe0e5c02007-01-26 15:51:26 -0500703 for (i = 0; i < right->header.nritems; i++) {
704 right->items[i].offset = push_space - right->items[i].size;
705 push_space = right->items[i].offset;
706 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500707
708 write_tree_block(root, t);
709 write_tree_block(root, right_buf);
710
711 fixup_low_keys(root, path, &right->items[0].key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500712
713 /* then fixup the leaf pointer in the path */
714 if (path->slots[0] < push_items) {
715 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500716 tree_block_release(root, path->nodes[0]);
717 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500718 path->slots[1] -= 1;
719 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -0500720 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500721 path->slots[0] -= push_items;
722 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500723 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724 return 0;
725}
726
Chris Mason74123bd2007-02-02 11:05:29 -0500727/*
728 * split the path's leaf in two, making sure there is at least data_size
729 * available for the resulting leaf level of the path.
730 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500731int split_leaf(struct ctree_root *root, struct ctree_path *path, int data_size)
732{
Chris Masoneb60cea2007-02-02 09:18:22 -0500733 struct tree_buffer *l_buf = path->nodes[0];
734 struct leaf *l = &l_buf->leaf;
735 int nritems;
736 int mid;
737 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500738 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -0500739 struct tree_buffer *right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500740 int space_needed = data_size + sizeof(struct item);
741 int data_copy_size;
742 int rt_data_off;
743 int i;
744 int ret;
745
Chris Mason00ec4c52007-02-24 12:47:20 -0500746 if (push_leaf_left(root, path, data_size) == 0 ||
747 push_leaf_right(root, path, data_size) == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500748 l_buf = path->nodes[0];
749 l = &l_buf->leaf;
750 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
751 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500752 }
Chris Mason5c680ed2007-02-22 11:39:13 -0500753 if (!path->nodes[1]) {
754 ret = insert_new_root(root, path, 1);
755 if (ret)
756 return ret;
757 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500758 slot = path->slots[0];
759 nritems = l->header.nritems;
760 mid = (nritems + 1)/ 2;
761
762 right_buffer = alloc_free_block(root);
763 BUG_ON(!right_buffer);
764 BUG_ON(mid == nritems);
765 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500766 memset(right, 0, sizeof(*right));
767 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -0500768 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500769 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
770 LEAF_DATA_SIZE)
771 BUG();
772 } else {
Chris Mason97571fd2007-02-24 13:39:08 -0500773 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500774 if (leaf_space_used(l, 0, mid + 1) + space_needed >
775 LEAF_DATA_SIZE)
776 BUG();
777 }
778 right->header.nritems = nritems - mid;
Chris Masoneb60cea2007-02-02 09:18:22 -0500779 right->header.blocknr = right_buffer->blocknr;
780 right->header.flags = node_level(0);
Chris Masoncfaa7292007-02-21 17:04:57 -0500781 right->header.parentid = root->node->node.header.parentid;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500782 data_copy_size = l->items[mid].offset + l->items[mid].size -
783 leaf_data_end(l);
784 memcpy(right->items, l->items + mid,
785 (nritems - mid) * sizeof(struct item));
786 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
787 l->data + leaf_data_end(l), data_copy_size);
788 rt_data_off = LEAF_DATA_SIZE -
789 (l->items[mid].offset + l->items[mid].size);
Chris Mason74123bd2007-02-02 11:05:29 -0500790
791 for (i = 0; i < right->header.nritems; i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500792 right->items[i].offset += rt_data_off;
Chris Mason74123bd2007-02-02 11:05:29 -0500793
Chris Masonbe0e5c02007-01-26 15:51:26 -0500794 l->header.nritems = mid;
795 ret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -0500796 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -0500797 write_tree_block(root, right_buffer);
798 write_tree_block(root, l_buf);
799
800 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500801 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500802 tree_block_release(root, path->nodes[0]);
803 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500804 path->slots[0] -= mid;
805 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500806 } else
807 tree_block_release(root, right_buffer);
808 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500809 return ret;
810}
811
Chris Mason74123bd2007-02-02 11:05:29 -0500812/*
813 * Given a key and some data, insert an item into the tree.
814 * This does all the path init required, making room in the tree if needed.
815 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500816int insert_item(struct ctree_root *root, struct key *key,
817 void *data, int data_size)
818{
819 int ret;
820 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500821 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500822 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500823 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500824 unsigned int nritems;
825 unsigned int data_end;
826 struct ctree_path path;
827
Chris Mason74123bd2007-02-02 11:05:29 -0500828 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -0500829 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -0500830 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -0500831 init_path(&path);
Chris Mason5c680ed2007-02-22 11:39:13 -0500832 ret = search_slot(root, key, &path, data_size);
Chris Masoneb60cea2007-02-02 09:18:22 -0500833 if (ret == 0) {
834 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500835 return -EEXIST;
Chris Masoneb60cea2007-02-02 09:18:22 -0500836 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500837
Chris Masoneb60cea2007-02-02 09:18:22 -0500838 slot_orig = path.slots[0];
839 leaf_buf = path.nodes[0];
840 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -0500841
Chris Masonbe0e5c02007-01-26 15:51:26 -0500842 nritems = leaf->header.nritems;
843 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -0500844
Chris Masonbe0e5c02007-01-26 15:51:26 -0500845 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
846 BUG();
847
848 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -0500849 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500850 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500851 fixup_low_keys(root, &path, key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500852 if (slot != nritems) {
853 int i;
854 unsigned int old_data = leaf->items[slot].offset +
855 leaf->items[slot].size;
856
857 /*
858 * item0..itemN ... dataN.offset..dataN.size .. data0.size
859 */
860 /* first correct the data pointers */
861 for (i = slot; i < nritems; i++)
862 leaf->items[i].offset -= data_size;
863
864 /* shift the items */
865 memmove(leaf->items + slot + 1, leaf->items + slot,
866 (nritems - slot) * sizeof(struct item));
867
868 /* shift the data */
869 memmove(leaf->data + data_end - data_size, leaf->data +
870 data_end, old_data - data_end);
871 data_end = old_data;
872 }
Chris Mason74123bd2007-02-02 11:05:29 -0500873 /* copy the new data in */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500874 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
875 leaf->items[slot].offset = data_end - data_size;
876 leaf->items[slot].size = data_size;
877 memcpy(leaf->data + data_end - data_size, data, data_size);
878 leaf->header.nritems += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500879 write_tree_block(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500880 if (leaf_free_space(leaf) < 0)
881 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -0500882 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500883 return 0;
884}
885
Chris Mason74123bd2007-02-02 11:05:29 -0500886/*
Chris Mason5de08d72007-02-24 06:24:44 -0500887 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -0500888 *
889 * If the delete empties a node, the node is removed from the tree,
890 * continuing all the way the root if required. The root is converted into
891 * a leaf if all the nodes are emptied.
892 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500893int del_ptr(struct ctree_root *root, struct ctree_path *path, int level)
894{
895 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -0500896 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500897 struct node *node;
898 int nritems;
Chris Mason9a8dd152007-02-23 08:38:36 -0500899 u64 blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500900
901 while(1) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500902 t = path->nodes[level];
903 if (!t)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500904 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500905 node = &t->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500906 slot = path->slots[level];
907 nritems = node->header.nritems;
908
909 if (slot != nritems -1) {
910 memmove(node->keys + slot, node->keys + slot + 1,
911 sizeof(struct key) * (nritems - slot - 1));
912 memmove(node->blockptrs + slot,
913 node->blockptrs + slot + 1,
914 sizeof(u64) * (nritems - slot - 1));
915 }
916 node->header.nritems--;
Chris Masoneb60cea2007-02-02 09:18:22 -0500917 write_tree_block(root, t);
Chris Mason9a8dd152007-02-23 08:38:36 -0500918 blocknr = t->blocknr;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500919 if (node->header.nritems != 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500920 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500921 fixup_low_keys(root, path, node->keys,
922 level + 1);
Chris Mason5de08d72007-02-24 06:24:44 -0500923 break;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500924 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500925 if (t == root->node) {
926 /* just turn the root into a leaf and break */
927 root->node->node.header.flags = node_level(0);
928 write_tree_block(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500929 break;
930 }
931 level++;
Chris Mason9a8dd152007-02-23 08:38:36 -0500932 free_extent(root, blocknr, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500933 if (!path->nodes[level])
934 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -0500935 }
936 return 0;
937}
938
Chris Mason74123bd2007-02-02 11:05:29 -0500939/*
940 * delete the item at the leaf level in path. If that empties
941 * the leaf, remove it from the tree
942 */
Chris Mason4920c9a2007-01-26 16:38:42 -0500943int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500944{
Chris Masonbe0e5c02007-01-26 15:51:26 -0500945 int slot;
946 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500947 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 int doff;
949 int dsize;
950
Chris Masoneb60cea2007-02-02 09:18:22 -0500951 leaf_buf = path->nodes[0];
952 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -0500953 slot = path->slots[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -0500954 doff = leaf->items[slot].offset;
955 dsize = leaf->items[slot].size;
956
957 if (slot != leaf->header.nritems - 1) {
958 int i;
959 int data_end = leaf_data_end(leaf);
960 memmove(leaf->data + data_end + dsize,
961 leaf->data + data_end,
962 doff - data_end);
963 for (i = slot + 1; i < leaf->header.nritems; i++)
964 leaf->items[i].offset += dsize;
965 memmove(leaf->items + slot, leaf->items + slot + 1,
966 sizeof(struct item) *
967 (leaf->header.nritems - slot - 1));
968 }
969 leaf->header.nritems -= 1;
Chris Mason74123bd2007-02-02 11:05:29 -0500970 /* delete the leaf if we've emptied it */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500971 if (leaf->header.nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500972 if (leaf_buf == root->node) {
973 leaf->header.flags = node_level(0);
974 write_tree_block(root, leaf_buf);
Chris Mason9a8dd152007-02-23 08:38:36 -0500975 } else {
Chris Mason4920c9a2007-01-26 16:38:42 -0500976 del_ptr(root, path, 1);
Chris Mason9a8dd152007-02-23 08:38:36 -0500977 free_extent(root, leaf_buf->blocknr, 1);
978 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500979 } else {
Chris Mason5de08d72007-02-24 06:24:44 -0500980 int used = leaf_space_used(leaf, 0, leaf->header.nritems);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500981 if (slot == 0)
Chris Masoneb60cea2007-02-02 09:18:22 -0500982 fixup_low_keys(root, path, &leaf->items[0].key, 1);
983 write_tree_block(root, leaf_buf);
Chris Mason74123bd2007-02-02 11:05:29 -0500984 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -0500985 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500986 /* push_leaf_left fixes the path.
987 * make sure the path still points to our leaf
988 * for possible call to del_ptr below
989 */
Chris Mason4920c9a2007-01-26 16:38:42 -0500990 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -0500991 leaf_buf->count++;
Chris Mason4920c9a2007-01-26 16:38:42 -0500992 push_leaf_left(root, path, 1);
Chris Mason00ec4c52007-02-24 12:47:20 -0500993 if (leaf->header.nritems)
994 push_leaf_right(root, path, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500995 if (leaf->header.nritems == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -0500996 u64 blocknr = leaf_buf->blocknr;
Chris Mason4920c9a2007-01-26 16:38:42 -0500997 path->slots[1] = slot;
998 del_ptr(root, path, 1);
Chris Mason5de08d72007-02-24 06:24:44 -0500999 tree_block_release(root, leaf_buf);
1000 free_extent(root, blocknr, 1);
1001 } else {
1002 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001003 }
1004 }
1005 }
1006 return 0;
1007}
1008
Chris Mason97571fd2007-02-24 13:39:08 -05001009/*
1010 * walk up the tree as far as required to find the next leaf.
1011 * returns 0 if it found something or -1 if there are no greater leaves.
1012 */
Chris Masond97e63b2007-02-20 16:40:44 -05001013int next_leaf(struct ctree_root *root, struct ctree_path *path)
1014{
1015 int slot;
1016 int level = 1;
1017 u64 blocknr;
1018 struct tree_buffer *c;
Chris Masoncfaa7292007-02-21 17:04:57 -05001019 struct tree_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001020
1021 while(level < MAX_LEVEL) {
1022 if (!path->nodes[level])
1023 return -1;
1024 slot = path->slots[level] + 1;
1025 c = path->nodes[level];
1026 if (slot >= c->node.header.nritems) {
1027 level++;
1028 continue;
1029 }
1030 blocknr = c->node.blockptrs[slot];
Chris Masoncfaa7292007-02-21 17:04:57 -05001031 if (next)
1032 tree_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001033 next = read_tree_block(root, blocknr);
1034 break;
1035 }
1036 path->slots[level] = slot;
1037 while(1) {
1038 level--;
1039 c = path->nodes[level];
1040 tree_block_release(root, c);
1041 path->nodes[level] = next;
1042 path->slots[level] = 0;
1043 if (!level)
1044 break;
1045 next = read_tree_block(root, next->node.blockptrs[0]);
1046 }
1047 return 0;
1048}
1049
Chris Masonbe0e5c02007-01-26 15:51:26 -05001050/* for testing only */
1051int next_key(int i, int max_key) {
Chris Mason5de08d72007-02-24 06:24:44 -05001052 return rand() % max_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05001053 //return i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001054}
1055
1056int main() {
Chris Masoneb60cea2007-02-02 09:18:22 -05001057 struct ctree_root *root;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001058 struct key ins;
Chris Mason4920c9a2007-01-26 16:38:42 -05001059 struct key last = { (u64)-1, 0, 0};
Chris Masonbe0e5c02007-01-26 15:51:26 -05001060 char *buf;
1061 int i;
1062 int num;
1063 int ret;
Chris Mason5de08d72007-02-24 06:24:44 -05001064 int run_size = 20000000;
1065 int max_key = 100000000;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001066 int tree_size = 0;
1067 struct ctree_path path;
Chris Masoncfaa7292007-02-21 17:04:57 -05001068 struct ctree_super_block super;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001069
Chris Masoneb60cea2007-02-02 09:18:22 -05001070 radix_tree_init();
1071
1072
Chris Masoncfaa7292007-02-21 17:04:57 -05001073 root = open_ctree("dbfile", &super);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001074 srand(55);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001075 for (i = 0; i < run_size; i++) {
1076 buf = malloc(64);
1077 num = next_key(i, max_key);
1078 // num = i;
1079 sprintf(buf, "string-%d", num);
Chris Mason5de08d72007-02-24 06:24:44 -05001080 if (i % 10000 == 0)
Chris Mason00ec4c52007-02-24 12:47:20 -05001081 fprintf(stderr, "insert %d:%d\n", num, i);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001082 ins.objectid = num;
1083 ins.offset = 0;
1084 ins.flags = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -05001085 ret = insert_item(root, &ins, buf, strlen(buf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001086 if (!ret)
1087 tree_size++;
Chris Mason5de08d72007-02-24 06:24:44 -05001088 free(buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001089 }
Chris Masoncfaa7292007-02-21 17:04:57 -05001090 write_ctree_super(root, &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001091 close_ctree(root);
Chris Masoncfaa7292007-02-21 17:04:57 -05001092
1093 root = open_ctree("dbfile", &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001094 printf("starting search\n");
Chris Masonbe0e5c02007-01-26 15:51:26 -05001095 srand(55);
1096 for (i = 0; i < run_size; i++) {
1097 num = next_key(i, max_key);
1098 ins.objectid = num;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001099 init_path(&path);
Chris Mason5de08d72007-02-24 06:24:44 -05001100 if (i % 10000 == 0)
Chris Mason00ec4c52007-02-24 12:47:20 -05001101 fprintf(stderr, "search %d:%d\n", num, i);
Chris Mason5c680ed2007-02-22 11:39:13 -05001102 ret = search_slot(root, &ins, &path, 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001103 if (ret) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001104 print_tree(root, root->node);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001105 printf("unable to find %d\n", num);
1106 exit(1);
1107 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001108 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001109 }
Chris Masoncfaa7292007-02-21 17:04:57 -05001110 write_ctree_super(root, &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001111 close_ctree(root);
Chris Masoncfaa7292007-02-21 17:04:57 -05001112 root = open_ctree("dbfile", &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001113 printf("node %p level %d total ptrs %d free spc %lu\n", root->node,
1114 node_level(root->node->node.header.flags),
1115 root->node->node.header.nritems,
1116 NODEPTRS_PER_BLOCK - root->node->node.header.nritems);
1117 printf("all searches good, deleting some items\n");
Chris Masonbe0e5c02007-01-26 15:51:26 -05001118 i = 0;
1119 srand(55);
Chris Mason4920c9a2007-01-26 16:38:42 -05001120 for (i = 0 ; i < run_size/4; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001121 num = next_key(i, max_key);
1122 ins.objectid = num;
Chris Mason4920c9a2007-01-26 16:38:42 -05001123 init_path(&path);
Chris Mason5de08d72007-02-24 06:24:44 -05001124 ret = search_slot(root, &ins, &path, -1);
1125 if (!ret) {
1126 if (i % 10000 == 0)
Chris Mason00ec4c52007-02-24 12:47:20 -05001127 fprintf(stderr, "del %d:%d\n", num, i);
Chris Mason5de08d72007-02-24 06:24:44 -05001128 ret = del_item(root, &path);
1129 if (ret != 0)
1130 BUG();
1131 tree_size--;
1132 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001133 release_path(root, &path);
Chris Mason4920c9a2007-01-26 16:38:42 -05001134 }
Chris Mason5de08d72007-02-24 06:24:44 -05001135 write_ctree_super(root, &super);
1136 close_ctree(root);
1137 root = open_ctree("dbfile", &super);
Chris Mason4920c9a2007-01-26 16:38:42 -05001138 srand(128);
1139 for (i = 0; i < run_size; i++) {
1140 buf = malloc(64);
1141 num = next_key(i, max_key);
1142 sprintf(buf, "string-%d", num);
1143 ins.objectid = num;
Chris Mason5de08d72007-02-24 06:24:44 -05001144 if (i % 10000 == 0)
Chris Mason00ec4c52007-02-24 12:47:20 -05001145 fprintf(stderr, "insert %d:%d\n", num, i);
Chris Masoneb60cea2007-02-02 09:18:22 -05001146 ret = insert_item(root, &ins, buf, strlen(buf));
Chris Mason4920c9a2007-01-26 16:38:42 -05001147 if (!ret)
1148 tree_size++;
Chris Mason5de08d72007-02-24 06:24:44 -05001149 free(buf);
Chris Mason4920c9a2007-01-26 16:38:42 -05001150 }
Chris Masoncfaa7292007-02-21 17:04:57 -05001151 write_ctree_super(root, &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001152 close_ctree(root);
Chris Masoncfaa7292007-02-21 17:04:57 -05001153 root = open_ctree("dbfile", &super);
Chris Masoneb60cea2007-02-02 09:18:22 -05001154 srand(128);
Chris Mason9a8dd152007-02-23 08:38:36 -05001155 printf("starting search2\n");
Chris Masoneb60cea2007-02-02 09:18:22 -05001156 for (i = 0; i < run_size; i++) {
1157 num = next_key(i, max_key);
1158 ins.objectid = num;
1159 init_path(&path);
Chris Mason5de08d72007-02-24 06:24:44 -05001160 if (i % 10000 == 0)
Chris Mason00ec4c52007-02-24 12:47:20 -05001161 fprintf(stderr, "search %d:%d\n", num, i);
Chris Mason5c680ed2007-02-22 11:39:13 -05001162 ret = search_slot(root, &ins, &path, 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05001163 if (ret) {
1164 print_tree(root, root->node);
1165 printf("unable to find %d\n", num);
1166 exit(1);
1167 }
1168 release_path(root, &path);
1169 }
1170 printf("starting big long delete run\n");
1171 while(root->node && root->node->node.header.nritems > 0) {
Chris Mason4920c9a2007-01-26 16:38:42 -05001172 struct leaf *leaf;
1173 int slot;
1174 ins.objectid = (u64)-1;
1175 init_path(&path);
Chris Mason5de08d72007-02-24 06:24:44 -05001176 ret = search_slot(root, &ins, &path, -1);
Chris Mason4920c9a2007-01-26 16:38:42 -05001177 if (ret == 0)
1178 BUG();
1179
Chris Masoneb60cea2007-02-02 09:18:22 -05001180 leaf = &path.nodes[0]->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001181 slot = path.slots[0];
1182 if (slot != leaf->header.nritems)
1183 BUG();
1184 while(path.slots[0] > 0) {
1185 path.slots[0] -= 1;
1186 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001187 leaf = &path.nodes[0]->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001188
1189 if (comp_keys(&last, &leaf->items[slot].key) <= 0)
1190 BUG();
1191 memcpy(&last, &leaf->items[slot].key, sizeof(last));
Chris Mason5de08d72007-02-24 06:24:44 -05001192 if (tree_size % 10000 == 0)
1193 printf("big del %d:%d\n", tree_size, i);
Chris Masoneb60cea2007-02-02 09:18:22 -05001194 ret = del_item(root, &path);
1195 if (ret != 0) {
1196 printf("del_item returned %d\n", ret);
Chris Mason4920c9a2007-01-26 16:38:42 -05001197 BUG();
Chris Masoneb60cea2007-02-02 09:18:22 -05001198 }
Chris Mason4920c9a2007-01-26 16:38:42 -05001199 tree_size--;
1200 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001201 release_path(root, &path);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001202 }
Chris Mason4920c9a2007-01-26 16:38:42 -05001203 printf("tree size is now %d\n", tree_size);
Chris Mason9a8dd152007-02-23 08:38:36 -05001204 printf("map tree\n");
Chris Mason00ec4c52007-02-24 12:47:20 -05001205 print_tree(root->extent_root, root->extent_root->node);
Chris Mason5de08d72007-02-24 06:24:44 -05001206 write_ctree_super(root, &super);
1207 close_ctree(root);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001208 return 0;
1209}