]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/extent-tree.c
Btrfs: allocator tweaks
[linux-2.6-omap-h63xx.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         if (root->fs_info->block_group_cache) {
315                 root->fs_info->block_group_cache->last_alloc =
316                         root->fs_info->block_group_cache->first_free;
317         }
318         return 0;
319 }
320
321 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
322                                  btrfs_root *extent_root)
323 {
324         struct btrfs_key ins;
325         struct btrfs_extent_item extent_item;
326         int i;
327         int ret;
328         u64 super_blocks_used;
329         struct btrfs_fs_info *info = extent_root->fs_info;
330
331         btrfs_set_extent_refs(&extent_item, 1);
332         ins.offset = 1;
333         ins.flags = 0;
334         btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
335         btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
336
337         for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
338                 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
339                 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
340                 btrfs_set_super_blocks_used(info->disk_super,
341                                             super_blocks_used + 1);
342                 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
343                                         sizeof(extent_item));
344                 BUG_ON(ret);
345         }
346         extent_root->fs_info->extent_tree_insert_nr = 0;
347         extent_root->fs_info->extent_tree_prealloc_nr = 0;
348         return 0;
349 }
350
351 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
352 {
353         int err;
354         struct btrfs_header *header;
355         struct buffer_head *bh;
356
357         if (!pending) {
358                 bh = btrfs_find_tree_block(root, blocknr);
359                 if (bh) {
360                         if (buffer_uptodate(bh)) {
361                                 u64 transid =
362                                     root->fs_info->running_transaction->transid;
363                                 header = btrfs_buffer_header(bh);
364                                 if (btrfs_header_generation(header) ==
365                                     transid) {
366                                         btrfs_block_release(root, bh);
367                                         return 0;
368                                 }
369                         }
370                         btrfs_block_release(root, bh);
371                 }
372                 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
373         } else {
374                 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
375         }
376         BUG_ON(err);
377         return 0;
378 }
379
380 /*
381  * remove an extent from the root, returns 0 on success
382  */
383 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
384                          *root, u64 blocknr, u64 num_blocks, int pin)
385 {
386         struct btrfs_path *path;
387         struct btrfs_key key;
388         struct btrfs_fs_info *info = root->fs_info;
389         struct btrfs_root *extent_root = info->extent_root;
390         int ret;
391         struct btrfs_extent_item *ei;
392         struct btrfs_key ins;
393         u32 refs;
394
395         key.objectid = blocknr;
396         key.flags = 0;
397         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
398         key.offset = num_blocks;
399
400         find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
401         path = btrfs_alloc_path();
402         BUG_ON(!path);
403         btrfs_init_path(path);
404
405         ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
406         if (ret) {
407                 printk("failed to find %Lu\n", key.objectid);
408                 btrfs_print_tree(extent_root, extent_root->node);
409                 printk("failed to find %Lu\n", key.objectid);
410                 BUG();
411         }
412         ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
413                             struct btrfs_extent_item);
414         BUG_ON(ei->refs == 0);
415         refs = btrfs_extent_refs(ei) - 1;
416         btrfs_set_extent_refs(ei, refs);
417         btrfs_mark_buffer_dirty(path->nodes[0]);
418         if (refs == 0) {
419                 u64 super_blocks_used;
420
421                 if (pin) {
422                         ret = pin_down_block(root, blocknr, 0);
423                         BUG_ON(ret);
424                 }
425
426                 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
427                 btrfs_set_super_blocks_used(info->disk_super,
428                                             super_blocks_used - num_blocks);
429                 ret = btrfs_del_item(trans, extent_root, path);
430                 if (ret)
431                         BUG();
432                 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
433                 BUG_ON(ret);
434         }
435         btrfs_release_path(extent_root, path);
436         btrfs_free_path(path);
437         finish_current_insert(trans, extent_root);
438         return ret;
439 }
440
441 /*
442  * find all the blocks marked as pending in the radix tree and remove
443  * them from the extent map
444  */
445 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
446                                btrfs_root *extent_root)
447 {
448         int ret;
449         int wret;
450         int err = 0;
451         unsigned long gang[4];
452         int i;
453         struct radix_tree_root *pending_radix;
454         struct radix_tree_root *pinned_radix;
455
456         pending_radix = &extent_root->fs_info->pending_del_radix;
457         pinned_radix = &extent_root->fs_info->pinned_radix;
458
459         while(1) {
460                 ret = find_first_radix_bit(pending_radix, gang,
461                                            ARRAY_SIZE(gang));
462                 if (!ret)
463                         break;
464                 for (i = 0; i < ret; i++) {
465                         wret = set_radix_bit(pinned_radix, gang[i]);
466                         BUG_ON(wret);
467                         wret = clear_radix_bit(pending_radix, gang[i]);
468                         BUG_ON(wret);
469                         wret = __free_extent(trans, extent_root,
470                                              gang[i], 1, 0);
471                         if (wret)
472                                 err = wret;
473                 }
474         }
475         return err;
476 }
477
478 /*
479  * remove an extent from the root, returns 0 on success
480  */
481 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
482                       *root, u64 blocknr, u64 num_blocks, int pin)
483 {
484         struct btrfs_root *extent_root = root->fs_info->extent_root;
485         int pending_ret;
486         int ret;
487
488         if (root == extent_root) {
489                 pin_down_block(root, blocknr, 1);
490                 return 0;
491         }
492         ret = __free_extent(trans, root, blocknr, num_blocks, pin);
493         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
494         return ret ? ret : pending_ret;
495 }
496
497 /*
498  * walks the btree of allocated extents and find a hole of a given size.
499  * The key ins is changed to record the hole:
500  * ins->objectid == block start
501  * ins->flags = BTRFS_EXTENT_ITEM_KEY
502  * ins->offset == number of blocks
503  * Any available blocks before search_start are skipped.
504  */
505 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
506                             *orig_root, u64 num_blocks, u64 search_start, u64
507                             search_end, struct btrfs_key *ins)
508 {
509         struct btrfs_path *path;
510         struct btrfs_key key;
511         int ret;
512         u64 hole_size = 0;
513         int slot = 0;
514         u64 last_block = 0;
515         u64 test_block;
516         int start_found;
517         struct btrfs_leaf *l;
518         struct btrfs_root * root = orig_root->fs_info->extent_root;
519         struct btrfs_fs_info *info = root->fs_info;
520         int total_needed = num_blocks;
521         int total_found = 0;
522         int fill_prealloc = 0;
523         int level;
524
525         path = btrfs_alloc_path();
526         ins->flags = 0;
527         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
528
529         level = btrfs_header_level(btrfs_buffer_header(root->node));
530         if (num_blocks == 0) {
531                 fill_prealloc = 1;
532                 num_blocks = 1;
533                 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
534         }
535         find_search_start(root, 0);
536         if (info->block_group_cache &&
537             info->block_group_cache->last_alloc > search_start)
538                 search_start = info->block_group_cache->last_alloc;
539
540 check_failed:
541         btrfs_init_path(path);
542         ins->objectid = search_start;
543         ins->offset = 0;
544         start_found = 0;
545         ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
546         if (ret < 0)
547                 goto error;
548
549         if (path->slots[0] > 0)
550                 path->slots[0]--;
551
552         while (1) {
553                 l = btrfs_buffer_leaf(path->nodes[0]);
554                 slot = path->slots[0];
555                 if (slot >= btrfs_header_nritems(&l->header)) {
556                         if (fill_prealloc) {
557                                 info->extent_tree_prealloc_nr = 0;
558                                 total_found = 0;
559                         }
560                         ret = btrfs_next_leaf(root, path);
561                         if (ret == 0)
562                                 continue;
563                         if (ret < 0)
564                                 goto error;
565                         if (!start_found) {
566                                 ins->objectid = search_start;
567                                 ins->offset = (u64)-1 - search_start;
568                                 start_found = 1;
569                                 goto check_pending;
570                         }
571                         ins->objectid = last_block > search_start ?
572                                         last_block : search_start;
573                         ins->offset = (u64)-1 - ins->objectid;
574                         goto check_pending;
575                 }
576                 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
577                 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
578                         goto next;
579                 if (key.objectid >= search_start) {
580                         if (start_found) {
581                                 if (last_block < search_start)
582                                         last_block = search_start;
583                                 hole_size = key.objectid - last_block;
584                                 if (hole_size >= num_blocks) {
585                                         ins->objectid = last_block;
586                                         ins->offset = hole_size;
587                                         goto check_pending;
588                                 }
589                         }
590                 }
591                 start_found = 1;
592                 last_block = key.objectid + key.offset;
593 next:
594                 path->slots[0]++;
595         }
596         // FIXME -ENOSPC
597 check_pending:
598         /* we have to make sure we didn't find an extent that has already
599          * been allocated by the map tree or the original allocation
600          */
601         btrfs_release_path(root, path);
602         BUG_ON(ins->objectid < search_start);
603         for (test_block = ins->objectid;
604              test_block < ins->objectid + num_blocks; test_block++) {
605                 if (test_radix_bit(&info->pinned_radix, test_block)) {
606                         search_start = test_block + 1;
607                         goto check_failed;
608                 }
609         }
610         if (!fill_prealloc && info->extent_tree_insert_nr) {
611                 u64 last =
612                   info->extent_tree_insert[info->extent_tree_insert_nr - 1];
613                 if (ins->objectid + num_blocks >
614                     info->extent_tree_insert[0] &&
615                     ins->objectid <= last) {
616                         search_start = last + 1;
617                         WARN_ON(1);
618                         goto check_failed;
619                 }
620         }
621         if (!fill_prealloc && info->extent_tree_prealloc_nr) {
622                 u64 first =
623                   info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
624                 if (ins->objectid + num_blocks > first &&
625                     ins->objectid <= info->extent_tree_prealloc[0]) {
626                         search_start = info->extent_tree_prealloc[0] + 1;
627                         WARN_ON(1);
628                         goto check_failed;
629                 }
630         }
631         if (fill_prealloc) {
632                 int nr;
633                 test_block = ins->objectid;
634                 while(test_block < ins->objectid + ins->offset &&
635                       total_found < total_needed) {
636                         nr = total_needed - total_found - 1;
637                         BUG_ON(nr < 0);
638                         info->extent_tree_prealloc[nr] = test_block;
639                         total_found++;
640                         test_block++;
641                 }
642                 if (total_found < total_needed) {
643                         search_start = test_block;
644                         goto check_failed;
645                 }
646                 info->extent_tree_prealloc_nr = total_found;
647         }
648         ret = radix_tree_gang_lookup(&info->block_group_radix,
649                                      (void **)&info->block_group_cache,
650                                      ins->objectid, 1);
651         if (ret) {
652                 info->block_group_cache->last_alloc = ins->objectid;
653         }
654         ins->offset = num_blocks;
655         btrfs_free_path(path);
656         return 0;
657 error:
658         btrfs_release_path(root, path);
659         btrfs_free_path(path);
660         return ret;
661 }
662 /*
663  * finds a free extent and does all the dirty work required for allocation
664  * returns the key for the extent through ins, and a tree buffer for
665  * the first block of the extent through buf.
666  *
667  * returns 0 if everything worked, non-zero otherwise.
668  */
669 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
670                        struct btrfs_root *root, u64 owner,
671                        u64 num_blocks, u64 search_start,
672                        u64 search_end, struct btrfs_key *ins)
673 {
674         int ret;
675         int pending_ret;
676         u64 super_blocks_used;
677         struct btrfs_fs_info *info = root->fs_info;
678         struct btrfs_root *extent_root = info->extent_root;
679         struct btrfs_extent_item extent_item;
680         struct btrfs_key prealloc_key;
681
682         btrfs_set_extent_refs(&extent_item, 1);
683         btrfs_set_extent_owner(&extent_item, owner);
684
685         if (root == extent_root) {
686                 int nr;
687                 BUG_ON(info->extent_tree_prealloc_nr == 0);
688                 BUG_ON(num_blocks != 1);
689                 ins->offset = 1;
690                 info->extent_tree_prealloc_nr--;
691                 nr = info->extent_tree_prealloc_nr;
692                 ins->objectid = info->extent_tree_prealloc[nr];
693                 info->extent_tree_insert[info->extent_tree_insert_nr++] =
694                         ins->objectid;
695                 ret = update_block_group(trans, root,
696                                          ins->objectid, ins->offset, 1);
697                 BUG_ON(ret);
698                 return 0;
699         }
700         /* do the real allocation */
701         ret = find_free_extent(trans, root, num_blocks, search_start,
702                                search_end, ins);
703         if (ret)
704                 return ret;
705
706         /* then do prealloc for the extent tree */
707         ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
708                                search_end, &prealloc_key);
709         if (ret)
710                 return ret;
711
712         super_blocks_used = btrfs_super_blocks_used(info->disk_super);
713         btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
714                                     num_blocks);
715         ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
716                                 sizeof(extent_item));
717
718         finish_current_insert(trans, extent_root);
719         pending_ret = del_pending_extents(trans, extent_root);
720         if (ret)
721                 return ret;
722         if (pending_ret)
723                 return pending_ret;
724         ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
725         return 0;
726 }
727
728 /*
729  * helper function to allocate a block for a given tree
730  * returns the tree buffer or NULL.
731  */
732 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
733                                            struct btrfs_root *root)
734 {
735         struct btrfs_key ins;
736         int ret;
737         struct buffer_head *buf;
738
739         ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
740                                  1, 0, (unsigned long)-1, &ins);
741         if (ret) {
742                 BUG();
743                 return NULL;
744         }
745         BUG_ON(ret);
746         buf = btrfs_find_create_tree_block(root, ins.objectid);
747         set_buffer_uptodate(buf);
748         return buf;
749 }
750
751 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
752                          struct btrfs_root *root, struct buffer_head *cur)
753 {
754         struct btrfs_disk_key *key;
755         struct btrfs_leaf *leaf;
756         struct btrfs_file_extent_item *fi;
757         int i;
758         int nritems;
759         int ret;
760
761         BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
762         leaf = btrfs_buffer_leaf(cur);
763         nritems = btrfs_header_nritems(&leaf->header);
764         for (i = 0; i < nritems; i++) {
765                 key = &leaf->items[i].key;
766                 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
767                         continue;
768                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
769                 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
770                         continue;
771                 /*
772                  * FIXME make sure to insert a trans record that
773                  * repeats the snapshot del on crash
774                  */
775                 ret = btrfs_free_extent(trans, root,
776                                         btrfs_file_extent_disk_blocknr(fi),
777                                         btrfs_file_extent_disk_num_blocks(fi),
778                                         0);
779                 BUG_ON(ret);
780         }
781         return 0;
782 }
783
784 /*
785  * helper function for drop_snapshot, this walks down the tree dropping ref
786  * counts as it goes.
787  */
788 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
789                           *root, struct btrfs_path *path, int *level)
790 {
791         struct buffer_head *next;
792         struct buffer_head *cur;
793         u64 blocknr;
794         int ret;
795         u32 refs;
796
797         WARN_ON(*level < 0);
798         WARN_ON(*level >= BTRFS_MAX_LEVEL);
799         ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
800                                1, &refs);
801         BUG_ON(ret);
802         if (refs > 1)
803                 goto out;
804         /*
805          * walk down to the last node level and free all the leaves
806          */
807         while(*level >= 0) {
808                 WARN_ON(*level < 0);
809                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
810                 cur = path->nodes[*level];
811                 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
812                         WARN_ON(1);
813                 if (path->slots[*level] >=
814                     btrfs_header_nritems(btrfs_buffer_header(cur)))
815                         break;
816                 if (*level == 0) {
817                         ret = drop_leaf_ref(trans, root, cur);
818                         BUG_ON(ret);
819                         break;
820                 }
821                 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
822                                               path->slots[*level]);
823                 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
824                 BUG_ON(ret);
825                 if (refs != 1) {
826                         path->slots[*level]++;
827                         ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
828                         BUG_ON(ret);
829                         continue;
830                 }
831                 next = read_tree_block(root, blocknr);
832                 WARN_ON(*level <= 0);
833                 if (path->nodes[*level-1])
834                         btrfs_block_release(root, path->nodes[*level-1]);
835                 path->nodes[*level-1] = next;
836                 *level = btrfs_header_level(btrfs_buffer_header(next));
837                 path->slots[*level] = 0;
838         }
839 out:
840         WARN_ON(*level < 0);
841         WARN_ON(*level >= BTRFS_MAX_LEVEL);
842         ret = btrfs_free_extent(trans, root,
843                                 bh_blocknr(path->nodes[*level]), 1, 1);
844         btrfs_block_release(root, path->nodes[*level]);
845         path->nodes[*level] = NULL;
846         *level += 1;
847         BUG_ON(ret);
848         return 0;
849 }
850
851 /*
852  * helper for dropping snapshots.  This walks back up the tree in the path
853  * to find the first node higher up where we haven't yet gone through
854  * all the slots
855  */
856 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
857                         *root, struct btrfs_path *path, int *level)
858 {
859         int i;
860         int slot;
861         int ret;
862         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
863                 slot = path->slots[i];
864                 if (slot < btrfs_header_nritems(
865                     btrfs_buffer_header(path->nodes[i])) - 1) {
866                         path->slots[i]++;
867                         *level = i;
868                         return 0;
869                 } else {
870                         ret = btrfs_free_extent(trans, root,
871                                                 bh_blocknr(path->nodes[*level]),
872                                                 1, 1);
873                         BUG_ON(ret);
874                         btrfs_block_release(root, path->nodes[*level]);
875                         path->nodes[*level] = NULL;
876                         *level = i + 1;
877                 }
878         }
879         return 1;
880 }
881
882 /*
883  * drop the reference count on the tree rooted at 'snap'.  This traverses
884  * the tree freeing any blocks that have a ref count of zero after being
885  * decremented.
886  */
887 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
888                         *root, struct buffer_head *snap)
889 {
890         int ret = 0;
891         int wret;
892         int level;
893         struct btrfs_path *path;
894         int i;
895         int orig_level;
896
897         path = btrfs_alloc_path();
898         BUG_ON(!path);
899         btrfs_init_path(path);
900
901         level = btrfs_header_level(btrfs_buffer_header(snap));
902         orig_level = level;
903         path->nodes[level] = snap;
904         path->slots[level] = 0;
905         while(1) {
906                 wret = walk_down_tree(trans, root, path, &level);
907                 if (wret > 0)
908                         break;
909                 if (wret < 0)
910                         ret = wret;
911
912                 wret = walk_up_tree(trans, root, path, &level);
913                 if (wret > 0)
914                         break;
915                 if (wret < 0)
916                         ret = wret;
917         }
918         for (i = 0; i <= orig_level; i++) {
919                 if (path->nodes[i]) {
920                         btrfs_block_release(root, path->nodes[i]);
921                 }
922         }
923         btrfs_free_path(path);
924         return ret;
925 }
926
927 int btrfs_free_block_groups(struct btrfs_fs_info *info)
928 {
929         int ret;
930         struct btrfs_block_group_cache *cache[8];
931         int i;
932
933         while(1) {
934                 ret = radix_tree_gang_lookup(&info->block_group_radix,
935                                              (void **)cache, 0,
936                                              ARRAY_SIZE(cache));
937                 if (!ret)
938                         break;
939                 for (i = 0; i < ret; i++) {
940                         radix_tree_delete(&info->block_group_radix,
941                                           cache[i]->key.objectid +
942                                           cache[i]->key.offset - 1);
943                         kfree(cache[i]);
944                 }
945         }
946         return 0;
947 }
948
949 int btrfs_read_block_groups(struct btrfs_root *root)
950 {
951         struct btrfs_path *path;
952         int ret;
953         int err = 0;
954         struct btrfs_block_group_item *bi;
955         struct btrfs_block_group_cache *cache;
956         struct btrfs_key key;
957         struct btrfs_key found_key;
958         struct btrfs_leaf *leaf;
959         u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
960
961         root = root->fs_info->extent_root;
962         key.objectid = 0;
963         key.offset = group_size_blocks;
964         key.flags = 0;
965         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
966
967         path = btrfs_alloc_path();
968         if (!path)
969                 return -ENOMEM;
970
971         while(1) {
972                 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
973                                         &key, path, 0, 0);
974                 if (ret != 0) {
975                         err = ret;
976                         break;
977                 }
978                 leaf = btrfs_buffer_leaf(path->nodes[0]);
979                 btrfs_disk_key_to_cpu(&found_key,
980                                       &leaf->items[path->slots[0]].key);
981                 cache = kmalloc(sizeof(*cache), GFP_NOFS);
982                 if (!cache) {
983                         err = -1;
984                         break;
985                 }
986                 bi = btrfs_item_ptr(leaf, path->slots[0],
987                                     struct btrfs_block_group_item);
988                 memcpy(&cache->item, bi, sizeof(*bi));
989                 memcpy(&cache->key, &found_key, sizeof(found_key));
990                 cache->last_alloc = 0;
991                 cache->first_free = 0;
992                 key.objectid = found_key.objectid + found_key.offset;
993                 btrfs_release_path(root, path);
994                 ret = radix_tree_insert(&root->fs_info->block_group_radix,
995                                         found_key.objectid +
996                                         found_key.offset - 1,
997                                         (void *)cache);
998                 BUG_ON(ret);
999                 if (key.objectid >=
1000                     btrfs_super_total_blocks(root->fs_info->disk_super))
1001                         break;
1002         }
1003
1004         btrfs_free_path(path);
1005         return 0;
1006 }