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