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