2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
20 #include <linux/crc32c.h>
21 #include <linux/pagemap.h>
25 #include "print-tree.h"
26 #include "transaction.h"
29 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
30 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
31 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
33 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
35 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
36 btrfs_root
*extent_root
);
37 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
38 btrfs_root
*extent_root
);
41 static int cache_block_group(struct btrfs_root
*root
,
42 struct btrfs_block_group_cache
*block_group
)
44 struct btrfs_path
*path
;
47 struct extent_buffer
*leaf
;
48 struct extent_io_tree
*free_space_cache
;
58 root
= root
->fs_info
->extent_root
;
59 free_space_cache
= &root
->fs_info
->free_space_cache
;
61 if (block_group
->cached
)
64 path
= btrfs_alloc_path();
69 first_free
= block_group
->key
.objectid
;
70 key
.objectid
= block_group
->key
.objectid
;
72 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
73 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
76 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
80 leaf
= path
->nodes
[0];
81 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
82 if (key
.objectid
+ key
.offset
> first_free
)
83 first_free
= key
.objectid
+ key
.offset
;
86 leaf
= path
->nodes
[0];
87 slot
= path
->slots
[0];
88 if (slot
>= btrfs_header_nritems(leaf
)) {
89 ret
= btrfs_next_leaf(root
, path
);
98 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
99 if (key
.objectid
< block_group
->key
.objectid
) {
102 if (key
.objectid
>= block_group
->key
.objectid
+
103 block_group
->key
.offset
) {
107 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
112 if (key
.objectid
> last
) {
113 hole_size
= key
.objectid
- last
;
114 set_extent_dirty(free_space_cache
, last
,
115 last
+ hole_size
- 1,
118 last
= key
.objectid
+ key
.offset
;
126 if (block_group
->key
.objectid
+
127 block_group
->key
.offset
> last
) {
128 hole_size
= block_group
->key
.objectid
+
129 block_group
->key
.offset
- last
;
130 set_extent_dirty(free_space_cache
, last
,
131 last
+ hole_size
- 1, GFP_NOFS
);
133 block_group
->cached
= 1;
135 btrfs_free_path(path
);
139 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
143 struct extent_io_tree
*block_group_cache
;
144 struct btrfs_block_group_cache
*block_group
= NULL
;
150 block_group_cache
= &info
->block_group_cache
;
151 ret
= find_first_extent_bit(block_group_cache
,
152 bytenr
, &start
, &end
,
153 BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
|
158 ret
= get_state_private(block_group_cache
, start
, &ptr
);
162 block_group
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
163 if (block_group
->key
.objectid
<= bytenr
&& bytenr
<
164 block_group
->key
.objectid
+ block_group
->key
.offset
)
169 static int block_group_bits(struct btrfs_block_group_cache
*cache
, u64 bits
)
171 if ((bits
& BLOCK_GROUP_DATA
) &&
172 (cache
->flags
& BTRFS_BLOCK_GROUP_DATA
))
174 if ((bits
& BLOCK_GROUP_METADATA
) &&
175 (cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
))
177 if ((bits
& BLOCK_GROUP_SYSTEM
) &&
178 (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
))
183 static int noinline
find_search_start(struct btrfs_root
*root
,
184 struct btrfs_block_group_cache
**cache_ret
,
185 u64
*start_ret
, int num
, int data
)
188 struct btrfs_block_group_cache
*cache
= *cache_ret
;
189 struct extent_io_tree
*free_space_cache
;
195 u64 search_start
= *start_ret
;
200 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
201 free_space_cache
= &root
->fs_info
->free_space_cache
;
204 ret
= cache_block_group(root
, cache
);
208 last
= max(search_start
, cache
->key
.objectid
);
209 if (!block_group_bits(cache
, data
)) {
214 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
215 last
, &start
, &end
, EXTENT_DIRTY
);
222 start
= max(last
, start
);
224 if (last
- start
< num
) {
225 if (last
== cache
->key
.objectid
+ cache
->key
.offset
)
229 if (start
+ num
> cache
->key
.objectid
+ cache
->key
.offset
)
231 if (start
+ num
> total_fs_bytes
)
237 cache
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
239 printk("Unable to find block group for %Lu\n", search_start
);
245 last
= cache
->key
.objectid
+ cache
->key
.offset
;
247 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
248 if (!cache
|| cache
->key
.objectid
>= total_fs_bytes
) {
257 if (cache_miss
&& !cache
->cached
) {
258 cache_block_group(root
, cache
);
260 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
262 cache
= btrfs_find_block_group(root
, cache
, last
, data
, 0);
270 static u64
div_factor(u64 num
, int factor
)
279 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
280 struct btrfs_block_group_cache
281 *hint
, u64 search_start
,
284 struct btrfs_block_group_cache
*cache
;
285 struct extent_io_tree
*block_group_cache
;
286 struct btrfs_block_group_cache
*found_group
= NULL
;
287 struct btrfs_fs_info
*info
= root
->fs_info
;
301 block_group_cache
= &info
->block_group_cache
;
302 total_fs_bytes
= btrfs_super_total_bytes(&root
->fs_info
->super_copy
);
309 if (search_start
&& search_start
< total_fs_bytes
) {
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 hint
->key
.objectid
< total_fs_bytes
) {
322 used
= btrfs_block_group_used(&hint
->item
);
323 if (used
+ hint
->pinned
<
324 div_factor(hint
->key
.offset
, factor
)) {
327 last
= hint
->key
.objectid
+ hint
->key
.offset
;
331 hint_last
= max(hint
->key
.objectid
, search_start
);
333 hint_last
= search_start
;
335 if (hint_last
>= total_fs_bytes
)
336 hint_last
= search_start
;
341 ret
= find_first_extent_bit(block_group_cache
, last
,
346 ret
= get_state_private(block_group_cache
, start
, &ptr
);
350 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
351 last
= cache
->key
.objectid
+ cache
->key
.offset
;
352 used
= btrfs_block_group_used(&cache
->item
);
354 if (cache
->key
.objectid
> total_fs_bytes
)
358 free_check
= cache
->key
.offset
;
360 free_check
= div_factor(cache
->key
.offset
, factor
);
361 if (used
+ cache
->pinned
< free_check
) {
376 static u64
hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
377 u64 owner
, u64 owner_offset
)
379 u32 high_crc
= ~(u32
)0;
380 u32 low_crc
= ~(u32
)0;
383 lenum
= cpu_to_le64(root_objectid
);
384 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
385 lenum
= cpu_to_le64(ref_generation
);
386 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
387 if (owner
>= BTRFS_FIRST_FREE_OBJECTID
) {
388 lenum
= cpu_to_le64(owner
);
389 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
390 lenum
= cpu_to_le64(owner_offset
);
391 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
393 return ((u64
)high_crc
<< 32) | (u64
)low_crc
;
396 static int match_extent_ref(struct extent_buffer
*leaf
,
397 struct btrfs_extent_ref
*disk_ref
,
398 struct btrfs_extent_ref
*cpu_ref
)
403 if (cpu_ref
->objectid
)
404 len
= sizeof(*cpu_ref
);
406 len
= 2 * sizeof(u64
);
407 ret
= memcmp_extent_buffer(leaf
, cpu_ref
, (unsigned long)disk_ref
,
412 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
413 struct btrfs_root
*root
,
414 struct btrfs_path
*path
, u64 bytenr
,
416 u64 ref_generation
, u64 owner
,
417 u64 owner_offset
, int del
)
420 struct btrfs_key key
;
421 struct btrfs_key found_key
;
422 struct btrfs_extent_ref ref
;
423 struct extent_buffer
*leaf
;
424 struct btrfs_extent_ref
*disk_ref
;
428 btrfs_set_stack_ref_root(&ref
, root_objectid
);
429 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
430 btrfs_set_stack_ref_objectid(&ref
, owner
);
431 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
433 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
436 key
.objectid
= bytenr
;
437 key
.type
= BTRFS_EXTENT_REF_KEY
;
440 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
444 leaf
= path
->nodes
[0];
446 u32 nritems
= btrfs_header_nritems(leaf
);
447 if (path
->slots
[0] >= nritems
) {
448 ret2
= btrfs_next_leaf(root
, path
);
451 leaf
= path
->nodes
[0];
453 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
454 if (found_key
.objectid
!= bytenr
||
455 found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
457 key
.offset
= found_key
.offset
;
459 btrfs_release_path(root
, path
);
463 disk_ref
= btrfs_item_ptr(path
->nodes
[0],
465 struct btrfs_extent_ref
);
466 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
)) {
470 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
471 key
.offset
= found_key
.offset
+ 1;
472 btrfs_release_path(root
, path
);
479 * Back reference rules. Back refs have three main goals:
481 * 1) differentiate between all holders of references to an extent so that
482 * when a reference is dropped we can make sure it was a valid reference
483 * before freeing the extent.
485 * 2) Provide enough information to quickly find the holders of an extent
486 * if we notice a given block is corrupted or bad.
488 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
489 * maintenance. This is actually the same as #2, but with a slightly
490 * different use case.
492 * File extents can be referenced by:
494 * - multiple snapshots, subvolumes, or different generations in one subvol
495 * - different files inside a single subvolume (in theory, not implemented yet)
496 * - different offsets inside a file (bookend extents in file.c)
498 * The extent ref structure has fields for:
500 * - Objectid of the subvolume root
501 * - Generation number of the tree holding the reference
502 * - objectid of the file holding the reference
503 * - offset in the file corresponding to the key holding the reference
505 * When a file extent is allocated the fields are filled in:
506 * (root_key.objectid, trans->transid, inode objectid, offset in file)
508 * When a leaf is cow'd new references are added for every file extent found
509 * in the leaf. It looks the same as the create case, but trans->transid
510 * will be different when the block is cow'd.
512 * (root_key.objectid, trans->transid, inode objectid, offset in file)
514 * When a file extent is removed either during snapshot deletion or file
515 * truncation, the corresponding back reference is found
518 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
519 * inode objectid, offset in file)
521 * Btree extents can be referenced by:
523 * - Different subvolumes
524 * - Different generations of the same subvolume
526 * Storing sufficient information for a full reverse mapping of a btree
527 * block would require storing the lowest key of the block in the backref,
528 * and it would require updating that lowest key either before write out or
529 * every time it changed. Instead, the objectid of the lowest key is stored
530 * along with the level of the tree block. This provides a hint
531 * about where in the btree the block can be found. Searches through the
532 * btree only need to look for a pointer to that block, so they stop one
533 * level higher than the level recorded in the backref.
535 * Some btrees do not do reference counting on their extents. These
536 * include the extent tree and the tree of tree roots. Backrefs for these
537 * trees always have a generation of zero.
539 * When a tree block is created, back references are inserted:
541 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
543 * When a tree block is cow'd in a reference counted root,
544 * new back references are added for all the blocks it points to.
545 * These are of the form (trans->transid will have increased since creation):
547 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
549 * Because the lowest_key_objectid and the level are just hints
550 * they are not used when backrefs are deleted. When a backref is deleted:
552 * if backref was for a tree root:
553 * root_objectid = root->root_key.objectid
555 * root_objectid = btrfs_header_owner(parent)
557 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
559 * Back Reference Key hashing:
561 * Back references have four fields, each 64 bits long. Unfortunately,
562 * This is hashed into a single 64 bit number and placed into the key offset.
563 * The key objectid corresponds to the first byte in the extent, and the
564 * key type is set to BTRFS_EXTENT_REF_KEY
566 int btrfs_insert_extent_backref(struct btrfs_trans_handle
*trans
,
567 struct btrfs_root
*root
,
568 struct btrfs_path
*path
, u64 bytenr
,
569 u64 root_objectid
, u64 ref_generation
,
570 u64 owner
, u64 owner_offset
)
573 struct btrfs_key key
;
574 struct btrfs_extent_ref ref
;
575 struct btrfs_extent_ref
*disk_ref
;
578 btrfs_set_stack_ref_root(&ref
, root_objectid
);
579 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
580 btrfs_set_stack_ref_objectid(&ref
, owner
);
581 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
583 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
586 key
.objectid
= bytenr
;
587 key
.type
= BTRFS_EXTENT_REF_KEY
;
589 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(ref
));
590 while (ret
== -EEXIST
) {
591 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
592 struct btrfs_extent_ref
);
593 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
))
596 btrfs_release_path(root
, path
);
597 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
602 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
603 struct btrfs_extent_ref
);
604 write_extent_buffer(path
->nodes
[0], &ref
, (unsigned long)disk_ref
,
606 btrfs_mark_buffer_dirty(path
->nodes
[0]);
608 btrfs_release_path(root
, path
);
612 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
613 struct btrfs_root
*root
,
614 u64 bytenr
, u64 num_bytes
,
615 u64 root_objectid
, u64 ref_generation
,
616 u64 owner
, u64 owner_offset
)
618 struct btrfs_path
*path
;
620 struct btrfs_key key
;
621 struct extent_buffer
*l
;
622 struct btrfs_extent_item
*item
;
625 WARN_ON(num_bytes
< root
->sectorsize
);
626 path
= btrfs_alloc_path();
631 key
.objectid
= bytenr
;
632 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
633 key
.offset
= num_bytes
;
634 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
643 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
644 refs
= btrfs_extent_refs(l
, item
);
645 btrfs_set_extent_refs(l
, item
, refs
+ 1);
646 btrfs_mark_buffer_dirty(path
->nodes
[0]);
648 btrfs_release_path(root
->fs_info
->extent_root
, path
);
651 ret
= btrfs_insert_extent_backref(trans
, root
->fs_info
->extent_root
,
652 path
, bytenr
, root_objectid
,
653 ref_generation
, owner
, owner_offset
);
655 finish_current_insert(trans
, root
->fs_info
->extent_root
);
656 del_pending_extents(trans
, root
->fs_info
->extent_root
);
658 btrfs_free_path(path
);
662 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
663 struct btrfs_root
*root
)
665 finish_current_insert(trans
, root
->fs_info
->extent_root
);
666 del_pending_extents(trans
, root
->fs_info
->extent_root
);
670 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
671 struct btrfs_root
*root
, u64 bytenr
,
672 u64 num_bytes
, u32
*refs
)
674 struct btrfs_path
*path
;
676 struct btrfs_key key
;
677 struct extent_buffer
*l
;
678 struct btrfs_extent_item
*item
;
680 WARN_ON(num_bytes
< root
->sectorsize
);
681 path
= btrfs_alloc_path();
683 key
.objectid
= bytenr
;
684 key
.offset
= num_bytes
;
685 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
686 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
691 btrfs_print_leaf(root
, path
->nodes
[0]);
692 printk("failed to find block number %Lu\n", bytenr
);
696 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
697 *refs
= btrfs_extent_refs(l
, item
);
699 btrfs_free_path(path
);
703 u32
btrfs_count_snapshots_in_path(struct btrfs_root
*root
,
704 struct btrfs_path
*count_path
,
707 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
708 struct btrfs_path
*path
;
711 u64 root_objectid
= root
->root_key
.objectid
;
716 struct btrfs_key key
;
717 struct btrfs_key found_key
;
718 struct extent_buffer
*l
;
719 struct btrfs_extent_item
*item
;
720 struct btrfs_extent_ref
*ref_item
;
723 path
= btrfs_alloc_path();
726 bytenr
= first_extent
;
728 bytenr
= count_path
->nodes
[level
]->start
;
731 key
.objectid
= bytenr
;
734 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
735 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
741 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
743 if (found_key
.objectid
!= bytenr
||
744 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
748 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
751 nritems
= btrfs_header_nritems(l
);
752 if (path
->slots
[0] >= nritems
) {
753 ret
= btrfs_next_leaf(extent_root
, path
);
758 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
759 if (found_key
.objectid
!= bytenr
)
762 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
768 ref_item
= btrfs_item_ptr(l
, path
->slots
[0],
769 struct btrfs_extent_ref
);
770 found_objectid
= btrfs_ref_root(l
, ref_item
);
772 if (found_objectid
!= root_objectid
) {
779 if (cur_count
== 0) {
783 if (level
>= 0 && root
->node
== count_path
->nodes
[level
])
786 btrfs_release_path(root
, path
);
790 btrfs_free_path(path
);
793 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
794 struct btrfs_root
*root
, u64 owner_objectid
)
800 struct btrfs_disk_key disk_key
;
802 level
= btrfs_header_level(root
->node
);
803 generation
= trans
->transid
;
804 nritems
= btrfs_header_nritems(root
->node
);
807 btrfs_item_key(root
->node
, &disk_key
, 0);
809 btrfs_node_key(root
->node
, &disk_key
, 0);
810 key_objectid
= btrfs_disk_key_objectid(&disk_key
);
814 return btrfs_inc_extent_ref(trans
, root
, root
->node
->start
,
815 root
->node
->len
, owner_objectid
,
816 generation
, level
, key_objectid
);
819 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
820 struct extent_buffer
*buf
)
824 struct btrfs_key key
;
825 struct btrfs_file_extent_item
*fi
;
834 level
= btrfs_header_level(buf
);
835 nritems
= btrfs_header_nritems(buf
);
836 for (i
= 0; i
< nritems
; i
++) {
839 btrfs_item_key_to_cpu(buf
, &key
, i
);
840 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
842 fi
= btrfs_item_ptr(buf
, i
,
843 struct btrfs_file_extent_item
);
844 if (btrfs_file_extent_type(buf
, fi
) ==
845 BTRFS_FILE_EXTENT_INLINE
)
847 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
848 if (disk_bytenr
== 0)
850 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
,
851 btrfs_file_extent_disk_num_bytes(buf
, fi
),
852 root
->root_key
.objectid
, trans
->transid
,
853 key
.objectid
, key
.offset
);
859 bytenr
= btrfs_node_blockptr(buf
, i
);
860 btrfs_node_key_to_cpu(buf
, &key
, i
);
861 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
,
862 btrfs_level_size(root
, level
- 1),
863 root
->root_key
.objectid
,
865 level
- 1, key
.objectid
);
876 for (i
=0; i
< faili
; i
++) {
879 btrfs_item_key_to_cpu(buf
, &key
, i
);
880 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
882 fi
= btrfs_item_ptr(buf
, i
,
883 struct btrfs_file_extent_item
);
884 if (btrfs_file_extent_type(buf
, fi
) ==
885 BTRFS_FILE_EXTENT_INLINE
)
887 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
888 if (disk_bytenr
== 0)
890 err
= btrfs_free_extent(trans
, root
, disk_bytenr
,
891 btrfs_file_extent_disk_num_bytes(buf
,
895 bytenr
= btrfs_node_blockptr(buf
, i
);
896 err
= btrfs_free_extent(trans
, root
, bytenr
,
897 btrfs_level_size(root
, level
- 1), 0);
905 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
906 struct btrfs_root
*root
,
907 struct btrfs_path
*path
,
908 struct btrfs_block_group_cache
*cache
)
912 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
914 struct extent_buffer
*leaf
;
916 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
921 leaf
= path
->nodes
[0];
922 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
923 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
924 btrfs_mark_buffer_dirty(leaf
);
925 btrfs_release_path(extent_root
, path
);
927 finish_current_insert(trans
, extent_root
);
928 pending_ret
= del_pending_extents(trans
, extent_root
);
937 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
938 struct btrfs_root
*root
)
940 struct extent_io_tree
*block_group_cache
;
941 struct btrfs_block_group_cache
*cache
;
945 struct btrfs_path
*path
;
951 block_group_cache
= &root
->fs_info
->block_group_cache
;
952 path
= btrfs_alloc_path();
957 ret
= find_first_extent_bit(block_group_cache
, last
,
958 &start
, &end
, BLOCK_GROUP_DIRTY
);
963 ret
= get_state_private(block_group_cache
, start
, &ptr
);
967 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
968 err
= write_one_cache_group(trans
, root
,
971 * if we fail to write the cache group, we want
972 * to keep it marked dirty in hopes that a later
979 clear_extent_bits(block_group_cache
, start
, end
,
980 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
982 btrfs_free_path(path
);
986 static int update_block_group(struct btrfs_trans_handle
*trans
,
987 struct btrfs_root
*root
,
988 u64 bytenr
, u64 num_bytes
, int alloc
,
991 struct btrfs_block_group_cache
*cache
;
992 struct btrfs_fs_info
*info
= root
->fs_info
;
993 u64 total
= num_bytes
;
1000 cache
= btrfs_lookup_block_group(info
, bytenr
);
1004 byte_in_group
= bytenr
- cache
->key
.objectid
;
1005 WARN_ON(byte_in_group
> cache
->key
.offset
);
1006 start
= cache
->key
.objectid
;
1007 end
= start
+ cache
->key
.offset
- 1;
1008 set_extent_bits(&info
->block_group_cache
, start
, end
,
1009 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
1011 old_val
= btrfs_block_group_used(&cache
->item
);
1012 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
1014 old_val
+= num_bytes
;
1016 old_val
-= num_bytes
;
1018 set_extent_dirty(&info
->free_space_cache
,
1019 bytenr
, bytenr
+ num_bytes
- 1,
1023 btrfs_set_block_group_used(&cache
->item
, old_val
);
1025 bytenr
+= num_bytes
;
1029 static int update_pinned_extents(struct btrfs_root
*root
,
1030 u64 bytenr
, u64 num
, int pin
)
1033 struct btrfs_block_group_cache
*cache
;
1034 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1037 set_extent_dirty(&fs_info
->pinned_extents
,
1038 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1040 clear_extent_dirty(&fs_info
->pinned_extents
,
1041 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1044 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1046 len
= min(num
, cache
->key
.offset
-
1047 (bytenr
- cache
->key
.objectid
));
1049 cache
->pinned
+= len
;
1050 fs_info
->total_pinned
+= len
;
1052 cache
->pinned
-= len
;
1053 fs_info
->total_pinned
-= len
;
1061 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_io_tree
*copy
)
1066 struct extent_io_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1070 ret
= find_first_extent_bit(pinned_extents
, last
,
1071 &start
, &end
, EXTENT_DIRTY
);
1074 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1080 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1081 struct btrfs_root
*root
,
1082 struct extent_io_tree
*unpin
)
1087 struct extent_io_tree
*free_space_cache
;
1088 free_space_cache
= &root
->fs_info
->free_space_cache
;
1091 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1095 update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1096 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1097 set_extent_dirty(free_space_cache
, start
, end
, GFP_NOFS
);
1102 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1103 struct btrfs_root
*extent_root
)
1107 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1108 struct extent_buffer
*eb
;
1109 struct btrfs_path
*path
;
1110 struct btrfs_key ins
;
1111 struct btrfs_disk_key first
;
1112 struct btrfs_extent_item extent_item
;
1117 btrfs_set_stack_extent_refs(&extent_item
, 1);
1118 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
1119 path
= btrfs_alloc_path();
1122 ret
= find_first_extent_bit(&info
->extent_ins
, 0, &start
,
1123 &end
, EXTENT_LOCKED
);
1127 ins
.objectid
= start
;
1128 ins
.offset
= end
+ 1 - start
;
1129 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
1130 &extent_item
, sizeof(extent_item
));
1131 clear_extent_bits(&info
->extent_ins
, start
, end
, EXTENT_LOCKED
,
1133 eb
= read_tree_block(extent_root
, ins
.objectid
, ins
.offset
);
1134 level
= btrfs_header_level(eb
);
1136 btrfs_item_key(eb
, &first
, 0);
1138 btrfs_node_key(eb
, &first
, 0);
1140 err
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1141 start
, extent_root
->root_key
.objectid
,
1143 btrfs_disk_key_objectid(&first
));
1145 free_extent_buffer(eb
);
1147 btrfs_free_path(path
);
1151 static int pin_down_bytes(struct btrfs_root
*root
, u64 bytenr
, u32 num_bytes
,
1155 struct extent_buffer
*buf
;
1158 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1160 if (btrfs_buffer_uptodate(buf
)) {
1162 root
->fs_info
->running_transaction
->transid
;
1163 u64 header_transid
=
1164 btrfs_header_generation(buf
);
1165 if (header_transid
== transid
) {
1166 clean_tree_block(NULL
, root
, buf
);
1167 free_extent_buffer(buf
);
1171 free_extent_buffer(buf
);
1173 update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1175 set_extent_bits(&root
->fs_info
->pending_del
,
1176 bytenr
, bytenr
+ num_bytes
- 1,
1177 EXTENT_LOCKED
, GFP_NOFS
);
1184 * remove an extent from the root, returns 0 on success
1186 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1187 *root
, u64 bytenr
, u64 num_bytes
,
1188 u64 root_objectid
, u64 ref_generation
,
1189 u64 owner_objectid
, u64 owner_offset
, int pin
,
1192 struct btrfs_path
*path
;
1193 struct btrfs_key key
;
1194 struct btrfs_fs_info
*info
= root
->fs_info
;
1195 struct btrfs_root
*extent_root
= info
->extent_root
;
1196 struct extent_buffer
*leaf
;
1198 int extent_slot
= 0;
1199 int found_extent
= 0;
1201 struct btrfs_extent_item
*ei
;
1204 key
.objectid
= bytenr
;
1205 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1206 key
.offset
= num_bytes
;
1207 path
= btrfs_alloc_path();
1212 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1213 bytenr
, root_objectid
,
1215 owner_objectid
, owner_offset
, 1);
1217 struct btrfs_key found_key
;
1218 extent_slot
= path
->slots
[0];
1219 while(extent_slot
> 0) {
1221 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
,
1223 if (found_key
.objectid
!= bytenr
)
1225 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
&&
1226 found_key
.offset
== num_bytes
) {
1230 if (path
->slots
[0] - extent_slot
> 5)
1234 ret
= btrfs_del_item(trans
, extent_root
, path
);
1236 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1238 printk("Unable to find ref byte nr %Lu root %Lu "
1239 " gen %Lu owner %Lu offset %Lu\n", bytenr
,
1240 root_objectid
, ref_generation
, owner_objectid
,
1243 if (!found_extent
) {
1244 btrfs_release_path(extent_root
, path
);
1245 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
1249 extent_slot
= path
->slots
[0];
1252 leaf
= path
->nodes
[0];
1253 ei
= btrfs_item_ptr(leaf
, extent_slot
,
1254 struct btrfs_extent_item
);
1255 refs
= btrfs_extent_refs(leaf
, ei
);
1258 btrfs_set_extent_refs(leaf
, ei
, refs
);
1260 btrfs_mark_buffer_dirty(leaf
);
1262 if (refs
== 0 && found_extent
&& path
->slots
[0] == extent_slot
+ 1) {
1263 /* if the back ref and the extent are next to each other
1264 * they get deleted below in one shot
1266 path
->slots
[0] = extent_slot
;
1268 } else if (found_extent
) {
1269 /* otherwise delete the extent back ref */
1270 ret
= btrfs_del_item(trans
, extent_root
, path
);
1272 /* if refs are 0, we need to setup the path for deletion */
1274 btrfs_release_path(extent_root
, path
);
1275 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
,
1288 ret
= pin_down_bytes(root
, bytenr
, num_bytes
, 0);
1294 /* block accounting for super block */
1295 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1296 btrfs_set_super_bytes_used(&info
->super_copy
,
1297 super_used
- num_bytes
);
1299 /* block accounting for root item */
1300 root_used
= btrfs_root_used(&root
->root_item
);
1301 btrfs_set_root_used(&root
->root_item
,
1302 root_used
- num_bytes
);
1303 ret
= btrfs_del_items(trans
, extent_root
, path
, path
->slots
[0],
1308 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1312 btrfs_free_path(path
);
1313 finish_current_insert(trans
, extent_root
);
1318 * find all the blocks marked as pending in the radix tree and remove
1319 * them from the extent map
1321 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1322 btrfs_root
*extent_root
)
1328 struct extent_io_tree
*pending_del
;
1329 struct extent_io_tree
*pinned_extents
;
1331 pending_del
= &extent_root
->fs_info
->pending_del
;
1332 pinned_extents
= &extent_root
->fs_info
->pinned_extents
;
1335 ret
= find_first_extent_bit(pending_del
, 0, &start
, &end
,
1339 update_pinned_extents(extent_root
, start
, end
+ 1 - start
, 1);
1340 clear_extent_bits(pending_del
, start
, end
, EXTENT_LOCKED
,
1342 ret
= __free_extent(trans
, extent_root
,
1343 start
, end
+ 1 - start
,
1344 extent_root
->root_key
.objectid
,
1353 * remove an extent from the root, returns 0 on success
1355 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1356 *root
, u64 bytenr
, u64 num_bytes
,
1357 u64 root_objectid
, u64 ref_generation
,
1358 u64 owner_objectid
, u64 owner_offset
, int pin
)
1360 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1364 WARN_ON(num_bytes
< root
->sectorsize
);
1365 if (!root
->ref_cows
)
1368 if (root
== extent_root
) {
1369 pin_down_bytes(root
, bytenr
, num_bytes
, 1);
1372 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, root_objectid
,
1373 ref_generation
, owner_objectid
, owner_offset
,
1375 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
1376 return ret
? ret
: pending_ret
;
1379 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
1381 u64 mask
= ((u64
)root
->stripesize
- 1);
1382 u64 ret
= (val
+ mask
) & ~mask
;
1387 * walks the btree of allocated extents and find a hole of a given size.
1388 * The key ins is changed to record the hole:
1389 * ins->objectid == block start
1390 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1391 * ins->offset == number of blocks
1392 * Any available blocks before search_start are skipped.
1394 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
1395 struct btrfs_root
*orig_root
,
1396 u64 num_bytes
, u64 empty_size
,
1397 u64 search_start
, u64 search_end
,
1398 u64 hint_byte
, struct btrfs_key
*ins
,
1399 u64 exclude_start
, u64 exclude_nr
,
1403 u64 orig_search_start
= search_start
;
1404 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
1405 struct btrfs_fs_info
*info
= root
->fs_info
;
1406 u64 total_needed
= num_bytes
;
1407 struct btrfs_block_group_cache
*block_group
;
1411 WARN_ON(num_bytes
< root
->sectorsize
);
1412 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1414 if (search_end
== (u64
)-1)
1415 search_end
= btrfs_super_total_bytes(&info
->super_copy
);
1418 block_group
= btrfs_lookup_block_group(info
, hint_byte
);
1420 hint_byte
= search_start
;
1421 block_group
= btrfs_find_block_group(root
, block_group
,
1422 hint_byte
, data
, 1);
1424 block_group
= btrfs_find_block_group(root
,
1426 search_start
, data
, 1);
1429 total_needed
+= empty_size
;
1433 block_group
= btrfs_lookup_block_group(info
, search_start
);
1435 block_group
= btrfs_lookup_block_group(info
,
1438 ret
= find_search_start(root
, &block_group
, &search_start
,
1439 total_needed
, data
);
1443 search_start
= stripe_align(root
, search_start
);
1444 ins
->objectid
= search_start
;
1445 ins
->offset
= num_bytes
;
1447 if (ins
->objectid
+ num_bytes
>= search_end
)
1450 if (ins
->objectid
+ num_bytes
>
1451 block_group
->key
.objectid
+ block_group
->key
.offset
) {
1452 search_start
= block_group
->key
.objectid
+
1453 block_group
->key
.offset
;
1457 if (test_range_bit(&info
->extent_ins
, ins
->objectid
,
1458 ins
->objectid
+ num_bytes
-1, EXTENT_LOCKED
, 0)) {
1459 search_start
= ins
->objectid
+ num_bytes
;
1463 if (test_range_bit(&info
->pinned_extents
, ins
->objectid
,
1464 ins
->objectid
+ num_bytes
-1, EXTENT_DIRTY
, 0)) {
1465 search_start
= ins
->objectid
+ num_bytes
;
1469 if (exclude_nr
> 0 && (ins
->objectid
+ num_bytes
> exclude_start
&&
1470 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1471 search_start
= exclude_start
+ exclude_nr
;
1475 if (!(data
& BLOCK_GROUP_DATA
)) {
1476 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1478 trans
->block_group
= block_group
;
1480 ins
->offset
= num_bytes
;
1484 if (search_start
+ num_bytes
>= search_end
) {
1486 search_start
= orig_search_start
;
1493 total_needed
-= empty_size
;
1498 block_group
= btrfs_lookup_block_group(info
, search_start
);
1500 block_group
= btrfs_find_block_group(root
, block_group
,
1501 search_start
, data
, 0);
1508 * finds a free extent and does all the dirty work required for allocation
1509 * returns the key for the extent through ins, and a tree buffer for
1510 * the first block of the extent through buf.
1512 * returns 0 if everything worked, non-zero otherwise.
1514 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1515 struct btrfs_root
*root
,
1516 u64 num_bytes
, u64 root_objectid
, u64 ref_generation
,
1517 u64 owner
, u64 owner_offset
,
1518 u64 empty_size
, u64 hint_byte
,
1519 u64 search_end
, struct btrfs_key
*ins
, int data
)
1525 u64 search_start
= 0;
1528 struct btrfs_fs_info
*info
= root
->fs_info
;
1529 struct btrfs_root
*extent_root
= info
->extent_root
;
1530 struct btrfs_extent_item
*extent_item
;
1531 struct btrfs_extent_ref
*ref
;
1532 struct btrfs_path
*path
;
1533 struct btrfs_key keys
[2];
1536 data
= BLOCK_GROUP_DATA
;
1537 else if (root
== root
->fs_info
->chunk_root
)
1538 data
= BLOCK_GROUP_SYSTEM
;
1540 data
= BLOCK_GROUP_METADATA
;
1542 new_hint
= max(hint_byte
, root
->fs_info
->alloc_start
);
1543 if (new_hint
< btrfs_super_total_bytes(&info
->super_copy
))
1544 hint_byte
= new_hint
;
1546 WARN_ON(num_bytes
< root
->sectorsize
);
1547 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
1548 search_start
, search_end
, hint_byte
, ins
,
1549 trans
->alloc_exclude_start
,
1550 trans
->alloc_exclude_nr
, data
);
1555 /* block accounting for super block */
1556 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1557 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
1559 /* block accounting for root item */
1560 root_used
= btrfs_root_used(&root
->root_item
);
1561 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
1563 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
1564 ins
->objectid
, ins
->objectid
+ ins
->offset
- 1,
1567 if (root
== extent_root
) {
1568 set_extent_bits(&root
->fs_info
->extent_ins
, ins
->objectid
,
1569 ins
->objectid
+ ins
->offset
- 1,
1570 EXTENT_LOCKED
, GFP_NOFS
);
1574 WARN_ON(trans
->alloc_exclude_nr
);
1575 trans
->alloc_exclude_start
= ins
->objectid
;
1576 trans
->alloc_exclude_nr
= ins
->offset
;
1578 memcpy(&keys
[0], ins
, sizeof(*ins
));
1579 keys
[1].offset
= hash_extent_ref(root_objectid
, ref_generation
,
1580 owner
, owner_offset
);
1581 keys
[1].objectid
= ins
->objectid
;
1582 keys
[1].type
= BTRFS_EXTENT_REF_KEY
;
1583 sizes
[0] = sizeof(*extent_item
);
1584 sizes
[1] = sizeof(*ref
);
1586 path
= btrfs_alloc_path();
1589 ret
= btrfs_insert_empty_items(trans
, extent_root
, path
, keys
,
1593 extent_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
1594 struct btrfs_extent_item
);
1595 btrfs_set_extent_refs(path
->nodes
[0], extent_item
, 1);
1596 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
1597 struct btrfs_extent_ref
);
1599 btrfs_set_ref_root(path
->nodes
[0], ref
, root_objectid
);
1600 btrfs_set_ref_generation(path
->nodes
[0], ref
, ref_generation
);
1601 btrfs_set_ref_objectid(path
->nodes
[0], ref
, owner
);
1602 btrfs_set_ref_offset(path
->nodes
[0], ref
, owner_offset
);
1604 btrfs_mark_buffer_dirty(path
->nodes
[0]);
1606 trans
->alloc_exclude_start
= 0;
1607 trans
->alloc_exclude_nr
= 0;
1608 btrfs_free_path(path
);
1609 finish_current_insert(trans
, extent_root
);
1610 pending_ret
= del_pending_extents(trans
, extent_root
);
1620 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0);
1622 printk("update block group failed for %Lu %Lu\n",
1623 ins
->objectid
, ins
->offset
);
1630 * helper function to allocate a block for a given tree
1631 * returns the tree buffer or NULL.
1633 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1634 struct btrfs_root
*root
,
1636 u64 root_objectid
, u64 hint
,
1642 ref_generation
= trans
->transid
;
1647 return __btrfs_alloc_free_block(trans
, root
, blocksize
, root_objectid
,
1648 ref_generation
, 0, 0, hint
, empty_size
);
1652 * helper function to allocate a block for a given tree
1653 * returns the tree buffer or NULL.
1655 struct extent_buffer
*__btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1656 struct btrfs_root
*root
,
1665 struct btrfs_key ins
;
1667 struct extent_buffer
*buf
;
1669 ret
= btrfs_alloc_extent(trans
, root
, blocksize
,
1670 root_objectid
, ref_generation
,
1671 level
, first_objectid
, empty_size
, hint
,
1675 return ERR_PTR(ret
);
1677 buf
= btrfs_find_create_tree_block(root
, ins
.objectid
, blocksize
);
1679 btrfs_free_extent(trans
, root
, ins
.objectid
, blocksize
,
1680 root
->root_key
.objectid
, ref_generation
,
1682 return ERR_PTR(-ENOMEM
);
1684 btrfs_set_header_generation(buf
, trans
->transid
);
1685 clean_tree_block(trans
, root
, buf
);
1686 wait_on_tree_block_writeback(root
, buf
);
1687 btrfs_set_buffer_uptodate(buf
);
1689 if (PageDirty(buf
->first_page
)) {
1690 printk("page %lu dirty\n", buf
->first_page
->index
);
1694 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
1695 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
1696 set_extent_bits(&BTRFS_I(root
->fs_info
->btree_inode
)->io_tree
,
1697 buf
->start
, buf
->start
+ buf
->len
- 1,
1698 EXTENT_CSUM
, GFP_NOFS
);
1699 buf
->flags
|= EXTENT_CSUM
;
1700 if (!btrfs_test_opt(root
, SSD
))
1701 btrfs_set_buffer_defrag(buf
);
1702 trans
->blocks_used
++;
1706 static int noinline
drop_leaf_ref(struct btrfs_trans_handle
*trans
,
1707 struct btrfs_root
*root
,
1708 struct extent_buffer
*leaf
)
1711 u64 leaf_generation
;
1712 struct btrfs_key key
;
1713 struct btrfs_file_extent_item
*fi
;
1718 BUG_ON(!btrfs_is_leaf(leaf
));
1719 nritems
= btrfs_header_nritems(leaf
);
1720 leaf_owner
= btrfs_header_owner(leaf
);
1721 leaf_generation
= btrfs_header_generation(leaf
);
1723 for (i
= 0; i
< nritems
; i
++) {
1726 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1727 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1729 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1730 if (btrfs_file_extent_type(leaf
, fi
) ==
1731 BTRFS_FILE_EXTENT_INLINE
)
1734 * FIXME make sure to insert a trans record that
1735 * repeats the snapshot del on crash
1737 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1738 if (disk_bytenr
== 0)
1740 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
1741 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
1742 leaf_owner
, leaf_generation
,
1743 key
.objectid
, key
.offset
, 0);
1749 static void noinline
reada_walk_down(struct btrfs_root
*root
,
1750 struct extent_buffer
*node
,
1763 nritems
= btrfs_header_nritems(node
);
1764 level
= btrfs_header_level(node
);
1768 for (i
= slot
; i
< nritems
&& skipped
< 32; i
++) {
1769 bytenr
= btrfs_node_blockptr(node
, i
);
1770 if (last
&& ((bytenr
> last
&& bytenr
- last
> 32 * 1024) ||
1771 (last
> bytenr
&& last
- bytenr
> 32 * 1024))) {
1775 blocksize
= btrfs_level_size(root
, level
- 1);
1777 ret
= lookup_extent_ref(NULL
, root
, bytenr
,
1785 mutex_unlock(&root
->fs_info
->fs_mutex
);
1786 ret
= readahead_tree_block(root
, bytenr
, blocksize
);
1787 last
= bytenr
+ blocksize
;
1789 mutex_lock(&root
->fs_info
->fs_mutex
);
1796 * helper function for drop_snapshot, this walks down the tree dropping ref
1797 * counts as it goes.
1799 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
1800 struct btrfs_root
*root
,
1801 struct btrfs_path
*path
, int *level
)
1806 struct extent_buffer
*next
;
1807 struct extent_buffer
*cur
;
1808 struct extent_buffer
*parent
;
1813 WARN_ON(*level
< 0);
1814 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1815 ret
= lookup_extent_ref(trans
, root
,
1816 path
->nodes
[*level
]->start
,
1817 path
->nodes
[*level
]->len
, &refs
);
1823 * walk down to the last node level and free all the leaves
1825 while(*level
>= 0) {
1826 WARN_ON(*level
< 0);
1827 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1828 cur
= path
->nodes
[*level
];
1830 if (btrfs_header_level(cur
) != *level
)
1833 if (path
->slots
[*level
] >=
1834 btrfs_header_nritems(cur
))
1837 ret
= drop_leaf_ref(trans
, root
, cur
);
1841 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1842 blocksize
= btrfs_level_size(root
, *level
- 1);
1843 ret
= lookup_extent_ref(trans
, root
, bytenr
, blocksize
, &refs
);
1846 parent
= path
->nodes
[*level
];
1847 root_owner
= btrfs_header_owner(parent
);
1848 root_gen
= btrfs_header_generation(parent
);
1849 path
->slots
[*level
]++;
1850 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1851 blocksize
, root_owner
,
1856 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
1857 if (!next
|| !btrfs_buffer_uptodate(next
)) {
1858 free_extent_buffer(next
);
1859 reada_walk_down(root
, cur
, path
->slots
[*level
]);
1860 mutex_unlock(&root
->fs_info
->fs_mutex
);
1861 next
= read_tree_block(root
, bytenr
, blocksize
);
1862 mutex_lock(&root
->fs_info
->fs_mutex
);
1864 /* we dropped the lock, check one more time */
1865 ret
= lookup_extent_ref(trans
, root
, bytenr
,
1869 parent
= path
->nodes
[*level
];
1870 root_owner
= btrfs_header_owner(parent
);
1871 root_gen
= btrfs_header_generation(parent
);
1873 path
->slots
[*level
]++;
1874 free_extent_buffer(next
);
1875 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1883 WARN_ON(*level
<= 0);
1884 if (path
->nodes
[*level
-1])
1885 free_extent_buffer(path
->nodes
[*level
-1]);
1886 path
->nodes
[*level
-1] = next
;
1887 *level
= btrfs_header_level(next
);
1888 path
->slots
[*level
] = 0;
1891 WARN_ON(*level
< 0);
1892 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1894 if (path
->nodes
[*level
] == root
->node
) {
1895 root_owner
= root
->root_key
.objectid
;
1896 parent
= path
->nodes
[*level
];
1898 parent
= path
->nodes
[*level
+ 1];
1899 root_owner
= btrfs_header_owner(parent
);
1902 root_gen
= btrfs_header_generation(parent
);
1903 ret
= btrfs_free_extent(trans
, root
, path
->nodes
[*level
]->start
,
1904 path
->nodes
[*level
]->len
,
1905 root_owner
, root_gen
, 0, 0, 1);
1906 free_extent_buffer(path
->nodes
[*level
]);
1907 path
->nodes
[*level
] = NULL
;
1914 * helper for dropping snapshots. This walks back up the tree in the path
1915 * to find the first node higher up where we haven't yet gone through
1918 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
1919 struct btrfs_root
*root
,
1920 struct btrfs_path
*path
, int *level
)
1924 struct btrfs_root_item
*root_item
= &root
->root_item
;
1929 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
1930 slot
= path
->slots
[i
];
1931 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
1932 struct extent_buffer
*node
;
1933 struct btrfs_disk_key disk_key
;
1934 node
= path
->nodes
[i
];
1937 WARN_ON(*level
== 0);
1938 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
1939 memcpy(&root_item
->drop_progress
,
1940 &disk_key
, sizeof(disk_key
));
1941 root_item
->drop_level
= i
;
1944 if (path
->nodes
[*level
] == root
->node
) {
1945 root_owner
= root
->root_key
.objectid
;
1947 btrfs_header_generation(path
->nodes
[*level
]);
1949 struct extent_buffer
*node
;
1950 node
= path
->nodes
[*level
+ 1];
1951 root_owner
= btrfs_header_owner(node
);
1952 root_gen
= btrfs_header_generation(node
);
1954 ret
= btrfs_free_extent(trans
, root
,
1955 path
->nodes
[*level
]->start
,
1956 path
->nodes
[*level
]->len
,
1957 root_owner
, root_gen
, 0, 0, 1);
1959 free_extent_buffer(path
->nodes
[*level
]);
1960 path
->nodes
[*level
] = NULL
;
1968 * drop the reference count on the tree rooted at 'snap'. This traverses
1969 * the tree freeing any blocks that have a ref count of zero after being
1972 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
1978 struct btrfs_path
*path
;
1981 struct btrfs_root_item
*root_item
= &root
->root_item
;
1983 path
= btrfs_alloc_path();
1986 level
= btrfs_header_level(root
->node
);
1988 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
1989 path
->nodes
[level
] = root
->node
;
1990 extent_buffer_get(root
->node
);
1991 path
->slots
[level
] = 0;
1993 struct btrfs_key key
;
1994 struct btrfs_disk_key found_key
;
1995 struct extent_buffer
*node
;
1997 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
1998 level
= root_item
->drop_level
;
1999 path
->lowest_level
= level
;
2000 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2005 node
= path
->nodes
[level
];
2006 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
2007 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
2008 sizeof(found_key
)));
2011 wret
= walk_down_tree(trans
, root
, path
, &level
);
2017 wret
= walk_up_tree(trans
, root
, path
, &level
);
2025 for (i
= 0; i
<= orig_level
; i
++) {
2026 if (path
->nodes
[i
]) {
2027 free_extent_buffer(path
->nodes
[i
]);
2028 path
->nodes
[i
] = NULL
;
2032 btrfs_free_path(path
);
2036 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
2043 ret
= find_first_extent_bit(&info
->block_group_cache
, 0,
2044 &start
, &end
, (unsigned int)-1);
2047 ret
= get_state_private(&info
->block_group_cache
, start
, &ptr
);
2049 kfree((void *)(unsigned long)ptr
);
2050 clear_extent_bits(&info
->block_group_cache
, start
,
2051 end
, (unsigned int)-1, GFP_NOFS
);
2054 ret
= find_first_extent_bit(&info
->free_space_cache
, 0,
2055 &start
, &end
, EXTENT_DIRTY
);
2058 clear_extent_dirty(&info
->free_space_cache
, start
,
2064 static int noinline
relocate_inode_pages(struct inode
*inode
, u64 start
,
2070 u64 existing_delalloc
;
2071 unsigned long last_index
;
2074 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2075 struct file_ra_state
*ra
;
2077 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2079 mutex_lock(&inode
->i_mutex
);
2080 i
= start
>> PAGE_CACHE_SHIFT
;
2081 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
2083 file_ra_state_init(ra
, inode
->i_mapping
);
2084 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
, last_index
);
2087 for (; i
<= last_index
; i
++) {
2088 page
= grab_cache_page(inode
->i_mapping
, i
);
2091 if (!PageUptodate(page
)) {
2092 btrfs_readpage(NULL
, page
);
2094 if (!PageUptodate(page
)) {
2096 page_cache_release(page
);
2100 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2101 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2103 lock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2105 delalloc_start
= page_start
;
2106 existing_delalloc
= count_range_bits(io_tree
,
2107 &delalloc_start
, page_end
,
2108 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
2110 set_extent_delalloc(io_tree
, page_start
,
2111 page_end
, GFP_NOFS
);
2113 unlock_extent(io_tree
, page_start
, page_end
, GFP_NOFS
);
2114 set_page_dirty(page
);
2116 page_cache_release(page
);
2120 mutex_unlock(&inode
->i_mutex
);
2125 * note, this releases the path
2127 static int noinline
relocate_one_reference(struct btrfs_root
*extent_root
,
2128 struct btrfs_path
*path
,
2129 struct btrfs_key
*extent_key
)
2131 struct inode
*inode
;
2132 struct btrfs_root
*found_root
;
2133 struct btrfs_key
*root_location
;
2134 struct btrfs_extent_ref
*ref
;
2141 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2142 struct btrfs_extent_ref
);
2143 ref_root
= btrfs_ref_root(path
->nodes
[0], ref
);
2144 ref_gen
= btrfs_ref_generation(path
->nodes
[0], ref
);
2145 ref_objectid
= btrfs_ref_objectid(path
->nodes
[0], ref
);
2146 ref_offset
= btrfs_ref_offset(path
->nodes
[0], ref
);
2147 btrfs_release_path(extent_root
, path
);
2149 root_location
= kmalloc(sizeof(*root_location
), GFP_NOFS
);
2150 root_location
->objectid
= ref_root
;
2152 root_location
->offset
= 0;
2154 root_location
->offset
= (u64
)-1;
2155 root_location
->type
= BTRFS_ROOT_ITEM_KEY
;
2157 found_root
= btrfs_read_fs_root_no_name(extent_root
->fs_info
,
2159 BUG_ON(!found_root
);
2160 kfree(root_location
);
2162 if (ref_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
2163 mutex_unlock(&extent_root
->fs_info
->fs_mutex
);
2164 inode
= btrfs_iget_locked(extent_root
->fs_info
->sb
,
2165 ref_objectid
, found_root
);
2166 if (inode
->i_state
& I_NEW
) {
2167 /* the inode and parent dir are two different roots */
2168 BTRFS_I(inode
)->root
= found_root
;
2169 BTRFS_I(inode
)->location
.objectid
= ref_objectid
;
2170 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
2171 BTRFS_I(inode
)->location
.offset
= 0;
2172 btrfs_read_locked_inode(inode
);
2173 unlock_new_inode(inode
);
2176 /* this can happen if the reference is not against
2177 * the latest version of the tree root
2179 if (is_bad_inode(inode
)) {
2180 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2183 relocate_inode_pages(inode
, ref_offset
, extent_key
->offset
);
2184 /* FIXME, data=ordered will help get rid of this */
2185 filemap_fdatawrite(inode
->i_mapping
);
2187 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2189 struct btrfs_trans_handle
*trans
;
2190 struct btrfs_key found_key
;
2191 struct extent_buffer
*eb
;
2195 trans
= btrfs_start_transaction(found_root
, 1);
2196 eb
= read_tree_block(found_root
, extent_key
->objectid
,
2197 extent_key
->offset
);
2198 level
= btrfs_header_level(eb
);
2201 btrfs_item_key_to_cpu(eb
, &found_key
, 0);
2203 btrfs_node_key_to_cpu(eb
, &found_key
, 0);
2205 free_extent_buffer(eb
);
2207 path
->lowest_level
= level
;
2209 ret
= btrfs_search_slot(trans
, found_root
, &found_key
, path
,
2211 path
->lowest_level
= 0;
2212 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
2213 if (!path
->nodes
[i
])
2215 free_extent_buffer(path
->nodes
[i
]);
2216 path
->nodes
[i
] = NULL
;
2218 btrfs_release_path(found_root
, path
);
2219 btrfs_end_transaction(trans
, found_root
);
2226 static int noinline
relocate_one_extent(struct btrfs_root
*extent_root
,
2227 struct btrfs_path
*path
,
2228 struct btrfs_key
*extent_key
)
2230 struct btrfs_key key
;
2231 struct btrfs_key found_key
;
2232 struct extent_buffer
*leaf
;
2237 key
.objectid
= extent_key
->objectid
;
2238 key
.type
= BTRFS_EXTENT_REF_KEY
;
2242 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2248 leaf
= path
->nodes
[0];
2249 nritems
= btrfs_header_nritems(leaf
);
2250 if (path
->slots
[0] == nritems
)
2253 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2254 if (found_key
.objectid
!= extent_key
->objectid
)
2257 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
2260 key
.offset
= found_key
.offset
+ 1;
2261 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2263 ret
= relocate_one_reference(extent_root
, path
, extent_key
);
2269 btrfs_release_path(extent_root
, path
);
2273 int btrfs_shrink_extent_tree(struct btrfs_root
*root
, u64 new_size
)
2275 struct btrfs_trans_handle
*trans
;
2276 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
2277 struct btrfs_path
*path
;
2280 struct btrfs_fs_info
*info
= root
->fs_info
;
2281 struct extent_io_tree
*block_group_cache
;
2282 struct btrfs_key key
;
2283 struct btrfs_key found_key
;
2284 struct extent_buffer
*leaf
;
2289 btrfs_set_super_total_bytes(&info
->super_copy
, new_size
);
2290 clear_extent_dirty(&info
->free_space_cache
, new_size
, (u64
)-1,
2292 block_group_cache
= &info
->block_group_cache
;
2293 path
= btrfs_alloc_path();
2294 root
= root
->fs_info
->extent_root
;
2299 key
.objectid
= new_size
;
2302 cur_byte
= key
.objectid
;
2304 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2308 ret
= btrfs_previous_item(root
, path
, 0, BTRFS_EXTENT_ITEM_KEY
);
2312 leaf
= path
->nodes
[0];
2313 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2314 if (found_key
.objectid
+ found_key
.offset
> new_size
) {
2315 cur_byte
= found_key
.objectid
;
2316 key
.objectid
= cur_byte
;
2319 btrfs_release_path(root
, path
);
2322 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2326 leaf
= path
->nodes
[0];
2327 nritems
= btrfs_header_nritems(leaf
);
2329 if (path
->slots
[0] >= nritems
) {
2330 ret
= btrfs_next_leaf(root
, path
);
2337 leaf
= path
->nodes
[0];
2338 nritems
= btrfs_header_nritems(leaf
);
2341 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2343 if (progress
&& need_resched()) {
2344 memcpy(&key
, &found_key
, sizeof(key
));
2345 mutex_unlock(&root
->fs_info
->fs_mutex
);
2347 mutex_lock(&root
->fs_info
->fs_mutex
);
2348 btrfs_release_path(root
, path
);
2349 btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2355 if (btrfs_key_type(&found_key
) != BTRFS_EXTENT_ITEM_KEY
||
2356 found_key
.objectid
+ found_key
.offset
<= cur_byte
) {
2362 cur_byte
= found_key
.objectid
+ found_key
.offset
;
2363 key
.objectid
= cur_byte
;
2364 btrfs_release_path(root
, path
);
2365 ret
= relocate_one_extent(root
, path
, &found_key
);
2368 btrfs_release_path(root
, path
);
2370 if (total_found
> 0) {
2371 trans
= btrfs_start_transaction(tree_root
, 1);
2372 btrfs_commit_transaction(trans
, tree_root
);
2374 mutex_unlock(&root
->fs_info
->fs_mutex
);
2375 btrfs_clean_old_snapshots(tree_root
);
2376 mutex_lock(&root
->fs_info
->fs_mutex
);
2378 trans
= btrfs_start_transaction(tree_root
, 1);
2379 btrfs_commit_transaction(trans
, tree_root
);
2383 trans
= btrfs_start_transaction(root
, 1);
2384 key
.objectid
= new_size
;
2390 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2394 leaf
= path
->nodes
[0];
2395 nritems
= btrfs_header_nritems(leaf
);
2397 if (path
->slots
[0] >= nritems
) {
2398 ret
= btrfs_next_leaf(root
, path
);
2405 leaf
= path
->nodes
[0];
2406 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2409 * btrfs_next_leaf doesn't cow buffers, we have to
2410 * do the search again
2412 memcpy(&key
, &found_key
, sizeof(key
));
2413 btrfs_release_path(root
, path
);
2417 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2418 if (btrfs_key_type(&found_key
) != BTRFS_BLOCK_GROUP_ITEM_KEY
) {
2419 printk("shrinker found key %Lu %u %Lu\n",
2420 found_key
.objectid
, found_key
.type
,
2425 ret
= get_state_private(&info
->block_group_cache
,
2426 found_key
.objectid
, &ptr
);
2428 kfree((void *)(unsigned long)ptr
);
2430 clear_extent_bits(&info
->block_group_cache
, found_key
.objectid
,
2431 found_key
.objectid
+ found_key
.offset
- 1,
2432 (unsigned int)-1, GFP_NOFS
);
2434 key
.objectid
= found_key
.objectid
+ 1;
2435 btrfs_del_item(trans
, root
, path
);
2436 btrfs_release_path(root
, path
);
2438 if (need_resched()) {
2439 mutex_unlock(&root
->fs_info
->fs_mutex
);
2441 mutex_lock(&root
->fs_info
->fs_mutex
);
2444 clear_extent_dirty(&info
->free_space_cache
, new_size
, (u64
)-1,
2446 btrfs_commit_transaction(trans
, root
);
2448 btrfs_free_path(path
);
2452 int btrfs_grow_extent_tree(struct btrfs_trans_handle
*trans
,
2453 struct btrfs_root
*root
, u64 new_size
)
2455 btrfs_set_super_total_bytes(&root
->fs_info
->super_copy
, new_size
);
2459 int find_first_block_group(struct btrfs_root
*root
, struct btrfs_path
*path
,
2460 struct btrfs_key
*key
)
2463 struct btrfs_key found_key
;
2464 struct extent_buffer
*leaf
;
2467 ret
= btrfs_search_slot(NULL
, root
, key
, path
, 0, 0);
2471 slot
= path
->slots
[0];
2472 leaf
= path
->nodes
[0];
2473 if (slot
>= btrfs_header_nritems(leaf
)) {
2474 ret
= btrfs_next_leaf(root
, path
);
2481 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2483 if (found_key
.objectid
>= key
->objectid
&&
2484 found_key
.type
== BTRFS_BLOCK_GROUP_ITEM_KEY
)
2493 int btrfs_read_block_groups(struct btrfs_root
*root
)
2495 struct btrfs_path
*path
;
2498 struct btrfs_block_group_cache
*cache
;
2499 struct btrfs_fs_info
*info
= root
->fs_info
;
2500 struct extent_io_tree
*block_group_cache
;
2501 struct btrfs_key key
;
2502 struct btrfs_key found_key
;
2503 struct extent_buffer
*leaf
;
2505 block_group_cache
= &info
->block_group_cache
;
2506 root
= info
->extent_root
;
2509 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2510 path
= btrfs_alloc_path();
2515 ret
= find_first_block_group(root
, path
, &key
);
2523 leaf
= path
->nodes
[0];
2524 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2525 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2531 read_extent_buffer(leaf
, &cache
->item
,
2532 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2533 sizeof(cache
->item
));
2534 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
2538 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
2539 btrfs_release_path(root
, path
);
2540 cache
->flags
= btrfs_block_group_flags(&cache
->item
);
2542 if (cache
->flags
& BTRFS_BLOCK_GROUP_DATA
) {
2543 bit
= BLOCK_GROUP_DATA
;
2544 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_SYSTEM
) {
2545 bit
= BLOCK_GROUP_SYSTEM
;
2546 } else if (cache
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2547 bit
= BLOCK_GROUP_METADATA
;
2550 /* use EXTENT_LOCKED to prevent merging */
2551 set_extent_bits(block_group_cache
, found_key
.objectid
,
2552 found_key
.objectid
+ found_key
.offset
- 1,
2553 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2554 set_state_private(block_group_cache
, found_key
.objectid
,
2555 (unsigned long)cache
);
2558 btrfs_super_total_bytes(&info
->super_copy
))
2563 btrfs_free_path(path
);