blob: 1118986d7569f07720d71b456a6a410c1e4b693d [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 Mason02217ed2007-03-02 16:08:05 -050037int btrfs_cow_block(struct ctree_root *root,
38 struct tree_buffer *buf,
39 struct tree_buffer *parent,
40 int parent_slot,
41 struct tree_buffer **cow_ret)
42{
43 struct tree_buffer *cow;
44
45 if (!list_empty(&buf->dirty)) {
46 *cow_ret = buf;
47 return 0;
48 }
49 cow = alloc_free_block(root);
50 memcpy(&cow->node, &buf->node, sizeof(buf->node));
51 cow->node.header.blocknr = cow->blocknr;
52 *cow_ret = cow;
53 if (buf == root->node) {
54 root->node = cow;
55 cow->count++;
56 tree_block_release(root, buf);
57 } else {
58 parent->node.blockptrs[parent_slot] = cow->blocknr;
59 BUG_ON(list_empty(&parent->dirty));
60 }
61 if (0 && root != root->extent_root && !is_leaf(cow->node.header.flags)) {
62 btrfs_inc_ref(root, cow);
63 }
64 tree_block_release(root, buf);
65 return 0;
66}
67
Chris Mason74123bd2007-02-02 11:05:29 -050068/*
69 * The leaf data grows from end-to-front in the node.
70 * this returns the address of the start of the last item,
71 * which is the stop of the leaf data stack
72 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050073static inline unsigned int leaf_data_end(struct leaf *leaf)
74{
75 unsigned int nr = leaf->header.nritems;
76 if (nr == 0)
Chris Masond97e63b2007-02-20 16:40:44 -050077 return sizeof(leaf->data);
Chris Masonbe0e5c02007-01-26 15:51:26 -050078 return leaf->items[nr-1].offset;
79}
80
Chris Mason74123bd2007-02-02 11:05:29 -050081/*
82 * The space between the end of the leaf items and
83 * the start of the leaf data. IOW, how much room
84 * the leaf has left for both items and data
85 */
Chris Mason5de08d72007-02-24 06:24:44 -050086int leaf_free_space(struct leaf *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -050087{
88 int data_end = leaf_data_end(leaf);
89 int nritems = leaf->header.nritems;
90 char *items_end = (char *)(leaf->items + nritems + 1);
91 return (char *)(leaf->data + data_end) - (char *)items_end;
92}
93
Chris Mason74123bd2007-02-02 11:05:29 -050094/*
95 * compare two keys in a memcmp fashion
96 */
Chris Masonbe0e5c02007-01-26 15:51:26 -050097int comp_keys(struct key *k1, struct key *k2)
98{
99 if (k1->objectid > k2->objectid)
100 return 1;
101 if (k1->objectid < k2->objectid)
102 return -1;
103 if (k1->flags > k2->flags)
104 return 1;
105 if (k1->flags < k2->flags)
106 return -1;
107 if (k1->offset > k2->offset)
108 return 1;
109 if (k1->offset < k2->offset)
110 return -1;
111 return 0;
112}
Chris Mason74123bd2007-02-02 11:05:29 -0500113
Chris Masonaa5d6be2007-02-28 16:35:06 -0500114int check_node(struct ctree_path *path, int level)
115{
116 int i;
117 struct node *parent = NULL;
118 struct node *node = &path->nodes[level]->node;
119 int parent_slot;
120
121 if (path->nodes[level + 1])
122 parent = &path->nodes[level + 1]->node;
123 parent_slot = path->slots[level + 1];
124 if (parent && node->header.nritems > 0) {
125 struct key *parent_key;
126 parent_key = &parent->keys[parent_slot];
127 BUG_ON(memcmp(parent_key, node->keys, sizeof(struct key)));
128 BUG_ON(parent->blockptrs[parent_slot] != node->header.blocknr);
129 }
130 BUG_ON(node->header.nritems > NODEPTRS_PER_BLOCK);
131 for (i = 0; i < node->header.nritems - 2; i++) {
132 BUG_ON(comp_keys(&node->keys[i], &node->keys[i+1]) >= 0);
133 }
134 return 0;
135}
136
137int check_leaf(struct ctree_path *path, int level)
138{
139 int i;
140 struct leaf *leaf = &path->nodes[level]->leaf;
141 struct node *parent = NULL;
142 int parent_slot;
143
144 if (path->nodes[level + 1])
145 parent = &path->nodes[level + 1]->node;
146 parent_slot = path->slots[level + 1];
147 if (parent && leaf->header.nritems > 0) {
148 struct key *parent_key;
149 parent_key = &parent->keys[parent_slot];
150 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
151 sizeof(struct key)));
152 BUG_ON(parent->blockptrs[parent_slot] != leaf->header.blocknr);
153 }
154 for (i = 0; i < leaf->header.nritems - 2; i++) {
155 BUG_ON(comp_keys(&leaf->items[i].key,
156 &leaf->items[i+1].key) >= 0);
157 BUG_ON(leaf->items[i].offset != leaf->items[i + 1].offset +
158 leaf->items[i + 1].size);
159 if (i == 0) {
160 BUG_ON(leaf->items[i].offset + leaf->items[i].size !=
161 LEAF_DATA_SIZE);
162 }
163 }
164 BUG_ON(leaf_free_space(leaf) < 0);
165 return 0;
166}
167
168int check_block(struct ctree_path *path, int level)
169{
170 if (level == 0)
171 return check_leaf(path, level);
172 return check_node(path, level);
173}
174
Chris Mason74123bd2007-02-02 11:05:29 -0500175/*
176 * search for key in the array p. items p are item_size apart
177 * and there are 'max' items in p
178 * the slot in the array is returned via slot, and it points to
179 * the place where you would insert key if it is not found in
180 * the array.
181 *
182 * slot may point to max if the key is bigger than all of the keys
183 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500184int generic_bin_search(char *p, int item_size, struct key *key,
185 int max, int *slot)
186{
187 int low = 0;
188 int high = max;
189 int mid;
190 int ret;
191 struct key *tmp;
192
193 while(low < high) {
194 mid = (low + high) / 2;
195 tmp = (struct key *)(p + mid * item_size);
196 ret = comp_keys(tmp, key);
197
198 if (ret < 0)
199 low = mid + 1;
200 else if (ret > 0)
201 high = mid;
202 else {
203 *slot = mid;
204 return 0;
205 }
206 }
207 *slot = low;
208 return 1;
209}
210
Chris Mason97571fd2007-02-24 13:39:08 -0500211/*
212 * simple bin_search frontend that does the right thing for
213 * leaves vs nodes
214 */
Chris Masonbe0e5c02007-01-26 15:51:26 -0500215int bin_search(struct node *c, struct key *key, int *slot)
216{
217 if (is_leaf(c->header.flags)) {
218 struct leaf *l = (struct leaf *)c;
219 return generic_bin_search((void *)l->items, sizeof(struct item),
220 key, c->header.nritems, slot);
221 } else {
222 return generic_bin_search((void *)c->keys, sizeof(struct key),
223 key, c->header.nritems, slot);
224 }
225 return -1;
226}
227
Chris Masonbb803952007-03-01 12:04:21 -0500228struct tree_buffer *read_node_slot(struct ctree_root *root,
229 struct tree_buffer *parent_buf,
230 int slot)
231{
232 struct node *node = &parent_buf->node;
233 if (slot < 0)
234 return NULL;
235 if (slot >= node->header.nritems)
236 return NULL;
237 return read_tree_block(root, node->blockptrs[slot]);
238}
239
240static int balance_level(struct ctree_root *root, struct ctree_path *path,
241 int level)
242{
243 struct tree_buffer *right_buf;
244 struct tree_buffer *mid_buf;
245 struct tree_buffer *left_buf;
246 struct tree_buffer *parent_buf = NULL;
247 struct node *right = NULL;
248 struct node *mid;
249 struct node *left = NULL;
250 struct node *parent = NULL;
251 int ret = 0;
252 int wret;
253 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500254 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500255 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500256
257 if (level == 0)
258 return 0;
259
260 mid_buf = path->nodes[level];
261 mid = &mid_buf->node;
Chris Mason79f95c82007-03-01 15:16:26 -0500262 orig_ptr = mid->blockptrs[orig_slot];
263
Chris Masonbb803952007-03-01 12:04:21 -0500264 if (level < MAX_LEVEL - 1)
265 parent_buf = path->nodes[level + 1];
266 pslot = path->slots[level + 1];
267
268 if (!parent_buf) {
269 struct tree_buffer *child;
270 u64 blocknr = mid_buf->blocknr;
271
272 if (mid->header.nritems != 1)
273 return 0;
274
275 /* promote the child to a root */
276 child = read_node_slot(root, mid_buf, 0);
277 BUG_ON(!child);
278 root->node = child;
279 path->nodes[level] = NULL;
280 /* once for the path */
281 tree_block_release(root, mid_buf);
282 /* once for the root ptr */
283 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500284 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500285 return free_extent(root, blocknr, 1);
286 }
287 parent = &parent_buf->node;
288
289 if (mid->header.nritems > NODEPTRS_PER_BLOCK / 4)
290 return 0;
291
Chris Masonbb803952007-03-01 12:04:21 -0500292 left_buf = read_node_slot(root, parent_buf, pslot - 1);
293 right_buf = read_node_slot(root, parent_buf, pslot + 1);
Chris Mason79f95c82007-03-01 15:16:26 -0500294
295 /* first, try to make some room in the middle buffer */
Chris Masonbb803952007-03-01 12:04:21 -0500296 if (left_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500297 btrfs_cow_block(root, left_buf, parent_buf,
298 pslot - 1, &left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500299 left = &left_buf->node;
Chris Masonbb803952007-03-01 12:04:21 -0500300 orig_slot += left->header.nritems;
Chris Mason79f95c82007-03-01 15:16:26 -0500301 wret = push_node_left(root, left_buf, mid_buf);
302 if (wret < 0)
303 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500304 }
Chris Mason79f95c82007-03-01 15:16:26 -0500305
306 /*
307 * then try to empty the right most buffer into the middle
308 */
Chris Masonbb803952007-03-01 12:04:21 -0500309 if (right_buf) {
Chris Mason02217ed2007-03-02 16:08:05 -0500310 btrfs_cow_block(root, right_buf, parent_buf,
311 pslot + 1, &right_buf);
Chris Mason79f95c82007-03-01 15:16:26 -0500312 right = &right_buf->node;
313 wret = push_node_left(root, mid_buf, right_buf);
314 if (wret < 0)
315 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -0500316 if (right->header.nritems == 0) {
317 u64 blocknr = right_buf->blocknr;
318 tree_block_release(root, right_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500319 clean_tree_block(root, right_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500320 right_buf = NULL;
321 right = NULL;
322 wret = del_ptr(root, path, level + 1, pslot + 1);
323 if (wret)
324 ret = wret;
325 wret = free_extent(root, blocknr, 1);
326 if (wret)
327 ret = wret;
328 } else {
329 memcpy(parent->keys + pslot + 1, right->keys,
330 sizeof(struct key));
Chris Mason02217ed2007-03-02 16:08:05 -0500331 BUG_ON(list_empty(&parent_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500332 }
333 }
Chris Mason79f95c82007-03-01 15:16:26 -0500334 if (mid->header.nritems == 1) {
335 /*
336 * we're not allowed to leave a node with one item in the
337 * tree during a delete. A deletion from lower in the tree
338 * could try to delete the only pointer in this node.
339 * So, pull some keys from the left.
340 * There has to be a left pointer at this point because
341 * otherwise we would have pulled some pointers from the
342 * right
343 */
344 BUG_ON(!left_buf);
345 wret = balance_node_right(root, mid_buf, left_buf);
346 if (wret < 0)
347 ret = wret;
348 BUG_ON(wret == 1);
349 }
Chris Masonbb803952007-03-01 12:04:21 -0500350 if (mid->header.nritems == 0) {
Chris Mason79f95c82007-03-01 15:16:26 -0500351 /* we've managed to empty the middle node, drop it */
Chris Masonbb803952007-03-01 12:04:21 -0500352 u64 blocknr = mid_buf->blocknr;
353 tree_block_release(root, mid_buf);
Chris Masoned2ff2c2007-03-01 18:59:40 -0500354 clean_tree_block(root, mid_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500355 mid_buf = NULL;
356 mid = NULL;
357 wret = del_ptr(root, path, level + 1, pslot);
358 if (wret)
359 ret = wret;
360 wret = free_extent(root, blocknr, 1);
361 if (wret)
362 ret = wret;
Chris Mason79f95c82007-03-01 15:16:26 -0500363 } else {
364 /* update the parent key to reflect our changes */
Chris Masonbb803952007-03-01 12:04:21 -0500365 memcpy(parent->keys + pslot, mid->keys, sizeof(struct key));
Chris Mason02217ed2007-03-02 16:08:05 -0500366 BUG_ON(list_empty(&parent_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500367 }
Chris Masonbb803952007-03-01 12:04:21 -0500368
Chris Mason79f95c82007-03-01 15:16:26 -0500369 /* update the path */
Chris Masonbb803952007-03-01 12:04:21 -0500370 if (left_buf) {
Chris Mason79f95c82007-03-01 15:16:26 -0500371 if (left->header.nritems > orig_slot) {
Chris Masonbb803952007-03-01 12:04:21 -0500372 left_buf->count++; // released below
373 path->nodes[level] = left_buf;
374 path->slots[level + 1] -= 1;
375 path->slots[level] = orig_slot;
376 if (mid_buf)
377 tree_block_release(root, mid_buf);
378 } else {
379 orig_slot -= left->header.nritems;
380 path->slots[level] = orig_slot;
381 }
382 }
Chris Mason79f95c82007-03-01 15:16:26 -0500383 /* double check we haven't messed things up */
384 check_block(path, level);
385 if (orig_ptr != path->nodes[level]->node.blockptrs[path->slots[level]])
386 BUG();
Chris Masonbb803952007-03-01 12:04:21 -0500387
388 if (right_buf)
389 tree_block_release(root, right_buf);
390 if (left_buf)
391 tree_block_release(root, left_buf);
Chris Masonbb803952007-03-01 12:04:21 -0500392 return ret;
393}
394
Chris Mason74123bd2007-02-02 11:05:29 -0500395/*
396 * look for key in the tree. path is filled in with nodes along the way
397 * if key is found, we return zero and you can find the item in the leaf
398 * level of the path (level 0)
399 *
400 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -0500401 * be inserted, and 1 is returned. If there are other errors during the
402 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -0500403 *
404 * if ins_len > 0, nodes and leaves will be split as we walk down the
405 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
406 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -0500407 */
Chris Mason5de08d72007-02-24 06:24:44 -0500408int search_slot(struct ctree_root *root, struct key *key,
Chris Mason02217ed2007-03-02 16:08:05 -0500409 struct ctree_path *p, int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500410{
Chris Masonbb803952007-03-01 12:04:21 -0500411 struct tree_buffer *b;
Chris Mason02217ed2007-03-02 16:08:05 -0500412 struct tree_buffer *cow_buf;
Chris Masoneb60cea2007-02-02 09:18:22 -0500413 struct node *c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500414 int slot;
415 int ret;
416 int level;
Chris Mason5c680ed2007-02-22 11:39:13 -0500417
Chris Masonbb803952007-03-01 12:04:21 -0500418again:
419 b = root->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500420 b->count++;
421 while (b) {
Chris Mason02217ed2007-03-02 16:08:05 -0500422 level = node_level(b->node.header.flags);
423 if (cow) {
424 int wret;
425 wret = btrfs_cow_block(root, b, p->nodes[level + 1],
426 p->slots[level + 1], &cow_buf);
427 b = cow_buf;
428 }
429 BUG_ON(!cow && ins_len);
Chris Masoneb60cea2007-02-02 09:18:22 -0500430 c = &b->node;
Chris Masoneb60cea2007-02-02 09:18:22 -0500431 p->nodes[level] = b;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500432 ret = check_block(p, level);
433 if (ret)
434 return -1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500435 ret = bin_search(c, key, &slot);
436 if (!is_leaf(c->header.flags)) {
437 if (ret && slot > 0)
438 slot -= 1;
439 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500440 if (ins_len > 0 &&
441 c->header.nritems == NODEPTRS_PER_BLOCK) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500442 int sret = split_node(root, p, level);
443 BUG_ON(sret > 0);
444 if (sret)
445 return sret;
446 b = p->nodes[level];
447 c = &b->node;
448 slot = p->slots[level];
Chris Masonbb803952007-03-01 12:04:21 -0500449 } else if (ins_len < 0) {
450 int sret = balance_level(root, p, level);
451 if (sret)
452 return sret;
453 b = p->nodes[level];
454 if (!b)
455 goto again;
456 c = &b->node;
457 slot = p->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500458 BUG_ON(c->header.nritems == 1);
Chris Mason5c680ed2007-02-22 11:39:13 -0500459 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500460 b = read_tree_block(root, c->blockptrs[slot]);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500461 } else {
Chris Mason5c680ed2007-02-22 11:39:13 -0500462 struct leaf *l = (struct leaf *)c;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500463 p->slots[level] = slot;
Chris Mason5de08d72007-02-24 06:24:44 -0500464 if (ins_len > 0 && leaf_free_space(l) <
465 sizeof(struct item) + ins_len) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500466 int sret = split_leaf(root, p, ins_len);
467 BUG_ON(sret > 0);
468 if (sret)
469 return sret;
470 }
Chris Masonbb803952007-03-01 12:04:21 -0500471 BUG_ON(root->node->count == 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500472 return ret;
473 }
474 }
Chris Masonbb803952007-03-01 12:04:21 -0500475 BUG_ON(root->node->count == 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500476 return 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500477}
478
Chris Mason74123bd2007-02-02 11:05:29 -0500479/*
480 * adjust the pointers going up the tree, starting at level
481 * making sure the right key of each node is points to 'key'.
482 * This is used after shifting pointers to the left, so it stops
483 * fixing up pointers when a given leaf/node is not in slot 0 of the
484 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -0500485 *
486 * If this fails to write a tree block, it returns -1, but continues
487 * fixing up the blocks in ram so the tree is consistent.
Chris Mason74123bd2007-02-02 11:05:29 -0500488 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500489static int fixup_low_keys(struct ctree_root *root,
Chris Masoneb60cea2007-02-02 09:18:22 -0500490 struct ctree_path *path, struct key *key,
491 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500492{
493 int i;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500494 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500495 for (i = level; i < MAX_LEVEL; i++) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500496 struct node *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500497 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -0500498 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -0500499 break;
Chris Masoneb60cea2007-02-02 09:18:22 -0500500 t = &path->nodes[i]->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500501 memcpy(t->keys + tslot, key, sizeof(*key));
Chris Mason02217ed2007-03-02 16:08:05 -0500502 BUG_ON(list_empty(&path->nodes[i]->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -0500503 if (tslot != 0)
504 break;
505 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500506 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500507}
508
Chris Mason74123bd2007-02-02 11:05:29 -0500509/*
510 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -0500511 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500512 *
513 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
514 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -0500515 */
Chris Masonbb803952007-03-01 12:04:21 -0500516static int push_node_left(struct ctree_root *root, struct tree_buffer *dst_buf,
517 struct tree_buffer *src_buf)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500518{
Chris Masonbb803952007-03-01 12:04:21 -0500519 struct node *src = &src_buf->node;
520 struct node *dst = &dst_buf->node;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500521 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500522 int src_nritems;
523 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500524 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500525
Chris Masonbe0e5c02007-01-26 15:51:26 -0500526 src_nritems = src->header.nritems;
Chris Masonbb803952007-03-01 12:04:21 -0500527 dst_nritems = dst->header.nritems;
528 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500529 if (push_items <= 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -0500530 return 1;
Chris Masoneb60cea2007-02-02 09:18:22 -0500531 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500532
533 if (src_nritems < push_items)
Chris Mason79f95c82007-03-01 15:16:26 -0500534 push_items = src_nritems;
535
Chris Masonbb803952007-03-01 12:04:21 -0500536 memcpy(dst->keys + dst_nritems, src->keys,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500537 push_items * sizeof(struct key));
Chris Masonbb803952007-03-01 12:04:21 -0500538 memcpy(dst->blockptrs + dst_nritems, src->blockptrs,
Chris Masonbe0e5c02007-01-26 15:51:26 -0500539 push_items * sizeof(u64));
Chris Masonbb803952007-03-01 12:04:21 -0500540 if (push_items < src_nritems) {
541 memmove(src->keys, src->keys + push_items,
542 (src_nritems - push_items) * sizeof(struct key));
543 memmove(src->blockptrs, src->blockptrs + push_items,
544 (src_nritems - push_items) * sizeof(u64));
545 }
Chris Masonbe0e5c02007-01-26 15:51:26 -0500546 src->header.nritems -= push_items;
547 dst->header.nritems += push_items;
548
Chris Mason02217ed2007-03-02 16:08:05 -0500549 BUG_ON(list_empty(&src_buf->dirty));
550 BUG_ON(list_empty(&dst_buf->dirty));
Chris Masonbb803952007-03-01 12:04:21 -0500551 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500552}
553
Chris Mason97571fd2007-02-24 13:39:08 -0500554/*
Chris Mason79f95c82007-03-01 15:16:26 -0500555 * try to push data from one node into the next node right in the
556 * tree.
557 *
558 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
559 * error, and > 0 if there was no room in the right hand block.
560 *
561 * this will only push up to 1/2 the contents of the left node over
562 */
563static int balance_node_right(struct ctree_root *root,
564 struct tree_buffer *dst_buf,
565 struct tree_buffer *src_buf)
566{
567 struct node *src = &src_buf->node;
568 struct node *dst = &dst_buf->node;
569 int push_items = 0;
570 int max_push;
571 int src_nritems;
572 int dst_nritems;
573 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -0500574
575 src_nritems = src->header.nritems;
576 dst_nritems = dst->header.nritems;
577 push_items = NODEPTRS_PER_BLOCK - dst_nritems;
578 if (push_items <= 0) {
579 return 1;
580 }
581
582 max_push = src_nritems / 2 + 1;
583 /* don't try to empty the node */
584 if (max_push > src_nritems)
585 return 1;
586 if (max_push < push_items)
587 push_items = max_push;
588
589 memmove(dst->keys + push_items, dst->keys,
590 dst_nritems * sizeof(struct key));
591 memmove(dst->blockptrs + push_items, dst->blockptrs,
592 dst_nritems * sizeof(u64));
593 memcpy(dst->keys, src->keys + src_nritems - push_items,
594 push_items * sizeof(struct key));
595 memcpy(dst->blockptrs, src->blockptrs + src_nritems - push_items,
596 push_items * sizeof(u64));
597
598 src->header.nritems -= push_items;
599 dst->header.nritems += push_items;
600
Chris Mason02217ed2007-03-02 16:08:05 -0500601 BUG_ON(list_empty(&src_buf->dirty));
602 BUG_ON(list_empty(&dst_buf->dirty));
Chris Mason79f95c82007-03-01 15:16:26 -0500603 return ret;
604}
605
606/*
Chris Mason97571fd2007-02-24 13:39:08 -0500607 * helper function to insert a new root level in the tree.
608 * A new node is allocated, and a single item is inserted to
609 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -0500610 *
611 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -0500612 */
Chris Mason5de08d72007-02-24 06:24:44 -0500613static int insert_new_root(struct ctree_root *root,
614 struct ctree_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -0500615{
616 struct tree_buffer *t;
617 struct node *lower;
618 struct node *c;
619 struct key *lower_key;
620
621 BUG_ON(path->nodes[level]);
622 BUG_ON(path->nodes[level-1] != root->node);
623
624 t = alloc_free_block(root);
625 c = &t->node;
626 memset(c, 0, sizeof(c));
627 c->header.nritems = 1;
628 c->header.flags = node_level(level);
629 c->header.blocknr = t->blocknr;
630 c->header.parentid = root->node->node.header.parentid;
631 lower = &path->nodes[level-1]->node;
632 if (is_leaf(lower->header.flags))
633 lower_key = &((struct leaf *)lower)->items[0].key;
634 else
635 lower_key = lower->keys;
636 memcpy(c->keys, lower_key, sizeof(struct key));
637 c->blockptrs[0] = path->nodes[level-1]->blocknr;
638 /* the super has an extra ref to root->node */
639 tree_block_release(root, root->node);
640 root->node = t;
641 t->count++;
Chris Mason5c680ed2007-02-22 11:39:13 -0500642 path->nodes[level] = t;
643 path->slots[level] = 0;
644 return 0;
645}
646
Chris Mason74123bd2007-02-02 11:05:29 -0500647/*
648 * worker function to insert a single pointer in a node.
649 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -0500650 *
Chris Mason74123bd2007-02-02 11:05:29 -0500651 * slot and level indicate where you want the key to go, and
652 * blocknr is the block the key points to.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500653 *
654 * returns zero on success and < 0 on any error
Chris Mason74123bd2007-02-02 11:05:29 -0500655 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500656static int insert_ptr(struct ctree_root *root,
Chris Mason74123bd2007-02-02 11:05:29 -0500657 struct ctree_path *path, struct key *key,
658 u64 blocknr, int slot, int level)
659{
Chris Mason74123bd2007-02-02 11:05:29 -0500660 struct node *lower;
Chris Mason74123bd2007-02-02 11:05:29 -0500661 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -0500662
663 BUG_ON(!path->nodes[level]);
Chris Mason74123bd2007-02-02 11:05:29 -0500664 lower = &path->nodes[level]->node;
665 nritems = lower->header.nritems;
666 if (slot > nritems)
667 BUG();
668 if (nritems == NODEPTRS_PER_BLOCK)
669 BUG();
670 if (slot != nritems) {
671 memmove(lower->keys + slot + 1, lower->keys + slot,
672 (nritems - slot) * sizeof(struct key));
673 memmove(lower->blockptrs + slot + 1, lower->blockptrs + slot,
674 (nritems - slot) * sizeof(u64));
675 }
676 memcpy(lower->keys + slot, key, sizeof(struct key));
677 lower->blockptrs[slot] = blocknr;
678 lower->header.nritems++;
679 if (lower->keys[1].objectid == 0)
680 BUG();
Chris Mason02217ed2007-03-02 16:08:05 -0500681 BUG_ON(list_empty(&path->nodes[level]->dirty));
Chris Mason74123bd2007-02-02 11:05:29 -0500682 return 0;
683}
684
Chris Mason97571fd2007-02-24 13:39:08 -0500685/*
686 * split the node at the specified level in path in two.
687 * The path is corrected to point to the appropriate node after the split
688 *
689 * Before splitting this tries to make some room in the node by pushing
690 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500691 *
692 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -0500693 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500694static int split_node(struct ctree_root *root, struct ctree_path *path,
695 int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500696{
Chris Mason5c680ed2007-02-22 11:39:13 -0500697 struct tree_buffer *t;
698 struct node *c;
699 struct tree_buffer *split_buffer;
700 struct node *split;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500701 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -0500702 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500703 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500704
Chris Mason5c680ed2007-02-22 11:39:13 -0500705 t = path->nodes[level];
706 c = &t->node;
707 if (t == root->node) {
708 /* trying to split the root, lets make a new one */
709 ret = insert_new_root(root, path, level + 1);
710 if (ret)
711 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500712 }
Chris Mason5c680ed2007-02-22 11:39:13 -0500713 split_buffer = alloc_free_block(root);
714 split = &split_buffer->node;
715 split->header.flags = c->header.flags;
716 split->header.blocknr = split_buffer->blocknr;
717 split->header.parentid = root->node->node.header.parentid;
718 mid = (c->header.nritems + 1) / 2;
719 memcpy(split->keys, c->keys + mid,
720 (c->header.nritems - mid) * sizeof(struct key));
721 memcpy(split->blockptrs, c->blockptrs + mid,
722 (c->header.nritems - mid) * sizeof(u64));
723 split->header.nritems = c->header.nritems - mid;
724 c->header.nritems = mid;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500725 ret = 0;
726
Chris Mason02217ed2007-03-02 16:08:05 -0500727 BUG_ON(list_empty(&t->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -0500728 wret = insert_ptr(root, path, split->keys, split_buffer->blocknr,
729 path->slots[level + 1] + 1, level + 1);
730 if (wret)
731 ret = wret;
732
Chris Mason5de08d72007-02-24 06:24:44 -0500733 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -0500734 path->slots[level] -= mid;
735 tree_block_release(root, t);
736 path->nodes[level] = split_buffer;
737 path->slots[level + 1] += 1;
738 } else {
739 tree_block_release(root, split_buffer);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500740 }
Chris Masonaa5d6be2007-02-28 16:35:06 -0500741 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500742}
743
Chris Mason74123bd2007-02-02 11:05:29 -0500744/*
745 * how many bytes are required to store the items in a leaf. start
746 * and nr indicate which items in the leaf to check. This totals up the
747 * space used both by the item structs and the item data
748 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500749static int leaf_space_used(struct leaf *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500750{
751 int data_len;
752 int end = start + nr - 1;
753
754 if (!nr)
755 return 0;
756 data_len = l->items[start].offset + l->items[start].size;
757 data_len = data_len - l->items[end].offset;
758 data_len += sizeof(struct item) * nr;
759 return data_len;
760}
761
Chris Mason74123bd2007-02-02 11:05:29 -0500762/*
Chris Mason00ec4c52007-02-24 12:47:20 -0500763 * push some data in the path leaf to the right, trying to free up at
764 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Masonaa5d6be2007-02-28 16:35:06 -0500765 *
766 * returns 1 if the push failed because the other node didn't have enough
767 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason00ec4c52007-02-24 12:47:20 -0500768 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500769static int push_leaf_right(struct ctree_root *root, struct ctree_path *path,
770 int data_size)
Chris Mason00ec4c52007-02-24 12:47:20 -0500771{
772 struct tree_buffer *left_buf = path->nodes[0];
773 struct leaf *left = &left_buf->leaf;
774 struct leaf *right;
775 struct tree_buffer *right_buf;
776 struct tree_buffer *upper;
777 int slot;
778 int i;
779 int free_space;
780 int push_space = 0;
781 int push_items = 0;
782 struct item *item;
783
784 slot = path->slots[1];
785 if (!path->nodes[1]) {
786 return 1;
787 }
788 upper = path->nodes[1];
789 if (slot >= upper->node.header.nritems - 1) {
790 return 1;
791 }
792 right_buf = read_tree_block(root, upper->node.blockptrs[slot + 1]);
793 right = &right_buf->leaf;
794 free_space = leaf_free_space(right);
795 if (free_space < data_size + sizeof(struct item)) {
796 tree_block_release(root, right_buf);
797 return 1;
798 }
Chris Mason02217ed2007-03-02 16:08:05 -0500799 /* cow and double check */
800 btrfs_cow_block(root, right_buf, upper, slot + 1, &right_buf);
801 right = &right_buf->leaf;
802 free_space = leaf_free_space(right);
803 if (free_space < data_size + sizeof(struct item)) {
804 tree_block_release(root, right_buf);
805 return 1;
806 }
807
Chris Mason00ec4c52007-02-24 12:47:20 -0500808 for (i = left->header.nritems - 1; i >= 0; i--) {
809 item = left->items + i;
810 if (path->slots[0] == i)
811 push_space += data_size + sizeof(*item);
812 if (item->size + sizeof(*item) + push_space > free_space)
813 break;
814 push_items++;
815 push_space += item->size + sizeof(*item);
816 }
817 if (push_items == 0) {
818 tree_block_release(root, right_buf);
819 return 1;
820 }
821 /* push left to right */
822 push_space = left->items[left->header.nritems - push_items].offset +
823 left->items[left->header.nritems - push_items].size;
824 push_space -= leaf_data_end(left);
825 /* make room in the right data area */
826 memmove(right->data + leaf_data_end(right) - push_space,
827 right->data + leaf_data_end(right),
828 LEAF_DATA_SIZE - leaf_data_end(right));
829 /* copy from the left data area */
830 memcpy(right->data + LEAF_DATA_SIZE - push_space,
831 left->data + leaf_data_end(left),
832 push_space);
833 memmove(right->items + push_items, right->items,
834 right->header.nritems * sizeof(struct item));
835 /* copy the items from left to right */
836 memcpy(right->items, left->items + left->header.nritems - push_items,
837 push_items * sizeof(struct item));
838
839 /* update the item pointers */
840 right->header.nritems += push_items;
841 push_space = LEAF_DATA_SIZE;
842 for (i = 0; i < right->header.nritems; i++) {
843 right->items[i].offset = push_space - right->items[i].size;
844 push_space = right->items[i].offset;
845 }
846 left->header.nritems -= push_items;
847
Chris Mason02217ed2007-03-02 16:08:05 -0500848 BUG_ON(list_empty(&left_buf->dirty));
849 BUG_ON(list_empty(&right_buf->dirty));
Chris Mason00ec4c52007-02-24 12:47:20 -0500850 memcpy(upper->node.keys + slot + 1,
851 &right->items[0].key, sizeof(struct key));
Chris Mason02217ed2007-03-02 16:08:05 -0500852 BUG_ON(list_empty(&upper->dirty));
853
Chris Mason00ec4c52007-02-24 12:47:20 -0500854 /* then fixup the leaf pointer in the path */
Chris Mason00ec4c52007-02-24 12:47:20 -0500855 if (path->slots[0] >= left->header.nritems) {
856 path->slots[0] -= left->header.nritems;
857 tree_block_release(root, path->nodes[0]);
858 path->nodes[0] = right_buf;
859 path->slots[1] += 1;
860 } else {
861 tree_block_release(root, right_buf);
862 }
863 return 0;
864}
865/*
Chris Mason74123bd2007-02-02 11:05:29 -0500866 * push some data in the path leaf to the left, trying to free up at
867 * least data_size bytes. returns zero if the push worked, nonzero otherwise
868 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500869static int push_leaf_left(struct ctree_root *root, struct ctree_path *path,
870 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500871{
Chris Masoneb60cea2007-02-02 09:18:22 -0500872 struct tree_buffer *right_buf = path->nodes[0];
873 struct leaf *right = &right_buf->leaf;
874 struct tree_buffer *t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500875 struct leaf *left;
876 int slot;
877 int i;
878 int free_space;
879 int push_space = 0;
880 int push_items = 0;
881 struct item *item;
882 int old_left_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500883 int ret = 0;
884 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500885
886 slot = path->slots[1];
887 if (slot == 0) {
888 return 1;
889 }
890 if (!path->nodes[1]) {
891 return 1;
892 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500893 t = read_tree_block(root, path->nodes[1]->node.blockptrs[slot - 1]);
894 left = &t->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500895 free_space = leaf_free_space(left);
896 if (free_space < data_size + sizeof(struct item)) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500897 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500898 return 1;
899 }
Chris Mason02217ed2007-03-02 16:08:05 -0500900
901 /* cow and double check */
902 btrfs_cow_block(root, t, path->nodes[1], slot - 1, &t);
903 left = &t->leaf;
904 free_space = leaf_free_space(left);
905 if (free_space < data_size + sizeof(struct item)) {
906 tree_block_release(root, t);
907 return 1;
908 }
909
Chris Masonbe0e5c02007-01-26 15:51:26 -0500910 for (i = 0; i < right->header.nritems; i++) {
911 item = right->items + i;
912 if (path->slots[0] == i)
913 push_space += data_size + sizeof(*item);
914 if (item->size + sizeof(*item) + push_space > free_space)
915 break;
916 push_items++;
917 push_space += item->size + sizeof(*item);
918 }
919 if (push_items == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -0500920 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500921 return 1;
922 }
923 /* push data from right to left */
924 memcpy(left->items + left->header.nritems,
925 right->items, push_items * sizeof(struct item));
926 push_space = LEAF_DATA_SIZE - right->items[push_items -1].offset;
927 memcpy(left->data + leaf_data_end(left) - push_space,
928 right->data + right->items[push_items - 1].offset,
929 push_space);
930 old_left_nritems = left->header.nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500931 BUG_ON(old_left_nritems < 0);
932
Chris Masonbe0e5c02007-01-26 15:51:26 -0500933 for(i = old_left_nritems; i < old_left_nritems + push_items; i++) {
934 left->items[i].offset -= LEAF_DATA_SIZE -
935 left->items[old_left_nritems -1].offset;
936 }
937 left->header.nritems += push_items;
938
939 /* fixup right node */
940 push_space = right->items[push_items-1].offset - leaf_data_end(right);
941 memmove(right->data + LEAF_DATA_SIZE - push_space, right->data +
942 leaf_data_end(right), push_space);
943 memmove(right->items, right->items + push_items,
944 (right->header.nritems - push_items) * sizeof(struct item));
945 right->header.nritems -= push_items;
946 push_space = LEAF_DATA_SIZE;
Chris Masoneb60cea2007-02-02 09:18:22 -0500947
Chris Masonbe0e5c02007-01-26 15:51:26 -0500948 for (i = 0; i < right->header.nritems; i++) {
949 right->items[i].offset = push_space - right->items[i].size;
950 push_space = right->items[i].offset;
951 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500952
Chris Mason02217ed2007-03-02 16:08:05 -0500953 BUG_ON(list_empty(&t->dirty));
954 BUG_ON(list_empty(&right_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -0500955
Chris Masonaa5d6be2007-02-28 16:35:06 -0500956 wret = fixup_low_keys(root, path, &right->items[0].key, 1);
957 if (wret)
958 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500959
960 /* then fixup the leaf pointer in the path */
961 if (path->slots[0] < push_items) {
962 path->slots[0] += old_left_nritems;
Chris Masoneb60cea2007-02-02 09:18:22 -0500963 tree_block_release(root, path->nodes[0]);
964 path->nodes[0] = t;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500965 path->slots[1] -= 1;
966 } else {
Chris Masoneb60cea2007-02-02 09:18:22 -0500967 tree_block_release(root, t);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500968 path->slots[0] -= push_items;
969 }
Chris Masoneb60cea2007-02-02 09:18:22 -0500970 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -0500971 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500972}
973
Chris Mason74123bd2007-02-02 11:05:29 -0500974/*
975 * split the path's leaf in two, making sure there is at least data_size
976 * available for the resulting leaf level of the path.
Chris Masonaa5d6be2007-02-28 16:35:06 -0500977 *
978 * returns 0 if all went well and < 0 on failure.
Chris Mason74123bd2007-02-02 11:05:29 -0500979 */
Chris Masonaa5d6be2007-02-28 16:35:06 -0500980static int split_leaf(struct ctree_root *root, struct ctree_path *path,
981 int data_size)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500982{
Chris Masonaa5d6be2007-02-28 16:35:06 -0500983 struct tree_buffer *l_buf;
984 struct leaf *l;
Chris Masoneb60cea2007-02-02 09:18:22 -0500985 int nritems;
986 int mid;
987 int slot;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500988 struct leaf *right;
Chris Masoneb60cea2007-02-02 09:18:22 -0500989 struct tree_buffer *right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500990 int space_needed = data_size + sizeof(struct item);
991 int data_copy_size;
992 int rt_data_off;
993 int i;
994 int ret;
Chris Masonaa5d6be2007-02-28 16:35:06 -0500995 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500996
Chris Masonaa5d6be2007-02-28 16:35:06 -0500997 wret = push_leaf_left(root, path, data_size);
998 if (wret < 0)
999 return wret;
1000 if (wret) {
1001 wret = push_leaf_right(root, path, data_size);
1002 if (wret < 0)
1003 return wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001004 }
Chris Mason02217ed2007-03-02 16:08:05 -05001005
Chris Masonaa5d6be2007-02-28 16:35:06 -05001006 l_buf = path->nodes[0];
1007 l = &l_buf->leaf;
1008
1009 /* did the pushes work? */
1010 if (leaf_free_space(l) >= sizeof(struct item) + data_size)
1011 return 0;
1012
Chris Mason5c680ed2007-02-22 11:39:13 -05001013 if (!path->nodes[1]) {
1014 ret = insert_new_root(root, path, 1);
1015 if (ret)
1016 return ret;
1017 }
Chris Masoneb60cea2007-02-02 09:18:22 -05001018 slot = path->slots[0];
1019 nritems = l->header.nritems;
1020 mid = (nritems + 1)/ 2;
1021
1022 right_buffer = alloc_free_block(root);
1023 BUG_ON(!right_buffer);
1024 BUG_ON(mid == nritems);
1025 right = &right_buffer->leaf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001026 memset(right, 0, sizeof(*right));
1027 if (mid <= slot) {
Chris Mason97571fd2007-02-24 13:39:08 -05001028 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001029 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
1030 LEAF_DATA_SIZE)
1031 BUG();
1032 } else {
Chris Mason97571fd2007-02-24 13:39:08 -05001033 /* FIXME, just alloc a new leaf here */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001034 if (leaf_space_used(l, 0, mid + 1) + space_needed >
1035 LEAF_DATA_SIZE)
1036 BUG();
1037 }
1038 right->header.nritems = nritems - mid;
Chris Masoneb60cea2007-02-02 09:18:22 -05001039 right->header.blocknr = right_buffer->blocknr;
1040 right->header.flags = node_level(0);
Chris Masoncfaa7292007-02-21 17:04:57 -05001041 right->header.parentid = root->node->node.header.parentid;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001042 data_copy_size = l->items[mid].offset + l->items[mid].size -
1043 leaf_data_end(l);
1044 memcpy(right->items, l->items + mid,
1045 (nritems - mid) * sizeof(struct item));
1046 memcpy(right->data + LEAF_DATA_SIZE - data_copy_size,
1047 l->data + leaf_data_end(l), data_copy_size);
1048 rt_data_off = LEAF_DATA_SIZE -
1049 (l->items[mid].offset + l->items[mid].size);
Chris Mason74123bd2007-02-02 11:05:29 -05001050
1051 for (i = 0; i < right->header.nritems; i++)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001052 right->items[i].offset += rt_data_off;
Chris Mason74123bd2007-02-02 11:05:29 -05001053
Chris Masonbe0e5c02007-01-26 15:51:26 -05001054 l->header.nritems = mid;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001055 ret = 0;
1056 wret = insert_ptr(root, path, &right->items[0].key,
Chris Mason5c680ed2007-02-22 11:39:13 -05001057 right_buffer->blocknr, path->slots[1] + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001058 if (wret)
1059 ret = wret;
Chris Mason02217ed2007-03-02 16:08:05 -05001060 BUG_ON(list_empty(&right_buffer->dirty));
1061 BUG_ON(list_empty(&l_buf->dirty));
Chris Masoneb60cea2007-02-02 09:18:22 -05001062 BUG_ON(path->slots[0] != slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001063 if (mid <= slot) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001064 tree_block_release(root, path->nodes[0]);
1065 path->nodes[0] = right_buffer;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001066 path->slots[0] -= mid;
1067 path->slots[1] += 1;
Chris Masoneb60cea2007-02-02 09:18:22 -05001068 } else
1069 tree_block_release(root, right_buffer);
1070 BUG_ON(path->slots[0] < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001071 return ret;
1072}
1073
Chris Mason74123bd2007-02-02 11:05:29 -05001074/*
1075 * Given a key and some data, insert an item into the tree.
1076 * This does all the path init required, making room in the tree if needed.
1077 */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001078int insert_item(struct ctree_root *root, struct key *key,
1079 void *data, int data_size)
1080{
Chris Masonaa5d6be2007-02-28 16:35:06 -05001081 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001082 int slot;
Chris Masoneb60cea2007-02-02 09:18:22 -05001083 int slot_orig;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001084 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001085 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001086 unsigned int nritems;
1087 unsigned int data_end;
1088 struct ctree_path path;
1089
Chris Mason74123bd2007-02-02 11:05:29 -05001090 /* create a root if there isn't one */
Chris Mason5c680ed2007-02-22 11:39:13 -05001091 if (!root->node)
Chris Masoncfaa7292007-02-21 17:04:57 -05001092 BUG();
Chris Masonbe0e5c02007-01-26 15:51:26 -05001093 init_path(&path);
Chris Mason02217ed2007-03-02 16:08:05 -05001094 ret = search_slot(root, key, &path, data_size, 1);
Chris Masoneb60cea2007-02-02 09:18:22 -05001095 if (ret == 0) {
1096 release_path(root, &path);
Chris Masonf0930a32007-03-02 09:47:58 -05001097 return -EEXIST;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001098 }
Chris Masoned2ff2c2007-03-01 18:59:40 -05001099 if (ret < 0)
1100 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001101
Chris Masoneb60cea2007-02-02 09:18:22 -05001102 slot_orig = path.slots[0];
1103 leaf_buf = path.nodes[0];
1104 leaf = &leaf_buf->leaf;
Chris Mason74123bd2007-02-02 11:05:29 -05001105
Chris Masonbe0e5c02007-01-26 15:51:26 -05001106 nritems = leaf->header.nritems;
1107 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05001108
Chris Masonbe0e5c02007-01-26 15:51:26 -05001109 if (leaf_free_space(leaf) < sizeof(struct item) + data_size)
1110 BUG();
1111
1112 slot = path.slots[0];
Chris Masoneb60cea2007-02-02 09:18:22 -05001113 BUG_ON(slot < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001114 if (slot != nritems) {
1115 int i;
1116 unsigned int old_data = leaf->items[slot].offset +
1117 leaf->items[slot].size;
1118
1119 /*
1120 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1121 */
1122 /* first correct the data pointers */
1123 for (i = slot; i < nritems; i++)
1124 leaf->items[i].offset -= data_size;
1125
1126 /* shift the items */
1127 memmove(leaf->items + slot + 1, leaf->items + slot,
1128 (nritems - slot) * sizeof(struct item));
1129
1130 /* shift the data */
1131 memmove(leaf->data + data_end - data_size, leaf->data +
1132 data_end, old_data - data_end);
1133 data_end = old_data;
1134 }
Chris Mason74123bd2007-02-02 11:05:29 -05001135 /* copy the new data in */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001136 memcpy(&leaf->items[slot].key, key, sizeof(struct key));
1137 leaf->items[slot].offset = data_end - data_size;
1138 leaf->items[slot].size = data_size;
1139 memcpy(leaf->data + data_end - data_size, data, data_size);
1140 leaf->header.nritems += 1;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001141
1142 ret = 0;
Chris Mason8e19f2c2007-02-28 09:27:02 -05001143 if (slot == 0)
Chris Masonaa5d6be2007-02-28 16:35:06 -05001144 ret = fixup_low_keys(root, &path, key, 1);
1145
Chris Mason02217ed2007-03-02 16:08:05 -05001146 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonbe0e5c02007-01-26 15:51:26 -05001147 if (leaf_free_space(leaf) < 0)
1148 BUG();
Chris Masonbb803952007-03-01 12:04:21 -05001149 check_leaf(&path, 0);
Chris Masoned2ff2c2007-03-01 18:59:40 -05001150out:
Chris Masoneb60cea2007-02-02 09:18:22 -05001151 release_path(root, &path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001152 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001153}
1154
Chris Mason74123bd2007-02-02 11:05:29 -05001155/*
Chris Mason5de08d72007-02-24 06:24:44 -05001156 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05001157 *
1158 * If the delete empties a node, the node is removed from the tree,
1159 * continuing all the way the root if required. The root is converted into
1160 * a leaf if all the nodes are emptied.
1161 */
Chris Masonbb803952007-03-01 12:04:21 -05001162static int del_ptr(struct ctree_root *root, struct ctree_path *path, int level,
1163 int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001164{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001165 struct node *node;
Chris Masonbb803952007-03-01 12:04:21 -05001166 struct tree_buffer *parent = path->nodes[level];
Chris Masonbe0e5c02007-01-26 15:51:26 -05001167 int nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001168 int ret = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001169 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001170
Chris Masonbb803952007-03-01 12:04:21 -05001171 node = &parent->node;
1172 nritems = node->header.nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001173
Chris Masonbb803952007-03-01 12:04:21 -05001174 if (slot != nritems -1) {
1175 memmove(node->keys + slot, node->keys + slot + 1,
1176 sizeof(struct key) * (nritems - slot - 1));
1177 memmove(node->blockptrs + slot,
1178 node->blockptrs + slot + 1,
1179 sizeof(u64) * (nritems - slot - 1));
1180 }
1181 node->header.nritems--;
1182 if (node->header.nritems == 0 && parent == root->node) {
1183 BUG_ON(node_level(root->node->node.header.flags) != 1);
1184 /* just turn the root into a leaf and break */
1185 root->node->node.header.flags = node_level(0);
1186 } else if (slot == 0) {
1187 wret = fixup_low_keys(root, path, node->keys, level + 1);
Chris Mason0f70abe2007-02-28 16:46:22 -05001188 if (wret)
1189 ret = wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001190 }
Chris Mason02217ed2007-03-02 16:08:05 -05001191 BUG_ON(list_empty(&parent->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001192 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001193}
1194
Chris Mason74123bd2007-02-02 11:05:29 -05001195/*
1196 * delete the item at the leaf level in path. If that empties
1197 * the leaf, remove it from the tree
1198 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001199int del_item(struct ctree_root *root, struct ctree_path *path)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001200{
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201 int slot;
1202 struct leaf *leaf;
Chris Masoneb60cea2007-02-02 09:18:22 -05001203 struct tree_buffer *leaf_buf;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001204 int doff;
1205 int dsize;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001206 int ret = 0;
1207 int wret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001208
Chris Masoneb60cea2007-02-02 09:18:22 -05001209 leaf_buf = path->nodes[0];
1210 leaf = &leaf_buf->leaf;
Chris Mason4920c9a2007-01-26 16:38:42 -05001211 slot = path->slots[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05001212 doff = leaf->items[slot].offset;
1213 dsize = leaf->items[slot].size;
1214
1215 if (slot != leaf->header.nritems - 1) {
1216 int i;
1217 int data_end = leaf_data_end(leaf);
1218 memmove(leaf->data + data_end + dsize,
1219 leaf->data + data_end,
1220 doff - data_end);
1221 for (i = slot + 1; i < leaf->header.nritems; i++)
1222 leaf->items[i].offset += dsize;
1223 memmove(leaf->items + slot, leaf->items + slot + 1,
1224 sizeof(struct item) *
1225 (leaf->header.nritems - slot - 1));
1226 }
1227 leaf->header.nritems -= 1;
Chris Mason74123bd2007-02-02 11:05:29 -05001228 /* delete the leaf if we've emptied it */
Chris Masonbe0e5c02007-01-26 15:51:26 -05001229 if (leaf->header.nritems == 0) {
Chris Masoneb60cea2007-02-02 09:18:22 -05001230 if (leaf_buf == root->node) {
1231 leaf->header.flags = node_level(0);
Chris Mason02217ed2007-03-02 16:08:05 -05001232 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Mason9a8dd152007-02-23 08:38:36 -05001233 } else {
Chris Masoned2ff2c2007-03-01 18:59:40 -05001234 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001235 wret = del_ptr(root, path, 1, path->slots[1]);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001236 if (wret)
1237 ret = wret;
Chris Mason0f70abe2007-02-28 16:46:22 -05001238 wret = free_extent(root, leaf_buf->blocknr, 1);
1239 if (wret)
1240 ret = wret;
Chris Mason9a8dd152007-02-23 08:38:36 -05001241 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001242 } else {
Chris Mason5de08d72007-02-24 06:24:44 -05001243 int used = leaf_space_used(leaf, 0, leaf->header.nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001244 if (slot == 0) {
1245 wret = fixup_low_keys(root, path,
1246 &leaf->items[0].key, 1);
1247 if (wret)
1248 ret = wret;
1249 }
Chris Mason02217ed2007-03-02 16:08:05 -05001250 BUG_ON(list_empty(&leaf_buf->dirty));
Chris Masonaa5d6be2007-02-28 16:35:06 -05001251
Chris Mason74123bd2007-02-02 11:05:29 -05001252 /* delete the leaf if it is mostly empty */
Chris Mason5de08d72007-02-24 06:24:44 -05001253 if (used < LEAF_DATA_SIZE / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001254 /* push_leaf_left fixes the path.
1255 * make sure the path still points to our leaf
1256 * for possible call to del_ptr below
1257 */
Chris Mason4920c9a2007-01-26 16:38:42 -05001258 slot = path->slots[1];
Chris Masoneb60cea2007-02-02 09:18:22 -05001259 leaf_buf->count++;
Chris Masonaa5d6be2007-02-28 16:35:06 -05001260 wret = push_leaf_left(root, path, 1);
1261 if (wret < 0)
1262 ret = wret;
Chris Masonf0930a32007-03-02 09:47:58 -05001263 if (path->nodes[0] == leaf_buf &&
1264 leaf->header.nritems) {
Chris Masonaa5d6be2007-02-28 16:35:06 -05001265 wret = push_leaf_right(root, path, 1);
1266 if (wret < 0)
1267 ret = wret;
1268 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001269 if (leaf->header.nritems == 0) {
Chris Mason5de08d72007-02-24 06:24:44 -05001270 u64 blocknr = leaf_buf->blocknr;
Chris Masoned2ff2c2007-03-01 18:59:40 -05001271 clean_tree_block(root, leaf_buf);
Chris Masonbb803952007-03-01 12:04:21 -05001272 wret = del_ptr(root, path, 1, slot);
Chris Masonaa5d6be2007-02-28 16:35:06 -05001273 if (wret)
1274 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001275 tree_block_release(root, leaf_buf);
Chris Mason0f70abe2007-02-28 16:46:22 -05001276 wret = free_extent(root, blocknr, 1);
1277 if (wret)
1278 ret = wret;
Chris Mason5de08d72007-02-24 06:24:44 -05001279 } else {
1280 tree_block_release(root, leaf_buf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001281 }
1282 }
1283 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05001284 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001285}
1286
Chris Mason97571fd2007-02-24 13:39:08 -05001287/*
1288 * walk up the tree as far as required to find the next leaf.
Chris Mason0f70abe2007-02-28 16:46:22 -05001289 * returns 0 if it found something or 1 if there are no greater leaves.
1290 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05001291 */
Chris Masond97e63b2007-02-20 16:40:44 -05001292int next_leaf(struct ctree_root *root, struct ctree_path *path)
1293{
1294 int slot;
1295 int level = 1;
1296 u64 blocknr;
1297 struct tree_buffer *c;
Chris Masoncfaa7292007-02-21 17:04:57 -05001298 struct tree_buffer *next = NULL;
Chris Masond97e63b2007-02-20 16:40:44 -05001299
1300 while(level < MAX_LEVEL) {
1301 if (!path->nodes[level])
Chris Mason0f70abe2007-02-28 16:46:22 -05001302 return 1;
Chris Masond97e63b2007-02-20 16:40:44 -05001303 slot = path->slots[level] + 1;
1304 c = path->nodes[level];
1305 if (slot >= c->node.header.nritems) {
1306 level++;
1307 continue;
1308 }
1309 blocknr = c->node.blockptrs[slot];
Chris Masoncfaa7292007-02-21 17:04:57 -05001310 if (next)
1311 tree_block_release(root, next);
Chris Masond97e63b2007-02-20 16:40:44 -05001312 next = read_tree_block(root, blocknr);
1313 break;
1314 }
1315 path->slots[level] = slot;
1316 while(1) {
1317 level--;
1318 c = path->nodes[level];
1319 tree_block_release(root, c);
1320 path->nodes[level] = next;
1321 path->slots[level] = 0;
1322 if (!level)
1323 break;
1324 next = read_tree_block(root, next->node.blockptrs[0]);
1325 }
1326 return 0;
1327}
1328
Chris Mason02217ed2007-03-02 16:08:05 -05001329