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