2 * Copyright (C) 2008 Red Hat. 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/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/math64.h>
24 #include "free-space-cache.h"
25 #include "transaction.h"
28 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
29 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
31 static void recalculate_thresholds(struct btrfs_block_group_cache
33 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
34 struct btrfs_free_space
*info
);
36 struct inode
*lookup_free_space_inode(struct btrfs_root
*root
,
37 struct btrfs_block_group_cache
38 *block_group
, struct btrfs_path
*path
)
41 struct btrfs_key location
;
42 struct btrfs_disk_key disk_key
;
43 struct btrfs_free_space_header
*header
;
44 struct extent_buffer
*leaf
;
45 struct inode
*inode
= NULL
;
48 spin_lock(&block_group
->lock
);
49 if (block_group
->inode
)
50 inode
= igrab(block_group
->inode
);
51 spin_unlock(&block_group
->lock
);
55 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
56 key
.offset
= block_group
->key
.objectid
;
59 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
63 btrfs_release_path(root
, path
);
64 return ERR_PTR(-ENOENT
);
67 leaf
= path
->nodes
[0];
68 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
69 struct btrfs_free_space_header
);
70 btrfs_free_space_key(leaf
, header
, &disk_key
);
71 btrfs_disk_key_to_cpu(&location
, &disk_key
);
72 btrfs_release_path(root
, path
);
74 inode
= btrfs_iget(root
->fs_info
->sb
, &location
, root
, NULL
);
76 return ERR_PTR(-ENOENT
);
79 if (is_bad_inode(inode
)) {
81 return ERR_PTR(-ENOENT
);
84 spin_lock(&block_group
->lock
);
85 if (!root
->fs_info
->closing
) {
86 block_group
->inode
= igrab(inode
);
87 block_group
->iref
= 1;
89 spin_unlock(&block_group
->lock
);
94 int create_free_space_inode(struct btrfs_root
*root
,
95 struct btrfs_trans_handle
*trans
,
96 struct btrfs_block_group_cache
*block_group
,
97 struct btrfs_path
*path
)
100 struct btrfs_disk_key disk_key
;
101 struct btrfs_free_space_header
*header
;
102 struct btrfs_inode_item
*inode_item
;
103 struct extent_buffer
*leaf
;
107 ret
= btrfs_find_free_objectid(trans
, root
, 0, &objectid
);
111 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
115 leaf
= path
->nodes
[0];
116 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
117 struct btrfs_inode_item
);
118 btrfs_item_key(leaf
, &disk_key
, path
->slots
[0]);
119 memset_extent_buffer(leaf
, 0, (unsigned long)inode_item
,
120 sizeof(*inode_item
));
121 btrfs_set_inode_generation(leaf
, inode_item
, trans
->transid
);
122 btrfs_set_inode_size(leaf
, inode_item
, 0);
123 btrfs_set_inode_nbytes(leaf
, inode_item
, 0);
124 btrfs_set_inode_uid(leaf
, inode_item
, 0);
125 btrfs_set_inode_gid(leaf
, inode_item
, 0);
126 btrfs_set_inode_mode(leaf
, inode_item
, S_IFREG
| 0600);
127 btrfs_set_inode_flags(leaf
, inode_item
, BTRFS_INODE_NOCOMPRESS
|
128 BTRFS_INODE_PREALLOC
| BTRFS_INODE_NODATASUM
);
129 btrfs_set_inode_nlink(leaf
, inode_item
, 1);
130 btrfs_set_inode_transid(leaf
, inode_item
, trans
->transid
);
131 btrfs_set_inode_block_group(leaf
, inode_item
,
132 block_group
->key
.objectid
);
133 btrfs_mark_buffer_dirty(leaf
);
134 btrfs_release_path(root
, path
);
136 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
137 key
.offset
= block_group
->key
.objectid
;
140 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
141 sizeof(struct btrfs_free_space_header
));
143 btrfs_release_path(root
, path
);
146 leaf
= path
->nodes
[0];
147 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
148 struct btrfs_free_space_header
);
149 memset_extent_buffer(leaf
, 0, (unsigned long)header
, sizeof(*header
));
150 btrfs_set_free_space_key(leaf
, header
, &disk_key
);
151 btrfs_mark_buffer_dirty(leaf
);
152 btrfs_release_path(root
, path
);
157 int btrfs_truncate_free_space_cache(struct btrfs_root
*root
,
158 struct btrfs_trans_handle
*trans
,
159 struct btrfs_path
*path
,
165 trans
->block_rsv
= root
->orphan_block_rsv
;
166 ret
= btrfs_block_rsv_check(trans
, root
,
167 root
->orphan_block_rsv
,
172 oldsize
= i_size_read(inode
);
173 btrfs_i_size_write(inode
, 0);
174 truncate_pagecache(inode
, oldsize
, 0);
177 * We don't need an orphan item because truncating the free space cache
178 * will never be split across transactions.
180 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
181 0, BTRFS_EXTENT_DATA_KEY
);
187 return btrfs_update_inode(trans
, root
, inode
);
190 int btrfs_write_out_cache(struct btrfs_root
*root
,
191 struct btrfs_trans_handle
*trans
,
192 struct btrfs_block_group_cache
*block_group
,
193 struct btrfs_path
*path
)
195 struct btrfs_free_space_header
*header
;
196 struct extent_buffer
*leaf
;
198 struct rb_node
*node
;
199 struct list_head
*pos
, *n
;
201 struct extent_state
*cached_state
= NULL
;
202 struct list_head bitmap_list
;
203 struct btrfs_key key
;
205 u32
*crc
, *checksums
;
206 pgoff_t index
= 0, last_index
= 0;
207 unsigned long first_page_offset
;
213 root
= root
->fs_info
->tree_root
;
215 INIT_LIST_HEAD(&bitmap_list
);
217 spin_lock(&block_group
->lock
);
218 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
219 spin_unlock(&block_group
->lock
);
222 spin_unlock(&block_group
->lock
);
224 inode
= lookup_free_space_inode(root
, block_group
, path
);
228 if (!i_size_read(inode
)) {
233 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
234 filemap_write_and_wait(inode
->i_mapping
);
235 btrfs_wait_ordered_range(inode
, inode
->i_size
&
236 ~(root
->sectorsize
- 1), (u64
)-1);
238 /* We need a checksum per page. */
239 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
240 crc
= checksums
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
246 /* Since the first page has all of our checksums and our generation we
247 * need to calculate the offset into the page that we can start writing
250 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
252 node
= rb_first(&block_group
->free_space_offset
);
257 * Lock all pages first so we can lock the extent safely.
259 * NOTE: Because we hold the ref the entire time we're going to write to
260 * the page find_get_page should never fail, so we don't do a check
261 * after find_get_page at this point. Just putting this here so people
262 * know and don't freak out.
264 while (index
<= last_index
) {
265 page
= grab_cache_page(inode
->i_mapping
, index
);
270 page
= find_get_page(inode
->i_mapping
, i
);
272 page_cache_release(page
);
273 page_cache_release(page
);
282 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
283 0, &cached_state
, GFP_NOFS
);
285 /* Write out the extent entries */
287 struct btrfs_free_space_entry
*entry
;
289 unsigned long offset
= 0;
290 unsigned long start_offset
= 0;
293 start_offset
= first_page_offset
;
294 offset
= start_offset
;
297 page
= find_get_page(inode
->i_mapping
, index
);
300 entry
= addr
+ start_offset
;
302 memset(addr
, 0, PAGE_CACHE_SIZE
);
304 struct btrfs_free_space
*e
;
306 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
309 entry
->offset
= cpu_to_le64(e
->offset
);
310 entry
->bytes
= cpu_to_le64(e
->bytes
);
312 entry
->type
= BTRFS_FREE_SPACE_BITMAP
;
313 list_add_tail(&e
->list
, &bitmap_list
);
316 entry
->type
= BTRFS_FREE_SPACE_EXTENT
;
318 node
= rb_next(node
);
321 offset
+= sizeof(struct btrfs_free_space_entry
);
322 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
328 *crc
= btrfs_csum_data(root
, addr
+ start_offset
, *crc
,
329 PAGE_CACHE_SIZE
- start_offset
);
332 btrfs_csum_final(*crc
, (char *)crc
);
335 bytes
+= PAGE_CACHE_SIZE
;
337 ClearPageChecked(page
);
338 set_page_extent_mapped(page
);
339 SetPageUptodate(page
);
340 set_page_dirty(page
);
343 * We need to release our reference we got for grab_cache_page,
344 * except for the first page which will hold our checksums, we
349 page_cache_release(page
);
352 page_cache_release(page
);
357 /* Write out the bitmaps */
358 list_for_each_safe(pos
, n
, &bitmap_list
) {
360 struct btrfs_free_space
*entry
=
361 list_entry(pos
, struct btrfs_free_space
, list
);
363 page
= find_get_page(inode
->i_mapping
, index
);
366 memcpy(addr
, entry
->bitmap
, PAGE_CACHE_SIZE
);
368 *crc
= btrfs_csum_data(root
, addr
, *crc
, PAGE_CACHE_SIZE
);
370 btrfs_csum_final(*crc
, (char *)crc
);
372 bytes
+= PAGE_CACHE_SIZE
;
374 ClearPageChecked(page
);
375 set_page_extent_mapped(page
);
376 SetPageUptodate(page
);
377 set_page_dirty(page
);
379 page_cache_release(page
);
380 page_cache_release(page
);
381 list_del_init(&entry
->list
);
385 /* Zero out the rest of the pages just to make sure */
386 while (index
<= last_index
) {
389 page
= find_get_page(inode
->i_mapping
, index
);
392 memset(addr
, 0, PAGE_CACHE_SIZE
);
394 ClearPageChecked(page
);
395 set_page_extent_mapped(page
);
396 SetPageUptodate(page
);
397 set_page_dirty(page
);
399 page_cache_release(page
);
400 page_cache_release(page
);
401 bytes
+= PAGE_CACHE_SIZE
;
405 btrfs_set_extent_delalloc(inode
, 0, bytes
- 1, &cached_state
);
407 /* Write the checksums and trans id to the first page */
412 page
= find_get_page(inode
->i_mapping
, 0);
415 memcpy(addr
, checksums
, sizeof(u32
) * num_checksums
);
416 gen
= addr
+ (sizeof(u32
) * num_checksums
);
417 *gen
= trans
->transid
;
419 ClearPageChecked(page
);
420 set_page_extent_mapped(page
);
421 SetPageUptodate(page
);
422 set_page_dirty(page
);
424 page_cache_release(page
);
425 page_cache_release(page
);
427 BTRFS_I(inode
)->generation
= trans
->transid
;
429 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
430 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
432 filemap_write_and_wait(inode
->i_mapping
);
434 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
435 key
.offset
= block_group
->key
.objectid
;
438 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
441 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
442 EXTENT_DIRTY
| EXTENT_DELALLOC
|
443 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, GFP_NOFS
);
446 leaf
= path
->nodes
[0];
448 struct btrfs_key found_key
;
449 BUG_ON(!path
->slots
[0]);
451 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
452 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
453 found_key
.offset
!= block_group
->key
.objectid
) {
455 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
456 EXTENT_DIRTY
| EXTENT_DELALLOC
|
457 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
,
459 btrfs_release_path(root
, path
);
463 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
464 struct btrfs_free_space_header
);
465 btrfs_set_free_space_entries(leaf
, header
, entries
);
466 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
467 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
468 btrfs_mark_buffer_dirty(leaf
);
469 btrfs_release_path(root
, path
);
475 invalidate_inode_pages2_range(inode
->i_mapping
, 0, index
);
476 spin_lock(&block_group
->lock
);
477 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
478 spin_unlock(&block_group
->lock
);
479 BTRFS_I(inode
)->generation
= 0;
482 btrfs_update_inode(trans
, root
, inode
);
487 static inline unsigned long offset_to_bit(u64 bitmap_start
, u64 sectorsize
,
490 BUG_ON(offset
< bitmap_start
);
491 offset
-= bitmap_start
;
492 return (unsigned long)(div64_u64(offset
, sectorsize
));
495 static inline unsigned long bytes_to_bits(u64 bytes
, u64 sectorsize
)
497 return (unsigned long)(div64_u64(bytes
, sectorsize
));
500 static inline u64
offset_to_bitmap(struct btrfs_block_group_cache
*block_group
,
504 u64 bytes_per_bitmap
;
506 bytes_per_bitmap
= BITS_PER_BITMAP
* block_group
->sectorsize
;
507 bitmap_start
= offset
- block_group
->key
.objectid
;
508 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
509 bitmap_start
*= bytes_per_bitmap
;
510 bitmap_start
+= block_group
->key
.objectid
;
515 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
516 struct rb_node
*node
, int bitmap
)
518 struct rb_node
**p
= &root
->rb_node
;
519 struct rb_node
*parent
= NULL
;
520 struct btrfs_free_space
*info
;
524 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
526 if (offset
< info
->offset
) {
528 } else if (offset
> info
->offset
) {
532 * we could have a bitmap entry and an extent entry
533 * share the same offset. If this is the case, we want
534 * the extent entry to always be found first if we do a
535 * linear search through the tree, since we want to have
536 * the quickest allocation time, and allocating from an
537 * extent is faster than allocating from a bitmap. So
538 * if we're inserting a bitmap and we find an entry at
539 * this offset, we want to go right, or after this entry
540 * logically. If we are inserting an extent and we've
541 * found a bitmap, we want to go left, or before
545 WARN_ON(info
->bitmap
);
548 WARN_ON(!info
->bitmap
);
554 rb_link_node(node
, parent
, p
);
555 rb_insert_color(node
, root
);
561 * searches the tree for the given offset.
563 * fuzzy - If this is set, then we are trying to make an allocation, and we just
564 * want a section that has at least bytes size and comes at or after the given
567 static struct btrfs_free_space
*
568 tree_search_offset(struct btrfs_block_group_cache
*block_group
,
569 u64 offset
, int bitmap_only
, int fuzzy
)
571 struct rb_node
*n
= block_group
->free_space_offset
.rb_node
;
572 struct btrfs_free_space
*entry
, *prev
= NULL
;
574 /* find entry that is closest to the 'offset' */
581 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
584 if (offset
< entry
->offset
)
586 else if (offset
> entry
->offset
)
599 * bitmap entry and extent entry may share same offset,
600 * in that case, bitmap entry comes after extent entry.
605 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
606 if (entry
->offset
!= offset
)
609 WARN_ON(!entry
->bitmap
);
614 * if previous extent entry covers the offset,
615 * we should return it instead of the bitmap entry
617 n
= &entry
->offset_index
;
622 prev
= rb_entry(n
, struct btrfs_free_space
,
625 if (prev
->offset
+ prev
->bytes
> offset
)
637 /* find last entry before the 'offset' */
639 if (entry
->offset
> offset
) {
640 n
= rb_prev(&entry
->offset_index
);
642 entry
= rb_entry(n
, struct btrfs_free_space
,
644 BUG_ON(entry
->offset
> offset
);
654 n
= &entry
->offset_index
;
659 prev
= rb_entry(n
, struct btrfs_free_space
,
662 if (prev
->offset
+ prev
->bytes
> offset
)
667 if (entry
->offset
+ BITS_PER_BITMAP
*
668 block_group
->sectorsize
> offset
)
670 } else if (entry
->offset
+ entry
->bytes
> offset
)
678 if (entry
->offset
+ BITS_PER_BITMAP
*
679 block_group
->sectorsize
> offset
)
682 if (entry
->offset
+ entry
->bytes
> offset
)
686 n
= rb_next(&entry
->offset_index
);
689 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
694 static void unlink_free_space(struct btrfs_block_group_cache
*block_group
,
695 struct btrfs_free_space
*info
)
697 rb_erase(&info
->offset_index
, &block_group
->free_space_offset
);
698 block_group
->free_extents
--;
699 block_group
->free_space
-= info
->bytes
;
702 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
703 struct btrfs_free_space
*info
)
707 BUG_ON(!info
->bitmap
&& !info
->bytes
);
708 ret
= tree_insert_offset(&block_group
->free_space_offset
, info
->offset
,
709 &info
->offset_index
, (info
->bitmap
!= NULL
));
713 block_group
->free_space
+= info
->bytes
;
714 block_group
->free_extents
++;
718 static void recalculate_thresholds(struct btrfs_block_group_cache
*block_group
)
725 * The goal is to keep the total amount of memory used per 1gb of space
726 * at or below 32k, so we need to adjust how much memory we allow to be
727 * used by extent based free space tracking
729 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
730 (div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024));
733 * we want to account for 1 more bitmap than what we have so we can make
734 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
735 * we add more bitmaps.
737 bitmap_bytes
= (block_group
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
739 if (bitmap_bytes
>= max_bytes
) {
740 block_group
->extents_thresh
= 0;
745 * we want the extent entry threshold to always be at most 1/2 the maxw
746 * bytes we can have, or whatever is less than that.
748 extent_bytes
= max_bytes
- bitmap_bytes
;
749 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
751 block_group
->extents_thresh
=
752 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
755 static void bitmap_clear_bits(struct btrfs_block_group_cache
*block_group
,
756 struct btrfs_free_space
*info
, u64 offset
,
759 unsigned long start
, end
;
762 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
763 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
764 BUG_ON(end
> BITS_PER_BITMAP
);
766 for (i
= start
; i
< end
; i
++)
767 clear_bit(i
, info
->bitmap
);
769 info
->bytes
-= bytes
;
770 block_group
->free_space
-= bytes
;
773 static void bitmap_set_bits(struct btrfs_block_group_cache
*block_group
,
774 struct btrfs_free_space
*info
, u64 offset
,
777 unsigned long start
, end
;
780 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
781 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
782 BUG_ON(end
> BITS_PER_BITMAP
);
784 for (i
= start
; i
< end
; i
++)
785 set_bit(i
, info
->bitmap
);
787 info
->bytes
+= bytes
;
788 block_group
->free_space
+= bytes
;
791 static int search_bitmap(struct btrfs_block_group_cache
*block_group
,
792 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
795 unsigned long found_bits
= 0;
796 unsigned long bits
, i
;
797 unsigned long next_zero
;
799 i
= offset_to_bit(bitmap_info
->offset
, block_group
->sectorsize
,
800 max_t(u64
, *offset
, bitmap_info
->offset
));
801 bits
= bytes_to_bits(*bytes
, block_group
->sectorsize
);
803 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
805 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
806 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
808 if ((next_zero
- i
) >= bits
) {
809 found_bits
= next_zero
- i
;
816 *offset
= (u64
)(i
* block_group
->sectorsize
) +
818 *bytes
= (u64
)(found_bits
) * block_group
->sectorsize
;
825 static struct btrfs_free_space
*find_free_space(struct btrfs_block_group_cache
826 *block_group
, u64
*offset
,
827 u64
*bytes
, int debug
)
829 struct btrfs_free_space
*entry
;
830 struct rb_node
*node
;
833 if (!block_group
->free_space_offset
.rb_node
)
836 entry
= tree_search_offset(block_group
,
837 offset_to_bitmap(block_group
, *offset
),
842 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
843 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
844 if (entry
->bytes
< *bytes
)
848 ret
= search_bitmap(block_group
, entry
, offset
, bytes
);
854 *offset
= entry
->offset
;
855 *bytes
= entry
->bytes
;
862 static void add_new_bitmap(struct btrfs_block_group_cache
*block_group
,
863 struct btrfs_free_space
*info
, u64 offset
)
865 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
866 int max_bitmaps
= (int)div64_u64(block_group
->key
.offset
+
867 bytes_per_bg
- 1, bytes_per_bg
);
868 BUG_ON(block_group
->total_bitmaps
>= max_bitmaps
);
870 info
->offset
= offset_to_bitmap(block_group
, offset
);
872 link_free_space(block_group
, info
);
873 block_group
->total_bitmaps
++;
875 recalculate_thresholds(block_group
);
878 static noinline
int remove_from_bitmap(struct btrfs_block_group_cache
*block_group
,
879 struct btrfs_free_space
*bitmap_info
,
880 u64
*offset
, u64
*bytes
)
883 u64 search_start
, search_bytes
;
887 end
= bitmap_info
->offset
+
888 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
) - 1;
891 * XXX - this can go away after a few releases.
893 * since the only user of btrfs_remove_free_space is the tree logging
894 * stuff, and the only way to test that is under crash conditions, we
895 * want to have this debug stuff here just in case somethings not
896 * working. Search the bitmap for the space we are trying to use to
897 * make sure its actually there. If its not there then we need to stop
898 * because something has gone wrong.
900 search_start
= *offset
;
901 search_bytes
= *bytes
;
902 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
904 BUG_ON(ret
< 0 || search_start
!= *offset
);
906 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
907 bitmap_clear_bits(block_group
, bitmap_info
, *offset
,
909 *bytes
-= end
- *offset
+ 1;
911 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
912 bitmap_clear_bits(block_group
, bitmap_info
, *offset
, *bytes
);
917 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
918 if (!bitmap_info
->bytes
) {
919 unlink_free_space(block_group
, bitmap_info
);
920 kfree(bitmap_info
->bitmap
);
922 block_group
->total_bitmaps
--;
923 recalculate_thresholds(block_group
);
927 * no entry after this bitmap, but we still have bytes to
928 * remove, so something has gone wrong.
933 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
937 * if the next entry isn't a bitmap we need to return to let the
938 * extent stuff do its work.
940 if (!bitmap_info
->bitmap
)
944 * Ok the next item is a bitmap, but it may not actually hold
945 * the information for the rest of this free space stuff, so
946 * look for it, and if we don't find it return so we can try
947 * everything over again.
949 search_start
= *offset
;
950 search_bytes
= *bytes
;
951 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
953 if (ret
< 0 || search_start
!= *offset
)
957 } else if (!bitmap_info
->bytes
) {
958 unlink_free_space(block_group
, bitmap_info
);
959 kfree(bitmap_info
->bitmap
);
961 block_group
->total_bitmaps
--;
962 recalculate_thresholds(block_group
);
968 static int insert_into_bitmap(struct btrfs_block_group_cache
*block_group
,
969 struct btrfs_free_space
*info
)
971 struct btrfs_free_space
*bitmap_info
;
973 u64 bytes
, offset
, end
;
977 * If we are below the extents threshold then we can add this as an
978 * extent, and don't have to deal with the bitmap
980 if (block_group
->free_extents
< block_group
->extents_thresh
&&
981 info
->bytes
> block_group
->sectorsize
* 4)
985 * some block groups are so tiny they can't be enveloped by a bitmap, so
986 * don't even bother to create a bitmap for this
988 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
989 block_group
->key
.offset
)
993 offset
= info
->offset
;
996 bitmap_info
= tree_search_offset(block_group
,
997 offset_to_bitmap(block_group
, offset
),
1004 end
= bitmap_info
->offset
+
1005 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
);
1007 if (offset
>= bitmap_info
->offset
&& offset
+ bytes
> end
) {
1008 bitmap_set_bits(block_group
, bitmap_info
, offset
,
1010 bytes
-= end
- offset
;
1013 } else if (offset
>= bitmap_info
->offset
&& offset
+ bytes
<= end
) {
1014 bitmap_set_bits(block_group
, bitmap_info
, offset
, bytes
);
1027 if (info
&& info
->bitmap
) {
1028 add_new_bitmap(block_group
, info
, offset
);
1033 spin_unlock(&block_group
->tree_lock
);
1035 /* no pre-allocated info, allocate a new one */
1037 info
= kzalloc(sizeof(struct btrfs_free_space
),
1040 spin_lock(&block_group
->tree_lock
);
1046 /* allocate the bitmap */
1047 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1048 spin_lock(&block_group
->tree_lock
);
1049 if (!info
->bitmap
) {
1059 kfree(info
->bitmap
);
1066 int btrfs_add_free_space(struct btrfs_block_group_cache
*block_group
,
1067 u64 offset
, u64 bytes
)
1069 struct btrfs_free_space
*right_info
= NULL
;
1070 struct btrfs_free_space
*left_info
= NULL
;
1071 struct btrfs_free_space
*info
= NULL
;
1074 info
= kzalloc(sizeof(struct btrfs_free_space
), GFP_NOFS
);
1078 info
->offset
= offset
;
1079 info
->bytes
= bytes
;
1081 spin_lock(&block_group
->tree_lock
);
1084 * first we want to see if there is free space adjacent to the range we
1085 * are adding, if there is remove that struct and add a new one to
1086 * cover the entire range
1088 right_info
= tree_search_offset(block_group
, offset
+ bytes
, 0, 0);
1089 if (right_info
&& rb_prev(&right_info
->offset_index
))
1090 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1091 struct btrfs_free_space
, offset_index
);
1093 left_info
= tree_search_offset(block_group
, offset
- 1, 0, 0);
1096 * If there was no extent directly to the left or right of this new
1097 * extent then we know we're going to have to allocate a new extent, so
1098 * before we do that see if we need to drop this into a bitmap
1100 if ((!left_info
|| left_info
->bitmap
) &&
1101 (!right_info
|| right_info
->bitmap
)) {
1102 ret
= insert_into_bitmap(block_group
, info
);
1112 if (right_info
&& !right_info
->bitmap
) {
1113 unlink_free_space(block_group
, right_info
);
1114 info
->bytes
+= right_info
->bytes
;
1118 if (left_info
&& !left_info
->bitmap
&&
1119 left_info
->offset
+ left_info
->bytes
== offset
) {
1120 unlink_free_space(block_group
, left_info
);
1121 info
->offset
= left_info
->offset
;
1122 info
->bytes
+= left_info
->bytes
;
1126 ret
= link_free_space(block_group
, info
);
1130 spin_unlock(&block_group
->tree_lock
);
1133 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1134 BUG_ON(ret
== -EEXIST
);
1140 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1141 u64 offset
, u64 bytes
)
1143 struct btrfs_free_space
*info
;
1144 struct btrfs_free_space
*next_info
= NULL
;
1147 spin_lock(&block_group
->tree_lock
);
1150 info
= tree_search_offset(block_group
, offset
, 0, 0);
1153 * oops didn't find an extent that matched the space we wanted
1154 * to remove, look for a bitmap instead
1156 info
= tree_search_offset(block_group
,
1157 offset_to_bitmap(block_group
, offset
),
1165 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1167 next_info
= rb_entry(rb_next(&info
->offset_index
),
1168 struct btrfs_free_space
,
1171 if (next_info
->bitmap
)
1172 end
= next_info
->offset
+ BITS_PER_BITMAP
*
1173 block_group
->sectorsize
- 1;
1175 end
= next_info
->offset
+ next_info
->bytes
;
1177 if (next_info
->bytes
< bytes
||
1178 next_info
->offset
> offset
|| offset
> end
) {
1179 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1180 " trying to use %llu\n",
1181 (unsigned long long)info
->offset
,
1182 (unsigned long long)info
->bytes
,
1183 (unsigned long long)bytes
);
1192 if (info
->bytes
== bytes
) {
1193 unlink_free_space(block_group
, info
);
1195 kfree(info
->bitmap
);
1196 block_group
->total_bitmaps
--;
1202 if (!info
->bitmap
&& info
->offset
== offset
) {
1203 unlink_free_space(block_group
, info
);
1204 info
->offset
+= bytes
;
1205 info
->bytes
-= bytes
;
1206 link_free_space(block_group
, info
);
1210 if (!info
->bitmap
&& info
->offset
<= offset
&&
1211 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1212 u64 old_start
= info
->offset
;
1214 * we're freeing space in the middle of the info,
1215 * this can happen during tree log replay
1217 * first unlink the old info and then
1218 * insert it again after the hole we're creating
1220 unlink_free_space(block_group
, info
);
1221 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1222 u64 old_end
= info
->offset
+ info
->bytes
;
1224 info
->offset
= offset
+ bytes
;
1225 info
->bytes
= old_end
- info
->offset
;
1226 ret
= link_free_space(block_group
, info
);
1231 /* the hole we're creating ends at the end
1232 * of the info struct, just free the info
1236 spin_unlock(&block_group
->tree_lock
);
1238 /* step two, insert a new info struct to cover
1239 * anything before the hole
1241 ret
= btrfs_add_free_space(block_group
, old_start
,
1242 offset
- old_start
);
1247 ret
= remove_from_bitmap(block_group
, info
, &offset
, &bytes
);
1252 spin_unlock(&block_group
->tree_lock
);
1257 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1260 struct btrfs_free_space
*info
;
1264 for (n
= rb_first(&block_group
->free_space_offset
); n
; n
= rb_next(n
)) {
1265 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1266 if (info
->bytes
>= bytes
)
1268 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1269 (unsigned long long)info
->offset
,
1270 (unsigned long long)info
->bytes
,
1271 (info
->bitmap
) ? "yes" : "no");
1273 printk(KERN_INFO
"block group has cluster?: %s\n",
1274 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1275 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1279 u64
btrfs_block_group_free_space(struct btrfs_block_group_cache
*block_group
)
1281 struct btrfs_free_space
*info
;
1285 for (n
= rb_first(&block_group
->free_space_offset
); n
;
1287 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1295 * for a given cluster, put all of its extents back into the free
1296 * space cache. If the block group passed doesn't match the block group
1297 * pointed to by the cluster, someone else raced in and freed the
1298 * cluster already. In that case, we just return without changing anything
1301 __btrfs_return_cluster_to_free_space(
1302 struct btrfs_block_group_cache
*block_group
,
1303 struct btrfs_free_cluster
*cluster
)
1305 struct btrfs_free_space
*entry
;
1306 struct rb_node
*node
;
1309 spin_lock(&cluster
->lock
);
1310 if (cluster
->block_group
!= block_group
)
1313 bitmap
= cluster
->points_to_bitmap
;
1314 cluster
->block_group
= NULL
;
1315 cluster
->window_start
= 0;
1316 list_del_init(&cluster
->block_group_list
);
1317 cluster
->points_to_bitmap
= false;
1322 node
= rb_first(&cluster
->root
);
1324 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1325 node
= rb_next(&entry
->offset_index
);
1326 rb_erase(&entry
->offset_index
, &cluster
->root
);
1327 BUG_ON(entry
->bitmap
);
1328 tree_insert_offset(&block_group
->free_space_offset
,
1329 entry
->offset
, &entry
->offset_index
, 0);
1331 cluster
->root
= RB_ROOT
;
1334 spin_unlock(&cluster
->lock
);
1335 btrfs_put_block_group(block_group
);
1339 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
1341 struct btrfs_free_space
*info
;
1342 struct rb_node
*node
;
1343 struct btrfs_free_cluster
*cluster
;
1344 struct list_head
*head
;
1346 spin_lock(&block_group
->tree_lock
);
1347 while ((head
= block_group
->cluster_list
.next
) !=
1348 &block_group
->cluster_list
) {
1349 cluster
= list_entry(head
, struct btrfs_free_cluster
,
1352 WARN_ON(cluster
->block_group
!= block_group
);
1353 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1354 if (need_resched()) {
1355 spin_unlock(&block_group
->tree_lock
);
1357 spin_lock(&block_group
->tree_lock
);
1361 while ((node
= rb_last(&block_group
->free_space_offset
)) != NULL
) {
1362 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1363 unlink_free_space(block_group
, info
);
1365 kfree(info
->bitmap
);
1367 if (need_resched()) {
1368 spin_unlock(&block_group
->tree_lock
);
1370 spin_lock(&block_group
->tree_lock
);
1374 spin_unlock(&block_group
->tree_lock
);
1377 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
1378 u64 offset
, u64 bytes
, u64 empty_size
)
1380 struct btrfs_free_space
*entry
= NULL
;
1381 u64 bytes_search
= bytes
+ empty_size
;
1384 spin_lock(&block_group
->tree_lock
);
1385 entry
= find_free_space(block_group
, &offset
, &bytes_search
, 0);
1390 if (entry
->bitmap
) {
1391 bitmap_clear_bits(block_group
, entry
, offset
, bytes
);
1392 if (!entry
->bytes
) {
1393 unlink_free_space(block_group
, entry
);
1394 kfree(entry
->bitmap
);
1396 block_group
->total_bitmaps
--;
1397 recalculate_thresholds(block_group
);
1400 unlink_free_space(block_group
, entry
);
1401 entry
->offset
+= bytes
;
1402 entry
->bytes
-= bytes
;
1406 link_free_space(block_group
, entry
);
1410 spin_unlock(&block_group
->tree_lock
);
1416 * given a cluster, put all of its extents back into the free space
1417 * cache. If a block group is passed, this function will only free
1418 * a cluster that belongs to the passed block group.
1420 * Otherwise, it'll get a reference on the block group pointed to by the
1421 * cluster and remove the cluster from it.
1423 int btrfs_return_cluster_to_free_space(
1424 struct btrfs_block_group_cache
*block_group
,
1425 struct btrfs_free_cluster
*cluster
)
1429 /* first, get a safe pointer to the block group */
1430 spin_lock(&cluster
->lock
);
1432 block_group
= cluster
->block_group
;
1434 spin_unlock(&cluster
->lock
);
1437 } else if (cluster
->block_group
!= block_group
) {
1438 /* someone else has already freed it don't redo their work */
1439 spin_unlock(&cluster
->lock
);
1442 atomic_inc(&block_group
->count
);
1443 spin_unlock(&cluster
->lock
);
1445 /* now return any extents the cluster had on it */
1446 spin_lock(&block_group
->tree_lock
);
1447 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1448 spin_unlock(&block_group
->tree_lock
);
1450 /* finally drop our ref */
1451 btrfs_put_block_group(block_group
);
1455 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1456 struct btrfs_free_cluster
*cluster
,
1457 u64 bytes
, u64 min_start
)
1459 struct btrfs_free_space
*entry
;
1461 u64 search_start
= cluster
->window_start
;
1462 u64 search_bytes
= bytes
;
1465 spin_lock(&block_group
->tree_lock
);
1466 spin_lock(&cluster
->lock
);
1468 if (!cluster
->points_to_bitmap
)
1471 if (cluster
->block_group
!= block_group
)
1475 * search_start is the beginning of the bitmap, but at some point it may
1476 * be a good idea to point to the actual start of the free area in the
1477 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1478 * to 1 to make sure we get the bitmap entry
1480 entry
= tree_search_offset(block_group
,
1481 offset_to_bitmap(block_group
, search_start
),
1483 if (!entry
|| !entry
->bitmap
)
1486 search_start
= min_start
;
1487 search_bytes
= bytes
;
1489 err
= search_bitmap(block_group
, entry
, &search_start
,
1495 bitmap_clear_bits(block_group
, entry
, ret
, bytes
);
1497 spin_unlock(&cluster
->lock
);
1498 spin_unlock(&block_group
->tree_lock
);
1504 * given a cluster, try to allocate 'bytes' from it, returns 0
1505 * if it couldn't find anything suitably large, or a logical disk offset
1506 * if things worked out
1508 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
1509 struct btrfs_free_cluster
*cluster
, u64 bytes
,
1512 struct btrfs_free_space
*entry
= NULL
;
1513 struct rb_node
*node
;
1516 if (cluster
->points_to_bitmap
)
1517 return btrfs_alloc_from_bitmap(block_group
, cluster
, bytes
,
1520 spin_lock(&cluster
->lock
);
1521 if (bytes
> cluster
->max_size
)
1524 if (cluster
->block_group
!= block_group
)
1527 node
= rb_first(&cluster
->root
);
1531 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1534 if (entry
->bytes
< bytes
|| entry
->offset
< min_start
) {
1535 struct rb_node
*node
;
1537 node
= rb_next(&entry
->offset_index
);
1540 entry
= rb_entry(node
, struct btrfs_free_space
,
1544 ret
= entry
->offset
;
1546 entry
->offset
+= bytes
;
1547 entry
->bytes
-= bytes
;
1549 if (entry
->bytes
== 0) {
1550 rb_erase(&entry
->offset_index
, &cluster
->root
);
1556 spin_unlock(&cluster
->lock
);
1561 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
1562 struct btrfs_free_space
*entry
,
1563 struct btrfs_free_cluster
*cluster
,
1564 u64 offset
, u64 bytes
, u64 min_bytes
)
1566 unsigned long next_zero
;
1568 unsigned long search_bits
;
1569 unsigned long total_bits
;
1570 unsigned long found_bits
;
1571 unsigned long start
= 0;
1572 unsigned long total_found
= 0;
1575 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
1576 max_t(u64
, offset
, entry
->offset
));
1577 search_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
1578 total_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
1582 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
1583 i
< BITS_PER_BITMAP
;
1584 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1585 next_zero
= find_next_zero_bit(entry
->bitmap
,
1586 BITS_PER_BITMAP
, i
);
1587 if (next_zero
- i
>= search_bits
) {
1588 found_bits
= next_zero
- i
;
1602 total_found
+= found_bits
;
1604 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
1605 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
1607 if (total_found
< total_bits
) {
1608 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, next_zero
);
1609 if (i
- start
> total_bits
* 2) {
1611 cluster
->max_size
= 0;
1617 cluster
->window_start
= start
* block_group
->sectorsize
+
1619 cluster
->points_to_bitmap
= true;
1625 * here we try to find a cluster of blocks in a block group. The goal
1626 * is to find at least bytes free and up to empty_size + bytes free.
1627 * We might not find them all in one contiguous area.
1629 * returns zero and sets up cluster if things worked out, otherwise
1630 * it returns -enospc
1632 int btrfs_find_space_cluster(struct btrfs_trans_handle
*trans
,
1633 struct btrfs_root
*root
,
1634 struct btrfs_block_group_cache
*block_group
,
1635 struct btrfs_free_cluster
*cluster
,
1636 u64 offset
, u64 bytes
, u64 empty_size
)
1638 struct btrfs_free_space
*entry
= NULL
;
1639 struct rb_node
*node
;
1640 struct btrfs_free_space
*next
;
1641 struct btrfs_free_space
*last
= NULL
;
1646 bool found_bitmap
= false;
1649 /* for metadata, allow allocates with more holes */
1650 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
1651 min_bytes
= bytes
+ empty_size
;
1652 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
1654 * we want to do larger allocations when we are
1655 * flushing out the delayed refs, it helps prevent
1656 * making more work as we go along.
1658 if (trans
->transaction
->delayed_refs
.flushing
)
1659 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 1);
1661 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 4);
1663 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
1665 spin_lock(&block_group
->tree_lock
);
1666 spin_lock(&cluster
->lock
);
1668 /* someone already found a cluster, hooray */
1669 if (cluster
->block_group
) {
1674 entry
= tree_search_offset(block_group
, offset
, found_bitmap
, 1);
1681 * If found_bitmap is true, we exhausted our search for extent entries,
1682 * and we just want to search all of the bitmaps that we can find, and
1683 * ignore any extent entries we find.
1685 while (entry
->bitmap
|| found_bitmap
||
1686 (!entry
->bitmap
&& entry
->bytes
< min_bytes
)) {
1687 struct rb_node
*node
= rb_next(&entry
->offset_index
);
1689 if (entry
->bitmap
&& entry
->bytes
> bytes
+ empty_size
) {
1690 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
,
1691 offset
, bytes
+ empty_size
,
1701 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1705 * We already searched all the extent entries from the passed in offset
1706 * to the end and didn't find enough space for the cluster, and we also
1707 * didn't find any bitmaps that met our criteria, just go ahead and exit
1714 cluster
->points_to_bitmap
= false;
1715 window_start
= entry
->offset
;
1716 window_free
= entry
->bytes
;
1718 max_extent
= entry
->bytes
;
1721 /* out window is just right, lets fill it */
1722 if (window_free
>= bytes
+ empty_size
)
1725 node
= rb_next(&last
->offset_index
);
1732 next
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1735 * we found a bitmap, so if this search doesn't result in a
1736 * cluster, we know to go and search again for the bitmaps and
1737 * start looking for space there
1741 offset
= next
->offset
;
1742 found_bitmap
= true;
1748 * we haven't filled the empty size and the window is
1749 * very large. reset and try again
1751 if (next
->offset
- (last
->offset
+ last
->bytes
) > 128 * 1024 ||
1752 next
->offset
- window_start
> (bytes
+ empty_size
) * 2) {
1754 window_start
= entry
->offset
;
1755 window_free
= entry
->bytes
;
1757 max_extent
= entry
->bytes
;
1760 window_free
+= next
->bytes
;
1761 if (entry
->bytes
> max_extent
)
1762 max_extent
= entry
->bytes
;
1766 cluster
->window_start
= entry
->offset
;
1769 * now we've found our entries, pull them out of the free space
1770 * cache and put them into the cluster rbtree
1772 * The cluster includes an rbtree, but only uses the offset index
1773 * of each free space cache entry.
1776 node
= rb_next(&entry
->offset_index
);
1777 if (entry
->bitmap
&& node
) {
1778 entry
= rb_entry(node
, struct btrfs_free_space
,
1781 } else if (entry
->bitmap
&& !node
) {
1785 rb_erase(&entry
->offset_index
, &block_group
->free_space_offset
);
1786 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
1787 &entry
->offset_index
, 0);
1790 if (!node
|| entry
== last
)
1793 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1796 cluster
->max_size
= max_extent
;
1799 atomic_inc(&block_group
->count
);
1800 list_add_tail(&cluster
->block_group_list
, &block_group
->cluster_list
);
1801 cluster
->block_group
= block_group
;
1803 spin_unlock(&cluster
->lock
);
1804 spin_unlock(&block_group
->tree_lock
);
1810 * simple code to zero out a cluster
1812 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
1814 spin_lock_init(&cluster
->lock
);
1815 spin_lock_init(&cluster
->refill_lock
);
1816 cluster
->root
= RB_ROOT
;
1817 cluster
->max_size
= 0;
1818 cluster
->points_to_bitmap
= false;
1819 INIT_LIST_HEAD(&cluster
->block_group_list
);
1820 cluster
->block_group
= NULL
;