2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/sched.h>
22 #include "print-tree.h"
23 #include "transaction.h"
25 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
26 btrfs_root
*extent_root
);
27 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
28 btrfs_root
*extent_root
);
30 static int cache_block_group(struct btrfs_root
*root
,
31 struct btrfs_block_group_cache
*block_group
)
33 struct btrfs_path
*path
;
36 struct extent_buffer
*leaf
;
37 struct radix_tree_root
*extent_radix
;
45 root
= root
->fs_info
->extent_root
;
46 extent_radix
= &root
->fs_info
->extent_map_radix
;
48 if (block_group
->cached
)
50 if (block_group
->data
)
52 path
= btrfs_alloc_path();
57 first_free
= block_group
->key
.objectid
;
58 key
.objectid
= block_group
->key
.objectid
;
61 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
62 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
67 if (ret
&& path
->slots
[0] > 0)
71 leaf
= path
->nodes
[0];
72 slot
= path
->slots
[0];
73 if (slot
>= btrfs_header_nritems(leaf
)) {
74 ret
= btrfs_next_leaf(root
, path
);
84 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
85 if (key
.objectid
< block_group
->key
.objectid
) {
86 if (key
.objectid
+ key
.offset
> first_free
)
87 first_free
= key
.objectid
+ key
.offset
;
91 if (key
.objectid
>= block_group
->key
.objectid
+
92 block_group
->key
.offset
) {
96 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
101 hole_size
= key
.objectid
- last
;
102 for (i
= 0; i
< hole_size
; i
++) {
103 set_radix_bit(extent_radix
, last
+ i
);
105 last
= key
.objectid
+ key
.offset
;
113 if (block_group
->key
.objectid
+
114 block_group
->key
.offset
> last
) {
115 hole_size
= block_group
->key
.objectid
+
116 block_group
->key
.offset
- last
;
117 for (i
= 0; i
< hole_size
; i
++) {
118 set_radix_bit(extent_radix
, last
+ i
);
121 block_group
->cached
= 1;
123 btrfs_free_path(path
);
127 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
131 struct btrfs_block_group_cache
*block_group
;
134 ret
= radix_tree_gang_lookup(&info
->block_group_radix
,
135 (void **)&block_group
,
138 if (block_group
->key
.objectid
<= blocknr
&& blocknr
<=
139 block_group
->key
.objectid
+ block_group
->key
.offset
)
142 ret
= radix_tree_gang_lookup(&info
->block_group_data_radix
,
143 (void **)&block_group
,
146 if (block_group
->key
.objectid
<= blocknr
&& blocknr
<=
147 block_group
->key
.objectid
+ block_group
->key
.offset
)
153 static u64
leaf_range(struct btrfs_root
*root
)
155 u64 size
= BTRFS_LEAF_DATA_SIZE(root
);
156 do_div(size
, sizeof(struct btrfs_extent_item
) +
157 sizeof(struct btrfs_item
));
161 static u64
find_search_start(struct btrfs_root
*root
,
162 struct btrfs_block_group_cache
**cache_ret
,
163 u64 search_start
, int num
)
165 unsigned long gang
[8];
167 struct btrfs_block_group_cache
*cache
= *cache_ret
;
168 u64 last
= max(search_start
, cache
->key
.objectid
);
173 ret
= cache_block_group(root
, cache
);
177 ret
= find_first_radix_bit(&root
->fs_info
->extent_map_radix
,
178 gang
, last
, ARRAY_SIZE(gang
));
181 last
= gang
[ret
-1] + 1;
183 if (ret
!= ARRAY_SIZE(gang
)) {
186 if (gang
[ret
-1] - gang
[0] > leaf_range(root
)) {
190 if (gang
[0] >= cache
->key
.objectid
+ cache
->key
.offset
) {
196 return max(cache
->last_alloc
, search_start
);
199 cache
= btrfs_lookup_block_group(root
->fs_info
,
200 last
+ cache
->key
.offset
- 1);
202 return max((*cache_ret
)->last_alloc
, search_start
);
204 cache
= btrfs_find_block_group(root
, cache
,
205 last
+ cache
->key
.offset
- 1, 0, 0);
210 static u64
div_factor(u64 num
, int factor
)
217 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
218 struct btrfs_block_group_cache
219 *hint
, u64 search_start
,
222 struct btrfs_block_group_cache
*cache
[8];
223 struct btrfs_block_group_cache
*found_group
= NULL
;
224 struct btrfs_fs_info
*info
= root
->fs_info
;
225 struct radix_tree_root
*radix
;
226 struct radix_tree_root
*swap_radix
;
240 radix
= &info
->block_group_data_radix
;
241 swap_radix
= &info
->block_group_radix
;
243 radix
= &info
->block_group_radix
;
244 swap_radix
= &info
->block_group_data_radix
;
248 struct btrfs_block_group_cache
*shint
;
249 shint
= btrfs_lookup_block_group(info
, search_start
);
250 if (shint
&& shint
->data
== data
) {
251 used
= btrfs_block_group_used(&shint
->item
);
252 if (used
+ shint
->pinned
<
253 div_factor(shint
->key
.offset
, factor
)) {
258 if (hint
&& hint
->data
== data
) {
259 used
= btrfs_block_group_used(&hint
->item
);
260 if (used
+ hint
->pinned
<
261 div_factor(hint
->key
.offset
, factor
)) {
264 if (used
>= div_factor(hint
->key
.offset
, 8)) {
265 radix_tree_tag_clear(radix
,
267 hint
->key
.offset
- 1,
268 BTRFS_BLOCK_GROUP_AVAIL
);
270 last
= hint
->key
.offset
* 3;
271 if (hint
->key
.objectid
>= last
)
272 last
= max(search_start
+ hint
->key
.offset
- 1,
273 hint
->key
.objectid
- last
);
275 last
= hint
->key
.objectid
+ hint
->key
.offset
;
279 hint_last
= max(hint
->key
.objectid
, search_start
);
281 hint_last
= search_start
;
286 ret
= radix_tree_gang_lookup_tag(radix
, (void **)cache
,
287 last
, ARRAY_SIZE(cache
),
288 BTRFS_BLOCK_GROUP_AVAIL
);
291 for (i
= 0; i
< ret
; i
++) {
292 last
= cache
[i
]->key
.objectid
+
293 cache
[i
]->key
.offset
;
294 used
= btrfs_block_group_used(&cache
[i
]->item
);
295 if (used
+ cache
[i
]->pinned
<
296 div_factor(cache
[i
]->key
.offset
, factor
)) {
297 found_group
= cache
[i
];
300 if (used
>= div_factor(cache
[i
]->key
.offset
, 8)) {
301 radix_tree_tag_clear(radix
,
302 cache
[i
]->key
.objectid
+
303 cache
[i
]->key
.offset
- 1,
304 BTRFS_BLOCK_GROUP_AVAIL
);
312 ret
= radix_tree_gang_lookup(radix
, (void **)cache
,
313 last
, ARRAY_SIZE(cache
));
316 for (i
= 0; i
< ret
; i
++) {
317 last
= cache
[i
]->key
.objectid
+
318 cache
[i
]->key
.offset
;
319 used
= btrfs_block_group_used(&cache
[i
]->item
);
320 if (used
+ cache
[i
]->pinned
< cache
[i
]->key
.offset
) {
321 found_group
= cache
[i
];
324 if (used
>= cache
[i
]->key
.offset
) {
325 radix_tree_tag_clear(radix
,
326 cache
[i
]->key
.objectid
+
327 cache
[i
]->key
.offset
- 1,
328 BTRFS_BLOCK_GROUP_AVAIL
);
339 struct radix_tree_root
*tmp
= radix
;
347 ret
= radix_tree_gang_lookup(radix
,
348 (void **)&found_group
, 0, 1);
350 ret
= radix_tree_gang_lookup(swap_radix
,
351 (void **)&found_group
,
360 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
361 struct btrfs_root
*root
,
362 u64 blocknr
, u64 num_blocks
)
364 struct btrfs_path
*path
;
366 struct btrfs_key key
;
367 struct extent_buffer
*l
;
368 struct btrfs_extent_item
*item
;
371 path
= btrfs_alloc_path();
375 key
.objectid
= blocknr
;
376 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
377 key
.offset
= num_blocks
;
378 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
387 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
388 refs
= btrfs_extent_refs(l
, item
);
389 btrfs_set_extent_refs(l
, item
, refs
+ 1);
390 btrfs_mark_buffer_dirty(path
->nodes
[0]);
392 btrfs_release_path(root
->fs_info
->extent_root
, path
);
393 btrfs_free_path(path
);
394 finish_current_insert(trans
, root
->fs_info
->extent_root
);
395 del_pending_extents(trans
, root
->fs_info
->extent_root
);
399 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
400 struct btrfs_root
*root
)
402 finish_current_insert(trans
, root
->fs_info
->extent_root
);
403 del_pending_extents(trans
, root
->fs_info
->extent_root
);
407 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
408 struct btrfs_root
*root
, u64 blocknr
,
409 u64 num_blocks
, u32
*refs
)
411 struct btrfs_path
*path
;
413 struct btrfs_key key
;
414 struct extent_buffer
*l
;
415 struct btrfs_extent_item
*item
;
417 path
= btrfs_alloc_path();
418 key
.objectid
= blocknr
;
419 key
.offset
= num_blocks
;
420 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
421 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
426 btrfs_print_leaf(root
, path
->nodes
[0]);
427 printk("failed to find block number %Lu\n", blocknr
);
431 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
432 *refs
= btrfs_extent_refs(l
, item
);
434 btrfs_free_path(path
);
438 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
439 struct btrfs_root
*root
)
441 return btrfs_inc_extent_ref(trans
, root
,
442 extent_buffer_blocknr(root
->node
), 1);
445 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
446 struct extent_buffer
*buf
)
450 struct btrfs_key key
;
451 struct btrfs_file_extent_item
*fi
;
461 leaf
= btrfs_is_leaf(buf
);
462 nritems
= btrfs_header_nritems(buf
);
463 for (i
= 0; i
< nritems
; i
++) {
466 btrfs_item_key_to_cpu(buf
, &key
, i
);
467 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
469 fi
= btrfs_item_ptr(buf
, i
,
470 struct btrfs_file_extent_item
);
471 if (btrfs_file_extent_type(buf
, fi
) ==
472 BTRFS_FILE_EXTENT_INLINE
)
474 disk_blocknr
= btrfs_file_extent_disk_blocknr(buf
, fi
);
475 if (disk_blocknr
== 0)
477 ret
= btrfs_inc_extent_ref(trans
, root
, disk_blocknr
,
478 btrfs_file_extent_disk_num_blocks(buf
, fi
));
484 blocknr
= btrfs_node_blockptr(buf
, i
);
485 ret
= btrfs_inc_extent_ref(trans
, root
, blocknr
, 1);
495 for (i
=0; i
< faili
; i
++) {
498 btrfs_item_key_to_cpu(buf
, &key
, i
);
499 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
501 fi
= btrfs_item_ptr(buf
, i
,
502 struct btrfs_file_extent_item
);
503 if (btrfs_file_extent_type(buf
, fi
) ==
504 BTRFS_FILE_EXTENT_INLINE
)
506 disk_blocknr
= btrfs_file_extent_disk_blocknr(buf
, fi
);
507 if (disk_blocknr
== 0)
509 err
= btrfs_free_extent(trans
, root
, disk_blocknr
,
510 btrfs_file_extent_disk_num_blocks(buf
,
514 blocknr
= btrfs_node_blockptr(buf
, i
);
515 err
= btrfs_free_extent(trans
, root
, blocknr
, 1, 0);
522 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
523 struct btrfs_root
*root
,
524 struct btrfs_path
*path
,
525 struct btrfs_block_group_cache
*cache
)
529 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
531 struct extent_buffer
*leaf
;
533 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
538 leaf
= path
->nodes
[0];
539 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
540 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
541 btrfs_mark_buffer_dirty(leaf
);
542 btrfs_release_path(extent_root
, path
);
544 finish_current_insert(trans
, extent_root
);
545 pending_ret
= del_pending_extents(trans
, extent_root
);
551 cache
->last_alloc
= cache
->first_free
;
556 static int write_dirty_block_radix(struct btrfs_trans_handle
*trans
,
557 struct btrfs_root
*root
,
558 struct radix_tree_root
*radix
)
560 struct btrfs_block_group_cache
*cache
[8];
565 struct btrfs_path
*path
;
566 unsigned long off
= 0;
568 path
= btrfs_alloc_path();
573 ret
= radix_tree_gang_lookup_tag(radix
, (void **)cache
,
574 off
, ARRAY_SIZE(cache
),
575 BTRFS_BLOCK_GROUP_DIRTY
);
578 for (i
= 0; i
< ret
; i
++) {
579 err
= write_one_cache_group(trans
, root
,
582 * if we fail to write the cache group, we want
583 * to keep it marked dirty in hopes that a later
588 off
= cache
[i
]->key
.objectid
+
589 cache
[i
]->key
.offset
;
593 radix_tree_tag_clear(radix
, cache
[i
]->key
.objectid
+
594 cache
[i
]->key
.offset
- 1,
595 BTRFS_BLOCK_GROUP_DIRTY
);
598 btrfs_free_path(path
);
602 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
603 struct btrfs_root
*root
)
607 ret
= write_dirty_block_radix(trans
, root
,
608 &root
->fs_info
->block_group_radix
);
609 ret2
= write_dirty_block_radix(trans
, root
,
610 &root
->fs_info
->block_group_data_radix
);
618 static int update_block_group(struct btrfs_trans_handle
*trans
,
619 struct btrfs_root
*root
,
620 u64 blocknr
, u64 num
, int alloc
, int mark_free
,
623 struct btrfs_block_group_cache
*cache
;
624 struct btrfs_fs_info
*info
= root
->fs_info
;
632 cache
= btrfs_lookup_block_group(info
, blocknr
);
636 block_in_group
= blocknr
- cache
->key
.objectid
;
637 WARN_ON(block_in_group
> cache
->key
.offset
);
638 radix_tree_tag_set(cache
->radix
, cache
->key
.objectid
+
639 cache
->key
.offset
- 1,
640 BTRFS_BLOCK_GROUP_DIRTY
);
642 old_val
= btrfs_block_group_used(&cache
->item
);
643 num
= min(total
, cache
->key
.offset
- block_in_group
);
645 if (blocknr
> cache
->last_alloc
)
646 cache
->last_alloc
= blocknr
;
648 for (i
= 0; i
< num
; i
++) {
649 clear_radix_bit(&info
->extent_map_radix
,
653 if (cache
->data
!= data
&&
654 old_val
< (cache
->key
.offset
>> 1)) {
656 radix_tree_delete(cache
->radix
,
657 cache
->key
.objectid
+
658 cache
->key
.offset
- 1);
662 &info
->block_group_data_radix
;
664 BTRFS_BLOCK_GROUP_DATA
;
666 cache
->radix
= &info
->block_group_radix
;
668 ~BTRFS_BLOCK_GROUP_DATA
;
670 ret
= radix_tree_insert(cache
->radix
,
671 cache
->key
.objectid
+
672 cache
->key
.offset
- 1,
678 if (blocknr
< cache
->first_free
)
679 cache
->first_free
= blocknr
;
680 if (!cache
->data
&& mark_free
) {
681 for (i
= 0; i
< num
; i
++) {
682 set_radix_bit(&info
->extent_map_radix
,
686 if (old_val
< (cache
->key
.offset
>> 1) &&
687 old_val
+ num
>= (cache
->key
.offset
>> 1)) {
688 radix_tree_tag_set(cache
->radix
,
689 cache
->key
.objectid
+
690 cache
->key
.offset
- 1,
691 BTRFS_BLOCK_GROUP_AVAIL
);
694 btrfs_set_block_group_used(&cache
->item
, old_val
);
701 int btrfs_copy_pinned(struct btrfs_root
*root
, struct radix_tree_root
*copy
)
703 unsigned long gang
[8];
705 struct radix_tree_root
*pinned_radix
= &root
->fs_info
->pinned_radix
;
710 ret
= find_first_radix_bit(pinned_radix
, gang
, last
,
714 for (i
= 0 ; i
< ret
; i
++) {
715 set_radix_bit(copy
, gang
[i
]);
719 ret
= find_first_radix_bit(&root
->fs_info
->extent_ins_radix
, gang
, 0,
725 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
726 struct btrfs_root
*root
,
727 struct radix_tree_root
*unpin_radix
)
729 unsigned long gang
[8];
730 struct btrfs_block_group_cache
*block_group
;
734 struct radix_tree_root
*pinned_radix
= &root
->fs_info
->pinned_radix
;
735 struct radix_tree_root
*extent_radix
= &root
->fs_info
->extent_map_radix
;
738 ret
= find_first_radix_bit(unpin_radix
, gang
, 0,
744 for (i
= 0; i
< ret
; i
++) {
745 clear_radix_bit(pinned_radix
, gang
[i
]);
746 clear_radix_bit(unpin_radix
, gang
[i
]);
747 block_group
= btrfs_lookup_block_group(root
->fs_info
,
750 WARN_ON(block_group
->pinned
== 0);
751 block_group
->pinned
--;
752 if (gang
[i
] < block_group
->last_alloc
)
753 block_group
->last_alloc
= gang
[i
];
754 if (!block_group
->data
)
755 set_radix_bit(extent_radix
, gang
[i
]);
762 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
763 btrfs_root
*extent_root
)
765 struct btrfs_key ins
;
766 struct btrfs_extent_item extent_item
;
770 unsigned long gang
[8];
771 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
773 btrfs_set_stack_extent_refs(&extent_item
, 1);
775 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
776 btrfs_set_stack_extent_owner(&extent_item
,
777 extent_root
->root_key
.objectid
);
780 ret
= find_first_radix_bit(&info
->extent_ins_radix
, gang
, 0,
785 for (i
= 0; i
< ret
; i
++) {
786 ins
.objectid
= gang
[i
];
787 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
789 sizeof(extent_item
));
790 clear_radix_bit(&info
->extent_ins_radix
, gang
[i
]);
797 static int pin_down_block(struct btrfs_root
*root
, u64 blocknr
, int pending
)
800 struct extent_buffer
*buf
;
803 buf
= btrfs_find_tree_block(root
, blocknr
);
805 if (btrfs_buffer_uptodate(buf
)) {
807 root
->fs_info
->running_transaction
->transid
;
808 if (btrfs_header_generation(buf
) == transid
) {
809 free_extent_buffer(buf
);
813 free_extent_buffer(buf
);
815 err
= set_radix_bit(&root
->fs_info
->pinned_radix
, blocknr
);
817 struct btrfs_block_group_cache
*cache
;
818 cache
= btrfs_lookup_block_group(root
->fs_info
,
824 err
= set_radix_bit(&root
->fs_info
->pending_del_radix
, blocknr
);
831 * remove an extent from the root, returns 0 on success
833 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
834 *root
, u64 blocknr
, u64 num_blocks
, int pin
,
837 struct btrfs_path
*path
;
838 struct btrfs_key key
;
839 struct btrfs_fs_info
*info
= root
->fs_info
;
840 struct btrfs_root
*extent_root
= info
->extent_root
;
841 struct extent_buffer
*leaf
;
843 struct btrfs_extent_item
*ei
;
846 key
.objectid
= blocknr
;
847 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
848 key
.offset
= num_blocks
;
850 path
= btrfs_alloc_path();
854 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
859 leaf
= path
->nodes
[0];
860 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
861 struct btrfs_extent_item
);
862 refs
= btrfs_extent_refs(leaf
, ei
);
865 btrfs_set_extent_refs(leaf
, ei
, refs
);
866 btrfs_mark_buffer_dirty(leaf
);
869 u64 super_blocks_used
, root_blocks_used
;
872 ret
= pin_down_block(root
, blocknr
, 0);
876 /* block accounting for super block */
877 super_blocks_used
= btrfs_super_blocks_used(&info
->super_copy
);
878 btrfs_set_super_blocks_used(&info
->super_copy
,
879 super_blocks_used
- num_blocks
);
881 /* block accounting for root item */
882 root_blocks_used
= btrfs_root_used(&root
->root_item
);
883 btrfs_set_root_used(&root
->root_item
,
884 root_blocks_used
- num_blocks
);
886 ret
= btrfs_del_item(trans
, extent_root
, path
);
890 ret
= update_block_group(trans
, root
, blocknr
, num_blocks
, 0,
894 btrfs_free_path(path
);
895 finish_current_insert(trans
, extent_root
);
900 * find all the blocks marked as pending in the radix tree and remove
901 * them from the extent map
903 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
904 btrfs_root
*extent_root
)
909 unsigned long gang
[4];
911 struct radix_tree_root
*pending_radix
;
912 struct radix_tree_root
*pinned_radix
;
913 struct btrfs_block_group_cache
*cache
;
915 pending_radix
= &extent_root
->fs_info
->pending_del_radix
;
916 pinned_radix
= &extent_root
->fs_info
->pinned_radix
;
919 ret
= find_first_radix_bit(pending_radix
, gang
, 0,
923 for (i
= 0; i
< ret
; i
++) {
924 wret
= set_radix_bit(pinned_radix
, gang
[i
]);
927 btrfs_lookup_block_group(extent_root
->fs_info
,
933 printk(KERN_CRIT
"set_radix_bit, err %d\n",
937 wret
= clear_radix_bit(pending_radix
, gang
[i
]);
939 wret
= __free_extent(trans
, extent_root
,
949 * remove an extent from the root, returns 0 on success
951 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
952 *root
, u64 blocknr
, u64 num_blocks
, int pin
)
954 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
958 if (root
== extent_root
) {
959 pin_down_block(root
, blocknr
, 1);
962 ret
= __free_extent(trans
, root
, blocknr
, num_blocks
, pin
, pin
== 0);
963 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
964 return ret
? ret
: pending_ret
;
968 * walks the btree of allocated extents and find a hole of a given size.
969 * The key ins is changed to record the hole:
970 * ins->objectid == block start
971 * ins->flags = BTRFS_EXTENT_ITEM_KEY
972 * ins->offset == number of blocks
973 * Any available blocks before search_start are skipped.
975 static int find_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
976 *orig_root
, u64 num_blocks
, u64 empty_size
,
977 u64 search_start
, u64 search_end
, u64 hint_block
,
978 struct btrfs_key
*ins
, u64 exclude_start
,
979 u64 exclude_nr
, int data
)
981 struct btrfs_path
*path
;
982 struct btrfs_key key
;
988 u64 orig_search_start
= search_start
;
990 struct extent_buffer
*l
;
991 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
992 struct btrfs_fs_info
*info
= root
->fs_info
;
993 int total_needed
= num_blocks
;
995 struct btrfs_block_group_cache
*block_group
;
999 WARN_ON(num_blocks
< 1);
1000 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1002 level
= btrfs_header_level(root
->node
);
1004 if (search_end
== (u64
)-1)
1005 search_end
= btrfs_super_total_blocks(&info
->super_copy
);
1007 block_group
= btrfs_lookup_block_group(info
, hint_block
);
1008 block_group
= btrfs_find_block_group(root
, block_group
,
1009 hint_block
, data
, 1);
1011 block_group
= btrfs_find_block_group(root
,
1012 trans
->block_group
, 0,
1016 total_needed
+= empty_size
;
1017 path
= btrfs_alloc_path();
1020 if (!block_group
->data
)
1021 search_start
= find_search_start(root
, &block_group
,
1022 search_start
, total_needed
);
1023 else if (!full_scan
)
1024 search_start
= max(block_group
->last_alloc
, search_start
);
1026 btrfs_init_path(path
);
1027 ins
->objectid
= search_start
;
1032 ret
= btrfs_search_slot(trans
, root
, ins
, path
, 0, 0);
1036 if (path
->slots
[0] > 0) {
1041 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1044 * a rare case, go back one key if we hit a block group item
1045 * instead of an extent item
1047 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
&&
1048 key
.objectid
+ key
.offset
>= search_start
) {
1049 ins
->objectid
= key
.objectid
;
1050 ins
->offset
= key
.offset
- 1;
1051 btrfs_release_path(root
, path
);
1052 ret
= btrfs_search_slot(trans
, root
, ins
, path
, 0, 0);
1056 if (path
->slots
[0] > 0) {
1063 slot
= path
->slots
[0];
1064 if (slot
>= btrfs_header_nritems(l
)) {
1065 ret
= btrfs_next_leaf(root
, path
);
1071 ins
->objectid
= search_start
;
1072 ins
->offset
= search_end
- search_start
;
1076 ins
->objectid
= last_block
> search_start
?
1077 last_block
: search_start
;
1078 ins
->offset
= search_end
- ins
->objectid
;
1082 btrfs_item_key_to_cpu(l
, &key
, slot
);
1083 if (key
.objectid
>= search_start
&& key
.objectid
> last_block
&&
1085 if (last_block
< search_start
)
1086 last_block
= search_start
;
1087 hole_size
= key
.objectid
- last_block
;
1088 if (hole_size
>= num_blocks
) {
1089 ins
->objectid
= last_block
;
1090 ins
->offset
= hole_size
;
1095 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
)
1099 last_block
= key
.objectid
+ key
.offset
;
1100 if (!full_scan
&& last_block
>= block_group
->key
.objectid
+
1101 block_group
->key
.offset
) {
1102 btrfs_release_path(root
, path
);
1103 search_start
= block_group
->key
.objectid
+
1104 block_group
->key
.offset
* 2;
1112 /* we have to make sure we didn't find an extent that has already
1113 * been allocated by the map tree or the original allocation
1115 btrfs_release_path(root
, path
);
1116 BUG_ON(ins
->objectid
< search_start
);
1118 if (ins
->objectid
+ num_blocks
>= search_end
)
1121 for (test_block
= ins
->objectid
;
1122 test_block
< ins
->objectid
+ num_blocks
; test_block
++) {
1123 if (test_radix_bit(&info
->pinned_radix
, test_block
) ||
1124 test_radix_bit(&info
->extent_ins_radix
, test_block
)) {
1125 search_start
= test_block
+ 1;
1129 if (exclude_nr
> 0 && (ins
->objectid
+ num_blocks
> exclude_start
&&
1130 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1131 search_start
= exclude_start
+ exclude_nr
;
1135 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1137 trans
->block_group
= block_group
;
1139 ins
->offset
= num_blocks
;
1140 btrfs_free_path(path
);
1144 if (search_start
+ num_blocks
>= search_end
) {
1146 search_start
= orig_search_start
;
1153 total_needed
-= empty_size
;
1158 block_group
= btrfs_lookup_block_group(info
, search_start
);
1161 block_group
= btrfs_find_block_group(root
, block_group
,
1162 search_start
, data
, 0);
1166 btrfs_release_path(root
, path
);
1167 btrfs_free_path(path
);
1171 * finds a free extent and does all the dirty work required for allocation
1172 * returns the key for the extent through ins, and a tree buffer for
1173 * the first block of the extent through buf.
1175 * returns 0 if everything worked, non-zero otherwise.
1177 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1178 struct btrfs_root
*root
, u64 owner
,
1179 u64 num_blocks
, u64 empty_size
, u64 hint_block
,
1180 u64 search_end
, struct btrfs_key
*ins
, int data
)
1184 u64 super_blocks_used
, root_blocks_used
;
1185 u64 search_start
= 0;
1186 struct btrfs_fs_info
*info
= root
->fs_info
;
1187 struct btrfs_root
*extent_root
= info
->extent_root
;
1188 struct btrfs_extent_item extent_item
;
1190 btrfs_set_stack_extent_refs(&extent_item
, 1);
1191 btrfs_set_stack_extent_owner(&extent_item
, owner
);
1193 WARN_ON(num_blocks
< 1);
1194 ret
= find_free_extent(trans
, root
, num_blocks
, empty_size
,
1195 search_start
, search_end
, hint_block
, ins
,
1196 trans
->alloc_exclude_start
,
1197 trans
->alloc_exclude_nr
, data
);
1202 /* block accounting for super block */
1203 super_blocks_used
= btrfs_super_blocks_used(&info
->super_copy
);
1204 btrfs_set_super_blocks_used(&info
->super_copy
, super_blocks_used
+
1207 /* block accounting for root item */
1208 root_blocks_used
= btrfs_root_used(&root
->root_item
);
1209 btrfs_set_root_used(&root
->root_item
, root_blocks_used
+
1212 if (root
== extent_root
) {
1213 BUG_ON(num_blocks
!= 1);
1214 set_radix_bit(&root
->fs_info
->extent_ins_radix
, ins
->objectid
);
1218 WARN_ON(trans
->alloc_exclude_nr
);
1219 trans
->alloc_exclude_start
= ins
->objectid
;
1220 trans
->alloc_exclude_nr
= ins
->offset
;
1221 ret
= btrfs_insert_item(trans
, extent_root
, ins
, &extent_item
,
1222 sizeof(extent_item
));
1224 trans
->alloc_exclude_start
= 0;
1225 trans
->alloc_exclude_nr
= 0;
1228 finish_current_insert(trans
, extent_root
);
1229 pending_ret
= del_pending_extents(trans
, extent_root
);
1238 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0,
1245 * helper function to allocate a block for a given tree
1246 * returns the tree buffer or NULL.
1248 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1249 struct btrfs_root
*root
, u64 hint
,
1252 struct btrfs_key ins
;
1254 struct extent_buffer
*buf
;
1256 ret
= btrfs_alloc_extent(trans
, root
, root
->root_key
.objectid
,
1257 1, empty_size
, hint
, (u64
)-1, &ins
, 0);
1260 return ERR_PTR(ret
);
1262 buf
= btrfs_find_create_tree_block(root
, ins
.objectid
);
1264 btrfs_free_extent(trans
, root
, ins
.objectid
, 1, 0);
1265 return ERR_PTR(-ENOMEM
);
1267 btrfs_set_buffer_uptodate(buf
);
1268 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
1269 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
1271 set_buffer_checked(buf);
1272 set_buffer_defrag(buf);
1274 /* FIXME!!!!!!!!!!!!!!!!
1275 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1277 trans
->blocks_used
++;
1281 static int drop_leaf_ref(struct btrfs_trans_handle
*trans
,
1282 struct btrfs_root
*root
, struct extent_buffer
*leaf
)
1284 struct btrfs_key key
;
1285 struct btrfs_file_extent_item
*fi
;
1290 BUG_ON(!btrfs_is_leaf(leaf
));
1291 nritems
= btrfs_header_nritems(leaf
);
1292 for (i
= 0; i
< nritems
; i
++) {
1295 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1296 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1298 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1299 if (btrfs_file_extent_type(leaf
, fi
) ==
1300 BTRFS_FILE_EXTENT_INLINE
)
1303 * FIXME make sure to insert a trans record that
1304 * repeats the snapshot del on crash
1306 disk_blocknr
= btrfs_file_extent_disk_blocknr(leaf
, fi
);
1307 if (disk_blocknr
== 0)
1309 ret
= btrfs_free_extent(trans
, root
, disk_blocknr
,
1310 btrfs_file_extent_disk_num_blocks(leaf
, fi
), 0);
1316 static void reada_walk_down(struct btrfs_root
*root
,
1317 struct extent_buffer
*node
)
1325 nritems
= btrfs_header_nritems(node
);
1326 for (i
= 0; i
< nritems
; i
++) {
1327 blocknr
= btrfs_node_blockptr(node
, i
);
1328 ret
= lookup_extent_ref(NULL
, root
, blocknr
, 1, &refs
);
1332 mutex_unlock(&root
->fs_info
->fs_mutex
);
1333 ret
= readahead_tree_block(root
, blocknr
);
1335 mutex_lock(&root
->fs_info
->fs_mutex
);
1342 * helper function for drop_snapshot, this walks down the tree dropping ref
1343 * counts as it goes.
1345 static int walk_down_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
1346 *root
, struct btrfs_path
*path
, int *level
)
1348 struct extent_buffer
*next
;
1349 struct extent_buffer
*cur
;
1354 WARN_ON(*level
< 0);
1355 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1356 ret
= lookup_extent_ref(trans
, root
,
1357 extent_buffer_blocknr(path
->nodes
[*level
]),
1364 * walk down to the last node level and free all the leaves
1366 while(*level
>= 0) {
1367 WARN_ON(*level
< 0);
1368 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1369 cur
= path
->nodes
[*level
];
1371 if (*level
> 0 && path
->slots
[*level
] == 0)
1372 reada_walk_down(root
, cur
);
1374 if (btrfs_header_level(cur
) != *level
)
1377 if (path
->slots
[*level
] >=
1378 btrfs_header_nritems(cur
))
1381 ret
= drop_leaf_ref(trans
, root
, cur
);
1385 blocknr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1386 ret
= lookup_extent_ref(trans
, root
, blocknr
, 1, &refs
);
1389 path
->slots
[*level
]++;
1390 ret
= btrfs_free_extent(trans
, root
, blocknr
, 1, 1);
1394 next
= btrfs_find_tree_block(root
, blocknr
);
1395 if (!next
|| !btrfs_buffer_uptodate(next
)) {
1396 free_extent_buffer(next
);
1397 mutex_unlock(&root
->fs_info
->fs_mutex
);
1398 next
= read_tree_block(root
, blocknr
);
1399 mutex_lock(&root
->fs_info
->fs_mutex
);
1401 /* we dropped the lock, check one more time */
1402 ret
= lookup_extent_ref(trans
, root
, blocknr
, 1, &refs
);
1405 path
->slots
[*level
]++;
1406 free_extent_buffer(next
);
1407 ret
= btrfs_free_extent(trans
, root
,
1413 WARN_ON(*level
<= 0);
1414 if (path
->nodes
[*level
-1])
1415 free_extent_buffer(path
->nodes
[*level
-1]);
1416 path
->nodes
[*level
-1] = next
;
1417 *level
= btrfs_header_level(next
);
1418 path
->slots
[*level
] = 0;
1421 WARN_ON(*level
< 0);
1422 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1423 ret
= btrfs_free_extent(trans
, root
,
1424 extent_buffer_blocknr(path
->nodes
[*level
]), 1, 1);
1425 free_extent_buffer(path
->nodes
[*level
]);
1426 path
->nodes
[*level
] = NULL
;
1433 * helper for dropping snapshots. This walks back up the tree in the path
1434 * to find the first node higher up where we haven't yet gone through
1437 static int walk_up_tree(struct btrfs_trans_handle
*trans
, struct btrfs_root
1438 *root
, struct btrfs_path
*path
, int *level
)
1443 struct btrfs_root_item
*root_item
= &root
->root_item
;
1445 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
1446 slot
= path
->slots
[i
];
1447 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
1448 struct extent_buffer
*node
;
1449 struct btrfs_disk_key disk_key
;
1450 node
= path
->nodes
[i
];
1453 WARN_ON(*level
== 0);
1454 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
1455 memcpy(&root_item
->drop_progress
,
1456 &disk_key
, sizeof(disk_key
));
1457 root_item
->drop_level
= i
;
1460 ret
= btrfs_free_extent(trans
, root
,
1461 extent_buffer_blocknr(path
->nodes
[*level
]),
1464 free_extent_buffer(path
->nodes
[*level
]);
1465 path
->nodes
[*level
] = NULL
;
1473 * drop the reference count on the tree rooted at 'snap'. This traverses
1474 * the tree freeing any blocks that have a ref count of zero after being
1477 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1483 struct btrfs_path
*path
;
1486 struct btrfs_root_item
*root_item
= &root
->root_item
;
1488 path
= btrfs_alloc_path();
1491 level
= btrfs_header_level(root
->node
);
1493 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
1494 path
->nodes
[level
] = root
->node
;
1495 path
->slots
[level
] = 0;
1497 struct btrfs_key key
;
1498 struct btrfs_disk_key found_key
;
1499 struct extent_buffer
*node
;
1501 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
1502 level
= root_item
->drop_level
;
1503 path
->lowest_level
= level
;
1504 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
1509 node
= path
->nodes
[level
];
1510 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
1511 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
1512 sizeof(found_key
)));
1515 wret
= walk_down_tree(trans
, root
, path
, &level
);
1521 wret
= walk_up_tree(trans
, root
, path
, &level
);
1527 extent_buffer_get(root
->node
);
1530 for (i
= 0; i
<= orig_level
; i
++) {
1531 if (path
->nodes
[i
]) {
1532 free_extent_buffer(path
->nodes
[i
]);
1537 btrfs_free_path(path
);
1541 static int free_block_group_radix(struct radix_tree_root
*radix
)
1544 struct btrfs_block_group_cache
*cache
[8];
1548 ret
= radix_tree_gang_lookup(radix
, (void **)cache
, 0,
1552 for (i
= 0; i
< ret
; i
++) {
1553 radix_tree_delete(radix
, cache
[i
]->key
.objectid
+
1554 cache
[i
]->key
.offset
- 1);
1561 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
1565 unsigned long gang
[16];
1568 ret
= free_block_group_radix(&info
->block_group_radix
);
1569 ret2
= free_block_group_radix(&info
->block_group_data_radix
);
1576 ret
= find_first_radix_bit(&info
->extent_map_radix
,
1577 gang
, 0, ARRAY_SIZE(gang
));
1580 for (i
= 0; i
< ret
; i
++) {
1581 clear_radix_bit(&info
->extent_map_radix
, gang
[i
]);
1587 int btrfs_read_block_groups(struct btrfs_root
*root
)
1589 struct btrfs_path
*path
;
1592 struct btrfs_block_group_cache
*cache
;
1593 struct btrfs_fs_info
*info
= root
->fs_info
;
1594 struct radix_tree_root
*radix
;
1595 struct btrfs_key key
;
1596 struct btrfs_key found_key
;
1597 struct extent_buffer
*leaf
;
1598 u64 group_size_blocks
;
1601 group_size_blocks
= BTRFS_BLOCK_GROUP_SIZE
>>
1602 root
->fs_info
->sb
->s_blocksize_bits
;
1603 root
= info
->extent_root
;
1605 key
.offset
= group_size_blocks
;
1606 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
1608 path
= btrfs_alloc_path();
1613 ret
= btrfs_search_slot(NULL
, info
->extent_root
,
1619 leaf
= path
->nodes
[0];
1620 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1621 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
1627 read_extent_buffer(leaf
, &cache
->item
,
1628 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
1629 sizeof(cache
->item
));
1630 if (cache
->item
.flags
& BTRFS_BLOCK_GROUP_DATA
) {
1631 radix
= &info
->block_group_data_radix
;
1634 radix
= &info
->block_group_radix
;
1638 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
1639 cache
->last_alloc
= cache
->key
.objectid
;
1640 cache
->first_free
= cache
->key
.objectid
;
1644 cache
->radix
= radix
;
1646 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
1647 btrfs_release_path(root
, path
);
1649 ret
= radix_tree_insert(radix
, found_key
.objectid
+
1650 found_key
.offset
- 1,
1653 used
= btrfs_block_group_used(&cache
->item
);
1654 if (used
< div_factor(key
.offset
, 8)) {
1655 radix_tree_tag_set(radix
, found_key
.objectid
+
1656 found_key
.offset
- 1,
1657 BTRFS_BLOCK_GROUP_AVAIL
);
1660 btrfs_super_total_blocks(&info
->super_copy
))
1664 btrfs_free_path(path
);