]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/extent-tree.c
Btrfs: more block allocator work
[mv-sheeva.git] / fs / btrfs / extent-tree.c
1 #include <linux/module.h>
2 #include "ctree.h"
3 #include "disk-io.h"
4 #include "print-tree.h"
5 #include "transaction.h"
6
7 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
8                             *orig_root, u64 num_blocks, u64 search_start, u64
9                             search_end, struct btrfs_key *ins);
10 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
11                                  btrfs_root *extent_root);
12 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
13                                btrfs_root *extent_root);
14
15 static int find_search_start(struct btrfs_root *root, int data)
16 {
17         struct btrfs_block_group_cache *cache[8];
18         struct btrfs_fs_info *info = root->fs_info;
19         u64 used;
20         u64 last;
21         int i;
22         int ret;
23
24         cache[0] = info->block_group_cache;
25         if (!cache[0])
26                 goto find_new;
27         used = btrfs_block_group_used(&cache[0]->item);
28         if (used < (cache[0]->key.offset * 3 / 2))
29                 return 0;
30 find_new:
31         last = 0;
32         while(1) {
33                 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
34                                                  (void **)cache,
35                                                  last, ARRAY_SIZE(cache),
36                                                  BTRFS_BLOCK_GROUP_DIRTY);
37                 if (!ret)
38                         break;
39                 for (i = 0; i < ret; i++) {
40                         used = btrfs_block_group_used(&cache[i]->item);
41                         if (used < (cache[i]->key.offset * 3 / 2)) {
42                                 info->block_group_cache = cache[i];
43                                 cache[i]->last_alloc = cache[i]->first_free;
44                                 return 0;
45                         }
46                         last = cache[i]->key.objectid +
47                                 cache[i]->key.offset - 1;
48                 }
49         }
50         last = 0;
51         while(1) {
52                 ret = radix_tree_gang_lookup(&info->block_group_radix,
53                                                  (void **)cache,
54                                                  last, ARRAY_SIZE(cache));
55                 if (!ret)
56                         break;
57                 for (i = 0; i < ret; i++) {
58                         used = btrfs_block_group_used(&cache[i]->item);
59                         if (used < (cache[i]->key.offset * 3 / 2)) {
60                                 info->block_group_cache = cache[i];
61                                 cache[i]->last_alloc = cache[i]->first_free;
62                                 return 0;
63                         }
64                         last = cache[i]->key.objectid +
65                                 cache[i]->key.offset - 1;
66                 }
67         }
68         info->block_group_cache = NULL;
69         return 0;
70 }
71
72 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
73                                 struct btrfs_root *root,
74                                 u64 blocknr, u64 num_blocks)
75 {
76         struct btrfs_path *path;
77         int ret;
78         struct btrfs_key key;
79         struct btrfs_leaf *l;
80         struct btrfs_extent_item *item;
81         struct btrfs_key ins;
82         u32 refs;
83
84         find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
85                          &ins);
86         path = btrfs_alloc_path();
87         BUG_ON(!path);
88         btrfs_init_path(path);
89         key.objectid = blocknr;
90         key.flags = 0;
91         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
92         key.offset = num_blocks;
93         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
94                                 0, 1);
95         if (ret != 0) {
96 printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
97                 BUG();
98         }
99         BUG_ON(ret != 0);
100         l = btrfs_buffer_leaf(path->nodes[0]);
101         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
102         refs = btrfs_extent_refs(item);
103         btrfs_set_extent_refs(item, refs + 1);
104         btrfs_mark_buffer_dirty(path->nodes[0]);
105
106         btrfs_release_path(root->fs_info->extent_root, path);
107         btrfs_free_path(path);
108         finish_current_insert(trans, root->fs_info->extent_root);
109         del_pending_extents(trans, root->fs_info->extent_root);
110         return 0;
111 }
112
113 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
114                              struct btrfs_root *root, u64 blocknr,
115                              u64 num_blocks, u32 *refs)
116 {
117         struct btrfs_path *path;
118         int ret;
119         struct btrfs_key key;
120         struct btrfs_leaf *l;
121         struct btrfs_extent_item *item;
122
123         path = btrfs_alloc_path();
124         btrfs_init_path(path);
125         key.objectid = blocknr;
126         key.offset = num_blocks;
127         key.flags = 0;
128         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
129         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
130                                 0, 0);
131         if (ret != 0)
132                 BUG();
133         l = btrfs_buffer_leaf(path->nodes[0]);
134         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
135         *refs = btrfs_extent_refs(item);
136         btrfs_release_path(root->fs_info->extent_root, path);
137         btrfs_free_path(path);
138         return 0;
139 }
140
141 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
142                        struct btrfs_root *root)
143 {
144         return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
145 }
146
147 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
148                   struct buffer_head *buf)
149 {
150         u64 blocknr;
151         struct btrfs_node *buf_node;
152         struct btrfs_leaf *buf_leaf;
153         struct btrfs_disk_key *key;
154         struct btrfs_file_extent_item *fi;
155         int i;
156         int leaf;
157         int ret;
158
159         if (!root->ref_cows)
160                 return 0;
161         buf_node = btrfs_buffer_node(buf);
162         leaf = btrfs_is_leaf(buf_node);
163         buf_leaf = btrfs_buffer_leaf(buf);
164         for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
165                 if (leaf) {
166                         key = &buf_leaf->items[i].key;
167                         if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
168                                 continue;
169                         fi = btrfs_item_ptr(buf_leaf, i,
170                                             struct btrfs_file_extent_item);
171                         if (btrfs_file_extent_type(fi) ==
172                             BTRFS_FILE_EXTENT_INLINE)
173                                 continue;
174                         ret = btrfs_inc_extent_ref(trans, root,
175                                     btrfs_file_extent_disk_blocknr(fi),
176                                     btrfs_file_extent_disk_num_blocks(fi));
177                         BUG_ON(ret);
178                 } else {
179                         blocknr = btrfs_node_blockptr(buf_node, i);
180                         ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
181                         BUG_ON(ret);
182                 }
183         }
184         return 0;
185 }
186
187 static int write_one_cache_group(struct btrfs_trans_handle *trans,
188                                  struct btrfs_root *root,
189                                  struct btrfs_path *path,
190                                  struct btrfs_block_group_cache *cache)
191 {
192         int ret;
193         int pending_ret;
194         struct btrfs_root *extent_root = root->fs_info->extent_root;
195         struct btrfs_block_group_item *bi;
196         struct btrfs_key ins;
197
198         find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
199         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
200         BUG_ON(ret);
201         bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
202                             struct btrfs_block_group_item);
203         memcpy(bi, &cache->item, sizeof(*bi));
204         mark_buffer_dirty(path->nodes[0]);
205         btrfs_release_path(extent_root, path);
206
207         finish_current_insert(trans, extent_root);
208         pending_ret = del_pending_extents(trans, extent_root);
209         if (ret)
210                 return ret;
211         if (pending_ret)
212                 return pending_ret;
213         return 0;
214
215 }
216
217 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
218                                     struct btrfs_root *root)
219 {
220         struct btrfs_block_group_cache *cache[8];
221         int ret;
222         int err = 0;
223         int werr = 0;
224         struct radix_tree_root *radix = &root->fs_info->block_group_radix;
225         int i;
226         struct btrfs_path *path;
227
228         path = btrfs_alloc_path();
229         if (!path)
230                 return -ENOMEM;
231
232         while(1) {
233                 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
234                                                  0, ARRAY_SIZE(cache),
235                                                  BTRFS_BLOCK_GROUP_DIRTY);
236                 if (!ret)
237                         break;
238                 for (i = 0; i < ret; i++) {
239                         radix_tree_tag_clear(radix, cache[i]->key.objectid +
240                                              cache[i]->key.offset - 1,
241                                              BTRFS_BLOCK_GROUP_DIRTY);
242                         err = write_one_cache_group(trans, root,
243                                                     path, cache[i]);
244                         if (err)
245                                 werr = err;
246                 }
247         }
248         btrfs_free_path(path);
249         return werr;
250 }
251
252 static int update_block_group(struct btrfs_trans_handle *trans,
253                               struct btrfs_root *root,
254                               u64 blocknr, u64 num, int alloc)
255 {
256         struct btrfs_block_group_cache *cache;
257         struct btrfs_fs_info *info = root->fs_info;
258         u64 total = num;
259         u64 old_val;
260         u64 block_in_group;
261         int ret;
262         while(total) {
263                 ret = radix_tree_gang_lookup(&info->block_group_radix,
264                                              (void **)&cache, blocknr, 1);
265                 if (!ret) {
266                         printk(KERN_CRIT "blocknr %Lu lookup failed\n",
267                                blocknr);
268                         return -1;
269                 }
270                 block_in_group = blocknr - cache->key.objectid;
271                 WARN_ON(block_in_group > cache->key.offset);
272                 radix_tree_tag_set(&info->block_group_radix,
273                                    cache->key.objectid + cache->key.offset - 1,
274                                    BTRFS_BLOCK_GROUP_DIRTY);
275
276                 old_val = btrfs_block_group_used(&cache->item);
277                 num = min(total, cache->key.offset - block_in_group);
278                 total -= num;
279                 blocknr += num;
280                 if (alloc) {
281                         old_val += num;
282                         if (blocknr > cache->last_alloc)
283                                 cache->last_alloc = blocknr;
284                 } else {
285                         old_val -= num;
286                         if (blocknr < cache->first_free)
287                                 cache->first_free = blocknr;
288                 }
289                 btrfs_set_block_group_used(&cache->item, old_val);
290         }
291         return 0;
292 }
293
294 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
295                                btrfs_root *root)
296 {
297         unsigned long gang[8];
298         u64 first = 0;
299         int ret;
300         int i;
301         struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
302
303         while(1) {
304                 ret = find_first_radix_bit(pinned_radix, gang,
305                                            ARRAY_SIZE(gang));
306                 if (!ret)
307                         break;
308                 if (!first)
309                         first = gang[0];
310                 for (i = 0; i < ret; i++) {
311                         clear_radix_bit(pinned_radix, gang[i]);
312                 }
313         }
314         root->fs_info->block_group_cache = NULL;
315         return 0;
316 }
317
318 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
319                                  btrfs_root *extent_root)
320 {
321         struct btrfs_key ins;
322         struct btrfs_extent_item extent_item;
323         int i;
324         int ret;
325         u64 super_blocks_used;
326         struct btrfs_fs_info *info = extent_root->fs_info;
327
328         btrfs_set_extent_refs(&extent_item, 1);
329         ins.offset = 1;
330         ins.flags = 0;
331         btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
332         btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
333
334         for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
335                 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
336                 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
337                 btrfs_set_super_blocks_used(info->disk_super,
338                                             super_blocks_used + 1);
339                 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
340                                         sizeof(extent_item));
341                 BUG_ON(ret);
342         }
343         extent_root->fs_info->extent_tree_insert_nr = 0;
344         extent_root->fs_info->extent_tree_prealloc_nr = 0;
345         return 0;
346 }
347
348 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
349 {
350         int err;
351         struct btrfs_header *header;
352         struct buffer_head *bh;
353
354         if (!pending) {
355                 bh = btrfs_find_tree_block(root, blocknr);
356                 if (bh) {
357                         if (buffer_uptodate(bh)) {
358                                 u64 transid =
359                                     root->fs_info->running_transaction->transid;
360                                 header = btrfs_buffer_header(bh);
361                                 if (btrfs_header_generation(header) ==
362                                     transid) {
363                                         btrfs_block_release(root, bh);
364                                         return 0;
365                                 }
366                         }
367                         btrfs_block_release(root, bh);
368                 }
369                 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
370         } else {
371                 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
372         }
373         BUG_ON(err);
374         return 0;
375 }
376
377 /*
378  * remove an extent from the root, returns 0 on success
379  */
380 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
381                          *root, u64 blocknr, u64 num_blocks, int pin)
382 {
383         struct btrfs_path *path;
384         struct btrfs_key key;
385         struct btrfs_fs_info *info = root->fs_info;
386         struct btrfs_root *extent_root = info->extent_root;
387         int ret;
388         struct btrfs_extent_item *ei;
389         struct btrfs_key ins;
390         u32 refs;
391
392         key.objectid = blocknr;
393         key.flags = 0;
394         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
395         key.offset = num_blocks;
396
397         find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
398         path = btrfs_alloc_path();
399         BUG_ON(!path);
400         btrfs_init_path(path);
401
402         ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
403         if (ret) {
404                 printk("failed to find %Lu\n", key.objectid);
405                 btrfs_print_tree(extent_root, extent_root->node);
406                 printk("failed to find %Lu\n", key.objectid);
407                 BUG();
408         }
409         ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
410                             struct btrfs_extent_item);
411         BUG_ON(ei->refs == 0);
412         refs = btrfs_extent_refs(ei) - 1;
413         btrfs_set_extent_refs(ei, refs);
414         btrfs_mark_buffer_dirty(path->nodes[0]);
415         if (refs == 0) {
416                 u64 super_blocks_used;
417
418                 if (pin) {
419                         ret = pin_down_block(root, blocknr, 0);
420                         BUG_ON(ret);
421                 }
422
423                 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
424                 btrfs_set_super_blocks_used(info->disk_super,
425                                             super_blocks_used - num_blocks);
426                 ret = btrfs_del_item(trans, extent_root, path);
427                 if (ret)
428                         BUG();
429                 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
430                 BUG_ON(ret);
431         }
432         btrfs_release_path(extent_root, path);
433         btrfs_free_path(path);
434         finish_current_insert(trans, extent_root);
435         return ret;
436 }
437
438 /*
439  * find all the blocks marked as pending in the radix tree and remove
440  * them from the extent map
441  */
442 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
443                                btrfs_root *extent_root)
444 {
445         int ret;
446         int wret;
447         int err = 0;
448         unsigned long gang[4];
449         int i;
450         struct radix_tree_root *pending_radix;
451         struct radix_tree_root *pinned_radix;
452
453         pending_radix = &extent_root->fs_info->pending_del_radix;
454         pinned_radix = &extent_root->fs_info->pinned_radix;
455
456         while(1) {
457                 ret = find_first_radix_bit(pending_radix, gang,
458                                            ARRAY_SIZE(gang));
459                 if (!ret)
460                         break;
461                 for (i = 0; i < ret; i++) {
462                         wret = set_radix_bit(pinned_radix, gang[i]);
463                         BUG_ON(wret);
464                         wret = clear_radix_bit(pending_radix, gang[i]);
465                         BUG_ON(wret);
466                         wret = __free_extent(trans, extent_root,
467                                              gang[i], 1, 0);
468                         if (wret)
469                                 err = wret;
470                 }
471         }
472         return err;
473 }
474
475 /*
476  * remove an extent from the root, returns 0 on success
477  */
478 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
479                       *root, u64 blocknr, u64 num_blocks, int pin)
480 {
481         struct btrfs_root *extent_root = root->fs_info->extent_root;
482         int pending_ret;
483         int ret;
484
485         if (root == extent_root) {
486                 pin_down_block(root, blocknr, 1);
487                 return 0;
488         }
489         ret = __free_extent(trans, root, blocknr, num_blocks, pin);
490         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
491         return ret ? ret : pending_ret;
492 }
493
494 /*
495  * walks the btree of allocated extents and find a hole of a given size.
496  * The key ins is changed to record the hole:
497  * ins->objectid == block start
498  * ins->flags = BTRFS_EXTENT_ITEM_KEY
499  * ins->offset == number of blocks
500  * Any available blocks before search_start are skipped.
501  */
502 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
503                             *orig_root, u64 num_blocks, u64 search_start, u64
504                             search_end, struct btrfs_key *ins)
505 {
506         struct btrfs_path *path;
507         struct btrfs_key key;
508         int ret;
509         u64 hole_size = 0;
510         int slot = 0;
511         u64 last_block = 0;
512         u64 test_block;
513         int start_found;
514         struct btrfs_leaf *l;
515         struct btrfs_root * root = orig_root->fs_info->extent_root;
516         struct btrfs_fs_info *info = root->fs_info;
517         int total_needed = num_blocks;
518         int total_found = 0;
519         int fill_prealloc = 0;
520         int level;
521
522         path = btrfs_alloc_path();
523         ins->flags = 0;
524         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
525
526         level = btrfs_header_level(btrfs_buffer_header(root->node));
527         if (num_blocks == 0) {
528                 fill_prealloc = 1;
529                 num_blocks = 1;
530                 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
531         }
532         find_search_start(root, 0);
533         if (info->block_group_cache &&
534             info->block_group_cache->last_alloc > search_start)
535                 search_start = info->block_group_cache->last_alloc;
536
537 check_failed:
538         btrfs_init_path(path);
539         ins->objectid = search_start;
540         ins->offset = 0;
541         start_found = 0;
542         ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
543         if (ret < 0)
544                 goto error;
545
546         if (path->slots[0] > 0)
547                 path->slots[0]--;
548
549         while (1) {
550                 l = btrfs_buffer_leaf(path->nodes[0]);
551                 slot = path->slots[0];
552                 if (slot >= btrfs_header_nritems(&l->header)) {
553                         if (fill_prealloc) {
554                                 info->extent_tree_prealloc_nr = 0;
555                                 total_found = 0;
556                         }
557                         ret = btrfs_next_leaf(root, path);
558                         if (ret == 0)
559                                 continue;
560                         if (ret < 0)
561                                 goto error;
562                         if (!start_found) {
563                                 ins->objectid = search_start;
564                                 ins->offset = (u64)-1 - search_start;
565                                 start_found = 1;
566                                 goto check_pending;
567                         }
568                         ins->objectid = last_block > search_start ?
569                                         last_block : search_start;
570                         ins->offset = (u64)-1 - ins->objectid;
571                         goto check_pending;
572                 }
573                 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
574                 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
575                         goto next;
576                 if (key.objectid >= search_start) {
577                         if (start_found) {
578                                 if (last_block < search_start)
579                                         last_block = search_start;
580                                 hole_size = key.objectid - last_block;
581                                 if (hole_size > num_blocks) {
582                                         ins->objectid = last_block;
583                                         ins->offset = hole_size;
584                                         goto check_pending;
585                                 }
586                         }
587                 }
588                 start_found = 1;
589                 last_block = key.objectid + key.offset;
590 next:
591                 path->slots[0]++;
592         }
593         // FIXME -ENOSPC
594 check_pending:
595         /* we have to make sure we didn't find an extent that has already
596          * been allocated by the map tree or the original allocation
597          */
598         btrfs_release_path(root, path);
599         BUG_ON(ins->objectid < search_start);
600         for (test_block = ins->objectid;
601              test_block < ins->objectid + num_blocks; test_block++) {
602                 if (test_radix_bit(&info->pinned_radix, test_block)) {
603                         search_start = test_block + 1;
604                         goto check_failed;
605                 }
606         }
607         if (!fill_prealloc && info->extent_tree_insert_nr) {
608                 u64 last =
609                   info->extent_tree_insert[info->extent_tree_insert_nr - 1];
610                 if (ins->objectid + num_blocks >
611                     info->extent_tree_insert[0] &&
612                     ins->objectid <= last) {
613                         search_start = last + 1;
614                         WARN_ON(1);
615                         goto check_failed;
616                 }
617         }
618         if (!fill_prealloc && info->extent_tree_prealloc_nr) {
619                 u64 first =
620                   info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
621                 if (ins->objectid + num_blocks > first &&
622                     ins->objectid <= info->extent_tree_prealloc[0]) {
623                         search_start = info->extent_tree_prealloc[0] + 1;
624                         WARN_ON(1);
625                         goto check_failed;
626                 }
627         }
628         if (fill_prealloc) {
629                 int nr;
630                 test_block = ins->objectid;
631                 while(test_block < ins->objectid + ins->offset &&
632                       total_found < total_needed) {
633                         nr = total_needed - total_found - 1;
634                         BUG_ON(nr < 0);
635                         info->extent_tree_prealloc[nr] = test_block;
636                         total_found++;
637                         test_block++;
638                 }
639                 if (total_found < total_needed) {
640                         search_start = test_block;
641                         goto check_failed;
642                 }
643                 info->extent_tree_prealloc_nr = total_found;
644         }
645         ret = radix_tree_gang_lookup(&info->block_group_radix,
646                                      (void **)&info->block_group_cache,
647                                      ins->objectid, 1);
648         if (ret) {
649                 info->block_group_cache->last_alloc = ins->objectid;
650         }
651         ins->offset = num_blocks;
652         btrfs_free_path(path);
653         return 0;
654 error:
655         btrfs_release_path(root, path);
656         btrfs_free_path(path);
657         return ret;
658 }
659 /*
660  * finds a free extent and does all the dirty work required for allocation
661  * returns the key for the extent through ins, and a tree buffer for
662  * the first block of the extent through buf.
663  *
664  * returns 0 if everything worked, non-zero otherwise.
665  */
666 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
667                        struct btrfs_root *root, u64 owner,
668                        u64 num_blocks, u64 search_start,
669                        u64 search_end, struct btrfs_key *ins)
670 {
671         int ret;
672         int pending_ret;
673         u64 super_blocks_used;
674         struct btrfs_fs_info *info = root->fs_info;
675         struct btrfs_root *extent_root = info->extent_root;
676         struct btrfs_extent_item extent_item;
677         struct btrfs_key prealloc_key;
678
679         btrfs_set_extent_refs(&extent_item, 1);
680         btrfs_set_extent_owner(&extent_item, owner);
681
682         if (root == extent_root) {
683                 int nr;
684                 BUG_ON(info->extent_tree_prealloc_nr == 0);
685                 BUG_ON(num_blocks != 1);
686                 ins->offset = 1;
687                 info->extent_tree_prealloc_nr--;
688                 nr = info->extent_tree_prealloc_nr;
689                 ins->objectid = info->extent_tree_prealloc[nr];
690                 info->extent_tree_insert[info->extent_tree_insert_nr++] =
691                         ins->objectid;
692                 ret = update_block_group(trans, root,
693                                          ins->objectid, ins->offset, 1);
694                 BUG_ON(ret);
695                 return 0;
696         }
697         /* do the real allocation */
698         ret = find_free_extent(trans, root, num_blocks, search_start,
699                                search_end, ins);
700         if (ret)
701                 return ret;
702
703         /* then do prealloc for the extent tree */
704         ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
705                                search_end, &prealloc_key);
706         if (ret)
707                 return ret;
708
709         super_blocks_used = btrfs_super_blocks_used(info->disk_super);
710         btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
711                                     num_blocks);
712         ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
713                                 sizeof(extent_item));
714
715         finish_current_insert(trans, extent_root);
716         pending_ret = del_pending_extents(trans, extent_root);
717         if (ret)
718                 return ret;
719         if (pending_ret)
720                 return pending_ret;
721         ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
722         return 0;
723 }
724
725 /*
726  * helper function to allocate a block for a given tree
727  * returns the tree buffer or NULL.
728  */
729 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
730                                            struct btrfs_root *root)
731 {
732         struct btrfs_key ins;
733         int ret;
734         struct buffer_head *buf;
735
736         ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
737                                  1, 0, (unsigned long)-1, &ins);
738         if (ret) {
739                 BUG();
740                 return NULL;
741         }
742         BUG_ON(ret);
743         buf = btrfs_find_create_tree_block(root, ins.objectid);
744         set_buffer_uptodate(buf);
745         return buf;
746 }
747
748 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
749                          struct btrfs_root *root, struct buffer_head *cur)
750 {
751         struct btrfs_disk_key *key;
752         struct btrfs_leaf *leaf;
753         struct btrfs_file_extent_item *fi;
754         int i;
755         int nritems;
756         int ret;
757
758         BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
759         leaf = btrfs_buffer_leaf(cur);
760         nritems = btrfs_header_nritems(&leaf->header);
761         for (i = 0; i < nritems; i++) {
762                 key = &leaf->items[i].key;
763                 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
764                         continue;
765                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
766                 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
767                         continue;
768                 /*
769                  * FIXME make sure to insert a trans record that
770                  * repeats the snapshot del on crash
771                  */
772                 ret = btrfs_free_extent(trans, root,
773                                         btrfs_file_extent_disk_blocknr(fi),
774                                         btrfs_file_extent_disk_num_blocks(fi),
775                                         0);
776                 BUG_ON(ret);
777         }
778         return 0;
779 }
780
781 /*
782  * helper function for drop_snapshot, this walks down the tree dropping ref
783  * counts as it goes.
784  */
785 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
786                           *root, struct btrfs_path *path, int *level)
787 {
788         struct buffer_head *next;
789         struct buffer_head *cur;
790         u64 blocknr;
791         int ret;
792         u32 refs;
793
794         WARN_ON(*level < 0);
795         WARN_ON(*level >= BTRFS_MAX_LEVEL);
796         ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
797                                1, &refs);
798         BUG_ON(ret);
799         if (refs > 1)
800                 goto out;
801         /*
802          * walk down to the last node level and free all the leaves
803          */
804         while(*level >= 0) {
805                 WARN_ON(*level < 0);
806                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
807                 cur = path->nodes[*level];
808                 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
809                         WARN_ON(1);
810                 if (path->slots[*level] >=
811                     btrfs_header_nritems(btrfs_buffer_header(cur)))
812                         break;
813                 if (*level == 0) {
814                         ret = drop_leaf_ref(trans, root, cur);
815                         BUG_ON(ret);
816                         break;
817                 }
818                 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
819                                               path->slots[*level]);
820                 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
821                 BUG_ON(ret);
822                 if (refs != 1) {
823                         path->slots[*level]++;
824                         ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
825                         BUG_ON(ret);
826                         continue;
827                 }
828                 next = read_tree_block(root, blocknr);
829                 WARN_ON(*level <= 0);
830                 if (path->nodes[*level-1])
831                         btrfs_block_release(root, path->nodes[*level-1]);
832                 path->nodes[*level-1] = next;
833                 *level = btrfs_header_level(btrfs_buffer_header(next));
834                 path->slots[*level] = 0;
835         }
836 out:
837         WARN_ON(*level < 0);
838         WARN_ON(*level >= BTRFS_MAX_LEVEL);
839         ret = btrfs_free_extent(trans, root,
840                                 bh_blocknr(path->nodes[*level]), 1, 1);
841         btrfs_block_release(root, path->nodes[*level]);
842         path->nodes[*level] = NULL;
843         *level += 1;
844         BUG_ON(ret);
845         return 0;
846 }
847
848 /*
849  * helper for dropping snapshots.  This walks back up the tree in the path
850  * to find the first node higher up where we haven't yet gone through
851  * all the slots
852  */
853 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
854                         *root, struct btrfs_path *path, int *level)
855 {
856         int i;
857         int slot;
858         int ret;
859         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
860                 slot = path->slots[i];
861                 if (slot < btrfs_header_nritems(
862                     btrfs_buffer_header(path->nodes[i])) - 1) {
863                         path->slots[i]++;
864                         *level = i;
865                         return 0;
866                 } else {
867                         ret = btrfs_free_extent(trans, root,
868                                                 bh_blocknr(path->nodes[*level]),
869                                                 1, 1);
870                         BUG_ON(ret);
871                         btrfs_block_release(root, path->nodes[*level]);
872                         path->nodes[*level] = NULL;
873                         *level = i + 1;
874                 }
875         }
876         return 1;
877 }
878
879 /*
880  * drop the reference count on the tree rooted at 'snap'.  This traverses
881  * the tree freeing any blocks that have a ref count of zero after being
882  * decremented.
883  */
884 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
885                         *root, struct buffer_head *snap)
886 {
887         int ret = 0;
888         int wret;
889         int level;
890         struct btrfs_path *path;
891         int i;
892         int orig_level;
893
894         path = btrfs_alloc_path();
895         BUG_ON(!path);
896         btrfs_init_path(path);
897
898         level = btrfs_header_level(btrfs_buffer_header(snap));
899         orig_level = level;
900         path->nodes[level] = snap;
901         path->slots[level] = 0;
902         while(1) {
903                 wret = walk_down_tree(trans, root, path, &level);
904                 if (wret > 0)
905                         break;
906                 if (wret < 0)
907                         ret = wret;
908
909                 wret = walk_up_tree(trans, root, path, &level);
910                 if (wret > 0)
911                         break;
912                 if (wret < 0)
913                         ret = wret;
914         }
915         for (i = 0; i <= orig_level; i++) {
916                 if (path->nodes[i]) {
917                         btrfs_block_release(root, path->nodes[i]);
918                 }
919         }
920         btrfs_free_path(path);
921         return ret;
922 }
923
924 int btrfs_free_block_groups(struct btrfs_fs_info *info)
925 {
926         int ret;
927         struct btrfs_block_group_cache *cache[8];
928         int i;
929
930         while(1) {
931                 ret = radix_tree_gang_lookup(&info->block_group_radix,
932                                              (void **)cache, 0,
933                                              ARRAY_SIZE(cache));
934                 if (!ret)
935                         break;
936                 for (i = 0; i < ret; i++) {
937                         radix_tree_delete(&info->block_group_radix,
938                                           cache[i]->key.objectid +
939                                           cache[i]->key.offset - 1);
940                         kfree(cache[i]);
941                 }
942         }
943         return 0;
944 }
945
946 int btrfs_read_block_groups(struct btrfs_root *root)
947 {
948         struct btrfs_path *path;
949         int ret;
950         int err = 0;
951         struct btrfs_block_group_item *bi;
952         struct btrfs_block_group_cache *cache;
953         struct btrfs_key key;
954         struct btrfs_key found_key;
955         struct btrfs_leaf *leaf;
956         u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
957
958         root = root->fs_info->extent_root;
959         key.objectid = 0;
960         key.offset = group_size_blocks;
961         key.flags = 0;
962         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
963
964         path = btrfs_alloc_path();
965         if (!path)
966                 return -ENOMEM;
967
968         while(1) {
969                 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
970                                         &key, path, 0, 0);
971                 if (ret != 0) {
972                         err = ret;
973                         break;
974                 }
975                 leaf = btrfs_buffer_leaf(path->nodes[0]);
976                 btrfs_disk_key_to_cpu(&found_key,
977                                       &leaf->items[path->slots[0]].key);
978                 cache = kmalloc(sizeof(*cache), GFP_NOFS);
979                 if (!cache) {
980                         err = -1;
981                         break;
982                 }
983                 bi = btrfs_item_ptr(leaf, path->slots[0],
984                                     struct btrfs_block_group_item);
985                 memcpy(&cache->item, bi, sizeof(*bi));
986                 memcpy(&cache->key, &found_key, sizeof(found_key));
987                 cache->last_alloc = 0;
988                 cache->first_free = 0;
989                 key.objectid = found_key.objectid + found_key.offset;
990                 btrfs_release_path(root, path);
991                 ret = radix_tree_insert(&root->fs_info->block_group_radix,
992                                         found_key.objectid +
993                                         found_key.offset - 1,
994                                         (void *)cache);
995                 BUG_ON(ret);
996                 if (key.objectid >=
997                     btrfs_super_total_blocks(root->fs_info->disk_super))
998                         break;
999         }
1000
1001         btrfs_free_path(path);
1002         return 0;
1003 }