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.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
25 #include "print-tree.h"
26 #include "transaction.h"
29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
31 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
33 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
36 btrfs_root
*extent_root
);
37 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
38 btrfs_root
*extent_root
);
41 static int cache_block_group(struct btrfs_root
*root
,
42 struct btrfs_block_group_cache
*block_group
)
44 struct btrfs_path
*path
;
47 struct extent_buffer
*leaf
;
48 struct extent_io_tree
*free_space_cache
;
58 root
= root
->fs_info
->extent_root
;
59 free_space_cache
= &root
->fs_info
->free_space_cache
;
61 if (block_group
->cached
)
64 path
= btrfs_alloc_path();
69 first_free
= block_group
->key
.objectid
;
70 key
.objectid
= block_group
->key
.objectid
;
72 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
73 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
76 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
80 leaf
= path
->nodes
[0];
81 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
82 if (key
.objectid
+ key
.offset
> first_free
)
83 first_free
= key
.objectid
+ key
.offset
;
86 leaf
= path
->nodes
[0];
87 slot
= path
->slots
[0];
88 if (slot
>= btrfs_header_nritems(leaf
)) {
89 ret
= btrfs_next_leaf(root
, path
);
98 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
99 if (key
.objectid
< block_group
->key
.objectid
) {
102 if (key
.objectid
>= block_group
->key
.objectid
+
103 block_group
->key
.offset
) {
107 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
112 if (key
.objectid
> last
) {
113 hole_size
= key
.objectid
- last
;
114 set_extent_dirty(free_space_cache
, last
,
115 last
+ hole_size
- 1,
118 last
= key
.objectid
+ key
.offset
;
126 if (block_group
->key
.objectid
+
127 block_group
->key
.offset
> last
) {
128 hole_size
= block_group
->key
.objectid
+
129 block_group
->key
.offset
- last
;
130 set_extent_dirty(free_space_cache
, last
,
131 last
+ hole_size
- 1, GFP_NOFS
);
133 block_group
->cached
= 1;
135 btrfs_free_path(path
);
139 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
143 struct extent_io_tree
*block_group_cache
;
144 struct btrfs_block_group_cache
*block_group
= NULL
;
150 block_group_cache
= &info
->block_group_cache
;
151 ret
= find_first_extent_bit(block_group_cache
,
152 bytenr
, &start
, &end
,
153 BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
|
158 ret
= get_state_private(block_group_cache
, start
, &ptr
);
162 block_group
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
163 if (block_group
->key
.objectid
<= bytenr
&& bytenr
<
164 block_group
->key
.objectid
+ block_group
->key
.offset
)
169 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
171 return (cache
->flags
& bits
) == bits
;
174 static int noinline
find_search_start(struct btrfs_root
*root
,
175 struct btrfs_block_group_cache
**cache_ret
,
176 u64
*start_ret
, int num
, int data
)
179 struct btrfs_block_group_cache
*cache
= *cache_ret
;
180 struct extent_io_tree
*free_space_cache
;
181 struct extent_state
*state
;
186 u64 search_start
= *start_ret
;
192 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
193 free_space_cache
= &root
->fs_info
->free_space_cache
;
196 ret
= cache_block_group(root
, cache
);
200 last
= max(search_start
, cache
->key
.objectid
);
201 if (!block_group_bits(cache
, data
) || cache
->ro
) {
205 spin_lock_irq(&free_space_cache
->lock
);
206 state
= find_first_extent_bit_state(free_space_cache
, last
, EXTENT_DIRTY
);
211 spin_unlock_irq(&free_space_cache
->lock
);
215 start
= max(last
, state
->start
);
216 last
= state
->end
+ 1;
217 if (last
- start
< num
) {
218 if (last
== cache
->key
.objectid
+ cache
->key
.offset
)
221 state
= extent_state_next(state
);
222 } while(state
&& !(state
->state
& EXTENT_DIRTY
));
225 spin_unlock_irq(&free_space_cache
->lock
);
228 if (start
+ num
> cache
->key
.objectid
+ cache
->key
.offset
)
230 if (start
+ num
> total_fs_bytes
)
232 if (!block_group_bits(cache
, data
)) {
233 printk("block group bits don't match %Lu %d\n", cache
->flags
, data
);
239 cache
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
241 printk("Unable to find block group for %Lu\n", search_start
);
247 last
= cache
->key
.objectid
+ cache
->key
.offset
;
249 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
250 if (!cache
|| cache
->key
.objectid
>= total_fs_bytes
) {
259 if (cache_miss
&& !cache
->cached
) {
260 cache_block_group(root
, cache
);
262 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
264 cache
= btrfs_find_block_group(root
, cache
, last
, data
, 0);
272 static u64
div_factor(u64 num
, int factor
)
281 static int block_group_state_bits(u64 flags
)
284 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
285 bits
|= BLOCK_GROUP_DATA
;
286 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
287 bits
|= BLOCK_GROUP_METADATA
;
288 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
289 bits
|= BLOCK_GROUP_SYSTEM
;
293 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
294 struct btrfs_block_group_cache
295 *hint
, u64 search_start
,
298 struct btrfs_block_group_cache
*cache
;
299 struct extent_io_tree
*block_group_cache
;
300 struct btrfs_block_group_cache
*found_group
= NULL
;
301 struct btrfs_fs_info
*info
= root
->fs_info
;
315 block_group_cache
= &info
->block_group_cache
;
316 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
321 bit
= block_group_state_bits(data
);
323 if (search_start
&& search_start
< total_fs_bytes
) {
324 struct btrfs_block_group_cache
*shint
;
325 shint
= btrfs_lookup_block_group(info
, search_start
);
326 if (shint
&& block_group_bits(shint
, data
) && !shint
->ro
) {
327 used
= btrfs_block_group_used(&shint
->item
);
328 if (used
+ shint
->pinned
<
329 div_factor(shint
->key
.offset
, factor
)) {
334 if (hint
&& !hint
->ro
&& block_group_bits(hint
, data
) &&
335 hint
->key
.objectid
< total_fs_bytes
) {
336 used
= btrfs_block_group_used(&hint
->item
);
337 if (used
+ hint
->pinned
<
338 div_factor(hint
->key
.offset
, factor
)) {
341 last
= hint
->key
.objectid
+ hint
->key
.offset
;
345 hint_last
= max(hint
->key
.objectid
, search_start
);
347 hint_last
= search_start
;
349 if (hint_last
>= total_fs_bytes
)
350 hint_last
= search_start
;
355 ret
= find_first_extent_bit(block_group_cache
, last
,
360 ret
= get_state_private(block_group_cache
, start
, &ptr
);
364 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
365 last
= cache
->key
.objectid
+ cache
->key
.offset
;
366 used
= btrfs_block_group_used(&cache
->item
);
368 if (cache
->key
.objectid
> total_fs_bytes
)
371 if (!cache
->ro
&& block_group_bits(cache
, data
)) {
373 free_check
= cache
->key
.offset
;
375 free_check
= div_factor(cache
->key
.offset
,
378 if (used
+ cache
->pinned
< free_check
) {
394 static u64
hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
395 u64 owner
, u64 owner_offset
)
397 u32 high_crc
= ~(u32
)0;
398 u32 low_crc
= ~(u32
)0;
400 lenum
= cpu_to_le64(root_objectid
);
401 high_crc
= btrfs_crc32c(high_crc
, &lenum
, sizeof(lenum
));
402 lenum
= cpu_to_le64(ref_generation
);
403 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
404 if (owner
>= BTRFS_FIRST_FREE_OBJECTID
) {
405 lenum
= cpu_to_le64(owner
);
406 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
407 lenum
= cpu_to_le64(owner_offset
);
408 low_crc
= btrfs_crc32c(low_crc
, &lenum
, sizeof(lenum
));
410 return ((u64
)high_crc
<< 32) | (u64
)low_crc
;
413 static int match_extent_ref(struct extent_buffer
*leaf
,
414 struct btrfs_extent_ref
*disk_ref
,
415 struct btrfs_extent_ref
*cpu_ref
)
420 if (cpu_ref
->objectid
)
421 len
= sizeof(*cpu_ref
);
423 len
= 2 * sizeof(u64
);
424 ret
= memcmp_extent_buffer(leaf
, cpu_ref
, (unsigned long)disk_ref
,
429 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
430 struct btrfs_root
*root
,
431 struct btrfs_path
*path
, u64 bytenr
,
433 u64 ref_generation
, u64 owner
,
434 u64 owner_offset
, int del
)
437 struct btrfs_key key
;
438 struct btrfs_key found_key
;
439 struct btrfs_extent_ref ref
;
440 struct extent_buffer
*leaf
;
441 struct btrfs_extent_ref
*disk_ref
;
445 btrfs_set_stack_ref_root(&ref
, root_objectid
);
446 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
447 btrfs_set_stack_ref_objectid(&ref
, owner
);
448 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
450 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
453 key
.objectid
= bytenr
;
454 key
.type
= BTRFS_EXTENT_REF_KEY
;
457 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
461 leaf
= path
->nodes
[0];
463 u32 nritems
= btrfs_header_nritems(leaf
);
464 if (path
->slots
[0] >= nritems
) {
465 ret2
= btrfs_next_leaf(root
, path
);
468 leaf
= path
->nodes
[0];
470 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
471 if (found_key
.objectid
!= bytenr
||
472 found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
474 key
.offset
= found_key
.offset
;
476 btrfs_release_path(root
, path
);
480 disk_ref
= btrfs_item_ptr(path
->nodes
[0],
482 struct btrfs_extent_ref
);
483 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
)) {
487 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
488 key
.offset
= found_key
.offset
+ 1;
489 btrfs_release_path(root
, path
);
496 * Back reference rules. Back refs have three main goals:
498 * 1) differentiate between all holders of references to an extent so that
499 * when a reference is dropped we can make sure it was a valid reference
500 * before freeing the extent.
502 * 2) Provide enough information to quickly find the holders of an extent
503 * if we notice a given block is corrupted or bad.
505 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
506 * maintenance. This is actually the same as #2, but with a slightly
507 * different use case.
509 * File extents can be referenced by:
511 * - multiple snapshots, subvolumes, or different generations in one subvol
512 * - different files inside a single subvolume (in theory, not implemented yet)
513 * - different offsets inside a file (bookend extents in file.c)
515 * The extent ref structure has fields for:
517 * - Objectid of the subvolume root
518 * - Generation number of the tree holding the reference
519 * - objectid of the file holding the reference
520 * - offset in the file corresponding to the key holding the reference
522 * When a file extent is allocated the fields are filled in:
523 * (root_key.objectid, trans->transid, inode objectid, offset in file)
525 * When a leaf is cow'd new references are added for every file extent found
526 * in the leaf. It looks the same as the create case, but trans->transid
527 * will be different when the block is cow'd.
529 * (root_key.objectid, trans->transid, inode objectid, offset in file)
531 * When a file extent is removed either during snapshot deletion or file
532 * truncation, the corresponding back reference is found
535 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
536 * inode objectid, offset in file)
538 * Btree extents can be referenced by:
540 * - Different subvolumes
541 * - Different generations of the same subvolume
543 * Storing sufficient information for a full reverse mapping of a btree
544 * block would require storing the lowest key of the block in the backref,
545 * and it would require updating that lowest key either before write out or
546 * every time it changed. Instead, the objectid of the lowest key is stored
547 * along with the level of the tree block. This provides a hint
548 * about where in the btree the block can be found. Searches through the
549 * btree only need to look for a pointer to that block, so they stop one
550 * level higher than the level recorded in the backref.
552 * Some btrees do not do reference counting on their extents. These
553 * include the extent tree and the tree of tree roots. Backrefs for these
554 * trees always have a generation of zero.
556 * When a tree block is created, back references are inserted:
558 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
560 * When a tree block is cow'd in a reference counted root,
561 * new back references are added for all the blocks it points to.
562 * These are of the form (trans->transid will have increased since creation):
564 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
566 * Because the lowest_key_objectid and the level are just hints
567 * they are not used when backrefs are deleted. When a backref is deleted:
569 * if backref was for a tree root:
570 * root_objectid = root->root_key.objectid
572 * root_objectid = btrfs_header_owner(parent)
574 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
576 * Back Reference Key hashing:
578 * Back references have four fields, each 64 bits long. Unfortunately,
579 * This is hashed into a single 64 bit number and placed into the key offset.
580 * The key objectid corresponds to the first byte in the extent, and the
581 * key type is set to BTRFS_EXTENT_REF_KEY
583 int btrfs_insert_extent_backref(struct btrfs_trans_handle
*trans
,
584 struct btrfs_root
*root
,
585 struct btrfs_path
*path
, u64 bytenr
,
586 u64 root_objectid
, u64 ref_generation
,
587 u64 owner
, u64 owner_offset
)
590 struct btrfs_key key
;
591 struct btrfs_extent_ref ref
;
592 struct btrfs_extent_ref
*disk_ref
;
595 btrfs_set_stack_ref_root(&ref
, root_objectid
);
596 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
597 btrfs_set_stack_ref_objectid(&ref
, owner
);
598 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
600 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
603 key
.objectid
= bytenr
;
604 key
.type
= BTRFS_EXTENT_REF_KEY
;
606 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(ref
));
607 while (ret
== -EEXIST
) {
608 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
609 struct btrfs_extent_ref
);
610 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
))
613 btrfs_release_path(root
, path
);
614 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
619 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
620 struct btrfs_extent_ref
);
621 write_extent_buffer(path
->nodes
[0], &ref
, (unsigned long)disk_ref
,
623 btrfs_mark_buffer_dirty(path
->nodes
[0]);
625 btrfs_release_path(root
, path
);
629 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
630 struct btrfs_root
*root
,
631 u64 bytenr
, u64 num_bytes
,
632 u64 root_objectid
, u64 ref_generation
,
633 u64 owner
, u64 owner_offset
)
635 struct btrfs_path
*path
;
637 struct btrfs_key key
;
638 struct extent_buffer
*l
;
639 struct btrfs_extent_item
*item
;
642 WARN_ON(num_bytes
< root
->sectorsize
);
643 path
= btrfs_alloc_path();
648 key
.objectid
= bytenr
;
649 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
650 key
.offset
= num_bytes
;
651 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
660 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
661 refs
= btrfs_extent_refs(l
, item
);
662 btrfs_set_extent_refs(l
, item
, refs
+ 1);
663 btrfs_mark_buffer_dirty(path
->nodes
[0]);
665 btrfs_release_path(root
->fs_info
->extent_root
, path
);
668 ret
= btrfs_insert_extent_backref(trans
, root
->fs_info
->extent_root
,
669 path
, bytenr
, root_objectid
,
670 ref_generation
, owner
, owner_offset
);
672 finish_current_insert(trans
, root
->fs_info
->extent_root
);
673 del_pending_extents(trans
, root
->fs_info
->extent_root
);
675 btrfs_free_path(path
);
679 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
680 struct btrfs_root
*root
)
682 finish_current_insert(trans
, root
->fs_info
->extent_root
);
683 del_pending_extents(trans
, root
->fs_info
->extent_root
);
687 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
688 struct btrfs_root
*root
, u64 bytenr
,
689 u64 num_bytes
, u32
*refs
)
691 struct btrfs_path
*path
;
693 struct btrfs_key key
;
694 struct extent_buffer
*l
;
695 struct btrfs_extent_item
*item
;
697 WARN_ON(num_bytes
< root
->sectorsize
);
698 path
= btrfs_alloc_path();
700 key
.objectid
= bytenr
;
701 key
.offset
= num_bytes
;
702 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
703 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
708 btrfs_print_leaf(root
, path
->nodes
[0]);
709 printk("failed to find block number %Lu\n", bytenr
);
713 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
714 *refs
= btrfs_extent_refs(l
, item
);
716 btrfs_free_path(path
);
720 u32
btrfs_count_snapshots_in_path(struct btrfs_root
*root
,
721 struct btrfs_path
*count_path
,
724 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
725 struct btrfs_path
*path
;
728 u64 root_objectid
= root
->root_key
.objectid
;
733 struct btrfs_key key
;
734 struct btrfs_key found_key
;
735 struct extent_buffer
*l
;
736 struct btrfs_extent_item
*item
;
737 struct btrfs_extent_ref
*ref_item
;
740 path
= btrfs_alloc_path();
743 bytenr
= first_extent
;
745 bytenr
= count_path
->nodes
[level
]->start
;
748 key
.objectid
= bytenr
;
751 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
752 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
758 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
760 if (found_key
.objectid
!= bytenr
||
761 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
765 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
768 nritems
= btrfs_header_nritems(l
);
769 if (path
->slots
[0] >= nritems
) {
770 ret
= btrfs_next_leaf(extent_root
, path
);
775 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
776 if (found_key
.objectid
!= bytenr
)
779 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
785 ref_item
= btrfs_item_ptr(l
, path
->slots
[0],
786 struct btrfs_extent_ref
);
787 found_objectid
= btrfs_ref_root(l
, ref_item
);
789 if (found_objectid
!= root_objectid
) {
796 if (cur_count
== 0) {
800 if (level
>= 0 && root
->node
== count_path
->nodes
[level
])
803 btrfs_release_path(root
, path
);
807 btrfs_free_path(path
);
810 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
811 struct btrfs_root
*root
, u64 owner_objectid
)
817 struct btrfs_disk_key disk_key
;
819 level
= btrfs_header_level(root
->node
);
820 generation
= trans
->transid
;
821 nritems
= btrfs_header_nritems(root
->node
);
824 btrfs_item_key(root
->node
, &disk_key
, 0);
826 btrfs_node_key(root
->node
, &disk_key
, 0);
827 key_objectid
= btrfs_disk_key_objectid(&disk_key
);
831 return btrfs_inc_extent_ref(trans
, root
, root
->node
->start
,
832 root
->node
->len
, owner_objectid
,
833 generation
, level
, key_objectid
);
836 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
837 struct extent_buffer
*buf
)
841 struct btrfs_key key
;
842 struct btrfs_file_extent_item
*fi
;
851 level
= btrfs_header_level(buf
);
852 nritems
= btrfs_header_nritems(buf
);
853 for (i
= 0; i
< nritems
; i
++) {
856 btrfs_item_key_to_cpu(buf
, &key
, i
);
857 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
859 fi
= btrfs_item_ptr(buf
, i
,
860 struct btrfs_file_extent_item
);
861 if (btrfs_file_extent_type(buf
, fi
) ==
862 BTRFS_FILE_EXTENT_INLINE
)
864 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
865 if (disk_bytenr
== 0)
867 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
,
868 btrfs_file_extent_disk_num_bytes(buf
, fi
),
869 root
->root_key
.objectid
, trans
->transid
,
870 key
.objectid
, key
.offset
);
876 bytenr
= btrfs_node_blockptr(buf
, i
);
877 btrfs_node_key_to_cpu(buf
, &key
, i
);
878 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
,
879 btrfs_level_size(root
, level
- 1),
880 root
->root_key
.objectid
,
882 level
- 1, key
.objectid
);
893 for (i
=0; i
< faili
; i
++) {
896 btrfs_item_key_to_cpu(buf
, &key
, i
);
897 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
899 fi
= btrfs_item_ptr(buf
, i
,
900 struct btrfs_file_extent_item
);
901 if (btrfs_file_extent_type(buf
, fi
) ==
902 BTRFS_FILE_EXTENT_INLINE
)
904 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
905 if (disk_bytenr
== 0)
907 err
= btrfs_free_extent(trans
, root
, disk_bytenr
,
908 btrfs_file_extent_disk_num_bytes(buf
,
912 bytenr
= btrfs_node_blockptr(buf
, i
);
913 err
= btrfs_free_extent(trans
, root
, bytenr
,
914 btrfs_level_size(root
, level
- 1), 0);
922 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
923 struct btrfs_root
*root
,
924 struct btrfs_path
*path
,
925 struct btrfs_block_group_cache
*cache
)
929 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
931 struct extent_buffer
*leaf
;
933 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
938 leaf
= path
->nodes
[0];
939 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
940 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
941 btrfs_mark_buffer_dirty(leaf
);
942 btrfs_release_path(extent_root
, path
);
944 finish_current_insert(trans
, extent_root
);
945 pending_ret
= del_pending_extents(trans
, extent_root
);
954 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
955 struct btrfs_root
*root
)
957 struct extent_io_tree
*block_group_cache
;
958 struct btrfs_block_group_cache
*cache
;
962 struct btrfs_path
*path
;
968 block_group_cache
= &root
->fs_info
->block_group_cache
;
969 path
= btrfs_alloc_path();
974 ret
= find_first_extent_bit(block_group_cache
, last
,
975 &start
, &end
, BLOCK_GROUP_DIRTY
);
980 ret
= get_state_private(block_group_cache
, start
, &ptr
);
983 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
984 err
= write_one_cache_group(trans
, root
,
987 * if we fail to write the cache group, we want
988 * to keep it marked dirty in hopes that a later
995 clear_extent_bits(block_group_cache
, start
, end
,
996 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
998 btrfs_free_path(path
);
1002 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
1005 struct list_head
*head
= &info
->space_info
;
1006 struct list_head
*cur
;
1007 struct btrfs_space_info
*found
;
1008 list_for_each(cur
, head
) {
1009 found
= list_entry(cur
, struct btrfs_space_info
, list
);
1010 if (found
->flags
== flags
)
1017 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
1018 u64 total_bytes
, u64 bytes_used
,
1019 struct btrfs_space_info
**space_info
)
1021 struct btrfs_space_info
*found
;
1023 found
= __find_space_info(info
, flags
);
1025 found
->total_bytes
+= total_bytes
;
1026 found
->bytes_used
+= bytes_used
;
1028 WARN_ON(found
->total_bytes
< found
->bytes_used
);
1029 *space_info
= found
;
1032 found
= kmalloc(sizeof(*found
), GFP_NOFS
);
1036 list_add(&found
->list
, &info
->space_info
);
1037 found
->flags
= flags
;
1038 found
->total_bytes
= total_bytes
;
1039 found
->bytes_used
= bytes_used
;
1040 found
->bytes_pinned
= 0;
1042 *space_info
= found
;
1046 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1048 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1049 BTRFS_BLOCK_GROUP_RAID1
|
1050 BTRFS_BLOCK_GROUP_RAID10
|
1051 BTRFS_BLOCK_GROUP_DUP
);
1053 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1054 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1055 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1056 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1057 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1058 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1062 static u64
reduce_alloc_profile(u64 flags
)
1064 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
1065 (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
1066 BTRFS_BLOCK_GROUP_RAID10
)))
1067 flags
&= ~BTRFS_BLOCK_GROUP_DUP
;
1069 if ((flags
& BTRFS_BLOCK_GROUP_RAID1
) &&
1070 (flags
& BTRFS_BLOCK_GROUP_RAID10
))
1071 flags
&= ~BTRFS_BLOCK_GROUP_RAID1
;
1073 if ((flags
& BTRFS_BLOCK_GROUP_RAID0
) &&
1074 ((flags
& BTRFS_BLOCK_GROUP_RAID1
) |
1075 (flags
& BTRFS_BLOCK_GROUP_RAID10
) |
1076 (flags
& BTRFS_BLOCK_GROUP_DUP
)))
1077 flags
&= ~BTRFS_BLOCK_GROUP_RAID0
;
1082 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1083 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1086 struct btrfs_space_info
*space_info
;
1092 flags
= reduce_alloc_profile(flags
);
1094 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1096 ret
= update_space_info(extent_root
->fs_info
, flags
,
1100 BUG_ON(!space_info
);
1102 if (space_info
->full
)
1105 thresh
= div_factor(space_info
->total_bytes
, 6);
1106 if ((space_info
->bytes_used
+ space_info
->bytes_pinned
+ alloc_bytes
) <
1110 ret
= btrfs_alloc_chunk(trans
, extent_root
, &start
, &num_bytes
, flags
);
1111 if (ret
== -ENOSPC
) {
1112 printk("space info full %Lu\n", flags
);
1113 space_info
->full
= 1;
1119 ret
= btrfs_make_block_group(trans
, extent_root
, 0, flags
,
1120 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, start
, num_bytes
);
1126 static int update_block_group(struct btrfs_trans_handle
*trans
,
1127 struct btrfs_root
*root
,
1128 u64 bytenr
, u64 num_bytes
, int alloc
,
1131 struct btrfs_block_group_cache
*cache
;
1132 struct btrfs_fs_info
*info
= root
->fs_info
;
1133 u64 total
= num_bytes
;
1140 cache
= btrfs_lookup_block_group(info
, bytenr
);
1144 byte_in_group
= bytenr
- cache
->key
.objectid
;
1145 WARN_ON(byte_in_group
> cache
->key
.offset
);
1146 start
= cache
->key
.objectid
;
1147 end
= start
+ cache
->key
.offset
- 1;
1148 set_extent_bits(&info
->block_group_cache
, start
, end
,
1149 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
1151 old_val
= btrfs_block_group_used(&cache
->item
);
1152 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1154 old_val
+= num_bytes
;
1155 cache
->space_info
->bytes_used
+= num_bytes
;
1157 old_val
-= num_bytes
;
1158 cache
->space_info
->bytes_used
-= num_bytes
;
1160 set_extent_dirty(&info
->free_space_cache
,
1161 bytenr
, bytenr
+ num_bytes
- 1,
1165 btrfs_set_block_group_used(&cache
->item
, old_val
);
1167 bytenr
+= num_bytes
;
1172 static int update_pinned_extents(struct btrfs_root
*root
,
1173 u64 bytenr
, u64 num
, int pin
)
1176 struct btrfs_block_group_cache
*cache
;
1177 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1180 set_extent_dirty(&fs_info
->pinned_extents
,
1181 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1183 clear_extent_dirty(&fs_info
->pinned_extents
,
1184 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1187 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1189 len
= min(num
, cache
->key
.offset
-
1190 (bytenr
- cache
->key
.objectid
));
1192 cache
->pinned
+= len
;
1193 cache
->space_info
->bytes_pinned
+= len
;
1194 fs_info
->total_pinned
+= len
;
1196 cache
->pinned
-= len
;
1197 cache
->space_info
->bytes_pinned
-= len
;
1198 fs_info
->total_pinned
-= len
;
1206 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
1211 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1215 ret
= find_first_extent_bit(pinned_extents
, last
,
1216 &start
, &end
, EXTENT_DIRTY
);
1219 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1225 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1226 struct btrfs_root
*root
,
1227 struct extent_io_tree
*unpin
)
1232 struct extent_io_tree
*free_space_cache
;
1233 free_space_cache
= &root
->fs_info
->free_space_cache
;
1236 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1240 update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1241 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1242 set_extent_dirty(free_space_cache
, start
, end
, GFP_NOFS
);
1247 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1248 struct btrfs_root
*extent_root
)
1252 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1253 struct extent_buffer
*eb
;
1254 struct btrfs_path
*path
;
1255 struct btrfs_key ins
;
1256 struct btrfs_disk_key first
;
1257 struct btrfs_extent_item extent_item
;
1262 btrfs_set_stack_extent_refs(&extent_item
, 1);
1263 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
1264 path
= btrfs_alloc_path();
1267 ret
= find_first_extent_bit(&info
->extent_ins
, 0, &start
,
1268 &end
, EXTENT_LOCKED
);
1272 ins
.objectid
= start
;
1273 ins
.offset
= end
+ 1 - start
;
1274 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
1275 &extent_item
, sizeof(extent_item
));
1276 clear_extent_bits(&info
->extent_ins
, start
, end
, EXTENT_LOCKED
,
1278 eb
= read_tree_block(extent_root
, ins
.objectid
, ins
.offset
);
1279 level
= btrfs_header_level(eb
);
1281 btrfs_item_key(eb
, &first
, 0);
1283 btrfs_node_key(eb
, &first
, 0);
1285 err
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1286 start
, extent_root
->root_key
.objectid
,
1288 btrfs_disk_key_objectid(&first
));
1290 free_extent_buffer(eb
);
1292 btrfs_free_path(path
);
1296 static int pin_down_bytes(struct btrfs_root
*root
, u64 bytenr
, u32 num_bytes
,
1300 struct extent_buffer
*buf
;
1303 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1305 if (btrfs_buffer_uptodate(buf
)) {
1307 root
->fs_info
->running_transaction
->transid
;
1308 u64 header_transid
=
1309 btrfs_header_generation(buf
);
1310 if (header_transid
== transid
&&
1311 !btrfs_header_flag(buf
,
1312 BTRFS_HEADER_FLAG_WRITTEN
)) {
1313 clean_tree_block(NULL
, root
, buf
);
1314 free_extent_buffer(buf
);
1318 free_extent_buffer(buf
);
1320 update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1322 set_extent_bits(&root
->fs_info
->pending_del
,
1323 bytenr
, bytenr
+ num_bytes
- 1,
1324 EXTENT_LOCKED
, GFP_NOFS
);
1331 * remove an extent from the root, returns 0 on success
1333 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1334 *root
, u64 bytenr
, u64 num_bytes
,
1335 u64 root_objectid
, u64 ref_generation
,
1336 u64 owner_objectid
, u64 owner_offset
, int pin
,
1339 struct btrfs_path
*path
;
1340 struct btrfs_key key
;
1341 struct btrfs_fs_info
*info
= root
->fs_info
;
1342 struct btrfs_root
*extent_root
= info
->extent_root
;
1343 struct extent_buffer
*leaf
;
1345 int extent_slot
= 0;
1346 int found_extent
= 0;
1348 struct btrfs_extent_item
*ei
;
1351 key
.objectid
= bytenr
;
1352 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1353 key
.offset
= num_bytes
;
1354 path
= btrfs_alloc_path();
1359 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1360 bytenr
, root_objectid
,
1362 owner_objectid
, owner_offset
, 1);
1364 struct btrfs_key found_key
;
1365 extent_slot
= path
->slots
[0];
1366 while(extent_slot
> 0) {
1368 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1370 if (found_key
.objectid
!= bytenr
)
1372 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1373 found_key
.offset
== num_bytes
) {
1377 if (path
->slots
[0] - extent_slot
> 5)
1381 ret
= btrfs_del_item(trans
, extent_root
, path
);
1383 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1385 printk("Unable to find ref byte nr %Lu root %Lu "
1386 " gen %Lu owner %Lu offset %Lu\n", bytenr
,
1387 root_objectid
, ref_generation
, owner_objectid
,
1390 if (!found_extent
) {
1391 btrfs_release_path(extent_root
, path
);
1392 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
1396 extent_slot
= path
->slots
[0];
1399 leaf
= path
->nodes
[0];
1400 ei
= btrfs_item_ptr(leaf
, extent_slot
,
1401 struct btrfs_extent_item
);
1402 refs
= btrfs_extent_refs(leaf
, ei
);
1405 btrfs_set_extent_refs(leaf
, ei
, refs
);
1407 btrfs_mark_buffer_dirty(leaf
);
1409 if (refs
== 0 && found_extent
&& path
->slots
[0] == extent_slot
+ 1) {
1410 /* if the back ref and the extent are next to each other
1411 * they get deleted below in one shot
1413 path
->slots
[0] = extent_slot
;
1415 } else if (found_extent
) {
1416 /* otherwise delete the extent back ref */
1417 ret
= btrfs_del_item(trans
, extent_root
, path
);
1419 /* if refs are 0, we need to setup the path for deletion */
1421 btrfs_release_path(extent_root
, path
);
1422 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
1435 ret
= pin_down_bytes(root
, bytenr
, num_bytes
, 0);
1441 /* block accounting for super block */
1442 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1443 btrfs_set_super_bytes_used(&info
->super_copy
,
1444 super_used
- num_bytes
);
1446 /* block accounting for root item */
1447 root_used
= btrfs_root_used(&root
->root_item
);
1448 btrfs_set_root_used(&root
->root_item
,
1449 root_used
- num_bytes
);
1450 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
1455 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1459 btrfs_free_path(path
);
1460 finish_current_insert(trans
, extent_root
);
1465 * find all the blocks marked as pending in the radix tree and remove
1466 * them from the extent map
1468 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1469 btrfs_root
*extent_root
)
1475 struct extent_io_tree
*pending_del
;
1476 struct extent_io_tree
*pinned_extents
;
1478 pending_del
= &extent_root
->fs_info
->pending_del
;
1479 pinned_extents
= &extent_root
->fs_info
->pinned_extents
;
1482 ret
= find_first_extent_bit(pending_del
, 0, &start
, &end
,
1486 update_pinned_extents(extent_root
, start
, end
+ 1 - start
, 1);
1487 clear_extent_bits(pending_del
, start
, end
, EXTENT_LOCKED
,
1489 ret
= __free_extent(trans
, extent_root
,
1490 start
, end
+ 1 - start
,
1491 extent_root
->root_key
.objectid
,
1500 * remove an extent from the root, returns 0 on success
1502 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1503 *root
, u64 bytenr
, u64 num_bytes
,
1504 u64 root_objectid
, u64 ref_generation
,
1505 u64 owner_objectid
, u64 owner_offset
, int pin
)
1507 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1511 WARN_ON(num_bytes
< root
->sectorsize
);
1512 if (!root
->ref_cows
)
1515 if (root
== extent_root
) {
1516 pin_down_bytes(root
, bytenr
, num_bytes
, 1);
1519 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, root_objectid
,
1520 ref_generation
, owner_objectid
, owner_offset
,
1522 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
1523 return ret
? ret
: pending_ret
;
1526 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
1528 u64 mask
= ((u64
)root
->stripesize
- 1);
1529 u64 ret
= (val
+ mask
) & ~mask
;
1534 * walks the btree of allocated extents and find a hole of a given size.
1535 * The key ins is changed to record the hole:
1536 * ins->objectid == block start
1537 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1538 * ins->offset == number of blocks
1539 * Any available blocks before search_start are skipped.
1541 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
1542 struct btrfs_root
*orig_root
,
1543 u64 num_bytes
, u64 empty_size
,
1544 u64 search_start
, u64 search_end
,
1545 u64 hint_byte
, struct btrfs_key
*ins
,
1546 u64 exclude_start
, u64 exclude_nr
,
1550 u64 orig_search_start
= search_start
;
1551 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
1552 struct btrfs_fs_info
*info
= root
->fs_info
;
1553 u64 total_needed
= num_bytes
;
1554 u64
*last_ptr
= NULL
;
1555 struct btrfs_block_group_cache
*block_group
;
1558 int empty_cluster
= 2 * 1024 * 1024;
1560 WARN_ON(num_bytes
< root
->sectorsize
);
1561 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1563 if (data
& BTRFS_BLOCK_GROUP_METADATA
) {
1564 last_ptr
= &root
->fs_info
->last_alloc
;
1565 empty_cluster
= 256 * 1024;
1568 if ((data
& BTRFS_BLOCK_GROUP_DATA
) && btrfs_test_opt(root
, SSD
)) {
1569 last_ptr
= &root
->fs_info
->last_data_alloc
;
1574 hint_byte
= *last_ptr
;
1576 empty_size
+= empty_cluster
;
1580 if (search_end
== (u64
)-1)
1581 search_end
= btrfs_super_total_bytes(&info
->super_copy
);
1584 block_group
= btrfs_lookup_block_group(info
, hint_byte
);
1586 hint_byte
= search_start
;
1587 block_group
= btrfs_find_block_group(root
, block_group
,
1588 hint_byte
, data
, 1);
1589 if (last_ptr
&& *last_ptr
== 0 && block_group
)
1590 hint_byte
= block_group
->key
.objectid
;
1592 block_group
= btrfs_find_block_group(root
,
1594 search_start
, data
, 1);
1596 search_start
= max(search_start
, hint_byte
);
1598 total_needed
+= empty_size
;
1602 block_group
= btrfs_lookup_block_group(info
, search_start
);
1604 block_group
= btrfs_lookup_block_group(info
,
1607 ret
= find_search_start(root
, &block_group
, &search_start
,
1608 total_needed
, data
);
1609 if (ret
== -ENOSPC
&& last_ptr
&& *last_ptr
) {
1611 block_group
= btrfs_lookup_block_group(info
,
1613 search_start
= orig_search_start
;
1614 ret
= find_search_start(root
, &block_group
, &search_start
,
1615 total_needed
, data
);
1622 if (last_ptr
&& *last_ptr
&& search_start
!= *last_ptr
) {
1625 empty_size
+= empty_cluster
;
1626 total_needed
+= empty_size
;
1628 block_group
= btrfs_lookup_block_group(info
,
1630 search_start
= orig_search_start
;
1631 ret
= find_search_start(root
, &block_group
,
1632 &search_start
, total_needed
, data
);
1639 search_start
= stripe_align(root
, search_start
);
1640 ins
->objectid
= search_start
;
1641 ins
->offset
= num_bytes
;
1643 if (ins
->objectid
+ num_bytes
>= search_end
)
1646 if (ins
->objectid
+ num_bytes
>
1647 block_group
->key
.objectid
+ block_group
->key
.offset
) {
1648 search_start
= block_group
->key
.objectid
+
1649 block_group
->key
.offset
;
1653 if (test_range_bit(&info
->extent_ins
, ins
->objectid
,
1654 ins
->objectid
+ num_bytes
-1, EXTENT_LOCKED
, 0)) {
1655 search_start
= ins
->objectid
+ num_bytes
;
1659 if (test_range_bit(&info
->pinned_extents
, ins
->objectid
,
1660 ins
->objectid
+ num_bytes
-1, EXTENT_DIRTY
, 0)) {
1661 search_start
= ins
->objectid
+ num_bytes
;
1665 if (exclude_nr
> 0 && (ins
->objectid
+ num_bytes
> exclude_start
&&
1666 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1667 search_start
= exclude_start
+ exclude_nr
;
1671 if (!(data
& BTRFS_BLOCK_GROUP_DATA
)) {
1672 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1674 trans
->block_group
= block_group
;
1676 ins
->offset
= num_bytes
;
1678 *last_ptr
= ins
->objectid
+ ins
->offset
;
1680 btrfs_super_total_bytes(&root
->fs_info
->super_copy
)) {
1687 if (search_start
+ num_bytes
>= search_end
) {
1689 search_start
= orig_search_start
;
1696 total_needed
-= empty_size
;
1701 block_group
= btrfs_lookup_block_group(info
, search_start
);
1703 block_group
= btrfs_find_block_group(root
, block_group
,
1704 search_start
, data
, 0);
1712 * finds a free extent and does all the dirty work required for allocation
1713 * returns the key for the extent through ins, and a tree buffer for
1714 * the first block of the extent through buf.
1716 * returns 0 if everything worked, non-zero otherwise.
1718 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1719 struct btrfs_root
*root
,
1720 u64 num_bytes
, u64 min_alloc_size
,
1721 u64 root_objectid
, u64 ref_generation
,
1722 u64 owner
, u64 owner_offset
,
1723 u64 empty_size
, u64 hint_byte
,
1724 u64 search_end
, struct btrfs_key
*ins
, u64 data
)
1730 u64 search_start
= 0;
1733 struct btrfs_fs_info
*info
= root
->fs_info
;
1734 struct btrfs_root
*extent_root
= info
->extent_root
;
1735 struct btrfs_extent_item
*extent_item
;
1736 struct btrfs_extent_ref
*ref
;
1737 struct btrfs_path
*path
;
1738 struct btrfs_key keys
[2];
1741 alloc_profile
= info
->avail_data_alloc_bits
&
1742 info
->data_alloc_profile
;
1743 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
1744 } else if (root
== root
->fs_info
->chunk_root
) {
1745 alloc_profile
= info
->avail_system_alloc_bits
&
1746 info
->system_alloc_profile
;
1747 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
1749 alloc_profile
= info
->avail_metadata_alloc_bits
&
1750 info
->metadata_alloc_profile
;
1751 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
1754 data
= reduce_alloc_profile(data
);
1755 if (root
->ref_cows
) {
1756 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
1757 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1759 BTRFS_BLOCK_GROUP_METADATA
|
1760 (info
->metadata_alloc_profile
&
1761 info
->avail_metadata_alloc_bits
));
1764 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1765 num_bytes
+ 2 * 1024 * 1024, data
);
1769 WARN_ON(num_bytes
< root
->sectorsize
);
1770 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
1771 search_start
, search_end
, hint_byte
, ins
,
1772 trans
->alloc_exclude_start
,
1773 trans
->alloc_exclude_nr
, data
);
1775 if (ret
== -ENOSPC
&& num_bytes
> min_alloc_size
) {
1776 num_bytes
= num_bytes
>> 1;
1777 num_bytes
= max(num_bytes
, min_alloc_size
);
1781 printk("allocation failed flags %Lu\n", data
);
1787 /* block accounting for super block */
1788 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1789 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
1791 /* block accounting for root item */
1792 root_used
= btrfs_root_used(&root
->root_item
);
1793 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
1795 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
1796 ins
->objectid
, ins
->objectid
+ ins
->offset
- 1,
1799 if (root
== extent_root
) {
1800 set_extent_bits(&root
->fs_info
->extent_ins
, ins
->objectid
,
1801 ins
->objectid
+ ins
->offset
- 1,
1802 EXTENT_LOCKED
, GFP_NOFS
);
1806 WARN_ON(trans
->alloc_exclude_nr
);
1807 trans
->alloc_exclude_start
= ins
->objectid
;
1808 trans
->alloc_exclude_nr
= ins
->offset
;
1810 memcpy(&keys
[0], ins
, sizeof(*ins
));
1811 keys
[1].offset
= hash_extent_ref(root_objectid
, ref_generation
,
1812 owner
, owner_offset
);
1813 keys
[1].objectid
= ins
->objectid
;
1814 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
1815 sizes
[0] = sizeof(*extent_item
);
1816 sizes
[1] = sizeof(*ref
);
1818 path
= btrfs_alloc_path();
1821 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
1825 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1826 struct btrfs_extent_item
);
1827 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, 1);
1828 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1829 struct btrfs_extent_ref
);
1831 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
1832 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
1833 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
1834 btrfs_set_ref_offset(path
->nodes
[0], ref
, owner_offset
);
1836 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1838 trans
->alloc_exclude_start
= 0;
1839 trans
->alloc_exclude_nr
= 0;
1840 btrfs_free_path(path
);
1841 finish_current_insert(trans
, extent_root
);
1842 pending_ret
= del_pending_extents(trans
, extent_root
);
1852 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0);
1854 printk("update block group failed for %Lu %Lu\n",
1855 ins
->objectid
, ins
->offset
);
1862 * helper function to allocate a block for a given tree
1863 * returns the tree buffer or NULL.
1865 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1866 struct btrfs_root
*root
,
1868 u64 root_objectid
, u64 hint
,
1874 ref_generation
= trans
->transid
;
1879 return __btrfs_alloc_free_block(trans
, root
, blocksize
, root_objectid
,
1880 ref_generation
, 0, 0, hint
, empty_size
);
1884 * helper function to allocate a block for a given tree
1885 * returns the tree buffer or NULL.
1887 struct extent_buffer
*__btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1888 struct btrfs_root
*root
,
1897 struct btrfs_key ins
;
1899 struct extent_buffer
*buf
;
1901 ret
= btrfs_alloc_extent(trans
, root
, blocksize
, blocksize
,
1902 root_objectid
, ref_generation
,
1903 level
, first_objectid
, empty_size
, hint
,
1907 return ERR_PTR(ret
);
1909 buf
= btrfs_find_create_tree_block(root
, ins
.objectid
, blocksize
);
1911 btrfs_free_extent(trans
, root
, ins
.objectid
, blocksize
,
1912 root
->root_key
.objectid
, ref_generation
,
1914 return ERR_PTR(-ENOMEM
);
1916 btrfs_set_header_generation(buf
, trans
->transid
);
1917 clean_tree_block(trans
, root
, buf
);
1918 btrfs_set_buffer_uptodate(buf
);
1920 if (PageDirty(buf
->first_page
)) {
1921 printk("page %lu dirty\n", buf
->first_page
->index
);
1925 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
1926 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
1927 if (!btrfs_test_opt(root
, SSD
))
1928 btrfs_set_buffer_defrag(buf
);
1929 trans
->blocks_used
++;
1933 static int noinline
drop_leaf_ref(struct btrfs_trans_handle
*trans
,
1934 struct btrfs_root
*root
,
1935 struct extent_buffer
*leaf
)
1938 u64 leaf_generation
;
1939 struct btrfs_key key
;
1940 struct btrfs_file_extent_item
*fi
;
1945 BUG_ON(!btrfs_is_leaf(leaf
));
1946 nritems
= btrfs_header_nritems(leaf
);
1947 leaf_owner
= btrfs_header_owner(leaf
);
1948 leaf_generation
= btrfs_header_generation(leaf
);
1950 for (i
= 0; i
< nritems
; i
++) {
1953 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1954 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1956 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1957 if (btrfs_file_extent_type(leaf
, fi
) ==
1958 BTRFS_FILE_EXTENT_INLINE
)
1961 * FIXME make sure to insert a trans record that
1962 * repeats the snapshot del on crash
1964 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1965 if (disk_bytenr
== 0)
1967 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
1968 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
1969 leaf_owner
, leaf_generation
,
1970 key
.objectid
, key
.offset
, 0);
1976 static void noinline
reada_walk_down(struct btrfs_root
*root
,
1977 struct extent_buffer
*node
,
1990 nritems
= btrfs_header_nritems(node
);
1991 level
= btrfs_header_level(node
);
1995 for (i
= slot
; i
< nritems
&& skipped
< 32; i
++) {
1996 bytenr
= btrfs_node_blockptr(node
, i
);
1997 if (last
&& ((bytenr
> last
&& bytenr
- last
> 32 * 1024) ||
1998 (last
> bytenr
&& last
- bytenr
> 32 * 1024))) {
2002 blocksize
= btrfs_level_size(root
, level
- 1);
2004 ret
= lookup_extent_ref(NULL
, root
, bytenr
,
2012 mutex_unlock(&root
->fs_info
->fs_mutex
);
2013 ret
= readahead_tree_block(root
, bytenr
, blocksize
);
2014 last
= bytenr
+ blocksize
;
2016 mutex_lock(&root
->fs_info
->fs_mutex
);
2023 * helper function for drop_snapshot, this walks down the tree dropping ref
2024 * counts as it goes.
2026 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
2027 struct btrfs_root
*root
,
2028 struct btrfs_path
*path
, int *level
)
2033 struct extent_buffer
*next
;
2034 struct extent_buffer
*cur
;
2035 struct extent_buffer
*parent
;
2040 WARN_ON(*level
< 0);
2041 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2042 ret
= lookup_extent_ref(trans
, root
,
2043 path
->nodes
[*level
]->start
,
2044 path
->nodes
[*level
]->len
, &refs
);
2050 * walk down to the last node level and free all the leaves
2052 while(*level
>= 0) {
2053 WARN_ON(*level
< 0);
2054 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2055 cur
= path
->nodes
[*level
];
2057 if (btrfs_header_level(cur
) != *level
)
2060 if (path
->slots
[*level
] >=
2061 btrfs_header_nritems(cur
))
2064 ret
= drop_leaf_ref(trans
, root
, cur
);
2068 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
2069 blocksize
= btrfs_level_size(root
, *level
- 1);
2070 ret
= lookup_extent_ref(trans
, root
, bytenr
, blocksize
, &refs
);
2073 parent
= path
->nodes
[*level
];
2074 root_owner
= btrfs_header_owner(parent
);
2075 root_gen
= btrfs_header_generation(parent
);
2076 path
->slots
[*level
]++;
2077 ret
= btrfs_free_extent(trans
, root
, bytenr
,
2078 blocksize
, root_owner
,
2083 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
2084 if (!next
|| !btrfs_buffer_uptodate(next
)) {
2085 free_extent_buffer(next
);
2086 reada_walk_down(root
, cur
, path
->slots
[*level
]);
2088 mutex_unlock(&root
->fs_info
->fs_mutex
);
2089 next
= read_tree_block(root
, bytenr
, blocksize
);
2090 mutex_lock(&root
->fs_info
->fs_mutex
);
2092 /* we've dropped the lock, double check */
2093 ret
= lookup_extent_ref(trans
, root
, bytenr
,
2097 parent
= path
->nodes
[*level
];
2098 root_owner
= btrfs_header_owner(parent
);
2099 root_gen
= btrfs_header_generation(parent
);
2101 path
->slots
[*level
]++;
2102 free_extent_buffer(next
);
2103 ret
= btrfs_free_extent(trans
, root
, bytenr
,
2111 btrfs_verify_block_csum(root
, next
);
2113 WARN_ON(*level
<= 0);
2114 if (path
->nodes
[*level
-1])
2115 free_extent_buffer(path
->nodes
[*level
-1]);
2116 path
->nodes
[*level
-1] = next
;
2117 *level
= btrfs_header_level(next
);
2118 path
->slots
[*level
] = 0;
2121 WARN_ON(*level
< 0);
2122 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2124 if (path
->nodes
[*level
] == root
->node
) {
2125 root_owner
= root
->root_key
.objectid
;
2126 parent
= path
->nodes
[*level
];
2128 parent
= path
->nodes
[*level
+ 1];
2129 root_owner
= btrfs_header_owner(parent
);
2132 root_gen
= btrfs_header_generation(parent
);
2133 ret
= btrfs_free_extent(trans
, root
, path
->nodes
[*level
]->start
,
2134 path
->nodes
[*level
]->len
,
2135 root_owner
, root_gen
, 0, 0, 1);
2136 free_extent_buffer(path
->nodes
[*level
]);
2137 path
->nodes
[*level
] = NULL
;
2144 * helper for dropping snapshots. This walks back up the tree in the path
2145 * to find the first node higher up where we haven't yet gone through
2148 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
2149 struct btrfs_root
*root
,
2150 struct btrfs_path
*path
, int *level
)
2154 struct btrfs_root_item
*root_item
= &root
->root_item
;
2159 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2160 slot
= path
->slots
[i
];
2161 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
2162 struct extent_buffer
*node
;
2163 struct btrfs_disk_key disk_key
;
2164 node
= path
->nodes
[i
];
2167 WARN_ON(*level
== 0);
2168 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
2169 memcpy(&root_item
->drop_progress
,
2170 &disk_key
, sizeof(disk_key
));
2171 root_item
->drop_level
= i
;
2174 if (path
->nodes
[*level
] == root
->node
) {
2175 root_owner
= root
->root_key
.objectid
;
2177 btrfs_header_generation(path
->nodes
[*level
]);
2179 struct extent_buffer
*node
;
2180 node
= path
->nodes
[*level
+ 1];
2181 root_owner
= btrfs_header_owner(node
);
2182 root_gen
= btrfs_header_generation(node
);
2184 ret
= btrfs_free_extent(trans
, root
,
2185 path
->nodes
[*level
]->start
,
2186 path
->nodes
[*level
]->len
,
2187 root_owner
, root_gen
, 0, 0, 1);
2189 free_extent_buffer(path
->nodes
[*level
]);
2190 path
->nodes
[*level
] = NULL
;
2198 * drop the reference count on the tree rooted at 'snap'. This traverses
2199 * the tree freeing any blocks that have a ref count of zero after being
2202 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
2208 struct btrfs_path
*path
;
2211 struct btrfs_root_item
*root_item
= &root
->root_item
;
2213 path
= btrfs_alloc_path();
2216 level
= btrfs_header_level(root
->node
);
2218 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2219 path
->nodes
[level
] = root
->node
;
2220 extent_buffer_get(root
->node
);
2221 path
->slots
[level
] = 0;
2223 struct btrfs_key key
;
2224 struct btrfs_disk_key found_key
;
2225 struct extent_buffer
*node
;
2227 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2228 level
= root_item
->drop_level
;
2229 path
->lowest_level
= level
;
2230 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2235 node
= path
->nodes
[level
];
2236 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
2237 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
2238 sizeof(found_key
)));
2241 wret
= walk_down_tree(trans
, root
, path
, &level
);
2247 wret
= walk_up_tree(trans
, root
, path
, &level
);
2255 for (i
= 0; i
<= orig_level
; i
++) {
2256 if (path
->nodes
[i
]) {
2257 free_extent_buffer(path
->nodes
[i
]);
2258 path
->nodes
[i
] = NULL
;
2262 btrfs_free_path(path
);
2266 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
2273 ret
= find_first_extent_bit(&info
->block_group_cache
, 0,
2274 &start
, &end
, (unsigned int)-1);
2277 ret
= get_state_private(&info
->block_group_cache
, start
, &ptr
);
2279 kfree((void *)(unsigned long)ptr
);
2280 clear_extent_bits(&info
->block_group_cache
, start
,
2281 end
, (unsigned int)-1, GFP_NOFS
);
2284 ret
= find_first_extent_bit(&info
->free_space_cache
, 0,
2285 &start
, &end
, EXTENT_DIRTY
);
2288 clear_extent_dirty(&info
->free_space_cache
, start
,
2294 static unsigned long calc_ra(unsigned long start
, unsigned long last
,
2297 return min(last
, start
+ nr
- 1);
2300 static int noinline
relocate_inode_pages(struct inode
*inode
, u64 start
,
2305 unsigned long last_index
;
2308 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2309 struct file_ra_state
*ra
;
2310 unsigned long total_read
= 0;
2311 unsigned long ra_pages
;
2313 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2315 mutex_lock(&inode
->i_mutex
);
2316 i
= start
>> PAGE_CACHE_SHIFT
;
2317 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
2319 ra_pages
= BTRFS_I(inode
)->root
->fs_info
->bdi
.ra_pages
;
2321 file_ra_state_init(ra
, inode
->i_mapping
);
2323 for (; i
<= last_index
; i
++) {
2324 if (total_read
% ra_pages
== 0) {
2325 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
,
2326 calc_ra(i
, last_index
, ra_pages
));
2329 page
= grab_cache_page(inode
->i_mapping
, i
);
2332 if (!PageUptodate(page
)) {
2333 btrfs_readpage(NULL
, page
);
2335 if (!PageUptodate(page
)) {
2337 page_cache_release(page
);
2341 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2342 ClearPageDirty(page
);
2344 cancel_dirty_page(page
, PAGE_CACHE_SIZE
);
2346 wait_on_page_writeback(page
);
2347 set_page_extent_mapped(page
);
2348 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2349 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2351 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2353 set_page_dirty(page
);
2354 set_extent_delalloc(io_tree
, page_start
,
2355 page_end
, GFP_NOFS
);
2357 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2359 page_cache_release(page
);
2360 balance_dirty_pages_ratelimited_nr(inode
->i_mapping
, 1);
2365 mutex_unlock(&inode
->i_mutex
);
2370 * note, this releases the path
2372 static int noinline
relocate_one_reference(struct btrfs_root
*extent_root
,
2373 struct btrfs_path
*path
,
2374 struct btrfs_key
*extent_key
)
2376 struct inode
*inode
;
2377 struct btrfs_root
*found_root
;
2378 struct btrfs_key
*root_location
;
2379 struct btrfs_extent_ref
*ref
;
2386 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2387 struct btrfs_extent_ref
);
2388 ref_root
= btrfs_ref_root(path
->nodes
[0], ref
);
2389 ref_gen
= btrfs_ref_generation(path
->nodes
[0], ref
);
2390 ref_objectid
= btrfs_ref_objectid(path
->nodes
[0], ref
);
2391 ref_offset
= btrfs_ref_offset(path
->nodes
[0], ref
);
2392 btrfs_release_path(extent_root
, path
);
2394 root_location
= kmalloc(sizeof(*root_location
), GFP_NOFS
);
2395 root_location
->objectid
= ref_root
;
2397 root_location
->offset
= 0;
2399 root_location
->offset
= (u64
)-1;
2400 root_location
->type
= BTRFS_ROOT_ITEM_KEY
;
2402 found_root
= btrfs_read_fs_root_no_name(extent_root
->fs_info
,
2404 BUG_ON(!found_root
);
2405 kfree(root_location
);
2407 if (ref_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
2408 mutex_unlock(&extent_root
->fs_info
->fs_mutex
);
2409 inode
= btrfs_iget_locked(extent_root
->fs_info
->sb
,
2410 ref_objectid
, found_root
);
2411 if (inode
->i_state
& I_NEW
) {
2412 /* the inode and parent dir are two different roots */
2413 BTRFS_I(inode
)->root
= found_root
;
2414 BTRFS_I(inode
)->location
.objectid
= ref_objectid
;
2415 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
2416 BTRFS_I(inode
)->location
.offset
= 0;
2417 btrfs_read_locked_inode(inode
);
2418 unlock_new_inode(inode
);
2421 /* this can happen if the reference is not against
2422 * the latest version of the tree root
2424 if (is_bad_inode(inode
)) {
2425 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2428 relocate_inode_pages(inode
, ref_offset
, extent_key
->offset
);
2430 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2432 struct btrfs_trans_handle
*trans
;
2433 struct btrfs_key found_key
;
2434 struct extent_buffer
*eb
;
2438 trans
= btrfs_start_transaction(found_root
, 1);
2439 eb
= read_tree_block(found_root
, extent_key
->objectid
,
2440 extent_key
->offset
);
2441 level
= btrfs_header_level(eb
);
2444 btrfs_item_key_to_cpu(eb
, &found_key
, 0);
2446 btrfs_node_key_to_cpu(eb
, &found_key
, 0);
2448 free_extent_buffer(eb
);
2450 path
->lowest_level
= level
;
2452 ret
= btrfs_search_slot(trans
, found_root
, &found_key
, path
,
2454 path
->lowest_level
= 0;
2455 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
2456 if (!path
->nodes
[i
])
2458 free_extent_buffer(path
->nodes
[i
]);
2459 path
->nodes
[i
] = NULL
;
2461 btrfs_release_path(found_root
, path
);
2462 btrfs_end_transaction(trans
, found_root
);
2469 static int noinline
relocate_one_extent(struct btrfs_root
*extent_root
,
2470 struct btrfs_path
*path
,
2471 struct btrfs_key
*extent_key
)
2473 struct btrfs_key key
;
2474 struct btrfs_key found_key
;
2475 struct extent_buffer
*leaf
;
2480 key
.objectid
= extent_key
->objectid
;
2481 key
.type
= BTRFS_EXTENT_REF_KEY
;
2485 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2491 leaf
= path
->nodes
[0];
2492 nritems
= btrfs_header_nritems(leaf
);
2493 if (path
->slots
[0] == nritems
)
2496 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2497 if (found_key
.objectid
!= extent_key
->objectid
)
2500 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
2503 key
.offset
= found_key
.offset
+ 1;
2504 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2506 ret
= relocate_one_reference(extent_root
, path
, extent_key
);
2512 btrfs_release_path(extent_root
, path
);
2516 static u64
update_block_group_flags(struct btrfs_root
*root
, u64 flags
)
2519 u64 stripped
= BTRFS_BLOCK_GROUP_RAID0
|
2520 BTRFS_BLOCK_GROUP_RAID1
| BTRFS_BLOCK_GROUP_RAID10
;
2522 num_devices
= btrfs_super_num_devices(&root
->fs_info
->super_copy
);
2523 if (num_devices
== 1) {
2524 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
2525 stripped
= flags
& ~stripped
;
2527 /* turn raid0 into single device chunks */
2528 if (flags
& BTRFS_BLOCK_GROUP_RAID0
)
2531 /* turn mirroring into duplication */
2532 if (flags
& (BTRFS_BLOCK_GROUP_RAID1
|
2533 BTRFS_BLOCK_GROUP_RAID10
))
2534 return stripped
| BTRFS_BLOCK_GROUP_DUP
;
2537 /* they already had raid on here, just return */
2538 if ((flags
& BTRFS_BLOCK_GROUP_DUP
) &&
2539 (flags
& BTRFS_BLOCK_GROUP_RAID1
)) {
2541 if (flags
& stripped
)
2544 stripped
|= BTRFS_BLOCK_GROUP_DUP
;
2545 stripped
= flags
& ~stripped
;
2547 /* switch duplicated blocks with raid1 */
2548 if (flags
& BTRFS_BLOCK_GROUP_DUP
)
2549 return stripped
| BTRFS_BLOCK_GROUP_RAID1
;
2551 /* turn single device chunks into raid0 */
2552 return stripped
| BTRFS_BLOCK_GROUP_RAID0
;
2557 int btrfs_shrink_extent_tree(struct btrfs_root
*root
, u64 shrink_start
)
2559 struct btrfs_trans_handle
*trans
;
2560 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
2561 struct btrfs_path
*path
;
2564 u64 shrink_last_byte
;
2565 u64 new_alloc_flags
;
2566 struct btrfs_block_group_cache
*shrink_block_group
;
2567 struct btrfs_fs_info
*info
= root
->fs_info
;
2568 struct btrfs_key key
;
2569 struct btrfs_key found_key
;
2570 struct extent_buffer
*leaf
;
2575 shrink_block_group
= btrfs_lookup_block_group(root
->fs_info
,
2577 BUG_ON(!shrink_block_group
);
2579 shrink_last_byte
= shrink_start
+ shrink_block_group
->key
.offset
;
2581 shrink_block_group
->space_info
->total_bytes
-=
2582 shrink_block_group
->key
.offset
;
2583 path
= btrfs_alloc_path();
2584 root
= root
->fs_info
->extent_root
;
2588 if (btrfs_block_group_used(&shrink_block_group
->item
) > 0) {
2589 trans
= btrfs_start_transaction(root
, 1);
2590 new_alloc_flags
= update_block_group_flags(root
,
2591 shrink_block_group
->flags
);
2592 do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
2593 btrfs_block_group_used(&shrink_block_group
->item
) +
2594 2 * 1024 * 1024, new_alloc_flags
);
2595 btrfs_end_transaction(trans
, root
);
2597 shrink_block_group
->ro
= 1;
2600 key
.objectid
= shrink_start
;
2603 cur_byte
= key
.objectid
;
2605 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2609 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
2614 leaf
= path
->nodes
[0];
2615 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2616 if (found_key
.objectid
+ found_key
.offset
> shrink_start
&&
2617 found_key
.objectid
< shrink_last_byte
) {
2618 cur_byte
= found_key
.objectid
;
2619 key
.objectid
= cur_byte
;
2622 btrfs_release_path(root
, path
);
2625 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2629 leaf
= path
->nodes
[0];
2630 nritems
= btrfs_header_nritems(leaf
);
2632 if (path
->slots
[0] >= nritems
) {
2633 ret
= btrfs_next_leaf(root
, path
);
2640 leaf
= path
->nodes
[0];
2641 nritems
= btrfs_header_nritems(leaf
);
2644 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2646 if (found_key
.objectid
>= shrink_last_byte
)
2649 if (progress
&& need_resched()) {
2650 memcpy(&key
, &found_key
, sizeof(key
));
2651 mutex_unlock(&root
->fs_info
->fs_mutex
);
2653 mutex_lock(&root
->fs_info
->fs_mutex
);
2654 btrfs_release_path(root
, path
);
2655 btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2661 if (btrfs_key_type(&found_key
) != BTRFS_EXTENT_ITEM_KEY
||
2662 found_key
.objectid
+ found_key
.offset
<= cur_byte
) {
2668 cur_byte
= found_key
.objectid
+ found_key
.offset
;
2669 key
.objectid
= cur_byte
;
2670 btrfs_release_path(root
, path
);
2671 ret
= relocate_one_extent(root
, path
, &found_key
);
2674 btrfs_release_path(root
, path
);
2676 if (total_found
> 0) {
2677 trans
= btrfs_start_transaction(tree_root
, 1);
2678 btrfs_commit_transaction(trans
, tree_root
);
2680 mutex_unlock(&root
->fs_info
->fs_mutex
);
2681 btrfs_clean_old_snapshots(tree_root
);
2682 mutex_lock(&root
->fs_info
->fs_mutex
);
2684 trans
= btrfs_start_transaction(tree_root
, 1);
2685 btrfs_commit_transaction(trans
, tree_root
);
2690 * we've freed all the extents, now remove the block
2691 * group item from the tree
2693 trans
= btrfs_start_transaction(root
, 1);
2694 memcpy(&key
, &shrink_block_group
->key
, sizeof(key
));
2696 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2702 leaf
= path
->nodes
[0];
2703 nritems
= btrfs_header_nritems(leaf
);
2704 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2705 kfree(shrink_block_group
);
2707 clear_extent_bits(&info
->block_group_cache
, found_key
.objectid
,
2708 found_key
.objectid
+ found_key
.offset
- 1,
2709 (unsigned int)-1, GFP_NOFS
);
2711 btrfs_del_item(trans
, root
, path
);
2712 clear_extent_dirty(&info
->free_space_cache
,
2713 shrink_start
, shrink_last_byte
- 1,
2715 btrfs_commit_transaction(trans
, root
);
2717 btrfs_free_path(path
);
2721 int find_first_block_group(struct btrfs_root
*root
, struct btrfs_path
*path
,
2722 struct btrfs_key
*key
)
2725 struct btrfs_key found_key
;
2726 struct extent_buffer
*leaf
;
2729 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
2733 slot
= path
->slots
[0];
2734 leaf
= path
->nodes
[0];
2735 if (slot
>= btrfs_header_nritems(leaf
)) {
2736 ret
= btrfs_next_leaf(root
, path
);
2743 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2745 if (found_key
.objectid
>= key
->objectid
&&
2746 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
2755 int btrfs_read_block_groups(struct btrfs_root
*root
)
2757 struct btrfs_path
*path
;
2760 struct btrfs_block_group_cache
*cache
;
2761 struct btrfs_fs_info
*info
= root
->fs_info
;
2762 struct btrfs_space_info
*space_info
;
2763 struct extent_io_tree
*block_group_cache
;
2764 struct btrfs_key key
;
2765 struct btrfs_key found_key
;
2766 struct extent_buffer
*leaf
;
2768 block_group_cache
= &info
->block_group_cache
;
2769 root
= info
->extent_root
;
2772 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2773 path
= btrfs_alloc_path();
2778 ret
= find_first_block_group(root
, path
, &key
);
2786 leaf
= path
->nodes
[0];
2787 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2788 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
2794 read_extent_buffer(leaf
, &cache
->item
,
2795 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2796 sizeof(cache
->item
));
2797 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
2799 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
2800 btrfs_release_path(root
, path
);
2801 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
2803 if (cache
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2804 bit
= BLOCK_GROUP_DATA
;
2805 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2806 bit
= BLOCK_GROUP_SYSTEM
;
2807 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2808 bit
= BLOCK_GROUP_METADATA
;
2810 set_avail_alloc_bits(info
, cache
->flags
);
2812 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
2813 btrfs_block_group_used(&cache
->item
),
2816 cache
->space_info
= space_info
;
2818 /* use EXTENT_LOCKED to prevent merging */
2819 set_extent_bits(block_group_cache
, found_key
.objectid
,
2820 found_key
.objectid
+ found_key
.offset
- 1,
2821 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2822 set_state_private(block_group_cache
, found_key
.objectid
,
2823 (unsigned long)cache
);
2826 btrfs_super_total_bytes(&info
->super_copy
))
2831 btrfs_free_path(path
);
2835 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
2836 struct btrfs_root
*root
, u64 bytes_used
,
2837 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
2842 struct btrfs_root
*extent_root
;
2843 struct btrfs_block_group_cache
*cache
;
2844 struct extent_io_tree
*block_group_cache
;
2846 extent_root
= root
->fs_info
->extent_root
;
2847 block_group_cache
= &root
->fs_info
->block_group_cache
;
2849 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
2851 cache
->key
.objectid
= chunk_offset
;
2852 cache
->key
.offset
= size
;
2854 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2855 memset(&cache
->item
, 0, sizeof(cache
->item
));
2856 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
2857 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
2858 cache
->flags
= type
;
2859 btrfs_set_block_group_flags(&cache
->item
, type
);
2861 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
2862 &cache
->space_info
);
2865 bit
= block_group_state_bits(type
);
2866 set_extent_bits(block_group_cache
, chunk_offset
,
2867 chunk_offset
+ size
- 1,
2868 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2870 set_state_private(block_group_cache
, chunk_offset
,
2871 (unsigned long)cache
);
2872 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
2873 sizeof(cache
->item
));
2876 finish_current_insert(trans
, extent_root
);
2877 ret
= del_pending_extents(trans
, extent_root
);
2879 set_avail_alloc_bits(extent_root
->fs_info
, type
);