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 static int readahead_cache(struct inode
*inode
)
192 struct file_ra_state
*ra
;
193 unsigned long last_index
;
195 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
199 file_ra_state_init(ra
, inode
->i_mapping
);
200 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
202 page_cache_sync_readahead(inode
->i_mapping
, ra
, NULL
, 0, last_index
);
209 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
210 struct btrfs_block_group_cache
*block_group
)
212 struct btrfs_root
*root
= fs_info
->tree_root
;
214 struct btrfs_free_space_header
*header
;
215 struct extent_buffer
*leaf
;
217 struct btrfs_path
*path
;
218 u32
*checksums
= NULL
, *crc
;
219 char *disk_crcs
= NULL
;
220 struct btrfs_key key
;
221 struct list_head bitmaps
;
225 u32 cur_crc
= ~(u32
)0;
227 unsigned long first_page_offset
;
232 * If we're unmounting then just return, since this does a search on the
233 * normal root and not the commit root and we could deadlock.
236 if (fs_info
->closing
)
240 * If this block group has been marked to be cleared for one reason or
241 * another then we can't trust the on disk cache, so just return.
243 spin_lock(&block_group
->lock
);
244 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
245 spin_unlock(&block_group
->lock
);
248 spin_unlock(&block_group
->lock
);
250 INIT_LIST_HEAD(&bitmaps
);
252 path
= btrfs_alloc_path();
256 inode
= lookup_free_space_inode(root
, block_group
, path
);
258 btrfs_free_path(path
);
262 /* Nothing in the space cache, goodbye */
263 if (!i_size_read(inode
)) {
264 btrfs_free_path(path
);
268 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
269 key
.offset
= block_group
->key
.objectid
;
272 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
274 btrfs_free_path(path
);
278 leaf
= path
->nodes
[0];
279 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
280 struct btrfs_free_space_header
);
281 num_entries
= btrfs_free_space_entries(leaf
, header
);
282 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
283 generation
= btrfs_free_space_generation(leaf
, header
);
284 btrfs_free_path(path
);
286 if (BTRFS_I(inode
)->generation
!= generation
) {
287 printk(KERN_ERR
"btrfs: free space inode generation (%llu) did"
288 " not match free space cache generation (%llu) for "
289 "block group %llu\n",
290 (unsigned long long)BTRFS_I(inode
)->generation
,
291 (unsigned long long)generation
,
292 (unsigned long long)block_group
->key
.objectid
);
299 /* Setup everything for doing checksumming */
300 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
301 checksums
= crc
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
304 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
305 disk_crcs
= kzalloc(first_page_offset
, GFP_NOFS
);
309 ret
= readahead_cache(inode
);
316 struct btrfs_free_space_entry
*entry
;
317 struct btrfs_free_space
*e
;
319 unsigned long offset
= 0;
320 unsigned long start_offset
= 0;
323 if (!num_entries
&& !num_bitmaps
)
327 start_offset
= first_page_offset
;
328 offset
= start_offset
;
331 page
= grab_cache_page(inode
->i_mapping
, index
);
337 if (!PageUptodate(page
)) {
338 btrfs_readpage(NULL
, page
);
340 if (!PageUptodate(page
)) {
342 page_cache_release(page
);
343 printk(KERN_ERR
"btrfs: error reading free "
344 "space cache: %llu\n",
346 block_group
->key
.objectid
);
355 memcpy(disk_crcs
, addr
, first_page_offset
);
356 gen
= addr
+ (sizeof(u32
) * num_checksums
);
357 if (*gen
!= BTRFS_I(inode
)->generation
) {
358 printk(KERN_ERR
"btrfs: space cache generation"
359 " (%llu) does not match inode (%llu) "
360 "for block group %llu\n",
361 (unsigned long long)*gen
,
363 BTRFS_I(inode
)->generation
,
365 block_group
->key
.objectid
);
368 page_cache_release(page
);
371 crc
= (u32
*)disk_crcs
;
373 entry
= addr
+ start_offset
;
375 /* First lets check our crc before we do anything fun */
377 cur_crc
= btrfs_csum_data(root
, addr
+ start_offset
, cur_crc
,
378 PAGE_CACHE_SIZE
- start_offset
);
379 btrfs_csum_final(cur_crc
, (char *)&cur_crc
);
380 if (cur_crc
!= *crc
) {
381 printk(KERN_ERR
"btrfs: crc mismatch for page %lu in "
382 "block group %llu\n", index
,
383 (unsigned long long)block_group
->key
.objectid
);
386 page_cache_release(page
);
396 e
= kzalloc(sizeof(struct btrfs_free_space
), GFP_NOFS
);
400 page_cache_release(page
);
404 e
->offset
= le64_to_cpu(entry
->offset
);
405 e
->bytes
= le64_to_cpu(entry
->bytes
);
410 page_cache_release(page
);
414 if (entry
->type
== BTRFS_FREE_SPACE_EXTENT
) {
415 spin_lock(&block_group
->tree_lock
);
416 ret
= link_free_space(block_group
, e
);
417 spin_unlock(&block_group
->tree_lock
);
420 e
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
425 page_cache_release(page
);
428 spin_lock(&block_group
->tree_lock
);
429 ret
= link_free_space(block_group
, e
);
430 block_group
->total_bitmaps
++;
431 recalculate_thresholds(block_group
);
432 spin_unlock(&block_group
->tree_lock
);
433 list_add_tail(&e
->list
, &bitmaps
);
437 offset
+= sizeof(struct btrfs_free_space_entry
);
438 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
445 * We read an entry out of this page, we need to move on to the
454 * We add the bitmaps at the end of the entries in order that
455 * the bitmap entries are added to the cache.
457 e
= list_entry(bitmaps
.next
, struct btrfs_free_space
, list
);
458 list_del_init(&e
->list
);
459 memcpy(e
->bitmap
, addr
, PAGE_CACHE_SIZE
);
464 page_cache_release(page
);
476 /* This cache is bogus, make sure it gets cleared */
477 spin_lock(&block_group
->lock
);
478 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
479 spin_unlock(&block_group
->lock
);
480 btrfs_remove_free_space_cache(block_group
);
484 int btrfs_write_out_cache(struct btrfs_root
*root
,
485 struct btrfs_trans_handle
*trans
,
486 struct btrfs_block_group_cache
*block_group
,
487 struct btrfs_path
*path
)
489 struct btrfs_free_space_header
*header
;
490 struct extent_buffer
*leaf
;
492 struct rb_node
*node
;
493 struct list_head
*pos
, *n
;
495 struct extent_state
*cached_state
= NULL
;
496 struct list_head bitmap_list
;
497 struct btrfs_key key
;
499 u32
*crc
, *checksums
;
500 pgoff_t index
= 0, last_index
= 0;
501 unsigned long first_page_offset
;
507 root
= root
->fs_info
->tree_root
;
509 INIT_LIST_HEAD(&bitmap_list
);
511 spin_lock(&block_group
->lock
);
512 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
513 spin_unlock(&block_group
->lock
);
516 spin_unlock(&block_group
->lock
);
518 inode
= lookup_free_space_inode(root
, block_group
, path
);
522 if (!i_size_read(inode
)) {
527 node
= rb_first(&block_group
->free_space_offset
);
533 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
534 filemap_write_and_wait(inode
->i_mapping
);
535 btrfs_wait_ordered_range(inode
, inode
->i_size
&
536 ~(root
->sectorsize
- 1), (u64
)-1);
538 /* We need a checksum per page. */
539 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
540 crc
= checksums
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
546 /* Since the first page has all of our checksums and our generation we
547 * need to calculate the offset into the page that we can start writing
550 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
553 * Lock all pages first so we can lock the extent safely.
555 * NOTE: Because we hold the ref the entire time we're going to write to
556 * the page find_get_page should never fail, so we don't do a check
557 * after find_get_page at this point. Just putting this here so people
558 * know and don't freak out.
560 while (index
<= last_index
) {
561 page
= grab_cache_page(inode
->i_mapping
, index
);
566 page
= find_get_page(inode
->i_mapping
, i
);
568 page_cache_release(page
);
569 page_cache_release(page
);
578 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
579 0, &cached_state
, GFP_NOFS
);
581 /* Write out the extent entries */
583 struct btrfs_free_space_entry
*entry
;
585 unsigned long offset
= 0;
586 unsigned long start_offset
= 0;
589 start_offset
= first_page_offset
;
590 offset
= start_offset
;
593 page
= find_get_page(inode
->i_mapping
, index
);
596 entry
= addr
+ start_offset
;
598 memset(addr
, 0, PAGE_CACHE_SIZE
);
600 struct btrfs_free_space
*e
;
602 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
605 entry
->offset
= cpu_to_le64(e
->offset
);
606 entry
->bytes
= cpu_to_le64(e
->bytes
);
608 entry
->type
= BTRFS_FREE_SPACE_BITMAP
;
609 list_add_tail(&e
->list
, &bitmap_list
);
612 entry
->type
= BTRFS_FREE_SPACE_EXTENT
;
614 node
= rb_next(node
);
617 offset
+= sizeof(struct btrfs_free_space_entry
);
618 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
624 *crc
= btrfs_csum_data(root
, addr
+ start_offset
, *crc
,
625 PAGE_CACHE_SIZE
- start_offset
);
628 btrfs_csum_final(*crc
, (char *)crc
);
631 bytes
+= PAGE_CACHE_SIZE
;
633 ClearPageChecked(page
);
634 set_page_extent_mapped(page
);
635 SetPageUptodate(page
);
636 set_page_dirty(page
);
639 * We need to release our reference we got for grab_cache_page,
640 * except for the first page which will hold our checksums, we
645 page_cache_release(page
);
648 page_cache_release(page
);
653 /* Write out the bitmaps */
654 list_for_each_safe(pos
, n
, &bitmap_list
) {
656 struct btrfs_free_space
*entry
=
657 list_entry(pos
, struct btrfs_free_space
, list
);
659 page
= find_get_page(inode
->i_mapping
, index
);
662 memcpy(addr
, entry
->bitmap
, PAGE_CACHE_SIZE
);
664 *crc
= btrfs_csum_data(root
, addr
, *crc
, PAGE_CACHE_SIZE
);
666 btrfs_csum_final(*crc
, (char *)crc
);
668 bytes
+= PAGE_CACHE_SIZE
;
670 ClearPageChecked(page
);
671 set_page_extent_mapped(page
);
672 SetPageUptodate(page
);
673 set_page_dirty(page
);
675 page_cache_release(page
);
676 page_cache_release(page
);
677 list_del_init(&entry
->list
);
681 /* Zero out the rest of the pages just to make sure */
682 while (index
<= last_index
) {
685 page
= find_get_page(inode
->i_mapping
, index
);
688 memset(addr
, 0, PAGE_CACHE_SIZE
);
690 ClearPageChecked(page
);
691 set_page_extent_mapped(page
);
692 SetPageUptodate(page
);
693 set_page_dirty(page
);
695 page_cache_release(page
);
696 page_cache_release(page
);
697 bytes
+= PAGE_CACHE_SIZE
;
701 btrfs_set_extent_delalloc(inode
, 0, bytes
- 1, &cached_state
);
703 /* Write the checksums and trans id to the first page */
708 page
= find_get_page(inode
->i_mapping
, 0);
711 memcpy(addr
, checksums
, sizeof(u32
) * num_checksums
);
712 gen
= addr
+ (sizeof(u32
) * num_checksums
);
713 *gen
= trans
->transid
;
715 ClearPageChecked(page
);
716 set_page_extent_mapped(page
);
717 SetPageUptodate(page
);
718 set_page_dirty(page
);
720 page_cache_release(page
);
721 page_cache_release(page
);
723 BTRFS_I(inode
)->generation
= trans
->transid
;
725 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
726 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
728 filemap_write_and_wait(inode
->i_mapping
);
730 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
731 key
.offset
= block_group
->key
.objectid
;
734 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
737 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
738 EXTENT_DIRTY
| EXTENT_DELALLOC
|
739 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, GFP_NOFS
);
742 leaf
= path
->nodes
[0];
744 struct btrfs_key found_key
;
745 BUG_ON(!path
->slots
[0]);
747 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
748 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
749 found_key
.offset
!= block_group
->key
.objectid
) {
751 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
752 EXTENT_DIRTY
| EXTENT_DELALLOC
|
753 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
,
755 btrfs_release_path(root
, path
);
759 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
760 struct btrfs_free_space_header
);
761 btrfs_set_free_space_entries(leaf
, header
, entries
);
762 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
763 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
764 btrfs_mark_buffer_dirty(leaf
);
765 btrfs_release_path(root
, path
);
771 invalidate_inode_pages2_range(inode
->i_mapping
, 0, index
);
772 spin_lock(&block_group
->lock
);
773 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
774 spin_unlock(&block_group
->lock
);
775 BTRFS_I(inode
)->generation
= 0;
778 btrfs_update_inode(trans
, root
, inode
);
783 static inline unsigned long offset_to_bit(u64 bitmap_start
, u64 sectorsize
,
786 BUG_ON(offset
< bitmap_start
);
787 offset
-= bitmap_start
;
788 return (unsigned long)(div64_u64(offset
, sectorsize
));
791 static inline unsigned long bytes_to_bits(u64 bytes
, u64 sectorsize
)
793 return (unsigned long)(div64_u64(bytes
, sectorsize
));
796 static inline u64
offset_to_bitmap(struct btrfs_block_group_cache
*block_group
,
800 u64 bytes_per_bitmap
;
802 bytes_per_bitmap
= BITS_PER_BITMAP
* block_group
->sectorsize
;
803 bitmap_start
= offset
- block_group
->key
.objectid
;
804 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
805 bitmap_start
*= bytes_per_bitmap
;
806 bitmap_start
+= block_group
->key
.objectid
;
811 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
812 struct rb_node
*node
, int bitmap
)
814 struct rb_node
**p
= &root
->rb_node
;
815 struct rb_node
*parent
= NULL
;
816 struct btrfs_free_space
*info
;
820 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
822 if (offset
< info
->offset
) {
824 } else if (offset
> info
->offset
) {
828 * we could have a bitmap entry and an extent entry
829 * share the same offset. If this is the case, we want
830 * the extent entry to always be found first if we do a
831 * linear search through the tree, since we want to have
832 * the quickest allocation time, and allocating from an
833 * extent is faster than allocating from a bitmap. So
834 * if we're inserting a bitmap and we find an entry at
835 * this offset, we want to go right, or after this entry
836 * logically. If we are inserting an extent and we've
837 * found a bitmap, we want to go left, or before
841 WARN_ON(info
->bitmap
);
844 WARN_ON(!info
->bitmap
);
850 rb_link_node(node
, parent
, p
);
851 rb_insert_color(node
, root
);
857 * searches the tree for the given offset.
859 * fuzzy - If this is set, then we are trying to make an allocation, and we just
860 * want a section that has at least bytes size and comes at or after the given
863 static struct btrfs_free_space
*
864 tree_search_offset(struct btrfs_block_group_cache
*block_group
,
865 u64 offset
, int bitmap_only
, int fuzzy
)
867 struct rb_node
*n
= block_group
->free_space_offset
.rb_node
;
868 struct btrfs_free_space
*entry
, *prev
= NULL
;
870 /* find entry that is closest to the 'offset' */
877 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
880 if (offset
< entry
->offset
)
882 else if (offset
> entry
->offset
)
895 * bitmap entry and extent entry may share same offset,
896 * in that case, bitmap entry comes after extent entry.
901 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
902 if (entry
->offset
!= offset
)
905 WARN_ON(!entry
->bitmap
);
910 * if previous extent entry covers the offset,
911 * we should return it instead of the bitmap entry
913 n
= &entry
->offset_index
;
918 prev
= rb_entry(n
, struct btrfs_free_space
,
921 if (prev
->offset
+ prev
->bytes
> offset
)
933 /* find last entry before the 'offset' */
935 if (entry
->offset
> offset
) {
936 n
= rb_prev(&entry
->offset_index
);
938 entry
= rb_entry(n
, struct btrfs_free_space
,
940 BUG_ON(entry
->offset
> offset
);
950 n
= &entry
->offset_index
;
955 prev
= rb_entry(n
, struct btrfs_free_space
,
958 if (prev
->offset
+ prev
->bytes
> offset
)
963 if (entry
->offset
+ BITS_PER_BITMAP
*
964 block_group
->sectorsize
> offset
)
966 } else if (entry
->offset
+ entry
->bytes
> offset
)
974 if (entry
->offset
+ BITS_PER_BITMAP
*
975 block_group
->sectorsize
> offset
)
978 if (entry
->offset
+ entry
->bytes
> offset
)
982 n
= rb_next(&entry
->offset_index
);
985 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
990 static void unlink_free_space(struct btrfs_block_group_cache
*block_group
,
991 struct btrfs_free_space
*info
)
993 rb_erase(&info
->offset_index
, &block_group
->free_space_offset
);
994 block_group
->free_extents
--;
995 block_group
->free_space
-= info
->bytes
;
998 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
999 struct btrfs_free_space
*info
)
1003 BUG_ON(!info
->bitmap
&& !info
->bytes
);
1004 ret
= tree_insert_offset(&block_group
->free_space_offset
, info
->offset
,
1005 &info
->offset_index
, (info
->bitmap
!= NULL
));
1009 block_group
->free_space
+= info
->bytes
;
1010 block_group
->free_extents
++;
1014 static void recalculate_thresholds(struct btrfs_block_group_cache
*block_group
)
1021 * The goal is to keep the total amount of memory used per 1gb of space
1022 * at or below 32k, so we need to adjust how much memory we allow to be
1023 * used by extent based free space tracking
1025 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
1026 (div64_u64(block_group
->key
.offset
, 1024 * 1024 * 1024));
1029 * we want to account for 1 more bitmap than what we have so we can make
1030 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1031 * we add more bitmaps.
1033 bitmap_bytes
= (block_group
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
1035 if (bitmap_bytes
>= max_bytes
) {
1036 block_group
->extents_thresh
= 0;
1041 * we want the extent entry threshold to always be at most 1/2 the maxw
1042 * bytes we can have, or whatever is less than that.
1044 extent_bytes
= max_bytes
- bitmap_bytes
;
1045 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
1047 block_group
->extents_thresh
=
1048 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
1051 static void bitmap_clear_bits(struct btrfs_block_group_cache
*block_group
,
1052 struct btrfs_free_space
*info
, u64 offset
,
1055 unsigned long start
, end
;
1058 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1059 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1060 BUG_ON(end
> BITS_PER_BITMAP
);
1062 for (i
= start
; i
< end
; i
++)
1063 clear_bit(i
, info
->bitmap
);
1065 info
->bytes
-= bytes
;
1066 block_group
->free_space
-= bytes
;
1069 static void bitmap_set_bits(struct btrfs_block_group_cache
*block_group
,
1070 struct btrfs_free_space
*info
, u64 offset
,
1073 unsigned long start
, end
;
1076 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1077 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1078 BUG_ON(end
> BITS_PER_BITMAP
);
1080 for (i
= start
; i
< end
; i
++)
1081 set_bit(i
, info
->bitmap
);
1083 info
->bytes
+= bytes
;
1084 block_group
->free_space
+= bytes
;
1087 static int search_bitmap(struct btrfs_block_group_cache
*block_group
,
1088 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1091 unsigned long found_bits
= 0;
1092 unsigned long bits
, i
;
1093 unsigned long next_zero
;
1095 i
= offset_to_bit(bitmap_info
->offset
, block_group
->sectorsize
,
1096 max_t(u64
, *offset
, bitmap_info
->offset
));
1097 bits
= bytes_to_bits(*bytes
, block_group
->sectorsize
);
1099 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
1100 i
< BITS_PER_BITMAP
;
1101 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1102 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1103 BITS_PER_BITMAP
, i
);
1104 if ((next_zero
- i
) >= bits
) {
1105 found_bits
= next_zero
- i
;
1112 *offset
= (u64
)(i
* block_group
->sectorsize
) +
1113 bitmap_info
->offset
;
1114 *bytes
= (u64
)(found_bits
) * block_group
->sectorsize
;
1121 static struct btrfs_free_space
*find_free_space(struct btrfs_block_group_cache
1122 *block_group
, u64
*offset
,
1123 u64
*bytes
, int debug
)
1125 struct btrfs_free_space
*entry
;
1126 struct rb_node
*node
;
1129 if (!block_group
->free_space_offset
.rb_node
)
1132 entry
= tree_search_offset(block_group
,
1133 offset_to_bitmap(block_group
, *offset
),
1138 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1139 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1140 if (entry
->bytes
< *bytes
)
1143 if (entry
->bitmap
) {
1144 ret
= search_bitmap(block_group
, entry
, offset
, bytes
);
1150 *offset
= entry
->offset
;
1151 *bytes
= entry
->bytes
;
1158 static void add_new_bitmap(struct btrfs_block_group_cache
*block_group
,
1159 struct btrfs_free_space
*info
, u64 offset
)
1161 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
1162 int max_bitmaps
= (int)div64_u64(block_group
->key
.offset
+
1163 bytes_per_bg
- 1, bytes_per_bg
);
1164 BUG_ON(block_group
->total_bitmaps
>= max_bitmaps
);
1166 info
->offset
= offset_to_bitmap(block_group
, offset
);
1168 link_free_space(block_group
, info
);
1169 block_group
->total_bitmaps
++;
1171 recalculate_thresholds(block_group
);
1174 static noinline
int remove_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1175 struct btrfs_free_space
*bitmap_info
,
1176 u64
*offset
, u64
*bytes
)
1179 u64 search_start
, search_bytes
;
1183 end
= bitmap_info
->offset
+
1184 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
) - 1;
1187 * XXX - this can go away after a few releases.
1189 * since the only user of btrfs_remove_free_space is the tree logging
1190 * stuff, and the only way to test that is under crash conditions, we
1191 * want to have this debug stuff here just in case somethings not
1192 * working. Search the bitmap for the space we are trying to use to
1193 * make sure its actually there. If its not there then we need to stop
1194 * because something has gone wrong.
1196 search_start
= *offset
;
1197 search_bytes
= *bytes
;
1198 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1200 BUG_ON(ret
< 0 || search_start
!= *offset
);
1202 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
1203 bitmap_clear_bits(block_group
, bitmap_info
, *offset
,
1205 *bytes
-= end
- *offset
+ 1;
1207 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
1208 bitmap_clear_bits(block_group
, bitmap_info
, *offset
, *bytes
);
1213 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1214 if (!bitmap_info
->bytes
) {
1215 unlink_free_space(block_group
, bitmap_info
);
1216 kfree(bitmap_info
->bitmap
);
1218 block_group
->total_bitmaps
--;
1219 recalculate_thresholds(block_group
);
1223 * no entry after this bitmap, but we still have bytes to
1224 * remove, so something has gone wrong.
1229 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1233 * if the next entry isn't a bitmap we need to return to let the
1234 * extent stuff do its work.
1236 if (!bitmap_info
->bitmap
)
1240 * Ok the next item is a bitmap, but it may not actually hold
1241 * the information for the rest of this free space stuff, so
1242 * look for it, and if we don't find it return so we can try
1243 * everything over again.
1245 search_start
= *offset
;
1246 search_bytes
= *bytes
;
1247 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1249 if (ret
< 0 || search_start
!= *offset
)
1253 } else if (!bitmap_info
->bytes
) {
1254 unlink_free_space(block_group
, bitmap_info
);
1255 kfree(bitmap_info
->bitmap
);
1257 block_group
->total_bitmaps
--;
1258 recalculate_thresholds(block_group
);
1264 static int insert_into_bitmap(struct btrfs_block_group_cache
*block_group
,
1265 struct btrfs_free_space
*info
)
1267 struct btrfs_free_space
*bitmap_info
;
1269 u64 bytes
, offset
, end
;
1273 * If we are below the extents threshold then we can add this as an
1274 * extent, and don't have to deal with the bitmap
1276 if (block_group
->free_extents
< block_group
->extents_thresh
&&
1277 info
->bytes
> block_group
->sectorsize
* 4)
1281 * some block groups are so tiny they can't be enveloped by a bitmap, so
1282 * don't even bother to create a bitmap for this
1284 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
1285 block_group
->key
.offset
)
1288 bytes
= info
->bytes
;
1289 offset
= info
->offset
;
1292 bitmap_info
= tree_search_offset(block_group
,
1293 offset_to_bitmap(block_group
, offset
),
1300 end
= bitmap_info
->offset
+
1301 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
);
1303 if (offset
>= bitmap_info
->offset
&& offset
+ bytes
> end
) {
1304 bitmap_set_bits(block_group
, bitmap_info
, offset
,
1306 bytes
-= end
- offset
;
1309 } else if (offset
>= bitmap_info
->offset
&& offset
+ bytes
<= end
) {
1310 bitmap_set_bits(block_group
, bitmap_info
, offset
, bytes
);
1323 if (info
&& info
->bitmap
) {
1324 add_new_bitmap(block_group
, info
, offset
);
1329 spin_unlock(&block_group
->tree_lock
);
1331 /* no pre-allocated info, allocate a new one */
1333 info
= kzalloc(sizeof(struct btrfs_free_space
),
1336 spin_lock(&block_group
->tree_lock
);
1342 /* allocate the bitmap */
1343 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1344 spin_lock(&block_group
->tree_lock
);
1345 if (!info
->bitmap
) {
1355 kfree(info
->bitmap
);
1362 int btrfs_add_free_space(struct btrfs_block_group_cache
*block_group
,
1363 u64 offset
, u64 bytes
)
1365 struct btrfs_free_space
*right_info
= NULL
;
1366 struct btrfs_free_space
*left_info
= NULL
;
1367 struct btrfs_free_space
*info
= NULL
;
1370 info
= kzalloc(sizeof(struct btrfs_free_space
), GFP_NOFS
);
1374 info
->offset
= offset
;
1375 info
->bytes
= bytes
;
1377 spin_lock(&block_group
->tree_lock
);
1380 * first we want to see if there is free space adjacent to the range we
1381 * are adding, if there is remove that struct and add a new one to
1382 * cover the entire range
1384 right_info
= tree_search_offset(block_group
, offset
+ bytes
, 0, 0);
1385 if (right_info
&& rb_prev(&right_info
->offset_index
))
1386 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1387 struct btrfs_free_space
, offset_index
);
1389 left_info
= tree_search_offset(block_group
, offset
- 1, 0, 0);
1392 * If there was no extent directly to the left or right of this new
1393 * extent then we know we're going to have to allocate a new extent, so
1394 * before we do that see if we need to drop this into a bitmap
1396 if ((!left_info
|| left_info
->bitmap
) &&
1397 (!right_info
|| right_info
->bitmap
)) {
1398 ret
= insert_into_bitmap(block_group
, info
);
1408 if (right_info
&& !right_info
->bitmap
) {
1409 unlink_free_space(block_group
, right_info
);
1410 info
->bytes
+= right_info
->bytes
;
1414 if (left_info
&& !left_info
->bitmap
&&
1415 left_info
->offset
+ left_info
->bytes
== offset
) {
1416 unlink_free_space(block_group
, left_info
);
1417 info
->offset
= left_info
->offset
;
1418 info
->bytes
+= left_info
->bytes
;
1422 ret
= link_free_space(block_group
, info
);
1426 spin_unlock(&block_group
->tree_lock
);
1429 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1430 BUG_ON(ret
== -EEXIST
);
1436 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1437 u64 offset
, u64 bytes
)
1439 struct btrfs_free_space
*info
;
1440 struct btrfs_free_space
*next_info
= NULL
;
1443 spin_lock(&block_group
->tree_lock
);
1446 info
= tree_search_offset(block_group
, offset
, 0, 0);
1449 * oops didn't find an extent that matched the space we wanted
1450 * to remove, look for a bitmap instead
1452 info
= tree_search_offset(block_group
,
1453 offset_to_bitmap(block_group
, offset
),
1461 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1463 next_info
= rb_entry(rb_next(&info
->offset_index
),
1464 struct btrfs_free_space
,
1467 if (next_info
->bitmap
)
1468 end
= next_info
->offset
+ BITS_PER_BITMAP
*
1469 block_group
->sectorsize
- 1;
1471 end
= next_info
->offset
+ next_info
->bytes
;
1473 if (next_info
->bytes
< bytes
||
1474 next_info
->offset
> offset
|| offset
> end
) {
1475 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1476 " trying to use %llu\n",
1477 (unsigned long long)info
->offset
,
1478 (unsigned long long)info
->bytes
,
1479 (unsigned long long)bytes
);
1488 if (info
->bytes
== bytes
) {
1489 unlink_free_space(block_group
, info
);
1491 kfree(info
->bitmap
);
1492 block_group
->total_bitmaps
--;
1498 if (!info
->bitmap
&& info
->offset
== offset
) {
1499 unlink_free_space(block_group
, info
);
1500 info
->offset
+= bytes
;
1501 info
->bytes
-= bytes
;
1502 link_free_space(block_group
, info
);
1506 if (!info
->bitmap
&& info
->offset
<= offset
&&
1507 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1508 u64 old_start
= info
->offset
;
1510 * we're freeing space in the middle of the info,
1511 * this can happen during tree log replay
1513 * first unlink the old info and then
1514 * insert it again after the hole we're creating
1516 unlink_free_space(block_group
, info
);
1517 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1518 u64 old_end
= info
->offset
+ info
->bytes
;
1520 info
->offset
= offset
+ bytes
;
1521 info
->bytes
= old_end
- info
->offset
;
1522 ret
= link_free_space(block_group
, info
);
1527 /* the hole we're creating ends at the end
1528 * of the info struct, just free the info
1532 spin_unlock(&block_group
->tree_lock
);
1534 /* step two, insert a new info struct to cover
1535 * anything before the hole
1537 ret
= btrfs_add_free_space(block_group
, old_start
,
1538 offset
- old_start
);
1543 ret
= remove_from_bitmap(block_group
, info
, &offset
, &bytes
);
1548 spin_unlock(&block_group
->tree_lock
);
1553 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1556 struct btrfs_free_space
*info
;
1560 for (n
= rb_first(&block_group
->free_space_offset
); n
; n
= rb_next(n
)) {
1561 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1562 if (info
->bytes
>= bytes
)
1564 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1565 (unsigned long long)info
->offset
,
1566 (unsigned long long)info
->bytes
,
1567 (info
->bitmap
) ? "yes" : "no");
1569 printk(KERN_INFO
"block group has cluster?: %s\n",
1570 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1571 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1575 u64
btrfs_block_group_free_space(struct btrfs_block_group_cache
*block_group
)
1577 struct btrfs_free_space
*info
;
1581 for (n
= rb_first(&block_group
->free_space_offset
); n
;
1583 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1591 * for a given cluster, put all of its extents back into the free
1592 * space cache. If the block group passed doesn't match the block group
1593 * pointed to by the cluster, someone else raced in and freed the
1594 * cluster already. In that case, we just return without changing anything
1597 __btrfs_return_cluster_to_free_space(
1598 struct btrfs_block_group_cache
*block_group
,
1599 struct btrfs_free_cluster
*cluster
)
1601 struct btrfs_free_space
*entry
;
1602 struct rb_node
*node
;
1605 spin_lock(&cluster
->lock
);
1606 if (cluster
->block_group
!= block_group
)
1609 bitmap
= cluster
->points_to_bitmap
;
1610 cluster
->block_group
= NULL
;
1611 cluster
->window_start
= 0;
1612 list_del_init(&cluster
->block_group_list
);
1613 cluster
->points_to_bitmap
= false;
1618 node
= rb_first(&cluster
->root
);
1620 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1621 node
= rb_next(&entry
->offset_index
);
1622 rb_erase(&entry
->offset_index
, &cluster
->root
);
1623 BUG_ON(entry
->bitmap
);
1624 tree_insert_offset(&block_group
->free_space_offset
,
1625 entry
->offset
, &entry
->offset_index
, 0);
1627 cluster
->root
= RB_ROOT
;
1630 spin_unlock(&cluster
->lock
);
1631 btrfs_put_block_group(block_group
);
1635 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
1637 struct btrfs_free_space
*info
;
1638 struct rb_node
*node
;
1639 struct btrfs_free_cluster
*cluster
;
1640 struct list_head
*head
;
1642 spin_lock(&block_group
->tree_lock
);
1643 while ((head
= block_group
->cluster_list
.next
) !=
1644 &block_group
->cluster_list
) {
1645 cluster
= list_entry(head
, struct btrfs_free_cluster
,
1648 WARN_ON(cluster
->block_group
!= block_group
);
1649 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1650 if (need_resched()) {
1651 spin_unlock(&block_group
->tree_lock
);
1653 spin_lock(&block_group
->tree_lock
);
1657 while ((node
= rb_last(&block_group
->free_space_offset
)) != NULL
) {
1658 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1659 unlink_free_space(block_group
, info
);
1661 kfree(info
->bitmap
);
1663 if (need_resched()) {
1664 spin_unlock(&block_group
->tree_lock
);
1666 spin_lock(&block_group
->tree_lock
);
1670 spin_unlock(&block_group
->tree_lock
);
1673 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
1674 u64 offset
, u64 bytes
, u64 empty_size
)
1676 struct btrfs_free_space
*entry
= NULL
;
1677 u64 bytes_search
= bytes
+ empty_size
;
1680 spin_lock(&block_group
->tree_lock
);
1681 entry
= find_free_space(block_group
, &offset
, &bytes_search
, 0);
1686 if (entry
->bitmap
) {
1687 bitmap_clear_bits(block_group
, entry
, offset
, bytes
);
1688 if (!entry
->bytes
) {
1689 unlink_free_space(block_group
, entry
);
1690 kfree(entry
->bitmap
);
1692 block_group
->total_bitmaps
--;
1693 recalculate_thresholds(block_group
);
1696 unlink_free_space(block_group
, entry
);
1697 entry
->offset
+= bytes
;
1698 entry
->bytes
-= bytes
;
1702 link_free_space(block_group
, entry
);
1706 spin_unlock(&block_group
->tree_lock
);
1712 * given a cluster, put all of its extents back into the free space
1713 * cache. If a block group is passed, this function will only free
1714 * a cluster that belongs to the passed block group.
1716 * Otherwise, it'll get a reference on the block group pointed to by the
1717 * cluster and remove the cluster from it.
1719 int btrfs_return_cluster_to_free_space(
1720 struct btrfs_block_group_cache
*block_group
,
1721 struct btrfs_free_cluster
*cluster
)
1725 /* first, get a safe pointer to the block group */
1726 spin_lock(&cluster
->lock
);
1728 block_group
= cluster
->block_group
;
1730 spin_unlock(&cluster
->lock
);
1733 } else if (cluster
->block_group
!= block_group
) {
1734 /* someone else has already freed it don't redo their work */
1735 spin_unlock(&cluster
->lock
);
1738 atomic_inc(&block_group
->count
);
1739 spin_unlock(&cluster
->lock
);
1741 /* now return any extents the cluster had on it */
1742 spin_lock(&block_group
->tree_lock
);
1743 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1744 spin_unlock(&block_group
->tree_lock
);
1746 /* finally drop our ref */
1747 btrfs_put_block_group(block_group
);
1751 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1752 struct btrfs_free_cluster
*cluster
,
1753 u64 bytes
, u64 min_start
)
1755 struct btrfs_free_space
*entry
;
1757 u64 search_start
= cluster
->window_start
;
1758 u64 search_bytes
= bytes
;
1761 spin_lock(&block_group
->tree_lock
);
1762 spin_lock(&cluster
->lock
);
1764 if (!cluster
->points_to_bitmap
)
1767 if (cluster
->block_group
!= block_group
)
1771 * search_start is the beginning of the bitmap, but at some point it may
1772 * be a good idea to point to the actual start of the free area in the
1773 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1774 * to 1 to make sure we get the bitmap entry
1776 entry
= tree_search_offset(block_group
,
1777 offset_to_bitmap(block_group
, search_start
),
1779 if (!entry
|| !entry
->bitmap
)
1782 search_start
= min_start
;
1783 search_bytes
= bytes
;
1785 err
= search_bitmap(block_group
, entry
, &search_start
,
1791 bitmap_clear_bits(block_group
, entry
, ret
, bytes
);
1793 spin_unlock(&cluster
->lock
);
1794 spin_unlock(&block_group
->tree_lock
);
1800 * given a cluster, try to allocate 'bytes' from it, returns 0
1801 * if it couldn't find anything suitably large, or a logical disk offset
1802 * if things worked out
1804 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
1805 struct btrfs_free_cluster
*cluster
, u64 bytes
,
1808 struct btrfs_free_space
*entry
= NULL
;
1809 struct rb_node
*node
;
1812 if (cluster
->points_to_bitmap
)
1813 return btrfs_alloc_from_bitmap(block_group
, cluster
, bytes
,
1816 spin_lock(&cluster
->lock
);
1817 if (bytes
> cluster
->max_size
)
1820 if (cluster
->block_group
!= block_group
)
1823 node
= rb_first(&cluster
->root
);
1827 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1830 if (entry
->bytes
< bytes
|| entry
->offset
< min_start
) {
1831 struct rb_node
*node
;
1833 node
= rb_next(&entry
->offset_index
);
1836 entry
= rb_entry(node
, struct btrfs_free_space
,
1840 ret
= entry
->offset
;
1842 entry
->offset
+= bytes
;
1843 entry
->bytes
-= bytes
;
1845 if (entry
->bytes
== 0) {
1846 rb_erase(&entry
->offset_index
, &cluster
->root
);
1852 spin_unlock(&cluster
->lock
);
1857 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
1858 struct btrfs_free_space
*entry
,
1859 struct btrfs_free_cluster
*cluster
,
1860 u64 offset
, u64 bytes
, u64 min_bytes
)
1862 unsigned long next_zero
;
1864 unsigned long search_bits
;
1865 unsigned long total_bits
;
1866 unsigned long found_bits
;
1867 unsigned long start
= 0;
1868 unsigned long total_found
= 0;
1871 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
1872 max_t(u64
, offset
, entry
->offset
));
1873 search_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
1874 total_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
1878 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
1879 i
< BITS_PER_BITMAP
;
1880 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1881 next_zero
= find_next_zero_bit(entry
->bitmap
,
1882 BITS_PER_BITMAP
, i
);
1883 if (next_zero
- i
>= search_bits
) {
1884 found_bits
= next_zero
- i
;
1898 total_found
+= found_bits
;
1900 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
1901 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
1903 if (total_found
< total_bits
) {
1904 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, next_zero
);
1905 if (i
- start
> total_bits
* 2) {
1907 cluster
->max_size
= 0;
1913 cluster
->window_start
= start
* block_group
->sectorsize
+
1915 cluster
->points_to_bitmap
= true;
1921 * here we try to find a cluster of blocks in a block group. The goal
1922 * is to find at least bytes free and up to empty_size + bytes free.
1923 * We might not find them all in one contiguous area.
1925 * returns zero and sets up cluster if things worked out, otherwise
1926 * it returns -enospc
1928 int btrfs_find_space_cluster(struct btrfs_trans_handle
*trans
,
1929 struct btrfs_root
*root
,
1930 struct btrfs_block_group_cache
*block_group
,
1931 struct btrfs_free_cluster
*cluster
,
1932 u64 offset
, u64 bytes
, u64 empty_size
)
1934 struct btrfs_free_space
*entry
= NULL
;
1935 struct rb_node
*node
;
1936 struct btrfs_free_space
*next
;
1937 struct btrfs_free_space
*last
= NULL
;
1942 bool found_bitmap
= false;
1945 /* for metadata, allow allocates with more holes */
1946 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
1947 min_bytes
= bytes
+ empty_size
;
1948 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
1950 * we want to do larger allocations when we are
1951 * flushing out the delayed refs, it helps prevent
1952 * making more work as we go along.
1954 if (trans
->transaction
->delayed_refs
.flushing
)
1955 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 1);
1957 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 4);
1959 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
1961 spin_lock(&block_group
->tree_lock
);
1962 spin_lock(&cluster
->lock
);
1964 /* someone already found a cluster, hooray */
1965 if (cluster
->block_group
) {
1970 entry
= tree_search_offset(block_group
, offset
, found_bitmap
, 1);
1977 * If found_bitmap is true, we exhausted our search for extent entries,
1978 * and we just want to search all of the bitmaps that we can find, and
1979 * ignore any extent entries we find.
1981 while (entry
->bitmap
|| found_bitmap
||
1982 (!entry
->bitmap
&& entry
->bytes
< min_bytes
)) {
1983 struct rb_node
*node
= rb_next(&entry
->offset_index
);
1985 if (entry
->bitmap
&& entry
->bytes
> bytes
+ empty_size
) {
1986 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
,
1987 offset
, bytes
+ empty_size
,
1997 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2001 * We already searched all the extent entries from the passed in offset
2002 * to the end and didn't find enough space for the cluster, and we also
2003 * didn't find any bitmaps that met our criteria, just go ahead and exit
2010 cluster
->points_to_bitmap
= false;
2011 window_start
= entry
->offset
;
2012 window_free
= entry
->bytes
;
2014 max_extent
= entry
->bytes
;
2017 /* out window is just right, lets fill it */
2018 if (window_free
>= bytes
+ empty_size
)
2021 node
= rb_next(&last
->offset_index
);
2028 next
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2031 * we found a bitmap, so if this search doesn't result in a
2032 * cluster, we know to go and search again for the bitmaps and
2033 * start looking for space there
2037 offset
= next
->offset
;
2038 found_bitmap
= true;
2044 * we haven't filled the empty size and the window is
2045 * very large. reset and try again
2047 if (next
->offset
- (last
->offset
+ last
->bytes
) > 128 * 1024 ||
2048 next
->offset
- window_start
> (bytes
+ empty_size
) * 2) {
2050 window_start
= entry
->offset
;
2051 window_free
= entry
->bytes
;
2053 max_extent
= entry
->bytes
;
2056 window_free
+= next
->bytes
;
2057 if (entry
->bytes
> max_extent
)
2058 max_extent
= entry
->bytes
;
2062 cluster
->window_start
= entry
->offset
;
2065 * now we've found our entries, pull them out of the free space
2066 * cache and put them into the cluster rbtree
2068 * The cluster includes an rbtree, but only uses the offset index
2069 * of each free space cache entry.
2072 node
= rb_next(&entry
->offset_index
);
2073 if (entry
->bitmap
&& node
) {
2074 entry
= rb_entry(node
, struct btrfs_free_space
,
2077 } else if (entry
->bitmap
&& !node
) {
2081 rb_erase(&entry
->offset_index
, &block_group
->free_space_offset
);
2082 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2083 &entry
->offset_index
, 0);
2086 if (!node
|| entry
== last
)
2089 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2092 cluster
->max_size
= max_extent
;
2095 atomic_inc(&block_group
->count
);
2096 list_add_tail(&cluster
->block_group_list
, &block_group
->cluster_list
);
2097 cluster
->block_group
= block_group
;
2099 spin_unlock(&cluster
->lock
);
2100 spin_unlock(&block_group
->tree_lock
);
2106 * simple code to zero out a cluster
2108 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
2110 spin_lock_init(&cluster
->lock
);
2111 spin_lock_init(&cluster
->refill_lock
);
2112 cluster
->root
= RB_ROOT
;
2113 cluster
->max_size
= 0;
2114 cluster
->points_to_bitmap
= false;
2115 INIT_LIST_HEAD(&cluster
->block_group_list
);
2116 cluster
->block_group
= NULL
;