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"
28 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
29 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
30 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
32 static int finish_current_insert(struct btrfs_trans_handle
*trans
, struct
33 btrfs_root
*extent_root
);
34 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
35 btrfs_root
*extent_root
);
37 static int cache_block_group(struct btrfs_root
*root
,
38 struct btrfs_block_group_cache
*block_group
)
40 struct btrfs_path
*path
;
43 struct extent_buffer
*leaf
;
44 struct extent_map_tree
*free_space_cache
;
54 root
= root
->fs_info
->extent_root
;
55 free_space_cache
= &root
->fs_info
->free_space_cache
;
57 if (block_group
->cached
)
60 path
= btrfs_alloc_path();
65 first_free
= block_group
->key
.objectid
;
66 key
.objectid
= block_group
->key
.objectid
;
69 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
70 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
75 if (ret
&& path
->slots
[0] > 0)
79 leaf
= path
->nodes
[0];
80 slot
= path
->slots
[0];
81 if (slot
>= btrfs_header_nritems(leaf
)) {
82 ret
= btrfs_next_leaf(root
, path
);
92 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
93 if (key
.objectid
< block_group
->key
.objectid
) {
94 if (btrfs_key_type(&key
) != BTRFS_EXTENT_REF_KEY
&&
95 key
.objectid
+ key
.offset
> first_free
)
96 first_free
= key
.objectid
+ key
.offset
;
100 if (key
.objectid
>= block_group
->key
.objectid
+
101 block_group
->key
.offset
) {
105 if (btrfs_key_type(&key
) == BTRFS_EXTENT_ITEM_KEY
) {
110 if (key
.objectid
> last
) {
111 hole_size
= key
.objectid
- last
;
112 set_extent_dirty(free_space_cache
, last
,
113 last
+ hole_size
- 1,
116 last
= key
.objectid
+ key
.offset
;
124 if (block_group
->key
.objectid
+
125 block_group
->key
.offset
> last
) {
126 hole_size
= block_group
->key
.objectid
+
127 block_group
->key
.offset
- last
;
128 set_extent_dirty(free_space_cache
, last
,
129 last
+ hole_size
- 1, GFP_NOFS
);
131 block_group
->cached
= 1;
133 btrfs_free_path(path
);
137 struct btrfs_block_group_cache
*btrfs_lookup_block_group(struct
141 struct extent_map_tree
*block_group_cache
;
142 struct btrfs_block_group_cache
*block_group
= NULL
;
148 block_group_cache
= &info
->block_group_cache
;
149 ret
= find_first_extent_bit(block_group_cache
,
150 bytenr
, &start
, &end
,
151 BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
);
155 ret
= get_state_private(block_group_cache
, start
, &ptr
);
159 block_group
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
160 if (block_group
->key
.objectid
<= bytenr
&& bytenr
<
161 block_group
->key
.objectid
+ block_group
->key
.offset
)
166 static u64 noinline
find_search_start(struct btrfs_root
*root
,
167 struct btrfs_block_group_cache
**cache_ret
,
168 u64 search_start
, int num
,
169 int data
, int full_scan
)
172 struct btrfs_block_group_cache
*cache
= *cache_ret
;
183 ret
= cache_block_group(root
, cache
);
187 last
= max(search_start
, cache
->key
.objectid
);
190 ret
= find_first_extent_bit(&root
->fs_info
->free_space_cache
,
191 last
, &start
, &end
, EXTENT_DIRTY
);
198 start
= max(last
, start
);
200 if (last
- start
< num
) {
201 if (last
== cache
->key
.objectid
+ cache
->key
.offset
)
205 if (data
!= BTRFS_BLOCK_GROUP_MIXED
&&
206 start
+ num
> cache
->key
.objectid
+ cache
->key
.offset
)
211 cache
= btrfs_lookup_block_group(root
->fs_info
, search_start
);
213 printk("Unable to find block group for %Lu\n",
221 last
= cache
->key
.objectid
+ cache
->key
.offset
;
223 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
229 data
= BTRFS_BLOCK_GROUP_MIXED
;
234 if (cache_miss
&& !cache
->cached
) {
235 cache_block_group(root
, cache
);
237 cache
= btrfs_lookup_block_group(root
->fs_info
, last
);
239 cache
= btrfs_find_block_group(root
, cache
, last
, data
, 0);
247 static u64
div_factor(u64 num
, int factor
)
256 struct btrfs_block_group_cache
*btrfs_find_block_group(struct btrfs_root
*root
,
257 struct btrfs_block_group_cache
258 *hint
, u64 search_start
,
261 struct btrfs_block_group_cache
*cache
;
262 struct extent_map_tree
*block_group_cache
;
263 struct btrfs_block_group_cache
*found_group
= NULL
;
264 struct btrfs_fs_info
*info
= root
->fs_info
;
278 block_group_cache
= &info
->block_group_cache
;
283 if (data
== BTRFS_BLOCK_GROUP_MIXED
) {
284 bit
= BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
;
287 bit
= BLOCK_GROUP_DATA
;
289 bit
= BLOCK_GROUP_METADATA
;
292 struct btrfs_block_group_cache
*shint
;
293 shint
= btrfs_lookup_block_group(info
, search_start
);
294 if (shint
&& (shint
->data
== data
||
295 shint
->data
== BTRFS_BLOCK_GROUP_MIXED
)) {
296 used
= btrfs_block_group_used(&shint
->item
);
297 if (used
+ shint
->pinned
<
298 div_factor(shint
->key
.offset
, factor
)) {
303 if (hint
&& (hint
->data
== data
||
304 hint
->data
== BTRFS_BLOCK_GROUP_MIXED
)) {
305 used
= btrfs_block_group_used(&hint
->item
);
306 if (used
+ hint
->pinned
<
307 div_factor(hint
->key
.offset
, factor
)) {
310 last
= hint
->key
.objectid
+ hint
->key
.offset
;
314 hint_last
= max(hint
->key
.objectid
, search_start
);
316 hint_last
= search_start
;
322 ret
= find_first_extent_bit(block_group_cache
, last
,
327 ret
= get_state_private(block_group_cache
, start
, &ptr
);
331 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
332 last
= cache
->key
.objectid
+ cache
->key
.offset
;
333 used
= btrfs_block_group_used(&cache
->item
);
336 free_check
= cache
->key
.offset
;
338 free_check
= div_factor(cache
->key
.offset
, factor
);
339 if (used
+ cache
->pinned
< free_check
) {
352 bit
= BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
;
360 static u64
hash_extent_ref(u64 root_objectid
, u64 ref_generation
,
361 u64 owner
, u64 owner_offset
)
363 u32 high_crc
= ~(u32
)0;
364 u32 low_crc
= ~(u32
)0;
367 lenum
= cpu_to_le64(root_objectid
);
368 high_crc
= crc32c(high_crc
, &lenum
, sizeof(lenum
));
369 lenum
= cpu_to_le64(ref_generation
);
370 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
373 lenum
= cpu_to_le64(owner
);
374 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
375 lenum
= cpu_to_le64(owner_offset
);
376 low_crc
= crc32c(low_crc
, &lenum
, sizeof(lenum
));
378 return ((u64
)high_crc
<< 32) | (u64
)low_crc
;
381 static int match_extent_ref(struct extent_buffer
*leaf
,
382 struct btrfs_extent_ref
*disk_ref
,
383 struct btrfs_extent_ref
*cpu_ref
)
388 if (cpu_ref
->objectid
)
389 len
= sizeof(*cpu_ref
);
391 len
= 2 * sizeof(u64
);
392 ret
= memcmp_extent_buffer(leaf
, cpu_ref
, (unsigned long)disk_ref
,
397 static int noinline
lookup_extent_backref(struct btrfs_trans_handle
*trans
,
398 struct btrfs_root
*root
,
399 struct btrfs_path
*path
, u64 bytenr
,
401 u64 ref_generation
, u64 owner
,
402 u64 owner_offset
, int del
)
405 struct btrfs_key key
;
406 struct btrfs_key found_key
;
407 struct btrfs_extent_ref ref
;
408 struct extent_buffer
*leaf
;
409 struct btrfs_extent_ref
*disk_ref
;
413 btrfs_set_stack_ref_root(&ref
, root_objectid
);
414 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
415 btrfs_set_stack_ref_objectid(&ref
, owner
);
416 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
418 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
421 key
.objectid
= bytenr
;
422 key
.type
= BTRFS_EXTENT_REF_KEY
;
425 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
429 leaf
= path
->nodes
[0];
431 u32 nritems
= btrfs_header_nritems(leaf
);
432 if (path
->slots
[0] >= nritems
) {
433 ret2
= btrfs_next_leaf(root
, path
);
436 leaf
= path
->nodes
[0];
438 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
439 if (found_key
.objectid
!= bytenr
||
440 found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
442 key
.offset
= found_key
.offset
;
444 btrfs_release_path(root
, path
);
448 disk_ref
= btrfs_item_ptr(path
->nodes
[0],
450 struct btrfs_extent_ref
);
451 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
)) {
455 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
456 key
.offset
= found_key
.offset
+ 1;
457 btrfs_release_path(root
, path
);
464 * Back reference rules. Back refs have three main goals:
466 * 1) differentiate between all holders of references to an extent so that
467 * when a reference is dropped we can make sure it was a valid reference
468 * before freeing the extent.
470 * 2) Provide enough information to quickly find the holders of an extent
471 * if we notice a given block is corrupted or bad.
473 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
474 * maintenance. This is actually the same as #2, but with a slightly
475 * different use case.
477 * File extents can be referenced by:
479 * - multiple snapshots, subvolumes, or different generations in one subvol
480 * - different files inside a single subvolume (in theory, not implemented yet)
481 * - different offsets inside a file (bookend extents in file.c)
483 * The extent ref structure has fields for:
485 * - Objectid of the subvolume root
486 * - Generation number of the tree holding the reference
487 * - objectid of the file holding the reference
488 * - offset in the file corresponding to the key holding the reference
490 * When a file extent is allocated the fields are filled in:
491 * (root_key.objectid, trans->transid, inode objectid, offset in file)
493 * When a leaf is cow'd new references are added for every file extent found
494 * in the leaf. It looks the same as the create case, but trans->transid
495 * will be different when the block is cow'd.
497 * (root_key.objectid, trans->transid, inode objectid, offset in file)
499 * When a file extent is removed either during snapshot deletion or file
500 * truncation, the corresponding back reference is found
503 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
504 * inode objectid, offset in file)
506 * Btree extents can be referenced by:
508 * - Different subvolumes
509 * - Different generations of the same subvolume
511 * Storing sufficient information for a full reverse mapping of a btree
512 * block would require storing the lowest key of the block in the backref,
513 * and it would require updating that lowest key either before write out or
514 * every time it changed. Instead, the objectid of the lowest key is stored
515 * along with the level of the tree block. This provides a hint
516 * about where in the btree the block can be found. Searches through the
517 * btree only need to look for a pointer to that block, so they stop one
518 * level higher than the level recorded in the backref.
520 * Some btrees do not do reference counting on their extents. These
521 * include the extent tree and the tree of tree roots. Backrefs for these
522 * trees always have a generation of zero.
524 * When a tree block is created, back references are inserted:
526 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
528 * When a tree block is cow'd in a reference counted root,
529 * new back references are added for all the blocks it points to.
530 * These are of the form (trans->transid will have increased since creation):
532 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
534 * Because the lowest_key_objectid and the level are just hints
535 * they are not used when backrefs are deleted. When a backref is deleted:
537 * if backref was for a tree root:
538 * root_objectid = root->root_key.objectid
540 * root_objectid = btrfs_header_owner(parent)
542 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
544 * Back Reference Key hashing:
546 * Back references have four fields, each 64 bits long. Unfortunately,
547 * This is hashed into a single 64 bit number and placed into the key offset.
548 * The key objectid corresponds to the first byte in the extent, and the
549 * key type is set to BTRFS_EXTENT_REF_KEY
551 int btrfs_insert_extent_backref(struct btrfs_trans_handle
*trans
,
552 struct btrfs_root
*root
,
553 struct btrfs_path
*path
, u64 bytenr
,
554 u64 root_objectid
, u64 ref_generation
,
555 u64 owner
, u64 owner_offset
)
558 struct btrfs_key key
;
559 struct btrfs_extent_ref ref
;
560 struct btrfs_extent_ref
*disk_ref
;
563 btrfs_set_stack_ref_root(&ref
, root_objectid
);
564 btrfs_set_stack_ref_generation(&ref
, ref_generation
);
565 btrfs_set_stack_ref_objectid(&ref
, owner
);
566 btrfs_set_stack_ref_offset(&ref
, owner_offset
);
568 hash
= hash_extent_ref(root_objectid
, ref_generation
, owner
,
571 key
.objectid
= bytenr
;
572 key
.type
= BTRFS_EXTENT_REF_KEY
;
574 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
, sizeof(ref
));
575 while (ret
== -EEXIST
) {
576 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
577 struct btrfs_extent_ref
);
578 if (match_extent_ref(path
->nodes
[0], disk_ref
, &ref
))
581 btrfs_release_path(root
, path
);
582 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
587 disk_ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
588 struct btrfs_extent_ref
);
589 write_extent_buffer(path
->nodes
[0], &ref
, (unsigned long)disk_ref
,
591 btrfs_mark_buffer_dirty(path
->nodes
[0]);
593 btrfs_release_path(root
, path
);
597 int btrfs_inc_extent_ref(struct btrfs_trans_handle
*trans
,
598 struct btrfs_root
*root
,
599 u64 bytenr
, u64 num_bytes
,
600 u64 root_objectid
, u64 ref_generation
,
601 u64 owner
, u64 owner_offset
)
603 struct btrfs_path
*path
;
605 struct btrfs_key key
;
606 struct extent_buffer
*l
;
607 struct btrfs_extent_item
*item
;
610 WARN_ON(num_bytes
< root
->sectorsize
);
611 path
= btrfs_alloc_path();
615 key
.objectid
= bytenr
;
616 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
617 key
.offset
= num_bytes
;
618 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
627 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
628 refs
= btrfs_extent_refs(l
, item
);
629 btrfs_set_extent_refs(l
, item
, refs
+ 1);
630 btrfs_mark_buffer_dirty(path
->nodes
[0]);
632 btrfs_release_path(root
->fs_info
->extent_root
, path
);
634 ret
= btrfs_insert_extent_backref(trans
, root
->fs_info
->extent_root
,
635 path
, bytenr
, root_objectid
,
636 ref_generation
, owner
, owner_offset
);
638 finish_current_insert(trans
, root
->fs_info
->extent_root
);
639 del_pending_extents(trans
, root
->fs_info
->extent_root
);
641 btrfs_free_path(path
);
645 int btrfs_extent_post_op(struct btrfs_trans_handle
*trans
,
646 struct btrfs_root
*root
)
648 finish_current_insert(trans
, root
->fs_info
->extent_root
);
649 del_pending_extents(trans
, root
->fs_info
->extent_root
);
653 static int lookup_extent_ref(struct btrfs_trans_handle
*trans
,
654 struct btrfs_root
*root
, u64 bytenr
,
655 u64 num_bytes
, u32
*refs
)
657 struct btrfs_path
*path
;
659 struct btrfs_key key
;
660 struct extent_buffer
*l
;
661 struct btrfs_extent_item
*item
;
663 WARN_ON(num_bytes
< root
->sectorsize
);
664 path
= btrfs_alloc_path();
665 key
.objectid
= bytenr
;
666 key
.offset
= num_bytes
;
667 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
668 ret
= btrfs_search_slot(trans
, root
->fs_info
->extent_root
, &key
, path
,
673 btrfs_print_leaf(root
, path
->nodes
[0]);
674 printk("failed to find block number %Lu\n", bytenr
);
678 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
679 *refs
= btrfs_extent_refs(l
, item
);
681 btrfs_free_path(path
);
685 u32
btrfs_count_snapshots_in_path(struct btrfs_root
*root
,
686 struct btrfs_path
*count_path
,
689 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
690 struct btrfs_path
*path
;
693 u64 root_objectid
= root
->root_key
.objectid
;
698 struct btrfs_key key
;
699 struct btrfs_key found_key
;
700 struct extent_buffer
*l
;
701 struct btrfs_extent_item
*item
;
702 struct btrfs_extent_ref
*ref_item
;
705 path
= btrfs_alloc_path();
708 bytenr
= first_extent
;
710 bytenr
= count_path
->nodes
[level
]->start
;
713 key
.objectid
= bytenr
;
716 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
717 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
723 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
725 if (found_key
.objectid
!= bytenr
||
726 found_key
.type
!= BTRFS_EXTENT_ITEM_KEY
) {
730 item
= btrfs_item_ptr(l
, path
->slots
[0], struct btrfs_extent_item
);
733 nritems
= btrfs_header_nritems(l
);
734 if (path
->slots
[0] >= nritems
) {
735 ret
= btrfs_next_leaf(extent_root
, path
);
740 btrfs_item_key_to_cpu(l
, &found_key
, path
->slots
[0]);
741 if (found_key
.objectid
!= bytenr
)
744 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
) {
750 ref_item
= btrfs_item_ptr(l
, path
->slots
[0],
751 struct btrfs_extent_ref
);
752 found_objectid
= btrfs_ref_root(l
, ref_item
);
754 if (found_objectid
!= root_objectid
) {
761 if (cur_count
== 0) {
765 if (level
>= 0 && root
->node
== count_path
->nodes
[level
])
768 btrfs_release_path(root
, path
);
772 btrfs_free_path(path
);
777 int btrfs_inc_root_ref(struct btrfs_trans_handle
*trans
,
778 struct btrfs_root
*root
, u64 owner_objectid
)
784 struct btrfs_disk_key disk_key
;
786 level
= btrfs_header_level(root
->node
);
787 generation
= trans
->transid
;
788 nritems
= btrfs_header_nritems(root
->node
);
791 btrfs_item_key(root
->node
, &disk_key
, 0);
793 btrfs_node_key(root
->node
, &disk_key
, 0);
794 key_objectid
= btrfs_disk_key_objectid(&disk_key
);
798 return btrfs_inc_extent_ref(trans
, root
, root
->node
->start
,
799 root
->node
->len
, owner_objectid
,
800 generation
, level
, key_objectid
);
803 int btrfs_inc_ref(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
804 struct extent_buffer
*buf
)
808 struct btrfs_key key
;
809 struct btrfs_file_extent_item
*fi
;
818 level
= btrfs_header_level(buf
);
819 nritems
= btrfs_header_nritems(buf
);
820 for (i
= 0; i
< nritems
; i
++) {
823 btrfs_item_key_to_cpu(buf
, &key
, i
);
824 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
826 fi
= btrfs_item_ptr(buf
, i
,
827 struct btrfs_file_extent_item
);
828 if (btrfs_file_extent_type(buf
, fi
) ==
829 BTRFS_FILE_EXTENT_INLINE
)
831 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
832 if (disk_bytenr
== 0)
834 ret
= btrfs_inc_extent_ref(trans
, root
, disk_bytenr
,
835 btrfs_file_extent_disk_num_bytes(buf
, fi
),
836 root
->root_key
.objectid
, trans
->transid
,
837 key
.objectid
, key
.offset
);
843 bytenr
= btrfs_node_blockptr(buf
, i
);
844 btrfs_node_key_to_cpu(buf
, &key
, i
);
845 ret
= btrfs_inc_extent_ref(trans
, root
, bytenr
,
846 btrfs_level_size(root
, level
- 1),
847 root
->root_key
.objectid
,
849 level
- 1, key
.objectid
);
860 for (i
=0; i
< faili
; i
++) {
863 btrfs_item_key_to_cpu(buf
, &key
, i
);
864 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
866 fi
= btrfs_item_ptr(buf
, i
,
867 struct btrfs_file_extent_item
);
868 if (btrfs_file_extent_type(buf
, fi
) ==
869 BTRFS_FILE_EXTENT_INLINE
)
871 disk_bytenr
= btrfs_file_extent_disk_bytenr(buf
, fi
);
872 if (disk_bytenr
== 0)
874 err
= btrfs_free_extent(trans
, root
, disk_bytenr
,
875 btrfs_file_extent_disk_num_bytes(buf
,
879 bytenr
= btrfs_node_blockptr(buf
, i
);
880 err
= btrfs_free_extent(trans
, root
, bytenr
,
881 btrfs_level_size(root
, level
- 1), 0);
889 static int write_one_cache_group(struct btrfs_trans_handle
*trans
,
890 struct btrfs_root
*root
,
891 struct btrfs_path
*path
,
892 struct btrfs_block_group_cache
*cache
)
896 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
898 struct extent_buffer
*leaf
;
900 ret
= btrfs_search_slot(trans
, extent_root
, &cache
->key
, path
, 0, 1);
905 leaf
= path
->nodes
[0];
906 bi
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
907 write_extent_buffer(leaf
, &cache
->item
, bi
, sizeof(cache
->item
));
908 btrfs_mark_buffer_dirty(leaf
);
909 btrfs_release_path(extent_root
, path
);
911 finish_current_insert(trans
, extent_root
);
912 pending_ret
= del_pending_extents(trans
, extent_root
);
921 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle
*trans
,
922 struct btrfs_root
*root
)
924 struct extent_map_tree
*block_group_cache
;
925 struct btrfs_block_group_cache
*cache
;
929 struct btrfs_path
*path
;
935 block_group_cache
= &root
->fs_info
->block_group_cache
;
936 path
= btrfs_alloc_path();
941 ret
= find_first_extent_bit(block_group_cache
, last
,
942 &start
, &end
, BLOCK_GROUP_DIRTY
);
947 ret
= get_state_private(block_group_cache
, start
, &ptr
);
951 cache
= (struct btrfs_block_group_cache
*)(unsigned long)ptr
;
952 err
= write_one_cache_group(trans
, root
,
955 * if we fail to write the cache group, we want
956 * to keep it marked dirty in hopes that a later
963 clear_extent_bits(block_group_cache
, start
, end
,
964 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
966 btrfs_free_path(path
);
970 static int update_block_group(struct btrfs_trans_handle
*trans
,
971 struct btrfs_root
*root
,
972 u64 bytenr
, u64 num_bytes
, int alloc
,
973 int mark_free
, int data
)
975 struct btrfs_block_group_cache
*cache
;
976 struct btrfs_fs_info
*info
= root
->fs_info
;
977 u64 total
= num_bytes
;
984 cache
= btrfs_lookup_block_group(info
, bytenr
);
988 byte_in_group
= bytenr
- cache
->key
.objectid
;
989 WARN_ON(byte_in_group
> cache
->key
.offset
);
990 start
= cache
->key
.objectid
;
991 end
= start
+ cache
->key
.offset
- 1;
992 set_extent_bits(&info
->block_group_cache
, start
, end
,
993 BLOCK_GROUP_DIRTY
, GFP_NOFS
);
995 old_val
= btrfs_block_group_used(&cache
->item
);
996 num_bytes
= min(total
, cache
->key
.offset
- byte_in_group
);
998 if (cache
->data
!= data
&&
999 old_val
< (cache
->key
.offset
>> 1)) {
1004 bit_to_clear
= BLOCK_GROUP_METADATA
;
1005 bit_to_set
= BLOCK_GROUP_DATA
;
1006 cache
->item
.flags
&=
1007 ~BTRFS_BLOCK_GROUP_MIXED
;
1008 cache
->item
.flags
|=
1009 BTRFS_BLOCK_GROUP_DATA
;
1011 bit_to_clear
= BLOCK_GROUP_DATA
;
1012 bit_to_set
= BLOCK_GROUP_METADATA
;
1013 cache
->item
.flags
&=
1014 ~BTRFS_BLOCK_GROUP_MIXED
;
1015 cache
->item
.flags
&=
1016 ~BTRFS_BLOCK_GROUP_DATA
;
1018 clear_extent_bits(&info
->block_group_cache
,
1019 start
, end
, bit_to_clear
,
1021 set_extent_bits(&info
->block_group_cache
,
1022 start
, end
, bit_to_set
,
1024 } else if (cache
->data
!= data
&&
1025 cache
->data
!= BTRFS_BLOCK_GROUP_MIXED
) {
1026 cache
->data
= BTRFS_BLOCK_GROUP_MIXED
;
1027 set_extent_bits(&info
->block_group_cache
,
1030 BLOCK_GROUP_METADATA
,
1033 old_val
+= num_bytes
;
1035 old_val
-= num_bytes
;
1037 set_extent_dirty(&info
->free_space_cache
,
1038 bytenr
, bytenr
+ num_bytes
- 1,
1042 btrfs_set_block_group_used(&cache
->item
, old_val
);
1044 bytenr
+= num_bytes
;
1048 static int update_pinned_extents(struct btrfs_root
*root
,
1049 u64 bytenr
, u64 num
, int pin
)
1052 struct btrfs_block_group_cache
*cache
;
1053 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1056 set_extent_dirty(&fs_info
->pinned_extents
,
1057 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1059 clear_extent_dirty(&fs_info
->pinned_extents
,
1060 bytenr
, bytenr
+ num
- 1, GFP_NOFS
);
1063 cache
= btrfs_lookup_block_group(fs_info
, bytenr
);
1065 len
= min(num
, cache
->key
.offset
-
1066 (bytenr
- cache
->key
.objectid
));
1068 cache
->pinned
+= len
;
1069 fs_info
->total_pinned
+= len
;
1071 cache
->pinned
-= len
;
1072 fs_info
->total_pinned
-= len
;
1080 int btrfs_copy_pinned(struct btrfs_root
*root
, struct extent_map_tree
*copy
)
1085 struct extent_map_tree
*pinned_extents
= &root
->fs_info
->pinned_extents
;
1089 ret
= find_first_extent_bit(pinned_extents
, last
,
1090 &start
, &end
, EXTENT_DIRTY
);
1093 set_extent_dirty(copy
, start
, end
, GFP_NOFS
);
1099 int btrfs_finish_extent_commit(struct btrfs_trans_handle
*trans
,
1100 struct btrfs_root
*root
,
1101 struct extent_map_tree
*unpin
)
1106 struct extent_map_tree
*free_space_cache
;
1107 free_space_cache
= &root
->fs_info
->free_space_cache
;
1110 ret
= find_first_extent_bit(unpin
, 0, &start
, &end
,
1114 update_pinned_extents(root
, start
, end
+ 1 - start
, 0);
1115 clear_extent_dirty(unpin
, start
, end
, GFP_NOFS
);
1116 set_extent_dirty(free_space_cache
, start
, end
, GFP_NOFS
);
1121 static int finish_current_insert(struct btrfs_trans_handle
*trans
,
1122 struct btrfs_root
*extent_root
)
1126 struct btrfs_fs_info
*info
= extent_root
->fs_info
;
1127 struct extent_buffer
*eb
;
1128 struct btrfs_path
*path
;
1129 struct btrfs_key ins
;
1130 struct btrfs_disk_key first
;
1131 struct btrfs_extent_item extent_item
;
1136 btrfs_set_stack_extent_refs(&extent_item
, 1);
1137 btrfs_set_key_type(&ins
, BTRFS_EXTENT_ITEM_KEY
);
1138 path
= btrfs_alloc_path();
1141 ret
= find_first_extent_bit(&info
->extent_ins
, 0, &start
,
1142 &end
, EXTENT_LOCKED
);
1146 ins
.objectid
= start
;
1147 ins
.offset
= end
+ 1 - start
;
1148 err
= btrfs_insert_item(trans
, extent_root
, &ins
,
1149 &extent_item
, sizeof(extent_item
));
1150 clear_extent_bits(&info
->extent_ins
, start
, end
, EXTENT_LOCKED
,
1152 eb
= read_tree_block(extent_root
, ins
.objectid
, ins
.offset
);
1153 level
= btrfs_header_level(eb
);
1155 btrfs_item_key(eb
, &first
, 0);
1157 btrfs_node_key(eb
, &first
, 0);
1159 err
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1160 start
, extent_root
->root_key
.objectid
,
1162 btrfs_disk_key_objectid(&first
));
1164 free_extent_buffer(eb
);
1166 btrfs_free_path(path
);
1170 static int pin_down_bytes(struct btrfs_root
*root
, u64 bytenr
, u32 num_bytes
,
1174 struct extent_buffer
*buf
;
1177 buf
= btrfs_find_tree_block(root
, bytenr
, num_bytes
);
1179 if (btrfs_buffer_uptodate(buf
)) {
1181 root
->fs_info
->running_transaction
->transid
;
1182 if (btrfs_header_generation(buf
) == transid
) {
1183 free_extent_buffer(buf
);
1187 free_extent_buffer(buf
);
1189 update_pinned_extents(root
, bytenr
, num_bytes
, 1);
1191 set_extent_bits(&root
->fs_info
->pending_del
,
1192 bytenr
, bytenr
+ num_bytes
- 1,
1193 EXTENT_LOCKED
, GFP_NOFS
);
1200 * remove an extent from the root, returns 0 on success
1202 static int __free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1203 *root
, u64 bytenr
, u64 num_bytes
,
1204 u64 root_objectid
, u64 ref_generation
,
1205 u64 owner_objectid
, u64 owner_offset
, int pin
,
1208 struct btrfs_path
*path
;
1209 struct btrfs_key key
;
1210 struct btrfs_fs_info
*info
= root
->fs_info
;
1211 struct btrfs_root
*extent_root
= info
->extent_root
;
1212 struct extent_buffer
*leaf
;
1214 struct btrfs_extent_item
*ei
;
1217 key
.objectid
= bytenr
;
1218 btrfs_set_key_type(&key
, BTRFS_EXTENT_ITEM_KEY
);
1219 key
.offset
= num_bytes
;
1221 path
= btrfs_alloc_path();
1225 ret
= lookup_extent_backref(trans
, extent_root
, path
,
1226 bytenr
, root_objectid
,
1228 owner_objectid
, owner_offset
, 1);
1230 ret
= btrfs_del_item(trans
, extent_root
, path
);
1232 btrfs_print_leaf(extent_root
, path
->nodes
[0]);
1234 printk("Unable to find ref byte nr %Lu root %Lu "
1235 " gen %Lu owner %Lu offset %Lu\n", bytenr
,
1236 root_objectid
, ref_generation
, owner_objectid
,
1239 btrfs_release_path(extent_root
, path
);
1240 ret
= btrfs_search_slot(trans
, extent_root
, &key
, path
, -1, 1);
1245 leaf
= path
->nodes
[0];
1246 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
1247 struct btrfs_extent_item
);
1248 refs
= btrfs_extent_refs(leaf
, ei
);
1251 btrfs_set_extent_refs(leaf
, ei
, refs
);
1252 btrfs_mark_buffer_dirty(leaf
);
1259 ret
= pin_down_bytes(root
, bytenr
, num_bytes
, 0);
1265 /* block accounting for super block */
1266 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1267 btrfs_set_super_bytes_used(&info
->super_copy
,
1268 super_used
- num_bytes
);
1270 /* block accounting for root item */
1271 root_used
= btrfs_root_used(&root
->root_item
);
1272 btrfs_set_root_used(&root
->root_item
,
1273 root_used
- num_bytes
);
1275 ret
= btrfs_del_item(trans
, extent_root
, path
);
1279 ret
= update_block_group(trans
, root
, bytenr
, num_bytes
, 0,
1283 btrfs_free_path(path
);
1284 finish_current_insert(trans
, extent_root
);
1289 * find all the blocks marked as pending in the radix tree and remove
1290 * them from the extent map
1292 static int del_pending_extents(struct btrfs_trans_handle
*trans
, struct
1293 btrfs_root
*extent_root
)
1299 struct extent_map_tree
*pending_del
;
1300 struct extent_map_tree
*pinned_extents
;
1302 pending_del
= &extent_root
->fs_info
->pending_del
;
1303 pinned_extents
= &extent_root
->fs_info
->pinned_extents
;
1306 ret
= find_first_extent_bit(pending_del
, 0, &start
, &end
,
1310 update_pinned_extents(extent_root
, start
, end
+ 1 - start
, 1);
1311 clear_extent_bits(pending_del
, start
, end
, EXTENT_LOCKED
,
1313 ret
= __free_extent(trans
, extent_root
,
1314 start
, end
+ 1 - start
,
1315 extent_root
->root_key
.objectid
,
1324 * remove an extent from the root, returns 0 on success
1326 int btrfs_free_extent(struct btrfs_trans_handle
*trans
, struct btrfs_root
1327 *root
, u64 bytenr
, u64 num_bytes
,
1328 u64 root_objectid
, u64 ref_generation
,
1329 u64 owner_objectid
, u64 owner_offset
, int pin
)
1331 struct btrfs_root
*extent_root
= root
->fs_info
->extent_root
;
1335 WARN_ON(num_bytes
< root
->sectorsize
);
1336 if (!root
->ref_cows
)
1339 if (root
== extent_root
) {
1340 pin_down_bytes(root
, bytenr
, num_bytes
, 1);
1343 ret
= __free_extent(trans
, root
, bytenr
, num_bytes
, root_objectid
,
1344 ref_generation
, owner_objectid
, owner_offset
,
1346 pending_ret
= del_pending_extents(trans
, root
->fs_info
->extent_root
);
1347 return ret
? ret
: pending_ret
;
1350 static u64
stripe_align(struct btrfs_root
*root
, u64 val
)
1352 u64 mask
= ((u64
)root
->stripesize
- 1);
1353 u64 ret
= (val
+ mask
) & ~mask
;
1358 * walks the btree of allocated extents and find a hole of a given size.
1359 * The key ins is changed to record the hole:
1360 * ins->objectid == block start
1361 * ins->flags = BTRFS_EXTENT_ITEM_KEY
1362 * ins->offset == number of blocks
1363 * Any available blocks before search_start are skipped.
1365 static int noinline
find_free_extent(struct btrfs_trans_handle
*trans
,
1366 struct btrfs_root
*orig_root
,
1367 u64 num_bytes
, u64 empty_size
,
1368 u64 search_start
, u64 search_end
,
1369 u64 hint_byte
, struct btrfs_key
*ins
,
1370 u64 exclude_start
, u64 exclude_nr
,
1373 struct btrfs_path
*path
;
1374 struct btrfs_key key
;
1380 u64 orig_search_start
= search_start
;
1382 struct extent_buffer
*l
;
1383 struct btrfs_root
* root
= orig_root
->fs_info
->extent_root
;
1384 struct btrfs_fs_info
*info
= root
->fs_info
;
1385 u64 total_needed
= num_bytes
;
1387 struct btrfs_block_group_cache
*block_group
;
1392 WARN_ON(num_bytes
< root
->sectorsize
);
1393 btrfs_set_key_type(ins
, BTRFS_EXTENT_ITEM_KEY
);
1395 level
= btrfs_header_level(root
->node
);
1397 if (num_bytes
>= 32 * 1024 * 1024 && hint_byte
) {
1398 data
= BTRFS_BLOCK_GROUP_MIXED
;
1401 if (search_end
== (u64
)-1)
1402 search_end
= btrfs_super_total_bytes(&info
->super_copy
);
1404 block_group
= btrfs_lookup_block_group(info
, hint_byte
);
1406 hint_byte
= search_start
;
1407 block_group
= btrfs_find_block_group(root
, block_group
,
1408 hint_byte
, data
, 1);
1410 block_group
= btrfs_find_block_group(root
,
1412 search_start
, data
, 1);
1415 total_needed
+= empty_size
;
1416 path
= btrfs_alloc_path();
1419 block_group
= btrfs_lookup_block_group(info
, search_start
);
1421 block_group
= btrfs_lookup_block_group(info
,
1424 search_start
= find_search_start(root
, &block_group
, search_start
,
1425 total_needed
, data
, full_scan
);
1426 search_start
= stripe_align(root
, search_start
);
1427 cached_start
= search_start
;
1428 btrfs_init_path(path
);
1429 ins
->objectid
= search_start
;
1434 ret
= btrfs_search_slot(trans
, root
, ins
, path
, 0, 0);
1438 if (path
->slots
[0] > 0) {
1443 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1446 * walk backwards to find the first extent item key
1448 while(btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
) {
1449 if (path
->slots
[0] == 0) {
1450 ret
= btrfs_prev_leaf(root
, path
);
1452 ret
= btrfs_search_slot(trans
, root
, ins
,
1456 if (path
->slots
[0] > 0)
1464 btrfs_item_key_to_cpu(l
, &key
, path
->slots
[0]);
1468 slot
= path
->slots
[0];
1469 if (slot
>= btrfs_header_nritems(l
)) {
1470 ret
= btrfs_next_leaf(root
, path
);
1476 search_start
= max(search_start
,
1477 block_group
->key
.objectid
);
1479 aligned
= stripe_align(root
, search_start
);
1480 ins
->objectid
= aligned
;
1481 if (aligned
>= search_end
) {
1485 ins
->offset
= search_end
- aligned
;
1489 ins
->objectid
= stripe_align(root
,
1490 last_byte
> search_start
?
1491 last_byte
: search_start
);
1492 if (search_end
<= ins
->objectid
) {
1496 ins
->offset
= search_end
- ins
->objectid
;
1497 BUG_ON(ins
->objectid
>= search_end
);
1500 btrfs_item_key_to_cpu(l
, &key
, slot
);
1502 if (key
.objectid
>= search_start
&& key
.objectid
> last_byte
&&
1504 if (last_byte
< search_start
)
1505 last_byte
= search_start
;
1506 aligned
= stripe_align(root
, last_byte
);
1507 hole_size
= key
.objectid
- aligned
;
1508 if (key
.objectid
> aligned
&& hole_size
>= num_bytes
) {
1509 ins
->objectid
= aligned
;
1510 ins
->offset
= hole_size
;
1514 if (btrfs_key_type(&key
) != BTRFS_EXTENT_ITEM_KEY
) {
1515 if (!start_found
&& btrfs_key_type(&key
) ==
1516 BTRFS_BLOCK_GROUP_ITEM_KEY
) {
1517 last_byte
= key
.objectid
;
1525 last_byte
= key
.objectid
+ key
.offset
;
1527 if (!full_scan
&& data
!= BTRFS_BLOCK_GROUP_MIXED
&&
1528 last_byte
>= block_group
->key
.objectid
+
1529 block_group
->key
.offset
) {
1530 btrfs_release_path(root
, path
);
1531 search_start
= block_group
->key
.objectid
+
1532 block_group
->key
.offset
;
1540 /* we have to make sure we didn't find an extent that has already
1541 * been allocated by the map tree or the original allocation
1543 btrfs_release_path(root
, path
);
1544 BUG_ON(ins
->objectid
< search_start
);
1546 if (ins
->objectid
+ num_bytes
>= search_end
)
1548 if (!full_scan
&& data
!= BTRFS_BLOCK_GROUP_MIXED
&&
1549 ins
->objectid
+ num_bytes
> block_group
->
1550 key
.objectid
+ block_group
->key
.offset
) {
1551 search_start
= block_group
->key
.objectid
+
1552 block_group
->key
.offset
;
1555 if (test_range_bit(&info
->extent_ins
, ins
->objectid
,
1556 ins
->objectid
+ num_bytes
-1, EXTENT_LOCKED
, 0)) {
1557 search_start
= ins
->objectid
+ num_bytes
;
1560 if (test_range_bit(&info
->pinned_extents
, ins
->objectid
,
1561 ins
->objectid
+ num_bytes
-1, EXTENT_DIRTY
, 0)) {
1562 search_start
= ins
->objectid
+ num_bytes
;
1565 if (exclude_nr
> 0 && (ins
->objectid
+ num_bytes
> exclude_start
&&
1566 ins
->objectid
< exclude_start
+ exclude_nr
)) {
1567 search_start
= exclude_start
+ exclude_nr
;
1571 block_group
= btrfs_lookup_block_group(info
, ins
->objectid
);
1573 trans
->block_group
= block_group
;
1575 ins
->offset
= num_bytes
;
1576 btrfs_free_path(path
);
1580 if (search_start
+ num_bytes
>= search_end
) {
1582 search_start
= orig_search_start
;
1589 total_needed
-= empty_size
;
1591 data
= BTRFS_BLOCK_GROUP_MIXED
;
1595 block_group
= btrfs_lookup_block_group(info
, search_start
);
1597 block_group
= btrfs_find_block_group(root
, block_group
,
1598 search_start
, data
, 0);
1602 btrfs_release_path(root
, path
);
1603 btrfs_free_path(path
);
1607 * finds a free extent and does all the dirty work required for allocation
1608 * returns the key for the extent through ins, and a tree buffer for
1609 * the first block of the extent through buf.
1611 * returns 0 if everything worked, non-zero otherwise.
1613 int btrfs_alloc_extent(struct btrfs_trans_handle
*trans
,
1614 struct btrfs_root
*root
,
1615 u64 num_bytes
, u64 root_objectid
, u64 ref_generation
,
1616 u64 owner
, u64 owner_offset
,
1617 u64 empty_size
, u64 hint_byte
,
1618 u64 search_end
, struct btrfs_key
*ins
, int data
)
1622 u64 super_used
, root_used
;
1623 u64 search_start
= 0;
1625 struct btrfs_fs_info
*info
= root
->fs_info
;
1626 struct btrfs_root
*extent_root
= info
->extent_root
;
1627 struct btrfs_extent_item extent_item
;
1628 struct btrfs_path
*path
;
1630 btrfs_set_stack_extent_refs(&extent_item
, 1);
1632 new_hint
= max(hint_byte
, root
->fs_info
->alloc_start
);
1633 if (new_hint
< btrfs_super_total_bytes(&info
->super_copy
))
1634 hint_byte
= new_hint
;
1636 WARN_ON(num_bytes
< root
->sectorsize
);
1637 ret
= find_free_extent(trans
, root
, num_bytes
, empty_size
,
1638 search_start
, search_end
, hint_byte
, ins
,
1639 trans
->alloc_exclude_start
,
1640 trans
->alloc_exclude_nr
, data
);
1645 /* block accounting for super block */
1646 super_used
= btrfs_super_bytes_used(&info
->super_copy
);
1647 btrfs_set_super_bytes_used(&info
->super_copy
, super_used
+ num_bytes
);
1649 /* block accounting for root item */
1650 root_used
= btrfs_root_used(&root
->root_item
);
1651 btrfs_set_root_used(&root
->root_item
, root_used
+ num_bytes
);
1653 clear_extent_dirty(&root
->fs_info
->free_space_cache
,
1654 ins
->objectid
, ins
->objectid
+ ins
->offset
- 1,
1657 if (root
== extent_root
) {
1658 set_extent_bits(&root
->fs_info
->extent_ins
, ins
->objectid
,
1659 ins
->objectid
+ ins
->offset
- 1,
1660 EXTENT_LOCKED
, GFP_NOFS
);
1665 WARN_ON(trans
->alloc_exclude_nr
);
1666 trans
->alloc_exclude_start
= ins
->objectid
;
1667 trans
->alloc_exclude_nr
= ins
->offset
;
1668 ret
= btrfs_insert_item(trans
, extent_root
, ins
, &extent_item
,
1669 sizeof(extent_item
));
1671 trans
->alloc_exclude_start
= 0;
1672 trans
->alloc_exclude_nr
= 0;
1675 path
= btrfs_alloc_path();
1677 ret
= btrfs_insert_extent_backref(trans
, extent_root
, path
,
1678 ins
->objectid
, root_objectid
,
1679 ref_generation
, owner
, owner_offset
);
1682 btrfs_free_path(path
);
1683 finish_current_insert(trans
, extent_root
);
1684 pending_ret
= del_pending_extents(trans
, extent_root
);
1694 ret
= update_block_group(trans
, root
, ins
->objectid
, ins
->offset
, 1, 0,
1701 * helper function to allocate a block for a given tree
1702 * returns the tree buffer or NULL.
1704 struct extent_buffer
*btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1705 struct btrfs_root
*root
,
1707 u64 root_objectid
, u64 hint
,
1713 ref_generation
= trans
->transid
;
1718 return __btrfs_alloc_free_block(trans
, root
, blocksize
, root_objectid
,
1719 ref_generation
, 0, 0, hint
, empty_size
);
1723 * helper function to allocate a block for a given tree
1724 * returns the tree buffer or NULL.
1726 struct extent_buffer
*__btrfs_alloc_free_block(struct btrfs_trans_handle
*trans
,
1727 struct btrfs_root
*root
,
1736 struct btrfs_key ins
;
1738 struct extent_buffer
*buf
;
1740 ret
= btrfs_alloc_extent(trans
, root
, blocksize
,
1741 root_objectid
, ref_generation
,
1742 level
, first_objectid
, empty_size
, hint
,
1746 return ERR_PTR(ret
);
1748 buf
= btrfs_find_create_tree_block(root
, ins
.objectid
, blocksize
);
1750 btrfs_free_extent(trans
, root
, ins
.objectid
, blocksize
,
1751 root
->root_key
.objectid
, ref_generation
,
1753 return ERR_PTR(-ENOMEM
);
1755 btrfs_set_buffer_uptodate(buf
);
1756 set_extent_dirty(&trans
->transaction
->dirty_pages
, buf
->start
,
1757 buf
->start
+ buf
->len
- 1, GFP_NOFS
);
1758 set_extent_bits(&BTRFS_I(root
->fs_info
->btree_inode
)->extent_tree
,
1759 buf
->start
, buf
->start
+ buf
->len
- 1,
1760 EXTENT_CSUM
, GFP_NOFS
);
1761 buf
->flags
|= EXTENT_CSUM
;
1762 btrfs_set_buffer_defrag(buf
);
1763 trans
->blocks_used
++;
1767 static int noinline
drop_leaf_ref(struct btrfs_trans_handle
*trans
,
1768 struct btrfs_root
*root
,
1769 struct extent_buffer
*leaf
)
1772 u64 leaf_generation
;
1773 struct btrfs_key key
;
1774 struct btrfs_file_extent_item
*fi
;
1779 BUG_ON(!btrfs_is_leaf(leaf
));
1780 nritems
= btrfs_header_nritems(leaf
);
1781 leaf_owner
= btrfs_header_owner(leaf
);
1782 leaf_generation
= btrfs_header_generation(leaf
);
1784 for (i
= 0; i
< nritems
; i
++) {
1787 btrfs_item_key_to_cpu(leaf
, &key
, i
);
1788 if (btrfs_key_type(&key
) != BTRFS_EXTENT_DATA_KEY
)
1790 fi
= btrfs_item_ptr(leaf
, i
, struct btrfs_file_extent_item
);
1791 if (btrfs_file_extent_type(leaf
, fi
) ==
1792 BTRFS_FILE_EXTENT_INLINE
)
1795 * FIXME make sure to insert a trans record that
1796 * repeats the snapshot del on crash
1798 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1799 if (disk_bytenr
== 0)
1801 ret
= btrfs_free_extent(trans
, root
, disk_bytenr
,
1802 btrfs_file_extent_disk_num_bytes(leaf
, fi
),
1803 leaf_owner
, leaf_generation
,
1804 key
.objectid
, key
.offset
, 0);
1810 static void noinline
reada_walk_down(struct btrfs_root
*root
,
1811 struct extent_buffer
*node
)
1821 nritems
= btrfs_header_nritems(node
);
1822 level
= btrfs_header_level(node
);
1823 for (i
= 0; i
< nritems
; i
++) {
1824 bytenr
= btrfs_node_blockptr(node
, i
);
1825 blocksize
= btrfs_level_size(root
, level
- 1);
1826 ret
= lookup_extent_ref(NULL
, root
, bytenr
, blocksize
, &refs
);
1830 mutex_unlock(&root
->fs_info
->fs_mutex
);
1831 ret
= readahead_tree_block(root
, bytenr
, blocksize
);
1833 mutex_lock(&root
->fs_info
->fs_mutex
);
1840 * helper function for drop_snapshot, this walks down the tree dropping ref
1841 * counts as it goes.
1843 static int noinline
walk_down_tree(struct btrfs_trans_handle
*trans
,
1844 struct btrfs_root
*root
,
1845 struct btrfs_path
*path
, int *level
)
1850 struct extent_buffer
*next
;
1851 struct extent_buffer
*cur
;
1852 struct extent_buffer
*parent
;
1857 WARN_ON(*level
< 0);
1858 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1859 ret
= lookup_extent_ref(trans
, root
,
1860 path
->nodes
[*level
]->start
,
1861 path
->nodes
[*level
]->len
, &refs
);
1867 * walk down to the last node level and free all the leaves
1869 while(*level
>= 0) {
1870 WARN_ON(*level
< 0);
1871 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1872 cur
= path
->nodes
[*level
];
1874 if (*level
> 0 && path
->slots
[*level
] == 0)
1875 reada_walk_down(root
, cur
);
1877 if (btrfs_header_level(cur
) != *level
)
1880 if (path
->slots
[*level
] >=
1881 btrfs_header_nritems(cur
))
1884 ret
= drop_leaf_ref(trans
, root
, cur
);
1888 bytenr
= btrfs_node_blockptr(cur
, path
->slots
[*level
]);
1889 blocksize
= btrfs_level_size(root
, *level
- 1);
1890 ret
= lookup_extent_ref(trans
, root
, bytenr
, blocksize
, &refs
);
1893 parent
= path
->nodes
[*level
];
1894 root_owner
= btrfs_header_owner(parent
);
1895 root_gen
= btrfs_header_generation(parent
);
1896 path
->slots
[*level
]++;
1897 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1898 blocksize
, root_owner
,
1903 next
= btrfs_find_tree_block(root
, bytenr
, blocksize
);
1904 if (!next
|| !btrfs_buffer_uptodate(next
)) {
1905 free_extent_buffer(next
);
1906 mutex_unlock(&root
->fs_info
->fs_mutex
);
1907 next
= read_tree_block(root
, bytenr
, blocksize
);
1908 mutex_lock(&root
->fs_info
->fs_mutex
);
1910 /* we dropped the lock, check one more time */
1911 ret
= lookup_extent_ref(trans
, root
, bytenr
,
1915 parent
= path
->nodes
[*level
];
1916 root_owner
= btrfs_header_owner(parent
);
1917 root_gen
= btrfs_header_generation(parent
);
1919 path
->slots
[*level
]++;
1920 free_extent_buffer(next
);
1921 ret
= btrfs_free_extent(trans
, root
, bytenr
,
1929 WARN_ON(*level
<= 0);
1930 if (path
->nodes
[*level
-1])
1931 free_extent_buffer(path
->nodes
[*level
-1]);
1932 path
->nodes
[*level
-1] = next
;
1933 *level
= btrfs_header_level(next
);
1934 path
->slots
[*level
] = 0;
1937 WARN_ON(*level
< 0);
1938 WARN_ON(*level
>= BTRFS_MAX_LEVEL
);
1940 if (path
->nodes
[*level
] == root
->node
) {
1941 root_owner
= root
->root_key
.objectid
;
1942 parent
= path
->nodes
[*level
];
1944 parent
= path
->nodes
[*level
+ 1];
1945 root_owner
= btrfs_header_owner(parent
);
1948 root_gen
= btrfs_header_generation(parent
);
1949 ret
= btrfs_free_extent(trans
, root
, path
->nodes
[*level
]->start
,
1950 path
->nodes
[*level
]->len
,
1951 root_owner
, root_gen
, 0, 0, 1);
1952 free_extent_buffer(path
->nodes
[*level
]);
1953 path
->nodes
[*level
] = NULL
;
1960 * helper for dropping snapshots. This walks back up the tree in the path
1961 * to find the first node higher up where we haven't yet gone through
1964 static int noinline
walk_up_tree(struct btrfs_trans_handle
*trans
,
1965 struct btrfs_root
*root
,
1966 struct btrfs_path
*path
, int *level
)
1970 struct btrfs_root_item
*root_item
= &root
->root_item
;
1975 for(i
= *level
; i
< BTRFS_MAX_LEVEL
- 1 && path
->nodes
[i
]; i
++) {
1976 slot
= path
->slots
[i
];
1977 if (slot
< btrfs_header_nritems(path
->nodes
[i
]) - 1) {
1978 struct extent_buffer
*node
;
1979 struct btrfs_disk_key disk_key
;
1980 node
= path
->nodes
[i
];
1983 WARN_ON(*level
== 0);
1984 btrfs_node_key(node
, &disk_key
, path
->slots
[i
]);
1985 memcpy(&root_item
->drop_progress
,
1986 &disk_key
, sizeof(disk_key
));
1987 root_item
->drop_level
= i
;
1990 if (path
->nodes
[*level
] == root
->node
) {
1991 root_owner
= root
->root_key
.objectid
;
1993 btrfs_header_generation(path
->nodes
[*level
]);
1995 struct extent_buffer
*node
;
1996 node
= path
->nodes
[*level
+ 1];
1997 root_owner
= btrfs_header_owner(node
);
1998 root_gen
= btrfs_header_generation(node
);
2000 ret
= btrfs_free_extent(trans
, root
,
2001 path
->nodes
[*level
]->start
,
2002 path
->nodes
[*level
]->len
,
2003 root_owner
, root_gen
, 0, 0, 1);
2005 free_extent_buffer(path
->nodes
[*level
]);
2006 path
->nodes
[*level
] = NULL
;
2014 * drop the reference count on the tree rooted at 'snap'. This traverses
2015 * the tree freeing any blocks that have a ref count of zero after being
2018 int btrfs_drop_snapshot(struct btrfs_trans_handle
*trans
, struct btrfs_root
2024 struct btrfs_path
*path
;
2027 struct btrfs_root_item
*root_item
= &root
->root_item
;
2029 path
= btrfs_alloc_path();
2032 level
= btrfs_header_level(root
->node
);
2034 if (btrfs_disk_key_objectid(&root_item
->drop_progress
) == 0) {
2035 path
->nodes
[level
] = root
->node
;
2036 extent_buffer_get(root
->node
);
2037 path
->slots
[level
] = 0;
2039 struct btrfs_key key
;
2040 struct btrfs_disk_key found_key
;
2041 struct extent_buffer
*node
;
2043 btrfs_disk_key_to_cpu(&key
, &root_item
->drop_progress
);
2044 level
= root_item
->drop_level
;
2045 path
->lowest_level
= level
;
2046 wret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2051 node
= path
->nodes
[level
];
2052 btrfs_node_key(node
, &found_key
, path
->slots
[level
]);
2053 WARN_ON(memcmp(&found_key
, &root_item
->drop_progress
,
2054 sizeof(found_key
)));
2057 wret
= walk_down_tree(trans
, root
, path
, &level
);
2063 wret
= walk_up_tree(trans
, root
, path
, &level
);
2071 for (i
= 0; i
<= orig_level
; i
++) {
2072 if (path
->nodes
[i
]) {
2073 free_extent_buffer(path
->nodes
[i
]);
2074 path
->nodes
[i
] = NULL
;
2078 btrfs_free_path(path
);
2082 int btrfs_free_block_groups(struct btrfs_fs_info
*info
)
2089 ret
= find_first_extent_bit(&info
->block_group_cache
, 0,
2090 &start
, &end
, (unsigned int)-1);
2093 ret
= get_state_private(&info
->block_group_cache
, start
, &ptr
);
2095 kfree((void *)(unsigned long)ptr
);
2096 clear_extent_bits(&info
->block_group_cache
, start
,
2097 end
, (unsigned int)-1, GFP_NOFS
);
2100 ret
= find_first_extent_bit(&info
->free_space_cache
, 0,
2101 &start
, &end
, EXTENT_DIRTY
);
2104 clear_extent_dirty(&info
->free_space_cache
, start
,
2110 static int noinline
relocate_inode_pages(struct inode
*inode
, u64 start
,
2116 u64 existing_delalloc
;
2117 unsigned long last_index
;
2120 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2121 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
2122 struct file_ra_state
*ra
;
2124 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
2126 mutex_lock(&inode
->i_mutex
);
2127 i
= start
>> PAGE_CACHE_SHIFT
;
2128 last_index
= (start
+ len
- 1) >> PAGE_CACHE_SHIFT
;
2130 file_ra_state_init(ra
, inode
->i_mapping
);
2131 btrfs_force_ra(inode
->i_mapping
, ra
, NULL
, i
, last_index
);
2134 for (; i
<= last_index
; i
++) {
2135 page
= grab_cache_page(inode
->i_mapping
, i
);
2138 if (!PageUptodate(page
)) {
2139 btrfs_readpage(NULL
, page
);
2141 if (!PageUptodate(page
)) {
2143 page_cache_release(page
);
2147 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2148 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2150 lock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2152 delalloc_start
= page_start
;
2154 count_range_bits(&BTRFS_I(inode
)->extent_tree
,
2155 &delalloc_start
, page_end
,
2156 PAGE_CACHE_SIZE
, EXTENT_DELALLOC
);
2158 set_extent_delalloc(em_tree
, page_start
,
2159 page_end
, GFP_NOFS
);
2161 spin_lock(&root
->fs_info
->delalloc_lock
);
2162 root
->fs_info
->delalloc_bytes
+= PAGE_CACHE_SIZE
-
2164 spin_unlock(&root
->fs_info
->delalloc_lock
);
2166 unlock_extent(em_tree
, page_start
, page_end
, GFP_NOFS
);
2167 set_page_dirty(page
);
2169 page_cache_release(page
);
2173 mutex_unlock(&inode
->i_mutex
);
2178 * note, this releases the path
2180 static int noinline
relocate_one_reference(struct btrfs_root
*extent_root
,
2181 struct btrfs_path
*path
,
2182 struct btrfs_key
*extent_key
)
2184 struct inode
*inode
;
2185 struct btrfs_root
*found_root
;
2186 struct btrfs_key
*root_location
;
2187 struct btrfs_extent_ref
*ref
;
2194 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2195 struct btrfs_extent_ref
);
2196 ref_root
= btrfs_ref_root(path
->nodes
[0], ref
);
2197 ref_gen
= btrfs_ref_generation(path
->nodes
[0], ref
);
2198 ref_objectid
= btrfs_ref_objectid(path
->nodes
[0], ref
);
2199 ref_offset
= btrfs_ref_offset(path
->nodes
[0], ref
);
2200 btrfs_release_path(extent_root
, path
);
2202 root_location
= kmalloc(sizeof(*root_location
), GFP_NOFS
);
2203 root_location
->objectid
= ref_root
;
2205 root_location
->offset
= 0;
2207 root_location
->offset
= (u64
)-1;
2208 root_location
->type
= BTRFS_ROOT_ITEM_KEY
;
2210 found_root
= btrfs_read_fs_root_no_name(extent_root
->fs_info
,
2212 BUG_ON(!found_root
);
2213 kfree(root_location
);
2215 if (ref_objectid
>= BTRFS_FIRST_FREE_OBJECTID
) {
2216 mutex_unlock(&extent_root
->fs_info
->fs_mutex
);
2217 inode
= btrfs_iget_locked(extent_root
->fs_info
->sb
,
2218 ref_objectid
, found_root
);
2219 if (inode
->i_state
& I_NEW
) {
2220 /* the inode and parent dir are two different roots */
2221 BTRFS_I(inode
)->root
= found_root
;
2222 BTRFS_I(inode
)->location
.objectid
= ref_objectid
;
2223 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
2224 BTRFS_I(inode
)->location
.offset
= 0;
2225 btrfs_read_locked_inode(inode
);
2226 unlock_new_inode(inode
);
2229 /* this can happen if the reference is not against
2230 * the latest version of the tree root
2232 if (is_bad_inode(inode
)) {
2233 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2236 relocate_inode_pages(inode
, ref_offset
, extent_key
->offset
);
2237 /* FIXME, data=ordered will help get rid of this */
2238 filemap_fdatawrite(inode
->i_mapping
);
2240 mutex_lock(&extent_root
->fs_info
->fs_mutex
);
2242 struct btrfs_trans_handle
*trans
;
2243 struct btrfs_key found_key
;
2244 struct extent_buffer
*eb
;
2248 trans
= btrfs_start_transaction(found_root
, 1);
2249 eb
= read_tree_block(found_root
, extent_key
->objectid
,
2250 extent_key
->offset
);
2251 level
= btrfs_header_level(eb
);
2254 btrfs_item_key_to_cpu(eb
, &found_key
, 0);
2256 btrfs_node_key_to_cpu(eb
, &found_key
, 0);
2258 free_extent_buffer(eb
);
2260 path
->lowest_level
= level
;
2262 ret
= btrfs_search_slot(trans
, found_root
, &found_key
, path
,
2264 path
->lowest_level
= 0;
2265 for (i
= level
; i
< BTRFS_MAX_LEVEL
; i
++) {
2266 if (!path
->nodes
[i
])
2268 free_extent_buffer(path
->nodes
[i
]);
2269 path
->nodes
[i
] = NULL
;
2271 btrfs_release_path(found_root
, path
);
2272 btrfs_end_transaction(trans
, found_root
);
2279 static int noinline
relocate_one_extent(struct btrfs_root
*extent_root
,
2280 struct btrfs_path
*path
,
2281 struct btrfs_key
*extent_key
)
2283 struct btrfs_key key
;
2284 struct btrfs_key found_key
;
2285 struct extent_buffer
*leaf
;
2290 key
.objectid
= extent_key
->objectid
;
2291 key
.type
= BTRFS_EXTENT_REF_KEY
;
2295 ret
= btrfs_search_slot(NULL
, extent_root
, &key
, path
, 0, 0);
2303 leaf
= path
->nodes
[0];
2304 nritems
= btrfs_header_nritems(leaf
);
2305 if (path
->slots
[0] == nritems
)
2308 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2309 if (found_key
.objectid
!= extent_key
->objectid
)
2312 if (found_key
.type
!= BTRFS_EXTENT_REF_KEY
)
2315 key
.offset
= found_key
.offset
+ 1;
2316 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
2318 ret
= relocate_one_reference(extent_root
, path
, extent_key
);
2324 btrfs_release_path(extent_root
, path
);
2328 static int find_overlapping_extent(struct btrfs_root
*root
,
2329 struct btrfs_path
*path
, u64 new_size
)
2331 struct btrfs_key found_key
;
2332 struct extent_buffer
*leaf
;
2336 if (path
->slots
[0] == 0) {
2337 ret
= btrfs_prev_leaf(root
, path
);
2346 leaf
= path
->nodes
[0];
2347 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2348 if (found_key
.type
== BTRFS_EXTENT_ITEM_KEY
) {
2349 if (found_key
.objectid
+ found_key
.offset
> new_size
)
2358 int btrfs_shrink_extent_tree(struct btrfs_root
*root
, u64 new_size
)
2360 struct btrfs_trans_handle
*trans
;
2361 struct btrfs_root
*tree_root
= root
->fs_info
->tree_root
;
2362 struct btrfs_path
*path
;
2365 struct btrfs_fs_info
*info
= root
->fs_info
;
2366 struct extent_map_tree
*block_group_cache
;
2367 struct btrfs_key key
;
2368 struct btrfs_key found_key
= { 0, 0, 0 };
2369 struct extent_buffer
*leaf
;
2374 btrfs_set_super_total_bytes(&info
->super_copy
, new_size
);
2375 block_group_cache
= &info
->block_group_cache
;
2376 path
= btrfs_alloc_path();
2377 root
= root
->fs_info
->extent_root
;
2382 key
.objectid
= new_size
;
2383 cur_byte
= key
.objectid
;
2388 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2392 leaf
= path
->nodes
[0];
2393 if (key
.objectid
== new_size
- 1) {
2394 ret
= find_overlapping_extent(root
, path
, new_size
);
2396 btrfs_release_path(root
, path
);
2397 ret
= btrfs_search_slot(NULL
, root
, &key
,
2403 nritems
= btrfs_header_nritems(leaf
);
2405 slot
= path
->slots
[0];
2407 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2408 if (slot
== nritems
||
2409 btrfs_key_type(&found_key
) != BTRFS_EXTENT_ITEM_KEY
) {
2411 if (path
->slots
[0] >= nritems
) {
2412 ret
= btrfs_next_leaf(root
, path
);
2422 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2423 if (found_key
.objectid
+ found_key
.offset
<= cur_byte
)
2426 cur_byte
= found_key
.objectid
+ found_key
.offset
;
2427 key
.objectid
= cur_byte
;
2428 btrfs_release_path(root
, path
);
2429 ret
= relocate_one_extent(root
, path
, &found_key
);
2432 btrfs_release_path(root
, path
);
2434 if (total_found
> 0) {
2435 trans
= btrfs_start_transaction(tree_root
, 1);
2436 btrfs_commit_transaction(trans
, tree_root
);
2438 mutex_unlock(&root
->fs_info
->fs_mutex
);
2439 btrfs_clean_old_snapshots(tree_root
);
2440 mutex_lock(&root
->fs_info
->fs_mutex
);
2442 trans
= btrfs_start_transaction(tree_root
, 1);
2443 btrfs_commit_transaction(trans
, tree_root
);
2447 trans
= btrfs_start_transaction(root
, 1);
2448 key
.objectid
= new_size
;
2454 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
2458 leaf
= path
->nodes
[0];
2459 nritems
= btrfs_header_nritems(leaf
);
2461 slot
= path
->slots
[0];
2463 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2464 if (slot
== nritems
||
2465 btrfs_key_type(&found_key
) != BTRFS_BLOCK_GROUP_ITEM_KEY
) {
2466 if (slot
< nritems
) {
2467 printk("shrinker found key %Lu %u %Lu\n",
2468 found_key
.objectid
, found_key
.type
,
2472 if (path
->slots
[0] >= nritems
) {
2473 ret
= btrfs_next_leaf(root
, path
);
2483 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
2484 ret
= get_state_private(&info
->block_group_cache
,
2485 found_key
.objectid
, &ptr
);
2487 kfree((void *)(unsigned long)ptr
);
2489 clear_extent_bits(&info
->block_group_cache
, found_key
.objectid
,
2490 found_key
.objectid
+ found_key
.offset
- 1,
2491 (unsigned int)-1, GFP_NOFS
);
2493 key
.objectid
= found_key
.objectid
+ 1;
2494 btrfs_del_item(trans
, root
, path
);
2495 btrfs_release_path(root
, path
);
2497 clear_extent_dirty(&info
->free_space_cache
, new_size
, (u64
)-1,
2499 btrfs_commit_transaction(trans
, root
);
2501 btrfs_free_path(path
);
2505 int btrfs_grow_extent_tree(struct btrfs_trans_handle
*trans
,
2506 struct btrfs_root
*root
, u64 new_size
)
2508 struct btrfs_path
*path
;
2513 struct btrfs_block_group_cache
*cache
;
2514 struct btrfs_block_group_item
*item
;
2515 struct btrfs_fs_info
*info
= root
->fs_info
;
2516 struct extent_map_tree
*block_group_cache
;
2517 struct btrfs_key key
;
2518 struct extent_buffer
*leaf
;
2522 old_size
= btrfs_super_total_bytes(&info
->super_copy
);
2523 block_group_cache
= &info
->block_group_cache
;
2525 root
= info
->extent_root
;
2527 cache
= btrfs_lookup_block_group(root
->fs_info
, old_size
- 1);
2529 cur_byte
= cache
->key
.objectid
+ cache
->key
.offset
;
2530 if (cur_byte
>= new_size
)
2533 key
.offset
= BTRFS_BLOCK_GROUP_SIZE
;
2534 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2536 path
= btrfs_alloc_path();
2540 while(cur_byte
< new_size
) {
2541 key
.objectid
= cur_byte
;
2542 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2543 sizeof(struct btrfs_block_group_item
));
2545 leaf
= path
->nodes
[0];
2546 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2547 struct btrfs_block_group_item
);
2549 btrfs_set_disk_block_group_used(leaf
, item
, 0);
2550 div_long_long_rem(nr
, 3, &rem
);
2552 btrfs_set_disk_block_group_flags(leaf
, item
,
2553 BTRFS_BLOCK_GROUP_DATA
);
2555 btrfs_set_disk_block_group_flags(leaf
, item
, 0);
2559 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2562 read_extent_buffer(leaf
, &cache
->item
, (unsigned long)item
,
2563 sizeof(cache
->item
));
2565 memcpy(&cache
->key
, &key
, sizeof(key
));
2568 cur_byte
= key
.objectid
+ key
.offset
;
2569 btrfs_release_path(root
, path
);
2571 if (cache
->item
.flags
& BTRFS_BLOCK_GROUP_DATA
) {
2572 bit
= BLOCK_GROUP_DATA
;
2573 cache
->data
= BTRFS_BLOCK_GROUP_DATA
;
2575 bit
= BLOCK_GROUP_METADATA
;
2579 /* use EXTENT_LOCKED to prevent merging */
2580 set_extent_bits(block_group_cache
, key
.objectid
,
2581 key
.objectid
+ key
.offset
- 1,
2582 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2583 set_state_private(block_group_cache
, key
.objectid
,
2584 (unsigned long)cache
);
2586 btrfs_free_path(path
);
2588 btrfs_set_super_total_bytes(&info
->super_copy
, new_size
);
2592 int btrfs_read_block_groups(struct btrfs_root
*root
)
2594 struct btrfs_path
*path
;
2598 struct btrfs_block_group_cache
*cache
;
2599 struct btrfs_fs_info
*info
= root
->fs_info
;
2600 struct extent_map_tree
*block_group_cache
;
2601 struct btrfs_key key
;
2602 struct btrfs_key found_key
;
2603 struct extent_buffer
*leaf
;
2605 block_group_cache
= &info
->block_group_cache
;
2607 root
= info
->extent_root
;
2609 key
.offset
= BTRFS_BLOCK_GROUP_SIZE
;
2610 btrfs_set_key_type(&key
, BTRFS_BLOCK_GROUP_ITEM_KEY
);
2612 path
= btrfs_alloc_path();
2617 ret
= btrfs_search_slot(NULL
, info
->extent_root
,
2623 leaf
= path
->nodes
[0];
2624 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2625 cache
= kmalloc(sizeof(*cache
), GFP_NOFS
);
2631 read_extent_buffer(leaf
, &cache
->item
,
2632 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2633 sizeof(cache
->item
));
2634 memcpy(&cache
->key
, &found_key
, sizeof(found_key
));
2637 key
.objectid
= found_key
.objectid
+ found_key
.offset
;
2638 btrfs_release_path(root
, path
);
2640 if (cache
->item
.flags
& BTRFS_BLOCK_GROUP_MIXED
) {
2641 bit
= BLOCK_GROUP_DATA
| BLOCK_GROUP_METADATA
;
2642 cache
->data
= BTRFS_BLOCK_GROUP_MIXED
;
2643 } else if (cache
->item
.flags
& BTRFS_BLOCK_GROUP_DATA
) {
2644 bit
= BLOCK_GROUP_DATA
;
2645 cache
->data
= BTRFS_BLOCK_GROUP_DATA
;
2647 bit
= BLOCK_GROUP_METADATA
;
2651 /* use EXTENT_LOCKED to prevent merging */
2652 set_extent_bits(block_group_cache
, found_key
.objectid
,
2653 found_key
.objectid
+ found_key
.offset
- 1,
2654 bit
| EXTENT_LOCKED
, GFP_NOFS
);
2655 set_state_private(block_group_cache
, found_key
.objectid
,
2656 (unsigned long)cache
);
2659 btrfs_super_total_bytes(&info
->super_copy
))
2663 btrfs_free_path(path
);