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
= kmem_cache_zalloc(btrfs_free_space_cachep
,
401 page_cache_release(page
);
405 e
->offset
= le64_to_cpu(entry
->offset
);
406 e
->bytes
= le64_to_cpu(entry
->bytes
);
409 kmem_cache_free(btrfs_free_space_cachep
, e
);
411 page_cache_release(page
);
415 if (entry
->type
== BTRFS_FREE_SPACE_EXTENT
) {
416 spin_lock(&block_group
->tree_lock
);
417 ret
= link_free_space(block_group
, e
);
418 spin_unlock(&block_group
->tree_lock
);
421 e
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
425 btrfs_free_space_cachep
, e
);
427 page_cache_release(page
);
430 spin_lock(&block_group
->tree_lock
);
431 ret
= link_free_space(block_group
, e
);
432 block_group
->total_bitmaps
++;
433 recalculate_thresholds(block_group
);
434 spin_unlock(&block_group
->tree_lock
);
435 list_add_tail(&e
->list
, &bitmaps
);
439 offset
+= sizeof(struct btrfs_free_space_entry
);
440 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
447 * We read an entry out of this page, we need to move on to the
456 * We add the bitmaps at the end of the entries in order that
457 * the bitmap entries are added to the cache.
459 e
= list_entry(bitmaps
.next
, struct btrfs_free_space
, list
);
460 list_del_init(&e
->list
);
461 memcpy(e
->bitmap
, addr
, PAGE_CACHE_SIZE
);
466 page_cache_release(page
);
478 /* This cache is bogus, make sure it gets cleared */
479 spin_lock(&block_group
->lock
);
480 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
481 spin_unlock(&block_group
->lock
);
482 btrfs_remove_free_space_cache(block_group
);
486 int btrfs_write_out_cache(struct btrfs_root
*root
,
487 struct btrfs_trans_handle
*trans
,
488 struct btrfs_block_group_cache
*block_group
,
489 struct btrfs_path
*path
)
491 struct btrfs_free_space_header
*header
;
492 struct extent_buffer
*leaf
;
494 struct rb_node
*node
;
495 struct list_head
*pos
, *n
;
497 struct extent_state
*cached_state
= NULL
;
498 struct list_head bitmap_list
;
499 struct btrfs_key key
;
501 u32
*crc
, *checksums
;
502 pgoff_t index
= 0, last_index
= 0;
503 unsigned long first_page_offset
;
509 root
= root
->fs_info
->tree_root
;
511 INIT_LIST_HEAD(&bitmap_list
);
513 spin_lock(&block_group
->lock
);
514 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
515 spin_unlock(&block_group
->lock
);
518 spin_unlock(&block_group
->lock
);
520 inode
= lookup_free_space_inode(root
, block_group
, path
);
524 if (!i_size_read(inode
)) {
529 node
= rb_first(&block_group
->free_space_offset
);
535 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
536 filemap_write_and_wait(inode
->i_mapping
);
537 btrfs_wait_ordered_range(inode
, inode
->i_size
&
538 ~(root
->sectorsize
- 1), (u64
)-1);
540 /* We need a checksum per page. */
541 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
542 crc
= checksums
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
548 /* Since the first page has all of our checksums and our generation we
549 * need to calculate the offset into the page that we can start writing
552 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
555 * Lock all pages first so we can lock the extent safely.
557 * NOTE: Because we hold the ref the entire time we're going to write to
558 * the page find_get_page should never fail, so we don't do a check
559 * after find_get_page at this point. Just putting this here so people
560 * know and don't freak out.
562 while (index
<= last_index
) {
563 page
= grab_cache_page(inode
->i_mapping
, index
);
568 page
= find_get_page(inode
->i_mapping
, i
);
570 page_cache_release(page
);
571 page_cache_release(page
);
580 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
581 0, &cached_state
, GFP_NOFS
);
583 /* Write out the extent entries */
585 struct btrfs_free_space_entry
*entry
;
587 unsigned long offset
= 0;
588 unsigned long start_offset
= 0;
591 start_offset
= first_page_offset
;
592 offset
= start_offset
;
595 page
= find_get_page(inode
->i_mapping
, index
);
598 entry
= addr
+ start_offset
;
600 memset(addr
, 0, PAGE_CACHE_SIZE
);
602 struct btrfs_free_space
*e
;
604 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
607 entry
->offset
= cpu_to_le64(e
->offset
);
608 entry
->bytes
= cpu_to_le64(e
->bytes
);
610 entry
->type
= BTRFS_FREE_SPACE_BITMAP
;
611 list_add_tail(&e
->list
, &bitmap_list
);
614 entry
->type
= BTRFS_FREE_SPACE_EXTENT
;
616 node
= rb_next(node
);
619 offset
+= sizeof(struct btrfs_free_space_entry
);
620 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
626 *crc
= btrfs_csum_data(root
, addr
+ start_offset
, *crc
,
627 PAGE_CACHE_SIZE
- start_offset
);
630 btrfs_csum_final(*crc
, (char *)crc
);
633 bytes
+= PAGE_CACHE_SIZE
;
635 ClearPageChecked(page
);
636 set_page_extent_mapped(page
);
637 SetPageUptodate(page
);
638 set_page_dirty(page
);
641 * We need to release our reference we got for grab_cache_page,
642 * except for the first page which will hold our checksums, we
647 page_cache_release(page
);
650 page_cache_release(page
);
655 /* Write out the bitmaps */
656 list_for_each_safe(pos
, n
, &bitmap_list
) {
658 struct btrfs_free_space
*entry
=
659 list_entry(pos
, struct btrfs_free_space
, list
);
661 page
= find_get_page(inode
->i_mapping
, index
);
664 memcpy(addr
, entry
->bitmap
, PAGE_CACHE_SIZE
);
666 *crc
= btrfs_csum_data(root
, addr
, *crc
, PAGE_CACHE_SIZE
);
668 btrfs_csum_final(*crc
, (char *)crc
);
670 bytes
+= PAGE_CACHE_SIZE
;
672 ClearPageChecked(page
);
673 set_page_extent_mapped(page
);
674 SetPageUptodate(page
);
675 set_page_dirty(page
);
677 page_cache_release(page
);
678 page_cache_release(page
);
679 list_del_init(&entry
->list
);
683 /* Zero out the rest of the pages just to make sure */
684 while (index
<= last_index
) {
687 page
= find_get_page(inode
->i_mapping
, index
);
690 memset(addr
, 0, PAGE_CACHE_SIZE
);
692 ClearPageChecked(page
);
693 set_page_extent_mapped(page
);
694 SetPageUptodate(page
);
695 set_page_dirty(page
);
697 page_cache_release(page
);
698 page_cache_release(page
);
699 bytes
+= PAGE_CACHE_SIZE
;
703 btrfs_set_extent_delalloc(inode
, 0, bytes
- 1, &cached_state
);
705 /* Write the checksums and trans id to the first page */
710 page
= find_get_page(inode
->i_mapping
, 0);
713 memcpy(addr
, checksums
, sizeof(u32
) * num_checksums
);
714 gen
= addr
+ (sizeof(u32
) * num_checksums
);
715 *gen
= trans
->transid
;
717 ClearPageChecked(page
);
718 set_page_extent_mapped(page
);
719 SetPageUptodate(page
);
720 set_page_dirty(page
);
722 page_cache_release(page
);
723 page_cache_release(page
);
725 BTRFS_I(inode
)->generation
= trans
->transid
;
727 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
728 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
730 filemap_write_and_wait(inode
->i_mapping
);
732 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
733 key
.offset
= block_group
->key
.objectid
;
736 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
739 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
740 EXTENT_DIRTY
| EXTENT_DELALLOC
|
741 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, GFP_NOFS
);
744 leaf
= path
->nodes
[0];
746 struct btrfs_key found_key
;
747 BUG_ON(!path
->slots
[0]);
749 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
750 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
751 found_key
.offset
!= block_group
->key
.objectid
) {
753 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
754 EXTENT_DIRTY
| EXTENT_DELALLOC
|
755 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
,
757 btrfs_release_path(root
, path
);
761 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
762 struct btrfs_free_space_header
);
763 btrfs_set_free_space_entries(leaf
, header
, entries
);
764 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
765 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
766 btrfs_mark_buffer_dirty(leaf
);
767 btrfs_release_path(root
, path
);
773 invalidate_inode_pages2_range(inode
->i_mapping
, 0, index
);
774 spin_lock(&block_group
->lock
);
775 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
776 spin_unlock(&block_group
->lock
);
777 BTRFS_I(inode
)->generation
= 0;
780 btrfs_update_inode(trans
, root
, inode
);
785 static inline unsigned long offset_to_bit(u64 bitmap_start
, u64 sectorsize
,
788 BUG_ON(offset
< bitmap_start
);
789 offset
-= bitmap_start
;
790 return (unsigned long)(div64_u64(offset
, sectorsize
));
793 static inline unsigned long bytes_to_bits(u64 bytes
, u64 sectorsize
)
795 return (unsigned long)(div64_u64(bytes
, sectorsize
));
798 static inline u64
offset_to_bitmap(struct btrfs_block_group_cache
*block_group
,
802 u64 bytes_per_bitmap
;
804 bytes_per_bitmap
= BITS_PER_BITMAP
* block_group
->sectorsize
;
805 bitmap_start
= offset
- block_group
->key
.objectid
;
806 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
807 bitmap_start
*= bytes_per_bitmap
;
808 bitmap_start
+= block_group
->key
.objectid
;
813 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
814 struct rb_node
*node
, int bitmap
)
816 struct rb_node
**p
= &root
->rb_node
;
817 struct rb_node
*parent
= NULL
;
818 struct btrfs_free_space
*info
;
822 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
824 if (offset
< info
->offset
) {
826 } else if (offset
> info
->offset
) {
830 * we could have a bitmap entry and an extent entry
831 * share the same offset. If this is the case, we want
832 * the extent entry to always be found first if we do a
833 * linear search through the tree, since we want to have
834 * the quickest allocation time, and allocating from an
835 * extent is faster than allocating from a bitmap. So
836 * if we're inserting a bitmap and we find an entry at
837 * this offset, we want to go right, or after this entry
838 * logically. If we are inserting an extent and we've
839 * found a bitmap, we want to go left, or before
843 WARN_ON(info
->bitmap
);
846 WARN_ON(!info
->bitmap
);
852 rb_link_node(node
, parent
, p
);
853 rb_insert_color(node
, root
);
859 * searches the tree for the given offset.
861 * fuzzy - If this is set, then we are trying to make an allocation, and we just
862 * want a section that has at least bytes size and comes at or after the given
865 static struct btrfs_free_space
*
866 tree_search_offset(struct btrfs_block_group_cache
*block_group
,
867 u64 offset
, int bitmap_only
, int fuzzy
)
869 struct rb_node
*n
= block_group
->free_space_offset
.rb_node
;
870 struct btrfs_free_space
*entry
, *prev
= NULL
;
872 /* find entry that is closest to the 'offset' */
879 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
882 if (offset
< entry
->offset
)
884 else if (offset
> entry
->offset
)
897 * bitmap entry and extent entry may share same offset,
898 * in that case, bitmap entry comes after extent entry.
903 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
904 if (entry
->offset
!= offset
)
907 WARN_ON(!entry
->bitmap
);
912 * if previous extent entry covers the offset,
913 * we should return it instead of the bitmap entry
915 n
= &entry
->offset_index
;
920 prev
= rb_entry(n
, struct btrfs_free_space
,
923 if (prev
->offset
+ prev
->bytes
> offset
)
935 /* find last entry before the 'offset' */
937 if (entry
->offset
> offset
) {
938 n
= rb_prev(&entry
->offset_index
);
940 entry
= rb_entry(n
, struct btrfs_free_space
,
942 BUG_ON(entry
->offset
> offset
);
952 n
= &entry
->offset_index
;
957 prev
= rb_entry(n
, struct btrfs_free_space
,
960 if (prev
->offset
+ prev
->bytes
> offset
)
965 if (entry
->offset
+ BITS_PER_BITMAP
*
966 block_group
->sectorsize
> offset
)
968 } else if (entry
->offset
+ entry
->bytes
> offset
)
976 if (entry
->offset
+ BITS_PER_BITMAP
*
977 block_group
->sectorsize
> offset
)
980 if (entry
->offset
+ entry
->bytes
> offset
)
984 n
= rb_next(&entry
->offset_index
);
987 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
993 __unlink_free_space(struct btrfs_block_group_cache
*block_group
,
994 struct btrfs_free_space
*info
)
996 rb_erase(&info
->offset_index
, &block_group
->free_space_offset
);
997 block_group
->free_extents
--;
1000 static void unlink_free_space(struct btrfs_block_group_cache
*block_group
,
1001 struct btrfs_free_space
*info
)
1003 __unlink_free_space(block_group
, info
);
1004 block_group
->free_space
-= info
->bytes
;
1007 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
1008 struct btrfs_free_space
*info
)
1012 BUG_ON(!info
->bitmap
&& !info
->bytes
);
1013 ret
= tree_insert_offset(&block_group
->free_space_offset
, info
->offset
,
1014 &info
->offset_index
, (info
->bitmap
!= NULL
));
1018 block_group
->free_space
+= info
->bytes
;
1019 block_group
->free_extents
++;
1023 static void recalculate_thresholds(struct btrfs_block_group_cache
*block_group
)
1028 u64 size
= block_group
->key
.offset
;
1031 * The goal is to keep the total amount of memory used per 1gb of space
1032 * at or below 32k, so we need to adjust how much memory we allow to be
1033 * used by extent based free space tracking
1035 if (size
< 1024 * 1024 * 1024)
1036 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1038 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
1039 div64_u64(size
, 1024 * 1024 * 1024);
1042 * we want to account for 1 more bitmap than what we have so we can make
1043 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1044 * we add more bitmaps.
1046 bitmap_bytes
= (block_group
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
1048 if (bitmap_bytes
>= max_bytes
) {
1049 block_group
->extents_thresh
= 0;
1054 * we want the extent entry threshold to always be at most 1/2 the maxw
1055 * bytes we can have, or whatever is less than that.
1057 extent_bytes
= max_bytes
- bitmap_bytes
;
1058 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
1060 block_group
->extents_thresh
=
1061 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
1064 static void bitmap_clear_bits(struct btrfs_block_group_cache
*block_group
,
1065 struct btrfs_free_space
*info
, u64 offset
,
1068 unsigned long start
, end
;
1071 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1072 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1073 BUG_ON(end
> BITS_PER_BITMAP
);
1075 for (i
= start
; i
< end
; i
++)
1076 clear_bit(i
, info
->bitmap
);
1078 info
->bytes
-= bytes
;
1079 block_group
->free_space
-= bytes
;
1082 static void bitmap_set_bits(struct btrfs_block_group_cache
*block_group
,
1083 struct btrfs_free_space
*info
, u64 offset
,
1086 unsigned long start
, end
;
1089 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1090 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1091 BUG_ON(end
> BITS_PER_BITMAP
);
1093 for (i
= start
; i
< end
; i
++)
1094 set_bit(i
, info
->bitmap
);
1096 info
->bytes
+= bytes
;
1097 block_group
->free_space
+= bytes
;
1100 static int search_bitmap(struct btrfs_block_group_cache
*block_group
,
1101 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1104 unsigned long found_bits
= 0;
1105 unsigned long bits
, i
;
1106 unsigned long next_zero
;
1108 i
= offset_to_bit(bitmap_info
->offset
, block_group
->sectorsize
,
1109 max_t(u64
, *offset
, bitmap_info
->offset
));
1110 bits
= bytes_to_bits(*bytes
, block_group
->sectorsize
);
1112 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
1113 i
< BITS_PER_BITMAP
;
1114 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1115 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1116 BITS_PER_BITMAP
, i
);
1117 if ((next_zero
- i
) >= bits
) {
1118 found_bits
= next_zero
- i
;
1125 *offset
= (u64
)(i
* block_group
->sectorsize
) +
1126 bitmap_info
->offset
;
1127 *bytes
= (u64
)(found_bits
) * block_group
->sectorsize
;
1134 static struct btrfs_free_space
*find_free_space(struct btrfs_block_group_cache
1135 *block_group
, u64
*offset
,
1136 u64
*bytes
, int debug
)
1138 struct btrfs_free_space
*entry
;
1139 struct rb_node
*node
;
1142 if (!block_group
->free_space_offset
.rb_node
)
1145 entry
= tree_search_offset(block_group
,
1146 offset_to_bitmap(block_group
, *offset
),
1151 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1152 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1153 if (entry
->bytes
< *bytes
)
1156 if (entry
->bitmap
) {
1157 ret
= search_bitmap(block_group
, entry
, offset
, bytes
);
1163 *offset
= entry
->offset
;
1164 *bytes
= entry
->bytes
;
1171 static void add_new_bitmap(struct btrfs_block_group_cache
*block_group
,
1172 struct btrfs_free_space
*info
, u64 offset
)
1174 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
1175 int max_bitmaps
= (int)div64_u64(block_group
->key
.offset
+
1176 bytes_per_bg
- 1, bytes_per_bg
);
1177 BUG_ON(block_group
->total_bitmaps
>= max_bitmaps
);
1179 info
->offset
= offset_to_bitmap(block_group
, offset
);
1181 link_free_space(block_group
, info
);
1182 block_group
->total_bitmaps
++;
1184 recalculate_thresholds(block_group
);
1187 static void free_bitmap(struct btrfs_block_group_cache
*block_group
,
1188 struct btrfs_free_space
*bitmap_info
)
1190 unlink_free_space(block_group
, bitmap_info
);
1191 kfree(bitmap_info
->bitmap
);
1192 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1193 block_group
->total_bitmaps
--;
1194 recalculate_thresholds(block_group
);
1197 static noinline
int remove_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1198 struct btrfs_free_space
*bitmap_info
,
1199 u64
*offset
, u64
*bytes
)
1202 u64 search_start
, search_bytes
;
1206 end
= bitmap_info
->offset
+
1207 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
) - 1;
1210 * XXX - this can go away after a few releases.
1212 * since the only user of btrfs_remove_free_space is the tree logging
1213 * stuff, and the only way to test that is under crash conditions, we
1214 * want to have this debug stuff here just in case somethings not
1215 * working. Search the bitmap for the space we are trying to use to
1216 * make sure its actually there. If its not there then we need to stop
1217 * because something has gone wrong.
1219 search_start
= *offset
;
1220 search_bytes
= *bytes
;
1221 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1222 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1224 BUG_ON(ret
< 0 || search_start
!= *offset
);
1226 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
1227 bitmap_clear_bits(block_group
, bitmap_info
, *offset
,
1229 *bytes
-= end
- *offset
+ 1;
1231 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
1232 bitmap_clear_bits(block_group
, bitmap_info
, *offset
, *bytes
);
1237 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1238 if (!bitmap_info
->bytes
)
1239 free_bitmap(block_group
, bitmap_info
);
1242 * no entry after this bitmap, but we still have bytes to
1243 * remove, so something has gone wrong.
1248 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1252 * if the next entry isn't a bitmap we need to return to let the
1253 * extent stuff do its work.
1255 if (!bitmap_info
->bitmap
)
1259 * Ok the next item is a bitmap, but it may not actually hold
1260 * the information for the rest of this free space stuff, so
1261 * look for it, and if we don't find it return so we can try
1262 * everything over again.
1264 search_start
= *offset
;
1265 search_bytes
= *bytes
;
1266 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1268 if (ret
< 0 || search_start
!= *offset
)
1272 } else if (!bitmap_info
->bytes
)
1273 free_bitmap(block_group
, bitmap_info
);
1278 static int insert_into_bitmap(struct btrfs_block_group_cache
*block_group
,
1279 struct btrfs_free_space
*info
)
1281 struct btrfs_free_space
*bitmap_info
;
1283 u64 bytes
, offset
, end
;
1287 * If we are below the extents threshold then we can add this as an
1288 * extent, and don't have to deal with the bitmap
1290 if (block_group
->free_extents
< block_group
->extents_thresh
&&
1291 info
->bytes
> block_group
->sectorsize
* 4)
1295 * some block groups are so tiny they can't be enveloped by a bitmap, so
1296 * don't even bother to create a bitmap for this
1298 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
1299 block_group
->key
.offset
)
1302 bytes
= info
->bytes
;
1303 offset
= info
->offset
;
1306 bitmap_info
= tree_search_offset(block_group
,
1307 offset_to_bitmap(block_group
, offset
),
1314 end
= bitmap_info
->offset
+
1315 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
);
1317 if (offset
>= bitmap_info
->offset
&& offset
+ bytes
> end
) {
1318 bitmap_set_bits(block_group
, bitmap_info
, offset
,
1320 bytes
-= end
- offset
;
1323 } else if (offset
>= bitmap_info
->offset
&& offset
+ bytes
<= end
) {
1324 bitmap_set_bits(block_group
, bitmap_info
, offset
, bytes
);
1337 if (info
&& info
->bitmap
) {
1338 add_new_bitmap(block_group
, info
, offset
);
1343 spin_unlock(&block_group
->tree_lock
);
1345 /* no pre-allocated info, allocate a new one */
1347 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
1350 spin_lock(&block_group
->tree_lock
);
1356 /* allocate the bitmap */
1357 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1358 spin_lock(&block_group
->tree_lock
);
1359 if (!info
->bitmap
) {
1369 kfree(info
->bitmap
);
1370 kmem_cache_free(btrfs_free_space_cachep
, info
);
1376 bool try_merge_free_space(struct btrfs_block_group_cache
*block_group
,
1377 struct btrfs_free_space
*info
, bool update_stat
)
1379 struct btrfs_free_space
*left_info
;
1380 struct btrfs_free_space
*right_info
;
1381 bool merged
= false;
1382 u64 offset
= info
->offset
;
1383 u64 bytes
= info
->bytes
;
1386 * first we want to see if there is free space adjacent to the range we
1387 * are adding, if there is remove that struct and add a new one to
1388 * cover the entire range
1390 right_info
= tree_search_offset(block_group
, offset
+ bytes
, 0, 0);
1391 if (right_info
&& rb_prev(&right_info
->offset_index
))
1392 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1393 struct btrfs_free_space
, offset_index
);
1395 left_info
= tree_search_offset(block_group
, offset
- 1, 0, 0);
1397 if (right_info
&& !right_info
->bitmap
) {
1399 unlink_free_space(block_group
, right_info
);
1401 __unlink_free_space(block_group
, right_info
);
1402 info
->bytes
+= right_info
->bytes
;
1403 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
1407 if (left_info
&& !left_info
->bitmap
&&
1408 left_info
->offset
+ left_info
->bytes
== offset
) {
1410 unlink_free_space(block_group
, left_info
);
1412 __unlink_free_space(block_group
, left_info
);
1413 info
->offset
= left_info
->offset
;
1414 info
->bytes
+= left_info
->bytes
;
1415 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
1422 int btrfs_add_free_space(struct btrfs_block_group_cache
*block_group
,
1423 u64 offset
, u64 bytes
)
1425 struct btrfs_free_space
*info
;
1428 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
1432 info
->offset
= offset
;
1433 info
->bytes
= bytes
;
1435 spin_lock(&block_group
->tree_lock
);
1437 if (try_merge_free_space(block_group
, info
, true))
1441 * There was no extent directly to the left or right of this new
1442 * extent then we know we're going to have to allocate a new extent, so
1443 * before we do that see if we need to drop this into a bitmap
1445 ret
= insert_into_bitmap(block_group
, info
);
1453 ret
= link_free_space(block_group
, info
);
1455 kmem_cache_free(btrfs_free_space_cachep
, info
);
1457 spin_unlock(&block_group
->tree_lock
);
1460 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1461 BUG_ON(ret
== -EEXIST
);
1467 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1468 u64 offset
, u64 bytes
)
1470 struct btrfs_free_space
*info
;
1471 struct btrfs_free_space
*next_info
= NULL
;
1474 spin_lock(&block_group
->tree_lock
);
1477 info
= tree_search_offset(block_group
, offset
, 0, 0);
1480 * oops didn't find an extent that matched the space we wanted
1481 * to remove, look for a bitmap instead
1483 info
= tree_search_offset(block_group
,
1484 offset_to_bitmap(block_group
, offset
),
1492 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1494 next_info
= rb_entry(rb_next(&info
->offset_index
),
1495 struct btrfs_free_space
,
1498 if (next_info
->bitmap
)
1499 end
= next_info
->offset
+ BITS_PER_BITMAP
*
1500 block_group
->sectorsize
- 1;
1502 end
= next_info
->offset
+ next_info
->bytes
;
1504 if (next_info
->bytes
< bytes
||
1505 next_info
->offset
> offset
|| offset
> end
) {
1506 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1507 " trying to use %llu\n",
1508 (unsigned long long)info
->offset
,
1509 (unsigned long long)info
->bytes
,
1510 (unsigned long long)bytes
);
1519 if (info
->bytes
== bytes
) {
1520 unlink_free_space(block_group
, info
);
1522 kfree(info
->bitmap
);
1523 block_group
->total_bitmaps
--;
1525 kmem_cache_free(btrfs_free_space_cachep
, info
);
1529 if (!info
->bitmap
&& info
->offset
== offset
) {
1530 unlink_free_space(block_group
, info
);
1531 info
->offset
+= bytes
;
1532 info
->bytes
-= bytes
;
1533 link_free_space(block_group
, info
);
1537 if (!info
->bitmap
&& info
->offset
<= offset
&&
1538 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1539 u64 old_start
= info
->offset
;
1541 * we're freeing space in the middle of the info,
1542 * this can happen during tree log replay
1544 * first unlink the old info and then
1545 * insert it again after the hole we're creating
1547 unlink_free_space(block_group
, info
);
1548 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1549 u64 old_end
= info
->offset
+ info
->bytes
;
1551 info
->offset
= offset
+ bytes
;
1552 info
->bytes
= old_end
- info
->offset
;
1553 ret
= link_free_space(block_group
, info
);
1558 /* the hole we're creating ends at the end
1559 * of the info struct, just free the info
1561 kmem_cache_free(btrfs_free_space_cachep
, info
);
1563 spin_unlock(&block_group
->tree_lock
);
1565 /* step two, insert a new info struct to cover
1566 * anything before the hole
1568 ret
= btrfs_add_free_space(block_group
, old_start
,
1569 offset
- old_start
);
1574 ret
= remove_from_bitmap(block_group
, info
, &offset
, &bytes
);
1579 spin_unlock(&block_group
->tree_lock
);
1584 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1587 struct btrfs_free_space
*info
;
1591 for (n
= rb_first(&block_group
->free_space_offset
); n
; n
= rb_next(n
)) {
1592 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1593 if (info
->bytes
>= bytes
)
1595 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1596 (unsigned long long)info
->offset
,
1597 (unsigned long long)info
->bytes
,
1598 (info
->bitmap
) ? "yes" : "no");
1600 printk(KERN_INFO
"block group has cluster?: %s\n",
1601 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1602 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1606 u64
btrfs_block_group_free_space(struct btrfs_block_group_cache
*block_group
)
1608 struct btrfs_free_space
*info
;
1612 for (n
= rb_first(&block_group
->free_space_offset
); n
;
1614 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1622 * for a given cluster, put all of its extents back into the free
1623 * space cache. If the block group passed doesn't match the block group
1624 * pointed to by the cluster, someone else raced in and freed the
1625 * cluster already. In that case, we just return without changing anything
1628 __btrfs_return_cluster_to_free_space(
1629 struct btrfs_block_group_cache
*block_group
,
1630 struct btrfs_free_cluster
*cluster
)
1632 struct btrfs_free_space
*entry
;
1633 struct rb_node
*node
;
1636 spin_lock(&cluster
->lock
);
1637 if (cluster
->block_group
!= block_group
)
1640 bitmap
= cluster
->points_to_bitmap
;
1641 cluster
->block_group
= NULL
;
1642 cluster
->window_start
= 0;
1643 list_del_init(&cluster
->block_group_list
);
1644 cluster
->points_to_bitmap
= false;
1649 node
= rb_first(&cluster
->root
);
1651 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1652 node
= rb_next(&entry
->offset_index
);
1653 rb_erase(&entry
->offset_index
, &cluster
->root
);
1654 BUG_ON(entry
->bitmap
);
1655 try_merge_free_space(block_group
, entry
, false);
1656 tree_insert_offset(&block_group
->free_space_offset
,
1657 entry
->offset
, &entry
->offset_index
, 0);
1659 cluster
->root
= RB_ROOT
;
1662 spin_unlock(&cluster
->lock
);
1663 btrfs_put_block_group(block_group
);
1667 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
1669 struct btrfs_free_space
*info
;
1670 struct rb_node
*node
;
1671 struct btrfs_free_cluster
*cluster
;
1672 struct list_head
*head
;
1674 spin_lock(&block_group
->tree_lock
);
1675 while ((head
= block_group
->cluster_list
.next
) !=
1676 &block_group
->cluster_list
) {
1677 cluster
= list_entry(head
, struct btrfs_free_cluster
,
1680 WARN_ON(cluster
->block_group
!= block_group
);
1681 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1682 if (need_resched()) {
1683 spin_unlock(&block_group
->tree_lock
);
1685 spin_lock(&block_group
->tree_lock
);
1689 while ((node
= rb_last(&block_group
->free_space_offset
)) != NULL
) {
1690 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1691 unlink_free_space(block_group
, info
);
1693 kfree(info
->bitmap
);
1694 kmem_cache_free(btrfs_free_space_cachep
, info
);
1695 if (need_resched()) {
1696 spin_unlock(&block_group
->tree_lock
);
1698 spin_lock(&block_group
->tree_lock
);
1702 spin_unlock(&block_group
->tree_lock
);
1705 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
1706 u64 offset
, u64 bytes
, u64 empty_size
)
1708 struct btrfs_free_space
*entry
= NULL
;
1709 u64 bytes_search
= bytes
+ empty_size
;
1712 spin_lock(&block_group
->tree_lock
);
1713 entry
= find_free_space(block_group
, &offset
, &bytes_search
, 0);
1718 if (entry
->bitmap
) {
1719 bitmap_clear_bits(block_group
, entry
, offset
, bytes
);
1721 free_bitmap(block_group
, entry
);
1723 unlink_free_space(block_group
, entry
);
1724 entry
->offset
+= bytes
;
1725 entry
->bytes
-= bytes
;
1727 kmem_cache_free(btrfs_free_space_cachep
, entry
);
1729 link_free_space(block_group
, entry
);
1733 spin_unlock(&block_group
->tree_lock
);
1739 * given a cluster, put all of its extents back into the free space
1740 * cache. If a block group is passed, this function will only free
1741 * a cluster that belongs to the passed block group.
1743 * Otherwise, it'll get a reference on the block group pointed to by the
1744 * cluster and remove the cluster from it.
1746 int btrfs_return_cluster_to_free_space(
1747 struct btrfs_block_group_cache
*block_group
,
1748 struct btrfs_free_cluster
*cluster
)
1752 /* first, get a safe pointer to the block group */
1753 spin_lock(&cluster
->lock
);
1755 block_group
= cluster
->block_group
;
1757 spin_unlock(&cluster
->lock
);
1760 } else if (cluster
->block_group
!= block_group
) {
1761 /* someone else has already freed it don't redo their work */
1762 spin_unlock(&cluster
->lock
);
1765 atomic_inc(&block_group
->count
);
1766 spin_unlock(&cluster
->lock
);
1768 /* now return any extents the cluster had on it */
1769 spin_lock(&block_group
->tree_lock
);
1770 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1771 spin_unlock(&block_group
->tree_lock
);
1773 /* finally drop our ref */
1774 btrfs_put_block_group(block_group
);
1778 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1779 struct btrfs_free_cluster
*cluster
,
1780 u64 bytes
, u64 min_start
)
1782 struct btrfs_free_space
*entry
;
1784 u64 search_start
= cluster
->window_start
;
1785 u64 search_bytes
= bytes
;
1788 spin_lock(&block_group
->tree_lock
);
1789 spin_lock(&cluster
->lock
);
1791 if (!cluster
->points_to_bitmap
)
1794 if (cluster
->block_group
!= block_group
)
1798 * search_start is the beginning of the bitmap, but at some point it may
1799 * be a good idea to point to the actual start of the free area in the
1800 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1801 * to 1 to make sure we get the bitmap entry
1803 entry
= tree_search_offset(block_group
,
1804 offset_to_bitmap(block_group
, search_start
),
1806 if (!entry
|| !entry
->bitmap
)
1809 search_start
= min_start
;
1810 search_bytes
= bytes
;
1812 err
= search_bitmap(block_group
, entry
, &search_start
,
1818 bitmap_clear_bits(block_group
, entry
, ret
, bytes
);
1819 if (entry
->bytes
== 0)
1820 free_bitmap(block_group
, entry
);
1822 spin_unlock(&cluster
->lock
);
1823 spin_unlock(&block_group
->tree_lock
);
1829 * given a cluster, try to allocate 'bytes' from it, returns 0
1830 * if it couldn't find anything suitably large, or a logical disk offset
1831 * if things worked out
1833 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
1834 struct btrfs_free_cluster
*cluster
, u64 bytes
,
1837 struct btrfs_free_space
*entry
= NULL
;
1838 struct rb_node
*node
;
1841 if (cluster
->points_to_bitmap
)
1842 return btrfs_alloc_from_bitmap(block_group
, cluster
, bytes
,
1845 spin_lock(&cluster
->lock
);
1846 if (bytes
> cluster
->max_size
)
1849 if (cluster
->block_group
!= block_group
)
1852 node
= rb_first(&cluster
->root
);
1856 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1859 if (entry
->bytes
< bytes
|| entry
->offset
< min_start
) {
1860 struct rb_node
*node
;
1862 node
= rb_next(&entry
->offset_index
);
1865 entry
= rb_entry(node
, struct btrfs_free_space
,
1869 ret
= entry
->offset
;
1871 entry
->offset
+= bytes
;
1872 entry
->bytes
-= bytes
;
1874 if (entry
->bytes
== 0)
1875 rb_erase(&entry
->offset_index
, &cluster
->root
);
1879 spin_unlock(&cluster
->lock
);
1884 spin_lock(&block_group
->tree_lock
);
1886 block_group
->free_space
-= bytes
;
1887 if (entry
->bytes
== 0) {
1888 block_group
->free_extents
--;
1889 kmem_cache_free(btrfs_free_space_cachep
, entry
);
1892 spin_unlock(&block_group
->tree_lock
);
1897 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
1898 struct btrfs_free_space
*entry
,
1899 struct btrfs_free_cluster
*cluster
,
1900 u64 offset
, u64 bytes
, u64 min_bytes
)
1902 unsigned long next_zero
;
1904 unsigned long search_bits
;
1905 unsigned long total_bits
;
1906 unsigned long found_bits
;
1907 unsigned long start
= 0;
1908 unsigned long total_found
= 0;
1911 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
1912 max_t(u64
, offset
, entry
->offset
));
1913 search_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
1914 total_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
1918 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
1919 i
< BITS_PER_BITMAP
;
1920 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1921 next_zero
= find_next_zero_bit(entry
->bitmap
,
1922 BITS_PER_BITMAP
, i
);
1923 if (next_zero
- i
>= search_bits
) {
1924 found_bits
= next_zero
- i
;
1938 total_found
+= found_bits
;
1940 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
1941 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
1943 if (total_found
< total_bits
) {
1944 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, next_zero
);
1945 if (i
- start
> total_bits
* 2) {
1947 cluster
->max_size
= 0;
1953 cluster
->window_start
= start
* block_group
->sectorsize
+
1955 cluster
->points_to_bitmap
= true;
1961 * here we try to find a cluster of blocks in a block group. The goal
1962 * is to find at least bytes free and up to empty_size + bytes free.
1963 * We might not find them all in one contiguous area.
1965 * returns zero and sets up cluster if things worked out, otherwise
1966 * it returns -enospc
1968 int btrfs_find_space_cluster(struct btrfs_trans_handle
*trans
,
1969 struct btrfs_root
*root
,
1970 struct btrfs_block_group_cache
*block_group
,
1971 struct btrfs_free_cluster
*cluster
,
1972 u64 offset
, u64 bytes
, u64 empty_size
)
1974 struct btrfs_free_space
*entry
= NULL
;
1975 struct rb_node
*node
;
1976 struct btrfs_free_space
*next
;
1977 struct btrfs_free_space
*last
= NULL
;
1982 bool found_bitmap
= false;
1985 /* for metadata, allow allocates with more holes */
1986 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
1987 min_bytes
= bytes
+ empty_size
;
1988 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
1990 * we want to do larger allocations when we are
1991 * flushing out the delayed refs, it helps prevent
1992 * making more work as we go along.
1994 if (trans
->transaction
->delayed_refs
.flushing
)
1995 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 1);
1997 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 4);
1999 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
2001 spin_lock(&block_group
->tree_lock
);
2004 * If we know we don't have enough space to make a cluster don't even
2005 * bother doing all the work to try and find one.
2007 if (block_group
->free_space
< min_bytes
) {
2008 spin_unlock(&block_group
->tree_lock
);
2012 spin_lock(&cluster
->lock
);
2014 /* someone already found a cluster, hooray */
2015 if (cluster
->block_group
) {
2020 entry
= tree_search_offset(block_group
, offset
, found_bitmap
, 1);
2027 * If found_bitmap is true, we exhausted our search for extent entries,
2028 * and we just want to search all of the bitmaps that we can find, and
2029 * ignore any extent entries we find.
2031 while (entry
->bitmap
|| found_bitmap
||
2032 (!entry
->bitmap
&& entry
->bytes
< min_bytes
)) {
2033 struct rb_node
*node
= rb_next(&entry
->offset_index
);
2035 if (entry
->bitmap
&& entry
->bytes
> bytes
+ empty_size
) {
2036 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
,
2037 offset
, bytes
+ empty_size
,
2047 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2051 * We already searched all the extent entries from the passed in offset
2052 * to the end and didn't find enough space for the cluster, and we also
2053 * didn't find any bitmaps that met our criteria, just go ahead and exit
2060 cluster
->points_to_bitmap
= false;
2061 window_start
= entry
->offset
;
2062 window_free
= entry
->bytes
;
2064 max_extent
= entry
->bytes
;
2067 /* out window is just right, lets fill it */
2068 if (window_free
>= bytes
+ empty_size
)
2071 node
= rb_next(&last
->offset_index
);
2078 next
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2081 * we found a bitmap, so if this search doesn't result in a
2082 * cluster, we know to go and search again for the bitmaps and
2083 * start looking for space there
2087 offset
= next
->offset
;
2088 found_bitmap
= true;
2094 * we haven't filled the empty size and the window is
2095 * very large. reset and try again
2097 if (next
->offset
- (last
->offset
+ last
->bytes
) > 128 * 1024 ||
2098 next
->offset
- window_start
> (bytes
+ empty_size
) * 2) {
2100 window_start
= entry
->offset
;
2101 window_free
= entry
->bytes
;
2103 max_extent
= entry
->bytes
;
2106 window_free
+= next
->bytes
;
2107 if (entry
->bytes
> max_extent
)
2108 max_extent
= entry
->bytes
;
2112 cluster
->window_start
= entry
->offset
;
2115 * now we've found our entries, pull them out of the free space
2116 * cache and put them into the cluster rbtree
2118 * The cluster includes an rbtree, but only uses the offset index
2119 * of each free space cache entry.
2122 node
= rb_next(&entry
->offset_index
);
2123 if (entry
->bitmap
&& node
) {
2124 entry
= rb_entry(node
, struct btrfs_free_space
,
2127 } else if (entry
->bitmap
&& !node
) {
2131 rb_erase(&entry
->offset_index
, &block_group
->free_space_offset
);
2132 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2133 &entry
->offset_index
, 0);
2136 if (!node
|| entry
== last
)
2139 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2142 cluster
->max_size
= max_extent
;
2145 atomic_inc(&block_group
->count
);
2146 list_add_tail(&cluster
->block_group_list
, &block_group
->cluster_list
);
2147 cluster
->block_group
= block_group
;
2149 spin_unlock(&cluster
->lock
);
2150 spin_unlock(&block_group
->tree_lock
);
2156 * simple code to zero out a cluster
2158 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
2160 spin_lock_init(&cluster
->lock
);
2161 spin_lock_init(&cluster
->refill_lock
);
2162 cluster
->root
= RB_ROOT
;
2163 cluster
->max_size
= 0;
2164 cluster
->points_to_bitmap
= false;
2165 INIT_LIST_HEAD(&cluster
->block_group_list
);
2166 cluster
->block_group
= NULL
;