]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/btrfs/ctree.c
Btrfs: verify csums on read
[net-next-2.6.git] / fs / btrfs / ctree.c
CommitLineData
2e635a27 1#include <linux/module.h>
eb60ceac
CM
2#include "ctree.h"
3#include "disk-io.h"
7f5c1516 4#include "transaction.h"
9a8dd150 5
e089f05c
CM
6static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
7 *root, struct btrfs_path *path, int level);
8static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
9 *root, struct btrfs_path *path, int data_size);
10static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6 11 *root, struct buffer_head *dst, struct buffer_head
e089f05c
CM
12 *src);
13static int balance_node_right(struct btrfs_trans_handle *trans, struct
e20d96d6
CM
14 btrfs_root *root, struct buffer_head *dst_buf,
15 struct buffer_head *src_buf);
e089f05c
CM
16static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
17 struct btrfs_path *path, int level, int slot);
d97e63b6 18
234b63a0 19inline void btrfs_init_path(struct btrfs_path *p)
be0e5c09
CM
20{
21 memset(p, 0, sizeof(*p));
22}
23
234b63a0 24void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
eb60ceac
CM
25{
26 int i;
234b63a0 27 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
eb60ceac
CM
28 if (!p->nodes[i])
29 break;
234b63a0 30 btrfs_block_release(root, p->nodes[i]);
eb60ceac 31 }
aa5d6bed 32 memset(p, 0, sizeof(*p));
eb60ceac
CM
33}
34
e089f05c 35static int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6
CM
36 *root, struct buffer_head *buf, struct buffer_head
37 *parent, int parent_slot, struct buffer_head
e089f05c 38 **cow_ret)
02217ed2 39{
e20d96d6
CM
40 struct buffer_head *cow;
41 struct btrfs_node *cow_node;
02217ed2 42
7f5c1516
CM
43 if (btrfs_header_generation(btrfs_buffer_header(buf)) ==
44 trans->transid) {
02217ed2
CM
45 *cow_ret = buf;
46 return 0;
47 }
e089f05c 48 cow = btrfs_alloc_free_block(trans, root);
e20d96d6
CM
49 cow_node = btrfs_buffer_node(cow);
50 memcpy(cow_node, btrfs_buffer_node(buf), root->blocksize);
51 btrfs_set_header_blocknr(&cow_node->header, cow->b_blocknr);
7f5c1516 52 btrfs_set_header_generation(&cow_node->header, trans->transid);
02217ed2 53 *cow_ret = cow;
d5719762 54 mark_buffer_dirty(cow);
e089f05c 55 btrfs_inc_ref(trans, root, buf);
02217ed2
CM
56 if (buf == root->node) {
57 root->node = cow;
e20d96d6 58 get_bh(cow);
a28ec197 59 if (buf != root->commit_root)
e20d96d6 60 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
234b63a0 61 btrfs_block_release(root, buf);
02217ed2 62 } else {
e20d96d6
CM
63 btrfs_set_node_blockptr(btrfs_buffer_node(parent), parent_slot,
64 cow->b_blocknr);
d5719762 65 mark_buffer_dirty(parent);
e20d96d6 66 btrfs_free_extent(trans, root, buf->b_blocknr, 1, 1);
02217ed2 67 }
234b63a0 68 btrfs_block_release(root, buf);
02217ed2
CM
69 return 0;
70}
71
74123bd7
CM
72/*
73 * The leaf data grows from end-to-front in the node.
74 * this returns the address of the start of the last item,
75 * which is the stop of the leaf data stack
76 */
123abc88
CM
77static inline unsigned int leaf_data_end(struct btrfs_root *root,
78 struct btrfs_leaf *leaf)
be0e5c09 79{
7518a238 80 u32 nr = btrfs_header_nritems(&leaf->header);
be0e5c09 81 if (nr == 0)
123abc88 82 return BTRFS_LEAF_DATA_SIZE(root);
0783fcfc 83 return btrfs_item_offset(leaf->items + nr - 1);
be0e5c09
CM
84}
85
74123bd7
CM
86/*
87 * The space between the end of the leaf items and
88 * the start of the leaf data. IOW, how much room
89 * the leaf has left for both items and data
90 */
123abc88 91int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf)
be0e5c09 92{
123abc88 93 int data_end = leaf_data_end(root, leaf);
7518a238 94 int nritems = btrfs_header_nritems(&leaf->header);
be0e5c09 95 char *items_end = (char *)(leaf->items + nritems + 1);
123abc88 96 return (char *)(btrfs_leaf_data(leaf) + data_end) - (char *)items_end;
be0e5c09
CM
97}
98
74123bd7
CM
99/*
100 * compare two keys in a memcmp fashion
101 */
9aca1d51 102static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
be0e5c09 103{
e2fa7227
CM
104 struct btrfs_key k1;
105
106 btrfs_disk_key_to_cpu(&k1, disk);
107
108 if (k1.objectid > k2->objectid)
be0e5c09 109 return 1;
e2fa7227 110 if (k1.objectid < k2->objectid)
be0e5c09 111 return -1;
a8a2ee0c
CM
112 if (k1.offset > k2->offset)
113 return 1;
114 if (k1.offset < k2->offset)
115 return -1;
f254e52c
CM
116 if (k1.flags > k2->flags)
117 return 1;
118 if (k1.flags < k2->flags)
119 return -1;
be0e5c09
CM
120 return 0;
121}
74123bd7 122
123abc88
CM
123static int check_node(struct btrfs_root *root, struct btrfs_path *path,
124 int level)
aa5d6bed
CM
125{
126 int i;
234b63a0 127 struct btrfs_node *parent = NULL;
e20d96d6 128 struct btrfs_node *node = btrfs_buffer_node(path->nodes[level]);
aa5d6bed 129 int parent_slot;
7518a238 130 u32 nritems = btrfs_header_nritems(&node->header);
aa5d6bed
CM
131
132 if (path->nodes[level + 1])
e20d96d6 133 parent = btrfs_buffer_node(path->nodes[level + 1]);
aa5d6bed 134 parent_slot = path->slots[level + 1];
7518a238
CM
135 BUG_ON(nritems == 0);
136 if (parent) {
e2fa7227 137 struct btrfs_disk_key *parent_key;
123abc88
CM
138 parent_key = &parent->ptrs[parent_slot].key;
139 BUG_ON(memcmp(parent_key, &node->ptrs[0].key,
e2fa7227 140 sizeof(struct btrfs_disk_key)));
1d4f8a0c 141 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
7518a238 142 btrfs_header_blocknr(&node->header));
aa5d6bed 143 }
123abc88 144 BUG_ON(nritems > BTRFS_NODEPTRS_PER_BLOCK(root));
7518a238 145 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
e2fa7227 146 struct btrfs_key cpukey;
123abc88
CM
147 btrfs_disk_key_to_cpu(&cpukey, &node->ptrs[i + 1].key);
148 BUG_ON(comp_keys(&node->ptrs[i].key, &cpukey) >= 0);
aa5d6bed
CM
149 }
150 return 0;
151}
152
123abc88
CM
153static int check_leaf(struct btrfs_root *root, struct btrfs_path *path,
154 int level)
aa5d6bed
CM
155{
156 int i;
e20d96d6 157 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[level]);
234b63a0 158 struct btrfs_node *parent = NULL;
aa5d6bed 159 int parent_slot;
7518a238 160 u32 nritems = btrfs_header_nritems(&leaf->header);
aa5d6bed
CM
161
162 if (path->nodes[level + 1])
e20d96d6 163 parent = btrfs_buffer_node(path->nodes[level + 1]);
aa5d6bed 164 parent_slot = path->slots[level + 1];
123abc88 165 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
7518a238
CM
166
167 if (nritems == 0)
168 return 0;
169
170 if (parent) {
e2fa7227 171 struct btrfs_disk_key *parent_key;
123abc88 172 parent_key = &parent->ptrs[parent_slot].key;
aa5d6bed 173 BUG_ON(memcmp(parent_key, &leaf->items[0].key,
e2fa7227 174 sizeof(struct btrfs_disk_key)));
1d4f8a0c 175 BUG_ON(btrfs_node_blockptr(parent, parent_slot) !=
7518a238 176 btrfs_header_blocknr(&leaf->header));
aa5d6bed 177 }
7518a238 178 for (i = 0; nritems > 1 && i < nritems - 2; i++) {
e2fa7227
CM
179 struct btrfs_key cpukey;
180 btrfs_disk_key_to_cpu(&cpukey, &leaf->items[i + 1].key);
aa5d6bed 181 BUG_ON(comp_keys(&leaf->items[i].key,
e2fa7227 182 &cpukey) >= 0);
0783fcfc
CM
183 BUG_ON(btrfs_item_offset(leaf->items + i) !=
184 btrfs_item_end(leaf->items + i + 1));
aa5d6bed 185 if (i == 0) {
0783fcfc
CM
186 BUG_ON(btrfs_item_offset(leaf->items + i) +
187 btrfs_item_size(leaf->items + i) !=
123abc88 188 BTRFS_LEAF_DATA_SIZE(root));
aa5d6bed
CM
189 }
190 }
aa5d6bed
CM
191 return 0;
192}
193
123abc88
CM
194static int check_block(struct btrfs_root *root, struct btrfs_path *path,
195 int level)
aa5d6bed
CM
196{
197 if (level == 0)
123abc88
CM
198 return check_leaf(root, path, level);
199 return check_node(root, path, level);
aa5d6bed
CM
200}
201
74123bd7
CM
202/*
203 * search for key in the array p. items p are item_size apart
204 * and there are 'max' items in p
205 * the slot in the array is returned via slot, and it points to
206 * the place where you would insert key if it is not found in
207 * the array.
208 *
209 * slot may point to max if the key is bigger than all of the keys
210 */
9aca1d51 211static int generic_bin_search(char *p, int item_size, struct btrfs_key *key,
be0e5c09
CM
212 int max, int *slot)
213{
214 int low = 0;
215 int high = max;
216 int mid;
217 int ret;
e2fa7227 218 struct btrfs_disk_key *tmp;
be0e5c09
CM
219
220 while(low < high) {
221 mid = (low + high) / 2;
e2fa7227 222 tmp = (struct btrfs_disk_key *)(p + mid * item_size);
be0e5c09
CM
223 ret = comp_keys(tmp, key);
224
225 if (ret < 0)
226 low = mid + 1;
227 else if (ret > 0)
228 high = mid;
229 else {
230 *slot = mid;
231 return 0;
232 }
233 }
234 *slot = low;
235 return 1;
236}
237
97571fd0
CM
238/*
239 * simple bin_search frontend that does the right thing for
240 * leaves vs nodes
241 */
9aca1d51 242static int bin_search(struct btrfs_node *c, struct btrfs_key *key, int *slot)
be0e5c09 243{
7518a238 244 if (btrfs_is_leaf(c)) {
234b63a0 245 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
0783fcfc
CM
246 return generic_bin_search((void *)l->items,
247 sizeof(struct btrfs_item),
7518a238
CM
248 key, btrfs_header_nritems(&c->header),
249 slot);
be0e5c09 250 } else {
123abc88
CM
251 return generic_bin_search((void *)c->ptrs,
252 sizeof(struct btrfs_key_ptr),
7518a238
CM
253 key, btrfs_header_nritems(&c->header),
254 slot);
be0e5c09
CM
255 }
256 return -1;
257}
258
e20d96d6
CM
259static struct buffer_head *read_node_slot(struct btrfs_root *root,
260 struct buffer_head *parent_buf,
bb803951
CM
261 int slot)
262{
e20d96d6 263 struct btrfs_node *node = btrfs_buffer_node(parent_buf);
bb803951
CM
264 if (slot < 0)
265 return NULL;
7518a238 266 if (slot >= btrfs_header_nritems(&node->header))
bb803951 267 return NULL;
1d4f8a0c 268 return read_tree_block(root, btrfs_node_blockptr(node, slot));
bb803951
CM
269}
270
e089f05c
CM
271static int balance_level(struct btrfs_trans_handle *trans, struct btrfs_root
272 *root, struct btrfs_path *path, int level)
bb803951 273{
e20d96d6
CM
274 struct buffer_head *right_buf;
275 struct buffer_head *mid_buf;
276 struct buffer_head *left_buf;
277 struct buffer_head *parent_buf = NULL;
234b63a0
CM
278 struct btrfs_node *right = NULL;
279 struct btrfs_node *mid;
280 struct btrfs_node *left = NULL;
281 struct btrfs_node *parent = NULL;
bb803951
CM
282 int ret = 0;
283 int wret;
284 int pslot;
bb803951 285 int orig_slot = path->slots[level];
79f95c82 286 u64 orig_ptr;
bb803951
CM
287
288 if (level == 0)
289 return 0;
290
291 mid_buf = path->nodes[level];
e20d96d6 292 mid = btrfs_buffer_node(mid_buf);
1d4f8a0c 293 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
79f95c82 294
234b63a0 295 if (level < BTRFS_MAX_LEVEL - 1)
bb803951
CM
296 parent_buf = path->nodes[level + 1];
297 pslot = path->slots[level + 1];
298
40689478
CM
299 /*
300 * deal with the case where there is only one pointer in the root
301 * by promoting the node below to a root
302 */
bb803951 303 if (!parent_buf) {
e20d96d6
CM
304 struct buffer_head *child;
305 u64 blocknr = mid_buf->b_blocknr;
bb803951 306
7518a238 307 if (btrfs_header_nritems(&mid->header) != 1)
bb803951
CM
308 return 0;
309
310 /* promote the child to a root */
311 child = read_node_slot(root, mid_buf, 0);
312 BUG_ON(!child);
313 root->node = child;
314 path->nodes[level] = NULL;
315 /* once for the path */
234b63a0 316 btrfs_block_release(root, mid_buf);
bb803951 317 /* once for the root ptr */
234b63a0 318 btrfs_block_release(root, mid_buf);
e089f05c
CM
319 clean_tree_block(trans, root, mid_buf);
320 return btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951 321 }
e20d96d6 322 parent = btrfs_buffer_node(parent_buf);
bb803951 323
123abc88
CM
324 if (btrfs_header_nritems(&mid->header) >
325 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
bb803951
CM
326 return 0;
327
bb803951
CM
328 left_buf = read_node_slot(root, parent_buf, pslot - 1);
329 right_buf = read_node_slot(root, parent_buf, pslot + 1);
79f95c82
CM
330
331 /* first, try to make some room in the middle buffer */
bb803951 332 if (left_buf) {
e089f05c
CM
333 btrfs_cow_block(trans, root, left_buf, parent_buf, pslot - 1,
334 &left_buf);
e20d96d6 335 left = btrfs_buffer_node(left_buf);
7518a238 336 orig_slot += btrfs_header_nritems(&left->header);
e089f05c 337 wret = push_node_left(trans, root, left_buf, mid_buf);
79f95c82
CM
338 if (wret < 0)
339 ret = wret;
bb803951 340 }
79f95c82
CM
341
342 /*
343 * then try to empty the right most buffer into the middle
344 */
bb803951 345 if (right_buf) {
e089f05c
CM
346 btrfs_cow_block(trans, root, right_buf, parent_buf, pslot + 1,
347 &right_buf);
e20d96d6 348 right = btrfs_buffer_node(right_buf);
e089f05c 349 wret = push_node_left(trans, root, mid_buf, right_buf);
79f95c82
CM
350 if (wret < 0)
351 ret = wret;
7518a238 352 if (btrfs_header_nritems(&right->header) == 0) {
e20d96d6 353 u64 blocknr = right_buf->b_blocknr;
234b63a0 354 btrfs_block_release(root, right_buf);
e089f05c 355 clean_tree_block(trans, root, right_buf);
bb803951
CM
356 right_buf = NULL;
357 right = NULL;
e089f05c
CM
358 wret = del_ptr(trans, root, path, level + 1, pslot +
359 1);
bb803951
CM
360 if (wret)
361 ret = wret;
e089f05c 362 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951
CM
363 if (wret)
364 ret = wret;
365 } else {
123abc88
CM
366 memcpy(&parent->ptrs[pslot + 1].key,
367 &right->ptrs[0].key,
e2fa7227 368 sizeof(struct btrfs_disk_key));
d5719762 369 mark_buffer_dirty(parent_buf);
bb803951
CM
370 }
371 }
7518a238 372 if (btrfs_header_nritems(&mid->header) == 1) {
79f95c82
CM
373 /*
374 * we're not allowed to leave a node with one item in the
375 * tree during a delete. A deletion from lower in the tree
376 * could try to delete the only pointer in this node.
377 * So, pull some keys from the left.
378 * There has to be a left pointer at this point because
379 * otherwise we would have pulled some pointers from the
380 * right
381 */
382 BUG_ON(!left_buf);
e089f05c 383 wret = balance_node_right(trans, root, mid_buf, left_buf);
79f95c82
CM
384 if (wret < 0)
385 ret = wret;
386 BUG_ON(wret == 1);
387 }
7518a238 388 if (btrfs_header_nritems(&mid->header) == 0) {
79f95c82 389 /* we've managed to empty the middle node, drop it */
e20d96d6 390 u64 blocknr = mid_buf->b_blocknr;
234b63a0 391 btrfs_block_release(root, mid_buf);
e089f05c 392 clean_tree_block(trans, root, mid_buf);
bb803951
CM
393 mid_buf = NULL;
394 mid = NULL;
e089f05c 395 wret = del_ptr(trans, root, path, level + 1, pslot);
bb803951
CM
396 if (wret)
397 ret = wret;
e089f05c 398 wret = btrfs_free_extent(trans, root, blocknr, 1, 1);
bb803951
CM
399 if (wret)
400 ret = wret;
79f95c82
CM
401 } else {
402 /* update the parent key to reflect our changes */
123abc88 403 memcpy(&parent->ptrs[pslot].key, &mid->ptrs[0].key,
e2fa7227 404 sizeof(struct btrfs_disk_key));
d5719762 405 mark_buffer_dirty(parent_buf);
79f95c82 406 }
bb803951 407
79f95c82 408 /* update the path */
bb803951 409 if (left_buf) {
7518a238 410 if (btrfs_header_nritems(&left->header) > orig_slot) {
e20d96d6 411 get_bh(left_buf);
bb803951
CM
412 path->nodes[level] = left_buf;
413 path->slots[level + 1] -= 1;
414 path->slots[level] = orig_slot;
415 if (mid_buf)
234b63a0 416 btrfs_block_release(root, mid_buf);
bb803951 417 } else {
7518a238 418 orig_slot -= btrfs_header_nritems(&left->header);
bb803951
CM
419 path->slots[level] = orig_slot;
420 }
421 }
79f95c82 422 /* double check we haven't messed things up */
123abc88 423 check_block(root, path, level);
e20d96d6
CM
424 if (orig_ptr !=
425 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[level]),
426 path->slots[level]))
79f95c82 427 BUG();
bb803951
CM
428
429 if (right_buf)
234b63a0 430 btrfs_block_release(root, right_buf);
bb803951 431 if (left_buf)
234b63a0 432 btrfs_block_release(root, left_buf);
bb803951
CM
433 return ret;
434}
435
74123bd7
CM
436/*
437 * look for key in the tree. path is filled in with nodes along the way
438 * if key is found, we return zero and you can find the item in the leaf
439 * level of the path (level 0)
440 *
441 * If the key isn't found, the path points to the slot where it should
aa5d6bed
CM
442 * be inserted, and 1 is returned. If there are other errors during the
443 * search a negative error number is returned.
97571fd0
CM
444 *
445 * if ins_len > 0, nodes and leaves will be split as we walk down the
446 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
447 * possible)
74123bd7 448 */
e089f05c
CM
449int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
450 *root, struct btrfs_key *key, struct btrfs_path *p, int
451 ins_len, int cow)
be0e5c09 452{
e20d96d6
CM
453 struct buffer_head *b;
454 struct buffer_head *cow_buf;
234b63a0 455 struct btrfs_node *c;
be0e5c09
CM
456 int slot;
457 int ret;
458 int level;
5c680ed6 459
bb803951
CM
460again:
461 b = root->node;
e20d96d6 462 get_bh(b);
eb60ceac 463 while (b) {
e20d96d6
CM
464 c = btrfs_buffer_node(b);
465 level = btrfs_header_level(&c->header);
02217ed2
CM
466 if (cow) {
467 int wret;
e20d96d6
CM
468 wret = btrfs_cow_block(trans, root, b,
469 p->nodes[level + 1],
470 p->slots[level + 1],
e089f05c 471 &cow_buf);
02217ed2
CM
472 b = cow_buf;
473 }
474 BUG_ON(!cow && ins_len);
e20d96d6 475 c = btrfs_buffer_node(b);
eb60ceac 476 p->nodes[level] = b;
123abc88 477 ret = check_block(root, p, level);
aa5d6bed
CM
478 if (ret)
479 return -1;
be0e5c09 480 ret = bin_search(c, key, &slot);
7518a238 481 if (!btrfs_is_leaf(c)) {
be0e5c09
CM
482 if (ret && slot > 0)
483 slot -= 1;
484 p->slots[level] = slot;
7518a238 485 if (ins_len > 0 && btrfs_header_nritems(&c->header) ==
123abc88 486 BTRFS_NODEPTRS_PER_BLOCK(root)) {
e089f05c 487 int sret = split_node(trans, root, p, level);
5c680ed6
CM
488 BUG_ON(sret > 0);
489 if (sret)
490 return sret;
491 b = p->nodes[level];
e20d96d6 492 c = btrfs_buffer_node(b);
5c680ed6 493 slot = p->slots[level];
bb803951 494 } else if (ins_len < 0) {
e089f05c
CM
495 int sret = balance_level(trans, root, p,
496 level);
bb803951
CM
497 if (sret)
498 return sret;
499 b = p->nodes[level];
500 if (!b)
501 goto again;
e20d96d6 502 c = btrfs_buffer_node(b);
bb803951 503 slot = p->slots[level];
7518a238 504 BUG_ON(btrfs_header_nritems(&c->header) == 1);
5c680ed6 505 }
1d4f8a0c 506 b = read_tree_block(root, btrfs_node_blockptr(c, slot));
be0e5c09 507 } else {
234b63a0 508 struct btrfs_leaf *l = (struct btrfs_leaf *)c;
be0e5c09 509 p->slots[level] = slot;
123abc88 510 if (ins_len > 0 && btrfs_leaf_free_space(root, l) <
0783fcfc 511 sizeof(struct btrfs_item) + ins_len) {
e089f05c 512 int sret = split_leaf(trans, root, p, ins_len);
5c680ed6
CM
513 BUG_ON(sret > 0);
514 if (sret)
515 return sret;
516 }
be0e5c09
CM
517 return ret;
518 }
519 }
aa5d6bed 520 return 1;
be0e5c09
CM
521}
522
74123bd7
CM
523/*
524 * adjust the pointers going up the tree, starting at level
525 * making sure the right key of each node is points to 'key'.
526 * This is used after shifting pointers to the left, so it stops
527 * fixing up pointers when a given leaf/node is not in slot 0 of the
528 * higher levels
aa5d6bed
CM
529 *
530 * If this fails to write a tree block, it returns -1, but continues
531 * fixing up the blocks in ram so the tree is consistent.
74123bd7 532 */
e089f05c
CM
533static int fixup_low_keys(struct btrfs_trans_handle *trans, struct btrfs_root
534 *root, struct btrfs_path *path, struct btrfs_disk_key
535 *key, int level)
be0e5c09
CM
536{
537 int i;
aa5d6bed 538 int ret = 0;
234b63a0
CM
539 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
540 struct btrfs_node *t;
be0e5c09 541 int tslot = path->slots[i];
eb60ceac 542 if (!path->nodes[i])
be0e5c09 543 break;
e20d96d6 544 t = btrfs_buffer_node(path->nodes[i]);
123abc88 545 memcpy(&t->ptrs[tslot].key, key, sizeof(*key));
d5719762 546 mark_buffer_dirty(path->nodes[i]);
be0e5c09
CM
547 if (tslot != 0)
548 break;
549 }
aa5d6bed 550 return ret;
be0e5c09
CM
551}
552
74123bd7
CM
553/*
554 * try to push data from one node into the next node left in the
79f95c82 555 * tree.
aa5d6bed
CM
556 *
557 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
558 * error, and > 0 if there was no room in the left hand block.
74123bd7 559 */
e089f05c 560static int push_node_left(struct btrfs_trans_handle *trans, struct btrfs_root
e20d96d6
CM
561 *root, struct buffer_head *dst_buf, struct
562 buffer_head *src_buf)
be0e5c09 563{
e20d96d6
CM
564 struct btrfs_node *src = btrfs_buffer_node(src_buf);
565 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
be0e5c09 566 int push_items = 0;
bb803951
CM
567 int src_nritems;
568 int dst_nritems;
aa5d6bed 569 int ret = 0;
be0e5c09 570
7518a238
CM
571 src_nritems = btrfs_header_nritems(&src->header);
572 dst_nritems = btrfs_header_nritems(&dst->header);
123abc88 573 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
eb60ceac 574 if (push_items <= 0) {
be0e5c09 575 return 1;
eb60ceac 576 }
be0e5c09 577
bb803951 578 if (src_nritems < push_items)
79f95c82
CM
579 push_items = src_nritems;
580
123abc88
CM
581 memcpy(dst->ptrs + dst_nritems, src->ptrs,
582 push_items * sizeof(struct btrfs_key_ptr));
bb803951 583 if (push_items < src_nritems) {
123abc88 584 memmove(src->ptrs, src->ptrs + push_items,
e2fa7227 585 (src_nritems - push_items) *
123abc88 586 sizeof(struct btrfs_key_ptr));
bb803951 587 }
7518a238
CM
588 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
589 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
d5719762
CM
590 mark_buffer_dirty(src_buf);
591 mark_buffer_dirty(dst_buf);
79f95c82
CM
592 return ret;
593}
594
595/*
596 * try to push data from one node into the next node right in the
597 * tree.
598 *
599 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
600 * error, and > 0 if there was no room in the right hand block.
601 *
602 * this will only push up to 1/2 the contents of the left node over
603 */
e089f05c 604static int balance_node_right(struct btrfs_trans_handle *trans, struct
e20d96d6
CM
605 btrfs_root *root, struct buffer_head *dst_buf,
606 struct buffer_head *src_buf)
79f95c82 607{
e20d96d6
CM
608 struct btrfs_node *src = btrfs_buffer_node(src_buf);
609 struct btrfs_node *dst = btrfs_buffer_node(dst_buf);
79f95c82
CM
610 int push_items = 0;
611 int max_push;
612 int src_nritems;
613 int dst_nritems;
614 int ret = 0;
79f95c82 615
7518a238
CM
616 src_nritems = btrfs_header_nritems(&src->header);
617 dst_nritems = btrfs_header_nritems(&dst->header);
123abc88 618 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
79f95c82
CM
619 if (push_items <= 0) {
620 return 1;
621 }
622
623 max_push = src_nritems / 2 + 1;
624 /* don't try to empty the node */
625 if (max_push > src_nritems)
626 return 1;
627 if (max_push < push_items)
628 push_items = max_push;
629
123abc88
CM
630 memmove(dst->ptrs + push_items, dst->ptrs,
631 dst_nritems * sizeof(struct btrfs_key_ptr));
632 memcpy(dst->ptrs, src->ptrs + src_nritems - push_items,
633 push_items * sizeof(struct btrfs_key_ptr));
79f95c82 634
7518a238
CM
635 btrfs_set_header_nritems(&src->header, src_nritems - push_items);
636 btrfs_set_header_nritems(&dst->header, dst_nritems + push_items);
79f95c82 637
d5719762
CM
638 mark_buffer_dirty(src_buf);
639 mark_buffer_dirty(dst_buf);
aa5d6bed 640 return ret;
be0e5c09
CM
641}
642
97571fd0
CM
643/*
644 * helper function to insert a new root level in the tree.
645 * A new node is allocated, and a single item is inserted to
646 * point to the existing root
aa5d6bed
CM
647 *
648 * returns zero on success or < 0 on failure.
97571fd0 649 */
e089f05c
CM
650static int insert_new_root(struct btrfs_trans_handle *trans, struct btrfs_root
651 *root, struct btrfs_path *path, int level)
5c680ed6 652{
e20d96d6 653 struct buffer_head *t;
234b63a0
CM
654 struct btrfs_node *lower;
655 struct btrfs_node *c;
e2fa7227 656 struct btrfs_disk_key *lower_key;
5c680ed6
CM
657
658 BUG_ON(path->nodes[level]);
659 BUG_ON(path->nodes[level-1] != root->node);
660
e089f05c 661 t = btrfs_alloc_free_block(trans, root);
e20d96d6 662 c = btrfs_buffer_node(t);
123abc88 663 memset(c, 0, root->blocksize);
7518a238
CM
664 btrfs_set_header_nritems(&c->header, 1);
665 btrfs_set_header_level(&c->header, level);
e20d96d6 666 btrfs_set_header_blocknr(&c->header, t->b_blocknr);
7f5c1516 667 btrfs_set_header_generation(&c->header, trans->transid);
7518a238 668 btrfs_set_header_parentid(&c->header,
e20d96d6
CM
669 btrfs_header_parentid(btrfs_buffer_header(root->node)));
670 lower = btrfs_buffer_node(path->nodes[level-1]);
7518a238 671 if (btrfs_is_leaf(lower))
234b63a0 672 lower_key = &((struct btrfs_leaf *)lower)->items[0].key;
5c680ed6 673 else
123abc88
CM
674 lower_key = &lower->ptrs[0].key;
675 memcpy(&c->ptrs[0].key, lower_key, sizeof(struct btrfs_disk_key));
e20d96d6 676 btrfs_set_node_blockptr(c, 0, path->nodes[level - 1]->b_blocknr);
d5719762
CM
677
678 mark_buffer_dirty(t);
679
5c680ed6 680 /* the super has an extra ref to root->node */
234b63a0 681 btrfs_block_release(root, root->node);
5c680ed6 682 root->node = t;
e20d96d6 683 get_bh(t);
5c680ed6
CM
684 path->nodes[level] = t;
685 path->slots[level] = 0;
686 return 0;
687}
688
74123bd7
CM
689/*
690 * worker function to insert a single pointer in a node.
691 * the node should have enough room for the pointer already
97571fd0 692 *
74123bd7
CM
693 * slot and level indicate where you want the key to go, and
694 * blocknr is the block the key points to.
aa5d6bed
CM
695 *
696 * returns zero on success and < 0 on any error
74123bd7 697 */
e089f05c
CM
698static int insert_ptr(struct btrfs_trans_handle *trans, struct btrfs_root
699 *root, struct btrfs_path *path, struct btrfs_disk_key
700 *key, u64 blocknr, int slot, int level)
74123bd7 701{
234b63a0 702 struct btrfs_node *lower;
74123bd7 703 int nritems;
5c680ed6
CM
704
705 BUG_ON(!path->nodes[level]);
e20d96d6 706 lower = btrfs_buffer_node(path->nodes[level]);
7518a238 707 nritems = btrfs_header_nritems(&lower->header);
74123bd7
CM
708 if (slot > nritems)
709 BUG();
123abc88 710 if (nritems == BTRFS_NODEPTRS_PER_BLOCK(root))
74123bd7
CM
711 BUG();
712 if (slot != nritems) {
123abc88
CM
713 memmove(lower->ptrs + slot + 1, lower->ptrs + slot,
714 (nritems - slot) * sizeof(struct btrfs_key_ptr));
74123bd7 715 }
123abc88 716 memcpy(&lower->ptrs[slot].key, key, sizeof(struct btrfs_disk_key));
1d4f8a0c 717 btrfs_set_node_blockptr(lower, slot, blocknr);
7518a238 718 btrfs_set_header_nritems(&lower->header, nritems + 1);
d5719762 719 mark_buffer_dirty(path->nodes[level]);
74123bd7
CM
720 return 0;
721}
722
97571fd0
CM
723/*
724 * split the node at the specified level in path in two.
725 * The path is corrected to point to the appropriate node after the split
726 *
727 * Before splitting this tries to make some room in the node by pushing
728 * left and right, if either one works, it returns right away.
aa5d6bed
CM
729 *
730 * returns 0 on success and < 0 on failure
97571fd0 731 */
e089f05c
CM
732static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
733 *root, struct btrfs_path *path, int level)
be0e5c09 734{
e20d96d6 735 struct buffer_head *t;
234b63a0 736 struct btrfs_node *c;
e20d96d6 737 struct buffer_head *split_buffer;
234b63a0 738 struct btrfs_node *split;
be0e5c09 739 int mid;
5c680ed6 740 int ret;
aa5d6bed 741 int wret;
7518a238 742 u32 c_nritems;
eb60ceac 743
5c680ed6 744 t = path->nodes[level];
e20d96d6 745 c = btrfs_buffer_node(t);
5c680ed6
CM
746 if (t == root->node) {
747 /* trying to split the root, lets make a new one */
e089f05c 748 ret = insert_new_root(trans, root, path, level + 1);
5c680ed6
CM
749 if (ret)
750 return ret;
be0e5c09 751 }
7518a238 752 c_nritems = btrfs_header_nritems(&c->header);
e089f05c 753 split_buffer = btrfs_alloc_free_block(trans, root);
e20d96d6 754 split = btrfs_buffer_node(split_buffer);
7518a238 755 btrfs_set_header_flags(&split->header, btrfs_header_flags(&c->header));
9a6f11ed 756 btrfs_set_header_level(&split->header, btrfs_header_level(&c->header));
e20d96d6 757 btrfs_set_header_blocknr(&split->header, split_buffer->b_blocknr);
7f5c1516 758 btrfs_set_header_generation(&split->header, trans->transid);
7518a238 759 btrfs_set_header_parentid(&split->header,
e20d96d6 760 btrfs_header_parentid(btrfs_buffer_header(root->node)));
7518a238 761 mid = (c_nritems + 1) / 2;
123abc88
CM
762 memcpy(split->ptrs, c->ptrs + mid,
763 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
7518a238
CM
764 btrfs_set_header_nritems(&split->header, c_nritems - mid);
765 btrfs_set_header_nritems(&c->header, mid);
aa5d6bed
CM
766 ret = 0;
767
d5719762
CM
768 mark_buffer_dirty(t);
769 mark_buffer_dirty(split_buffer);
e089f05c 770 wret = insert_ptr(trans, root, path, &split->ptrs[0].key,
e20d96d6 771 split_buffer->b_blocknr, path->slots[level + 1] + 1,
123abc88 772 level + 1);
aa5d6bed
CM
773 if (wret)
774 ret = wret;
775
5de08d7d 776 if (path->slots[level] >= mid) {
5c680ed6 777 path->slots[level] -= mid;
234b63a0 778 btrfs_block_release(root, t);
5c680ed6
CM
779 path->nodes[level] = split_buffer;
780 path->slots[level + 1] += 1;
781 } else {
234b63a0 782 btrfs_block_release(root, split_buffer);
be0e5c09 783 }
aa5d6bed 784 return ret;
be0e5c09
CM
785}
786
74123bd7
CM
787/*
788 * how many bytes are required to store the items in a leaf. start
789 * and nr indicate which items in the leaf to check. This totals up the
790 * space used both by the item structs and the item data
791 */
234b63a0 792static int leaf_space_used(struct btrfs_leaf *l, int start, int nr)
be0e5c09
CM
793{
794 int data_len;
795 int end = start + nr - 1;
796
797 if (!nr)
798 return 0;
0783fcfc
CM
799 data_len = btrfs_item_end(l->items + start);
800 data_len = data_len - btrfs_item_offset(l->items + end);
801 data_len += sizeof(struct btrfs_item) * nr;
be0e5c09
CM
802 return data_len;
803}
804
00ec4c51
CM
805/*
806 * push some data in the path leaf to the right, trying to free up at
807 * least data_size bytes. returns zero if the push worked, nonzero otherwise
aa5d6bed
CM
808 *
809 * returns 1 if the push failed because the other node didn't have enough
810 * room, 0 if everything worked out and < 0 if there were major errors.
00ec4c51 811 */
e089f05c
CM
812static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
813 *root, struct btrfs_path *path, int data_size)
00ec4c51 814{
e20d96d6
CM
815 struct buffer_head *left_buf = path->nodes[0];
816 struct btrfs_leaf *left = btrfs_buffer_leaf(left_buf);
234b63a0 817 struct btrfs_leaf *right;
e20d96d6
CM
818 struct buffer_head *right_buf;
819 struct buffer_head *upper;
820 struct btrfs_node *upper_node;
00ec4c51
CM
821 int slot;
822 int i;
823 int free_space;
824 int push_space = 0;
825 int push_items = 0;
0783fcfc 826 struct btrfs_item *item;
7518a238
CM
827 u32 left_nritems;
828 u32 right_nritems;
00ec4c51
CM
829
830 slot = path->slots[1];
831 if (!path->nodes[1]) {
832 return 1;
833 }
834 upper = path->nodes[1];
e20d96d6
CM
835 upper_node = btrfs_buffer_node(upper);
836 if (slot >= btrfs_header_nritems(&upper_node->header) - 1) {
00ec4c51
CM
837 return 1;
838 }
e20d96d6
CM
839 right_buf = read_tree_block(root,
840 btrfs_node_blockptr(btrfs_buffer_node(upper), slot + 1));
841 right = btrfs_buffer_leaf(right_buf);
123abc88 842 free_space = btrfs_leaf_free_space(root, right);
0783fcfc 843 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 844 btrfs_block_release(root, right_buf);
00ec4c51
CM
845 return 1;
846 }
02217ed2 847 /* cow and double check */
e089f05c 848 btrfs_cow_block(trans, root, right_buf, upper, slot + 1, &right_buf);
e20d96d6 849 right = btrfs_buffer_leaf(right_buf);
123abc88 850 free_space = btrfs_leaf_free_space(root, right);
0783fcfc 851 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 852 btrfs_block_release(root, right_buf);
02217ed2
CM
853 return 1;
854 }
855
7518a238
CM
856 left_nritems = btrfs_header_nritems(&left->header);
857 for (i = left_nritems - 1; i >= 0; i--) {
00ec4c51
CM
858 item = left->items + i;
859 if (path->slots[0] == i)
860 push_space += data_size + sizeof(*item);
0783fcfc
CM
861 if (btrfs_item_size(item) + sizeof(*item) + push_space >
862 free_space)
00ec4c51
CM
863 break;
864 push_items++;
0783fcfc 865 push_space += btrfs_item_size(item) + sizeof(*item);
00ec4c51
CM
866 }
867 if (push_items == 0) {
234b63a0 868 btrfs_block_release(root, right_buf);
00ec4c51
CM
869 return 1;
870 }
7518a238 871 right_nritems = btrfs_header_nritems(&right->header);
00ec4c51 872 /* push left to right */
0783fcfc 873 push_space = btrfs_item_end(left->items + left_nritems - push_items);
123abc88 874 push_space -= leaf_data_end(root, left);
00ec4c51 875 /* make room in the right data area */
123abc88
CM
876 memmove(btrfs_leaf_data(right) + leaf_data_end(root, right) -
877 push_space, btrfs_leaf_data(right) + leaf_data_end(root, right),
878 BTRFS_LEAF_DATA_SIZE(root) - leaf_data_end(root, right));
00ec4c51 879 /* copy from the left data area */
123abc88
CM
880 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) - push_space,
881 btrfs_leaf_data(left) + leaf_data_end(root, left), push_space);
00ec4c51 882 memmove(right->items + push_items, right->items,
0783fcfc 883 right_nritems * sizeof(struct btrfs_item));
00ec4c51 884 /* copy the items from left to right */
7518a238 885 memcpy(right->items, left->items + left_nritems - push_items,
0783fcfc 886 push_items * sizeof(struct btrfs_item));
00ec4c51
CM
887
888 /* update the item pointers */
7518a238
CM
889 right_nritems += push_items;
890 btrfs_set_header_nritems(&right->header, right_nritems);
123abc88 891 push_space = BTRFS_LEAF_DATA_SIZE(root);
7518a238 892 for (i = 0; i < right_nritems; i++) {
0783fcfc
CM
893 btrfs_set_item_offset(right->items + i, push_space -
894 btrfs_item_size(right->items + i));
895 push_space = btrfs_item_offset(right->items + i);
00ec4c51 896 }
7518a238
CM
897 left_nritems -= push_items;
898 btrfs_set_header_nritems(&left->header, left_nritems);
00ec4c51 899
d5719762
CM
900 mark_buffer_dirty(left_buf);
901 mark_buffer_dirty(right_buf);
e20d96d6 902 memcpy(&upper_node->ptrs[slot + 1].key,
e2fa7227 903 &right->items[0].key, sizeof(struct btrfs_disk_key));
d5719762 904 mark_buffer_dirty(upper);
02217ed2 905
00ec4c51 906 /* then fixup the leaf pointer in the path */
7518a238
CM
907 if (path->slots[0] >= left_nritems) {
908 path->slots[0] -= left_nritems;
234b63a0 909 btrfs_block_release(root, path->nodes[0]);
00ec4c51
CM
910 path->nodes[0] = right_buf;
911 path->slots[1] += 1;
912 } else {
234b63a0 913 btrfs_block_release(root, right_buf);
00ec4c51
CM
914 }
915 return 0;
916}
74123bd7
CM
917/*
918 * push some data in the path leaf to the left, trying to free up at
919 * least data_size bytes. returns zero if the push worked, nonzero otherwise
920 */
e089f05c
CM
921static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
922 *root, struct btrfs_path *path, int data_size)
be0e5c09 923{
e20d96d6
CM
924 struct buffer_head *right_buf = path->nodes[0];
925 struct btrfs_leaf *right = btrfs_buffer_leaf(right_buf);
926 struct buffer_head *t;
234b63a0 927 struct btrfs_leaf *left;
be0e5c09
CM
928 int slot;
929 int i;
930 int free_space;
931 int push_space = 0;
932 int push_items = 0;
0783fcfc 933 struct btrfs_item *item;
7518a238 934 u32 old_left_nritems;
aa5d6bed
CM
935 int ret = 0;
936 int wret;
be0e5c09
CM
937
938 slot = path->slots[1];
939 if (slot == 0) {
940 return 1;
941 }
942 if (!path->nodes[1]) {
943 return 1;
944 }
e20d96d6
CM
945 t = read_tree_block(root,
946 btrfs_node_blockptr(btrfs_buffer_node(path->nodes[1]), slot - 1));
947 left = btrfs_buffer_leaf(t);
123abc88 948 free_space = btrfs_leaf_free_space(root, left);
0783fcfc 949 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 950 btrfs_block_release(root, t);
be0e5c09
CM
951 return 1;
952 }
02217ed2
CM
953
954 /* cow and double check */
e089f05c 955 btrfs_cow_block(trans, root, t, path->nodes[1], slot - 1, &t);
e20d96d6 956 left = btrfs_buffer_leaf(t);
123abc88 957 free_space = btrfs_leaf_free_space(root, left);
0783fcfc 958 if (free_space < data_size + sizeof(struct btrfs_item)) {
234b63a0 959 btrfs_block_release(root, t);
02217ed2
CM
960 return 1;
961 }
962
7518a238 963 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
be0e5c09
CM
964 item = right->items + i;
965 if (path->slots[0] == i)
966 push_space += data_size + sizeof(*item);
0783fcfc
CM
967 if (btrfs_item_size(item) + sizeof(*item) + push_space >
968 free_space)
be0e5c09
CM
969 break;
970 push_items++;
0783fcfc 971 push_space += btrfs_item_size(item) + sizeof(*item);
be0e5c09
CM
972 }
973 if (push_items == 0) {
234b63a0 974 btrfs_block_release(root, t);
be0e5c09
CM
975 return 1;
976 }
977 /* push data from right to left */
7518a238 978 memcpy(left->items + btrfs_header_nritems(&left->header),
0783fcfc 979 right->items, push_items * sizeof(struct btrfs_item));
123abc88 980 push_space = BTRFS_LEAF_DATA_SIZE(root) -
0783fcfc 981 btrfs_item_offset(right->items + push_items -1);
123abc88
CM
982 memcpy(btrfs_leaf_data(left) + leaf_data_end(root, left) - push_space,
983 btrfs_leaf_data(right) +
984 btrfs_item_offset(right->items + push_items - 1),
be0e5c09 985 push_space);
7518a238 986 old_left_nritems = btrfs_header_nritems(&left->header);
eb60ceac
CM
987 BUG_ON(old_left_nritems < 0);
988
0783fcfc 989 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
123abc88
CM
990 u32 ioff = btrfs_item_offset(left->items + i);
991 btrfs_set_item_offset(left->items + i, ioff -
992 (BTRFS_LEAF_DATA_SIZE(root) -
0783fcfc
CM
993 btrfs_item_offset(left->items +
994 old_left_nritems - 1)));
be0e5c09 995 }
7518a238 996 btrfs_set_header_nritems(&left->header, old_left_nritems + push_items);
be0e5c09
CM
997
998 /* fixup right node */
0783fcfc 999 push_space = btrfs_item_offset(right->items + push_items - 1) -
123abc88
CM
1000 leaf_data_end(root, right);
1001 memmove(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1002 push_space, btrfs_leaf_data(right) +
1003 leaf_data_end(root, right), push_space);
be0e5c09 1004 memmove(right->items, right->items + push_items,
7518a238 1005 (btrfs_header_nritems(&right->header) - push_items) *
0783fcfc 1006 sizeof(struct btrfs_item));
7518a238
CM
1007 btrfs_set_header_nritems(&right->header,
1008 btrfs_header_nritems(&right->header) -
1009 push_items);
123abc88 1010 push_space = BTRFS_LEAF_DATA_SIZE(root);
eb60ceac 1011
7518a238 1012 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
0783fcfc
CM
1013 btrfs_set_item_offset(right->items + i, push_space -
1014 btrfs_item_size(right->items + i));
1015 push_space = btrfs_item_offset(right->items + i);
be0e5c09 1016 }
eb60ceac 1017
d5719762
CM
1018 mark_buffer_dirty(t);
1019 mark_buffer_dirty(right_buf);
eb60ceac 1020
e089f05c 1021 wret = fixup_low_keys(trans, root, path, &right->items[0].key, 1);
aa5d6bed
CM
1022 if (wret)
1023 ret = wret;
be0e5c09
CM
1024
1025 /* then fixup the leaf pointer in the path */
1026 if (path->slots[0] < push_items) {
1027 path->slots[0] += old_left_nritems;
234b63a0 1028 btrfs_block_release(root, path->nodes[0]);
eb60ceac 1029 path->nodes[0] = t;
be0e5c09
CM
1030 path->slots[1] -= 1;
1031 } else {
234b63a0 1032 btrfs_block_release(root, t);
be0e5c09
CM
1033 path->slots[0] -= push_items;
1034 }
eb60ceac 1035 BUG_ON(path->slots[0] < 0);
aa5d6bed 1036 return ret;
be0e5c09
CM
1037}
1038
74123bd7
CM
1039/*
1040 * split the path's leaf in two, making sure there is at least data_size
1041 * available for the resulting leaf level of the path.
aa5d6bed
CM
1042 *
1043 * returns 0 if all went well and < 0 on failure.
74123bd7 1044 */
e089f05c
CM
1045static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
1046 *root, struct btrfs_path *path, int data_size)
be0e5c09 1047{
e20d96d6 1048 struct buffer_head *l_buf;
234b63a0 1049 struct btrfs_leaf *l;
7518a238 1050 u32 nritems;
eb60ceac
CM
1051 int mid;
1052 int slot;
234b63a0 1053 struct btrfs_leaf *right;
e20d96d6 1054 struct buffer_head *right_buffer;
0783fcfc 1055 int space_needed = data_size + sizeof(struct btrfs_item);
be0e5c09
CM
1056 int data_copy_size;
1057 int rt_data_off;
1058 int i;
1059 int ret;
aa5d6bed
CM
1060 int wret;
1061
40689478 1062 /* first try to make some room by pushing left and right */
e089f05c 1063 wret = push_leaf_left(trans, root, path, data_size);
eaee50e8
CM
1064 if (wret < 0)
1065 return wret;
1066 if (wret) {
e089f05c 1067 wret = push_leaf_right(trans, root, path, data_size);
eaee50e8
CM
1068 if (wret < 0)
1069 return wret;
1070 }
aa5d6bed 1071 l_buf = path->nodes[0];
e20d96d6 1072 l = btrfs_buffer_leaf(l_buf);
aa5d6bed
CM
1073
1074 /* did the pushes work? */
123abc88
CM
1075 if (btrfs_leaf_free_space(root, l) >=
1076 sizeof(struct btrfs_item) + data_size)
aa5d6bed
CM
1077 return 0;
1078
5c680ed6 1079 if (!path->nodes[1]) {
e089f05c 1080 ret = insert_new_root(trans, root, path, 1);
5c680ed6
CM
1081 if (ret)
1082 return ret;
1083 }
eb60ceac 1084 slot = path->slots[0];
7518a238 1085 nritems = btrfs_header_nritems(&l->header);
eb60ceac 1086 mid = (nritems + 1)/ 2;
e089f05c 1087 right_buffer = btrfs_alloc_free_block(trans, root);
eb60ceac
CM
1088 BUG_ON(!right_buffer);
1089 BUG_ON(mid == nritems);
e20d96d6 1090 right = btrfs_buffer_leaf(right_buffer);
123abc88 1091 memset(&right->header, 0, sizeof(right->header));
be0e5c09 1092 if (mid <= slot) {
97571fd0 1093 /* FIXME, just alloc a new leaf here */
be0e5c09 1094 if (leaf_space_used(l, mid, nritems - mid) + space_needed >
123abc88 1095 BTRFS_LEAF_DATA_SIZE(root))
be0e5c09
CM
1096 BUG();
1097 } else {
97571fd0 1098 /* FIXME, just alloc a new leaf here */
be0e5c09 1099 if (leaf_space_used(l, 0, mid + 1) + space_needed >
123abc88 1100 BTRFS_LEAF_DATA_SIZE(root))
be0e5c09
CM
1101 BUG();
1102 }
7518a238 1103 btrfs_set_header_nritems(&right->header, nritems - mid);
e20d96d6 1104 btrfs_set_header_blocknr(&right->header, right_buffer->b_blocknr);
7f5c1516 1105 btrfs_set_header_generation(&right->header, trans->transid);
7518a238
CM
1106 btrfs_set_header_level(&right->header, 0);
1107 btrfs_set_header_parentid(&right->header,
e20d96d6 1108 btrfs_header_parentid(btrfs_buffer_header(root->node)));
123abc88
CM
1109 data_copy_size = btrfs_item_end(l->items + mid) -
1110 leaf_data_end(root, l);
be0e5c09 1111 memcpy(right->items, l->items + mid,
0783fcfc 1112 (nritems - mid) * sizeof(struct btrfs_item));
123abc88
CM
1113 memcpy(btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
1114 data_copy_size, btrfs_leaf_data(l) +
1115 leaf_data_end(root, l), data_copy_size);
1116 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
1117 btrfs_item_end(l->items + mid);
74123bd7 1118
0783fcfc 1119 for (i = 0; i < btrfs_header_nritems(&right->header); i++) {
123abc88 1120 u32 ioff = btrfs_item_offset(right->items + i);
0783fcfc
CM
1121 btrfs_set_item_offset(right->items + i, ioff + rt_data_off);
1122 }
74123bd7 1123
7518a238 1124 btrfs_set_header_nritems(&l->header, mid);
aa5d6bed 1125 ret = 0;
e089f05c 1126 wret = insert_ptr(trans, root, path, &right->items[0].key,
e20d96d6 1127 right_buffer->b_blocknr, path->slots[1] + 1, 1);
aa5d6bed
CM
1128 if (wret)
1129 ret = wret;
d5719762
CM
1130 mark_buffer_dirty(right_buffer);
1131 mark_buffer_dirty(l_buf);
eb60ceac 1132 BUG_ON(path->slots[0] != slot);
be0e5c09 1133 if (mid <= slot) {
234b63a0 1134 btrfs_block_release(root, path->nodes[0]);
eb60ceac 1135 path->nodes[0] = right_buffer;
be0e5c09
CM
1136 path->slots[0] -= mid;
1137 path->slots[1] += 1;
eb60ceac 1138 } else
234b63a0 1139 btrfs_block_release(root, right_buffer);
eb60ceac 1140 BUG_ON(path->slots[0] < 0);
be0e5c09
CM
1141 return ret;
1142}
1143
74123bd7
CM
1144/*
1145 * Given a key and some data, insert an item into the tree.
1146 * This does all the path init required, making room in the tree if needed.
1147 */
e089f05c
CM
1148int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
1149 *root, struct btrfs_path *path, struct btrfs_key
1150 *cpu_key, u32 data_size)
be0e5c09 1151{
aa5d6bed 1152 int ret = 0;
be0e5c09 1153 int slot;
eb60ceac 1154 int slot_orig;
234b63a0 1155 struct btrfs_leaf *leaf;
e20d96d6 1156 struct buffer_head *leaf_buf;
7518a238 1157 u32 nritems;
be0e5c09 1158 unsigned int data_end;
e2fa7227
CM
1159 struct btrfs_disk_key disk_key;
1160
1161 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
be0e5c09 1162
74123bd7 1163 /* create a root if there isn't one */
5c680ed6 1164 if (!root->node)
cfaa7295 1165 BUG();
e089f05c 1166 ret = btrfs_search_slot(trans, root, cpu_key, path, data_size, 1);
eb60ceac 1167 if (ret == 0) {
f0930a37 1168 return -EEXIST;
aa5d6bed 1169 }
ed2ff2cb
CM
1170 if (ret < 0)
1171 goto out;
be0e5c09 1172
62e2749e
CM
1173 slot_orig = path->slots[0];
1174 leaf_buf = path->nodes[0];
e20d96d6 1175 leaf = btrfs_buffer_leaf(leaf_buf);
74123bd7 1176
7518a238 1177 nritems = btrfs_header_nritems(&leaf->header);
123abc88 1178 data_end = leaf_data_end(root, leaf);
eb60ceac 1179
123abc88 1180 if (btrfs_leaf_free_space(root, leaf) <
234b63a0 1181 sizeof(struct btrfs_item) + data_size)
be0e5c09
CM
1182 BUG();
1183
62e2749e 1184 slot = path->slots[0];
eb60ceac 1185 BUG_ON(slot < 0);
be0e5c09
CM
1186 if (slot != nritems) {
1187 int i;
0783fcfc 1188 unsigned int old_data = btrfs_item_end(leaf->items + slot);
be0e5c09
CM
1189
1190 /*
1191 * item0..itemN ... dataN.offset..dataN.size .. data0.size
1192 */
1193 /* first correct the data pointers */
0783fcfc 1194 for (i = slot; i < nritems; i++) {
123abc88 1195 u32 ioff = btrfs_item_offset(leaf->items + i);
0783fcfc
CM
1196 btrfs_set_item_offset(leaf->items + i,
1197 ioff - data_size);
1198 }
be0e5c09
CM
1199
1200 /* shift the items */
1201 memmove(leaf->items + slot + 1, leaf->items + slot,
0783fcfc 1202 (nritems - slot) * sizeof(struct btrfs_item));
be0e5c09
CM
1203
1204 /* shift the data */
123abc88
CM
1205 memmove(btrfs_leaf_data(leaf) + data_end - data_size,
1206 btrfs_leaf_data(leaf) +
be0e5c09
CM
1207 data_end, old_data - data_end);
1208 data_end = old_data;
1209 }
62e2749e 1210 /* setup the item for the new data */
e2fa7227
CM
1211 memcpy(&leaf->items[slot].key, &disk_key,
1212 sizeof(struct btrfs_disk_key));
0783fcfc
CM
1213 btrfs_set_item_offset(leaf->items + slot, data_end - data_size);
1214 btrfs_set_item_size(leaf->items + slot, data_size);
7518a238 1215 btrfs_set_header_nritems(&leaf->header, nritems + 1);
d5719762 1216 mark_buffer_dirty(leaf_buf);
aa5d6bed
CM
1217
1218 ret = 0;
8e19f2cd 1219 if (slot == 0)
e089f05c 1220 ret = fixup_low_keys(trans, root, path, &disk_key, 1);
aa5d6bed 1221
123abc88 1222 if (btrfs_leaf_free_space(root, leaf) < 0)
be0e5c09 1223 BUG();
62e2749e 1224 check_leaf(root, path, 0);
ed2ff2cb 1225out:
62e2749e
CM
1226 return ret;
1227}
1228
1229/*
1230 * Given a key and some data, insert an item into the tree.
1231 * This does all the path init required, making room in the tree if needed.
1232 */
e089f05c
CM
1233int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
1234 *root, struct btrfs_key *cpu_key, void *data, u32
1235 data_size)
62e2749e
CM
1236{
1237 int ret = 0;
1238 struct btrfs_path path;
1239 u8 *ptr;
1240
1241 btrfs_init_path(&path);
e089f05c 1242 ret = btrfs_insert_empty_item(trans, root, &path, cpu_key, data_size);
62e2749e 1243 if (!ret) {
e20d96d6
CM
1244 ptr = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]),
1245 path.slots[0], u8);
62e2749e 1246 memcpy(ptr, data, data_size);
d5719762 1247 mark_buffer_dirty(path.nodes[0]);
62e2749e 1248 }
234b63a0 1249 btrfs_release_path(root, &path);
aa5d6bed 1250 return ret;
be0e5c09
CM
1251}
1252
74123bd7 1253/*
5de08d7d 1254 * delete the pointer from a given node.
74123bd7
CM
1255 *
1256 * If the delete empties a node, the node is removed from the tree,
1257 * continuing all the way the root if required. The root is converted into
1258 * a leaf if all the nodes are emptied.
1259 */
e089f05c
CM
1260static int del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1261 struct btrfs_path *path, int level, int slot)
be0e5c09 1262{
234b63a0 1263 struct btrfs_node *node;
e20d96d6 1264 struct buffer_head *parent = path->nodes[level];
7518a238 1265 u32 nritems;
aa5d6bed 1266 int ret = 0;
bb803951 1267 int wret;
be0e5c09 1268
e20d96d6 1269 node = btrfs_buffer_node(parent);
7518a238 1270 nritems = btrfs_header_nritems(&node->header);
bb803951 1271 if (slot != nritems -1) {
123abc88
CM
1272 memmove(node->ptrs + slot, node->ptrs + slot + 1,
1273 sizeof(struct btrfs_key_ptr) * (nritems - slot - 1));
bb803951 1274 }
7518a238
CM
1275 nritems--;
1276 btrfs_set_header_nritems(&node->header, nritems);
1277 if (nritems == 0 && parent == root->node) {
e20d96d6
CM
1278 struct btrfs_header *header = btrfs_buffer_header(root->node);
1279 BUG_ON(btrfs_header_level(header) != 1);
bb803951 1280 /* just turn the root into a leaf and break */
e20d96d6 1281 btrfs_set_header_level(header, 0);
bb803951 1282 } else if (slot == 0) {
e089f05c 1283 wret = fixup_low_keys(trans, root, path, &node->ptrs[0].key,
123abc88 1284 level + 1);
0f70abe2
CM
1285 if (wret)
1286 ret = wret;
be0e5c09 1287 }
d5719762 1288 mark_buffer_dirty(parent);
aa5d6bed 1289 return ret;
be0e5c09
CM
1290}
1291
74123bd7
CM
1292/*
1293 * delete the item at the leaf level in path. If that empties
1294 * the leaf, remove it from the tree
1295 */
e089f05c
CM
1296int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1297 struct btrfs_path *path)
be0e5c09 1298{
be0e5c09 1299 int slot;
234b63a0 1300 struct btrfs_leaf *leaf;
e20d96d6 1301 struct buffer_head *leaf_buf;
be0e5c09
CM
1302 int doff;
1303 int dsize;
aa5d6bed
CM
1304 int ret = 0;
1305 int wret;
7518a238 1306 u32 nritems;
be0e5c09 1307
eb60ceac 1308 leaf_buf = path->nodes[0];
e20d96d6 1309 leaf = btrfs_buffer_leaf(leaf_buf);
4920c9ac 1310 slot = path->slots[0];
0783fcfc
CM
1311 doff = btrfs_item_offset(leaf->items + slot);
1312 dsize = btrfs_item_size(leaf->items + slot);
7518a238 1313 nritems = btrfs_header_nritems(&leaf->header);
be0e5c09 1314
7518a238 1315 if (slot != nritems - 1) {
be0e5c09 1316 int i;
123abc88
CM
1317 int data_end = leaf_data_end(root, leaf);
1318 memmove(btrfs_leaf_data(leaf) + data_end + dsize,
1319 btrfs_leaf_data(leaf) + data_end,
be0e5c09 1320 doff - data_end);
0783fcfc 1321 for (i = slot + 1; i < nritems; i++) {
123abc88 1322 u32 ioff = btrfs_item_offset(leaf->items + i);
0783fcfc
CM
1323 btrfs_set_item_offset(leaf->items + i, ioff + dsize);
1324 }
be0e5c09 1325 memmove(leaf->items + slot, leaf->items + slot + 1,
0783fcfc 1326 sizeof(struct btrfs_item) *
7518a238 1327 (nritems - slot - 1));
be0e5c09 1328 }
7518a238
CM
1329 btrfs_set_header_nritems(&leaf->header, nritems - 1);
1330 nritems--;
74123bd7 1331 /* delete the leaf if we've emptied it */
7518a238 1332 if (nritems == 0) {
eb60ceac 1333 if (leaf_buf == root->node) {
7518a238 1334 btrfs_set_header_level(&leaf->header, 0);
9a8dd150 1335 } else {
e089f05c
CM
1336 clean_tree_block(trans, root, leaf_buf);
1337 wret = del_ptr(trans, root, path, 1, path->slots[1]);
aa5d6bed
CM
1338 if (wret)
1339 ret = wret;
e089f05c 1340 wret = btrfs_free_extent(trans, root,
e20d96d6 1341 leaf_buf->b_blocknr, 1, 1);
0f70abe2
CM
1342 if (wret)
1343 ret = wret;
9a8dd150 1344 }
be0e5c09 1345 } else {
7518a238 1346 int used = leaf_space_used(leaf, 0, nritems);
aa5d6bed 1347 if (slot == 0) {
e089f05c
CM
1348 wret = fixup_low_keys(trans, root, path,
1349 &leaf->items[0].key, 1);
aa5d6bed
CM
1350 if (wret)
1351 ret = wret;
1352 }
aa5d6bed 1353
74123bd7 1354 /* delete the leaf if it is mostly empty */
123abc88 1355 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
be0e5c09
CM
1356 /* push_leaf_left fixes the path.
1357 * make sure the path still points to our leaf
1358 * for possible call to del_ptr below
1359 */
4920c9ac 1360 slot = path->slots[1];
e20d96d6 1361 get_bh(leaf_buf);
e089f05c 1362 wret = push_leaf_left(trans, root, path, 1);
aa5d6bed
CM
1363 if (wret < 0)
1364 ret = wret;
f0930a37 1365 if (path->nodes[0] == leaf_buf &&
7518a238 1366 btrfs_header_nritems(&leaf->header)) {
e089f05c 1367 wret = push_leaf_right(trans, root, path, 1);
aa5d6bed
CM
1368 if (wret < 0)
1369 ret = wret;
1370 }
7518a238 1371 if (btrfs_header_nritems(&leaf->header) == 0) {
e20d96d6 1372 u64 blocknr = leaf_buf->b_blocknr;
e089f05c
CM
1373 clean_tree_block(trans, root, leaf_buf);
1374 wret = del_ptr(trans, root, path, 1, slot);
aa5d6bed
CM
1375 if (wret)
1376 ret = wret;
234b63a0 1377 btrfs_block_release(root, leaf_buf);
e089f05c
CM
1378 wret = btrfs_free_extent(trans, root, blocknr,
1379 1, 1);
0f70abe2
CM
1380 if (wret)
1381 ret = wret;
5de08d7d 1382 } else {
d5719762 1383 mark_buffer_dirty(leaf_buf);
234b63a0 1384 btrfs_block_release(root, leaf_buf);
be0e5c09 1385 }
d5719762
CM
1386 } else {
1387 mark_buffer_dirty(leaf_buf);
be0e5c09
CM
1388 }
1389 }
aa5d6bed 1390 return ret;
be0e5c09
CM
1391}
1392
97571fd0
CM
1393/*
1394 * walk up the tree as far as required to find the next leaf.
0f70abe2
CM
1395 * returns 0 if it found something or 1 if there are no greater leaves.
1396 * returns < 0 on io errors.
97571fd0 1397 */
234b63a0 1398int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
d97e63b6
CM
1399{
1400 int slot;
1401 int level = 1;
1402 u64 blocknr;
e20d96d6
CM
1403 struct buffer_head *c;
1404 struct btrfs_node *c_node;
1405 struct buffer_head *next = NULL;
d97e63b6 1406
234b63a0 1407 while(level < BTRFS_MAX_LEVEL) {
d97e63b6 1408 if (!path->nodes[level])
0f70abe2 1409 return 1;
d97e63b6
CM
1410 slot = path->slots[level] + 1;
1411 c = path->nodes[level];
e20d96d6
CM
1412 c_node = btrfs_buffer_node(c);
1413 if (slot >= btrfs_header_nritems(&c_node->header)) {
d97e63b6
CM
1414 level++;
1415 continue;
1416 }
e20d96d6 1417 blocknr = btrfs_node_blockptr(c_node, slot);
cfaa7295 1418 if (next)
234b63a0 1419 btrfs_block_release(root, next);
d97e63b6
CM
1420 next = read_tree_block(root, blocknr);
1421 break;
1422 }
1423 path->slots[level] = slot;
1424 while(1) {
1425 level--;
1426 c = path->nodes[level];
234b63a0 1427 btrfs_block_release(root, c);
d97e63b6
CM
1428 path->nodes[level] = next;
1429 path->slots[level] = 0;
1430 if (!level)
1431 break;
1d4f8a0c 1432 next = read_tree_block(root,
e20d96d6 1433 btrfs_node_blockptr(btrfs_buffer_node(next), 0));
d97e63b6
CM
1434 }
1435 return 0;
1436}