Btrfs: Properly setup root key while reading the root
[linux-2.6-block.git] / fs / btrfs / extent-tree.c
... / ...
CommitLineData
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
29static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
31static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
33
34static 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 }
111next:
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;
125err:
126 btrfs_free_path(path);
127 return 0;
128}
129
130struct 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 *)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
162static 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
172again:
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 }
193out:
194 return search_start;
195
196new_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
207static u64 div_factor(u64 num, int factor)
208{
209 num *= factor;
210 do_div(num, 10);
211 return num;
212}
213
214struct 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 }
271again:
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 *)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 }
308found:
309 return found_group;
310}
311
312int 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
352int 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
360static 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);
387out:
388 btrfs_free_path(path);
389 return 0;
390}
391
392int 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
399int 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;
448fail:
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
478static 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);
499fail:
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
510int 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
559static 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
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_bytes;
612 } else {
613 old_val -= num_bytes;
614 if (mark_free) {
615 set_extent_dirty(&info->free_space_cache,
616 bytenr, bytenr + num_bytes - 1,
617 GFP_NOFS);
618 }
619 }
620 btrfs_set_block_group_used(&cache->item, old_val);
621 total -= num_bytes;
622 bytenr += num_bytes;
623 }
624 return 0;
625}
626
627int 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
646int 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
672static 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
704static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
705 int pending)
706{
707 int err = 0;
708 struct extent_buffer *buf;
709
710 if (!pending) {
711 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
712 if (buf) {
713 if (btrfs_buffer_uptodate(buf)) {
714 u64 transid =
715 root->fs_info->running_transaction->transid;
716 if (btrfs_header_generation(buf) == transid) {
717 free_extent_buffer(buf);
718 return 0;
719 }
720 }
721 free_extent_buffer(buf);
722 }
723 set_extent_dirty(&root->fs_info->pinned_extents,
724 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
725 } else {
726 set_extent_bits(&root->fs_info->pending_del,
727 bytenr, bytenr + num_bytes - 1,
728 EXTENT_LOCKED, GFP_NOFS);
729 }
730 BUG_ON(err < 0);
731 return 0;
732}
733
734/*
735 * remove an extent from the root, returns 0 on success
736 */
737static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
738 *root, u64 bytenr, u64 num_bytes, int pin,
739 int mark_free)
740{
741 struct btrfs_path *path;
742 struct btrfs_key key;
743 struct btrfs_fs_info *info = root->fs_info;
744 struct btrfs_root *extent_root = info->extent_root;
745 struct extent_buffer *leaf;
746 int ret;
747 struct btrfs_extent_item *ei;
748 u32 refs;
749
750 key.objectid = bytenr;
751 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
752 key.offset = num_bytes;
753
754 path = btrfs_alloc_path();
755 if (!path)
756 return -ENOMEM;
757
758 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
759 if (ret < 0)
760 return ret;
761 BUG_ON(ret);
762
763 leaf = path->nodes[0];
764 ei = btrfs_item_ptr(leaf, path->slots[0],
765 struct btrfs_extent_item);
766 refs = btrfs_extent_refs(leaf, ei);
767 BUG_ON(refs == 0);
768 refs -= 1;
769 btrfs_set_extent_refs(leaf, ei, refs);
770 btrfs_mark_buffer_dirty(leaf);
771
772 if (refs == 0) {
773 u64 super_used;
774 u64 root_used;
775
776 if (pin) {
777 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
778 BUG_ON(ret);
779 }
780
781 /* block accounting for super block */
782 super_used = btrfs_super_bytes_used(&info->super_copy);
783 btrfs_set_super_bytes_used(&info->super_copy,
784 super_used - num_bytes);
785
786 /* block accounting for root item */
787 root_used = btrfs_root_used(&root->root_item);
788 btrfs_set_root_used(&root->root_item,
789 root_used - num_bytes);
790
791 ret = btrfs_del_item(trans, extent_root, path);
792 if (ret) {
793 return ret;
794 }
795 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
796 mark_free, 0);
797 BUG_ON(ret);
798 }
799 btrfs_free_path(path);
800 finish_current_insert(trans, extent_root);
801 return ret;
802}
803
804/*
805 * find all the blocks marked as pending in the radix tree and remove
806 * them from the extent map
807 */
808static int del_pending_extents(struct btrfs_trans_handle *trans, struct
809 btrfs_root *extent_root)
810{
811 int ret;
812 int err = 0;
813 u64 start;
814 u64 end;
815 struct extent_map_tree *pending_del;
816 struct extent_map_tree *pinned_extents;
817
818 pending_del = &extent_root->fs_info->pending_del;
819 pinned_extents = &extent_root->fs_info->pinned_extents;
820
821 while(1) {
822 ret = find_first_extent_bit(pending_del, 0, &start, &end,
823 EXTENT_LOCKED);
824 if (ret)
825 break;
826
827 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
828 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
829 GFP_NOFS);
830 ret = __free_extent(trans, extent_root,
831 start, end + 1 - start, 0, 0);
832 if (ret)
833 err = ret;
834 }
835 return err;
836}
837
838/*
839 * remove an extent from the root, returns 0 on success
840 */
841int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
842 *root, u64 bytenr, u64 num_bytes, int pin)
843{
844 struct btrfs_root *extent_root = root->fs_info->extent_root;
845 int pending_ret;
846 int ret;
847
848 WARN_ON(num_bytes < root->sectorsize);
849 if (root == extent_root) {
850 pin_down_bytes(root, bytenr, num_bytes, 1);
851 return 0;
852 }
853 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
854 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
855 return ret ? ret : pending_ret;
856}
857
858/*
859 * walks the btree of allocated extents and find a hole of a given size.
860 * The key ins is changed to record the hole:
861 * ins->objectid == block start
862 * ins->flags = BTRFS_EXTENT_ITEM_KEY
863 * ins->offset == number of blocks
864 * Any available blocks before search_start are skipped.
865 */
866static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
867 *orig_root, u64 num_bytes, u64 empty_size,
868 u64 search_start, u64 search_end, u64 hint_byte,
869 struct btrfs_key *ins, u64 exclude_start,
870 u64 exclude_nr, int data)
871{
872 struct btrfs_path *path;
873 struct btrfs_key key;
874 int ret;
875 u64 hole_size = 0;
876 int slot = 0;
877 u64 last_byte = 0;
878 u64 orig_search_start = search_start;
879 int start_found;
880 struct extent_buffer *l;
881 struct btrfs_root * root = orig_root->fs_info->extent_root;
882 struct btrfs_fs_info *info = root->fs_info;
883 u64 total_needed = num_bytes;
884 int level;
885 struct btrfs_block_group_cache *block_group;
886 int full_scan = 0;
887 int wrapped = 0;
888
889 WARN_ON(num_bytes < root->sectorsize);
890 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
891
892 level = btrfs_header_level(root->node);
893
894 if (search_end == (u64)-1)
895 search_end = btrfs_super_total_bytes(&info->super_copy);
896 if (hint_byte) {
897 block_group = btrfs_lookup_block_group(info, hint_byte);
898 block_group = btrfs_find_block_group(root, block_group,
899 hint_byte, data, 1);
900 } else {
901 block_group = btrfs_find_block_group(root,
902 trans->block_group, 0,
903 data, 1);
904 }
905
906 total_needed += empty_size;
907 path = btrfs_alloc_path();
908
909check_failed:
910 search_start = find_search_start(root, &block_group,
911 search_start, total_needed, data);
912 btrfs_init_path(path);
913 ins->objectid = search_start;
914 ins->offset = 0;
915 start_found = 0;
916 path->reada = 2;
917
918 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
919 if (ret < 0)
920 goto error;
921
922 if (path->slots[0] > 0) {
923 path->slots[0]--;
924 }
925
926 l = path->nodes[0];
927 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
928
929 /*
930 * a rare case, go back one key if we hit a block group item
931 * instead of an extent item
932 */
933 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
934 key.objectid + key.offset >= search_start) {
935 ins->objectid = key.objectid;
936 ins->offset = key.offset - 1;
937 btrfs_release_path(root, path);
938 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
939 if (ret < 0)
940 goto error;
941
942 if (path->slots[0] > 0) {
943 path->slots[0]--;
944 }
945 }
946
947 while (1) {
948 l = path->nodes[0];
949 slot = path->slots[0];
950 if (slot >= btrfs_header_nritems(l)) {
951 ret = btrfs_next_leaf(root, path);
952 if (ret == 0)
953 continue;
954 if (ret < 0)
955 goto error;
956
957 search_start = max(search_start,
958 block_group->key.objectid);
959 if (!start_found) {
960 ins->objectid = search_start;
961 ins->offset = search_end - search_start;
962 start_found = 1;
963 goto check_pending;
964 }
965 ins->objectid = last_byte > search_start ?
966 last_byte : search_start;
967 ins->offset = search_end - ins->objectid;
968 BUG_ON(ins->objectid >= search_end);
969 goto check_pending;
970 }
971 btrfs_item_key_to_cpu(l, &key, slot);
972
973 if (key.objectid >= search_start && key.objectid > last_byte &&
974 start_found) {
975 if (last_byte < search_start)
976 last_byte = search_start;
977 hole_size = key.objectid - last_byte;
978 if (hole_size >= num_bytes) {
979 ins->objectid = last_byte;
980 ins->offset = hole_size;
981 goto check_pending;
982 }
983 }
984 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
985 if (!start_found) {
986 last_byte = key.objectid;
987 start_found = 1;
988 }
989 goto next;
990 }
991
992
993 start_found = 1;
994 last_byte = key.objectid + key.offset;
995
996 if (!full_scan && last_byte >= block_group->key.objectid +
997 block_group->key.offset) {
998 btrfs_release_path(root, path);
999 search_start = block_group->key.objectid +
1000 block_group->key.offset;
1001 goto new_group;
1002 }
1003next:
1004 path->slots[0]++;
1005 cond_resched();
1006 }
1007check_pending:
1008 /* we have to make sure we didn't find an extent that has already
1009 * been allocated by the map tree or the original allocation
1010 */
1011 btrfs_release_path(root, path);
1012 BUG_ON(ins->objectid < search_start);
1013
1014 if (ins->objectid + num_bytes >= search_end)
1015 goto enospc;
1016
1017 if (!full_scan && ins->objectid + num_bytes >= block_group->
1018 key.objectid + block_group->key.offset) {
1019 search_start = block_group->key.objectid +
1020 block_group->key.offset;
1021 goto new_group;
1022 }
1023 if (test_range_bit(&info->extent_ins, ins->objectid,
1024 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1025 search_start = ins->objectid + num_bytes;
1026 goto new_group;
1027 }
1028 if (test_range_bit(&info->pinned_extents, ins->objectid,
1029 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1030 search_start = ins->objectid + num_bytes;
1031 goto new_group;
1032 }
1033 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1034 ins->objectid < exclude_start + exclude_nr)) {
1035 search_start = exclude_start + exclude_nr;
1036 goto new_group;
1037 }
1038 if (!data) {
1039 block_group = btrfs_lookup_block_group(info, ins->objectid);
1040 if (block_group)
1041 trans->block_group = block_group;
1042 }
1043 ins->offset = num_bytes;
1044 btrfs_free_path(path);
1045 return 0;
1046
1047new_group:
1048 if (search_start + num_bytes >= search_end) {
1049enospc:
1050 search_start = orig_search_start;
1051 if (full_scan) {
1052 ret = -ENOSPC;
1053 goto error;
1054 }
1055 if (wrapped) {
1056 if (!full_scan)
1057 total_needed -= empty_size;
1058 full_scan = 1;
1059 } else
1060 wrapped = 1;
1061 }
1062 block_group = btrfs_lookup_block_group(info, search_start);
1063 cond_resched();
1064 if (!full_scan)
1065 block_group = btrfs_find_block_group(root, block_group,
1066 search_start, data, 0);
1067 goto check_failed;
1068
1069error:
1070 btrfs_release_path(root, path);
1071 btrfs_free_path(path);
1072 return ret;
1073}
1074/*
1075 * finds a free extent and does all the dirty work required for allocation
1076 * returns the key for the extent through ins, and a tree buffer for
1077 * the first block of the extent through buf.
1078 *
1079 * returns 0 if everything worked, non-zero otherwise.
1080 */
1081int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1082 struct btrfs_root *root, u64 owner,
1083 u64 num_bytes, u64 empty_size, u64 hint_byte,
1084 u64 search_end, struct btrfs_key *ins, int data)
1085{
1086 int ret;
1087 int pending_ret;
1088 u64 super_used, root_used;
1089 u64 search_start = 0;
1090 struct btrfs_fs_info *info = root->fs_info;
1091 struct btrfs_root *extent_root = info->extent_root;
1092 struct btrfs_extent_item extent_item;
1093
1094 btrfs_set_stack_extent_refs(&extent_item, 1);
1095 btrfs_set_stack_extent_owner(&extent_item, owner);
1096
1097 WARN_ON(num_bytes < root->sectorsize);
1098 ret = find_free_extent(trans, root, num_bytes, empty_size,
1099 search_start, search_end, hint_byte, ins,
1100 trans->alloc_exclude_start,
1101 trans->alloc_exclude_nr, data);
1102 BUG_ON(ret);
1103 if (ret)
1104 return ret;
1105
1106 /* block accounting for super block */
1107 super_used = btrfs_super_bytes_used(&info->super_copy);
1108 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1109
1110 /* block accounting for root item */
1111 root_used = btrfs_root_used(&root->root_item);
1112 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1113
1114 clear_extent_dirty(&root->fs_info->free_space_cache,
1115 ins->objectid, ins->objectid + ins->offset - 1,
1116 GFP_NOFS);
1117
1118 if (root == extent_root) {
1119 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1120 ins->objectid + ins->offset - 1,
1121 EXTENT_LOCKED, GFP_NOFS);
1122 WARN_ON(data == 1);
1123 goto update_block;
1124 }
1125
1126 WARN_ON(trans->alloc_exclude_nr);
1127 trans->alloc_exclude_start = ins->objectid;
1128 trans->alloc_exclude_nr = ins->offset;
1129 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1130 sizeof(extent_item));
1131
1132 trans->alloc_exclude_start = 0;
1133 trans->alloc_exclude_nr = 0;
1134
1135 BUG_ON(ret);
1136 finish_current_insert(trans, extent_root);
1137 pending_ret = del_pending_extents(trans, extent_root);
1138
1139 if (ret) {
1140 return ret;
1141 }
1142 if (pending_ret) {
1143 return pending_ret;
1144 }
1145
1146update_block:
1147 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1148 data);
1149 BUG_ON(ret);
1150 return 0;
1151}
1152
1153/*
1154 * helper function to allocate a block for a given tree
1155 * returns the tree buffer or NULL.
1156 */
1157struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1158 struct btrfs_root *root,
1159 u32 blocksize, u64 hint,
1160 u64 empty_size)
1161{
1162 struct btrfs_key ins;
1163 int ret;
1164 struct extent_buffer *buf;
1165
1166 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1167 blocksize, empty_size, hint,
1168 (u64)-1, &ins, 0);
1169 if (ret) {
1170 BUG_ON(ret > 0);
1171 return ERR_PTR(ret);
1172 }
1173 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1174 if (!buf) {
1175 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1176 return ERR_PTR(-ENOMEM);
1177 }
1178 btrfs_set_buffer_uptodate(buf);
1179 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1180 buf->start + buf->len - 1, GFP_NOFS);
1181 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1182 buf->start, buf->start + buf->len - 1,
1183 EXTENT_CSUM, GFP_NOFS);
1184 buf->flags |= EXTENT_CSUM;
1185 btrfs_set_buffer_defrag(buf);
1186 trans->blocks_used++;
1187 return buf;
1188}
1189
1190static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root, struct extent_buffer *leaf)
1192{
1193 struct btrfs_key key;
1194 struct btrfs_file_extent_item *fi;
1195 int i;
1196 int nritems;
1197 int ret;
1198
1199 BUG_ON(!btrfs_is_leaf(leaf));
1200 nritems = btrfs_header_nritems(leaf);
1201 for (i = 0; i < nritems; i++) {
1202 u64 disk_bytenr;
1203
1204 btrfs_item_key_to_cpu(leaf, &key, i);
1205 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1206 continue;
1207 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1208 if (btrfs_file_extent_type(leaf, fi) ==
1209 BTRFS_FILE_EXTENT_INLINE)
1210 continue;
1211 /*
1212 * FIXME make sure to insert a trans record that
1213 * repeats the snapshot del on crash
1214 */
1215 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1216 if (disk_bytenr == 0)
1217 continue;
1218 ret = btrfs_free_extent(trans, root, disk_bytenr,
1219 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1220 BUG_ON(ret);
1221 }
1222 return 0;
1223}
1224
1225static void reada_walk_down(struct btrfs_root *root,
1226 struct extent_buffer *node)
1227{
1228 int i;
1229 u32 nritems;
1230 u64 bytenr;
1231 int ret;
1232 u32 refs;
1233 int level;
1234 u32 blocksize;
1235
1236 nritems = btrfs_header_nritems(node);
1237 level = btrfs_header_level(node);
1238 for (i = 0; i < nritems; i++) {
1239 bytenr = btrfs_node_blockptr(node, i);
1240 blocksize = btrfs_level_size(root, level - 1);
1241 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1242 BUG_ON(ret);
1243 if (refs != 1)
1244 continue;
1245 mutex_unlock(&root->fs_info->fs_mutex);
1246 ret = readahead_tree_block(root, bytenr, blocksize);
1247 cond_resched();
1248 mutex_lock(&root->fs_info->fs_mutex);
1249 if (ret)
1250 break;
1251 }
1252}
1253
1254/*
1255 * helper function for drop_snapshot, this walks down the tree dropping ref
1256 * counts as it goes.
1257 */
1258static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1259 *root, struct btrfs_path *path, int *level)
1260{
1261 struct extent_buffer *next;
1262 struct extent_buffer *cur;
1263 u64 bytenr;
1264 u32 blocksize;
1265 int ret;
1266 u32 refs;
1267
1268 WARN_ON(*level < 0);
1269 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1270 ret = lookup_extent_ref(trans, root,
1271 path->nodes[*level]->start,
1272 path->nodes[*level]->len, &refs);
1273 BUG_ON(ret);
1274 if (refs > 1)
1275 goto out;
1276
1277 /*
1278 * walk down to the last node level and free all the leaves
1279 */
1280 while(*level >= 0) {
1281 WARN_ON(*level < 0);
1282 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1283 cur = path->nodes[*level];
1284
1285 if (*level > 0 && path->slots[*level] == 0)
1286 reada_walk_down(root, cur);
1287
1288 if (btrfs_header_level(cur) != *level)
1289 WARN_ON(1);
1290
1291 if (path->slots[*level] >=
1292 btrfs_header_nritems(cur))
1293 break;
1294 if (*level == 0) {
1295 ret = drop_leaf_ref(trans, root, cur);
1296 BUG_ON(ret);
1297 break;
1298 }
1299 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1300 blocksize = btrfs_level_size(root, *level - 1);
1301 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1302 BUG_ON(ret);
1303 if (refs != 1) {
1304 path->slots[*level]++;
1305 ret = btrfs_free_extent(trans, root, bytenr,
1306 blocksize, 1);
1307 BUG_ON(ret);
1308 continue;
1309 }
1310 next = btrfs_find_tree_block(root, bytenr, blocksize);
1311 if (!next || !btrfs_buffer_uptodate(next)) {
1312 free_extent_buffer(next);
1313 mutex_unlock(&root->fs_info->fs_mutex);
1314 next = read_tree_block(root, bytenr, blocksize);
1315 mutex_lock(&root->fs_info->fs_mutex);
1316
1317 /* we dropped the lock, check one more time */
1318 ret = lookup_extent_ref(trans, root, bytenr,
1319 blocksize, &refs);
1320 BUG_ON(ret);
1321 if (refs != 1) {
1322 path->slots[*level]++;
1323 free_extent_buffer(next);
1324 ret = btrfs_free_extent(trans, root,
1325 bytenr, blocksize, 1);
1326 BUG_ON(ret);
1327 continue;
1328 }
1329 }
1330 WARN_ON(*level <= 0);
1331 if (path->nodes[*level-1])
1332 free_extent_buffer(path->nodes[*level-1]);
1333 path->nodes[*level-1] = next;
1334 *level = btrfs_header_level(next);
1335 path->slots[*level] = 0;
1336 }
1337out:
1338 WARN_ON(*level < 0);
1339 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1340 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1341 path->nodes[*level]->len, 1);
1342 free_extent_buffer(path->nodes[*level]);
1343 path->nodes[*level] = NULL;
1344 *level += 1;
1345 BUG_ON(ret);
1346 return 0;
1347}
1348
1349/*
1350 * helper for dropping snapshots. This walks back up the tree in the path
1351 * to find the first node higher up where we haven't yet gone through
1352 * all the slots
1353 */
1354static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1355 *root, struct btrfs_path *path, int *level)
1356{
1357 int i;
1358 int slot;
1359 int ret;
1360 struct btrfs_root_item *root_item = &root->root_item;
1361
1362 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1363 slot = path->slots[i];
1364 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1365 struct extent_buffer *node;
1366 struct btrfs_disk_key disk_key;
1367 node = path->nodes[i];
1368 path->slots[i]++;
1369 *level = i;
1370 WARN_ON(*level == 0);
1371 btrfs_node_key(node, &disk_key, path->slots[i]);
1372 memcpy(&root_item->drop_progress,
1373 &disk_key, sizeof(disk_key));
1374 root_item->drop_level = i;
1375 return 0;
1376 } else {
1377 ret = btrfs_free_extent(trans, root,
1378 path->nodes[*level]->start,
1379 path->nodes[*level]->len, 1);
1380 BUG_ON(ret);
1381 free_extent_buffer(path->nodes[*level]);
1382 path->nodes[*level] = NULL;
1383 *level = i + 1;
1384 }
1385 }
1386 return 1;
1387}
1388
1389/*
1390 * drop the reference count on the tree rooted at 'snap'. This traverses
1391 * the tree freeing any blocks that have a ref count of zero after being
1392 * decremented.
1393 */
1394int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1395 *root)
1396{
1397 int ret = 0;
1398 int wret;
1399 int level;
1400 struct btrfs_path *path;
1401 int i;
1402 int orig_level;
1403 struct btrfs_root_item *root_item = &root->root_item;
1404
1405 path = btrfs_alloc_path();
1406 BUG_ON(!path);
1407
1408 level = btrfs_header_level(root->node);
1409 orig_level = level;
1410 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1411 path->nodes[level] = root->node;
1412 extent_buffer_get(root->node);
1413 path->slots[level] = 0;
1414 } else {
1415 struct btrfs_key key;
1416 struct btrfs_disk_key found_key;
1417 struct extent_buffer *node;
1418
1419 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1420 level = root_item->drop_level;
1421 path->lowest_level = level;
1422 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1423 if (wret < 0) {
1424 ret = wret;
1425 goto out;
1426 }
1427 node = path->nodes[level];
1428 btrfs_node_key(node, &found_key, path->slots[level]);
1429 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1430 sizeof(found_key)));
1431 }
1432 while(1) {
1433 wret = walk_down_tree(trans, root, path, &level);
1434 if (wret > 0)
1435 break;
1436 if (wret < 0)
1437 ret = wret;
1438
1439 wret = walk_up_tree(trans, root, path, &level);
1440 if (wret > 0)
1441 break;
1442 if (wret < 0)
1443 ret = wret;
1444 ret = -EAGAIN;
1445 break;
1446 }
1447 for (i = 0; i <= orig_level; i++) {
1448 if (path->nodes[i]) {
1449 free_extent_buffer(path->nodes[i]);
1450 path->nodes[i] = NULL;
1451 }
1452 }
1453out:
1454 btrfs_free_path(path);
1455 return ret;
1456}
1457
1458int btrfs_free_block_groups(struct btrfs_fs_info *info)
1459{
1460 u64 start;
1461 u64 end;
1462 int ret;
1463
1464 while(1) {
1465 ret = find_first_extent_bit(&info->block_group_cache, 0,
1466 &start, &end, (unsigned int)-1);
1467 if (ret)
1468 break;
1469 clear_extent_bits(&info->block_group_cache, start,
1470 end, (unsigned int)-1, GFP_NOFS);
1471 }
1472 while(1) {
1473 ret = find_first_extent_bit(&info->free_space_cache, 0,
1474 &start, &end, EXTENT_DIRTY);
1475 if (ret)
1476 break;
1477 clear_extent_dirty(&info->free_space_cache, start,
1478 end, GFP_NOFS);
1479 }
1480 return 0;
1481}
1482
1483int btrfs_read_block_groups(struct btrfs_root *root)
1484{
1485 struct btrfs_path *path;
1486 int ret;
1487 int err = 0;
1488 int bit;
1489 struct btrfs_block_group_cache *cache;
1490 struct btrfs_fs_info *info = root->fs_info;
1491 struct extent_map_tree *block_group_cache;
1492 struct btrfs_key key;
1493 struct btrfs_key found_key;
1494 struct extent_buffer *leaf;
1495
1496 block_group_cache = &info->block_group_cache;
1497
1498 root = info->extent_root;
1499 key.objectid = 0;
1500 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1501 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1502
1503 path = btrfs_alloc_path();
1504 if (!path)
1505 return -ENOMEM;
1506
1507 while(1) {
1508 ret = btrfs_search_slot(NULL, info->extent_root,
1509 &key, path, 0, 0);
1510 if (ret != 0) {
1511 err = ret;
1512 break;
1513 }
1514 leaf = path->nodes[0];
1515 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1516 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1517 if (!cache) {
1518 err = -1;
1519 break;
1520 }
1521
1522 read_extent_buffer(leaf, &cache->item,
1523 btrfs_item_ptr_offset(leaf, path->slots[0]),
1524 sizeof(cache->item));
1525 memcpy(&cache->key, &found_key, sizeof(found_key));
1526 cache->cached = 0;
1527
1528 key.objectid = found_key.objectid + found_key.offset;
1529 btrfs_release_path(root, path);
1530
1531 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1532 bit = BLOCK_GROUP_DATA;
1533 cache->data = 1;
1534 } else {
1535 bit = BLOCK_GROUP_METADATA;
1536 cache->data = 0;
1537 }
1538
1539 /* use EXTENT_LOCKED to prevent merging */
1540 set_extent_bits(block_group_cache, found_key.objectid,
1541 found_key.objectid + found_key.offset - 1,
1542 bit | EXTENT_LOCKED, GFP_NOFS);
1543 set_state_private(block_group_cache, found_key.objectid,
1544 (u64)cache);
1545
1546 if (key.objectid >=
1547 btrfs_super_total_bytes(&info->super_copy))
1548 break;
1549 }
1550
1551 btrfs_free_path(path);
1552 return 0;
1553}