Btrfs: Stop using radix trees for the block group cache
[linux-2.6-block.git] / fs / btrfs / extent-tree.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/sched.h>
20 #include "ctree.h"
21 #include "disk-io.h"
22 #include "print-tree.h"
23 #include "transaction.h"
24
25 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
28
29 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30                                  btrfs_root *extent_root);
31 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32                                btrfs_root *extent_root);
33
34 static int cache_block_group(struct btrfs_root *root,
35                              struct btrfs_block_group_cache *block_group)
36 {
37         struct btrfs_path *path;
38         int ret;
39         struct btrfs_key key;
40         struct extent_buffer *leaf;
41         struct extent_map_tree *free_space_cache;
42         int slot;
43         u64 last = 0;
44         u64 hole_size;
45         u64 first_free;
46         int found = 0;
47
48         root = root->fs_info->extent_root;
49         free_space_cache = &root->fs_info->free_space_cache;
50
51         if (block_group->cached)
52                 return 0;
53
54         path = btrfs_alloc_path();
55         if (!path)
56                 return -ENOMEM;
57
58         path->reada = 2;
59         first_free = block_group->key.objectid;
60         key.objectid = block_group->key.objectid;
61         key.offset = 0;
62
63         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
65
66         if (ret < 0)
67                 return ret;
68
69         if (ret && path->slots[0] > 0)
70                 path->slots[0]--;
71
72         while(1) {
73                 leaf = path->nodes[0];
74                 slot = path->slots[0];
75                 if (slot >= btrfs_header_nritems(leaf)) {
76                         ret = btrfs_next_leaf(root, path);
77                         if (ret < 0)
78                                 goto err;
79                         if (ret == 0) {
80                                 continue;
81                         } else {
82                                 break;
83                         }
84                 }
85
86                 btrfs_item_key_to_cpu(leaf, &key, slot);
87                 if (key.objectid < block_group->key.objectid) {
88                         if (key.objectid + key.offset > first_free)
89                                 first_free = key.objectid + key.offset;
90                         goto next;
91                 }
92
93                 if (key.objectid >= block_group->key.objectid +
94                     block_group->key.offset) {
95                         break;
96                 }
97
98                 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
99                         if (!found) {
100                                 last = first_free;
101                                 found = 1;
102                         }
103                         if (key.objectid > last) {
104                                 hole_size = key.objectid - last;
105                                 set_extent_dirty(free_space_cache, last,
106                                                  last + hole_size - 1,
107                                                  GFP_NOFS);
108                         }
109                         last = key.objectid + key.offset;
110                 }
111 next:
112                 path->slots[0]++;
113         }
114
115         if (!found)
116                 last = first_free;
117         if (block_group->key.objectid +
118             block_group->key.offset > last) {
119                 hole_size = block_group->key.objectid +
120                         block_group->key.offset - last;
121                 set_extent_dirty(free_space_cache, last,
122                                  last + hole_size - 1, GFP_NOFS);
123         }
124         block_group->cached = 1;
125 err:
126         btrfs_free_path(path);
127         return 0;
128 }
129
130 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
131                                                          btrfs_fs_info *info,
132                                                          u64 blocknr)
133 {
134         struct extent_map_tree *block_group_cache;
135         struct btrfs_block_group_cache *block_group = NULL;
136         u64 ptr;
137         u64 start;
138         u64 end;
139         int ret;
140
141         block_group_cache = &info->block_group_cache;
142         ret = find_first_extent_bit(block_group_cache,
143                                     blocknr, &start, &end,
144                                     BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
145         if (ret) {
146                 return NULL;
147         }
148         ret = get_state_private(block_group_cache, start, &ptr);
149         if (ret)
150                 return NULL;
151
152         block_group = (struct btrfs_block_group_cache *)ptr;
153
154
155         if (block_group->key.objectid <= blocknr && blocknr <=
156             block_group->key.objectid + block_group->key.offset)
157                 return block_group;
158
159         return NULL;
160 }
161
162 static u64 find_search_start(struct btrfs_root *root,
163                              struct btrfs_block_group_cache **cache_ret,
164                              u64 search_start, int num, int data)
165 {
166         int ret;
167         struct btrfs_block_group_cache *cache = *cache_ret;
168         u64 last = max(search_start, cache->key.objectid);
169         u64 start = 0;
170         u64 end = 0;
171
172 again:
173         ret = cache_block_group(root, cache);
174         if (ret)
175                 goto out;
176         while(1) {
177                 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
178                                             last, &start, &end, EXTENT_DIRTY);
179                 if (ret)
180                         goto out;
181
182                 start = max(last, start);
183                 last = end + 1;
184                 if (end + 1 - start < num)
185                         continue;
186                 if (start + num >= cache->key.objectid + cache->key.offset)
187                         goto new_group;
188                 return start;
189         }
190 out:
191         return max(cache->last_alloc, search_start);
192
193 new_group:
194         cache = btrfs_lookup_block_group(root->fs_info,
195                                          last + cache->key.offset - 1);
196         if (!cache) {
197                 return max((*cache_ret)->last_alloc, search_start);
198         }
199         cache = btrfs_find_block_group(root, cache,
200                                        last + cache->key.offset - 1, data, 0);
201         *cache_ret = cache;
202         last = min(cache->key.objectid, last);
203         goto again;
204 }
205
206 static u64 div_factor(u64 num, int factor)
207 {
208         num *= factor;
209         do_div(num, 10);
210         return num;
211 }
212
213 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
214                                                  struct btrfs_block_group_cache
215                                                  *hint, u64 search_start,
216                                                  int data, int owner)
217 {
218         struct btrfs_block_group_cache *cache;
219         struct extent_map_tree *block_group_cache;
220         struct btrfs_block_group_cache *found_group = NULL;
221         struct btrfs_fs_info *info = root->fs_info;
222         u64 used;
223         u64 last = 0;
224         u64 hint_last;
225         u64 start;
226         u64 end;
227         u64 free_check;
228         u64 ptr;
229         int bit;
230         int ret;
231         int full_search = 0;
232         int factor = 8;
233         int data_swap = 0;
234
235         block_group_cache = &info->block_group_cache;
236
237         if (!owner)
238                 factor = 5;
239
240         if (data)
241                 bit = BLOCK_GROUP_DATA;
242         else
243                 bit = BLOCK_GROUP_METADATA;
244
245         if (search_start) {
246                 struct btrfs_block_group_cache *shint;
247                 shint = btrfs_lookup_block_group(info, search_start);
248                 if (shint && shint->data == data) {
249                         used = btrfs_block_group_used(&shint->item);
250                         if (used + shint->pinned <
251                             div_factor(shint->key.offset, factor)) {
252                                 return shint;
253                         }
254                 }
255         }
256         if (hint && hint->data == data) {
257                 used = btrfs_block_group_used(&hint->item);
258                 if (used + hint->pinned <
259                     div_factor(hint->key.offset, factor)) {
260                         return hint;
261                 }
262                 last = hint->key.offset * 3;
263                 if (hint->key.objectid >= last)
264                         last = max(search_start + hint->key.offset - 1,
265                                    hint->key.objectid - last);
266                 else
267                         last = hint->key.objectid + hint->key.offset;
268                 hint_last = last;
269         } else {
270                 if (hint)
271                         hint_last = max(hint->key.objectid, search_start);
272                 else
273                         hint_last = search_start;
274
275                 last = hint_last;
276         }
277 again:
278         while(1) {
279                 ret = find_first_extent_bit(block_group_cache, last,
280                                             &start, &end, bit);
281                 if (ret)
282                         break;
283
284                 ret = get_state_private(block_group_cache, start, &ptr);
285                 if (ret)
286                         break;
287
288                 cache = (struct btrfs_block_group_cache *)ptr;
289                 last = cache->key.objectid + cache->key.offset;
290                 used = btrfs_block_group_used(&cache->item);
291
292                 if (full_search)
293                         free_check = cache->key.offset;
294                 else
295                         free_check = div_factor(cache->key.offset, factor);
296
297                 if (used + cache->pinned < free_check) {
298                         found_group = cache;
299                         goto found;
300                 }
301                 cond_resched();
302         }
303         if (!full_search) {
304                 last = search_start;
305                 full_search = 1;
306                 goto again;
307         }
308         if (!data_swap) {
309                 data_swap = 1;
310                 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
311                 last = search_start;
312                 goto again;
313         }
314 found:
315         return found_group;
316 }
317
318 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
319                                 struct btrfs_root *root,
320                                 u64 blocknr, u64 num_blocks)
321 {
322         struct btrfs_path *path;
323         int ret;
324         struct btrfs_key key;
325         struct extent_buffer *l;
326         struct btrfs_extent_item *item;
327         u32 refs;
328
329         path = btrfs_alloc_path();
330         if (!path)
331                 return -ENOMEM;
332
333         key.objectid = blocknr;
334         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
335         key.offset = num_blocks;
336         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
337                                 0, 1);
338         if (ret < 0)
339                 return ret;
340         if (ret != 0) {
341                 BUG();
342         }
343         BUG_ON(ret != 0);
344         l = path->nodes[0];
345         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
346         refs = btrfs_extent_refs(l, item);
347         btrfs_set_extent_refs(l, item, refs + 1);
348         btrfs_mark_buffer_dirty(path->nodes[0]);
349
350         btrfs_release_path(root->fs_info->extent_root, path);
351         btrfs_free_path(path);
352         finish_current_insert(trans, root->fs_info->extent_root);
353         del_pending_extents(trans, root->fs_info->extent_root);
354         return 0;
355 }
356
357 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
358                          struct btrfs_root *root)
359 {
360         finish_current_insert(trans, root->fs_info->extent_root);
361         del_pending_extents(trans, root->fs_info->extent_root);
362         return 0;
363 }
364
365 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
366                              struct btrfs_root *root, u64 blocknr,
367                              u64 num_blocks, u32 *refs)
368 {
369         struct btrfs_path *path;
370         int ret;
371         struct btrfs_key key;
372         struct extent_buffer *l;
373         struct btrfs_extent_item *item;
374
375         path = btrfs_alloc_path();
376         key.objectid = blocknr;
377         key.offset = num_blocks;
378         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
379         ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
380                                 0, 0);
381         if (ret < 0)
382                 goto out;
383         if (ret != 0) {
384                 btrfs_print_leaf(root, path->nodes[0]);
385                 printk("failed to find block number %Lu\n", blocknr);
386                 BUG();
387         }
388         l = path->nodes[0];
389         item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
390         *refs = btrfs_extent_refs(l, item);
391 out:
392         btrfs_free_path(path);
393         return 0;
394 }
395
396 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
397                        struct btrfs_root *root)
398 {
399         return btrfs_inc_extent_ref(trans, root,
400                                     extent_buffer_blocknr(root->node), 1);
401 }
402
403 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
404                   struct extent_buffer *buf)
405 {
406         u64 blocknr;
407         u32 nritems;
408         struct btrfs_key key;
409         struct btrfs_file_extent_item *fi;
410         int i;
411         int leaf;
412         int ret;
413         int faili;
414         int err;
415
416         if (!root->ref_cows)
417                 return 0;
418
419         leaf = btrfs_is_leaf(buf);
420         nritems = btrfs_header_nritems(buf);
421         for (i = 0; i < nritems; i++) {
422                 if (leaf) {
423                         u64 disk_blocknr;
424                         btrfs_item_key_to_cpu(buf, &key, i);
425                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
426                                 continue;
427                         fi = btrfs_item_ptr(buf, i,
428                                             struct btrfs_file_extent_item);
429                         if (btrfs_file_extent_type(buf, fi) ==
430                             BTRFS_FILE_EXTENT_INLINE)
431                                 continue;
432                         disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
433                         if (disk_blocknr == 0)
434                                 continue;
435                         ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
436                                     btrfs_file_extent_disk_num_blocks(buf, fi));
437                         if (ret) {
438                                 faili = i;
439                                 goto fail;
440                         }
441                 } else {
442                         blocknr = btrfs_node_blockptr(buf, i);
443                         ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
444                         if (ret) {
445                                 faili = i;
446                                 goto fail;
447                         }
448                 }
449         }
450         return 0;
451 fail:
452         WARN_ON(1);
453         for (i =0; i < faili; i++) {
454                 if (leaf) {
455                         u64 disk_blocknr;
456                         btrfs_item_key_to_cpu(buf, &key, i);
457                         if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
458                                 continue;
459                         fi = btrfs_item_ptr(buf, i,
460                                             struct btrfs_file_extent_item);
461                         if (btrfs_file_extent_type(buf, fi) ==
462                             BTRFS_FILE_EXTENT_INLINE)
463                                 continue;
464                         disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
465                         if (disk_blocknr == 0)
466                                 continue;
467                         err = btrfs_free_extent(trans, root, disk_blocknr,
468                                     btrfs_file_extent_disk_num_blocks(buf,
469                                                                       fi), 0);
470                         BUG_ON(err);
471                 } else {
472                         blocknr = btrfs_node_blockptr(buf, i);
473                         err = btrfs_free_extent(trans, root, blocknr, 1, 0);
474                         BUG_ON(err);
475                 }
476         }
477         return ret;
478 }
479
480 static int write_one_cache_group(struct btrfs_trans_handle *trans,
481                                  struct btrfs_root *root,
482                                  struct btrfs_path *path,
483                                  struct btrfs_block_group_cache *cache)
484 {
485         int ret;
486         int pending_ret;
487         struct btrfs_root *extent_root = root->fs_info->extent_root;
488         unsigned long bi;
489         struct extent_buffer *leaf;
490
491         ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
492         if (ret < 0)
493                 goto fail;
494         BUG_ON(ret);
495
496         leaf = path->nodes[0];
497         bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
498         write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
499         btrfs_mark_buffer_dirty(leaf);
500         btrfs_release_path(extent_root, path);
501 fail:
502         finish_current_insert(trans, extent_root);
503         pending_ret = del_pending_extents(trans, extent_root);
504         if (ret)
505                 return ret;
506         if (pending_ret)
507                 return pending_ret;
508         if (cache->data)
509                 cache->last_alloc = cache->first_free;
510         return 0;
511
512 }
513
514 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
515                                    struct btrfs_root *root)
516 {
517         struct extent_map_tree *block_group_cache;
518         struct btrfs_block_group_cache *cache;
519         int ret;
520         int err = 0;
521         int werr = 0;
522         struct btrfs_path *path;
523         u64 last = 0;
524         u64 start;
525         u64 end;
526         u64 ptr;
527
528         block_group_cache = &root->fs_info->block_group_cache;
529         path = btrfs_alloc_path();
530         if (!path)
531                 return -ENOMEM;
532
533         while(1) {
534                 ret = find_first_extent_bit(block_group_cache, last,
535                                             &start, &end, BLOCK_GROUP_DIRTY);
536                 if (ret)
537                         break;
538
539                 last = end + 1;
540                 ret = get_state_private(block_group_cache, start, &ptr);
541                 if (ret)
542                         break;
543
544                 cache = (struct btrfs_block_group_cache *)ptr;
545                 err = write_one_cache_group(trans, root,
546                                             path, cache);
547                 /*
548                  * if we fail to write the cache group, we want
549                  * to keep it marked dirty in hopes that a later
550                  * write will work
551                  */
552                 if (err) {
553                         werr = err;
554                         continue;
555                 }
556                 clear_extent_bits(block_group_cache, start, end,
557                                   BLOCK_GROUP_DIRTY, GFP_NOFS);
558         }
559         btrfs_free_path(path);
560         return werr;
561 }
562
563 static int update_block_group(struct btrfs_trans_handle *trans,
564                               struct btrfs_root *root,
565                               u64 blocknr, u64 num, int alloc, int mark_free,
566                               int data)
567 {
568         struct btrfs_block_group_cache *cache;
569         struct btrfs_fs_info *info = root->fs_info;
570         u64 total = num;
571         u64 old_val;
572         u64 block_in_group;
573         u64 start;
574         u64 end;
575
576         while(total) {
577                 cache = btrfs_lookup_block_group(info, blocknr);
578                 if (!cache) {
579                         return -1;
580                 }
581                 block_in_group = blocknr - cache->key.objectid;
582                 WARN_ON(block_in_group > cache->key.offset);
583                 start = cache->key.objectid;
584                 end = start + cache->key.offset - 1;
585                 set_extent_bits(&info->block_group_cache, start, end,
586                                 BLOCK_GROUP_DIRTY, GFP_NOFS);
587
588                 old_val = btrfs_block_group_used(&cache->item);
589                 num = min(total, cache->key.offset - block_in_group);
590                 if (alloc) {
591                         if (blocknr > cache->last_alloc)
592                                 cache->last_alloc = blocknr;
593                         if (cache->data != data &&
594                             old_val < (cache->key.offset >> 1)) {
595                                 int bit_to_clear;
596                                 int bit_to_set;
597
598                                 cache->data = data;
599                                 if (data) {
600                                         bit_to_clear = BLOCK_GROUP_DATA;
601                                         bit_to_set = BLOCK_GROUP_METADATA;
602                                         cache->item.flags |=
603                                                 BTRFS_BLOCK_GROUP_DATA;
604                                 } else {
605                                         bit_to_clear = BLOCK_GROUP_METADATA;
606                                         bit_to_set = BLOCK_GROUP_DATA;
607                                         cache->item.flags &=
608                                                 ~BTRFS_BLOCK_GROUP_DATA;
609                                 }
610                                 clear_extent_bits(&info->block_group_cache,
611                                                   start, end, bit_to_clear,
612                                                   GFP_NOFS);
613                                 set_extent_bits(&info->block_group_cache,
614                                                 start, end, bit_to_set,
615                                                 GFP_NOFS);
616                         }
617                         old_val += num;
618                 } else {
619                         old_val -= num;
620                         if (blocknr < cache->first_free)
621                                 cache->first_free = blocknr;
622                         if (mark_free) {
623                                 set_extent_dirty(&info->free_space_cache,
624                                                  blocknr, blocknr + num - 1,
625                                                  GFP_NOFS);
626                         }
627                 }
628                 btrfs_set_block_group_used(&cache->item, old_val);
629                 total -= num;
630                 blocknr += num;
631         }
632         return 0;
633 }
634
635 int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
636 {
637         unsigned long gang[8];
638         u64 last = 0;
639         struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
640         int ret;
641         int i;
642
643         while(1) {
644                 ret = find_first_radix_bit(pinned_radix, gang, last,
645                                            ARRAY_SIZE(gang));
646                 if (!ret)
647                         break;
648                 for (i = 0 ; i < ret; i++) {
649                         set_radix_bit(copy, gang[i]);
650                         last = gang[i] + 1;
651                 }
652         }
653         ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
654                                    ARRAY_SIZE(gang));
655         WARN_ON(ret);
656         return 0;
657 }
658
659 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
660                                struct btrfs_root *root,
661                                struct radix_tree_root *unpin_radix)
662 {
663         unsigned long gang[8];
664         struct btrfs_block_group_cache *block_group;
665         u64 first = 0;
666         int ret;
667         int i;
668         struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
669         struct extent_map_tree *free_space_cache;
670
671         free_space_cache = &root->fs_info->free_space_cache;
672
673         while(1) {
674                 ret = find_first_radix_bit(unpin_radix, gang, 0,
675                                            ARRAY_SIZE(gang));
676                 if (!ret)
677                         break;
678                 if (!first)
679                         first = gang[0];
680                 for (i = 0; i < ret; i++) {
681                         clear_radix_bit(pinned_radix, gang[i]);
682                         clear_radix_bit(unpin_radix, gang[i]);
683                         block_group = btrfs_lookup_block_group(root->fs_info,
684                                                                gang[i]);
685                         if (block_group) {
686                                 WARN_ON(block_group->pinned == 0);
687                                 block_group->pinned--;
688                                 if (gang[i] < block_group->last_alloc)
689                                         block_group->last_alloc = gang[i];
690                                 set_extent_dirty(free_space_cache,
691                                                  gang[i], gang[i], GFP_NOFS);
692                         }
693                 }
694         }
695         return 0;
696 }
697
698 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
699                                  btrfs_root *extent_root)
700 {
701         struct btrfs_key ins;
702         struct btrfs_extent_item extent_item;
703         int i;
704         int ret;
705         int err;
706         unsigned long gang[8];
707         struct btrfs_fs_info *info = extent_root->fs_info;
708
709         btrfs_set_stack_extent_refs(&extent_item, 1);
710         ins.offset = 1;
711         btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
712         btrfs_set_stack_extent_owner(&extent_item,
713                                      extent_root->root_key.objectid);
714
715         while(1) {
716                 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
717                                            ARRAY_SIZE(gang));
718                 if (!ret)
719                         break;
720
721                 for (i = 0; i < ret; i++) {
722                         ins.objectid = gang[i];
723                         err = btrfs_insert_item(trans, extent_root, &ins,
724                                                 &extent_item,
725                                                 sizeof(extent_item));
726                         clear_radix_bit(&info->extent_ins_radix, gang[i]);
727                         WARN_ON(err);
728                 }
729         }
730         return 0;
731 }
732
733 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
734 {
735         int err;
736         struct extent_buffer *buf;
737
738         if (!pending) {
739                 buf = btrfs_find_tree_block(root, blocknr);
740                 if (buf) {
741                         if (btrfs_buffer_uptodate(buf)) {
742                                 u64 transid =
743                                     root->fs_info->running_transaction->transid;
744                                 if (btrfs_header_generation(buf) == transid) {
745                                         free_extent_buffer(buf);
746                                         return 0;
747                                 }
748                         }
749                         free_extent_buffer(buf);
750                 }
751                 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
752                 if (!err) {
753                         struct btrfs_block_group_cache *cache;
754                         cache = btrfs_lookup_block_group(root->fs_info,
755                                                          blocknr);
756                         if (cache)
757                                 cache->pinned++;
758                 }
759         } else {
760                 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
761         }
762         BUG_ON(err < 0);
763         return 0;
764 }
765
766 /*
767  * remove an extent from the root, returns 0 on success
768  */
769 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
770                          *root, u64 blocknr, u64 num_blocks, int pin,
771                          int mark_free)
772 {
773         struct btrfs_path *path;
774         struct btrfs_key key;
775         struct btrfs_fs_info *info = root->fs_info;
776         struct btrfs_root *extent_root = info->extent_root;
777         struct extent_buffer *leaf;
778         int ret;
779         struct btrfs_extent_item *ei;
780         u32 refs;
781
782         key.objectid = blocknr;
783         btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
784         key.offset = num_blocks;
785
786         path = btrfs_alloc_path();
787         if (!path)
788                 return -ENOMEM;
789
790         ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
791         if (ret < 0)
792                 return ret;
793         BUG_ON(ret);
794
795         leaf = path->nodes[0];
796         ei = btrfs_item_ptr(leaf, path->slots[0],
797                             struct btrfs_extent_item);
798         refs = btrfs_extent_refs(leaf, ei);
799         BUG_ON(refs == 0);
800         refs -= 1;
801         btrfs_set_extent_refs(leaf, ei, refs);
802         btrfs_mark_buffer_dirty(leaf);
803
804         if (refs == 0) {
805                 u64 super_blocks_used, root_blocks_used;
806
807                 if (pin) {
808                         ret = pin_down_block(root, blocknr, 0);
809                         BUG_ON(ret);
810                 }
811
812                 /* block accounting for super block */
813                 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
814                 btrfs_set_super_blocks_used(&info->super_copy,
815                                             super_blocks_used - num_blocks);
816
817                 /* block accounting for root item */
818                 root_blocks_used = btrfs_root_used(&root->root_item);
819                 btrfs_set_root_used(&root->root_item,
820                                            root_blocks_used - num_blocks);
821
822                 ret = btrfs_del_item(trans, extent_root, path);
823                 if (ret) {
824                         return ret;
825                 }
826                 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
827                                          mark_free, 0);
828                 BUG_ON(ret);
829         }
830         btrfs_free_path(path);
831         finish_current_insert(trans, extent_root);
832         return ret;
833 }
834
835 /*
836  * find all the blocks marked as pending in the radix tree and remove
837  * them from the extent map
838  */
839 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
840                                btrfs_root *extent_root)
841 {
842         int ret;
843         int wret;
844         int err = 0;
845         unsigned long gang[4];
846         int i;
847         struct radix_tree_root *pending_radix;
848         struct radix_tree_root *pinned_radix;
849         struct btrfs_block_group_cache *cache;
850
851         pending_radix = &extent_root->fs_info->pending_del_radix;
852         pinned_radix = &extent_root->fs_info->pinned_radix;
853
854         while(1) {
855                 ret = find_first_radix_bit(pending_radix, gang, 0,
856                                            ARRAY_SIZE(gang));
857                 if (!ret)
858                         break;
859                 for (i = 0; i < ret; i++) {
860                         wret = set_radix_bit(pinned_radix, gang[i]);
861                         if (wret == 0) {
862                                 cache =
863                                   btrfs_lookup_block_group(extent_root->fs_info,
864                                                            gang[i]);
865                                 if (cache)
866                                         cache->pinned++;
867                         }
868                         if (wret < 0) {
869                                 printk(KERN_CRIT "set_radix_bit, err %d\n",
870                                        wret);
871                                 BUG_ON(wret < 0);
872                         }
873                         wret = clear_radix_bit(pending_radix, gang[i]);
874                         BUG_ON(wret);
875                         wret = __free_extent(trans, extent_root,
876                                              gang[i], 1, 0, 0);
877                         if (wret)
878                                 err = wret;
879                 }
880         }
881         return err;
882 }
883
884 /*
885  * remove an extent from the root, returns 0 on success
886  */
887 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
888                       *root, u64 blocknr, u64 num_blocks, int pin)
889 {
890         struct btrfs_root *extent_root = root->fs_info->extent_root;
891         int pending_ret;
892         int ret;
893
894         if (root == extent_root) {
895                 pin_down_block(root, blocknr, 1);
896                 return 0;
897         }
898         ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
899         pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
900         return ret ? ret : pending_ret;
901 }
902
903 /*
904  * walks the btree of allocated extents and find a hole of a given size.
905  * The key ins is changed to record the hole:
906  * ins->objectid == block start
907  * ins->flags = BTRFS_EXTENT_ITEM_KEY
908  * ins->offset == number of blocks
909  * Any available blocks before search_start are skipped.
910  */
911 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
912                             *orig_root, u64 num_blocks, u64 empty_size,
913                             u64 search_start, u64 search_end, u64 hint_block,
914                             struct btrfs_key *ins, u64 exclude_start,
915                             u64 exclude_nr, int data)
916 {
917         struct btrfs_path *path;
918         struct btrfs_key key;
919         int ret;
920         u64 hole_size = 0;
921         int slot = 0;
922         u64 last_block = 0;
923         u64 test_block;
924         u64 orig_search_start = search_start;
925         int start_found;
926         struct extent_buffer *l;
927         struct btrfs_root * root = orig_root->fs_info->extent_root;
928         struct btrfs_fs_info *info = root->fs_info;
929         int total_needed = num_blocks;
930         int level;
931         struct btrfs_block_group_cache *block_group;
932         int full_scan = 0;
933         int wrapped = 0;
934         u64 cached_search_start = 0;
935
936         WARN_ON(num_blocks < 1);
937         btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
938
939         level = btrfs_header_level(root->node);
940
941         if (search_end == (u64)-1)
942                 search_end = btrfs_super_total_blocks(&info->super_copy);
943         if (hint_block) {
944                 block_group = btrfs_lookup_block_group(info, hint_block);
945                 block_group = btrfs_find_block_group(root, block_group,
946                                                      hint_block, data, 1);
947         } else {
948                 block_group = btrfs_find_block_group(root,
949                                                      trans->block_group, 0,
950                                                      data, 1);
951         }
952
953         total_needed += empty_size;
954         path = btrfs_alloc_path();
955
956 check_failed:
957         search_start = find_search_start(root, &block_group,
958                                          search_start, total_needed, data);
959         cached_search_start = search_start;
960
961         btrfs_init_path(path);
962         ins->objectid = search_start;
963         ins->offset = 0;
964         start_found = 0;
965         path->reada = 2;
966
967         ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
968         if (ret < 0)
969                 goto error;
970
971         if (path->slots[0] > 0) {
972                 path->slots[0]--;
973         }
974
975         l = path->nodes[0];
976         btrfs_item_key_to_cpu(l, &key, path->slots[0]);
977
978         /*
979          * a rare case, go back one key if we hit a block group item
980          * instead of an extent item
981          */
982         if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
983             key.objectid + key.offset >= search_start) {
984                 ins->objectid = key.objectid;
985                 ins->offset = key.offset - 1;
986                 btrfs_release_path(root, path);
987                 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
988                 if (ret < 0)
989                         goto error;
990
991                 if (path->slots[0] > 0) {
992                         path->slots[0]--;
993                 }
994         }
995
996         while (1) {
997                 l = path->nodes[0];
998                 slot = path->slots[0];
999                 if (slot >= btrfs_header_nritems(l)) {
1000                         ret = btrfs_next_leaf(root, path);
1001                         if (ret == 0)
1002                                 continue;
1003                         if (ret < 0)
1004                                 goto error;
1005                         if (!start_found) {
1006                                 ins->objectid = search_start;
1007                                 ins->offset = search_end - search_start;
1008                                 start_found = 1;
1009                                 goto check_pending;
1010                         }
1011                         ins->objectid = last_block > search_start ?
1012                                         last_block : search_start;
1013                         ins->offset = search_end - ins->objectid;
1014                         goto check_pending;
1015                 }
1016                 btrfs_item_key_to_cpu(l, &key, slot);
1017
1018                 if (key.objectid >= search_start && key.objectid > last_block &&
1019                     start_found) {
1020                         if (last_block < search_start)
1021                                 last_block = search_start;
1022                         hole_size = key.objectid - last_block;
1023                         if (hole_size >= num_blocks) {
1024                                 ins->objectid = last_block;
1025                                 ins->offset = hole_size;
1026                                 goto check_pending;
1027                         }
1028                 }
1029                 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1030                         if (!start_found) {
1031                                 last_block = key.objectid;
1032                                 start_found = 1;
1033                         }
1034                         goto next;
1035                 }
1036
1037
1038                 start_found = 1;
1039                 last_block = key.objectid + key.offset;
1040
1041                 if (!full_scan && last_block >= block_group->key.objectid +
1042                     block_group->key.offset) {
1043                         btrfs_release_path(root, path);
1044                         search_start = block_group->key.objectid +
1045                                 block_group->key.offset * 2;
1046                         goto new_group;
1047                 }
1048 next:
1049                 path->slots[0]++;
1050                 cond_resched();
1051         }
1052 check_pending:
1053         /* we have to make sure we didn't find an extent that has already
1054          * been allocated by the map tree or the original allocation
1055          */
1056         btrfs_release_path(root, path);
1057         BUG_ON(ins->objectid < search_start);
1058
1059         if (ins->objectid + num_blocks >= search_end)
1060                 goto enospc;
1061
1062         for (test_block = ins->objectid;
1063              test_block < ins->objectid + num_blocks; test_block++) {
1064                 if (test_radix_bit(&info->pinned_radix, test_block) ||
1065                     test_radix_bit(&info->extent_ins_radix, test_block)) {
1066                         search_start = test_block + 1;
1067                         goto new_group;
1068                 }
1069         }
1070         if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1071             ins->objectid < exclude_start + exclude_nr)) {
1072                 search_start = exclude_start + exclude_nr;
1073                 goto new_group;
1074         }
1075         if (!data) {
1076                 block_group = btrfs_lookup_block_group(info, ins->objectid);
1077                 if (block_group)
1078                         trans->block_group = block_group;
1079         }
1080         ins->offset = num_blocks;
1081         btrfs_free_path(path);
1082         return 0;
1083
1084 new_group:
1085         if (search_start + num_blocks >= search_end) {
1086 enospc:
1087                 search_start = orig_search_start;
1088                 if (full_scan) {
1089                         ret = -ENOSPC;
1090                         goto error;
1091                 }
1092                 if (wrapped) {
1093                         if (!full_scan)
1094                                 total_needed -= empty_size;
1095                         full_scan = 1;
1096                 } else
1097                         wrapped = 1;
1098         }
1099         block_group = btrfs_lookup_block_group(info, search_start);
1100         cond_resched();
1101         if (!full_scan)
1102                 block_group = btrfs_find_block_group(root, block_group,
1103                                                      search_start, data, 0);
1104         goto check_failed;
1105
1106 error:
1107         btrfs_release_path(root, path);
1108         btrfs_free_path(path);
1109         return ret;
1110 }
1111 /*
1112  * finds a free extent and does all the dirty work required for allocation
1113  * returns the key for the extent through ins, and a tree buffer for
1114  * the first block of the extent through buf.
1115  *
1116  * returns 0 if everything worked, non-zero otherwise.
1117  */
1118 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1119                        struct btrfs_root *root, u64 owner,
1120                        u64 num_blocks, u64 empty_size, u64 hint_block,
1121                        u64 search_end, struct btrfs_key *ins, int data)
1122 {
1123         int ret;
1124         int pending_ret;
1125         u64 super_blocks_used, root_blocks_used;
1126         u64 search_start = 0;
1127         struct btrfs_fs_info *info = root->fs_info;
1128         struct btrfs_root *extent_root = info->extent_root;
1129         struct btrfs_extent_item extent_item;
1130
1131         btrfs_set_stack_extent_refs(&extent_item, 1);
1132         btrfs_set_stack_extent_owner(&extent_item, owner);
1133
1134         WARN_ON(num_blocks < 1);
1135         ret = find_free_extent(trans, root, num_blocks, empty_size,
1136                                search_start, search_end, hint_block, ins,
1137                                trans->alloc_exclude_start,
1138                                trans->alloc_exclude_nr, data);
1139         BUG_ON(ret);
1140         if (ret)
1141                 return ret;
1142
1143         /* block accounting for super block */
1144         super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1145         btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1146                                     num_blocks);
1147
1148         /* block accounting for root item */
1149         root_blocks_used = btrfs_root_used(&root->root_item);
1150         btrfs_set_root_used(&root->root_item, root_blocks_used +
1151                                    num_blocks);
1152
1153         clear_extent_dirty(&root->fs_info->free_space_cache,
1154                            ins->objectid, ins->objectid + ins->offset - 1,
1155                            GFP_NOFS);
1156
1157         if (root == extent_root) {
1158                 BUG_ON(num_blocks != 1);
1159                 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1160                 goto update_block;
1161         }
1162
1163         WARN_ON(trans->alloc_exclude_nr);
1164         trans->alloc_exclude_start = ins->objectid;
1165         trans->alloc_exclude_nr = ins->offset;
1166         ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1167                                 sizeof(extent_item));
1168
1169         trans->alloc_exclude_start = 0;
1170         trans->alloc_exclude_nr = 0;
1171
1172         BUG_ON(ret);
1173         finish_current_insert(trans, extent_root);
1174         pending_ret = del_pending_extents(trans, extent_root);
1175
1176         if (ret) {
1177                 return ret;
1178         }
1179         if (pending_ret) {
1180                 return pending_ret;
1181         }
1182
1183 update_block:
1184         ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1185                                  data);
1186         BUG_ON(ret);
1187         return 0;
1188 }
1189
1190 /*
1191  * helper function to allocate a block for a given tree
1192  * returns the tree buffer or NULL.
1193  */
1194 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1195                                              struct btrfs_root *root, u64 hint,
1196                                              u64 empty_size)
1197 {
1198         struct btrfs_key ins;
1199         int ret;
1200         struct extent_buffer *buf;
1201
1202         ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1203                                  1, empty_size, hint, (u64)-1, &ins, 0);
1204         if (ret) {
1205                 BUG_ON(ret > 0);
1206                 return ERR_PTR(ret);
1207         }
1208         buf = btrfs_find_create_tree_block(root, ins.objectid);
1209         if (!buf) {
1210                 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1211                 return ERR_PTR(-ENOMEM);
1212         }
1213         btrfs_set_buffer_uptodate(buf);
1214         buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1215         set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1216                          buf->start + buf->len - 1, GFP_NOFS);
1217         /*
1218         set_buffer_checked(buf);
1219         set_buffer_defrag(buf);
1220         */
1221         /* FIXME!!!!!!!!!!!!!!!!
1222         set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1223         */
1224         trans->blocks_used++;
1225         return buf;
1226 }
1227
1228 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1229                          struct btrfs_root *root, struct extent_buffer *leaf)
1230 {
1231         struct btrfs_key key;
1232         struct btrfs_file_extent_item *fi;
1233         int i;
1234         int nritems;
1235         int ret;
1236
1237         BUG_ON(!btrfs_is_leaf(leaf));
1238         nritems = btrfs_header_nritems(leaf);
1239         for (i = 0; i < nritems; i++) {
1240                 u64 disk_blocknr;
1241
1242                 btrfs_item_key_to_cpu(leaf, &key, i);
1243                 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1244                         continue;
1245                 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1246                 if (btrfs_file_extent_type(leaf, fi) ==
1247                     BTRFS_FILE_EXTENT_INLINE)
1248                         continue;
1249                 /*
1250                  * FIXME make sure to insert a trans record that
1251                  * repeats the snapshot del on crash
1252                  */
1253                 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
1254                 if (disk_blocknr == 0)
1255                         continue;
1256                 ret = btrfs_free_extent(trans, root, disk_blocknr,
1257                                 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
1258                 BUG_ON(ret);
1259         }
1260         return 0;
1261 }
1262
1263 static void reada_walk_down(struct btrfs_root *root,
1264                             struct extent_buffer *node)
1265 {
1266         int i;
1267         u32 nritems;
1268         u64 blocknr;
1269         int ret;
1270         u32 refs;
1271
1272         nritems = btrfs_header_nritems(node);
1273         for (i = 0; i < nritems; i++) {
1274                 blocknr = btrfs_node_blockptr(node, i);
1275                 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1276                 BUG_ON(ret);
1277                 if (refs != 1)
1278                         continue;
1279                 mutex_unlock(&root->fs_info->fs_mutex);
1280                 ret = readahead_tree_block(root, blocknr);
1281                 cond_resched();
1282                 mutex_lock(&root->fs_info->fs_mutex);
1283                 if (ret)
1284                         break;
1285         }
1286 }
1287
1288 /*
1289  * helper function for drop_snapshot, this walks down the tree dropping ref
1290  * counts as it goes.
1291  */
1292 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1293                           *root, struct btrfs_path *path, int *level)
1294 {
1295         struct extent_buffer *next;
1296         struct extent_buffer *cur;
1297         u64 blocknr;
1298         int ret;
1299         u32 refs;
1300
1301         WARN_ON(*level < 0);
1302         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1303         ret = lookup_extent_ref(trans, root,
1304                                 extent_buffer_blocknr(path->nodes[*level]),
1305                                 1, &refs);
1306         BUG_ON(ret);
1307         if (refs > 1)
1308                 goto out;
1309
1310         /*
1311          * walk down to the last node level and free all the leaves
1312          */
1313         while(*level >= 0) {
1314                 WARN_ON(*level < 0);
1315                 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1316                 cur = path->nodes[*level];
1317
1318                 if (*level > 0 && path->slots[*level] == 0)
1319                         reada_walk_down(root, cur);
1320
1321                 if (btrfs_header_level(cur) != *level)
1322                         WARN_ON(1);
1323
1324                 if (path->slots[*level] >=
1325                     btrfs_header_nritems(cur))
1326                         break;
1327                 if (*level == 0) {
1328                         ret = drop_leaf_ref(trans, root, cur);
1329                         BUG_ON(ret);
1330                         break;
1331                 }
1332                 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
1333                 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1334                 BUG_ON(ret);
1335                 if (refs != 1) {
1336                         path->slots[*level]++;
1337                         ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1338                         BUG_ON(ret);
1339                         continue;
1340                 }
1341                 next = btrfs_find_tree_block(root, blocknr);
1342                 if (!next || !btrfs_buffer_uptodate(next)) {
1343                         free_extent_buffer(next);
1344                         mutex_unlock(&root->fs_info->fs_mutex);
1345                         next = read_tree_block(root, blocknr);
1346                         mutex_lock(&root->fs_info->fs_mutex);
1347
1348                         /* we dropped the lock, check one more time */
1349                         ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1350                         BUG_ON(ret);
1351                         if (refs != 1) {
1352                                 path->slots[*level]++;
1353                                 free_extent_buffer(next);
1354                                 ret = btrfs_free_extent(trans, root,
1355                                                         blocknr, 1, 1);
1356                                 BUG_ON(ret);
1357                                 continue;
1358                         }
1359                 }
1360                 WARN_ON(*level <= 0);
1361                 if (path->nodes[*level-1])
1362                         free_extent_buffer(path->nodes[*level-1]);
1363                 path->nodes[*level-1] = next;
1364                 *level = btrfs_header_level(next);
1365                 path->slots[*level] = 0;
1366         }
1367 out:
1368         WARN_ON(*level < 0);
1369         WARN_ON(*level >= BTRFS_MAX_LEVEL);
1370         ret = btrfs_free_extent(trans, root,
1371                         extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1372         free_extent_buffer(path->nodes[*level]);
1373         path->nodes[*level] = NULL;
1374         *level += 1;
1375         BUG_ON(ret);
1376         return 0;
1377 }
1378
1379 /*
1380  * helper for dropping snapshots.  This walks back up the tree in the path
1381  * to find the first node higher up where we haven't yet gone through
1382  * all the slots
1383  */
1384 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1385                         *root, struct btrfs_path *path, int *level)
1386 {
1387         int i;
1388         int slot;
1389         int ret;
1390         struct btrfs_root_item *root_item = &root->root_item;
1391
1392         for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1393                 slot = path->slots[i];
1394                 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1395                         struct extent_buffer *node;
1396                         struct btrfs_disk_key disk_key;
1397                         node = path->nodes[i];
1398                         path->slots[i]++;
1399                         *level = i;
1400                         WARN_ON(*level == 0);
1401                         btrfs_node_key(node, &disk_key, path->slots[i]);
1402                         memcpy(&root_item->drop_progress,
1403                                &disk_key, sizeof(disk_key));
1404                         root_item->drop_level = i;
1405                         return 0;
1406                 } else {
1407                         ret = btrfs_free_extent(trans, root,
1408                                     extent_buffer_blocknr(path->nodes[*level]),
1409                                     1, 1);
1410                         BUG_ON(ret);
1411                         free_extent_buffer(path->nodes[*level]);
1412                         path->nodes[*level] = NULL;
1413                         *level = i + 1;
1414                 }
1415         }
1416         return 1;
1417 }
1418
1419 /*
1420  * drop the reference count on the tree rooted at 'snap'.  This traverses
1421  * the tree freeing any blocks that have a ref count of zero after being
1422  * decremented.
1423  */
1424 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1425                         *root)
1426 {
1427         int ret = 0;
1428         int wret;
1429         int level;
1430         struct btrfs_path *path;
1431         int i;
1432         int orig_level;
1433         struct btrfs_root_item *root_item = &root->root_item;
1434
1435         path = btrfs_alloc_path();
1436         BUG_ON(!path);
1437
1438         level = btrfs_header_level(root->node);
1439         orig_level = level;
1440         if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1441                 path->nodes[level] = root->node;
1442                 extent_buffer_get(root->node);
1443                 path->slots[level] = 0;
1444         } else {
1445                 struct btrfs_key key;
1446                 struct btrfs_disk_key found_key;
1447                 struct extent_buffer *node;
1448
1449                 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1450                 level = root_item->drop_level;
1451                 path->lowest_level = level;
1452                 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1453                 if (wret < 0) {
1454                         ret = wret;
1455                         goto out;
1456                 }
1457                 node = path->nodes[level];
1458                 btrfs_node_key(node, &found_key, path->slots[level]);
1459                 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1460                                sizeof(found_key)));
1461         }
1462         while(1) {
1463                 wret = walk_down_tree(trans, root, path, &level);
1464                 if (wret > 0)
1465                         break;
1466                 if (wret < 0)
1467                         ret = wret;
1468
1469                 wret = walk_up_tree(trans, root, path, &level);
1470                 if (wret > 0)
1471                         break;
1472                 if (wret < 0)
1473                         ret = wret;
1474                 ret = -EAGAIN;
1475                 break;
1476         }
1477         for (i = 0; i <= orig_level; i++) {
1478                 if (path->nodes[i]) {
1479                         free_extent_buffer(path->nodes[i]);
1480                         path->nodes[i] = 0;
1481                 }
1482         }
1483 out:
1484         btrfs_free_path(path);
1485         return ret;
1486 }
1487
1488 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1489 {
1490         u64 start;
1491         u64 end;
1492         int ret;
1493
1494         while(1) {
1495                 ret = find_first_extent_bit(&info->block_group_cache, 0,
1496                                             &start, &end, (unsigned int)-1);
1497                 if (ret)
1498                         break;
1499                 clear_extent_bits(&info->block_group_cache, start,
1500                                   end, (unsigned int)-1, GFP_NOFS);
1501         }
1502         while(1) {
1503                 ret = find_first_extent_bit(&info->free_space_cache, 0,
1504                                             &start, &end, EXTENT_DIRTY);
1505                 if (ret)
1506                         break;
1507                 clear_extent_dirty(&info->free_space_cache, start,
1508                                    end, GFP_NOFS);
1509         }
1510         return 0;
1511 }
1512
1513 int btrfs_read_block_groups(struct btrfs_root *root)
1514 {
1515         struct btrfs_path *path;
1516         int ret;
1517         int err = 0;
1518         int bit;
1519         struct btrfs_block_group_cache *cache;
1520         struct btrfs_fs_info *info = root->fs_info;
1521         struct extent_map_tree *block_group_cache;
1522         struct btrfs_key key;
1523         struct btrfs_key found_key;
1524         struct extent_buffer *leaf;
1525         u64 group_size_blocks;
1526
1527         block_group_cache = &info->block_group_cache;
1528
1529         group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1530                 info->sb->s_blocksize_bits;
1531
1532         root = info->extent_root;
1533         key.objectid = 0;
1534         key.offset = group_size_blocks;
1535         btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1536
1537         path = btrfs_alloc_path();
1538         if (!path)
1539                 return -ENOMEM;
1540
1541         while(1) {
1542                 ret = btrfs_search_slot(NULL, info->extent_root,
1543                                         &key, path, 0, 0);
1544                 if (ret != 0) {
1545                         err = ret;
1546                         break;
1547                 }
1548                 leaf = path->nodes[0];
1549                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1550                 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1551                 if (!cache) {
1552                         err = -1;
1553                         break;
1554                 }
1555
1556                 read_extent_buffer(leaf, &cache->item,
1557                                    btrfs_item_ptr_offset(leaf, path->slots[0]),
1558                                    sizeof(cache->item));
1559                 memcpy(&cache->key, &found_key, sizeof(found_key));
1560                 cache->last_alloc = cache->key.objectid;
1561                 cache->first_free = cache->key.objectid;
1562                 cache->pinned = 0;
1563                 cache->cached = 0;
1564
1565                 key.objectid = found_key.objectid + found_key.offset;
1566                 btrfs_release_path(root, path);
1567
1568                 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1569                         bit = BLOCK_GROUP_DATA;
1570                         cache->data = 1;
1571                 } else {
1572                         bit = BLOCK_GROUP_METADATA;
1573                         cache->data = 0;
1574                 }
1575
1576                 /* use EXTENT_LOCKED to prevent merging */
1577                 set_extent_bits(block_group_cache, found_key.objectid,
1578                                 found_key.objectid + found_key.offset - 1,
1579                                 bit | EXTENT_LOCKED, GFP_NOFS);
1580                 set_state_private(block_group_cache, found_key.objectid,
1581                                   (u64)cache);
1582
1583                 if (key.objectid >=
1584                     btrfs_super_total_blocks(&info->super_copy))
1585                         break;
1586         }
1587
1588         btrfs_free_path(path);
1589         return 0;
1590 }