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