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