blob: 3c5f4c2dd525d36667d9360b96bffce6004b6d8f [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 Masonaa5d6be2007-02-28 16:35:06 -05009static int split_node(struct ctree_root *root, struct ctree_path *path,
10 int level);
11static int split_leaf(struct ctree_root *root, struct ctree_path *path,
12 int data_size);
Chris Masonbb803952007-03-01 12:04:21 -050013static int push_node_left(struct ctree_root *root, struct tree_buffer *dst,
14 struct tree_buffer *src);
Chris Mason79f95c82007-03-01 15:16:26 -050015static int balance_node_right(struct ctree_root *root,
16 struct tree_buffer *dst_buf,
17 struct tree_buffer *src_buf);
Chris Masonbb803952007-03-01 12:04:21 -050018static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
19 int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050020
Chris Mason5de08d72007-02-24 06:24:44 -050021inline void init_path(struct ctree_path *p)
Chris Masonbe0e5c02007-01-26 15:51:26 -050022{
23 memset(p, 0, sizeof(*p));
24}
25
Chris Mason5de08d72007-02-24 06:24:44 -050026void release_path(struct ctree_root *root, struct ctree_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050027{
28 int i;
29 for (i = 0; i < MAX_LEVEL; i++) {
30 if (!p->nodes[i])
31 break;
32 tree_block_release(root, p->nodes[i]);
33 }
Chris Masonaa5d6be2007-02-28 16:35:06 -050034 memset(p, 0, sizeof(*p));
Chris Masoneb60cea2007-02-02 09:18:22 -050035}
36
Chris Mason74123bd2007-02-02 11:05:29 -050037/*
38 * The leaf data grows from end-to-front in the node.
39 * this returns the address of the start of the last item,
40 * which is the stop of the leaf data stack
41 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050042static inline unsigned int leaf_data_end(struct leaf *leaf)
43{
44 unsigned int nr = leaf->header.nritems;
45 if (nr == 0)
Chris Masond97e63b2007-02-20 16:40:44 -050046 return sizeof(leaf->data);
Chris Masonbe0e5c02007-01-26 15:51:26 -050047 return leaf->items[nr-1].offset;
48}
49
Chris Mason74123bd2007-02-02 11:05:29 -050050/*
51 * The space between the end of the leaf items and
52 * the start of the leaf data. IOW, how much room
53 * the leaf has left for both items and data
54 */
Chris Mason5de08d72007-02-24 06:24:44 -050055int leaf_free_space(struct leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050056{
57 int data_end = leaf_data_end(leaf);
58 int nritems = leaf->header.nritems;
59 char *items_end = (char *)(leaf->items + nritems + 1);
60 return (char *)(leaf->data + data_end) - (char *)items_end;
61}
62
Chris Mason74123bd2007-02-02 11:05:29 -050063/*
64 * compare two keys in a memcmp fashion
65 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050066int comp_keys(struct key *k1, struct key *k2)
67{
68 if (k1->objectid > k2->objectid)
69 return 1;
70 if (k1->objectid < k2->objectid)
71 return -1;
72 if (k1->flags > k2->flags)
73 return 1;
74 if (k1->flags < k2->flags)
75 return -1;
76 if (k1->offset > k2->offset)
77 return 1;
78 if (k1->offset < k2->offset)
79 return -1;
80 return 0;
81}
Chris Mason74123bd2007-02-02 11:05:29 -050082
Chris Masonaa5d6be2007-02-28 16:35:06 -050083int check_node(struct ctree_path *path, int level)
84{
85 int i;
86 struct node *parent = NULL;
87 struct node *node = &path->nodes[level]->node;
88 int parent_slot;
89
90 if (path->nodes[level + 1])
91 parent = &path->nodes[level + 1]->node;
92 parent_slot = path->slots[level + 1];
93 if (parent && node->header.nritems > 0) {
94 struct key *parent_key;
95 parent_key = &parent->keys[parent_slot];
96 BUG_ON(memcmp(parent_key, node->keys, sizeof(struct key)));
97 BUG_ON(parent->blockptrs[parent_slot] != node->header.blocknr);
98 }
99 BUG_ON(node->header.nritems > NODEPTRS_PER_BLOCK);
100 for (i = 0; i < node->header.nritems - 2; i++) {
101 BUG_ON(comp_keys(&node->keys[i], &node->keys[i+1]) >= 0);
102 }
103 return 0;
104}
105
106int check_leaf(struct ctree_path *path, int level)
107{
108 int i;
109 struct leaf *leaf = &path->nodes[level]->leaf;
110 struct node *parent = NULL;
111 int parent_slot;
112
113 if (path->nodes[level + 1])
114 parent = &path->nodes[level + 1]->node;
115 parent_slot = path->slots[level + 1];
116 if (parent && leaf->header.nritems > 0) {
117 struct key *parent_key;
118 parent_key = &parent->keys[parent_slot];
119 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
120 sizeof(struct key)));
121 BUG_ON(parent->blockptrs[parent_slot] != leaf->header.blocknr);
122 }
123 for (i = 0; i < leaf->header.nritems - 2; i++) {
124 BUG_ON(comp_keys(&leaf->items[i].key,
125 &leaf->items[i+1].key) >= 0);
126 BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset +
127 leaf->items[i + 1].size);
128 if (i == 0) {
129 BUG_ON(leaf->items[i].offset + leaf->items[i].size !=
130 LEAF_DATA_SIZE);
131 }
132 }
133 BUG_ON(leaf_free_space(leaf) < 0);
134 return 0;
135}
136
137int check_block(struct ctree_path *path, int level)
138{
139 if (level == 0)
140 return check_leaf(path, level);
141 return check_node(path, level);
142}
143
Chris Mason74123bd2007-02-02 11:05:29 -0500144/*
145 * search for key in the array p. items p are item_size apart
146 * and there are 'max' items in p
147 * the slot in the array is returned via slot, and it points to
148 * the place where you would insert key if it is not found in
149 * the array.
150 *
151 * slot may point to max if the key is bigger than all of the keys
152 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500153int generic_bin_search(char *p, int item_size, struct key *key,
154 int max, int *slot)
155{
156 int low = 0;
157 int high = max;
158 int mid;
159 int ret;
160 struct key *tmp;
161
162 while(low < high) {
163 mid = (low + high) / 2;
164 tmp = (struct key *)(p + mid * item_size);
165 ret = comp_keys(tmp, key);
166
167 if (ret < 0)
168 low = mid + 1;
169 else if (ret > 0)
170 high = mid;
171 else {
172 *slot = mid;
173 return 0;
174 }
175 }
176 *slot = low;
177 return 1;
178}
179
Chris Mason97571fd2007-02-24 13:39:08 -0500180/*
181 * simple bin_search frontend that does the right thing for
182 * leaves vs nodes
183 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500184int bin_search(struct node *c, struct key *key, int *slot)
185{
186 if (is_leaf(c->header.flags)) {
187 struct leaf *l = (struct leaf *)c;
188 return generic_bin_search((void *)l->items, sizeof(struct item),
189 key, c->header.nritems, slot);
190 } else {
191 return generic_bin_search((void *)c->keys, sizeof(struct key),
192 key, c->header.nritems, slot);
193 }
194 return -1;
195}
196
Chris Masonbb803952007-03-01 12:04:21 -0500197struct tree_buffer *read_node_slot(struct ctree_root *root,
198 struct tree_buffer *parent_buf,
199 int slot)
200{
201 struct node *node = &parent_buf->node;
202 if (slot < 0)
203 return NULL;
204 if (slot >= node->header.nritems)
205 return NULL;
206 return read_tree_block(root, node->blockptrs[slot]);
207}
208
209static int balance_level(struct ctree_root *root, struct ctree_path *path,
210 int level)
211{
212 struct tree_buffer *right_buf;
213 struct tree_buffer *mid_buf;
214 struct tree_buffer *left_buf;
215 struct tree_buffer *parent_buf = NULL;
216 struct node *right = NULL;
217 struct node *mid;
218 struct node *left = NULL;
219 struct node *parent = NULL;
220 int ret = 0;
221 int wret;
222 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500223 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500224 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500225
226 if (level == 0)
227 return 0;
228
229 mid_buf = path->nodes[level];
230 mid = &mid_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500231 orig_ptr = mid->blockptrs[orig_slot];
232
Chris Masonbb803952007-03-01 12:04:21 -0500233 if (level < MAX_LEVEL - 1)
234 parent_buf = path->nodes[level + 1];
235 pslot = path->slots[level + 1];
236
237 if (!parent_buf) {
238 struct tree_buffer *child;
239 u64 blocknr = mid_buf->blocknr;
240
241 if (mid->header.nritems != 1)
242 return 0;
243
244 /* promote the child to a root */
245 child = read_node_slot(root, mid_buf, 0);
246 BUG_ON(!child);
247 root->node = child;
248 path->nodes[level] = NULL;
249 /* once for the path */
250 tree_block_release(root, mid_buf);
251 /* once for the root ptr */
252 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500253 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500254 return free_extent(root, blocknr, 1);
255 }
256 parent = &parent_buf->node;
257
258 if (mid->header.nritems > NODEPTRS_PER_BLOCK / 4)
259 return 0;
260
Chris Masonbb803952007-03-01 12:04:21 -0500261 left_buf = read_node_slot(root, parent_buf, pslot - 1);
262 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500263
264 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500265 if (left_buf) {
266 left = &left_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500267 orig_slot += left->header.nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500268 wret = push_node_left(root, left_buf, mid_buf);
269 if (wret < 0)
270 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500271 }
Chris Mason79f95c82007-03-01 15:16:26 -0500272
273 /*
274 * then try to empty the right most buffer into the middle
275 */
Chris Masonbb803952007-03-01 12:04:21 -0500276 if (right_buf) {
Chris Mason79f95c82007-03-01 15:16:26 -0500277 right = &right_buf->node;
278 wret = push_node_left(root, mid_buf, right_buf);
279 if (wret < 0)
280 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500281 if (right->header.nritems == 0) {
282 u64 blocknr = right_buf->blocknr;
283 tree_block_release(root, right_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500284 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500285 right_buf = NULL;
286 right = NULL;
287 wret = del_ptr(root, path, level + 1, pslot + 1);
288 if (wret)
289 ret = wret;
290 wret = free_extent(root, blocknr, 1);
291 if (wret)
292 ret = wret;
293 } else {
294 memcpy(parent->keys + pslot + 1, right->keys,
295 sizeof(struct key));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500296 wret = dirty_tree_block(root, parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500297 if (wret)
298 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500299 }
300 }
Chris Mason79f95c82007-03-01 15:16:26 -0500301 if (mid->header.nritems == 1) {
302 /*
303 * we're not allowed to leave a node with one item in the
304 * tree during a delete. A deletion from lower in the tree
305 * could try to delete the only pointer in this node.
306 * So, pull some keys from the left.
307 * There has to be a left pointer at this point because
308 * otherwise we would have pulled some pointers from the
309 * right
310 */
311 BUG_ON(!left_buf);
312 wret = balance_node_right(root, mid_buf, left_buf);
313 if (wret < 0)
314 ret = wret;
315 BUG_ON(wret == 1);
316 }
Chris Masonbb803952007-03-01 12:04:21 -0500317 if (mid->header.nritems == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500318 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500319 u64 blocknr = mid_buf->blocknr;
320 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500321 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500322 mid_buf = NULL;
323 mid = NULL;
324 wret = del_ptr(root, path, level + 1, pslot);
325 if (wret)
326 ret = wret;
327 wret = free_extent(root, blocknr, 1);
328 if (wret)
329 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500330 } else {
331 /* update the parent key to reflect our changes */
Chris Masonbb803952007-03-01 12:04:21 -0500332 memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500333 wret = dirty_tree_block(root, parent_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500334 if (wret)
335 ret = wret;
336 }
Chris Masonbb803952007-03-01 12:04:21 -0500337
Chris Mason79f95c82007-03-01 15:16:26 -0500338 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500339 if (left_buf) {
Chris Mason79f95c82007-03-01 15:16:26 -0500340 if (left->header.nritems > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500341 left_buf->count++; // released below
342 path->nodes[level] = left_buf;
343 path->slots[level + 1] -= 1;
344 path->slots[level] = orig_slot;
345 if (mid_buf)
346 tree_block_release(root, mid_buf);
347 } else {
348 orig_slot -= left->header.nritems;
349 path->slots[level] = orig_slot;
350 }
351 }
Chris Mason79f95c82007-03-01 15:16:26 -0500352 /* double check we haven't messed things up */
353 check_block(path, level);
354 if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]])
355 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500356
357 if (right_buf)
358 tree_block_release(root, right_buf);
359 if (left_buf)
360 tree_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500361 return ret;
362}
363
Chris Mason74123bd2007-02-02 11:05:29 -0500364/*
365 * look for key in the tree. path is filled in with nodes along the way
366 * if key is found, we return zero and you can find the item in the leaf
367 * level of the path (level 0)
368 *
369 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500370 * be inserted, and 1 is returned. If there are other errors during the
371 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500372 *
373 * if ins_len > 0, nodes and leaves will be split as we walk down the
374 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
375 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500376 */
Chris Mason5de08d72007-02-24 06:24:44 -0500377int search_slot(struct ctree_root *root, struct key *key,
378 struct ctree_path *p, int ins_len)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500379{
Chris Masonbb803952007-03-01 12:04:21 -0500380 struct tree_buffer *b;
Chris Masoneb60cea2007-02-02 09:18:22 -0500381 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500382 int slot;
383 int ret;
384 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500385
Chris Masonbb803952007-03-01 12:04:21 -0500386again:
387 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500388 b->count++;
389 while (b) {
390 c = &b->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500391 level = node_level(c->header.flags);
Chris Masoneb60cea2007-02-02 09:18:22 -0500392 p->nodes[level] = b;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500393 ret = check_block(p, level);
394 if (ret)
395 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500396 ret = bin_search(c, key, &slot);
397 if (!is_leaf(c->header.flags)) {
398 if (ret && slot > 0)
399 slot -= 1;
400 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500401 if (ins_len > 0 &&
402 c->header.nritems == NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500403 int sret = split_node(root, p, level);
404 BUG_ON(sret > 0);
405 if (sret)
406 return sret;
407 b = p->nodes[level];
408 c = &b->node;
409 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500410 } else if (ins_len < 0) {
411 int sret = balance_level(root, p, level);
412 if (sret)
413 return sret;
414 b = p->nodes[level];
415 if (!b)
416 goto again;
417 c = &b->node;
418 slot = p->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500419 BUG_ON(c->header.nritems == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500420 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500421 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500422 } else {
Chris Mason5c680ed2007-02-22 11:39:13 -0500423 struct leaf *l = (struct leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500424 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500425 if (ins_len > 0 && leaf_free_space(l) <
426 sizeof(struct item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500427 int sret = split_leaf(root, p, ins_len);
428 BUG_ON(sret > 0);
429 if (sret)
430 return sret;
431 }
Chris Masonbb803952007-03-01 12:04:21 -0500432 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500433 return ret;
434 }
435 }
Chris Masonbb803952007-03-01 12:04:21 -0500436 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500437 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500438}
439
Chris Mason74123bd2007-02-02 11:05:29 -0500440/*
441 * adjust the pointers going up the tree, starting at level
442 * making sure the right key of each node is points to 'key'.
443 * This is used after shifting pointers to the left, so it stops
444 * fixing up pointers when a given leaf/node is not in slot 0 of the
445 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500446 *
447 * If this fails to write a tree block, it returns -1, but continues
448 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500449 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500450static int fixup_low_keys(struct ctree_root *root,
Chris Masoneb60cea2007-02-02 09:18:22 -0500451 struct ctree_path *path, struct key *key,
452 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500453{
454 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500455 int ret = 0;
456 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500457 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500458 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500459 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500460 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500461 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500462 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500463 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500464 wret = dirty_tree_block(root, path->nodes[i]);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500465 if (wret)
466 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500467 if (tslot != 0)
468 break;
469 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500470 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500471}
472
Chris Mason74123bd2007-02-02 11:05:29 -0500473/*
474 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500475 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500476 *
477 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
478 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500479 */
Chris Masonbb803952007-03-01 12:04:21 -0500480static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
481 struct tree_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500482{
Chris Masonbb803952007-03-01 12:04:21 -0500483 struct node *src = &src_buf->node;
484 struct node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500485 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500486 int src_nritems;
487 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500488 int ret = 0;
489 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500490
Chris Masonbe0e5c02007-01-26 15:51:26 -0500491 src_nritems = src->header.nritems;
Chris Masonbb803952007-03-01 12:04:21 -0500492 dst_nritems = dst->header.nritems;
493 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500494 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500496 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500497
498 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500499 push_items = src_nritems;
500
Chris Masonbb803952007-03-01 12:04:21 -0500501 memcpy(dst->keys + dst_nritems, src->keys,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500502 push_items * sizeof(struct key));
Chris Masonbb803952007-03-01 12:04:21 -0500503 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500504 push_items * sizeof(u64));
Chris Masonbb803952007-03-01 12:04:21 -0500505 if (push_items < src_nritems) {
506 memmove(src->keys, src->keys + push_items,
507 (src_nritems - push_items) * sizeof(struct key));
508 memmove(src->blockptrs, src->blockptrs + push_items,
509 (src_nritems - push_items) * sizeof(u64));
510 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500511 src->header.nritems -= push_items;
512 dst->header.nritems += push_items;
513
Chris Masoned2ff2c2007-03-01 18:59:40 -0500514 wret = dirty_tree_block(root, src_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500515 if (wret < 0)
516 ret = wret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500517
Chris Masoned2ff2c2007-03-01 18:59:40 -0500518 wret = dirty_tree_block(root, dst_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500519 if (wret < 0)
520 ret = wret;
521 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500522}
523
Chris Mason97571fd2007-02-24 13:39:08 -0500524/*
Chris Mason79f95c82007-03-01 15:16:26 -0500525 * try to push data from one node into the next node right in the
526 * tree.
527 *
528 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
529 * error, and > 0 if there was no room in the right hand block.
530 *
531 * this will only push up to 1/2 the contents of the left node over
532 */
533static int balance_node_right(struct ctree_root *root,
534 struct tree_buffer *dst_buf,
535 struct tree_buffer *src_buf)
536{
537 struct node *src = &src_buf->node;
538 struct node *dst = &dst_buf->node;
539 int push_items = 0;
540 int max_push;
541 int src_nritems;
542 int dst_nritems;
543 int ret = 0;
544 int wret;
545
546 src_nritems = src->header.nritems;
547 dst_nritems = dst->header.nritems;
548 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
549 if (push_items <= 0) {
550 return 1;
551 }
552
553 max_push = src_nritems / 2 + 1;
554 /* don't try to empty the node */
555 if (max_push > src_nritems)
556 return 1;
557 if (max_push < push_items)
558 push_items = max_push;
559
560 memmove(dst->keys + push_items, dst->keys,
561 dst_nritems * sizeof(struct key));
562 memmove(dst->blockptrs + push_items, dst->blockptrs,
563 dst_nritems * sizeof(u64));
564 memcpy(dst->keys, src->keys + src_nritems - push_items,
565 push_items * sizeof(struct key));
566 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
567 push_items * sizeof(u64));
568
569 src->header.nritems -= push_items;
570 dst->header.nritems += push_items;
571
Chris Masoned2ff2c2007-03-01 18:59:40 -0500572 wret = dirty_tree_block(root, src_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500573 if (wret < 0)
574 ret = wret;
575
Chris Masoned2ff2c2007-03-01 18:59:40 -0500576 wret = dirty_tree_block(root, dst_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500577 if (wret < 0)
578 ret = wret;
579 return ret;
580}
581
582/*
Chris Mason97571fd2007-02-24 13:39:08 -0500583 * helper function to insert a new root level in the tree.
584 * A new node is allocated, and a single item is inserted to
585 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500586 *
587 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500588 */
Chris Mason5de08d72007-02-24 06:24:44 -0500589static int insert_new_root(struct ctree_root *root,
590 struct ctree_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500591{
592 struct tree_buffer *t;
593 struct node *lower;
594 struct node *c;
595 struct key *lower_key;
596
597 BUG_ON(path->nodes[level]);
598 BUG_ON(path->nodes[level-1] != root->node);
599
600 t = alloc_free_block(root);
601 c = &t->node;
602 memset(c, 0, sizeof(c));
603 c->header.nritems = 1;
604 c->header.flags = node_level(level);
605 c->header.blocknr = t->blocknr;
606 c->header.parentid = root->node->node.header.parentid;
607 lower = &path->nodes[level-1]->node;
608 if (is_leaf(lower->header.flags))
609 lower_key = &((struct leaf *)lower)->items[0].key;
610 else
611 lower_key = lower->keys;
612 memcpy(c->keys, lower_key, sizeof(struct key));
613 c->blockptrs[0] = path->nodes[level-1]->blocknr;
614 /* the super has an extra ref to root->node */
615 tree_block_release(root, root->node);
616 root->node = t;
617 t->count++;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500618 dirty_tree_block(root, t);
Chris Mason5c680ed2007-02-22 11:39:13 -0500619 path->nodes[level] = t;
620 path->slots[level] = 0;
621 return 0;
622}
623
Chris Mason74123bd2007-02-02 11:05:29 -0500624/*
625 * worker function to insert a single pointer in a node.
626 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500627 *
Chris Mason74123bd2007-02-02 11:05:29 -0500628 * slot and level indicate where you want the key to go, and
629 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500630 *
631 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500632 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500633static int insert_ptr(struct ctree_root *root,
Chris Mason74123bd2007-02-02 11:05:29 -0500634 struct ctree_path *path, struct key *key,
635 u64 blocknr, int slot, int level)
636{
Chris Mason74123bd2007-02-02 11:05:29 -0500637 struct node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500638 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500639
640 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500641 lower = &path->nodes[level]->node;
642 nritems = lower->header.nritems;
643 if (slot > nritems)
644 BUG();
645 if (nritems == NODEPTRS_PER_BLOCK)
646 BUG();
647 if (slot != nritems) {
648 memmove(lower->keys + slot + 1, lower->keys + slot,
649 (nritems - slot) * sizeof(struct key));
650 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
651 (nritems - slot) * sizeof(u64));
652 }
653 memcpy(lower->keys + slot, key, sizeof(struct key));
654 lower->blockptrs[slot] = blocknr;
655 lower->header.nritems++;
656 if (lower->keys[1].objectid == 0)
657 BUG();
Chris Masoned2ff2c2007-03-01 18:59:40 -0500658 dirty_tree_block(root, path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500659 return 0;
660}
661
Chris Mason97571fd2007-02-24 13:39:08 -0500662/*
663 * split the node at the specified level in path in two.
664 * The path is corrected to point to the appropriate node after the split
665 *
666 * Before splitting this tries to make some room in the node by pushing
667 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500668 *
669 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500670 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500671static int split_node(struct ctree_root *root, struct ctree_path *path,
672 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500673{
Chris Mason5c680ed2007-02-22 11:39:13 -0500674 struct tree_buffer *t;
675 struct node *c;
676 struct tree_buffer *split_buffer;
677 struct node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500678 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500679 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500680 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500681
Chris Mason5c680ed2007-02-22 11:39:13 -0500682 t = path->nodes[level];
683 c = &t->node;
684 if (t == root->node) {
685 /* trying to split the root, lets make a new one */
686 ret = insert_new_root(root, path, level + 1);
687 if (ret)
688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500689 }
Chris Mason5c680ed2007-02-22 11:39:13 -0500690 split_buffer = alloc_free_block(root);
691 split = &split_buffer->node;
692 split->header.flags = c->header.flags;
693 split->header.blocknr = split_buffer->blocknr;
694 split->header.parentid = root->node->node.header.parentid;
695 mid = (c->header.nritems + 1) / 2;
696 memcpy(split->keys, c->keys + mid,
697 (c->header.nritems - mid) * sizeof(struct key));
698 memcpy(split->blockptrs, c->blockptrs + mid,
699 (c->header.nritems - mid) * sizeof(u64));
700 split->header.nritems = c->header.nritems - mid;
701 c->header.nritems = mid;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500702 ret = 0;
703
Chris Masoned2ff2c2007-03-01 18:59:40 -0500704 wret = dirty_tree_block(root, t);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500705 if (wret)
706 ret = wret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500707 wret = dirty_tree_block(root, split_buffer);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500708 if (wret)
709 ret = wret;
710 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
711 path->slots[level + 1] + 1, level + 1);
712 if (wret)
713 ret = wret;
714
Chris Mason5de08d72007-02-24 06:24:44 -0500715 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500716 path->slots[level] -= mid;
717 tree_block_release(root, t);
718 path->nodes[level] = split_buffer;
719 path->slots[level + 1] += 1;
720 } else {
721 tree_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500722 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500723 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500724}
725
Chris Mason74123bd2007-02-02 11:05:29 -0500726/*
727 * how many bytes are required to store the items in a leaf. start
728 * and nr indicate which items in the leaf to check. This totals up the
729 * space used both by the item structs and the item data
730 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500731static int leaf_space_used(struct leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500732{
733 int data_len;
734 int end = start + nr - 1;
735
736 if (!nr)
737 return 0;
738 data_len = l->items[start].offset + l->items[start].size;
739 data_len = data_len - l->items[end].offset;
740 data_len += sizeof(struct item) * nr;
741 return data_len;
742}
743
Chris Mason74123bd2007-02-02 11:05:29 -0500744/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500745 * push some data in the path leaf to the right, trying to free up at
746 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500747 *
748 * returns 1 if the push failed because the other node didn't have enough
749 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500750 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500751static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
752 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500753{
754 struct tree_buffer *left_buf = path->nodes[0];
755 struct leaf *left = &left_buf->leaf;
756 struct leaf *right;
757 struct tree_buffer *right_buf;
758 struct tree_buffer *upper;
759 int slot;
760 int i;
761 int free_space;
762 int push_space = 0;
763 int push_items = 0;
764 struct item *item;
765
766 slot = path->slots[1];
767 if (!path->nodes[1]) {
768 return 1;
769 }
770 upper = path->nodes[1];
771 if (slot >= upper->node.header.nritems - 1) {
772 return 1;
773 }
774 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
775 right = &right_buf->leaf;
776 free_space = leaf_free_space(right);
777 if (free_space < data_size + sizeof(struct item)) {
778 tree_block_release(root, right_buf);
779 return 1;
780 }
781 for (i = left->header.nritems - 1; i >= 0; i--) {
782 item = left->items + i;
783 if (path->slots[0] == i)
784 push_space += data_size + sizeof(*item);
785 if (item->size + sizeof(*item) + push_space > free_space)
786 break;
787 push_items++;
788 push_space += item->size + sizeof(*item);
789 }
790 if (push_items == 0) {
791 tree_block_release(root, right_buf);
792 return 1;
793 }
794 /* push left to right */
795 push_space = left->items[left->header.nritems - push_items].offset +
796 left->items[left->header.nritems - push_items].size;
797 push_space -= leaf_data_end(left);
798 /* make room in the right data area */
799 memmove(right->data + leaf_data_end(right) - push_space,
800 right->data + leaf_data_end(right),
801 LEAF_DATA_SIZE - leaf_data_end(right));
802 /* copy from the left data area */
803 memcpy(right->data + LEAF_DATA_SIZE - push_space,
804 left->data + leaf_data_end(left),
805 push_space);
806 memmove(right->items + push_items, right->items,
807 right->header.nritems * sizeof(struct item));
808 /* copy the items from left to right */
809 memcpy(right->items, left->items + left->header.nritems - push_items,
810 push_items * sizeof(struct item));
811
812 /* update the item pointers */
813 right->header.nritems += push_items;
814 push_space = LEAF_DATA_SIZE;
815 for (i = 0; i < right->header.nritems; i++) {
816 right->items[i].offset = push_space - right->items[i].size;
817 push_space = right->items[i].offset;
818 }
819 left->header.nritems -= push_items;
820
Chris Masoned2ff2c2007-03-01 18:59:40 -0500821 dirty_tree_block(root, left_buf);
822 dirty_tree_block(root, right_buf);
Chris Mason00ec4c52007-02-24 12:47:20 -0500823 memcpy(upper->node.keys + slot + 1,
824 &right->items[0].key, sizeof(struct key));
Chris Masoned2ff2c2007-03-01 18:59:40 -0500825 dirty_tree_block(root, upper);
Chris Mason00ec4c52007-02-24 12:47:20 -0500826 /* then fixup the leaf pointer in the path */
Chris Mason00ec4c52007-02-24 12:47:20 -0500827 if (path->slots[0] >= left->header.nritems) {
828 path->slots[0] -= left->header.nritems;
829 tree_block_release(root, path->nodes[0]);
830 path->nodes[0] = right_buf;
831 path->slots[1] += 1;
832 } else {
833 tree_block_release(root, right_buf);
834 }
835 return 0;
836}
837/*
Chris Mason74123bd2007-02-02 11:05:29 -0500838 * push some data in the path leaf to the left, trying to free up at
839 * least data_size bytes. returns zero if the push worked, nonzero otherwise
840 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500841static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
842 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500843{
Chris Masoneb60cea2007-02-02 09:18:22 -0500844 struct tree_buffer *right_buf = path->nodes[0];
845 struct leaf *right = &right_buf->leaf;
846 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500847 struct leaf *left;
848 int slot;
849 int i;
850 int free_space;
851 int push_space = 0;
852 int push_items = 0;
853 struct item *item;
854 int old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500855 int ret = 0;
856 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500857
858 slot = path->slots[1];
859 if (slot == 0) {
860 return 1;
861 }
862 if (!path->nodes[1]) {
863 return 1;
864 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500865 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
866 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500867 free_space = leaf_free_space(left);
868 if (free_space < data_size + sizeof(struct item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500869 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500870 return 1;
871 }
872 for (i = 0; i < right->header.nritems; i++) {
873 item = right->items + i;
874 if (path->slots[0] == i)
875 push_space += data_size + sizeof(*item);
876 if (item->size + sizeof(*item) + push_space > free_space)
877 break;
878 push_items++;
879 push_space += item->size + sizeof(*item);
880 }
881 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500882 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500883 return 1;
884 }
885 /* push data from right to left */
886 memcpy(left->items + left->header.nritems,
887 right->items, push_items * sizeof(struct item));
888 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
889 memcpy(left->data + leaf_data_end(left) - push_space,
890 right->data + right->items[push_items - 1].offset,
891 push_space);
892 old_left_nritems = left->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500893 BUG_ON(old_left_nritems < 0);
894
Chris Masonbe0e5c02007-01-26 15:51:26 -0500895 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
896 left->items[i].offset -= LEAF_DATA_SIZE -
897 left->items[old_left_nritems -1].offset;
898 }
899 left->header.nritems += push_items;
900
901 /* fixup right node */
902 push_space = right->items[push_items-1].offset - leaf_data_end(right);
903 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
904 leaf_data_end(right), push_space);
905 memmove(right->items, right->items + push_items,
906 (right->header.nritems - push_items) * sizeof(struct item));
907 right->header.nritems -= push_items;
908 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500909
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910 for (i = 0; i < right->header.nritems; i++) {
911 right->items[i].offset = push_space - right->items[i].size;
912 push_space = right->items[i].offset;
913 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500914
Chris Masoned2ff2c2007-03-01 18:59:40 -0500915 wret = dirty_tree_block(root, t);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500916 if (wret)
917 ret = wret;
Chris Masoned2ff2c2007-03-01 18:59:40 -0500918 wret = dirty_tree_block(root, right_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500919 if (wret)
920 ret = wret;
Chris Masoneb60cea2007-02-02 09:18:22 -0500921
Chris Masonaa5d6be2007-02-28 16:35:06 -0500922 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
923 if (wret)
924 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500925
926 /* then fixup the leaf pointer in the path */
927 if (path->slots[0] < push_items) {
928 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500929 tree_block_release(root, path->nodes[0]);
930 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500931 path->slots[1] -= 1;
932 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -0500933 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500934 path->slots[0] -= push_items;
935 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500936 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500937 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500938}
939
Chris Mason74123bd2007-02-02 11:05:29 -0500940/*
941 * split the path's leaf in two, making sure there is at least data_size
942 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500943 *
944 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -0500945 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500946static int split_leaf(struct ctree_root *root, struct ctree_path *path,
947 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948{
Chris Masonaa5d6be2007-02-28 16:35:06 -0500949 struct tree_buffer *l_buf;
950 struct leaf *l;
Chris Masoneb60cea2007-02-02 09:18:22 -0500951 int nritems;
952 int mid;
953 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500954 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -0500955 struct tree_buffer *right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500956 int space_needed = data_size + sizeof(struct item);
957 int data_copy_size;
958 int rt_data_off;
959 int i;
960 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500961 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500962
Chris Masonaa5d6be2007-02-28 16:35:06 -0500963 wret = push_leaf_left(root, path, data_size);
964 if (wret < 0)
965 return wret;
966 if (wret) {
967 wret = push_leaf_right(root, path, data_size);
968 if (wret < 0)
969 return wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500970 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500971 l_buf = path->nodes[0];
972 l = &l_buf->leaf;
973
974 /* did the pushes work? */
975 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
976 return 0;
977
Chris Mason5c680ed2007-02-22 11:39:13 -0500978 if (!path->nodes[1]) {
979 ret = insert_new_root(root, path, 1);
980 if (ret)
981 return ret;
982 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500983 slot = path->slots[0];
984 nritems = l->header.nritems;
985 mid = (nritems + 1)/ 2;
986
987 right_buffer = alloc_free_block(root);
988 BUG_ON(!right_buffer);
989 BUG_ON(mid == nritems);
990 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500991 memset(right, 0, sizeof(*right));
992 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -0500993 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500994 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
995 LEAF_DATA_SIZE)
996 BUG();
997 } else {
Chris Mason97571fd2007-02-24 13:39:08 -0500998 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500999 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1000 LEAF_DATA_SIZE)
1001 BUG();
1002 }
1003 right->header.nritems = nritems - mid;
Chris Masoneb60cea2007-02-02 09:18:22 -05001004 right->header.blocknr = right_buffer->blocknr;
1005 right->header.flags = node_level(0);
Chris Masoncfaa7292007-02-21 17:04:57 -05001006 right->header.parentid = root->node->node.header.parentid;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001007 data_copy_size = l->items[mid].offset + l->items[mid].size -
1008 leaf_data_end(l);
1009 memcpy(right->items, l->items + mid,
1010 (nritems - mid) * sizeof(struct item));
1011 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1012 l->data + leaf_data_end(l), data_copy_size);
1013 rt_data_off = LEAF_DATA_SIZE -
1014 (l->items[mid].offset + l->items[mid].size);
Chris Mason74123bd2007-02-02 11:05:29 -05001015
1016 for (i = 0; i < right->header.nritems; i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001017 right->items[i].offset += rt_data_off;
Chris Mason74123bd2007-02-02 11:05:29 -05001018
Chris Masonbe0e5c02007-01-26 15:51:26 -05001019 l->header.nritems = mid;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001020 ret = 0;
1021 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001022 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001023 if (wret)
1024 ret = wret;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001025 wret = dirty_tree_block(root, right_buffer);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001026 if (wret)
1027 ret = wret;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001028 wret = dirty_tree_block(root, l_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001029 if (wret)
1030 ret = wret;
Chris Masoneb60cea2007-02-02 09:18:22 -05001031
1032 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001033 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001034 tree_block_release(root, path->nodes[0]);
1035 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001036 path->slots[0] -= mid;
1037 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001038 } else
1039 tree_block_release(root, right_buffer);
1040 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001041 return ret;
1042}
1043
Chris Mason74123bd2007-02-02 11:05:29 -05001044/*
1045 * Given a key and some data, insert an item into the tree.
1046 * This does all the path init required, making room in the tree if needed.
1047 */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001048int insert_item(struct ctree_root *root, struct key *key,
1049 void *data, int data_size)
1050{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001051 int ret = 0;
1052 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001053 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001054 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001055 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001056 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001057 unsigned int nritems;
1058 unsigned int data_end;
1059 struct ctree_path path;
1060
Chris Mason74123bd2007-02-02 11:05:29 -05001061 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001062 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001063 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -05001064 init_path(&path);
Chris Mason5c680ed2007-02-22 11:39:13 -05001065 ret = search_slot(root, key, &path, data_size);
Chris Masoneb60cea2007-02-02 09:18:22 -05001066 if (ret == 0) {
1067 release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001068 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001069 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001070 if (ret < 0)
1071 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001072
Chris Masoneb60cea2007-02-02 09:18:22 -05001073 slot_orig = path.slots[0];
1074 leaf_buf = path.nodes[0];
1075 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001076
Chris Masonbe0e5c02007-01-26 15:51:26 -05001077 nritems = leaf->header.nritems;
1078 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001079
Chris Masonbe0e5c02007-01-26 15:51:26 -05001080 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
1081 BUG();
1082
1083 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001084 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001085 if (slot != nritems) {
1086 int i;
1087 unsigned int old_data = leaf->items[slot].offset +
1088 leaf->items[slot].size;
1089
1090 /*
1091 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1092 */
1093 /* first correct the data pointers */
1094 for (i = slot; i < nritems; i++)
1095 leaf->items[i].offset -= data_size;
1096
1097 /* shift the items */
1098 memmove(leaf->items + slot + 1, leaf->items + slot,
1099 (nritems - slot) * sizeof(struct item));
1100
1101 /* shift the data */
1102 memmove(leaf->data + data_end - data_size, leaf->data +
1103 data_end, old_data - data_end);
1104 data_end = old_data;
1105 }
Chris Mason74123bd2007-02-02 11:05:29 -05001106 /* copy the new data in */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001107 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
1108 leaf->items[slot].offset = data_end - data_size;
1109 leaf->items[slot].size = data_size;
1110 memcpy(leaf->data + data_end - data_size, data, data_size);
1111 leaf->header.nritems += 1;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001112
1113 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001114 if (slot == 0)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001115 ret = fixup_low_keys(root, &path, key, 1);
1116
Chris Masoned2ff2c2007-03-01 18:59:40 -05001117 wret = dirty_tree_block(root, leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001118 if (wret)
1119 ret = wret;
1120
Chris Masonbe0e5c02007-01-26 15:51:26 -05001121 if (leaf_free_space(leaf) < 0)
1122 BUG();
Chris Masonbb803952007-03-01 12:04:21 -05001123 check_leaf(&path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001124out:
Chris Masoneb60cea2007-02-02 09:18:22 -05001125 release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001126 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001127}
1128
Chris Mason74123bd2007-02-02 11:05:29 -05001129/*
Chris Mason5de08d72007-02-24 06:24:44 -05001130 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001131 *
1132 * If the delete empties a node, the node is removed from the tree,
1133 * continuing all the way the root if required. The root is converted into
1134 * a leaf if all the nodes are emptied.
1135 */
Chris Masonbb803952007-03-01 12:04:21 -05001136static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
1137 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001138{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001139 struct node *node;
Chris Masonbb803952007-03-01 12:04:21 -05001140 struct tree_buffer *parent = path->nodes[level];
Chris Masonbe0e5c02007-01-26 15:51:26 -05001141 int nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001142 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001143 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001144
Chris Masonbb803952007-03-01 12:04:21 -05001145 node = &parent->node;
1146 nritems = node->header.nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001147
Chris Masonbb803952007-03-01 12:04:21 -05001148 if (slot != nritems -1) {
1149 memmove(node->keys + slot, node->keys + slot + 1,
1150 sizeof(struct key) * (nritems - slot - 1));
1151 memmove(node->blockptrs + slot,
1152 node->blockptrs + slot + 1,
1153 sizeof(u64) * (nritems - slot - 1));
1154 }
1155 node->header.nritems--;
1156 if (node->header.nritems == 0 && parent == root->node) {
1157 BUG_ON(node_level(root->node->node.header.flags) != 1);
1158 /* just turn the root into a leaf and break */
1159 root->node->node.header.flags = node_level(0);
1160 } else if (slot == 0) {
1161 wret = fixup_low_keys(root, path, node->keys, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001162 if (wret)
1163 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001164 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001165 wret = dirty_tree_block(root, parent);
Chris Masonbb803952007-03-01 12:04:21 -05001166 if (wret)
1167 ret = wret;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001168 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001169}
1170
Chris Mason74123bd2007-02-02 11:05:29 -05001171/*
1172 * delete the item at the leaf level in path. If that empties
1173 * the leaf, remove it from the tree
1174 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001175int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001176{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001177 int slot;
1178 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001179 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001180 int doff;
1181 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001182 int ret = 0;
1183 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001184
Chris Masoneb60cea2007-02-02 09:18:22 -05001185 leaf_buf = path->nodes[0];
1186 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001187 slot = path->slots[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05001188 doff = leaf->items[slot].offset;
1189 dsize = leaf->items[slot].size;
1190
1191 if (slot != leaf->header.nritems - 1) {
1192 int i;
1193 int data_end = leaf_data_end(leaf);
1194 memmove(leaf->data + data_end + dsize,
1195 leaf->data + data_end,
1196 doff - data_end);
1197 for (i = slot + 1; i < leaf->header.nritems; i++)
1198 leaf->items[i].offset += dsize;
1199 memmove(leaf->items + slot, leaf->items + slot + 1,
1200 sizeof(struct item) *
1201 (leaf->header.nritems - slot - 1));
1202 }
1203 leaf->header.nritems -= 1;
Chris Mason74123bd2007-02-02 11:05:29 -05001204 /* delete the leaf if we've emptied it */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001205 if (leaf->header.nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001206 if (leaf_buf == root->node) {
1207 leaf->header.flags = node_level(0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001208 dirty_tree_block(root, leaf_buf);
Chris Mason9a8dd152007-02-23 08:38:36 -05001209 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001210 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001211 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001212 if (wret)
1213 ret = wret;
Chris Mason0f70abe2007-02-28 16:46:22 -05001214 wret = free_extent(root, leaf_buf->blocknr, 1);
1215 if (wret)
1216 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001217 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001218 } else {
Chris Mason5de08d72007-02-24 06:24:44 -05001219 int used = leaf_space_used(leaf, 0, leaf->header.nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001220 if (slot == 0) {
1221 wret = fixup_low_keys(root, path,
1222 &leaf->items[0].key, 1);
1223 if (wret)
1224 ret = wret;
1225 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001226 wret = dirty_tree_block(root, leaf_buf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001227 if (wret)
1228 ret = wret;
1229
Chris Mason74123bd2007-02-02 11:05:29 -05001230 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -05001231 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001232 /* push_leaf_left fixes the path.
1233 * make sure the path still points to our leaf
1234 * for possible call to del_ptr below
1235 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001236 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001237 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001238 wret = push_leaf_left(root, path, 1);
1239 if (wret < 0)
1240 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001241 if (path->nodes[0] == leaf_buf &&
1242 leaf->header.nritems) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001243 wret = push_leaf_right(root, path, 1);
1244 if (wret < 0)
1245 ret = wret;
1246 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001247 if (leaf->header.nritems == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001248 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001249 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001250 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001251 if (wret)
1252 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001253 tree_block_release(root, leaf_buf);
Chris Mason0f70abe2007-02-28 16:46:22 -05001254 wret = free_extent(root, blocknr, 1);
1255 if (wret)
1256 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001257 } else {
1258 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 }
1260 }
1261 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001262 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001263}
1264
Chris Mason97571fd2007-02-24 13:39:08 -05001265/*
1266 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001267 * returns 0 if it found something or 1 if there are no greater leaves.
1268 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001269 */
Chris Masond97e63b2007-02-20 16:40:44 -05001270int next_leaf(struct ctree_root *root, struct ctree_path *path)
1271{
1272 int slot;
1273 int level = 1;
1274 u64 blocknr;
1275 struct tree_buffer *c;
Chris Masoncfaa7292007-02-21 17:04:57 -05001276 struct tree_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001277
1278 while(level < MAX_LEVEL) {
1279 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001280 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001281 slot = path->slots[level] + 1;
1282 c = path->nodes[level];
1283 if (slot >= c->node.header.nritems) {
1284 level++;
1285 continue;
1286 }
1287 blocknr = c->node.blockptrs[slot];
Chris Masoncfaa7292007-02-21 17:04:57 -05001288 if (next)
1289 tree_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001290 next = read_tree_block(root, blocknr);
1291 break;
1292 }
1293 path->slots[level] = slot;
1294 while(1) {
1295 level--;
1296 c = path->nodes[level];
1297 tree_block_release(root, c);
1298 path->nodes[level] = next;
1299 path->slots[level] = 0;
1300 if (!level)
1301 break;
1302 next = read_tree_block(root, next->node.blockptrs[0]);
1303 }
1304 return 0;
1305}
1306