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.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
37 btrfs_root
*extent_root
);
38 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
39 btrfs_root
*extent_root
);
40 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
41 struct btrfs_root
*root
, u64 bytes_used
,
42 u64 type
, u64 chunk_tree
, u64 chunk_objectid
,
45 static int cache_block_group(struct btrfs_root
*root
,
46 struct btrfs_block_group_cache
*block_group
)
48 struct btrfs_path
*path
;
51 struct extent_buffer
*leaf
;
52 struct extent_io_tree
*free_space_cache
;
62 root
= root
->fs_info
->extent_root
;
63 free_space_cache
= &root
->fs_info
->free_space_cache
;
65 if (block_group
->cached
)
68 path
= btrfs_alloc_path();
73 first_free
= block_group
->key
.objectid
;
74 key
.objectid
= block_group
->key
.objectid
;
76 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
77 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
80 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
84 leaf
= path
->nodes
[0];
85 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
86 if (key
.objectid
+ key
.offset
> first_free
)
87 first_free
= key
.objectid
+ key
.offset
;
90 leaf
= path
->nodes
[0];
91 slot
= path
->slots
[0];
92 if (slot
>= btrfs_header_nritems(leaf
)) {
93 ret
= btrfs_next_leaf(root
, path
);
102 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
103 if (key
.objectid
< block_group
->key
.objectid
) {
106 if (key
.objectid
>= block_group
->key
.objectid
+
107 block_group
->key
.offset
) {
111 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
116 if (key
.objectid
> last
) {
117 hole_size
= key
.objectid
- last
;
118 set_extent_dirty(free_space_cache
, last
,
119 last
+ hole_size
- 1,
122 last
= key
.objectid
+ key
.offset
;
130 if (block_group
->key
.objectid
+
131 block_group
->key
.offset
> last
) {
132 hole_size
= block_group
->key
.objectid
+
133 block_group
->key
.offset
- last
;
134 set_extent_dirty(free_space_cache
, last
,
135 last
+ hole_size
- 1, GFP_NOFS
);
137 block_group
->cached
= 1;
139 btrfs_free_path(path
);
143 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
147 struct extent_io_tree
*block_group_cache
;
148 struct btrfs_block_group_cache
*block_group
= NULL
;
154 block_group_cache
= &info
->block_group_cache
;
155 ret
= find_first_extent_bit(block_group_cache
,
156 bytenr
, &start
, &end
,
157 BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
|
162 ret
= get_state_private(block_group_cache
, start
, &ptr
);
166 block_group
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
167 if (block_group
->key
.objectid
<= bytenr
&& bytenr
<
168 block_group
->key
.objectid
+ block_group
->key
.offset
)
173 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
175 return (cache
->flags
& bits
) == bits
;
178 static int noinline
find_search_start(struct btrfs_root
*root
,
179 struct btrfs_block_group_cache
**cache_ret
,
180 u64
*start_ret
, int num
, int data
)
183 struct btrfs_block_group_cache
*cache
= *cache_ret
;
188 u64 search_start
= *start_ret
;
195 ret
= cache_block_group(root
, cache
);
199 last
= max(search_start
, cache
->key
.objectid
);
200 if (!block_group_bits(cache
, data
)) {
205 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
206 last
, &start
, &end
, EXTENT_DIRTY
);
213 start
= max(last
, start
);
215 if (last
- start
< num
) {
216 if (last
== cache
->key
.objectid
+ cache
->key
.offset
)
220 if (start
+ num
> cache
->key
.objectid
+ cache
->key
.offset
)
226 cache
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
228 printk("Unable to find block group for %llu\n",
229 (unsigned long long)search_start
);
235 last
= cache
->key
.objectid
+ cache
->key
.offset
;
237 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
247 if (cache_miss
&& !cache
->cached
) {
248 cache_block_group(root
, cache
);
250 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
252 cache
= btrfs_find_block_group(root
, cache
, last
, data
, 0);
260 static u64
div_factor(u64 num
, int factor
)
269 static int block_group_state_bits(u64 flags
)
272 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
273 bits
|= BLOCK_GROUP_DATA
;
274 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
275 bits
|= BLOCK_GROUP_METADATA
;
276 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
277 bits
|= BLOCK_GROUP_SYSTEM
;
281 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
282 struct btrfs_block_group_cache
283 *hint
, u64 search_start
,
286 struct btrfs_block_group_cache
*cache
;
287 struct extent_io_tree
*block_group_cache
;
288 struct btrfs_block_group_cache
*found_group
= NULL
;
289 struct btrfs_fs_info
*info
= root
->fs_info
;
302 block_group_cache
= &info
->block_group_cache
;
307 bit
= block_group_state_bits(data
);
310 struct btrfs_block_group_cache
*shint
;
311 shint
= btrfs_lookup_block_group(info
, search_start
);
312 if (shint
&& block_group_bits(shint
, data
)) {
313 used
= btrfs_block_group_used(&shint
->item
);
314 if (used
+ shint
->pinned
<
315 div_factor(shint
->key
.offset
, factor
)) {
320 if (hint
&& block_group_bits(hint
, data
)) {
321 used
= btrfs_block_group_used(&hint
->item
);
322 if (used
+ hint
->pinned
<
323 div_factor(hint
->key
.offset
, factor
)) {
326 last
= hint
->key
.objectid
+ hint
->key
.offset
;
330 hint_last
= max(hint
->key
.objectid
, search_start
);
332 hint_last
= search_start
;
338 ret
= find_first_extent_bit(block_group_cache
, last
,
343 ret
= get_state_private(block_group_cache
, start
, &ptr
);
347 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
348 last
= cache
->key
.objectid
+ cache
->key
.offset
;
349 used
= btrfs_block_group_used(&cache
->item
);
351 if (block_group_bits(cache
, data
)) {
353 free_check
= cache
->key
.offset
;
355 free_check
= div_factor(cache
->key
.offset
,
358 if (used
+ cache
->pinned
< free_check
) {
374 static u64
hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
375 u64 owner
, u64 owner_offset
)
377 u32 high_crc
= ~(u32
)0;
378 u32 low_crc
= ~(u32
)0;
381 lenum
= cpu_to_le64(root_objectid
);
382 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
383 lenum
= cpu_to_le64(ref_generation
);
384 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
385 if (owner
>= BTRFS_FIRST_FREE_OBJECTID
) {
386 lenum
= cpu_to_le64(owner
);
387 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
388 lenum
= cpu_to_le64(owner_offset
);
389 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
391 return ((u64
)high_crc
<< 32) | (u64
)low_crc
;
394 static int match_extent_ref(struct extent_buffer
*leaf
,
395 struct btrfs_extent_ref
*disk_ref
,
396 struct btrfs_extent_ref
*cpu_ref
)
401 if (cpu_ref
->objectid
)
402 len
= sizeof(*cpu_ref
);
404 len
= 2 * sizeof(u64
);
405 ret
= memcmp_extent_buffer(leaf
, cpu_ref
, (unsigned long)disk_ref
,
410 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
411 struct btrfs_root
*root
,
412 struct btrfs_path
*path
, u64 bytenr
,
414 u64 ref_generation
, u64 owner
,
415 u64 owner_offset
, int del
)
418 struct btrfs_key key
;
419 struct btrfs_key found_key
;
420 struct btrfs_extent_ref ref
;
421 struct extent_buffer
*leaf
;
422 struct btrfs_extent_ref
*disk_ref
;
426 btrfs_set_stack_ref_root(&ref
, root_objectid
);
427 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
428 btrfs_set_stack_ref_objectid(&ref
, owner
);
429 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
431 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
434 key
.objectid
= bytenr
;
435 key
.type
= BTRFS_EXTENT_REF_KEY
;
438 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
442 leaf
= path
->nodes
[0];
444 u32 nritems
= btrfs_header_nritems(leaf
);
445 if (path
->slots
[0] >= nritems
) {
446 ret2
= btrfs_next_leaf(root
, path
);
449 leaf
= path
->nodes
[0];
451 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
452 if (found_key
.objectid
!= bytenr
||
453 found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
455 key
.offset
= found_key
.offset
;
457 btrfs_release_path(root
, path
);
461 disk_ref
= btrfs_item_ptr(path
->nodes
[0],
463 struct btrfs_extent_ref
);
464 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
)) {
468 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
469 key
.offset
= found_key
.offset
+ 1;
470 btrfs_release_path(root
, path
);
477 * Back reference rules. Back refs have three main goals:
479 * 1) differentiate between all holders of references to an extent so that
480 * when a reference is dropped we can make sure it was a valid reference
481 * before freeing the extent.
483 * 2) Provide enough information to quickly find the holders of an extent
484 * if we notice a given block is corrupted or bad.
486 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
487 * maintenance. This is actually the same as #2, but with a slightly
488 * different use case.
490 * File extents can be referenced by:
492 * - multiple snapshots, subvolumes, or different generations in one subvol
493 * - different files inside a single subvolume (in theory, not implemented yet)
494 * - different offsets inside a file (bookend extents in file.c)
496 * The extent ref structure has fields for:
498 * - Objectid of the subvolume root
499 * - Generation number of the tree holding the reference
500 * - objectid of the file holding the reference
501 * - offset in the file corresponding to the key holding the reference
503 * When a file extent is allocated the fields are filled in:
504 * (root_key.objectid, trans->transid, inode objectid, offset in file)
506 * When a leaf is cow'd new references are added for every file extent found
507 * in the leaf. It looks the same as the create case, but trans->transid
508 * will be different when the block is cow'd.
510 * (root_key.objectid, trans->transid, inode objectid, offset in file)
512 * When a file extent is removed either during snapshot deletion or file
513 * truncation, the corresponding back reference is found
516 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
517 * inode objectid, offset in file)
519 * Btree extents can be referenced by:
521 * - Different subvolumes
522 * - Different generations of the same subvolume
524 * Storing sufficient information for a full reverse mapping of a btree
525 * block would require storing the lowest key of the block in the backref,
526 * and it would require updating that lowest key either before write out or
527 * every time it changed. Instead, the objectid of the lowest key is stored
528 * along with the level of the tree block. This provides a hint
529 * about where in the btree the block can be found. Searches through the
530 * btree only need to look for a pointer to that block, so they stop one
531 * level higher than the level recorded in the backref.
533 * Some btrees do not do reference counting on their extents. These
534 * include the extent tree and the tree of tree roots. Backrefs for these
535 * trees always have a generation of zero.
537 * When a tree block is created, back references are inserted:
539 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
541 * When a tree block is cow'd in a reference counted root,
542 * new back references are added for all the blocks it points to.
543 * These are of the form (trans->transid will have increased since creation):
545 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
547 * Because the lowest_key_objectid and the level are just hints
548 * they are not used when backrefs are deleted. When a backref is deleted:
550 * if backref was for a tree root:
551 * root_objectid = root->root_key.objectid
553 * root_objectid = btrfs_header_owner(parent)
555 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
557 * Back Reference Key hashing:
559 * Back references have four fields, each 64 bits long. Unfortunately,
560 * This is hashed into a single 64 bit number and placed into the key offset.
561 * The key objectid corresponds to the first byte in the extent, and the
562 * key type is set to BTRFS_EXTENT_REF_KEY
564 int btrfs_insert_extent_backref(struct btrfs_trans_handle
*trans
,
565 struct btrfs_root
*root
,
566 struct btrfs_path
*path
, u64 bytenr
,
567 u64 root_objectid
, u64 ref_generation
,
568 u64 owner
, u64 owner_offset
)
571 struct btrfs_key key
;
572 struct btrfs_extent_ref ref
;
573 struct btrfs_extent_ref
*disk_ref
;
576 btrfs_set_stack_ref_root(&ref
, root_objectid
);
577 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
578 btrfs_set_stack_ref_objectid(&ref
, owner
);
579 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
581 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
584 key
.objectid
= bytenr
;
585 key
.type
= BTRFS_EXTENT_REF_KEY
;
587 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(ref
));
588 while (ret
== -EEXIST
) {
589 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
590 struct btrfs_extent_ref
);
591 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
))
594 btrfs_release_path(root
, path
);
595 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
600 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
601 struct btrfs_extent_ref
);
602 write_extent_buffer(path
->nodes
[0], &ref
, (unsigned long)disk_ref
,
604 btrfs_mark_buffer_dirty(path
->nodes
[0]);
606 btrfs_release_path(root
, path
);
610 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
611 struct btrfs_root
*root
,
612 u64 bytenr
, u64 num_bytes
,
613 u64 root_objectid
, u64 ref_generation
,
614 u64 owner
, u64 owner_offset
)
616 struct btrfs_path
*path
;
618 struct btrfs_key key
;
619 struct extent_buffer
*l
;
620 struct btrfs_extent_item
*item
;
623 WARN_ON(num_bytes
< root
->sectorsize
);
624 path
= btrfs_alloc_path();
628 key
.objectid
= bytenr
;
629 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
630 key
.offset
= num_bytes
;
631 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
640 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
641 refs
= btrfs_extent_refs(l
, item
);
642 btrfs_set_extent_refs(l
, item
, refs
+ 1);
643 btrfs_mark_buffer_dirty(path
->nodes
[0]);
645 btrfs_release_path(root
->fs_info
->extent_root
, path
);
647 ret
= btrfs_insert_extent_backref(trans
, root
->fs_info
->extent_root
,
648 path
, bytenr
, root_objectid
,
649 ref_generation
, owner
, owner_offset
);
651 finish_current_insert(trans
, root
->fs_info
->extent_root
);
652 del_pending_extents(trans
, root
->fs_info
->extent_root
);
654 btrfs_free_path(path
);
658 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
659 struct btrfs_root
*root
)
661 finish_current_insert(trans
, root
->fs_info
->extent_root
);
662 del_pending_extents(trans
, root
->fs_info
->extent_root
);
666 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
667 struct btrfs_root
*root
, u64 bytenr
,
668 u64 num_bytes
, u32
*refs
)
670 struct btrfs_path
*path
;
672 struct btrfs_key key
;
673 struct extent_buffer
*l
;
674 struct btrfs_extent_item
*item
;
676 WARN_ON(num_bytes
< root
->sectorsize
);
677 path
= btrfs_alloc_path();
678 key
.objectid
= bytenr
;
679 key
.offset
= num_bytes
;
680 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
681 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
686 btrfs_print_leaf(root
, path
->nodes
[0]);
687 printk("failed to find block number %llu\n",
688 (unsigned long long)bytenr
);
692 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
693 *refs
= btrfs_extent_refs(l
, item
);
695 btrfs_free_path(path
);
699 u32
btrfs_count_snapshots_in_path(struct btrfs_root
*root
,
700 struct btrfs_path
*count_path
,
703 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
704 struct btrfs_path
*path
;
707 u64 root_objectid
= root
->root_key
.objectid
;
713 struct btrfs_key key
;
714 struct btrfs_key found_key
;
715 struct extent_buffer
*l
;
716 struct btrfs_extent_item
*item
;
717 struct btrfs_extent_ref
*ref_item
;
720 path
= btrfs_alloc_path();
723 bytenr
= first_extent
;
725 bytenr
= count_path
->nodes
[level
]->start
;
728 key
.objectid
= bytenr
;
731 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
732 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
738 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
740 if (found_key
.objectid
!= bytenr
||
741 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
745 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
746 refs
= btrfs_extent_refs(l
, item
);
748 nritems
= btrfs_header_nritems(l
);
749 if (path
->slots
[0] >= nritems
) {
750 ret
= btrfs_next_leaf(extent_root
, path
);
755 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
756 if (found_key
.objectid
!= bytenr
)
758 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
764 ref_item
= btrfs_item_ptr(l
, path
->slots
[0],
765 struct btrfs_extent_ref
);
766 found_objectid
= btrfs_ref_root(l
, ref_item
);
768 if (found_objectid
!= root_objectid
) {
775 if (cur_count
== 0) {
779 if (level
>= 0 && root
->node
== count_path
->nodes
[level
])
782 btrfs_release_path(root
, path
);
786 btrfs_free_path(path
);
789 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
790 struct btrfs_root
*root
, u64 owner_objectid
)
796 struct btrfs_disk_key disk_key
;
798 level
= btrfs_header_level(root
->node
);
799 generation
= trans
->transid
;
800 nritems
= btrfs_header_nritems(root
->node
);
803 btrfs_item_key(root
->node
, &disk_key
, 0);
805 btrfs_node_key(root
->node
, &disk_key
, 0);
806 key_objectid
= btrfs_disk_key_objectid(&disk_key
);
810 return btrfs_inc_extent_ref(trans
, root
, root
->node
->start
,
811 root
->node
->len
, owner_objectid
,
812 generation
, level
, key_objectid
);
815 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
816 struct extent_buffer
*buf
)
820 struct btrfs_key key
;
821 struct btrfs_file_extent_item
*fi
;
830 level
= btrfs_header_level(buf
);
831 nritems
= btrfs_header_nritems(buf
);
832 for (i
= 0; i
< nritems
; i
++) {
835 btrfs_item_key_to_cpu(buf
, &key
, i
);
836 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
838 fi
= btrfs_item_ptr(buf
, i
,
839 struct btrfs_file_extent_item
);
840 if (btrfs_file_extent_type(buf
, fi
) ==
841 BTRFS_FILE_EXTENT_INLINE
)
843 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
844 if (disk_bytenr
== 0)
846 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
,
847 btrfs_file_extent_disk_num_bytes(buf
, fi
),
848 root
->root_key
.objectid
, trans
->transid
,
849 key
.objectid
, key
.offset
);
855 bytenr
= btrfs_node_blockptr(buf
, i
);
856 btrfs_node_key_to_cpu(buf
, &key
, i
);
857 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
,
858 btrfs_level_size(root
, level
- 1),
859 root
->root_key
.objectid
,
861 level
- 1, key
.objectid
);
872 for (i
=0; i
< faili
; i
++) {
875 btrfs_item_key_to_cpu(buf
, &key
, i
);
876 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
878 fi
= btrfs_item_ptr(buf
, i
,
879 struct btrfs_file_extent_item
);
880 if (btrfs_file_extent_type(buf
, fi
) ==
881 BTRFS_FILE_EXTENT_INLINE
)
883 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
884 if (disk_bytenr
== 0)
886 err
= btrfs_free_extent(trans
, root
, disk_bytenr
,
887 btrfs_file_extent_disk_num_bytes(buf
,
891 bytenr
= btrfs_node_blockptr(buf
, i
);
892 err
= btrfs_free_extent(trans
, root
, bytenr
,
893 btrfs_level_size(root
, level
- 1), 0);
901 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
902 struct btrfs_root
*root
,
903 struct btrfs_path
*path
,
904 struct btrfs_block_group_cache
*cache
)
908 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
910 struct extent_buffer
*leaf
;
912 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
917 leaf
= path
->nodes
[0];
918 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
919 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
920 btrfs_mark_buffer_dirty(leaf
);
921 btrfs_release_path(extent_root
, path
);
923 finish_current_insert(trans
, extent_root
);
924 pending_ret
= del_pending_extents(trans
, extent_root
);
933 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
934 struct btrfs_root
*root
)
936 struct extent_io_tree
*block_group_cache
;
937 struct btrfs_block_group_cache
*cache
;
941 struct btrfs_path
*path
;
947 block_group_cache
= &root
->fs_info
->block_group_cache
;
948 path
= btrfs_alloc_path();
953 ret
= find_first_extent_bit(block_group_cache
, last
,
954 &start
, &end
, BLOCK_GROUP_DIRTY
);
959 ret
= get_state_private(block_group_cache
, start
, &ptr
);
963 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
964 err
= write_one_cache_group(trans
, root
,
967 * if we fail to write the cache group, we want
968 * to keep it marked dirty in hopes that a later
975 clear_extent_bits(block_group_cache
, start
, end
,
976 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
978 btrfs_free_path(path
);
982 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
985 struct list_head
*head
= &info
->space_info
;
986 struct list_head
*cur
;
987 struct btrfs_space_info
*found
;
988 list_for_each(cur
, head
) {
989 found
= list_entry(cur
, struct btrfs_space_info
, list
);
990 if (found
->flags
== flags
)
997 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
998 u64 total_bytes
, u64 bytes_used
,
999 struct btrfs_space_info
**space_info
)
1001 struct btrfs_space_info
*found
;
1003 found
= __find_space_info(info
, flags
);
1005 found
->total_bytes
+= total_bytes
;
1006 found
->bytes_used
+= bytes_used
;
1007 WARN_ON(found
->total_bytes
< found
->bytes_used
);
1008 *space_info
= found
;
1011 found
= kmalloc(sizeof(*found
), GFP_NOFS
);
1015 list_add(&found
->list
, &info
->space_info
);
1016 found
->flags
= flags
;
1017 found
->total_bytes
= total_bytes
;
1018 found
->bytes_used
= bytes_used
;
1019 found
->bytes_pinned
= 0;
1021 *space_info
= found
;
1026 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1028 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1029 BTRFS_BLOCK_GROUP_RAID1
|
1030 BTRFS_BLOCK_GROUP_DUP
);
1032 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1033 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1034 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1035 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1036 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1037 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1041 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1042 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1045 struct btrfs_space_info
*space_info
;
1051 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1053 ret
= update_space_info(extent_root
->fs_info
, flags
,
1057 BUG_ON(!space_info
);
1059 if (space_info
->full
)
1062 thresh
= div_factor(space_info
->total_bytes
, 7);
1063 if ((space_info
->bytes_used
+ space_info
->bytes_pinned
+ alloc_bytes
) <
1067 ret
= btrfs_alloc_chunk(trans
, extent_root
, &start
, &num_bytes
, flags
);
1068 if (ret
== -ENOSPC
) {
1069 printk("space info full %llu\n", (unsigned long long)flags
);
1070 space_info
->full
= 1;
1076 ret
= btrfs_make_block_group(trans
, extent_root
, 0, flags
,
1077 extent_root
->fs_info
->chunk_root
->root_key
.objectid
,
1083 static int update_block_group(struct btrfs_trans_handle
*trans
,
1084 struct btrfs_root
*root
,
1085 u64 bytenr
, u64 num_bytes
, int alloc
,
1088 struct btrfs_block_group_cache
*cache
;
1089 struct btrfs_fs_info
*info
= root
->fs_info
;
1090 u64 total
= num_bytes
;
1097 cache
= btrfs_lookup_block_group(info
, bytenr
);
1101 byte_in_group
= bytenr
- cache
->key
.objectid
;
1102 WARN_ON(byte_in_group
> cache
->key
.offset
);
1103 start
= cache
->key
.objectid
;
1104 end
= start
+ cache
->key
.offset
- 1;
1105 set_extent_bits(&info
->block_group_cache
, start
, end
,
1106 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
1108 old_val
= btrfs_block_group_used(&cache
->item
);
1109 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1111 old_val
+= num_bytes
;
1112 cache
->space_info
->bytes_used
+= num_bytes
;
1114 old_val
-= num_bytes
;
1115 cache
->space_info
->bytes_used
-= num_bytes
;
1117 set_extent_dirty(&info
->free_space_cache
,
1118 bytenr
, bytenr
+ num_bytes
- 1,
1122 btrfs_set_block_group_used(&cache
->item
, old_val
);
1124 bytenr
+= num_bytes
;
1129 static int update_pinned_extents(struct btrfs_root
*root
,
1130 u64 bytenr
, u64 num
, int pin
)
1133 struct btrfs_block_group_cache
*cache
;
1134 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1137 set_extent_dirty(&fs_info
->pinned_extents
,
1138 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1140 clear_extent_dirty(&fs_info
->pinned_extents
,
1141 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1144 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1146 len
= min(num
, cache
->key
.offset
-
1147 (bytenr
- cache
->key
.objectid
));
1149 cache
->pinned
+= len
;
1150 cache
->space_info
->bytes_pinned
+= len
;
1151 fs_info
->total_pinned
+= len
;
1153 cache
->pinned
-= len
;
1154 cache
->space_info
->bytes_pinned
-= len
;
1155 fs_info
->total_pinned
-= len
;
1163 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
1168 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1172 ret
= find_first_extent_bit(pinned_extents
, last
,
1173 &start
, &end
, EXTENT_DIRTY
);
1176 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1182 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1183 struct btrfs_root
*root
,
1184 struct extent_io_tree
*unpin
)
1189 struct extent_io_tree
*free_space_cache
;
1190 free_space_cache
= &root
->fs_info
->free_space_cache
;
1193 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1197 update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1198 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1199 set_extent_dirty(free_space_cache
, start
, end
, GFP_NOFS
);
1204 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1205 struct btrfs_root
*extent_root
)
1209 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1210 struct extent_buffer
*eb
;
1211 struct btrfs_path
*path
;
1212 struct btrfs_key ins
;
1213 struct btrfs_disk_key first
;
1214 struct btrfs_extent_item extent_item
;
1219 btrfs_set_stack_extent_refs(&extent_item
, 1);
1220 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
1221 path
= btrfs_alloc_path();
1224 ret
= find_first_extent_bit(&info
->extent_ins
, 0, &start
,
1225 &end
, EXTENT_LOCKED
);
1229 ins
.objectid
= start
;
1230 ins
.offset
= end
+ 1 - start
;
1231 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
1232 &extent_item
, sizeof(extent_item
));
1233 clear_extent_bits(&info
->extent_ins
, start
, end
, EXTENT_LOCKED
,
1235 eb
= read_tree_block(extent_root
, ins
.objectid
, ins
.offset
);
1236 level
= btrfs_header_level(eb
);
1238 btrfs_item_key(eb
, &first
, 0);
1240 btrfs_node_key(eb
, &first
, 0);
1242 err
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1243 start
, extent_root
->root_key
.objectid
,
1245 btrfs_disk_key_objectid(&first
));
1247 free_extent_buffer(eb
);
1249 btrfs_free_path(path
);
1253 static int pin_down_bytes(struct btrfs_root
*root
, u64 bytenr
, u32 num_bytes
,
1257 struct extent_buffer
*buf
;
1260 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1262 if (btrfs_buffer_uptodate(buf
)) {
1264 root
->fs_info
->running_transaction
->transid
;
1265 if (btrfs_header_generation(buf
) == transid
) {
1266 free_extent_buffer(buf
);
1270 free_extent_buffer(buf
);
1272 update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1274 set_extent_bits(&root
->fs_info
->pending_del
,
1275 bytenr
, bytenr
+ num_bytes
- 1,
1276 EXTENT_LOCKED
, GFP_NOFS
);
1283 * remove an extent from the root, returns 0 on success
1285 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1286 *root
, u64 bytenr
, u64 num_bytes
,
1287 u64 root_objectid
, u64 ref_generation
,
1288 u64 owner_objectid
, u64 owner_offset
, int pin
,
1291 struct btrfs_path
*path
;
1292 struct btrfs_key key
;
1293 struct btrfs_fs_info
*info
= root
->fs_info
;
1294 struct btrfs_extent_ops
*ops
= info
->extent_ops
;
1295 struct btrfs_root
*extent_root
= info
->extent_root
;
1296 struct extent_buffer
*leaf
;
1298 int extent_slot
= 0;
1299 int found_extent
= 0;
1301 struct btrfs_extent_item
*ei
;
1304 key
.objectid
= bytenr
;
1305 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1306 key
.offset
= num_bytes
;
1308 path
= btrfs_alloc_path();
1312 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1313 bytenr
, root_objectid
,
1315 owner_objectid
, owner_offset
, 1);
1317 struct btrfs_key found_key
;
1318 extent_slot
= path
->slots
[0];
1319 while(extent_slot
> 0) {
1321 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1323 if (found_key
.objectid
!= bytenr
)
1325 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1326 found_key
.offset
== num_bytes
) {
1330 if (path
->slots
[0] - extent_slot
> 5)
1334 ret
= btrfs_del_item(trans
, extent_root
, path
);
1336 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1338 printk("Unable to find ref byte nr %llu root %llu "
1339 " gen %llu owner %llu offset %llu\n",
1340 (unsigned long long)bytenr
,
1341 (unsigned long long)root_objectid
,
1342 (unsigned long long)ref_generation
,
1343 (unsigned long long)owner_objectid
,
1344 (unsigned long long)owner_offset
);
1346 if (!found_extent
) {
1347 btrfs_release_path(extent_root
, path
);
1348 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
1352 extent_slot
= path
->slots
[0];
1355 leaf
= path
->nodes
[0];
1356 ei
= btrfs_item_ptr(leaf
, extent_slot
,
1357 struct btrfs_extent_item
);
1358 refs
= btrfs_extent_refs(leaf
, ei
);
1361 btrfs_set_extent_refs(leaf
, ei
, refs
);
1363 btrfs_mark_buffer_dirty(leaf
);
1365 if (refs
== 0 && found_extent
&& path
->slots
[0] == extent_slot
+ 1) {
1366 /* if the back ref and the extent are next to each other
1367 * they get deleted below in one shot
1369 path
->slots
[0] = extent_slot
;
1371 } else if (found_extent
) {
1372 /* otherwise delete the extent back ref */
1373 ret
= btrfs_del_item(trans
, extent_root
, path
);
1375 /* if refs are 0, we need to setup the path for deletion */
1377 btrfs_release_path(extent_root
, path
);
1378 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
1391 ret
= pin_down_bytes(root
, bytenr
, num_bytes
, 0);
1397 /* block accounting for super block */
1398 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1399 btrfs_set_super_bytes_used(&info
->super_copy
,
1400 super_used
- num_bytes
);
1402 /* block accounting for root item */
1403 root_used
= btrfs_root_used(&root
->root_item
);
1404 btrfs_set_root_used(&root
->root_item
,
1405 root_used
- num_bytes
);
1406 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
1411 if (ops
&& ops
->free_extent
)
1412 ops
->free_extent(root
, bytenr
, num_bytes
);
1414 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1418 btrfs_free_path(path
);
1419 finish_current_insert(trans
, extent_root
);
1424 * find all the blocks marked as pending in the radix tree and remove
1425 * them from the extent map
1427 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1428 btrfs_root
*extent_root
)
1434 struct extent_io_tree
*pending_del
;
1435 struct extent_io_tree
*pinned_extents
;
1437 pending_del
= &extent_root
->fs_info
->pending_del
;
1438 pinned_extents
= &extent_root
->fs_info
->pinned_extents
;
1441 ret
= find_first_extent_bit(pending_del
, 0, &start
, &end
,
1445 update_pinned_extents(extent_root
, start
, end
+ 1 - start
, 1);
1446 clear_extent_bits(pending_del
, start
, end
, EXTENT_LOCKED
,
1448 ret
= __free_extent(trans
, extent_root
,
1449 start
, end
+ 1 - start
,
1450 extent_root
->root_key
.objectid
,
1459 * remove an extent from the root, returns 0 on success
1461 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1462 *root
, u64 bytenr
, u64 num_bytes
,
1463 u64 root_objectid
, u64 ref_generation
,
1464 u64 owner_objectid
, u64 owner_offset
, int pin
)
1466 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1470 WARN_ON(num_bytes
< root
->sectorsize
);
1471 if (!root
->ref_cows
)
1474 if (root
== extent_root
) {
1475 pin_down_bytes(root
, bytenr
, num_bytes
, 1);
1478 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, root_objectid
,
1479 ref_generation
, owner_objectid
, owner_offset
,
1481 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
1482 return ret
? ret
: pending_ret
;
1485 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
1487 u64 mask
= ((u64
)root
->stripesize
- 1);
1488 u64 ret
= (val
+ mask
) & ~mask
;
1493 * walks the btree of allocated extents and find a hole of a given size.
1494 * The key ins is changed to record the hole:
1495 * ins->objectid == block start
1496 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1497 * ins->offset == number of blocks
1498 * Any available blocks before search_start are skipped.
1500 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
1501 struct btrfs_root
*orig_root
,
1502 u64 num_bytes
, u64 empty_size
,
1503 u64 search_start
, u64 search_end
,
1504 u64 hint_byte
, struct btrfs_key
*ins
,
1505 u64 exclude_start
, u64 exclude_nr
,
1509 u64 orig_search_start
= search_start
;
1510 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
1511 struct btrfs_fs_info
*info
= root
->fs_info
;
1512 u64 total_needed
= num_bytes
;
1513 struct btrfs_block_group_cache
*block_group
;
1517 WARN_ON(num_bytes
< root
->sectorsize
);
1518 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1520 if (search_end
== (u64
)-1)
1521 search_end
= btrfs_super_total_bytes(&info
->super_copy
);
1524 block_group
= btrfs_lookup_block_group(info
, hint_byte
);
1526 hint_byte
= search_start
;
1527 block_group
= btrfs_find_block_group(root
, block_group
,
1528 hint_byte
, data
, 1);
1530 block_group
= btrfs_find_block_group(root
,
1532 search_start
, data
, 1);
1535 total_needed
+= empty_size
;
1539 block_group
= btrfs_lookup_block_group(info
, search_start
);
1541 block_group
= btrfs_lookup_block_group(info
,
1544 ret
= find_search_start(root
, &block_group
, &search_start
,
1545 total_needed
, data
);
1549 search_start
= stripe_align(root
, search_start
);
1550 ins
->objectid
= search_start
;
1551 ins
->offset
= num_bytes
;
1553 if (ins
->objectid
+ num_bytes
>= search_end
)
1556 if (ins
->objectid
+ num_bytes
>
1557 block_group
->key
.objectid
+ block_group
->key
.offset
) {
1558 search_start
= block_group
->key
.objectid
+
1559 block_group
->key
.offset
;
1563 if (test_range_bit(&info
->extent_ins
, ins
->objectid
,
1564 ins
->objectid
+ num_bytes
-1, EXTENT_LOCKED
, 0)) {
1565 search_start
= ins
->objectid
+ num_bytes
;
1569 if (test_range_bit(&info
->pinned_extents
, ins
->objectid
,
1570 ins
->objectid
+ num_bytes
-1, EXTENT_DIRTY
, 0)) {
1571 search_start
= ins
->objectid
+ num_bytes
;
1575 if (exclude_nr
> 0 && (ins
->objectid
+ num_bytes
> exclude_start
&&
1576 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1577 search_start
= exclude_start
+ exclude_nr
;
1581 if (!(data
& BTRFS_BLOCK_GROUP_DATA
)) {
1582 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1584 trans
->block_group
= block_group
;
1586 ins
->offset
= num_bytes
;
1590 if (search_start
+ num_bytes
>= search_end
) {
1592 search_start
= orig_search_start
;
1599 total_needed
-= empty_size
;
1604 block_group
= btrfs_lookup_block_group(info
, search_start
);
1606 block_group
= btrfs_find_block_group(root
, block_group
,
1607 search_start
, data
, 0);
1614 * finds a free extent and does all the dirty work required for allocation
1615 * returns the key for the extent through ins, and a tree buffer for
1616 * the first block of the extent through buf.
1618 * returns 0 if everything worked, non-zero otherwise.
1620 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1621 struct btrfs_root
*root
,
1622 u64 num_bytes
, u64 root_objectid
, u64 ref_generation
,
1623 u64 owner
, u64 owner_offset
,
1624 u64 empty_size
, u64 hint_byte
,
1625 u64 search_end
, struct btrfs_key
*ins
, int data
)
1629 u64 super_used
, root_used
;
1630 u64 search_start
= 0;
1632 struct btrfs_fs_info
*info
= root
->fs_info
;
1633 struct btrfs_extent_ops
*ops
= info
->extent_ops
;
1635 struct btrfs_root
*extent_root
= info
->extent_root
;
1636 struct btrfs_path
*path
;
1637 struct btrfs_extent_item
*extent_item
;
1638 struct btrfs_extent_ref
*ref
;
1639 struct btrfs_key keys
[2];
1642 alloc_profile
= info
->avail_data_alloc_bits
&
1643 info
->data_alloc_profile
;
1644 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
1645 } else if (root
== info
->chunk_root
|| info
->force_system_allocs
) {
1646 alloc_profile
= info
->avail_system_alloc_bits
&
1647 info
->system_alloc_profile
;
1648 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
1650 alloc_profile
= info
->avail_metadata_alloc_bits
&
1651 info
->metadata_alloc_profile
;
1652 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
1655 if (root
->ref_cows
) {
1656 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
1657 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1659 BTRFS_BLOCK_GROUP_METADATA
);
1662 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1663 num_bytes
+ 2 * 1024 * 1024, data
);
1667 WARN_ON(num_bytes
< root
->sectorsize
);
1668 if (ops
&& ops
->alloc_extent
) {
1669 ret
= ops
->alloc_extent(root
, num_bytes
, hint_byte
, ins
);
1671 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
1672 search_start
, search_end
, hint_byte
,
1673 ins
, trans
->alloc_exclude_start
,
1674 trans
->alloc_exclude_nr
, data
);
1680 /* block accounting for super block */
1681 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1682 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
1684 /* block accounting for root item */
1685 root_used
= btrfs_root_used(&root
->root_item
);
1686 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
1688 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
1689 ins
->objectid
, ins
->objectid
+ ins
->offset
- 1,
1692 if (root
== extent_root
) {
1693 set_extent_bits(&root
->fs_info
->extent_ins
, ins
->objectid
,
1694 ins
->objectid
+ ins
->offset
- 1,
1695 EXTENT_LOCKED
, GFP_NOFS
);
1699 WARN_ON(trans
->alloc_exclude_nr
);
1700 trans
->alloc_exclude_start
= ins
->objectid
;
1701 trans
->alloc_exclude_nr
= ins
->offset
;
1703 memcpy(&keys
[0], ins
, sizeof(*ins
));
1704 keys
[1].offset
= hash_extent_ref(root_objectid
, ref_generation
,
1705 owner
, owner_offset
);
1706 keys
[1].objectid
= ins
->objectid
;
1707 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
1708 sizes
[0] = sizeof(*extent_item
);
1709 sizes
[1] = sizeof(*ref
);
1711 path
= btrfs_alloc_path();
1714 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
1718 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1719 struct btrfs_extent_item
);
1720 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, 1);
1721 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1722 struct btrfs_extent_ref
);
1724 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
1725 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
1726 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
1727 btrfs_set_ref_offset(path
->nodes
[0], ref
, owner_offset
);
1729 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1731 trans
->alloc_exclude_start
= 0;
1732 trans
->alloc_exclude_nr
= 0;
1733 btrfs_free_path(path
);
1734 finish_current_insert(trans
, extent_root
);
1735 pending_ret
= del_pending_extents(trans
, extent_root
);
1745 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0);
1747 printk("update block group failed for %llu %llu\n",
1748 (unsigned long long)ins
->objectid
,
1749 (unsigned long long)ins
->offset
);
1756 * helper function to allocate a block for a given tree
1757 * returns the tree buffer or NULL.
1759 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1760 struct btrfs_root
*root
,
1762 u64 root_objectid
, u64 hint
,
1768 ref_generation
= trans
->transid
;
1773 return __btrfs_alloc_free_block(trans
, root
, blocksize
, root_objectid
,
1774 ref_generation
, 0, 0, hint
, empty_size
);
1778 * helper function to allocate a block for a given tree
1779 * returns the tree buffer or NULL.
1781 struct extent_buffer
*__btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1782 struct btrfs_root
*root
,
1791 struct btrfs_key ins
;
1793 struct extent_buffer
*buf
;
1795 ret
= btrfs_alloc_extent(trans
, root
, blocksize
,
1796 root_objectid
, ref_generation
,
1797 level
, first_objectid
, empty_size
, hint
,
1801 return ERR_PTR(ret
);
1803 buf
= btrfs_find_create_tree_block(root
, ins
.objectid
, blocksize
);
1805 btrfs_free_extent(trans
, root
, ins
.objectid
, blocksize
,
1806 root
->root_key
.objectid
, ref_generation
,
1809 return ERR_PTR(-ENOMEM
);
1811 btrfs_set_buffer_uptodate(buf
);
1812 trans
->blocks_used
++;
1816 static int noinline
drop_leaf_ref(struct btrfs_trans_handle
*trans
,
1817 struct btrfs_root
*root
,
1818 struct extent_buffer
*leaf
)
1821 u64 leaf_generation
;
1822 struct btrfs_key key
;
1823 struct btrfs_file_extent_item
*fi
;
1828 BUG_ON(!btrfs_is_leaf(leaf
));
1829 nritems
= btrfs_header_nritems(leaf
);
1830 leaf_owner
= btrfs_header_owner(leaf
);
1831 leaf_generation
= btrfs_header_generation(leaf
);
1833 for (i
= 0; i
< nritems
; i
++) {
1836 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1837 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1839 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1840 if (btrfs_file_extent_type(leaf
, fi
) ==
1841 BTRFS_FILE_EXTENT_INLINE
)
1844 * FIXME make sure to insert a trans record that
1845 * repeats the snapshot del on crash
1847 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1848 if (disk_bytenr
== 0)
1850 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
1851 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
1852 leaf_owner
, leaf_generation
,
1853 key
.objectid
, key
.offset
, 0);
1859 static void noinline
reada_walk_down(struct btrfs_root
*root
,
1860 struct extent_buffer
*node
,
1873 nritems
= btrfs_header_nritems(node
);
1874 level
= btrfs_header_level(node
);
1878 for (i
= slot
; i
< nritems
&& skipped
< 32; i
++) {
1879 bytenr
= btrfs_node_blockptr(node
, i
);
1880 if (last
&& ((bytenr
> last
&& bytenr
- last
> 32 * 1024) ||
1881 (last
> bytenr
&& last
- bytenr
> 32 * 1024))) {
1885 blocksize
= btrfs_level_size(root
, level
- 1);
1887 ret
= lookup_extent_ref(NULL
, root
, bytenr
,
1895 mutex_unlock(&root
->fs_info
->fs_mutex
);
1896 ret
= readahead_tree_block(root
, bytenr
, blocksize
);
1897 last
= bytenr
+ blocksize
;
1899 mutex_lock(&root
->fs_info
->fs_mutex
);
1906 * helper function for drop_snapshot, this walks down the tree dropping ref
1907 * counts as it goes.
1909 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
1910 struct btrfs_root
*root
,
1911 struct btrfs_path
*path
, int *level
)
1916 struct extent_buffer
*next
;
1917 struct extent_buffer
*cur
;
1918 struct extent_buffer
*parent
;
1923 WARN_ON(*level
< 0);
1924 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1925 ret
= lookup_extent_ref(trans
, root
,
1926 path
->nodes
[*level
]->start
,
1927 path
->nodes
[*level
]->len
, &refs
);
1933 * walk down to the last node level and free all the leaves
1935 while(*level
>= 0) {
1936 WARN_ON(*level
< 0);
1937 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1938 cur
= path
->nodes
[*level
];
1940 if (btrfs_header_level(cur
) != *level
)
1943 if (path
->slots
[*level
] >=
1944 btrfs_header_nritems(cur
))
1947 ret
= drop_leaf_ref(trans
, root
, cur
);
1951 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1952 blocksize
= btrfs_level_size(root
, *level
- 1);
1953 ret
= lookup_extent_ref(trans
, root
, bytenr
, blocksize
, &refs
);
1956 parent
= path
->nodes
[*level
];
1957 root_owner
= btrfs_header_owner(parent
);
1958 root_gen
= btrfs_header_generation(parent
);
1959 path
->slots
[*level
]++;
1960 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1961 blocksize
, root_owner
,
1966 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
1967 if (!next
|| !btrfs_buffer_uptodate(next
)) {
1968 free_extent_buffer(next
);
1969 reada_walk_down(root
, cur
, path
->slots
[*level
]);
1970 mutex_unlock(&root
->fs_info
->fs_mutex
);
1971 next
= read_tree_block(root
, bytenr
, blocksize
);
1972 mutex_lock(&root
->fs_info
->fs_mutex
);
1974 /* we dropped the lock, check one more time */
1975 ret
= lookup_extent_ref(trans
, root
, bytenr
,
1979 parent
= path
->nodes
[*level
];
1980 root_owner
= btrfs_header_owner(parent
);
1981 root_gen
= btrfs_header_generation(parent
);
1983 path
->slots
[*level
]++;
1984 free_extent_buffer(next
);
1985 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1993 WARN_ON(*level
<= 0);
1994 if (path
->nodes
[*level
-1])
1995 free_extent_buffer(path
->nodes
[*level
-1]);
1996 path
->nodes
[*level
-1] = next
;
1997 *level
= btrfs_header_level(next
);
1998 path
->slots
[*level
] = 0;
2001 WARN_ON(*level
< 0);
2002 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2004 if (path
->nodes
[*level
] == root
->node
) {
2005 root_owner
= root
->root_key
.objectid
;
2006 parent
= path
->nodes
[*level
];
2008 parent
= path
->nodes
[*level
+ 1];
2009 root_owner
= btrfs_header_owner(parent
);
2012 root_gen
= btrfs_header_generation(parent
);
2013 ret
= btrfs_free_extent(trans
, root
, path
->nodes
[*level
]->start
,
2014 path
->nodes
[*level
]->len
,
2015 root_owner
, root_gen
, 0, 0, 1);
2016 free_extent_buffer(path
->nodes
[*level
]);
2017 path
->nodes
[*level
] = NULL
;
2024 * helper for dropping snapshots. This walks back up the tree in the path
2025 * to find the first node higher up where we haven't yet gone through
2028 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
2029 struct btrfs_root
*root
,
2030 struct btrfs_path
*path
, int *level
)
2034 struct btrfs_root_item
*root_item
= &root
->root_item
;
2039 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2040 slot
= path
->slots
[i
];
2041 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
2042 struct extent_buffer
*node
;
2043 struct btrfs_disk_key disk_key
;
2044 node
= path
->nodes
[i
];
2047 WARN_ON(*level
== 0);
2048 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
2049 memcpy(&root_item
->drop_progress
,
2050 &disk_key
, sizeof(disk_key
));
2051 root_item
->drop_level
= i
;
2054 if (path
->nodes
[*level
] == root
->node
) {
2055 root_owner
= root
->root_key
.objectid
;
2057 btrfs_header_generation(path
->nodes
[*level
]);
2059 struct extent_buffer
*node
;
2060 node
= path
->nodes
[*level
+ 1];
2061 root_owner
= btrfs_header_owner(node
);
2062 root_gen
= btrfs_header_generation(node
);
2064 ret
= btrfs_free_extent(trans
, root
,
2065 path
->nodes
[*level
]->start
,
2066 path
->nodes
[*level
]->len
,
2067 root_owner
, root_gen
, 0, 0, 1);
2069 free_extent_buffer(path
->nodes
[*level
]);
2070 path
->nodes
[*level
] = NULL
;
2078 * drop the reference count on the tree rooted at 'snap'. This traverses
2079 * the tree freeing any blocks that have a ref count of zero after being
2082 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
2088 struct btrfs_path
*path
;
2091 struct btrfs_root_item
*root_item
= &root
->root_item
;
2093 path
= btrfs_alloc_path();
2096 level
= btrfs_header_level(root
->node
);
2098 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2099 path
->nodes
[level
] = root
->node
;
2100 extent_buffer_get(root
->node
);
2101 path
->slots
[level
] = 0;
2103 struct btrfs_key key
;
2104 struct btrfs_disk_key found_key
;
2105 struct extent_buffer
*node
;
2107 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2108 level
= root_item
->drop_level
;
2109 path
->lowest_level
= level
;
2110 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2115 node
= path
->nodes
[level
];
2116 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
2117 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
2118 sizeof(found_key
)));
2121 wret
= walk_down_tree(trans
, root
, path
, &level
);
2127 wret
= walk_up_tree(trans
, root
, path
, &level
);
2137 for (i
= 0; i
<= orig_level
; i
++) {
2138 if (path
->nodes
[i
]) {
2139 free_extent_buffer(path
->nodes
[i
]);
2140 path
->nodes
[i
] = NULL
;
2144 btrfs_free_path(path
);
2148 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
2155 ret
= find_first_extent_bit(&info
->block_group_cache
, 0,
2156 &start
, &end
, (unsigned int)-1);
2159 ret
= get_state_private(&info
->block_group_cache
, start
, &ptr
);
2161 kfree((void *)(unsigned long)ptr
);
2162 clear_extent_bits(&info
->block_group_cache
, start
,
2163 end
, (unsigned int)-1, GFP_NOFS
);
2166 ret
= find_first_extent_bit(&info
->free_space_cache
, 0,
2167 &start
, &end
, EXTENT_DIRTY
);
2170 clear_extent_dirty(&info
->free_space_cache
, start
,
2176 int find_first_block_group(struct btrfs_root
*root
, struct btrfs_path
*path
,
2177 struct btrfs_key
*key
)
2180 struct btrfs_key found_key
;
2181 struct extent_buffer
*leaf
;
2184 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
2188 slot
= path
->slots
[0];
2189 leaf
= path
->nodes
[0];
2190 if (slot
>= btrfs_header_nritems(leaf
)) {
2191 ret
= btrfs_next_leaf(root
, path
);
2198 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2200 if (found_key
.objectid
>= key
->objectid
&&
2201 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
2210 int btrfs_read_block_groups(struct btrfs_root
*root
)
2212 struct btrfs_path
*path
;
2215 struct btrfs_block_group_cache
*cache
;
2216 struct btrfs_fs_info
*info
= root
->fs_info
;
2217 struct btrfs_space_info
*space_info
;
2218 struct extent_io_tree
*block_group_cache
;
2219 struct btrfs_key key
;
2220 struct btrfs_key found_key
;
2221 struct extent_buffer
*leaf
;
2223 block_group_cache
= &info
->block_group_cache
;
2225 root
= info
->extent_root
;
2228 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2229 path
= btrfs_alloc_path();
2234 ret
= find_first_block_group(root
, path
, &key
);
2242 leaf
= path
->nodes
[0];
2243 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2244 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2250 read_extent_buffer(leaf
, &cache
->item
,
2251 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2252 sizeof(cache
->item
));
2253 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
2256 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
2257 btrfs_release_path(root
, path
);
2258 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
2260 if (cache
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2261 bit
= BLOCK_GROUP_DATA
;
2262 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2263 bit
= BLOCK_GROUP_SYSTEM
;
2264 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2265 bit
= BLOCK_GROUP_METADATA
;
2267 set_avail_alloc_bits(info
, cache
->flags
);
2269 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
2270 btrfs_block_group_used(&cache
->item
),
2273 cache
->space_info
= space_info
;
2275 /* use EXTENT_LOCKED to prevent merging */
2276 set_extent_bits(block_group_cache
, found_key
.objectid
,
2277 found_key
.objectid
+ found_key
.offset
- 1,
2278 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2279 set_state_private(block_group_cache
, found_key
.objectid
,
2280 (unsigned long)cache
);
2283 btrfs_super_total_bytes(&info
->super_copy
))
2288 btrfs_free_path(path
);
2292 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
2293 struct btrfs_root
*root
, u64 bytes_used
,
2294 u64 type
, u64 chunk_tree
, u64 chunk_objectid
,
2299 struct btrfs_root
*extent_root
;
2300 struct btrfs_block_group_cache
*cache
;
2301 struct extent_io_tree
*block_group_cache
;
2303 extent_root
= root
->fs_info
->extent_root
;
2304 block_group_cache
= &root
->fs_info
->block_group_cache
;
2306 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
2308 cache
->key
.objectid
= chunk_objectid
;
2309 cache
->key
.offset
= size
;
2310 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2311 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
2312 btrfs_set_block_group_chunk_tree(&cache
->item
, chunk_tree
);
2313 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
2314 cache
->flags
= type
;
2315 btrfs_set_block_group_flags(&cache
->item
, type
);
2317 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
2318 &cache
->space_info
);
2321 bit
= block_group_state_bits(type
);
2322 set_extent_bits(block_group_cache
, chunk_objectid
,
2323 chunk_objectid
+ size
- 1,
2324 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2325 set_state_private(block_group_cache
, chunk_objectid
,
2326 (unsigned long)cache
);
2328 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
2329 sizeof(cache
->item
));
2332 finish_current_insert(trans
, extent_root
);
2333 ret
= del_pending_extents(trans
, extent_root
);
2335 set_avail_alloc_bits(extent_root
->fs_info
, type
);
2339 u64
btrfs_hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
2340 u64 owner
, u64 owner_offset
)
2342 return hash_extent_ref(root_objectid
, ref_generation
,
2343 owner
, owner_offset
);
2346 int btrfs_update_block_group(struct btrfs_trans_handle
*trans
,
2347 struct btrfs_root
*root
,
2348 u64 bytenr
, u64 num_bytes
, int alloc
,
2351 return update_block_group(trans
, root
, bytenr
, num_bytes
,