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"
27 #include "extent_io.h"
29 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
30 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
32 static void recalculate_thresholds(struct btrfs_block_group_cache
34 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
35 struct btrfs_free_space
*info
);
37 struct inode
*lookup_free_space_inode(struct btrfs_root
*root
,
38 struct btrfs_block_group_cache
39 *block_group
, struct btrfs_path
*path
)
42 struct btrfs_key location
;
43 struct btrfs_disk_key disk_key
;
44 struct btrfs_free_space_header
*header
;
45 struct extent_buffer
*leaf
;
46 struct inode
*inode
= NULL
;
49 spin_lock(&block_group
->lock
);
50 if (block_group
->inode
)
51 inode
= igrab(block_group
->inode
);
52 spin_unlock(&block_group
->lock
);
56 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
57 key
.offset
= block_group
->key
.objectid
;
60 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
64 btrfs_release_path(root
, path
);
65 return ERR_PTR(-ENOENT
);
68 leaf
= path
->nodes
[0];
69 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
70 struct btrfs_free_space_header
);
71 btrfs_free_space_key(leaf
, header
, &disk_key
);
72 btrfs_disk_key_to_cpu(&location
, &disk_key
);
73 btrfs_release_path(root
, path
);
75 inode
= btrfs_iget(root
->fs_info
->sb
, &location
, root
, NULL
);
77 return ERR_PTR(-ENOENT
);
80 if (is_bad_inode(inode
)) {
82 return ERR_PTR(-ENOENT
);
85 inode
->i_mapping
->flags
&= ~__GFP_FS
;
87 spin_lock(&block_group
->lock
);
88 if (!root
->fs_info
->closing
) {
89 block_group
->inode
= igrab(inode
);
90 block_group
->iref
= 1;
92 spin_unlock(&block_group
->lock
);
97 int create_free_space_inode(struct btrfs_root
*root
,
98 struct btrfs_trans_handle
*trans
,
99 struct btrfs_block_group_cache
*block_group
,
100 struct btrfs_path
*path
)
102 struct btrfs_key key
;
103 struct btrfs_disk_key disk_key
;
104 struct btrfs_free_space_header
*header
;
105 struct btrfs_inode_item
*inode_item
;
106 struct extent_buffer
*leaf
;
110 ret
= btrfs_find_free_objectid(trans
, root
, 0, &objectid
);
114 ret
= btrfs_insert_empty_inode(trans
, root
, path
, objectid
);
118 leaf
= path
->nodes
[0];
119 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
120 struct btrfs_inode_item
);
121 btrfs_item_key(leaf
, &disk_key
, path
->slots
[0]);
122 memset_extent_buffer(leaf
, 0, (unsigned long)inode_item
,
123 sizeof(*inode_item
));
124 btrfs_set_inode_generation(leaf
, inode_item
, trans
->transid
);
125 btrfs_set_inode_size(leaf
, inode_item
, 0);
126 btrfs_set_inode_nbytes(leaf
, inode_item
, 0);
127 btrfs_set_inode_uid(leaf
, inode_item
, 0);
128 btrfs_set_inode_gid(leaf
, inode_item
, 0);
129 btrfs_set_inode_mode(leaf
, inode_item
, S_IFREG
| 0600);
130 btrfs_set_inode_flags(leaf
, inode_item
, BTRFS_INODE_NOCOMPRESS
|
131 BTRFS_INODE_PREALLOC
| BTRFS_INODE_NODATASUM
);
132 btrfs_set_inode_nlink(leaf
, inode_item
, 1);
133 btrfs_set_inode_transid(leaf
, inode_item
, trans
->transid
);
134 btrfs_set_inode_block_group(leaf
, inode_item
,
135 block_group
->key
.objectid
);
136 btrfs_mark_buffer_dirty(leaf
);
137 btrfs_release_path(root
, path
);
139 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
140 key
.offset
= block_group
->key
.objectid
;
143 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
144 sizeof(struct btrfs_free_space_header
));
146 btrfs_release_path(root
, path
);
149 leaf
= path
->nodes
[0];
150 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
151 struct btrfs_free_space_header
);
152 memset_extent_buffer(leaf
, 0, (unsigned long)header
, sizeof(*header
));
153 btrfs_set_free_space_key(leaf
, header
, &disk_key
);
154 btrfs_mark_buffer_dirty(leaf
);
155 btrfs_release_path(root
, path
);
160 int btrfs_truncate_free_space_cache(struct btrfs_root
*root
,
161 struct btrfs_trans_handle
*trans
,
162 struct btrfs_path
*path
,
168 trans
->block_rsv
= root
->orphan_block_rsv
;
169 ret
= btrfs_block_rsv_check(trans
, root
,
170 root
->orphan_block_rsv
,
175 oldsize
= i_size_read(inode
);
176 btrfs_i_size_write(inode
, 0);
177 truncate_pagecache(inode
, oldsize
, 0);
180 * We don't need an orphan item because truncating the free space cache
181 * will never be split across transactions.
183 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
184 0, BTRFS_EXTENT_DATA_KEY
);
190 return btrfs_update_inode(trans
, root
, inode
);
193 static int readahead_cache(struct inode
*inode
)
195 struct file_ra_state
*ra
;
196 unsigned long last_index
;
198 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
202 file_ra_state_init(ra
, inode
->i_mapping
);
203 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
205 page_cache_sync_readahead(inode
->i_mapping
, ra
, NULL
, 0, last_index
);
212 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
213 struct btrfs_block_group_cache
*block_group
)
215 struct btrfs_root
*root
= fs_info
->tree_root
;
217 struct btrfs_free_space_header
*header
;
218 struct extent_buffer
*leaf
;
220 struct btrfs_path
*path
;
221 u32
*checksums
= NULL
, *crc
;
222 char *disk_crcs
= NULL
;
223 struct btrfs_key key
;
224 struct list_head bitmaps
;
228 u64 used
= btrfs_block_group_used(&block_group
->item
);
229 u32 cur_crc
= ~(u32
)0;
231 unsigned long first_page_offset
;
236 * If we're unmounting then just return, since this does a search on the
237 * normal root and not the commit root and we could deadlock.
240 if (fs_info
->closing
)
244 * If this block group has been marked to be cleared for one reason or
245 * another then we can't trust the on disk cache, so just return.
247 spin_lock(&block_group
->lock
);
248 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
249 spin_unlock(&block_group
->lock
);
252 spin_unlock(&block_group
->lock
);
254 INIT_LIST_HEAD(&bitmaps
);
256 path
= btrfs_alloc_path();
260 inode
= lookup_free_space_inode(root
, block_group
, path
);
262 btrfs_free_path(path
);
266 /* Nothing in the space cache, goodbye */
267 if (!i_size_read(inode
)) {
268 btrfs_free_path(path
);
272 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
273 key
.offset
= block_group
->key
.objectid
;
276 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
278 btrfs_free_path(path
);
282 leaf
= path
->nodes
[0];
283 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
284 struct btrfs_free_space_header
);
285 num_entries
= btrfs_free_space_entries(leaf
, header
);
286 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
287 generation
= btrfs_free_space_generation(leaf
, header
);
288 btrfs_free_path(path
);
290 if (BTRFS_I(inode
)->generation
!= generation
) {
291 printk(KERN_ERR
"btrfs: free space inode generation (%llu) did"
292 " not match free space cache generation (%llu) for "
293 "block group %llu\n",
294 (unsigned long long)BTRFS_I(inode
)->generation
,
295 (unsigned long long)generation
,
296 (unsigned long long)block_group
->key
.objectid
);
303 /* Setup everything for doing checksumming */
304 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
305 checksums
= crc
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
308 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
309 disk_crcs
= kzalloc(first_page_offset
, GFP_NOFS
);
313 ret
= readahead_cache(inode
);
320 struct btrfs_free_space_entry
*entry
;
321 struct btrfs_free_space
*e
;
323 unsigned long offset
= 0;
324 unsigned long start_offset
= 0;
327 if (!num_entries
&& !num_bitmaps
)
331 start_offset
= first_page_offset
;
332 offset
= start_offset
;
335 page
= grab_cache_page(inode
->i_mapping
, index
);
341 if (!PageUptodate(page
)) {
342 btrfs_readpage(NULL
, page
);
344 if (!PageUptodate(page
)) {
346 page_cache_release(page
);
347 printk(KERN_ERR
"btrfs: error reading free "
348 "space cache: %llu\n",
350 block_group
->key
.objectid
);
359 memcpy(disk_crcs
, addr
, first_page_offset
);
360 gen
= addr
+ (sizeof(u32
) * num_checksums
);
361 if (*gen
!= BTRFS_I(inode
)->generation
) {
362 printk(KERN_ERR
"btrfs: space cache generation"
363 " (%llu) does not match inode (%llu) "
364 "for block group %llu\n",
365 (unsigned long long)*gen
,
367 BTRFS_I(inode
)->generation
,
369 block_group
->key
.objectid
);
372 page_cache_release(page
);
375 crc
= (u32
*)disk_crcs
;
377 entry
= addr
+ start_offset
;
379 /* First lets check our crc before we do anything fun */
381 cur_crc
= btrfs_csum_data(root
, addr
+ start_offset
, cur_crc
,
382 PAGE_CACHE_SIZE
- start_offset
);
383 btrfs_csum_final(cur_crc
, (char *)&cur_crc
);
384 if (cur_crc
!= *crc
) {
385 printk(KERN_ERR
"btrfs: crc mismatch for page %lu in "
386 "block group %llu\n", index
,
387 (unsigned long long)block_group
->key
.objectid
);
390 page_cache_release(page
);
400 e
= kmem_cache_zalloc(btrfs_free_space_cachep
,
405 page_cache_release(page
);
409 e
->offset
= le64_to_cpu(entry
->offset
);
410 e
->bytes
= le64_to_cpu(entry
->bytes
);
413 kmem_cache_free(btrfs_free_space_cachep
, e
);
415 page_cache_release(page
);
419 if (entry
->type
== BTRFS_FREE_SPACE_EXTENT
) {
420 spin_lock(&block_group
->tree_lock
);
421 ret
= link_free_space(block_group
, e
);
422 spin_unlock(&block_group
->tree_lock
);
425 e
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
429 btrfs_free_space_cachep
, e
);
431 page_cache_release(page
);
434 spin_lock(&block_group
->tree_lock
);
435 ret
= link_free_space(block_group
, e
);
436 block_group
->total_bitmaps
++;
437 recalculate_thresholds(block_group
);
438 spin_unlock(&block_group
->tree_lock
);
439 list_add_tail(&e
->list
, &bitmaps
);
443 offset
+= sizeof(struct btrfs_free_space_entry
);
444 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
451 * We read an entry out of this page, we need to move on to the
460 * We add the bitmaps at the end of the entries in order that
461 * the bitmap entries are added to the cache.
463 e
= list_entry(bitmaps
.next
, struct btrfs_free_space
, list
);
464 list_del_init(&e
->list
);
465 memcpy(e
->bitmap
, addr
, PAGE_CACHE_SIZE
);
470 page_cache_release(page
);
474 spin_lock(&block_group
->tree_lock
);
475 if (block_group
->free_space
!= (block_group
->key
.offset
- used
-
476 block_group
->bytes_super
)) {
477 spin_unlock(&block_group
->tree_lock
);
478 printk(KERN_ERR
"block group %llu has an wrong amount of free "
479 "space\n", block_group
->key
.objectid
);
483 spin_unlock(&block_group
->tree_lock
);
493 /* This cache is bogus, make sure it gets cleared */
494 spin_lock(&block_group
->lock
);
495 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
496 spin_unlock(&block_group
->lock
);
497 btrfs_remove_free_space_cache(block_group
);
501 int btrfs_write_out_cache(struct btrfs_root
*root
,
502 struct btrfs_trans_handle
*trans
,
503 struct btrfs_block_group_cache
*block_group
,
504 struct btrfs_path
*path
)
506 struct btrfs_free_space_header
*header
;
507 struct extent_buffer
*leaf
;
509 struct rb_node
*node
;
510 struct list_head
*pos
, *n
;
512 struct extent_state
*cached_state
= NULL
;
513 struct btrfs_free_cluster
*cluster
= NULL
;
514 struct extent_io_tree
*unpin
= NULL
;
515 struct list_head bitmap_list
;
516 struct btrfs_key key
;
519 u32
*crc
, *checksums
;
520 pgoff_t index
= 0, last_index
= 0;
521 unsigned long first_page_offset
;
526 bool next_page
= false;
528 root
= root
->fs_info
->tree_root
;
530 INIT_LIST_HEAD(&bitmap_list
);
532 spin_lock(&block_group
->lock
);
533 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
534 spin_unlock(&block_group
->lock
);
537 spin_unlock(&block_group
->lock
);
539 inode
= lookup_free_space_inode(root
, block_group
, path
);
543 if (!i_size_read(inode
)) {
548 node
= rb_first(&block_group
->free_space_offset
);
554 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
555 filemap_write_and_wait(inode
->i_mapping
);
556 btrfs_wait_ordered_range(inode
, inode
->i_size
&
557 ~(root
->sectorsize
- 1), (u64
)-1);
559 /* We need a checksum per page. */
560 num_checksums
= i_size_read(inode
) / PAGE_CACHE_SIZE
;
561 crc
= checksums
= kzalloc(sizeof(u32
) * num_checksums
, GFP_NOFS
);
567 /* Since the first page has all of our checksums and our generation we
568 * need to calculate the offset into the page that we can start writing
571 first_page_offset
= (sizeof(u32
) * num_checksums
) + sizeof(u64
);
573 /* Get the cluster for this block_group if it exists */
574 if (!list_empty(&block_group
->cluster_list
))
575 cluster
= list_entry(block_group
->cluster_list
.next
,
576 struct btrfs_free_cluster
,
580 * We shouldn't have switched the pinned extents yet so this is the
583 unpin
= root
->fs_info
->pinned_extents
;
586 * Lock all pages first so we can lock the extent safely.
588 * NOTE: Because we hold the ref the entire time we're going to write to
589 * the page find_get_page should never fail, so we don't do a check
590 * after find_get_page at this point. Just putting this here so people
591 * know and don't freak out.
593 while (index
<= last_index
) {
594 page
= grab_cache_page(inode
->i_mapping
, index
);
599 page
= find_get_page(inode
->i_mapping
, i
);
601 page_cache_release(page
);
602 page_cache_release(page
);
611 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
612 0, &cached_state
, GFP_NOFS
);
615 * When searching for pinned extents, we need to start at our start
618 start
= block_group
->key
.objectid
;
620 /* Write out the extent entries */
622 struct btrfs_free_space_entry
*entry
;
624 unsigned long offset
= 0;
625 unsigned long start_offset
= 0;
630 start_offset
= first_page_offset
;
631 offset
= start_offset
;
634 page
= find_get_page(inode
->i_mapping
, index
);
637 entry
= addr
+ start_offset
;
639 memset(addr
, 0, PAGE_CACHE_SIZE
);
640 while (node
&& !next_page
) {
641 struct btrfs_free_space
*e
;
643 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
646 entry
->offset
= cpu_to_le64(e
->offset
);
647 entry
->bytes
= cpu_to_le64(e
->bytes
);
649 entry
->type
= BTRFS_FREE_SPACE_BITMAP
;
650 list_add_tail(&e
->list
, &bitmap_list
);
653 entry
->type
= BTRFS_FREE_SPACE_EXTENT
;
655 node
= rb_next(node
);
656 if (!node
&& cluster
) {
657 node
= rb_first(&cluster
->root
);
660 offset
+= sizeof(struct btrfs_free_space_entry
);
661 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
668 * We want to add any pinned extents to our free space cache
669 * so we don't leak the space
671 while (!next_page
&& (start
< block_group
->key
.objectid
+
672 block_group
->key
.offset
)) {
673 ret
= find_first_extent_bit(unpin
, start
, &start
, &end
,
680 /* This pinned extent is out of our range */
681 if (start
>= block_group
->key
.objectid
+
682 block_group
->key
.offset
)
685 len
= block_group
->key
.objectid
+
686 block_group
->key
.offset
- start
;
687 len
= min(len
, end
+ 1 - start
);
690 entry
->offset
= cpu_to_le64(start
);
691 entry
->bytes
= cpu_to_le64(len
);
692 entry
->type
= BTRFS_FREE_SPACE_EXTENT
;
695 offset
+= sizeof(struct btrfs_free_space_entry
);
696 if (offset
+ sizeof(struct btrfs_free_space_entry
) >=
702 *crc
= btrfs_csum_data(root
, addr
+ start_offset
, *crc
,
703 PAGE_CACHE_SIZE
- start_offset
);
706 btrfs_csum_final(*crc
, (char *)crc
);
709 bytes
+= PAGE_CACHE_SIZE
;
711 ClearPageChecked(page
);
712 set_page_extent_mapped(page
);
713 SetPageUptodate(page
);
714 set_page_dirty(page
);
717 * We need to release our reference we got for grab_cache_page,
718 * except for the first page which will hold our checksums, we
723 page_cache_release(page
);
726 page_cache_release(page
);
729 } while (node
|| next_page
);
731 /* Write out the bitmaps */
732 list_for_each_safe(pos
, n
, &bitmap_list
) {
734 struct btrfs_free_space
*entry
=
735 list_entry(pos
, struct btrfs_free_space
, list
);
737 page
= find_get_page(inode
->i_mapping
, index
);
740 memcpy(addr
, entry
->bitmap
, PAGE_CACHE_SIZE
);
742 *crc
= btrfs_csum_data(root
, addr
, *crc
, PAGE_CACHE_SIZE
);
744 btrfs_csum_final(*crc
, (char *)crc
);
746 bytes
+= PAGE_CACHE_SIZE
;
748 ClearPageChecked(page
);
749 set_page_extent_mapped(page
);
750 SetPageUptodate(page
);
751 set_page_dirty(page
);
753 page_cache_release(page
);
754 page_cache_release(page
);
755 list_del_init(&entry
->list
);
759 /* Zero out the rest of the pages just to make sure */
760 while (index
<= last_index
) {
763 page
= find_get_page(inode
->i_mapping
, index
);
766 memset(addr
, 0, PAGE_CACHE_SIZE
);
768 ClearPageChecked(page
);
769 set_page_extent_mapped(page
);
770 SetPageUptodate(page
);
771 set_page_dirty(page
);
773 page_cache_release(page
);
774 page_cache_release(page
);
775 bytes
+= PAGE_CACHE_SIZE
;
779 btrfs_set_extent_delalloc(inode
, 0, bytes
- 1, &cached_state
);
781 /* Write the checksums and trans id to the first page */
786 page
= find_get_page(inode
->i_mapping
, 0);
789 memcpy(addr
, checksums
, sizeof(u32
) * num_checksums
);
790 gen
= addr
+ (sizeof(u32
) * num_checksums
);
791 *gen
= trans
->transid
;
793 ClearPageChecked(page
);
794 set_page_extent_mapped(page
);
795 SetPageUptodate(page
);
796 set_page_dirty(page
);
798 page_cache_release(page
);
799 page_cache_release(page
);
801 BTRFS_I(inode
)->generation
= trans
->transid
;
803 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
804 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
806 filemap_write_and_wait(inode
->i_mapping
);
808 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
809 key
.offset
= block_group
->key
.objectid
;
812 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
815 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
816 EXTENT_DIRTY
| EXTENT_DELALLOC
|
817 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
, GFP_NOFS
);
820 leaf
= path
->nodes
[0];
822 struct btrfs_key found_key
;
823 BUG_ON(!path
->slots
[0]);
825 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
826 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
827 found_key
.offset
!= block_group
->key
.objectid
) {
829 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, bytes
- 1,
830 EXTENT_DIRTY
| EXTENT_DELALLOC
|
831 EXTENT_DO_ACCOUNTING
, 0, 0, NULL
,
833 btrfs_release_path(root
, path
);
837 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
838 struct btrfs_free_space_header
);
839 btrfs_set_free_space_entries(leaf
, header
, entries
);
840 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
841 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
842 btrfs_mark_buffer_dirty(leaf
);
843 btrfs_release_path(root
, path
);
849 invalidate_inode_pages2_range(inode
->i_mapping
, 0, index
);
850 spin_lock(&block_group
->lock
);
851 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
852 spin_unlock(&block_group
->lock
);
853 BTRFS_I(inode
)->generation
= 0;
856 btrfs_update_inode(trans
, root
, inode
);
861 static inline unsigned long offset_to_bit(u64 bitmap_start
, u64 sectorsize
,
864 BUG_ON(offset
< bitmap_start
);
865 offset
-= bitmap_start
;
866 return (unsigned long)(div64_u64(offset
, sectorsize
));
869 static inline unsigned long bytes_to_bits(u64 bytes
, u64 sectorsize
)
871 return (unsigned long)(div64_u64(bytes
, sectorsize
));
874 static inline u64
offset_to_bitmap(struct btrfs_block_group_cache
*block_group
,
878 u64 bytes_per_bitmap
;
880 bytes_per_bitmap
= BITS_PER_BITMAP
* block_group
->sectorsize
;
881 bitmap_start
= offset
- block_group
->key
.objectid
;
882 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
883 bitmap_start
*= bytes_per_bitmap
;
884 bitmap_start
+= block_group
->key
.objectid
;
889 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
890 struct rb_node
*node
, int bitmap
)
892 struct rb_node
**p
= &root
->rb_node
;
893 struct rb_node
*parent
= NULL
;
894 struct btrfs_free_space
*info
;
898 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
900 if (offset
< info
->offset
) {
902 } else if (offset
> info
->offset
) {
906 * we could have a bitmap entry and an extent entry
907 * share the same offset. If this is the case, we want
908 * the extent entry to always be found first if we do a
909 * linear search through the tree, since we want to have
910 * the quickest allocation time, and allocating from an
911 * extent is faster than allocating from a bitmap. So
912 * if we're inserting a bitmap and we find an entry at
913 * this offset, we want to go right, or after this entry
914 * logically. If we are inserting an extent and we've
915 * found a bitmap, we want to go left, or before
919 WARN_ON(info
->bitmap
);
922 WARN_ON(!info
->bitmap
);
928 rb_link_node(node
, parent
, p
);
929 rb_insert_color(node
, root
);
935 * searches the tree for the given offset.
937 * fuzzy - If this is set, then we are trying to make an allocation, and we just
938 * want a section that has at least bytes size and comes at or after the given
941 static struct btrfs_free_space
*
942 tree_search_offset(struct btrfs_block_group_cache
*block_group
,
943 u64 offset
, int bitmap_only
, int fuzzy
)
945 struct rb_node
*n
= block_group
->free_space_offset
.rb_node
;
946 struct btrfs_free_space
*entry
, *prev
= NULL
;
948 /* find entry that is closest to the 'offset' */
955 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
958 if (offset
< entry
->offset
)
960 else if (offset
> entry
->offset
)
973 * bitmap entry and extent entry may share same offset,
974 * in that case, bitmap entry comes after extent entry.
979 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
980 if (entry
->offset
!= offset
)
983 WARN_ON(!entry
->bitmap
);
988 * if previous extent entry covers the offset,
989 * we should return it instead of the bitmap entry
991 n
= &entry
->offset_index
;
996 prev
= rb_entry(n
, struct btrfs_free_space
,
999 if (prev
->offset
+ prev
->bytes
> offset
)
1011 /* find last entry before the 'offset' */
1013 if (entry
->offset
> offset
) {
1014 n
= rb_prev(&entry
->offset_index
);
1016 entry
= rb_entry(n
, struct btrfs_free_space
,
1018 BUG_ON(entry
->offset
> offset
);
1027 if (entry
->bitmap
) {
1028 n
= &entry
->offset_index
;
1033 prev
= rb_entry(n
, struct btrfs_free_space
,
1035 if (!prev
->bitmap
) {
1036 if (prev
->offset
+ prev
->bytes
> offset
)
1041 if (entry
->offset
+ BITS_PER_BITMAP
*
1042 block_group
->sectorsize
> offset
)
1044 } else if (entry
->offset
+ entry
->bytes
> offset
)
1051 if (entry
->bitmap
) {
1052 if (entry
->offset
+ BITS_PER_BITMAP
*
1053 block_group
->sectorsize
> offset
)
1056 if (entry
->offset
+ entry
->bytes
> offset
)
1060 n
= rb_next(&entry
->offset_index
);
1063 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1069 __unlink_free_space(struct btrfs_block_group_cache
*block_group
,
1070 struct btrfs_free_space
*info
)
1072 rb_erase(&info
->offset_index
, &block_group
->free_space_offset
);
1073 block_group
->free_extents
--;
1076 static void unlink_free_space(struct btrfs_block_group_cache
*block_group
,
1077 struct btrfs_free_space
*info
)
1079 __unlink_free_space(block_group
, info
);
1080 block_group
->free_space
-= info
->bytes
;
1083 static int link_free_space(struct btrfs_block_group_cache
*block_group
,
1084 struct btrfs_free_space
*info
)
1088 BUG_ON(!info
->bitmap
&& !info
->bytes
);
1089 ret
= tree_insert_offset(&block_group
->free_space_offset
, info
->offset
,
1090 &info
->offset_index
, (info
->bitmap
!= NULL
));
1094 block_group
->free_space
+= info
->bytes
;
1095 block_group
->free_extents
++;
1099 static void recalculate_thresholds(struct btrfs_block_group_cache
*block_group
)
1104 u64 size
= block_group
->key
.offset
;
1107 * The goal is to keep the total amount of memory used per 1gb of space
1108 * at or below 32k, so we need to adjust how much memory we allow to be
1109 * used by extent based free space tracking
1111 if (size
< 1024 * 1024 * 1024)
1112 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1114 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
1115 div64_u64(size
, 1024 * 1024 * 1024);
1118 * we want to account for 1 more bitmap than what we have so we can make
1119 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1120 * we add more bitmaps.
1122 bitmap_bytes
= (block_group
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
1124 if (bitmap_bytes
>= max_bytes
) {
1125 block_group
->extents_thresh
= 0;
1130 * we want the extent entry threshold to always be at most 1/2 the maxw
1131 * bytes we can have, or whatever is less than that.
1133 extent_bytes
= max_bytes
- bitmap_bytes
;
1134 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
1136 block_group
->extents_thresh
=
1137 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
1140 static void bitmap_clear_bits(struct btrfs_block_group_cache
*block_group
,
1141 struct btrfs_free_space
*info
, u64 offset
,
1144 unsigned long start
, end
;
1147 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1148 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1149 BUG_ON(end
> BITS_PER_BITMAP
);
1151 for (i
= start
; i
< end
; i
++)
1152 clear_bit(i
, info
->bitmap
);
1154 info
->bytes
-= bytes
;
1155 block_group
->free_space
-= bytes
;
1158 static void bitmap_set_bits(struct btrfs_block_group_cache
*block_group
,
1159 struct btrfs_free_space
*info
, u64 offset
,
1162 unsigned long start
, end
;
1165 start
= offset_to_bit(info
->offset
, block_group
->sectorsize
, offset
);
1166 end
= start
+ bytes_to_bits(bytes
, block_group
->sectorsize
);
1167 BUG_ON(end
> BITS_PER_BITMAP
);
1169 for (i
= start
; i
< end
; i
++)
1170 set_bit(i
, info
->bitmap
);
1172 info
->bytes
+= bytes
;
1173 block_group
->free_space
+= bytes
;
1176 static int search_bitmap(struct btrfs_block_group_cache
*block_group
,
1177 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1180 unsigned long found_bits
= 0;
1181 unsigned long bits
, i
;
1182 unsigned long next_zero
;
1184 i
= offset_to_bit(bitmap_info
->offset
, block_group
->sectorsize
,
1185 max_t(u64
, *offset
, bitmap_info
->offset
));
1186 bits
= bytes_to_bits(*bytes
, block_group
->sectorsize
);
1188 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
1189 i
< BITS_PER_BITMAP
;
1190 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1191 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1192 BITS_PER_BITMAP
, i
);
1193 if ((next_zero
- i
) >= bits
) {
1194 found_bits
= next_zero
- i
;
1201 *offset
= (u64
)(i
* block_group
->sectorsize
) +
1202 bitmap_info
->offset
;
1203 *bytes
= (u64
)(found_bits
) * block_group
->sectorsize
;
1210 static struct btrfs_free_space
*find_free_space(struct btrfs_block_group_cache
1211 *block_group
, u64
*offset
,
1212 u64
*bytes
, int debug
)
1214 struct btrfs_free_space
*entry
;
1215 struct rb_node
*node
;
1218 if (!block_group
->free_space_offset
.rb_node
)
1221 entry
= tree_search_offset(block_group
,
1222 offset_to_bitmap(block_group
, *offset
),
1227 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1228 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1229 if (entry
->bytes
< *bytes
)
1232 if (entry
->bitmap
) {
1233 ret
= search_bitmap(block_group
, entry
, offset
, bytes
);
1239 *offset
= entry
->offset
;
1240 *bytes
= entry
->bytes
;
1247 static void add_new_bitmap(struct btrfs_block_group_cache
*block_group
,
1248 struct btrfs_free_space
*info
, u64 offset
)
1250 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
1251 int max_bitmaps
= (int)div64_u64(block_group
->key
.offset
+
1252 bytes_per_bg
- 1, bytes_per_bg
);
1253 BUG_ON(block_group
->total_bitmaps
>= max_bitmaps
);
1255 info
->offset
= offset_to_bitmap(block_group
, offset
);
1257 link_free_space(block_group
, info
);
1258 block_group
->total_bitmaps
++;
1260 recalculate_thresholds(block_group
);
1263 static void free_bitmap(struct btrfs_block_group_cache
*block_group
,
1264 struct btrfs_free_space
*bitmap_info
)
1266 unlink_free_space(block_group
, bitmap_info
);
1267 kfree(bitmap_info
->bitmap
);
1268 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1269 block_group
->total_bitmaps
--;
1270 recalculate_thresholds(block_group
);
1273 static noinline
int remove_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1274 struct btrfs_free_space
*bitmap_info
,
1275 u64
*offset
, u64
*bytes
)
1278 u64 search_start
, search_bytes
;
1282 end
= bitmap_info
->offset
+
1283 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
) - 1;
1286 * XXX - this can go away after a few releases.
1288 * since the only user of btrfs_remove_free_space is the tree logging
1289 * stuff, and the only way to test that is under crash conditions, we
1290 * want to have this debug stuff here just in case somethings not
1291 * working. Search the bitmap for the space we are trying to use to
1292 * make sure its actually there. If its not there then we need to stop
1293 * because something has gone wrong.
1295 search_start
= *offset
;
1296 search_bytes
= *bytes
;
1297 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1298 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1300 BUG_ON(ret
< 0 || search_start
!= *offset
);
1302 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
1303 bitmap_clear_bits(block_group
, bitmap_info
, *offset
,
1305 *bytes
-= end
- *offset
+ 1;
1307 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
1308 bitmap_clear_bits(block_group
, bitmap_info
, *offset
, *bytes
);
1313 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1314 if (!bitmap_info
->bytes
)
1315 free_bitmap(block_group
, bitmap_info
);
1318 * no entry after this bitmap, but we still have bytes to
1319 * remove, so something has gone wrong.
1324 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1328 * if the next entry isn't a bitmap we need to return to let the
1329 * extent stuff do its work.
1331 if (!bitmap_info
->bitmap
)
1335 * Ok the next item is a bitmap, but it may not actually hold
1336 * the information for the rest of this free space stuff, so
1337 * look for it, and if we don't find it return so we can try
1338 * everything over again.
1340 search_start
= *offset
;
1341 search_bytes
= *bytes
;
1342 ret
= search_bitmap(block_group
, bitmap_info
, &search_start
,
1344 if (ret
< 0 || search_start
!= *offset
)
1348 } else if (!bitmap_info
->bytes
)
1349 free_bitmap(block_group
, bitmap_info
);
1354 static int insert_into_bitmap(struct btrfs_block_group_cache
*block_group
,
1355 struct btrfs_free_space
*info
)
1357 struct btrfs_free_space
*bitmap_info
;
1359 u64 bytes
, offset
, end
;
1363 * If we are below the extents threshold then we can add this as an
1364 * extent, and don't have to deal with the bitmap
1366 if (block_group
->free_extents
< block_group
->extents_thresh
) {
1368 * If this block group has some small extents we don't want to
1369 * use up all of our free slots in the cache with them, we want
1370 * to reserve them to larger extents, however if we have plent
1371 * of cache left then go ahead an dadd them, no sense in adding
1372 * the overhead of a bitmap if we don't have to.
1374 if (info
->bytes
<= block_group
->sectorsize
* 4) {
1375 if (block_group
->free_extents
* 2 <=
1376 block_group
->extents_thresh
)
1384 * some block groups are so tiny they can't be enveloped by a bitmap, so
1385 * don't even bother to create a bitmap for this
1387 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
1388 block_group
->key
.offset
)
1391 bytes
= info
->bytes
;
1392 offset
= info
->offset
;
1395 bitmap_info
= tree_search_offset(block_group
,
1396 offset_to_bitmap(block_group
, offset
),
1403 end
= bitmap_info
->offset
+
1404 (u64
)(BITS_PER_BITMAP
* block_group
->sectorsize
);
1406 if (offset
>= bitmap_info
->offset
&& offset
+ bytes
> end
) {
1407 bitmap_set_bits(block_group
, bitmap_info
, offset
,
1409 bytes
-= end
- offset
;
1412 } else if (offset
>= bitmap_info
->offset
&& offset
+ bytes
<= end
) {
1413 bitmap_set_bits(block_group
, bitmap_info
, offset
, bytes
);
1426 if (info
&& info
->bitmap
) {
1427 add_new_bitmap(block_group
, info
, offset
);
1432 spin_unlock(&block_group
->tree_lock
);
1434 /* no pre-allocated info, allocate a new one */
1436 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
1439 spin_lock(&block_group
->tree_lock
);
1445 /* allocate the bitmap */
1446 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1447 spin_lock(&block_group
->tree_lock
);
1448 if (!info
->bitmap
) {
1458 kfree(info
->bitmap
);
1459 kmem_cache_free(btrfs_free_space_cachep
, info
);
1465 bool try_merge_free_space(struct btrfs_block_group_cache
*block_group
,
1466 struct btrfs_free_space
*info
, bool update_stat
)
1468 struct btrfs_free_space
*left_info
;
1469 struct btrfs_free_space
*right_info
;
1470 bool merged
= false;
1471 u64 offset
= info
->offset
;
1472 u64 bytes
= info
->bytes
;
1475 * first we want to see if there is free space adjacent to the range we
1476 * are adding, if there is remove that struct and add a new one to
1477 * cover the entire range
1479 right_info
= tree_search_offset(block_group
, offset
+ bytes
, 0, 0);
1480 if (right_info
&& rb_prev(&right_info
->offset_index
))
1481 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1482 struct btrfs_free_space
, offset_index
);
1484 left_info
= tree_search_offset(block_group
, offset
- 1, 0, 0);
1486 if (right_info
&& !right_info
->bitmap
) {
1488 unlink_free_space(block_group
, right_info
);
1490 __unlink_free_space(block_group
, right_info
);
1491 info
->bytes
+= right_info
->bytes
;
1492 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
1496 if (left_info
&& !left_info
->bitmap
&&
1497 left_info
->offset
+ left_info
->bytes
== offset
) {
1499 unlink_free_space(block_group
, left_info
);
1501 __unlink_free_space(block_group
, left_info
);
1502 info
->offset
= left_info
->offset
;
1503 info
->bytes
+= left_info
->bytes
;
1504 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
1511 int btrfs_add_free_space(struct btrfs_block_group_cache
*block_group
,
1512 u64 offset
, u64 bytes
)
1514 struct btrfs_free_space
*info
;
1517 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
1521 info
->offset
= offset
;
1522 info
->bytes
= bytes
;
1524 spin_lock(&block_group
->tree_lock
);
1526 if (try_merge_free_space(block_group
, info
, true))
1530 * There was no extent directly to the left or right of this new
1531 * extent then we know we're going to have to allocate a new extent, so
1532 * before we do that see if we need to drop this into a bitmap
1534 ret
= insert_into_bitmap(block_group
, info
);
1542 ret
= link_free_space(block_group
, info
);
1544 kmem_cache_free(btrfs_free_space_cachep
, info
);
1546 spin_unlock(&block_group
->tree_lock
);
1549 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1550 BUG_ON(ret
== -EEXIST
);
1556 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1557 u64 offset
, u64 bytes
)
1559 struct btrfs_free_space
*info
;
1560 struct btrfs_free_space
*next_info
= NULL
;
1563 spin_lock(&block_group
->tree_lock
);
1566 info
= tree_search_offset(block_group
, offset
, 0, 0);
1569 * oops didn't find an extent that matched the space we wanted
1570 * to remove, look for a bitmap instead
1572 info
= tree_search_offset(block_group
,
1573 offset_to_bitmap(block_group
, offset
),
1581 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1583 next_info
= rb_entry(rb_next(&info
->offset_index
),
1584 struct btrfs_free_space
,
1587 if (next_info
->bitmap
)
1588 end
= next_info
->offset
+ BITS_PER_BITMAP
*
1589 block_group
->sectorsize
- 1;
1591 end
= next_info
->offset
+ next_info
->bytes
;
1593 if (next_info
->bytes
< bytes
||
1594 next_info
->offset
> offset
|| offset
> end
) {
1595 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1596 " trying to use %llu\n",
1597 (unsigned long long)info
->offset
,
1598 (unsigned long long)info
->bytes
,
1599 (unsigned long long)bytes
);
1608 if (info
->bytes
== bytes
) {
1609 unlink_free_space(block_group
, info
);
1611 kfree(info
->bitmap
);
1612 block_group
->total_bitmaps
--;
1614 kmem_cache_free(btrfs_free_space_cachep
, info
);
1618 if (!info
->bitmap
&& info
->offset
== offset
) {
1619 unlink_free_space(block_group
, info
);
1620 info
->offset
+= bytes
;
1621 info
->bytes
-= bytes
;
1622 link_free_space(block_group
, info
);
1626 if (!info
->bitmap
&& info
->offset
<= offset
&&
1627 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1628 u64 old_start
= info
->offset
;
1630 * we're freeing space in the middle of the info,
1631 * this can happen during tree log replay
1633 * first unlink the old info and then
1634 * insert it again after the hole we're creating
1636 unlink_free_space(block_group
, info
);
1637 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1638 u64 old_end
= info
->offset
+ info
->bytes
;
1640 info
->offset
= offset
+ bytes
;
1641 info
->bytes
= old_end
- info
->offset
;
1642 ret
= link_free_space(block_group
, info
);
1647 /* the hole we're creating ends at the end
1648 * of the info struct, just free the info
1650 kmem_cache_free(btrfs_free_space_cachep
, info
);
1652 spin_unlock(&block_group
->tree_lock
);
1654 /* step two, insert a new info struct to cover
1655 * anything before the hole
1657 ret
= btrfs_add_free_space(block_group
, old_start
,
1658 offset
- old_start
);
1663 ret
= remove_from_bitmap(block_group
, info
, &offset
, &bytes
);
1668 spin_unlock(&block_group
->tree_lock
);
1673 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1676 struct btrfs_free_space
*info
;
1680 for (n
= rb_first(&block_group
->free_space_offset
); n
; n
= rb_next(n
)) {
1681 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1682 if (info
->bytes
>= bytes
)
1684 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1685 (unsigned long long)info
->offset
,
1686 (unsigned long long)info
->bytes
,
1687 (info
->bitmap
) ? "yes" : "no");
1689 printk(KERN_INFO
"block group has cluster?: %s\n",
1690 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1691 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1695 u64
btrfs_block_group_free_space(struct btrfs_block_group_cache
*block_group
)
1697 struct btrfs_free_space
*info
;
1701 for (n
= rb_first(&block_group
->free_space_offset
); n
;
1703 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1711 * for a given cluster, put all of its extents back into the free
1712 * space cache. If the block group passed doesn't match the block group
1713 * pointed to by the cluster, someone else raced in and freed the
1714 * cluster already. In that case, we just return without changing anything
1717 __btrfs_return_cluster_to_free_space(
1718 struct btrfs_block_group_cache
*block_group
,
1719 struct btrfs_free_cluster
*cluster
)
1721 struct btrfs_free_space
*entry
;
1722 struct rb_node
*node
;
1724 spin_lock(&cluster
->lock
);
1725 if (cluster
->block_group
!= block_group
)
1728 cluster
->block_group
= NULL
;
1729 cluster
->window_start
= 0;
1730 list_del_init(&cluster
->block_group_list
);
1732 node
= rb_first(&cluster
->root
);
1736 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1737 node
= rb_next(&entry
->offset_index
);
1738 rb_erase(&entry
->offset_index
, &cluster
->root
);
1740 bitmap
= (entry
->bitmap
!= NULL
);
1742 try_merge_free_space(block_group
, entry
, false);
1743 tree_insert_offset(&block_group
->free_space_offset
,
1744 entry
->offset
, &entry
->offset_index
, bitmap
);
1746 cluster
->root
= RB_ROOT
;
1749 spin_unlock(&cluster
->lock
);
1750 btrfs_put_block_group(block_group
);
1754 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
1756 struct btrfs_free_space
*info
;
1757 struct rb_node
*node
;
1758 struct btrfs_free_cluster
*cluster
;
1759 struct list_head
*head
;
1761 spin_lock(&block_group
->tree_lock
);
1762 while ((head
= block_group
->cluster_list
.next
) !=
1763 &block_group
->cluster_list
) {
1764 cluster
= list_entry(head
, struct btrfs_free_cluster
,
1767 WARN_ON(cluster
->block_group
!= block_group
);
1768 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1769 if (need_resched()) {
1770 spin_unlock(&block_group
->tree_lock
);
1772 spin_lock(&block_group
->tree_lock
);
1776 while ((node
= rb_last(&block_group
->free_space_offset
)) != NULL
) {
1777 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1778 unlink_free_space(block_group
, info
);
1780 kfree(info
->bitmap
);
1781 kmem_cache_free(btrfs_free_space_cachep
, info
);
1782 if (need_resched()) {
1783 spin_unlock(&block_group
->tree_lock
);
1785 spin_lock(&block_group
->tree_lock
);
1789 spin_unlock(&block_group
->tree_lock
);
1792 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
1793 u64 offset
, u64 bytes
, u64 empty_size
)
1795 struct btrfs_free_space
*entry
= NULL
;
1796 u64 bytes_search
= bytes
+ empty_size
;
1799 spin_lock(&block_group
->tree_lock
);
1800 entry
= find_free_space(block_group
, &offset
, &bytes_search
, 0);
1805 if (entry
->bitmap
) {
1806 bitmap_clear_bits(block_group
, entry
, offset
, bytes
);
1808 free_bitmap(block_group
, entry
);
1810 unlink_free_space(block_group
, entry
);
1811 entry
->offset
+= bytes
;
1812 entry
->bytes
-= bytes
;
1814 kmem_cache_free(btrfs_free_space_cachep
, entry
);
1816 link_free_space(block_group
, entry
);
1820 spin_unlock(&block_group
->tree_lock
);
1826 * given a cluster, put all of its extents back into the free space
1827 * cache. If a block group is passed, this function will only free
1828 * a cluster that belongs to the passed block group.
1830 * Otherwise, it'll get a reference on the block group pointed to by the
1831 * cluster and remove the cluster from it.
1833 int btrfs_return_cluster_to_free_space(
1834 struct btrfs_block_group_cache
*block_group
,
1835 struct btrfs_free_cluster
*cluster
)
1839 /* first, get a safe pointer to the block group */
1840 spin_lock(&cluster
->lock
);
1842 block_group
= cluster
->block_group
;
1844 spin_unlock(&cluster
->lock
);
1847 } else if (cluster
->block_group
!= block_group
) {
1848 /* someone else has already freed it don't redo their work */
1849 spin_unlock(&cluster
->lock
);
1852 atomic_inc(&block_group
->count
);
1853 spin_unlock(&cluster
->lock
);
1855 /* now return any extents the cluster had on it */
1856 spin_lock(&block_group
->tree_lock
);
1857 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
1858 spin_unlock(&block_group
->tree_lock
);
1860 /* finally drop our ref */
1861 btrfs_put_block_group(block_group
);
1865 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
1866 struct btrfs_free_cluster
*cluster
,
1867 struct btrfs_free_space
*entry
,
1868 u64 bytes
, u64 min_start
)
1871 u64 search_start
= cluster
->window_start
;
1872 u64 search_bytes
= bytes
;
1875 search_start
= min_start
;
1876 search_bytes
= bytes
;
1878 err
= search_bitmap(block_group
, entry
, &search_start
,
1884 bitmap_clear_bits(block_group
, entry
, ret
, bytes
);
1890 * given a cluster, try to allocate 'bytes' from it, returns 0
1891 * if it couldn't find anything suitably large, or a logical disk offset
1892 * if things worked out
1894 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
1895 struct btrfs_free_cluster
*cluster
, u64 bytes
,
1898 struct btrfs_free_space
*entry
= NULL
;
1899 struct rb_node
*node
;
1902 spin_lock(&cluster
->lock
);
1903 if (bytes
> cluster
->max_size
)
1906 if (cluster
->block_group
!= block_group
)
1909 node
= rb_first(&cluster
->root
);
1913 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1915 if (entry
->bytes
< bytes
||
1916 (!entry
->bitmap
&& entry
->offset
< min_start
)) {
1917 struct rb_node
*node
;
1919 node
= rb_next(&entry
->offset_index
);
1922 entry
= rb_entry(node
, struct btrfs_free_space
,
1927 if (entry
->bitmap
) {
1928 ret
= btrfs_alloc_from_bitmap(block_group
,
1929 cluster
, entry
, bytes
,
1932 struct rb_node
*node
;
1933 node
= rb_next(&entry
->offset_index
);
1936 entry
= rb_entry(node
, struct btrfs_free_space
,
1942 ret
= entry
->offset
;
1944 entry
->offset
+= bytes
;
1945 entry
->bytes
-= bytes
;
1948 if (entry
->bytes
== 0)
1949 rb_erase(&entry
->offset_index
, &cluster
->root
);
1953 spin_unlock(&cluster
->lock
);
1958 spin_lock(&block_group
->tree_lock
);
1960 block_group
->free_space
-= bytes
;
1961 if (entry
->bytes
== 0) {
1962 block_group
->free_extents
--;
1963 if (entry
->bitmap
) {
1964 kfree(entry
->bitmap
);
1965 block_group
->total_bitmaps
--;
1966 recalculate_thresholds(block_group
);
1968 kmem_cache_free(btrfs_free_space_cachep
, entry
);
1971 spin_unlock(&block_group
->tree_lock
);
1976 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
1977 struct btrfs_free_space
*entry
,
1978 struct btrfs_free_cluster
*cluster
,
1979 u64 offset
, u64 bytes
, u64 min_bytes
)
1981 unsigned long next_zero
;
1983 unsigned long search_bits
;
1984 unsigned long total_bits
;
1985 unsigned long found_bits
;
1986 unsigned long start
= 0;
1987 unsigned long total_found
= 0;
1991 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
1992 max_t(u64
, offset
, entry
->offset
));
1993 search_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
1994 total_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
1998 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
1999 i
< BITS_PER_BITMAP
;
2000 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
2001 next_zero
= find_next_zero_bit(entry
->bitmap
,
2002 BITS_PER_BITMAP
, i
);
2003 if (next_zero
- i
>= search_bits
) {
2004 found_bits
= next_zero
- i
;
2018 total_found
+= found_bits
;
2020 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
2021 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
2023 if (total_found
< total_bits
) {
2024 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, next_zero
);
2025 if (i
- start
> total_bits
* 2) {
2027 cluster
->max_size
= 0;
2033 cluster
->window_start
= start
* block_group
->sectorsize
+
2035 rb_erase(&entry
->offset_index
, &block_group
->free_space_offset
);
2036 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2037 &entry
->offset_index
, 1);
2044 * This searches the block group for just extents to fill the cluster with.
2046 static int setup_cluster_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2047 struct btrfs_free_cluster
*cluster
,
2048 u64 offset
, u64 bytes
, u64 min_bytes
)
2050 struct btrfs_free_space
*first
= NULL
;
2051 struct btrfs_free_space
*entry
= NULL
;
2052 struct btrfs_free_space
*prev
= NULL
;
2053 struct btrfs_free_space
*last
;
2054 struct rb_node
*node
;
2058 u64 max_gap
= 128 * 1024;
2060 entry
= tree_search_offset(block_group
, offset
, 0, 1);
2065 * We don't want bitmaps, so just move along until we find a normal
2068 while (entry
->bitmap
) {
2069 node
= rb_next(&entry
->offset_index
);
2072 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2075 window_start
= entry
->offset
;
2076 window_free
= entry
->bytes
;
2077 max_extent
= entry
->bytes
;
2082 while (window_free
<= min_bytes
) {
2083 node
= rb_next(&entry
->offset_index
);
2086 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2091 * we haven't filled the empty size and the window is
2092 * very large. reset and try again
2094 if (entry
->offset
- (prev
->offset
+ prev
->bytes
) > max_gap
||
2095 entry
->offset
- window_start
> (min_bytes
* 2)) {
2097 window_start
= entry
->offset
;
2098 window_free
= entry
->bytes
;
2100 max_extent
= entry
->bytes
;
2103 window_free
+= entry
->bytes
;
2104 if (entry
->bytes
> max_extent
)
2105 max_extent
= entry
->bytes
;
2110 cluster
->window_start
= first
->offset
;
2112 node
= &first
->offset_index
;
2115 * now we've found our entries, pull them out of the free space
2116 * cache and put them into the cluster rbtree
2121 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2122 node
= rb_next(&entry
->offset_index
);
2126 rb_erase(&entry
->offset_index
, &block_group
->free_space_offset
);
2127 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2128 &entry
->offset_index
, 0);
2130 } while (node
&& entry
!= last
);
2132 cluster
->max_size
= max_extent
;
2138 * This specifically looks for bitmaps that may work in the cluster, we assume
2139 * that we have already failed to find extents that will work.
2141 static int setup_cluster_bitmap(struct btrfs_block_group_cache
*block_group
,
2142 struct btrfs_free_cluster
*cluster
,
2143 u64 offset
, u64 bytes
, u64 min_bytes
)
2145 struct btrfs_free_space
*entry
;
2146 struct rb_node
*node
;
2149 if (block_group
->total_bitmaps
== 0)
2152 entry
= tree_search_offset(block_group
,
2153 offset_to_bitmap(block_group
, offset
),
2158 node
= &entry
->offset_index
;
2160 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2161 node
= rb_next(&entry
->offset_index
);
2164 if (entry
->bytes
< min_bytes
)
2166 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
, offset
,
2168 } while (ret
&& node
);
2174 * here we try to find a cluster of blocks in a block group. The goal
2175 * is to find at least bytes free and up to empty_size + bytes free.
2176 * We might not find them all in one contiguous area.
2178 * returns zero and sets up cluster if things worked out, otherwise
2179 * it returns -enospc
2181 int btrfs_find_space_cluster(struct btrfs_trans_handle
*trans
,
2182 struct btrfs_root
*root
,
2183 struct btrfs_block_group_cache
*block_group
,
2184 struct btrfs_free_cluster
*cluster
,
2185 u64 offset
, u64 bytes
, u64 empty_size
)
2190 /* for metadata, allow allocates with more holes */
2191 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
2192 min_bytes
= bytes
+ empty_size
;
2193 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2195 * we want to do larger allocations when we are
2196 * flushing out the delayed refs, it helps prevent
2197 * making more work as we go along.
2199 if (trans
->transaction
->delayed_refs
.flushing
)
2200 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 1);
2202 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 4);
2204 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
2206 spin_lock(&block_group
->tree_lock
);
2209 * If we know we don't have enough space to make a cluster don't even
2210 * bother doing all the work to try and find one.
2212 if (block_group
->free_space
< min_bytes
) {
2213 spin_unlock(&block_group
->tree_lock
);
2217 spin_lock(&cluster
->lock
);
2219 /* someone already found a cluster, hooray */
2220 if (cluster
->block_group
) {
2225 ret
= setup_cluster_no_bitmap(block_group
, cluster
, offset
, bytes
,
2228 ret
= setup_cluster_bitmap(block_group
, cluster
, offset
,
2232 atomic_inc(&block_group
->count
);
2233 list_add_tail(&cluster
->block_group_list
,
2234 &block_group
->cluster_list
);
2235 cluster
->block_group
= block_group
;
2238 spin_unlock(&cluster
->lock
);
2239 spin_unlock(&block_group
->tree_lock
);
2245 * simple code to zero out a cluster
2247 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
2249 spin_lock_init(&cluster
->lock
);
2250 spin_lock_init(&cluster
->refill_lock
);
2251 cluster
->root
= RB_ROOT
;
2252 cluster
->max_size
= 0;
2253 INIT_LIST_HEAD(&cluster
->block_group_list
);
2254 cluster
->block_group
= NULL
;
2257 int btrfs_trim_block_group(struct btrfs_block_group_cache
*block_group
,
2258 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
)
2260 struct btrfs_free_space
*entry
= NULL
;
2261 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2263 u64 actually_trimmed
;
2268 while (start
< end
) {
2269 spin_lock(&block_group
->tree_lock
);
2271 if (block_group
->free_space
< minlen
) {
2272 spin_unlock(&block_group
->tree_lock
);
2276 entry
= tree_search_offset(block_group
, start
, 0, 1);
2278 entry
= tree_search_offset(block_group
,
2279 offset_to_bitmap(block_group
,
2283 if (!entry
|| entry
->offset
>= end
) {
2284 spin_unlock(&block_group
->tree_lock
);
2288 if (entry
->bitmap
) {
2289 ret
= search_bitmap(block_group
, entry
, &start
, &bytes
);
2292 spin_unlock(&block_group
->tree_lock
);
2295 bytes
= min(bytes
, end
- start
);
2296 bitmap_clear_bits(block_group
, entry
,
2298 if (entry
->bytes
== 0)
2299 free_bitmap(block_group
, entry
);
2301 start
= entry
->offset
+ BITS_PER_BITMAP
*
2302 block_group
->sectorsize
;
2303 spin_unlock(&block_group
->tree_lock
);
2308 start
= entry
->offset
;
2309 bytes
= min(entry
->bytes
, end
- start
);
2310 unlink_free_space(block_group
, entry
);
2314 spin_unlock(&block_group
->tree_lock
);
2316 if (bytes
>= minlen
) {
2318 update_ret
= btrfs_update_reserved_bytes(block_group
,
2321 ret
= btrfs_error_discard_extent(fs_info
->extent_root
,
2326 btrfs_add_free_space(block_group
,
2329 btrfs_update_reserved_bytes(block_group
,
2334 *trimmed
+= actually_trimmed
;
2339 if (fatal_signal_pending(current
)) {