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 inode->i_mapping->flags &= ~__GFP_FS;
86 spin_lock(&block_group->lock);
87 if (!root->fs_info->closing) {
88 block_group->inode = igrab(inode);
89 block_group->iref = 1;
91 spin_unlock(&block_group->lock);
96 int create_free_space_inode(struct btrfs_root *root,
97 struct btrfs_trans_handle *trans,
98 struct btrfs_block_group_cache *block_group,
99 struct btrfs_path *path)
101 struct btrfs_key key;
102 struct btrfs_disk_key disk_key;
103 struct btrfs_free_space_header *header;
104 struct btrfs_inode_item *inode_item;
105 struct extent_buffer *leaf;
109 ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
113 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
117 leaf = path->nodes[0];
118 inode_item = btrfs_item_ptr(leaf, path->slots[0],
119 struct btrfs_inode_item);
120 btrfs_item_key(leaf, &disk_key, path->slots[0]);
121 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
122 sizeof(*inode_item));
123 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
124 btrfs_set_inode_size(leaf, inode_item, 0);
125 btrfs_set_inode_nbytes(leaf, inode_item, 0);
126 btrfs_set_inode_uid(leaf, inode_item, 0);
127 btrfs_set_inode_gid(leaf, inode_item, 0);
128 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
129 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
130 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
131 btrfs_set_inode_nlink(leaf, inode_item, 1);
132 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
133 btrfs_set_inode_block_group(leaf, inode_item,
134 block_group->key.objectid);
135 btrfs_mark_buffer_dirty(leaf);
136 btrfs_release_path(root, path);
138 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
139 key.offset = block_group->key.objectid;
142 ret = btrfs_insert_empty_item(trans, root, path, &key,
143 sizeof(struct btrfs_free_space_header));
145 btrfs_release_path(root, path);
148 leaf = path->nodes[0];
149 header = btrfs_item_ptr(leaf, path->slots[0],
150 struct btrfs_free_space_header);
151 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
152 btrfs_set_free_space_key(leaf, header, &disk_key);
153 btrfs_mark_buffer_dirty(leaf);
154 btrfs_release_path(root, path);
159 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
160 struct btrfs_trans_handle *trans,
161 struct btrfs_path *path,
167 trans->block_rsv = root->orphan_block_rsv;
168 ret = btrfs_block_rsv_check(trans, root,
169 root->orphan_block_rsv,
174 oldsize = i_size_read(inode);
175 btrfs_i_size_write(inode, 0);
176 truncate_pagecache(inode, oldsize, 0);
179 * We don't need an orphan item because truncating the free space cache
180 * will never be split across transactions.
182 ret = btrfs_truncate_inode_items(trans, root, inode,
183 0, BTRFS_EXTENT_DATA_KEY);
189 return btrfs_update_inode(trans, root, inode);
192 static int readahead_cache(struct inode *inode)
194 struct file_ra_state *ra;
195 unsigned long last_index;
197 ra = kzalloc(sizeof(*ra), GFP_NOFS);
201 file_ra_state_init(ra, inode->i_mapping);
202 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
204 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
211 int load_free_space_cache(struct btrfs_fs_info *fs_info,
212 struct btrfs_block_group_cache *block_group)
214 struct btrfs_root *root = fs_info->tree_root;
216 struct btrfs_free_space_header *header;
217 struct extent_buffer *leaf;
219 struct btrfs_path *path;
220 u32 *checksums = NULL, *crc;
221 char *disk_crcs = NULL;
222 struct btrfs_key key;
223 struct list_head bitmaps;
227 u32 cur_crc = ~(u32)0;
229 unsigned long first_page_offset;
234 * If we're unmounting then just return, since this does a search on the
235 * normal root and not the commit root and we could deadlock.
238 if (fs_info->closing)
242 * If this block group has been marked to be cleared for one reason or
243 * another then we can't trust the on disk cache, so just return.
245 spin_lock(&block_group->lock);
246 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
247 spin_unlock(&block_group->lock);
250 spin_unlock(&block_group->lock);
252 INIT_LIST_HEAD(&bitmaps);
254 path = btrfs_alloc_path();
258 inode = lookup_free_space_inode(root, block_group, path);
260 btrfs_free_path(path);
264 /* Nothing in the space cache, goodbye */
265 if (!i_size_read(inode)) {
266 btrfs_free_path(path);
270 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
271 key.offset = block_group->key.objectid;
274 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
276 btrfs_free_path(path);
280 leaf = path->nodes[0];
281 header = btrfs_item_ptr(leaf, path->slots[0],
282 struct btrfs_free_space_header);
283 num_entries = btrfs_free_space_entries(leaf, header);
284 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
285 generation = btrfs_free_space_generation(leaf, header);
286 btrfs_free_path(path);
288 if (BTRFS_I(inode)->generation != generation) {
289 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
290 " not match free space cache generation (%llu) for "
291 "block group %llu\n",
292 (unsigned long long)BTRFS_I(inode)->generation,
293 (unsigned long long)generation,
294 (unsigned long long)block_group->key.objectid);
301 /* Setup everything for doing checksumming */
302 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
303 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
306 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
307 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
311 ret = readahead_cache(inode);
318 struct btrfs_free_space_entry *entry;
319 struct btrfs_free_space *e;
321 unsigned long offset = 0;
322 unsigned long start_offset = 0;
325 if (!num_entries && !num_bitmaps)
329 start_offset = first_page_offset;
330 offset = start_offset;
333 page = grab_cache_page(inode->i_mapping, index);
339 if (!PageUptodate(page)) {
340 btrfs_readpage(NULL, page);
342 if (!PageUptodate(page)) {
344 page_cache_release(page);
345 printk(KERN_ERR "btrfs: error reading free "
346 "space cache: %llu\n",
348 block_group->key.objectid);
357 memcpy(disk_crcs, addr, first_page_offset);
358 gen = addr + (sizeof(u32) * num_checksums);
359 if (*gen != BTRFS_I(inode)->generation) {
360 printk(KERN_ERR "btrfs: space cache generation"
361 " (%llu) does not match inode (%llu) "
362 "for block group %llu\n",
363 (unsigned long long)*gen,
365 BTRFS_I(inode)->generation,
367 block_group->key.objectid);
370 page_cache_release(page);
373 crc = (u32 *)disk_crcs;
375 entry = addr + start_offset;
377 /* First lets check our crc before we do anything fun */
379 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
380 PAGE_CACHE_SIZE - start_offset);
381 btrfs_csum_final(cur_crc, (char *)&cur_crc);
382 if (cur_crc != *crc) {
383 printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
384 "block group %llu\n", index,
385 (unsigned long long)block_group->key.objectid);
388 page_cache_release(page);
398 e = kmem_cache_zalloc(btrfs_free_space_cachep,
403 page_cache_release(page);
407 e->offset = le64_to_cpu(entry->offset);
408 e->bytes = le64_to_cpu(entry->bytes);
411 kmem_cache_free(btrfs_free_space_cachep, e);
413 page_cache_release(page);
417 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
418 spin_lock(&block_group->tree_lock);
419 ret = link_free_space(block_group, e);
420 spin_unlock(&block_group->tree_lock);
423 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
427 btrfs_free_space_cachep, e);
429 page_cache_release(page);
432 spin_lock(&block_group->tree_lock);
433 ret = link_free_space(block_group, e);
434 block_group->total_bitmaps++;
435 recalculate_thresholds(block_group);
436 spin_unlock(&block_group->tree_lock);
437 list_add_tail(&e->list, &bitmaps);
441 offset += sizeof(struct btrfs_free_space_entry);
442 if (offset + sizeof(struct btrfs_free_space_entry) >=
449 * We read an entry out of this page, we need to move on to the
458 * We add the bitmaps at the end of the entries in order that
459 * the bitmap entries are added to the cache.
461 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
462 list_del_init(&e->list);
463 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
468 page_cache_release(page);
480 /* This cache is bogus, make sure it gets cleared */
481 spin_lock(&block_group->lock);
482 block_group->disk_cache_state = BTRFS_DC_CLEAR;
483 spin_unlock(&block_group->lock);
484 btrfs_remove_free_space_cache(block_group);
488 int btrfs_write_out_cache(struct btrfs_root *root,
489 struct btrfs_trans_handle *trans,
490 struct btrfs_block_group_cache *block_group,
491 struct btrfs_path *path)
493 struct btrfs_free_space_header *header;
494 struct extent_buffer *leaf;
496 struct rb_node *node;
497 struct list_head *pos, *n;
499 struct extent_state *cached_state = NULL;
500 struct list_head bitmap_list;
501 struct btrfs_key key;
503 u32 *crc, *checksums;
504 pgoff_t index = 0, last_index = 0;
505 unsigned long first_page_offset;
511 root = root->fs_info->tree_root;
513 INIT_LIST_HEAD(&bitmap_list);
515 spin_lock(&block_group->lock);
516 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
517 spin_unlock(&block_group->lock);
520 spin_unlock(&block_group->lock);
522 inode = lookup_free_space_inode(root, block_group, path);
526 if (!i_size_read(inode)) {
531 node = rb_first(&block_group->free_space_offset);
537 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
538 filemap_write_and_wait(inode->i_mapping);
539 btrfs_wait_ordered_range(inode, inode->i_size &
540 ~(root->sectorsize - 1), (u64)-1);
542 /* We need a checksum per page. */
543 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
544 crc = checksums = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
550 /* Since the first page has all of our checksums and our generation we
551 * need to calculate the offset into the page that we can start writing
554 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
557 * Lock all pages first so we can lock the extent safely.
559 * NOTE: Because we hold the ref the entire time we're going to write to
560 * the page find_get_page should never fail, so we don't do a check
561 * after find_get_page at this point. Just putting this here so people
562 * know and don't freak out.
564 while (index <= last_index) {
565 page = grab_cache_page(inode->i_mapping, index);
570 page = find_get_page(inode->i_mapping, i);
572 page_cache_release(page);
573 page_cache_release(page);
582 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
583 0, &cached_state, GFP_NOFS);
585 /* Write out the extent entries */
587 struct btrfs_free_space_entry *entry;
589 unsigned long offset = 0;
590 unsigned long start_offset = 0;
593 start_offset = first_page_offset;
594 offset = start_offset;
597 page = find_get_page(inode->i_mapping, index);
600 entry = addr + start_offset;
602 memset(addr, 0, PAGE_CACHE_SIZE);
604 struct btrfs_free_space *e;
606 e = rb_entry(node, struct btrfs_free_space, offset_index);
609 entry->offset = cpu_to_le64(e->offset);
610 entry->bytes = cpu_to_le64(e->bytes);
612 entry->type = BTRFS_FREE_SPACE_BITMAP;
613 list_add_tail(&e->list, &bitmap_list);
616 entry->type = BTRFS_FREE_SPACE_EXTENT;
618 node = rb_next(node);
621 offset += sizeof(struct btrfs_free_space_entry);
622 if (offset + sizeof(struct btrfs_free_space_entry) >=
628 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
629 PAGE_CACHE_SIZE - start_offset);
632 btrfs_csum_final(*crc, (char *)crc);
635 bytes += PAGE_CACHE_SIZE;
637 ClearPageChecked(page);
638 set_page_extent_mapped(page);
639 SetPageUptodate(page);
640 set_page_dirty(page);
643 * We need to release our reference we got for grab_cache_page,
644 * except for the first page which will hold our checksums, we
649 page_cache_release(page);
652 page_cache_release(page);
657 /* Write out the bitmaps */
658 list_for_each_safe(pos, n, &bitmap_list) {
660 struct btrfs_free_space *entry =
661 list_entry(pos, struct btrfs_free_space, list);
663 page = find_get_page(inode->i_mapping, index);
666 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
668 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
670 btrfs_csum_final(*crc, (char *)crc);
672 bytes += PAGE_CACHE_SIZE;
674 ClearPageChecked(page);
675 set_page_extent_mapped(page);
676 SetPageUptodate(page);
677 set_page_dirty(page);
679 page_cache_release(page);
680 page_cache_release(page);
681 list_del_init(&entry->list);
685 /* Zero out the rest of the pages just to make sure */
686 while (index <= last_index) {
689 page = find_get_page(inode->i_mapping, index);
692 memset(addr, 0, PAGE_CACHE_SIZE);
694 ClearPageChecked(page);
695 set_page_extent_mapped(page);
696 SetPageUptodate(page);
697 set_page_dirty(page);
699 page_cache_release(page);
700 page_cache_release(page);
701 bytes += PAGE_CACHE_SIZE;
705 btrfs_set_extent_delalloc(inode, 0, bytes - 1, &cached_state);
707 /* Write the checksums and trans id to the first page */
712 page = find_get_page(inode->i_mapping, 0);
715 memcpy(addr, checksums, sizeof(u32) * num_checksums);
716 gen = addr + (sizeof(u32) * num_checksums);
717 *gen = trans->transid;
719 ClearPageChecked(page);
720 set_page_extent_mapped(page);
721 SetPageUptodate(page);
722 set_page_dirty(page);
724 page_cache_release(page);
725 page_cache_release(page);
727 BTRFS_I(inode)->generation = trans->transid;
729 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
730 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
732 filemap_write_and_wait(inode->i_mapping);
734 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
735 key.offset = block_group->key.objectid;
738 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
741 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
742 EXTENT_DIRTY | EXTENT_DELALLOC |
743 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
746 leaf = path->nodes[0];
748 struct btrfs_key found_key;
749 BUG_ON(!path->slots[0]);
751 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
752 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
753 found_key.offset != block_group->key.objectid) {
755 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
756 EXTENT_DIRTY | EXTENT_DELALLOC |
757 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
759 btrfs_release_path(root, path);
763 header = btrfs_item_ptr(leaf, path->slots[0],
764 struct btrfs_free_space_header);
765 btrfs_set_free_space_entries(leaf, header, entries);
766 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
767 btrfs_set_free_space_generation(leaf, header, trans->transid);
768 btrfs_mark_buffer_dirty(leaf);
769 btrfs_release_path(root, path);
775 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
776 spin_lock(&block_group->lock);
777 block_group->disk_cache_state = BTRFS_DC_ERROR;
778 spin_unlock(&block_group->lock);
779 BTRFS_I(inode)->generation = 0;
782 btrfs_update_inode(trans, root, inode);
787 static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
790 BUG_ON(offset < bitmap_start);
791 offset -= bitmap_start;
792 return (unsigned long)(div64_u64(offset, sectorsize));
795 static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
797 return (unsigned long)(div64_u64(bytes, sectorsize));
800 static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
804 u64 bytes_per_bitmap;
806 bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
807 bitmap_start = offset - block_group->key.objectid;
808 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
809 bitmap_start *= bytes_per_bitmap;
810 bitmap_start += block_group->key.objectid;
815 static int tree_insert_offset(struct rb_root *root, u64 offset,
816 struct rb_node *node, int bitmap)
818 struct rb_node **p = &root->rb_node;
819 struct rb_node *parent = NULL;
820 struct btrfs_free_space *info;
824 info = rb_entry(parent, struct btrfs_free_space, offset_index);
826 if (offset < info->offset) {
828 } else if (offset > info->offset) {
832 * we could have a bitmap entry and an extent entry
833 * share the same offset. If this is the case, we want
834 * the extent entry to always be found first if we do a
835 * linear search through the tree, since we want to have
836 * the quickest allocation time, and allocating from an
837 * extent is faster than allocating from a bitmap. So
838 * if we're inserting a bitmap and we find an entry at
839 * this offset, we want to go right, or after this entry
840 * logically. If we are inserting an extent and we've
841 * found a bitmap, we want to go left, or before
845 WARN_ON(info->bitmap);
848 WARN_ON(!info->bitmap);
854 rb_link_node(node, parent, p);
855 rb_insert_color(node, root);
861 * searches the tree for the given offset.
863 * fuzzy - If this is set, then we are trying to make an allocation, and we just
864 * want a section that has at least bytes size and comes at or after the given
867 static struct btrfs_free_space *
868 tree_search_offset(struct btrfs_block_group_cache *block_group,
869 u64 offset, int bitmap_only, int fuzzy)
871 struct rb_node *n = block_group->free_space_offset.rb_node;
872 struct btrfs_free_space *entry, *prev = NULL;
874 /* find entry that is closest to the 'offset' */
881 entry = rb_entry(n, struct btrfs_free_space, offset_index);
884 if (offset < entry->offset)
886 else if (offset > entry->offset)
899 * bitmap entry and extent entry may share same offset,
900 * in that case, bitmap entry comes after extent entry.
905 entry = rb_entry(n, struct btrfs_free_space, offset_index);
906 if (entry->offset != offset)
909 WARN_ON(!entry->bitmap);
914 * if previous extent entry covers the offset,
915 * we should return it instead of the bitmap entry
917 n = &entry->offset_index;
922 prev = rb_entry(n, struct btrfs_free_space,
925 if (prev->offset + prev->bytes > offset)
937 /* find last entry before the 'offset' */
939 if (entry->offset > offset) {
940 n = rb_prev(&entry->offset_index);
942 entry = rb_entry(n, struct btrfs_free_space,
944 BUG_ON(entry->offset > offset);
954 n = &entry->offset_index;
959 prev = rb_entry(n, struct btrfs_free_space,
962 if (prev->offset + prev->bytes > offset)
967 if (entry->offset + BITS_PER_BITMAP *
968 block_group->sectorsize > offset)
970 } else if (entry->offset + entry->bytes > offset)
978 if (entry->offset + BITS_PER_BITMAP *
979 block_group->sectorsize > offset)
982 if (entry->offset + entry->bytes > offset)
986 n = rb_next(&entry->offset_index);
989 entry = rb_entry(n, struct btrfs_free_space, offset_index);
995 __unlink_free_space(struct btrfs_block_group_cache *block_group,
996 struct btrfs_free_space *info)
998 rb_erase(&info->offset_index, &block_group->free_space_offset);
999 block_group->free_extents--;
1002 static void unlink_free_space(struct btrfs_block_group_cache *block_group,
1003 struct btrfs_free_space *info)
1005 __unlink_free_space(block_group, info);
1006 block_group->free_space -= info->bytes;
1009 static int link_free_space(struct btrfs_block_group_cache *block_group,
1010 struct btrfs_free_space *info)
1014 BUG_ON(!info->bitmap && !info->bytes);
1015 ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
1016 &info->offset_index, (info->bitmap != NULL));
1020 block_group->free_space += info->bytes;
1021 block_group->free_extents++;
1025 static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
1030 u64 size = block_group->key.offset;
1033 * The goal is to keep the total amount of memory used per 1gb of space
1034 * at or below 32k, so we need to adjust how much memory we allow to be
1035 * used by extent based free space tracking
1037 if (size < 1024 * 1024 * 1024)
1038 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1040 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1041 div64_u64(size, 1024 * 1024 * 1024);
1044 * we want to account for 1 more bitmap than what we have so we can make
1045 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1046 * we add more bitmaps.
1048 bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
1050 if (bitmap_bytes >= max_bytes) {
1051 block_group->extents_thresh = 0;
1056 * we want the extent entry threshold to always be at most 1/2 the maxw
1057 * bytes we can have, or whatever is less than that.
1059 extent_bytes = max_bytes - bitmap_bytes;
1060 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1062 block_group->extents_thresh =
1063 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1066 static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
1067 struct btrfs_free_space *info, u64 offset,
1070 unsigned long start, end;
1073 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1074 end = start + bytes_to_bits(bytes, block_group->sectorsize);
1075 BUG_ON(end > BITS_PER_BITMAP);
1077 for (i = start; i < end; i++)
1078 clear_bit(i, info->bitmap);
1080 info->bytes -= bytes;
1081 block_group->free_space -= bytes;
1084 static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
1085 struct btrfs_free_space *info, u64 offset,
1088 unsigned long start, end;
1091 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1092 end = start + bytes_to_bits(bytes, block_group->sectorsize);
1093 BUG_ON(end > BITS_PER_BITMAP);
1095 for (i = start; i < end; i++)
1096 set_bit(i, info->bitmap);
1098 info->bytes += bytes;
1099 block_group->free_space += bytes;
1102 static int search_bitmap(struct btrfs_block_group_cache *block_group,
1103 struct btrfs_free_space *bitmap_info, u64 *offset,
1106 unsigned long found_bits = 0;
1107 unsigned long bits, i;
1108 unsigned long next_zero;
1110 i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
1111 max_t(u64, *offset, bitmap_info->offset));
1112 bits = bytes_to_bits(*bytes, block_group->sectorsize);
1114 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1115 i < BITS_PER_BITMAP;
1116 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1117 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1118 BITS_PER_BITMAP, i);
1119 if ((next_zero - i) >= bits) {
1120 found_bits = next_zero - i;
1127 *offset = (u64)(i * block_group->sectorsize) +
1128 bitmap_info->offset;
1129 *bytes = (u64)(found_bits) * block_group->sectorsize;
1136 static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
1137 *block_group, u64 *offset,
1138 u64 *bytes, int debug)
1140 struct btrfs_free_space *entry;
1141 struct rb_node *node;
1144 if (!block_group->free_space_offset.rb_node)
1147 entry = tree_search_offset(block_group,
1148 offset_to_bitmap(block_group, *offset),
1153 for (node = &entry->offset_index; node; node = rb_next(node)) {
1154 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1155 if (entry->bytes < *bytes)
1158 if (entry->bitmap) {
1159 ret = search_bitmap(block_group, entry, offset, bytes);
1165 *offset = entry->offset;
1166 *bytes = entry->bytes;
1173 static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
1174 struct btrfs_free_space *info, u64 offset)
1176 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1177 int max_bitmaps = (int)div64_u64(block_group->key.offset +
1178 bytes_per_bg - 1, bytes_per_bg);
1179 BUG_ON(block_group->total_bitmaps >= max_bitmaps);
1181 info->offset = offset_to_bitmap(block_group, offset);
1183 link_free_space(block_group, info);
1184 block_group->total_bitmaps++;
1186 recalculate_thresholds(block_group);
1189 static void free_bitmap(struct btrfs_block_group_cache *block_group,
1190 struct btrfs_free_space *bitmap_info)
1192 unlink_free_space(block_group, bitmap_info);
1193 kfree(bitmap_info->bitmap);
1194 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1195 block_group->total_bitmaps--;
1196 recalculate_thresholds(block_group);
1199 static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
1200 struct btrfs_free_space *bitmap_info,
1201 u64 *offset, u64 *bytes)
1204 u64 search_start, search_bytes;
1208 end = bitmap_info->offset +
1209 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
1212 * XXX - this can go away after a few releases.
1214 * since the only user of btrfs_remove_free_space is the tree logging
1215 * stuff, and the only way to test that is under crash conditions, we
1216 * want to have this debug stuff here just in case somethings not
1217 * working. Search the bitmap for the space we are trying to use to
1218 * make sure its actually there. If its not there then we need to stop
1219 * because something has gone wrong.
1221 search_start = *offset;
1222 search_bytes = *bytes;
1223 search_bytes = min(search_bytes, end - search_start + 1);
1224 ret = search_bitmap(block_group, bitmap_info, &search_start,
1226 BUG_ON(ret < 0 || search_start != *offset);
1228 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
1229 bitmap_clear_bits(block_group, bitmap_info, *offset,
1231 *bytes -= end - *offset + 1;
1233 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
1234 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
1239 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1240 if (!bitmap_info->bytes)
1241 free_bitmap(block_group, bitmap_info);
1244 * no entry after this bitmap, but we still have bytes to
1245 * remove, so something has gone wrong.
1250 bitmap_info = rb_entry(next, struct btrfs_free_space,
1254 * if the next entry isn't a bitmap we need to return to let the
1255 * extent stuff do its work.
1257 if (!bitmap_info->bitmap)
1261 * Ok the next item is a bitmap, but it may not actually hold
1262 * the information for the rest of this free space stuff, so
1263 * look for it, and if we don't find it return so we can try
1264 * everything over again.
1266 search_start = *offset;
1267 search_bytes = *bytes;
1268 ret = search_bitmap(block_group, bitmap_info, &search_start,
1270 if (ret < 0 || search_start != *offset)
1274 } else if (!bitmap_info->bytes)
1275 free_bitmap(block_group, bitmap_info);
1280 static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
1281 struct btrfs_free_space *info)
1283 struct btrfs_free_space *bitmap_info;
1285 u64 bytes, offset, end;
1289 * If we are below the extents threshold then we can add this as an
1290 * extent, and don't have to deal with the bitmap
1292 if (block_group->free_extents < block_group->extents_thresh) {
1294 * If this block group has some small extents we don't want to
1295 * use up all of our free slots in the cache with them, we want
1296 * to reserve them to larger extents, however if we have plent
1297 * of cache left then go ahead an dadd them, no sense in adding
1298 * the overhead of a bitmap if we don't have to.
1300 if (info->bytes <= block_group->sectorsize * 4) {
1301 if (block_group->free_extents * 2 <=
1302 block_group->extents_thresh)
1310 * some block groups are so tiny they can't be enveloped by a bitmap, so
1311 * don't even bother to create a bitmap for this
1313 if (BITS_PER_BITMAP * block_group->sectorsize >
1314 block_group->key.offset)
1317 bytes = info->bytes;
1318 offset = info->offset;
1321 bitmap_info = tree_search_offset(block_group,
1322 offset_to_bitmap(block_group, offset),
1329 end = bitmap_info->offset +
1330 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
1332 if (offset >= bitmap_info->offset && offset + bytes > end) {
1333 bitmap_set_bits(block_group, bitmap_info, offset,
1335 bytes -= end - offset;
1338 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
1339 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
1352 if (info && info->bitmap) {
1353 add_new_bitmap(block_group, info, offset);
1358 spin_unlock(&block_group->tree_lock);
1360 /* no pre-allocated info, allocate a new one */
1362 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1365 spin_lock(&block_group->tree_lock);
1371 /* allocate the bitmap */
1372 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1373 spin_lock(&block_group->tree_lock);
1374 if (!info->bitmap) {
1384 kfree(info->bitmap);
1385 kmem_cache_free(btrfs_free_space_cachep, info);
1391 bool try_merge_free_space(struct btrfs_block_group_cache *block_group,
1392 struct btrfs_free_space *info, bool update_stat)
1394 struct btrfs_free_space *left_info;
1395 struct btrfs_free_space *right_info;
1396 bool merged = false;
1397 u64 offset = info->offset;
1398 u64 bytes = info->bytes;
1401 * first we want to see if there is free space adjacent to the range we
1402 * are adding, if there is remove that struct and add a new one to
1403 * cover the entire range
1405 right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
1406 if (right_info && rb_prev(&right_info->offset_index))
1407 left_info = rb_entry(rb_prev(&right_info->offset_index),
1408 struct btrfs_free_space, offset_index);
1410 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
1412 if (right_info && !right_info->bitmap) {
1414 unlink_free_space(block_group, right_info);
1416 __unlink_free_space(block_group, right_info);
1417 info->bytes += right_info->bytes;
1418 kmem_cache_free(btrfs_free_space_cachep, right_info);
1422 if (left_info && !left_info->bitmap &&
1423 left_info->offset + left_info->bytes == offset) {
1425 unlink_free_space(block_group, left_info);
1427 __unlink_free_space(block_group, left_info);
1428 info->offset = left_info->offset;
1429 info->bytes += left_info->bytes;
1430 kmem_cache_free(btrfs_free_space_cachep, left_info);
1437 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1438 u64 offset, u64 bytes)
1440 struct btrfs_free_space *info;
1443 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
1447 info->offset = offset;
1448 info->bytes = bytes;
1450 spin_lock(&block_group->tree_lock);
1452 if (try_merge_free_space(block_group, info, true))
1456 * There was no extent directly to the left or right of this new
1457 * extent then we know we're going to have to allocate a new extent, so
1458 * before we do that see if we need to drop this into a bitmap
1460 ret = insert_into_bitmap(block_group, info);
1468 ret = link_free_space(block_group, info);
1470 kmem_cache_free(btrfs_free_space_cachep, info);
1472 spin_unlock(&block_group->tree_lock);
1475 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
1476 BUG_ON(ret == -EEXIST);
1482 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1483 u64 offset, u64 bytes)
1485 struct btrfs_free_space *info;
1486 struct btrfs_free_space *next_info = NULL;
1489 spin_lock(&block_group->tree_lock);
1492 info = tree_search_offset(block_group, offset, 0, 0);
1495 * oops didn't find an extent that matched the space we wanted
1496 * to remove, look for a bitmap instead
1498 info = tree_search_offset(block_group,
1499 offset_to_bitmap(block_group, offset),
1507 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1509 next_info = rb_entry(rb_next(&info->offset_index),
1510 struct btrfs_free_space,
1513 if (next_info->bitmap)
1514 end = next_info->offset + BITS_PER_BITMAP *
1515 block_group->sectorsize - 1;
1517 end = next_info->offset + next_info->bytes;
1519 if (next_info->bytes < bytes ||
1520 next_info->offset > offset || offset > end) {
1521 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1522 " trying to use %llu\n",
1523 (unsigned long long)info->offset,
1524 (unsigned long long)info->bytes,
1525 (unsigned long long)bytes);
1534 if (info->bytes == bytes) {
1535 unlink_free_space(block_group, info);
1537 kfree(info->bitmap);
1538 block_group->total_bitmaps--;
1540 kmem_cache_free(btrfs_free_space_cachep, info);
1544 if (!info->bitmap && info->offset == offset) {
1545 unlink_free_space(block_group, info);
1546 info->offset += bytes;
1547 info->bytes -= bytes;
1548 link_free_space(block_group, info);
1552 if (!info->bitmap && info->offset <= offset &&
1553 info->offset + info->bytes >= offset + bytes) {
1554 u64 old_start = info->offset;
1556 * we're freeing space in the middle of the info,
1557 * this can happen during tree log replay
1559 * first unlink the old info and then
1560 * insert it again after the hole we're creating
1562 unlink_free_space(block_group, info);
1563 if (offset + bytes < info->offset + info->bytes) {
1564 u64 old_end = info->offset + info->bytes;
1566 info->offset = offset + bytes;
1567 info->bytes = old_end - info->offset;
1568 ret = link_free_space(block_group, info);
1573 /* the hole we're creating ends at the end
1574 * of the info struct, just free the info
1576 kmem_cache_free(btrfs_free_space_cachep, info);
1578 spin_unlock(&block_group->tree_lock);
1580 /* step two, insert a new info struct to cover
1581 * anything before the hole
1583 ret = btrfs_add_free_space(block_group, old_start,
1584 offset - old_start);
1589 ret = remove_from_bitmap(block_group, info, &offset, &bytes);
1594 spin_unlock(&block_group->tree_lock);
1599 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1602 struct btrfs_free_space *info;
1606 for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
1607 info = rb_entry(n, struct btrfs_free_space, offset_index);
1608 if (info->bytes >= bytes)
1610 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
1611 (unsigned long long)info->offset,
1612 (unsigned long long)info->bytes,
1613 (info->bitmap) ? "yes" : "no");
1615 printk(KERN_INFO "block group has cluster?: %s\n",
1616 list_empty(&block_group->cluster_list) ? "no" : "yes");
1617 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1621 u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
1623 struct btrfs_free_space *info;
1627 for (n = rb_first(&block_group->free_space_offset); n;
1629 info = rb_entry(n, struct btrfs_free_space, offset_index);
1637 * for a given cluster, put all of its extents back into the free
1638 * space cache. If the block group passed doesn't match the block group
1639 * pointed to by the cluster, someone else raced in and freed the
1640 * cluster already. In that case, we just return without changing anything
1643 __btrfs_return_cluster_to_free_space(
1644 struct btrfs_block_group_cache *block_group,
1645 struct btrfs_free_cluster *cluster)
1647 struct btrfs_free_space *entry;
1648 struct rb_node *node;
1650 spin_lock(&cluster->lock);
1651 if (cluster->block_group != block_group)
1654 cluster->block_group = NULL;
1655 cluster->window_start = 0;
1656 list_del_init(&cluster->block_group_list);
1658 node = rb_first(&cluster->root);
1662 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1663 node = rb_next(&entry->offset_index);
1664 rb_erase(&entry->offset_index, &cluster->root);
1666 bitmap = (entry->bitmap != NULL);
1668 try_merge_free_space(block_group, entry, false);
1669 tree_insert_offset(&block_group->free_space_offset,
1670 entry->offset, &entry->offset_index, bitmap);
1672 cluster->root = RB_ROOT;
1675 spin_unlock(&cluster->lock);
1676 btrfs_put_block_group(block_group);
1680 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1682 struct btrfs_free_space *info;
1683 struct rb_node *node;
1684 struct btrfs_free_cluster *cluster;
1685 struct list_head *head;
1687 spin_lock(&block_group->tree_lock);
1688 while ((head = block_group->cluster_list.next) !=
1689 &block_group->cluster_list) {
1690 cluster = list_entry(head, struct btrfs_free_cluster,
1693 WARN_ON(cluster->block_group != block_group);
1694 __btrfs_return_cluster_to_free_space(block_group, cluster);
1695 if (need_resched()) {
1696 spin_unlock(&block_group->tree_lock);
1698 spin_lock(&block_group->tree_lock);
1702 while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
1703 info = rb_entry(node, struct btrfs_free_space, offset_index);
1704 unlink_free_space(block_group, info);
1706 kfree(info->bitmap);
1707 kmem_cache_free(btrfs_free_space_cachep, info);
1708 if (need_resched()) {
1709 spin_unlock(&block_group->tree_lock);
1711 spin_lock(&block_group->tree_lock);
1715 spin_unlock(&block_group->tree_lock);
1718 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1719 u64 offset, u64 bytes, u64 empty_size)
1721 struct btrfs_free_space *entry = NULL;
1722 u64 bytes_search = bytes + empty_size;
1725 spin_lock(&block_group->tree_lock);
1726 entry = find_free_space(block_group, &offset, &bytes_search, 0);
1731 if (entry->bitmap) {
1732 bitmap_clear_bits(block_group, entry, offset, bytes);
1734 free_bitmap(block_group, entry);
1736 unlink_free_space(block_group, entry);
1737 entry->offset += bytes;
1738 entry->bytes -= bytes;
1740 kmem_cache_free(btrfs_free_space_cachep, entry);
1742 link_free_space(block_group, entry);
1746 spin_unlock(&block_group->tree_lock);
1752 * given a cluster, put all of its extents back into the free space
1753 * cache. If a block group is passed, this function will only free
1754 * a cluster that belongs to the passed block group.
1756 * Otherwise, it'll get a reference on the block group pointed to by the
1757 * cluster and remove the cluster from it.
1759 int btrfs_return_cluster_to_free_space(
1760 struct btrfs_block_group_cache *block_group,
1761 struct btrfs_free_cluster *cluster)
1765 /* first, get a safe pointer to the block group */
1766 spin_lock(&cluster->lock);
1768 block_group = cluster->block_group;
1770 spin_unlock(&cluster->lock);
1773 } else if (cluster->block_group != block_group) {
1774 /* someone else has already freed it don't redo their work */
1775 spin_unlock(&cluster->lock);
1778 atomic_inc(&block_group->count);
1779 spin_unlock(&cluster->lock);
1781 /* now return any extents the cluster had on it */
1782 spin_lock(&block_group->tree_lock);
1783 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1784 spin_unlock(&block_group->tree_lock);
1786 /* finally drop our ref */
1787 btrfs_put_block_group(block_group);
1791 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1792 struct btrfs_free_cluster *cluster,
1793 struct btrfs_free_space *entry,
1794 u64 bytes, u64 min_start)
1797 u64 search_start = cluster->window_start;
1798 u64 search_bytes = bytes;
1801 search_start = min_start;
1802 search_bytes = bytes;
1804 err = search_bitmap(block_group, entry, &search_start,
1810 bitmap_clear_bits(block_group, entry, ret, bytes);
1816 * given a cluster, try to allocate 'bytes' from it, returns 0
1817 * if it couldn't find anything suitably large, or a logical disk offset
1818 * if things worked out
1820 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1821 struct btrfs_free_cluster *cluster, u64 bytes,
1824 struct btrfs_free_space *entry = NULL;
1825 struct rb_node *node;
1828 spin_lock(&cluster->lock);
1829 if (bytes > cluster->max_size)
1832 if (cluster->block_group != block_group)
1835 node = rb_first(&cluster->root);
1839 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1841 if (entry->bytes < bytes ||
1842 (!entry->bitmap && entry->offset < min_start)) {
1843 struct rb_node *node;
1845 node = rb_next(&entry->offset_index);
1848 entry = rb_entry(node, struct btrfs_free_space,
1853 if (entry->bitmap) {
1854 ret = btrfs_alloc_from_bitmap(block_group,
1855 cluster, entry, bytes,
1858 struct rb_node *node;
1859 node = rb_next(&entry->offset_index);
1862 entry = rb_entry(node, struct btrfs_free_space,
1868 ret = entry->offset;
1870 entry->offset += bytes;
1871 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 if (entry->bitmap) {
1890 kfree(entry->bitmap);
1891 block_group->total_bitmaps--;
1892 recalculate_thresholds(block_group);
1894 kmem_cache_free(btrfs_free_space_cachep, entry);
1897 spin_unlock(&block_group->tree_lock);
1902 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1903 struct btrfs_free_space *entry,
1904 struct btrfs_free_cluster *cluster,
1905 u64 offset, u64 bytes, u64 min_bytes)
1907 unsigned long next_zero;
1909 unsigned long search_bits;
1910 unsigned long total_bits;
1911 unsigned long found_bits;
1912 unsigned long start = 0;
1913 unsigned long total_found = 0;
1917 i = offset_to_bit(entry->offset, block_group->sectorsize,
1918 max_t(u64, offset, entry->offset));
1919 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
1920 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
1924 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
1925 i < BITS_PER_BITMAP;
1926 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
1927 next_zero = find_next_zero_bit(entry->bitmap,
1928 BITS_PER_BITMAP, i);
1929 if (next_zero - i >= search_bits) {
1930 found_bits = next_zero - i;
1944 total_found += found_bits;
1946 if (cluster->max_size < found_bits * block_group->sectorsize)
1947 cluster->max_size = found_bits * block_group->sectorsize;
1949 if (total_found < total_bits) {
1950 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
1951 if (i - start > total_bits * 2) {
1953 cluster->max_size = 0;
1959 cluster->window_start = start * block_group->sectorsize +
1961 rb_erase(&entry->offset_index, &block_group->free_space_offset);
1962 ret = tree_insert_offset(&cluster->root, entry->offset,
1963 &entry->offset_index, 1);
1970 * This searches the block group for just extents to fill the cluster with.
1972 static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
1973 struct btrfs_free_cluster *cluster,
1974 u64 offset, u64 bytes, u64 min_bytes)
1976 struct btrfs_free_space *first = NULL;
1977 struct btrfs_free_space *entry = NULL;
1978 struct btrfs_free_space *prev = NULL;
1979 struct btrfs_free_space *last;
1980 struct rb_node *node;
1984 u64 max_gap = 128 * 1024;
1986 entry = tree_search_offset(block_group, offset, 0, 1);
1991 * We don't want bitmaps, so just move along until we find a normal
1994 while (entry->bitmap) {
1995 node = rb_next(&entry->offset_index);
1998 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2001 window_start = entry->offset;
2002 window_free = entry->bytes;
2003 max_extent = entry->bytes;
2008 while (window_free <= min_bytes) {
2009 node = rb_next(&entry->offset_index);
2012 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2017 * we haven't filled the empty size and the window is
2018 * very large. reset and try again
2020 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2021 entry->offset - window_start > (min_bytes * 2)) {
2023 window_start = entry->offset;
2024 window_free = entry->bytes;
2026 max_extent = entry->bytes;
2029 window_free += entry->bytes;
2030 if (entry->bytes > max_extent)
2031 max_extent = entry->bytes;
2036 cluster->window_start = first->offset;
2038 node = &first->offset_index;
2041 * now we've found our entries, pull them out of the free space
2042 * cache and put them into the cluster rbtree
2047 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2048 node = rb_next(&entry->offset_index);
2052 rb_erase(&entry->offset_index, &block_group->free_space_offset);
2053 ret = tree_insert_offset(&cluster->root, entry->offset,
2054 &entry->offset_index, 0);
2056 } while (node && entry != last);
2058 cluster->max_size = max_extent;
2064 * This specifically looks for bitmaps that may work in the cluster, we assume
2065 * that we have already failed to find extents that will work.
2067 static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2068 struct btrfs_free_cluster *cluster,
2069 u64 offset, u64 bytes, u64 min_bytes)
2071 struct btrfs_free_space *entry;
2072 struct rb_node *node;
2075 if (block_group->total_bitmaps == 0)
2078 entry = tree_search_offset(block_group,
2079 offset_to_bitmap(block_group, offset),
2084 node = &entry->offset_index;
2086 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2087 node = rb_next(&entry->offset_index);
2090 if (entry->bytes < min_bytes)
2092 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2094 } while (ret && node);
2100 * here we try to find a cluster of blocks in a block group. The goal
2101 * is to find at least bytes free and up to empty_size + bytes free.
2102 * We might not find them all in one contiguous area.
2104 * returns zero and sets up cluster if things worked out, otherwise
2105 * it returns -enospc
2107 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *root,
2109 struct btrfs_block_group_cache *block_group,
2110 struct btrfs_free_cluster *cluster,
2111 u64 offset, u64 bytes, u64 empty_size)
2116 /* for metadata, allow allocates with more holes */
2117 if (btrfs_test_opt(root, SSD_SPREAD)) {
2118 min_bytes = bytes + empty_size;
2119 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
2121 * we want to do larger allocations when we are
2122 * flushing out the delayed refs, it helps prevent
2123 * making more work as we go along.
2125 if (trans->transaction->delayed_refs.flushing)
2126 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2128 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2130 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2132 spin_lock(&block_group->tree_lock);
2135 * If we know we don't have enough space to make a cluster don't even
2136 * bother doing all the work to try and find one.
2138 if (block_group->free_space < min_bytes) {
2139 spin_unlock(&block_group->tree_lock);
2143 spin_lock(&cluster->lock);
2145 /* someone already found a cluster, hooray */
2146 if (cluster->block_group) {
2151 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2154 ret = setup_cluster_bitmap(block_group, cluster, offset,
2158 atomic_inc(&block_group->count);
2159 list_add_tail(&cluster->block_group_list,
2160 &block_group->cluster_list);
2161 cluster->block_group = block_group;
2164 spin_unlock(&cluster->lock);
2165 spin_unlock(&block_group->tree_lock);
2171 * simple code to zero out a cluster
2173 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2175 spin_lock_init(&cluster->lock);
2176 spin_lock_init(&cluster->refill_lock);
2177 cluster->root = RB_ROOT;
2178 cluster->max_size = 0;
2179 INIT_LIST_HEAD(&cluster->block_group_list);
2180 cluster->block_group = NULL;
2183 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2184 u64 *trimmed, u64 start, u64 end, u64 minlen)
2186 struct btrfs_free_space *entry = NULL;
2187 struct btrfs_fs_info *fs_info = block_group->fs_info;
2189 u64 actually_trimmed;
2194 while (start < end) {
2195 spin_lock(&block_group->tree_lock);
2197 if (block_group->free_space < minlen) {
2198 spin_unlock(&block_group->tree_lock);
2202 entry = tree_search_offset(block_group, start, 0, 1);
2204 entry = tree_search_offset(block_group,
2205 offset_to_bitmap(block_group,
2209 if (!entry || entry->offset >= end) {
2210 spin_unlock(&block_group->tree_lock);
2214 if (entry->bitmap) {
2215 ret = search_bitmap(block_group, entry, &start, &bytes);
2218 spin_unlock(&block_group->tree_lock);
2221 bytes = min(bytes, end - start);
2222 bitmap_clear_bits(block_group, entry,
2224 if (entry->bytes == 0)
2225 free_bitmap(block_group, entry);
2227 start = entry->offset + BITS_PER_BITMAP *
2228 block_group->sectorsize;
2229 spin_unlock(&block_group->tree_lock);
2234 start = entry->offset;
2235 bytes = min(entry->bytes, end - start);
2236 unlink_free_space(block_group, entry);
2240 spin_unlock(&block_group->tree_lock);
2242 if (bytes >= minlen) {
2244 update_ret = btrfs_update_reserved_bytes(block_group,
2247 ret = btrfs_error_discard_extent(fs_info->extent_root,
2252 btrfs_add_free_space(block_group,
2255 btrfs_update_reserved_bytes(block_group,
2260 *trimmed += actually_trimmed;
2265 if (fatal_signal_pending(current)) {