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