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
);
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
;
184 u64 search_start
= *start_ret
;
191 ret
= cache_block_group(root
, cache
);
195 last
= max(search_start
, cache
->key
.objectid
);
196 if (!block_group_bits(cache
, data
)) {
201 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
202 last
, &start
, &end
, EXTENT_DIRTY
);
209 start
= max(last
, start
);
211 if (last
- start
< num
) {
212 if (last
== cache
->key
.objectid
+ cache
->key
.offset
)
216 if (start
+ num
> cache
->key
.objectid
+ cache
->key
.offset
)
222 cache
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
224 printk("Unable to find block group for %llu\n",
225 (unsigned long long)search_start
);
231 last
= cache
->key
.objectid
+ cache
->key
.offset
;
233 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
243 if (cache_miss
&& !cache
->cached
) {
244 cache_block_group(root
, cache
);
246 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
248 cache
= btrfs_find_block_group(root
, cache
, last
, data
, 0);
256 static u64
div_factor(u64 num
, int factor
)
265 static int block_group_state_bits(u64 flags
)
268 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
269 bits
|= BLOCK_GROUP_DATA
;
270 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
271 bits
|= BLOCK_GROUP_METADATA
;
272 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
273 bits
|= BLOCK_GROUP_SYSTEM
;
277 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
278 struct btrfs_block_group_cache
279 *hint
, u64 search_start
,
282 struct btrfs_block_group_cache
*cache
;
283 struct extent_io_tree
*block_group_cache
;
284 struct btrfs_block_group_cache
*found_group
= NULL
;
285 struct btrfs_fs_info
*info
= root
->fs_info
;
298 block_group_cache
= &info
->block_group_cache
;
303 bit
= block_group_state_bits(data
);
306 struct btrfs_block_group_cache
*shint
;
307 shint
= btrfs_lookup_block_group(info
, search_start
);
308 if (shint
&& block_group_bits(shint
, data
)) {
309 used
= btrfs_block_group_used(&shint
->item
);
310 if (used
+ shint
->pinned
<
311 div_factor(shint
->key
.offset
, factor
)) {
316 if (hint
&& block_group_bits(hint
, data
)) {
317 used
= btrfs_block_group_used(&hint
->item
);
318 if (used
+ hint
->pinned
<
319 div_factor(hint
->key
.offset
, factor
)) {
322 last
= hint
->key
.objectid
+ hint
->key
.offset
;
326 hint_last
= max(hint
->key
.objectid
, search_start
);
328 hint_last
= search_start
;
334 ret
= find_first_extent_bit(block_group_cache
, last
,
339 ret
= get_state_private(block_group_cache
, start
, &ptr
);
343 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
344 last
= cache
->key
.objectid
+ cache
->key
.offset
;
345 used
= btrfs_block_group_used(&cache
->item
);
347 if (block_group_bits(cache
, data
)) {
349 free_check
= cache
->key
.offset
;
351 free_check
= div_factor(cache
->key
.offset
,
354 if (used
+ cache
->pinned
< free_check
) {
370 static u64
hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
371 u64 owner
, u64 owner_offset
)
373 u32 high_crc
= ~(u32
)0;
374 u32 low_crc
= ~(u32
)0;
377 lenum
= cpu_to_le64(root_objectid
);
378 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
379 lenum
= cpu_to_le64(ref_generation
);
380 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
381 if (owner
>= BTRFS_FIRST_FREE_OBJECTID
) {
382 lenum
= cpu_to_le64(owner
);
383 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
384 lenum
= cpu_to_le64(owner_offset
);
385 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
387 return ((u64
)high_crc
<< 32) | (u64
)low_crc
;
390 static int match_extent_ref(struct extent_buffer
*leaf
,
391 struct btrfs_extent_ref
*disk_ref
,
392 struct btrfs_extent_ref
*cpu_ref
)
397 if (cpu_ref
->objectid
)
398 len
= sizeof(*cpu_ref
);
400 len
= 2 * sizeof(u64
);
401 ret
= memcmp_extent_buffer(leaf
, cpu_ref
, (unsigned long)disk_ref
,
406 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
407 struct btrfs_root
*root
,
408 struct btrfs_path
*path
, u64 bytenr
,
410 u64 ref_generation
, u64 owner
,
411 u64 owner_offset
, int del
)
414 struct btrfs_key key
;
415 struct btrfs_key found_key
;
416 struct btrfs_extent_ref ref
;
417 struct extent_buffer
*leaf
;
418 struct btrfs_extent_ref
*disk_ref
;
422 btrfs_set_stack_ref_root(&ref
, root_objectid
);
423 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
424 btrfs_set_stack_ref_objectid(&ref
, owner
);
425 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
427 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
430 key
.objectid
= bytenr
;
431 key
.type
= BTRFS_EXTENT_REF_KEY
;
434 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
438 leaf
= path
->nodes
[0];
440 u32 nritems
= btrfs_header_nritems(leaf
);
441 if (path
->slots
[0] >= nritems
) {
442 ret2
= btrfs_next_leaf(root
, path
);
445 leaf
= path
->nodes
[0];
447 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
448 if (found_key
.objectid
!= bytenr
||
449 found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
451 key
.offset
= found_key
.offset
;
453 btrfs_release_path(root
, path
);
457 disk_ref
= btrfs_item_ptr(path
->nodes
[0],
459 struct btrfs_extent_ref
);
460 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
)) {
464 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
465 key
.offset
= found_key
.offset
+ 1;
466 btrfs_release_path(root
, path
);
473 * Back reference rules. Back refs have three main goals:
475 * 1) differentiate between all holders of references to an extent so that
476 * when a reference is dropped we can make sure it was a valid reference
477 * before freeing the extent.
479 * 2) Provide enough information to quickly find the holders of an extent
480 * if we notice a given block is corrupted or bad.
482 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
483 * maintenance. This is actually the same as #2, but with a slightly
484 * different use case.
486 * File extents can be referenced by:
488 * - multiple snapshots, subvolumes, or different generations in one subvol
489 * - different files inside a single subvolume (in theory, not implemented yet)
490 * - different offsets inside a file (bookend extents in file.c)
492 * The extent ref structure has fields for:
494 * - Objectid of the subvolume root
495 * - Generation number of the tree holding the reference
496 * - objectid of the file holding the reference
497 * - offset in the file corresponding to the key holding the reference
499 * When a file extent is allocated the fields are filled in:
500 * (root_key.objectid, trans->transid, inode objectid, offset in file)
502 * When a leaf is cow'd new references are added for every file extent found
503 * in the leaf. It looks the same as the create case, but trans->transid
504 * will be different when the block is cow'd.
506 * (root_key.objectid, trans->transid, inode objectid, offset in file)
508 * When a file extent is removed either during snapshot deletion or file
509 * truncation, the corresponding back reference is found
512 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
513 * inode objectid, offset in file)
515 * Btree extents can be referenced by:
517 * - Different subvolumes
518 * - Different generations of the same subvolume
520 * Storing sufficient information for a full reverse mapping of a btree
521 * block would require storing the lowest key of the block in the backref,
522 * and it would require updating that lowest key either before write out or
523 * every time it changed. Instead, the objectid of the lowest key is stored
524 * along with the level of the tree block. This provides a hint
525 * about where in the btree the block can be found. Searches through the
526 * btree only need to look for a pointer to that block, so they stop one
527 * level higher than the level recorded in the backref.
529 * Some btrees do not do reference counting on their extents. These
530 * include the extent tree and the tree of tree roots. Backrefs for these
531 * trees always have a generation of zero.
533 * When a tree block is created, back references are inserted:
535 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
537 * When a tree block is cow'd in a reference counted root,
538 * new back references are added for all the blocks it points to.
539 * These are of the form (trans->transid will have increased since creation):
541 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
543 * Because the lowest_key_objectid and the level are just hints
544 * they are not used when backrefs are deleted. When a backref is deleted:
546 * if backref was for a tree root:
547 * root_objectid = root->root_key.objectid
549 * root_objectid = btrfs_header_owner(parent)
551 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
553 * Back Reference Key hashing:
555 * Back references have four fields, each 64 bits long. Unfortunately,
556 * This is hashed into a single 64 bit number and placed into the key offset.
557 * The key objectid corresponds to the first byte in the extent, and the
558 * key type is set to BTRFS_EXTENT_REF_KEY
560 int btrfs_insert_extent_backref(struct btrfs_trans_handle
*trans
,
561 struct btrfs_root
*root
,
562 struct btrfs_path
*path
, u64 bytenr
,
563 u64 root_objectid
, u64 ref_generation
,
564 u64 owner
, u64 owner_offset
)
567 struct btrfs_key key
;
568 struct btrfs_extent_ref ref
;
569 struct btrfs_extent_ref
*disk_ref
;
572 btrfs_set_stack_ref_root(&ref
, root_objectid
);
573 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
574 btrfs_set_stack_ref_objectid(&ref
, owner
);
575 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
577 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
580 key
.objectid
= bytenr
;
581 key
.type
= BTRFS_EXTENT_REF_KEY
;
583 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(ref
));
584 while (ret
== -EEXIST
) {
585 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
586 struct btrfs_extent_ref
);
587 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
))
590 btrfs_release_path(root
, path
);
591 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
596 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
597 struct btrfs_extent_ref
);
598 write_extent_buffer(path
->nodes
[0], &ref
, (unsigned long)disk_ref
,
600 btrfs_mark_buffer_dirty(path
->nodes
[0]);
602 btrfs_release_path(root
, path
);
606 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
607 struct btrfs_root
*root
,
608 u64 bytenr
, u64 num_bytes
,
609 u64 root_objectid
, u64 ref_generation
,
610 u64 owner
, u64 owner_offset
)
612 struct btrfs_path
*path
;
614 struct btrfs_key key
;
615 struct extent_buffer
*l
;
616 struct btrfs_extent_item
*item
;
619 WARN_ON(num_bytes
< root
->sectorsize
);
620 path
= btrfs_alloc_path();
624 key
.objectid
= bytenr
;
625 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
626 key
.offset
= num_bytes
;
627 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
636 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
637 refs
= btrfs_extent_refs(l
, item
);
638 btrfs_set_extent_refs(l
, item
, refs
+ 1);
639 btrfs_mark_buffer_dirty(path
->nodes
[0]);
641 btrfs_release_path(root
->fs_info
->extent_root
, path
);
643 ret
= btrfs_insert_extent_backref(trans
, root
->fs_info
->extent_root
,
644 path
, bytenr
, root_objectid
,
645 ref_generation
, owner
, owner_offset
);
647 finish_current_insert(trans
, root
->fs_info
->extent_root
);
648 del_pending_extents(trans
, root
->fs_info
->extent_root
);
650 btrfs_free_path(path
);
654 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
655 struct btrfs_root
*root
)
657 finish_current_insert(trans
, root
->fs_info
->extent_root
);
658 del_pending_extents(trans
, root
->fs_info
->extent_root
);
662 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
663 struct btrfs_root
*root
, u64 bytenr
,
664 u64 num_bytes
, u32
*refs
)
666 struct btrfs_path
*path
;
668 struct btrfs_key key
;
669 struct extent_buffer
*l
;
670 struct btrfs_extent_item
*item
;
672 WARN_ON(num_bytes
< root
->sectorsize
);
673 path
= btrfs_alloc_path();
674 key
.objectid
= bytenr
;
675 key
.offset
= num_bytes
;
676 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
677 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
682 btrfs_print_leaf(root
, path
->nodes
[0]);
683 printk("failed to find block number %llu\n",
684 (unsigned long long)bytenr
);
688 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
689 *refs
= btrfs_extent_refs(l
, item
);
691 btrfs_free_path(path
);
695 u32
btrfs_count_snapshots_in_path(struct btrfs_root
*root
,
696 struct btrfs_path
*count_path
,
699 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
700 struct btrfs_path
*path
;
703 u64 root_objectid
= root
->root_key
.objectid
;
709 struct btrfs_key key
;
710 struct btrfs_key found_key
;
711 struct extent_buffer
*l
;
712 struct btrfs_extent_item
*item
;
713 struct btrfs_extent_ref
*ref_item
;
716 path
= btrfs_alloc_path();
719 bytenr
= first_extent
;
721 bytenr
= count_path
->nodes
[level
]->start
;
724 key
.objectid
= bytenr
;
727 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
728 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
734 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
736 if (found_key
.objectid
!= bytenr
||
737 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
741 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
742 refs
= btrfs_extent_refs(l
, item
);
744 nritems
= btrfs_header_nritems(l
);
745 if (path
->slots
[0] >= nritems
) {
746 ret
= btrfs_next_leaf(extent_root
, path
);
751 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
752 if (found_key
.objectid
!= bytenr
)
754 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
760 ref_item
= btrfs_item_ptr(l
, path
->slots
[0],
761 struct btrfs_extent_ref
);
762 found_objectid
= btrfs_ref_root(l
, ref_item
);
764 if (found_objectid
!= root_objectid
) {
771 if (cur_count
== 0) {
775 if (level
>= 0 && root
->node
== count_path
->nodes
[level
])
778 btrfs_release_path(root
, path
);
782 btrfs_free_path(path
);
785 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
786 struct btrfs_root
*root
, u64 owner_objectid
)
792 struct btrfs_disk_key disk_key
;
794 level
= btrfs_header_level(root
->node
);
795 generation
= trans
->transid
;
796 nritems
= btrfs_header_nritems(root
->node
);
799 btrfs_item_key(root
->node
, &disk_key
, 0);
801 btrfs_node_key(root
->node
, &disk_key
, 0);
802 key_objectid
= btrfs_disk_key_objectid(&disk_key
);
806 return btrfs_inc_extent_ref(trans
, root
, root
->node
->start
,
807 root
->node
->len
, owner_objectid
,
808 generation
, level
, key_objectid
);
811 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
812 struct extent_buffer
*buf
)
816 struct btrfs_key key
;
817 struct btrfs_file_extent_item
*fi
;
826 level
= btrfs_header_level(buf
);
827 nritems
= btrfs_header_nritems(buf
);
828 for (i
= 0; i
< nritems
; i
++) {
831 btrfs_item_key_to_cpu(buf
, &key
, i
);
832 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
834 fi
= btrfs_item_ptr(buf
, i
,
835 struct btrfs_file_extent_item
);
836 if (btrfs_file_extent_type(buf
, fi
) ==
837 BTRFS_FILE_EXTENT_INLINE
)
839 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
840 if (disk_bytenr
== 0)
842 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
,
843 btrfs_file_extent_disk_num_bytes(buf
, fi
),
844 root
->root_key
.objectid
, trans
->transid
,
845 key
.objectid
, key
.offset
);
851 bytenr
= btrfs_node_blockptr(buf
, i
);
852 btrfs_node_key_to_cpu(buf
, &key
, i
);
853 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
,
854 btrfs_level_size(root
, level
- 1),
855 root
->root_key
.objectid
,
857 level
- 1, key
.objectid
);
868 for (i
=0; i
< faili
; i
++) {
871 btrfs_item_key_to_cpu(buf
, &key
, i
);
872 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
874 fi
= btrfs_item_ptr(buf
, i
,
875 struct btrfs_file_extent_item
);
876 if (btrfs_file_extent_type(buf
, fi
) ==
877 BTRFS_FILE_EXTENT_INLINE
)
879 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
880 if (disk_bytenr
== 0)
882 err
= btrfs_free_extent(trans
, root
, disk_bytenr
,
883 btrfs_file_extent_disk_num_bytes(buf
,
887 bytenr
= btrfs_node_blockptr(buf
, i
);
888 err
= btrfs_free_extent(trans
, root
, bytenr
,
889 btrfs_level_size(root
, level
- 1), 0);
897 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
898 struct btrfs_root
*root
,
899 struct btrfs_path
*path
,
900 struct btrfs_block_group_cache
*cache
)
904 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
906 struct extent_buffer
*leaf
;
908 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
913 leaf
= path
->nodes
[0];
914 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
915 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
916 btrfs_mark_buffer_dirty(leaf
);
917 btrfs_release_path(extent_root
, path
);
919 finish_current_insert(trans
, extent_root
);
920 pending_ret
= del_pending_extents(trans
, extent_root
);
929 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
930 struct btrfs_root
*root
)
932 struct extent_io_tree
*block_group_cache
;
933 struct btrfs_block_group_cache
*cache
;
937 struct btrfs_path
*path
;
943 block_group_cache
= &root
->fs_info
->block_group_cache
;
944 path
= btrfs_alloc_path();
949 ret
= find_first_extent_bit(block_group_cache
, last
,
950 &start
, &end
, BLOCK_GROUP_DIRTY
);
955 ret
= get_state_private(block_group_cache
, start
, &ptr
);
958 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
959 err
= write_one_cache_group(trans
, root
,
962 * if we fail to write the cache group, we want
963 * to keep it marked dirty in hopes that a later
970 clear_extent_bits(block_group_cache
, start
, end
,
971 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
973 btrfs_free_path(path
);
977 static struct btrfs_space_info
*__find_space_info(struct btrfs_fs_info
*info
,
980 struct list_head
*head
= &info
->space_info
;
981 struct list_head
*cur
;
982 struct btrfs_space_info
*found
;
983 list_for_each(cur
, head
) {
984 found
= list_entry(cur
, struct btrfs_space_info
, list
);
985 if (found
->flags
== flags
)
992 static int update_space_info(struct btrfs_fs_info
*info
, u64 flags
,
993 u64 total_bytes
, u64 bytes_used
,
994 struct btrfs_space_info
**space_info
)
996 struct btrfs_space_info
*found
;
998 found
= __find_space_info(info
, flags
);
1000 found
->total_bytes
+= total_bytes
;
1001 found
->bytes_used
+= bytes_used
;
1002 WARN_ON(found
->total_bytes
< found
->bytes_used
);
1003 *space_info
= found
;
1006 found
= kmalloc(sizeof(*found
), GFP_NOFS
);
1010 list_add(&found
->list
, &info
->space_info
);
1011 found
->flags
= flags
;
1012 found
->total_bytes
= total_bytes
;
1013 found
->bytes_used
= bytes_used
;
1014 found
->bytes_pinned
= 0;
1016 *space_info
= found
;
1021 static void set_avail_alloc_bits(struct btrfs_fs_info
*fs_info
, u64 flags
)
1023 u64 extra_flags
= flags
& (BTRFS_BLOCK_GROUP_RAID0
|
1024 BTRFS_BLOCK_GROUP_RAID1
|
1025 BTRFS_BLOCK_GROUP_DUP
);
1027 if (flags
& BTRFS_BLOCK_GROUP_DATA
)
1028 fs_info
->avail_data_alloc_bits
|= extra_flags
;
1029 if (flags
& BTRFS_BLOCK_GROUP_METADATA
)
1030 fs_info
->avail_metadata_alloc_bits
|= extra_flags
;
1031 if (flags
& BTRFS_BLOCK_GROUP_SYSTEM
)
1032 fs_info
->avail_system_alloc_bits
|= extra_flags
;
1036 static int do_chunk_alloc(struct btrfs_trans_handle
*trans
,
1037 struct btrfs_root
*extent_root
, u64 alloc_bytes
,
1040 struct btrfs_space_info
*space_info
;
1046 space_info
= __find_space_info(extent_root
->fs_info
, flags
);
1048 ret
= update_space_info(extent_root
->fs_info
, flags
,
1052 BUG_ON(!space_info
);
1054 if (space_info
->full
)
1057 thresh
= div_factor(space_info
->total_bytes
, 7);
1058 if ((space_info
->bytes_used
+ space_info
->bytes_pinned
+ alloc_bytes
) <
1062 ret
= btrfs_alloc_chunk(trans
, extent_root
, &start
, &num_bytes
, flags
);
1063 if (ret
== -ENOSPC
) {
1064 space_info
->full
= 1;
1070 ret
= btrfs_make_block_group(trans
, extent_root
, 0, flags
,
1071 BTRFS_FIRST_CHUNK_TREE_OBJECTID
, start
, num_bytes
);
1076 static int update_block_group(struct btrfs_trans_handle
*trans
,
1077 struct btrfs_root
*root
,
1078 u64 bytenr
, u64 num_bytes
, int alloc
,
1081 struct btrfs_block_group_cache
*cache
;
1082 struct btrfs_fs_info
*info
= root
->fs_info
;
1083 u64 total
= num_bytes
;
1090 cache
= btrfs_lookup_block_group(info
, bytenr
);
1094 byte_in_group
= bytenr
- cache
->key
.objectid
;
1095 WARN_ON(byte_in_group
> cache
->key
.offset
);
1096 start
= cache
->key
.objectid
;
1097 end
= start
+ cache
->key
.offset
- 1;
1098 set_extent_bits(&info
->block_group_cache
, start
, end
,
1099 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
1101 old_val
= btrfs_block_group_used(&cache
->item
);
1102 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1104 old_val
+= num_bytes
;
1105 cache
->space_info
->bytes_used
+= num_bytes
;
1107 old_val
-= num_bytes
;
1108 cache
->space_info
->bytes_used
-= num_bytes
;
1110 set_extent_dirty(&info
->free_space_cache
,
1111 bytenr
, bytenr
+ num_bytes
- 1,
1115 btrfs_set_block_group_used(&cache
->item
, old_val
);
1117 bytenr
+= num_bytes
;
1122 static int update_pinned_extents(struct btrfs_root
*root
,
1123 u64 bytenr
, u64 num
, int pin
)
1126 struct btrfs_block_group_cache
*cache
;
1127 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1130 set_extent_dirty(&fs_info
->pinned_extents
,
1131 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1133 clear_extent_dirty(&fs_info
->pinned_extents
,
1134 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1137 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1139 len
= min(num
, cache
->key
.offset
-
1140 (bytenr
- cache
->key
.objectid
));
1142 cache
->pinned
+= len
;
1143 cache
->space_info
->bytes_pinned
+= len
;
1144 fs_info
->total_pinned
+= len
;
1146 cache
->pinned
-= len
;
1147 cache
->space_info
->bytes_pinned
-= len
;
1148 fs_info
->total_pinned
-= len
;
1156 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
1161 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1165 ret
= find_first_extent_bit(pinned_extents
, last
,
1166 &start
, &end
, EXTENT_DIRTY
);
1169 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1175 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1176 struct btrfs_root
*root
,
1177 struct extent_io_tree
*unpin
)
1182 struct extent_io_tree
*free_space_cache
;
1183 free_space_cache
= &root
->fs_info
->free_space_cache
;
1186 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1190 update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1191 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1192 set_extent_dirty(free_space_cache
, start
, end
, GFP_NOFS
);
1197 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1198 struct btrfs_root
*extent_root
)
1202 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1203 struct extent_buffer
*eb
;
1204 struct btrfs_path
*path
;
1205 struct btrfs_key ins
;
1206 struct btrfs_disk_key first
;
1207 struct btrfs_extent_item extent_item
;
1212 btrfs_set_stack_extent_refs(&extent_item
, 1);
1213 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
1214 path
= btrfs_alloc_path();
1217 ret
= find_first_extent_bit(&info
->extent_ins
, 0, &start
,
1218 &end
, EXTENT_LOCKED
);
1222 ins
.objectid
= start
;
1223 ins
.offset
= end
+ 1 - start
;
1224 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
1225 &extent_item
, sizeof(extent_item
));
1226 clear_extent_bits(&info
->extent_ins
, start
, end
, EXTENT_LOCKED
,
1228 eb
= read_tree_block(extent_root
, ins
.objectid
, ins
.offset
,
1230 level
= btrfs_header_level(eb
);
1232 btrfs_item_key(eb
, &first
, 0);
1234 btrfs_node_key(eb
, &first
, 0);
1236 err
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1237 start
, extent_root
->root_key
.objectid
,
1239 btrfs_disk_key_objectid(&first
));
1241 free_extent_buffer(eb
);
1243 btrfs_free_path(path
);
1247 static int pin_down_bytes(struct btrfs_root
*root
, u64 bytenr
, u32 num_bytes
,
1251 struct extent_buffer
*buf
;
1254 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1256 if (btrfs_buffer_uptodate(buf
)) {
1258 root
->fs_info
->running_transaction
->transid
;
1259 if (btrfs_header_generation(buf
) ==
1260 transid
&& !btrfs_header_flag(buf
,
1261 BTRFS_HEADER_FLAG_WRITTEN
)) {
1262 free_extent_buffer(buf
);
1266 free_extent_buffer(buf
);
1268 update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1270 set_extent_bits(&root
->fs_info
->pending_del
,
1271 bytenr
, bytenr
+ num_bytes
- 1,
1272 EXTENT_LOCKED
, GFP_NOFS
);
1279 * remove an extent from the root, returns 0 on success
1281 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1282 *root
, u64 bytenr
, u64 num_bytes
,
1283 u64 root_objectid
, u64 ref_generation
,
1284 u64 owner_objectid
, u64 owner_offset
, int pin
,
1287 struct btrfs_path
*path
;
1288 struct btrfs_key key
;
1289 struct btrfs_fs_info
*info
= root
->fs_info
;
1290 struct btrfs_extent_ops
*ops
= info
->extent_ops
;
1291 struct btrfs_root
*extent_root
= info
->extent_root
;
1292 struct extent_buffer
*leaf
;
1294 int extent_slot
= 0;
1295 int found_extent
= 0;
1297 struct btrfs_extent_item
*ei
;
1300 key
.objectid
= bytenr
;
1301 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1302 key
.offset
= num_bytes
;
1304 path
= btrfs_alloc_path();
1308 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1309 bytenr
, root_objectid
,
1311 owner_objectid
, owner_offset
, 1);
1313 struct btrfs_key found_key
;
1314 extent_slot
= path
->slots
[0];
1315 while(extent_slot
> 0) {
1317 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1319 if (found_key
.objectid
!= bytenr
)
1321 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1322 found_key
.offset
== num_bytes
) {
1326 if (path
->slots
[0] - extent_slot
> 5)
1330 ret
= btrfs_del_item(trans
, extent_root
, path
);
1332 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1334 printk("Unable to find ref byte nr %llu root %llu "
1335 " gen %llu owner %llu offset %llu\n",
1336 (unsigned long long)bytenr
,
1337 (unsigned long long)root_objectid
,
1338 (unsigned long long)ref_generation
,
1339 (unsigned long long)owner_objectid
,
1340 (unsigned long long)owner_offset
);
1342 if (!found_extent
) {
1343 btrfs_release_path(extent_root
, path
);
1344 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
1348 extent_slot
= path
->slots
[0];
1351 leaf
= path
->nodes
[0];
1352 ei
= btrfs_item_ptr(leaf
, extent_slot
,
1353 struct btrfs_extent_item
);
1354 refs
= btrfs_extent_refs(leaf
, ei
);
1357 btrfs_set_extent_refs(leaf
, ei
, refs
);
1359 btrfs_mark_buffer_dirty(leaf
);
1361 if (refs
== 0 && found_extent
&& path
->slots
[0] == extent_slot
+ 1) {
1362 /* if the back ref and the extent are next to each other
1363 * they get deleted below in one shot
1365 path
->slots
[0] = extent_slot
;
1367 } else if (found_extent
) {
1368 /* otherwise delete the extent back ref */
1369 ret
= btrfs_del_item(trans
, extent_root
, path
);
1371 /* if refs are 0, we need to setup the path for deletion */
1373 btrfs_release_path(extent_root
, path
);
1374 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
1387 ret
= pin_down_bytes(root
, bytenr
, num_bytes
, 0);
1393 /* block accounting for super block */
1394 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1395 btrfs_set_super_bytes_used(&info
->super_copy
,
1396 super_used
- num_bytes
);
1398 /* block accounting for root item */
1399 root_used
= btrfs_root_used(&root
->root_item
);
1400 btrfs_set_root_used(&root
->root_item
,
1401 root_used
- num_bytes
);
1402 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
1407 if (ops
&& ops
->free_extent
)
1408 ops
->free_extent(root
, bytenr
, num_bytes
);
1410 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1414 btrfs_free_path(path
);
1415 finish_current_insert(trans
, extent_root
);
1420 * find all the blocks marked as pending in the radix tree and remove
1421 * them from the extent map
1423 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1424 btrfs_root
*extent_root
)
1430 struct extent_io_tree
*pending_del
;
1431 struct extent_io_tree
*pinned_extents
;
1433 pending_del
= &extent_root
->fs_info
->pending_del
;
1434 pinned_extents
= &extent_root
->fs_info
->pinned_extents
;
1437 ret
= find_first_extent_bit(pending_del
, 0, &start
, &end
,
1441 update_pinned_extents(extent_root
, start
, end
+ 1 - start
, 1);
1442 clear_extent_bits(pending_del
, start
, end
, EXTENT_LOCKED
,
1444 ret
= __free_extent(trans
, extent_root
,
1445 start
, end
+ 1 - start
,
1446 extent_root
->root_key
.objectid
,
1455 * remove an extent from the root, returns 0 on success
1457 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1458 *root
, u64 bytenr
, u64 num_bytes
,
1459 u64 root_objectid
, u64 ref_generation
,
1460 u64 owner_objectid
, u64 owner_offset
, int pin
)
1462 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1466 WARN_ON(num_bytes
< root
->sectorsize
);
1467 if (!root
->ref_cows
)
1470 if (root
== extent_root
) {
1471 pin_down_bytes(root
, bytenr
, num_bytes
, 1);
1474 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, root_objectid
,
1475 ref_generation
, owner_objectid
, owner_offset
,
1477 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
1478 return ret
? ret
: pending_ret
;
1481 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
1483 u64 mask
= ((u64
)root
->stripesize
- 1);
1484 u64 ret
= (val
+ mask
) & ~mask
;
1489 * walks the btree of allocated extents and find a hole of a given size.
1490 * The key ins is changed to record the hole:
1491 * ins->objectid == block start
1492 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1493 * ins->offset == number of blocks
1494 * Any available blocks before search_start are skipped.
1496 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
1497 struct btrfs_root
*orig_root
,
1498 u64 num_bytes
, u64 empty_size
,
1499 u64 search_start
, u64 search_end
,
1500 u64 hint_byte
, struct btrfs_key
*ins
,
1501 u64 exclude_start
, u64 exclude_nr
,
1505 u64 orig_search_start
= search_start
;
1506 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
1507 struct btrfs_fs_info
*info
= root
->fs_info
;
1508 u64 total_needed
= num_bytes
;
1509 struct btrfs_block_group_cache
*block_group
;
1513 WARN_ON(num_bytes
< root
->sectorsize
);
1514 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1516 if (search_end
== (u64
)-1)
1517 search_end
= btrfs_super_total_bytes(&info
->super_copy
);
1520 block_group
= btrfs_lookup_block_group(info
, hint_byte
);
1522 hint_byte
= search_start
;
1523 block_group
= btrfs_find_block_group(root
, block_group
,
1524 hint_byte
, data
, 1);
1526 block_group
= btrfs_find_block_group(root
,
1528 search_start
, data
, 1);
1531 total_needed
+= empty_size
;
1535 block_group
= btrfs_lookup_block_group(info
, search_start
);
1537 block_group
= btrfs_lookup_block_group(info
,
1540 ret
= find_search_start(root
, &block_group
, &search_start
,
1541 total_needed
, data
);
1545 search_start
= stripe_align(root
, search_start
);
1546 ins
->objectid
= search_start
;
1547 ins
->offset
= num_bytes
;
1549 if (ins
->objectid
+ num_bytes
>= search_end
)
1552 if (ins
->objectid
+ num_bytes
>
1553 block_group
->key
.objectid
+ block_group
->key
.offset
) {
1554 search_start
= block_group
->key
.objectid
+
1555 block_group
->key
.offset
;
1559 if (test_range_bit(&info
->extent_ins
, ins
->objectid
,
1560 ins
->objectid
+ num_bytes
-1, EXTENT_LOCKED
, 0)) {
1561 search_start
= ins
->objectid
+ num_bytes
;
1565 if (test_range_bit(&info
->pinned_extents
, ins
->objectid
,
1566 ins
->objectid
+ num_bytes
-1, EXTENT_DIRTY
, 0)) {
1567 search_start
= ins
->objectid
+ num_bytes
;
1571 if (exclude_nr
> 0 && (ins
->objectid
+ num_bytes
> exclude_start
&&
1572 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1573 search_start
= exclude_start
+ exclude_nr
;
1577 if (!(data
& BTRFS_BLOCK_GROUP_DATA
)) {
1578 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1580 trans
->block_group
= block_group
;
1582 ins
->offset
= num_bytes
;
1586 if (search_start
+ num_bytes
>= search_end
) {
1588 search_start
= orig_search_start
;
1595 total_needed
-= empty_size
;
1600 block_group
= btrfs_lookup_block_group(info
, search_start
);
1602 block_group
= btrfs_find_block_group(root
, block_group
,
1603 search_start
, data
, 0);
1610 * finds a free extent and does all the dirty work required for allocation
1611 * returns the key for the extent through ins, and a tree buffer for
1612 * the first block of the extent through buf.
1614 * returns 0 if everything worked, non-zero otherwise.
1616 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1617 struct btrfs_root
*root
,
1618 u64 num_bytes
, u64 root_objectid
, u64 ref_generation
,
1619 u64 owner
, u64 owner_offset
,
1620 u64 empty_size
, u64 hint_byte
,
1621 u64 search_end
, struct btrfs_key
*ins
, int data
)
1625 u64 super_used
, root_used
;
1626 u64 search_start
= 0;
1629 struct btrfs_fs_info
*info
= root
->fs_info
;
1630 struct btrfs_root
*extent_root
= info
->extent_root
;
1631 struct btrfs_path
*path
;
1632 struct btrfs_extent_item
*extent_item
;
1633 struct btrfs_extent_ref
*ref
;
1634 struct btrfs_key keys
[2];
1636 if (info
->extent_ops
) {
1637 struct btrfs_extent_ops
*ops
= info
->extent_ops
;
1638 ret
= ops
->alloc_extent(root
, num_bytes
, hint_byte
, ins
);
1644 alloc_profile
= info
->avail_data_alloc_bits
&
1645 info
->data_alloc_profile
;
1646 data
= BTRFS_BLOCK_GROUP_DATA
| alloc_profile
;
1647 } else if ((info
->system_allocs
> 0 || root
== info
->chunk_root
) &&
1648 info
->system_allocs
>= 0) {
1649 alloc_profile
= info
->avail_system_alloc_bits
&
1650 info
->system_alloc_profile
;
1651 data
= BTRFS_BLOCK_GROUP_SYSTEM
| alloc_profile
;
1653 alloc_profile
= info
->avail_metadata_alloc_bits
&
1654 info
->metadata_alloc_profile
;
1655 data
= BTRFS_BLOCK_GROUP_METADATA
| alloc_profile
;
1658 if (root
->ref_cows
) {
1659 if (!(data
& BTRFS_BLOCK_GROUP_METADATA
)) {
1660 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1662 BTRFS_BLOCK_GROUP_METADATA
);
1665 ret
= do_chunk_alloc(trans
, root
->fs_info
->extent_root
,
1666 num_bytes
+ 2 * 1024 * 1024, data
);
1670 WARN_ON(num_bytes
< root
->sectorsize
);
1671 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
1672 search_start
, search_end
, hint_byte
, ins
,
1673 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 btrfs_node_ptr_generation(node
, i
));
1898 last
= bytenr
+ blocksize
;
1900 mutex_lock(&root
->fs_info
->fs_mutex
);
1907 * helper function for drop_snapshot, this walks down the tree dropping ref
1908 * counts as it goes.
1910 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
1911 struct btrfs_root
*root
,
1912 struct btrfs_path
*path
, int *level
)
1918 struct extent_buffer
*next
;
1919 struct extent_buffer
*cur
;
1920 struct extent_buffer
*parent
;
1925 WARN_ON(*level
< 0);
1926 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1927 ret
= lookup_extent_ref(trans
, root
,
1928 path
->nodes
[*level
]->start
,
1929 path
->nodes
[*level
]->len
, &refs
);
1935 * walk down to the last node level and free all the leaves
1937 while(*level
>= 0) {
1938 WARN_ON(*level
< 0);
1939 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1940 cur
= path
->nodes
[*level
];
1942 if (btrfs_header_level(cur
) != *level
)
1945 if (path
->slots
[*level
] >=
1946 btrfs_header_nritems(cur
))
1949 ret
= drop_leaf_ref(trans
, root
, cur
);
1953 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1954 ptr_gen
= btrfs_node_ptr_generation(cur
, path
->slots
[*level
]);
1955 blocksize
= btrfs_level_size(root
, *level
- 1);
1956 ret
= lookup_extent_ref(trans
, root
, bytenr
, blocksize
, &refs
);
1959 parent
= path
->nodes
[*level
];
1960 root_owner
= btrfs_header_owner(parent
);
1961 root_gen
= btrfs_header_generation(parent
);
1962 path
->slots
[*level
]++;
1963 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1964 blocksize
, root_owner
,
1969 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
1970 if (!next
|| !btrfs_buffer_uptodate(next
)) {
1971 free_extent_buffer(next
);
1972 reada_walk_down(root
, cur
, path
->slots
[*level
]);
1973 mutex_unlock(&root
->fs_info
->fs_mutex
);
1974 next
= read_tree_block(root
, bytenr
, blocksize
,
1976 mutex_lock(&root
->fs_info
->fs_mutex
);
1978 /* we dropped the lock, check one more time */
1979 ret
= lookup_extent_ref(trans
, root
, bytenr
,
1983 parent
= path
->nodes
[*level
];
1984 root_owner
= btrfs_header_owner(parent
);
1985 root_gen
= btrfs_header_generation(parent
);
1987 path
->slots
[*level
]++;
1988 free_extent_buffer(next
);
1989 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1997 WARN_ON(*level
<= 0);
1998 if (path
->nodes
[*level
-1])
1999 free_extent_buffer(path
->nodes
[*level
-1]);
2000 path
->nodes
[*level
-1] = next
;
2001 *level
= btrfs_header_level(next
);
2002 path
->slots
[*level
] = 0;
2005 WARN_ON(*level
< 0);
2006 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
2008 if (path
->nodes
[*level
] == root
->node
) {
2009 root_owner
= root
->root_key
.objectid
;
2010 parent
= path
->nodes
[*level
];
2012 parent
= path
->nodes
[*level
+ 1];
2013 root_owner
= btrfs_header_owner(parent
);
2016 root_gen
= btrfs_header_generation(parent
);
2017 ret
= btrfs_free_extent(trans
, root
, path
->nodes
[*level
]->start
,
2018 path
->nodes
[*level
]->len
,
2019 root_owner
, root_gen
, 0, 0, 1);
2020 free_extent_buffer(path
->nodes
[*level
]);
2021 path
->nodes
[*level
] = NULL
;
2028 * helper for dropping snapshots. This walks back up the tree in the path
2029 * to find the first node higher up where we haven't yet gone through
2032 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
2033 struct btrfs_root
*root
,
2034 struct btrfs_path
*path
, int *level
)
2038 struct btrfs_root_item
*root_item
= &root
->root_item
;
2043 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
2044 slot
= path
->slots
[i
];
2045 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
2046 struct extent_buffer
*node
;
2047 struct btrfs_disk_key disk_key
;
2048 node
= path
->nodes
[i
];
2051 WARN_ON(*level
== 0);
2052 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
2053 memcpy(&root_item
->drop_progress
,
2054 &disk_key
, sizeof(disk_key
));
2055 root_item
->drop_level
= i
;
2058 if (path
->nodes
[*level
] == root
->node
) {
2059 root_owner
= root
->root_key
.objectid
;
2061 btrfs_header_generation(path
->nodes
[*level
]);
2063 struct extent_buffer
*node
;
2064 node
= path
->nodes
[*level
+ 1];
2065 root_owner
= btrfs_header_owner(node
);
2066 root_gen
= btrfs_header_generation(node
);
2068 ret
= btrfs_free_extent(trans
, root
,
2069 path
->nodes
[*level
]->start
,
2070 path
->nodes
[*level
]->len
,
2071 root_owner
, root_gen
, 0, 0, 1);
2073 free_extent_buffer(path
->nodes
[*level
]);
2074 path
->nodes
[*level
] = NULL
;
2082 * drop the reference count on the tree rooted at 'snap'. This traverses
2083 * the tree freeing any blocks that have a ref count of zero after being
2086 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
2092 struct btrfs_path
*path
;
2095 struct btrfs_root_item
*root_item
= &root
->root_item
;
2097 path
= btrfs_alloc_path();
2100 level
= btrfs_header_level(root
->node
);
2102 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2103 path
->nodes
[level
] = root
->node
;
2104 extent_buffer_get(root
->node
);
2105 path
->slots
[level
] = 0;
2107 struct btrfs_key key
;
2108 struct btrfs_disk_key found_key
;
2109 struct extent_buffer
*node
;
2111 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2112 level
= root_item
->drop_level
;
2113 path
->lowest_level
= level
;
2114 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2119 node
= path
->nodes
[level
];
2120 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
2121 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
2122 sizeof(found_key
)));
2125 wret
= walk_down_tree(trans
, root
, path
, &level
);
2131 wret
= walk_up_tree(trans
, root
, path
, &level
);
2141 for (i
= 0; i
<= orig_level
; i
++) {
2142 if (path
->nodes
[i
]) {
2143 free_extent_buffer(path
->nodes
[i
]);
2144 path
->nodes
[i
] = NULL
;
2148 btrfs_free_path(path
);
2152 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
2159 ret
= find_first_extent_bit(&info
->block_group_cache
, 0,
2160 &start
, &end
, (unsigned int)-1);
2163 ret
= get_state_private(&info
->block_group_cache
, start
, &ptr
);
2165 kfree((void *)(unsigned long)ptr
);
2166 clear_extent_bits(&info
->block_group_cache
, start
,
2167 end
, (unsigned int)-1, GFP_NOFS
);
2170 ret
= find_first_extent_bit(&info
->free_space_cache
, 0,
2171 &start
, &end
, EXTENT_DIRTY
);
2174 clear_extent_dirty(&info
->free_space_cache
, start
,
2180 int find_first_block_group(struct btrfs_root
*root
, struct btrfs_path
*path
,
2181 struct btrfs_key
*key
)
2184 struct btrfs_key found_key
;
2185 struct extent_buffer
*leaf
;
2188 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
2192 slot
= path
->slots
[0];
2193 leaf
= path
->nodes
[0];
2194 if (slot
>= btrfs_header_nritems(leaf
)) {
2195 ret
= btrfs_next_leaf(root
, path
);
2202 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2204 if (found_key
.objectid
>= key
->objectid
&&
2205 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
2214 int btrfs_read_block_groups(struct btrfs_root
*root
)
2216 struct btrfs_path
*path
;
2219 struct btrfs_block_group_cache
*cache
;
2220 struct btrfs_fs_info
*info
= root
->fs_info
;
2221 struct btrfs_space_info
*space_info
;
2222 struct extent_io_tree
*block_group_cache
;
2223 struct btrfs_key key
;
2224 struct btrfs_key found_key
;
2225 struct extent_buffer
*leaf
;
2227 block_group_cache
= &info
->block_group_cache
;
2229 root
= info
->extent_root
;
2232 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2233 path
= btrfs_alloc_path();
2238 ret
= find_first_block_group(root
, path
, &key
);
2246 leaf
= path
->nodes
[0];
2247 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2248 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2254 read_extent_buffer(leaf
, &cache
->item
,
2255 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2256 sizeof(cache
->item
));
2257 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
2260 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
2261 btrfs_release_path(root
, path
);
2262 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
2264 if (cache
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2265 bit
= BLOCK_GROUP_DATA
;
2266 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2267 bit
= BLOCK_GROUP_SYSTEM
;
2268 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2269 bit
= BLOCK_GROUP_METADATA
;
2271 set_avail_alloc_bits(info
, cache
->flags
);
2273 ret
= update_space_info(info
, cache
->flags
, found_key
.offset
,
2274 btrfs_block_group_used(&cache
->item
),
2277 cache
->space_info
= space_info
;
2279 /* use EXTENT_LOCKED to prevent merging */
2280 set_extent_bits(block_group_cache
, found_key
.objectid
,
2281 found_key
.objectid
+ found_key
.offset
- 1,
2282 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2283 set_state_private(block_group_cache
, found_key
.objectid
,
2284 (unsigned long)cache
);
2287 btrfs_super_total_bytes(&info
->super_copy
))
2292 btrfs_free_path(path
);
2296 int btrfs_make_block_group(struct btrfs_trans_handle
*trans
,
2297 struct btrfs_root
*root
, u64 bytes_used
,
2298 u64 type
, u64 chunk_objectid
, u64 chunk_offset
,
2303 struct btrfs_root
*extent_root
;
2304 struct btrfs_block_group_cache
*cache
;
2305 struct extent_io_tree
*block_group_cache
;
2307 extent_root
= root
->fs_info
->extent_root
;
2308 block_group_cache
= &root
->fs_info
->block_group_cache
;
2310 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
2312 cache
->key
.objectid
= chunk_offset
;
2313 cache
->key
.offset
= size
;
2315 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2316 btrfs_set_block_group_used(&cache
->item
, bytes_used
);
2317 btrfs_set_block_group_chunk_objectid(&cache
->item
, chunk_objectid
);
2318 cache
->flags
= type
;
2319 btrfs_set_block_group_flags(&cache
->item
, type
);
2321 ret
= update_space_info(root
->fs_info
, cache
->flags
, size
, bytes_used
,
2322 &cache
->space_info
);
2325 bit
= block_group_state_bits(type
);
2326 set_extent_bits(block_group_cache
, chunk_offset
,
2327 chunk_offset
+ size
- 1,
2328 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2330 set_state_private(block_group_cache
, chunk_offset
,
2331 (unsigned long)cache
);
2332 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
2333 sizeof(cache
->item
));
2336 finish_current_insert(trans
, extent_root
);
2337 ret
= del_pending_extents(trans
, extent_root
);
2339 set_avail_alloc_bits(extent_root
->fs_info
, type
);
2344 * This is for converter use only.
2346 * In that case, we don't know where are free blocks located.
2347 * Therefore all block group cache entries must be setup properly
2348 * before doing any block allocation.
2350 int btrfs_make_block_groups(struct btrfs_trans_handle
*trans
,
2351 struct btrfs_root
*root
)
2359 u64 total_metadata
= 0;
2363 struct btrfs_root
*extent_root
;
2364 struct btrfs_block_group_cache
*cache
;
2365 struct extent_io_tree
*block_group_cache
;
2367 extent_root
= root
->fs_info
->extent_root
;
2368 block_group_cache
= &root
->fs_info
->block_group_cache
;
2369 chunk_objectid
= BTRFS_FIRST_CHUNK_TREE_OBJECTID
;
2370 total_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
2371 group_align
= 64 * root
->sectorsize
;
2374 while (cur_start
< total_bytes
) {
2375 group_size
= total_bytes
/ 12;
2376 group_size
= min_t(u64
, group_size
, total_bytes
- cur_start
);
2377 if (cur_start
== 0) {
2378 bit
= BLOCK_GROUP_SYSTEM
;
2379 group_type
= BTRFS_BLOCK_GROUP_SYSTEM
;
2381 group_size
&= ~(group_align
- 1);
2382 group_size
= max_t(u64
, group_size
, 32 * 1024 * 1024);
2383 group_size
= min_t(u64
, group_size
, 128 * 1024 * 1024);
2385 group_size
&= ~(group_align
- 1);
2386 if (total_data
>= total_metadata
* 2) {
2387 group_type
= BTRFS_BLOCK_GROUP_METADATA
;
2388 group_size
= min_t(u64
, group_size
,
2389 1ULL * 1024 * 1024 * 1024);
2390 total_metadata
+= group_size
;
2392 group_type
= BTRFS_BLOCK_GROUP_DATA
;
2393 group_size
= min_t(u64
, group_size
,
2394 5ULL * 1024 * 1024 * 1024);
2395 total_data
+= group_size
;
2397 if ((total_bytes
- cur_start
) * 4 < group_size
* 5)
2398 group_size
= total_bytes
- cur_start
;
2401 cache
= kzalloc(sizeof(*cache
), GFP_NOFS
);
2404 cache
->key
.objectid
= cur_start
;
2405 cache
->key
.offset
= group_size
;
2406 btrfs_set_key_type(&cache
->key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2408 btrfs_set_block_group_used(&cache
->item
, 0);
2409 btrfs_set_block_group_chunk_objectid(&cache
->item
,
2411 btrfs_set_block_group_flags(&cache
->item
, group_type
);
2413 cache
->flags
= group_type
;
2415 ret
= update_space_info(root
->fs_info
, group_type
, group_size
,
2416 0, &cache
->space_info
);
2418 set_avail_alloc_bits(extent_root
->fs_info
, group_type
);
2420 set_extent_bits(block_group_cache
, cur_start
,
2421 cur_start
+ group_size
- 1,
2422 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2423 set_state_private(block_group_cache
, cur_start
,
2424 (unsigned long)cache
);
2425 cur_start
+= group_size
;
2427 /* then insert all the items */
2429 while(cur_start
< total_bytes
) {
2430 cache
= btrfs_lookup_block_group(root
->fs_info
, cur_start
);
2433 ret
= btrfs_insert_item(trans
, extent_root
, &cache
->key
, &cache
->item
,
2434 sizeof(cache
->item
));
2437 finish_current_insert(trans
, extent_root
);
2438 ret
= del_pending_extents(trans
, extent_root
);
2441 cur_start
= cache
->key
.objectid
+ cache
->key
.offset
;
2446 u64
btrfs_hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
2447 u64 owner
, u64 owner_offset
)
2449 return hash_extent_ref(root_objectid
, ref_generation
,
2450 owner
, owner_offset
);
2453 int btrfs_update_block_group(struct btrfs_trans_handle
*trans
,
2454 struct btrfs_root
*root
,
2455 u64 bytenr
, u64 num_bytes
, int alloc
,
2458 return update_block_group(trans
, root
, bytenr
, num_bytes
,