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