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"
28 #include "inode-map.h"
30 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
31 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
33 static int link_free_space(struct btrfs_free_space_ctl *ctl,
34 struct btrfs_free_space *info);
36 static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
37 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 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
52 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
56 btrfs_release_path(path);
57 return ERR_PTR(-ENOENT);
60 leaf = path->nodes[0];
61 header = btrfs_item_ptr(leaf, path->slots[0],
62 struct btrfs_free_space_header);
63 btrfs_free_space_key(leaf, header, &disk_key);
64 btrfs_disk_key_to_cpu(&location, &disk_key);
65 btrfs_release_path(path);
67 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
69 return ERR_PTR(-ENOENT);
72 if (is_bad_inode(inode)) {
74 return ERR_PTR(-ENOENT);
77 inode->i_mapping->flags &= ~__GFP_FS;
82 struct inode *lookup_free_space_inode(struct btrfs_root *root,
83 struct btrfs_block_group_cache
84 *block_group, struct btrfs_path *path)
86 struct inode *inode = NULL;
88 spin_lock(&block_group->lock);
89 if (block_group->inode)
90 inode = igrab(block_group->inode);
91 spin_unlock(&block_group->lock);
95 inode = __lookup_free_space_inode(root, path,
96 block_group->key.objectid);
100 spin_lock(&block_group->lock);
101 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) {
102 printk(KERN_INFO "Old style space inode found, converting.\n");
103 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NODATASUM;
104 block_group->disk_cache_state = BTRFS_DC_CLEAR;
107 if (!btrfs_fs_closing(root->fs_info)) {
108 block_group->inode = igrab(inode);
109 block_group->iref = 1;
111 spin_unlock(&block_group->lock);
116 int __create_free_space_inode(struct btrfs_root *root,
117 struct btrfs_trans_handle *trans,
118 struct btrfs_path *path, u64 ino, u64 offset)
120 struct btrfs_key key;
121 struct btrfs_disk_key disk_key;
122 struct btrfs_free_space_header *header;
123 struct btrfs_inode_item *inode_item;
124 struct extent_buffer *leaf;
127 ret = btrfs_insert_empty_inode(trans, root, path, ino);
131 leaf = path->nodes[0];
132 inode_item = btrfs_item_ptr(leaf, path->slots[0],
133 struct btrfs_inode_item);
134 btrfs_item_key(leaf, &disk_key, path->slots[0]);
135 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
136 sizeof(*inode_item));
137 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
138 btrfs_set_inode_size(leaf, inode_item, 0);
139 btrfs_set_inode_nbytes(leaf, inode_item, 0);
140 btrfs_set_inode_uid(leaf, inode_item, 0);
141 btrfs_set_inode_gid(leaf, inode_item, 0);
142 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
143 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
144 BTRFS_INODE_PREALLOC);
145 btrfs_set_inode_nlink(leaf, inode_item, 1);
146 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
147 btrfs_set_inode_block_group(leaf, inode_item, offset);
148 btrfs_mark_buffer_dirty(leaf);
149 btrfs_release_path(path);
151 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
155 ret = btrfs_insert_empty_item(trans, root, path, &key,
156 sizeof(struct btrfs_free_space_header));
158 btrfs_release_path(path);
161 leaf = path->nodes[0];
162 header = btrfs_item_ptr(leaf, path->slots[0],
163 struct btrfs_free_space_header);
164 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
165 btrfs_set_free_space_key(leaf, header, &disk_key);
166 btrfs_mark_buffer_dirty(leaf);
167 btrfs_release_path(path);
172 int create_free_space_inode(struct btrfs_root *root,
173 struct btrfs_trans_handle *trans,
174 struct btrfs_block_group_cache *block_group,
175 struct btrfs_path *path)
180 ret = btrfs_find_free_objectid(root, &ino);
184 return __create_free_space_inode(root, trans, path, ino,
185 block_group->key.objectid);
188 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
189 struct btrfs_trans_handle *trans,
190 struct btrfs_path *path,
196 trans->block_rsv = root->orphan_block_rsv;
197 ret = btrfs_block_rsv_check(trans, root,
198 root->orphan_block_rsv,
203 oldsize = i_size_read(inode);
204 btrfs_i_size_write(inode, 0);
205 truncate_pagecache(inode, oldsize, 0);
208 * We don't need an orphan item because truncating the free space cache
209 * will never be split across transactions.
211 ret = btrfs_truncate_inode_items(trans, root, inode,
212 0, BTRFS_EXTENT_DATA_KEY);
218 ret = btrfs_update_inode(trans, root, inode);
222 static int readahead_cache(struct inode *inode)
224 struct file_ra_state *ra;
225 unsigned long last_index;
227 ra = kzalloc(sizeof(*ra), GFP_NOFS);
231 file_ra_state_init(ra, inode->i_mapping);
232 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
234 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
241 int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
242 struct btrfs_free_space_ctl *ctl,
243 struct btrfs_path *path, u64 offset)
245 struct btrfs_free_space_header *header;
246 struct extent_buffer *leaf;
248 struct btrfs_key key;
249 struct list_head bitmaps;
256 INIT_LIST_HEAD(&bitmaps);
258 /* Nothing in the space cache, goodbye */
259 if (!i_size_read(inode))
262 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
266 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
270 btrfs_release_path(path);
277 leaf = path->nodes[0];
278 header = btrfs_item_ptr(leaf, path->slots[0],
279 struct btrfs_free_space_header);
280 num_entries = btrfs_free_space_entries(leaf, header);
281 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
282 generation = btrfs_free_space_generation(leaf, header);
283 btrfs_release_path(path);
285 if (BTRFS_I(inode)->generation != generation) {
286 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
287 " not match free space cache generation (%llu)\n",
288 (unsigned long long)BTRFS_I(inode)->generation,
289 (unsigned long long)generation);
296 ret = readahead_cache(inode);
301 struct btrfs_free_space_entry *entry;
302 struct btrfs_free_space *e;
304 unsigned long offset = 0;
307 if (!num_entries && !num_bitmaps)
310 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
314 if (!PageUptodate(page)) {
315 btrfs_readpage(NULL, page);
317 if (!PageUptodate(page)) {
319 page_cache_release(page);
320 printk(KERN_ERR "btrfs: error reading free "
331 * We put a bogus crc in the front of the first page in
332 * case old kernels try to mount a fs with the new
333 * format to make sure they discard the cache.
336 offset += sizeof(u64);
339 if (*gen != BTRFS_I(inode)->generation) {
340 printk(KERN_ERR "btrfs: space cache generation"
341 " (%llu) does not match inode (%llu)\n",
342 (unsigned long long)*gen,
344 BTRFS_I(inode)->generation);
347 page_cache_release(page);
351 offset += sizeof(u64);
360 e = kmem_cache_zalloc(btrfs_free_space_cachep,
365 page_cache_release(page);
369 e->offset = le64_to_cpu(entry->offset);
370 e->bytes = le64_to_cpu(entry->bytes);
373 kmem_cache_free(btrfs_free_space_cachep, e);
375 page_cache_release(page);
379 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
380 spin_lock(&ctl->tree_lock);
381 ret = link_free_space(ctl, e);
382 spin_unlock(&ctl->tree_lock);
384 printk(KERN_ERR "Duplicate entries in "
385 "free space cache, dumping\n");
388 page_cache_release(page);
392 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
396 btrfs_free_space_cachep, e);
398 page_cache_release(page);
401 spin_lock(&ctl->tree_lock);
402 ret = link_free_space(ctl, e);
403 ctl->total_bitmaps++;
404 ctl->op->recalc_thresholds(ctl);
405 spin_unlock(&ctl->tree_lock);
407 printk(KERN_ERR "Duplicate entries in "
408 "free space cache, dumping\n");
411 page_cache_release(page);
414 list_add_tail(&e->list, &bitmaps);
418 offset += sizeof(struct btrfs_free_space_entry);
419 if (offset + sizeof(struct btrfs_free_space_entry) >=
426 * We read an entry out of this page, we need to move on to the
435 * We add the bitmaps at the end of the entries in order that
436 * the bitmap entries are added to the cache.
438 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
439 list_del_init(&e->list);
440 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
445 page_cache_release(page);
453 __btrfs_remove_free_space_cache(ctl);
457 int load_free_space_cache(struct btrfs_fs_info *fs_info,
458 struct btrfs_block_group_cache *block_group)
460 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
461 struct btrfs_root *root = fs_info->tree_root;
463 struct btrfs_path *path;
466 u64 used = btrfs_block_group_used(&block_group->item);
469 * If we're unmounting then just return, since this does a search on the
470 * normal root and not the commit root and we could deadlock.
472 if (btrfs_fs_closing(fs_info))
476 * If this block group has been marked to be cleared for one reason or
477 * another then we can't trust the on disk cache, so just return.
479 spin_lock(&block_group->lock);
480 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
481 spin_unlock(&block_group->lock);
484 spin_unlock(&block_group->lock);
486 path = btrfs_alloc_path();
490 inode = lookup_free_space_inode(root, block_group, path);
492 btrfs_free_path(path);
496 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
497 path, block_group->key.objectid);
498 btrfs_free_path(path);
502 spin_lock(&ctl->tree_lock);
503 matched = (ctl->free_space == (block_group->key.offset - used -
504 block_group->bytes_super));
505 spin_unlock(&ctl->tree_lock);
508 __btrfs_remove_free_space_cache(ctl);
509 printk(KERN_ERR "block group %llu has an wrong amount of free "
510 "space\n", block_group->key.objectid);
515 /* This cache is bogus, make sure it gets cleared */
516 spin_lock(&block_group->lock);
517 block_group->disk_cache_state = BTRFS_DC_CLEAR;
518 spin_unlock(&block_group->lock);
521 printk(KERN_ERR "btrfs: failed to load free space cache "
522 "for block group %llu\n", block_group->key.objectid);
529 int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
530 struct btrfs_free_space_ctl *ctl,
531 struct btrfs_block_group_cache *block_group,
532 struct btrfs_trans_handle *trans,
533 struct btrfs_path *path, u64 offset)
535 struct btrfs_free_space_header *header;
536 struct extent_buffer *leaf;
537 struct rb_node *node;
538 struct list_head *pos, *n;
541 struct extent_state *cached_state = NULL;
542 struct btrfs_free_cluster *cluster = NULL;
543 struct extent_io_tree *unpin = NULL;
544 struct list_head bitmap_list;
545 struct btrfs_key key;
549 int index = 0, num_pages = 0;
553 bool next_page = false;
554 bool out_of_space = false;
556 INIT_LIST_HEAD(&bitmap_list);
558 node = rb_first(&ctl->free_space_offset);
562 if (!i_size_read(inode))
565 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
568 filemap_write_and_wait(inode->i_mapping);
569 btrfs_wait_ordered_range(inode, inode->i_size &
570 ~(root->sectorsize - 1), (u64)-1);
572 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
576 /* Get the cluster for this block_group if it exists */
577 if (block_group && !list_empty(&block_group->cluster_list))
578 cluster = list_entry(block_group->cluster_list.next,
579 struct btrfs_free_cluster,
583 * We shouldn't have switched the pinned extents yet so this is the
586 unpin = root->fs_info->pinned_extents;
589 * Lock all pages first so we can lock the extent safely.
591 * NOTE: Because we hold the ref the entire time we're going to write to
592 * the page find_get_page should never fail, so we don't do a check
593 * after find_get_page at this point. Just putting this here so people
594 * know and don't freak out.
596 while (index < num_pages) {
597 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
601 for (i = 0; i < num_pages; i++) {
602 unlock_page(pages[i]);
603 page_cache_release(pages[i]);
612 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
613 0, &cached_state, GFP_NOFS);
616 * When searching for pinned extents, we need to start at our start
620 start = block_group->key.objectid;
622 /* Write out the extent entries */
624 struct btrfs_free_space_entry *entry;
626 unsigned long offset = 0;
630 if (index >= num_pages) {
637 orig = addr = kmap(page);
642 * We're going to put in a bogus crc for this page to
643 * make sure that old kernels who aren't aware of this
644 * format will be sure to discard the cache.
647 offset += sizeof(u64);
650 *gen = trans->transid;
652 offset += sizeof(u64);
656 memset(addr, 0, PAGE_CACHE_SIZE - offset);
657 while (node && !next_page) {
658 struct btrfs_free_space *e;
660 e = rb_entry(node, struct btrfs_free_space, offset_index);
663 entry->offset = cpu_to_le64(e->offset);
664 entry->bytes = cpu_to_le64(e->bytes);
666 entry->type = BTRFS_FREE_SPACE_BITMAP;
667 list_add_tail(&e->list, &bitmap_list);
670 entry->type = BTRFS_FREE_SPACE_EXTENT;
672 node = rb_next(node);
673 if (!node && cluster) {
674 node = rb_first(&cluster->root);
677 offset += sizeof(struct btrfs_free_space_entry);
678 if (offset + sizeof(struct btrfs_free_space_entry) >=
685 * We want to add any pinned extents to our free space cache
686 * so we don't leak the space
688 while (block_group && !next_page &&
689 (start < block_group->key.objectid +
690 block_group->key.offset)) {
691 ret = find_first_extent_bit(unpin, start, &start, &end,
698 /* This pinned extent is out of our range */
699 if (start >= block_group->key.objectid +
700 block_group->key.offset)
703 len = block_group->key.objectid +
704 block_group->key.offset - start;
705 len = min(len, end + 1 - start);
708 entry->offset = cpu_to_le64(start);
709 entry->bytes = cpu_to_le64(len);
710 entry->type = BTRFS_FREE_SPACE_EXTENT;
713 offset += sizeof(struct btrfs_free_space_entry);
714 if (offset + sizeof(struct btrfs_free_space_entry) >=
720 /* Generate bogus crc value */
723 crc = btrfs_csum_data(root, orig + sizeof(u64), crc,
724 PAGE_CACHE_SIZE - sizeof(u64));
725 btrfs_csum_final(crc, (char *)&crc);
733 bytes += PAGE_CACHE_SIZE;
736 } while (node || next_page);
738 /* Write out the bitmaps */
739 list_for_each_safe(pos, n, &bitmap_list) {
741 struct btrfs_free_space *entry =
742 list_entry(pos, struct btrfs_free_space, list);
744 if (index >= num_pages) {
751 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
753 bytes += PAGE_CACHE_SIZE;
755 list_del_init(&entry->list);
760 btrfs_drop_pages(pages, num_pages);
761 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
762 i_size_read(inode) - 1, &cached_state,
768 /* Zero out the rest of the pages just to make sure */
769 while (index < num_pages) {
774 memset(addr, 0, PAGE_CACHE_SIZE);
776 bytes += PAGE_CACHE_SIZE;
780 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
781 bytes, &cached_state);
782 btrfs_drop_pages(pages, num_pages);
783 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
784 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
791 BTRFS_I(inode)->generation = trans->transid;
793 filemap_write_and_wait(inode->i_mapping);
795 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
799 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
802 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
803 EXTENT_DIRTY | EXTENT_DELALLOC |
804 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
807 leaf = path->nodes[0];
809 struct btrfs_key found_key;
810 BUG_ON(!path->slots[0]);
812 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
813 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
814 found_key.offset != offset) {
816 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
817 EXTENT_DIRTY | EXTENT_DELALLOC |
818 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
820 btrfs_release_path(path);
824 header = btrfs_item_ptr(leaf, path->slots[0],
825 struct btrfs_free_space_header);
826 btrfs_set_free_space_entries(leaf, header, entries);
827 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
828 btrfs_set_free_space_generation(leaf, header, trans->transid);
829 btrfs_mark_buffer_dirty(leaf);
830 btrfs_release_path(path);
837 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
838 BTRFS_I(inode)->generation = 0;
840 btrfs_update_inode(trans, root, inode);
844 int btrfs_write_out_cache(struct btrfs_root *root,
845 struct btrfs_trans_handle *trans,
846 struct btrfs_block_group_cache *block_group,
847 struct btrfs_path *path)
849 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
853 root = root->fs_info->tree_root;
855 spin_lock(&block_group->lock);
856 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
857 spin_unlock(&block_group->lock);
860 spin_unlock(&block_group->lock);
862 inode = lookup_free_space_inode(root, block_group, path);
866 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
867 path, block_group->key.objectid);
869 spin_lock(&block_group->lock);
870 block_group->disk_cache_state = BTRFS_DC_ERROR;
871 spin_unlock(&block_group->lock);
874 printk(KERN_ERR "btrfs: failed to write free space cace "
875 "for block group %llu\n", block_group->key.objectid);
882 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
885 BUG_ON(offset < bitmap_start);
886 offset -= bitmap_start;
887 return (unsigned long)(div_u64(offset, unit));
890 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
892 return (unsigned long)(div_u64(bytes, unit));
895 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
899 u64 bytes_per_bitmap;
901 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
902 bitmap_start = offset - ctl->start;
903 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
904 bitmap_start *= bytes_per_bitmap;
905 bitmap_start += ctl->start;
910 static int tree_insert_offset(struct rb_root *root, u64 offset,
911 struct rb_node *node, int bitmap)
913 struct rb_node **p = &root->rb_node;
914 struct rb_node *parent = NULL;
915 struct btrfs_free_space *info;
919 info = rb_entry(parent, struct btrfs_free_space, offset_index);
921 if (offset < info->offset) {
923 } else if (offset > info->offset) {
927 * we could have a bitmap entry and an extent entry
928 * share the same offset. If this is the case, we want
929 * the extent entry to always be found first if we do a
930 * linear search through the tree, since we want to have
931 * the quickest allocation time, and allocating from an
932 * extent is faster than allocating from a bitmap. So
933 * if we're inserting a bitmap and we find an entry at
934 * this offset, we want to go right, or after this entry
935 * logically. If we are inserting an extent and we've
936 * found a bitmap, we want to go left, or before
955 rb_link_node(node, parent, p);
956 rb_insert_color(node, root);
962 * searches the tree for the given offset.
964 * fuzzy - If this is set, then we are trying to make an allocation, and we just
965 * want a section that has at least bytes size and comes at or after the given
968 static struct btrfs_free_space *
969 tree_search_offset(struct btrfs_free_space_ctl *ctl,
970 u64 offset, int bitmap_only, int fuzzy)
972 struct rb_node *n = ctl->free_space_offset.rb_node;
973 struct btrfs_free_space *entry, *prev = NULL;
975 /* find entry that is closest to the 'offset' */
982 entry = rb_entry(n, struct btrfs_free_space, offset_index);
985 if (offset < entry->offset)
987 else if (offset > entry->offset)
1000 * bitmap entry and extent entry may share same offset,
1001 * in that case, bitmap entry comes after extent entry.
1006 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1007 if (entry->offset != offset)
1010 WARN_ON(!entry->bitmap);
1013 if (entry->bitmap) {
1015 * if previous extent entry covers the offset,
1016 * we should return it instead of the bitmap entry
1018 n = &entry->offset_index;
1023 prev = rb_entry(n, struct btrfs_free_space,
1025 if (!prev->bitmap) {
1026 if (prev->offset + prev->bytes > offset)
1038 /* find last entry before the 'offset' */
1040 if (entry->offset > offset) {
1041 n = rb_prev(&entry->offset_index);
1043 entry = rb_entry(n, struct btrfs_free_space,
1045 BUG_ON(entry->offset > offset);
1054 if (entry->bitmap) {
1055 n = &entry->offset_index;
1060 prev = rb_entry(n, struct btrfs_free_space,
1062 if (!prev->bitmap) {
1063 if (prev->offset + prev->bytes > offset)
1068 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1070 } else if (entry->offset + entry->bytes > offset)
1077 if (entry->bitmap) {
1078 if (entry->offset + BITS_PER_BITMAP *
1082 if (entry->offset + entry->bytes > offset)
1086 n = rb_next(&entry->offset_index);
1089 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1095 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1096 struct btrfs_free_space *info)
1098 rb_erase(&info->offset_index, &ctl->free_space_offset);
1099 ctl->free_extents--;
1102 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1103 struct btrfs_free_space *info)
1105 __unlink_free_space(ctl, info);
1106 ctl->free_space -= info->bytes;
1109 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1110 struct btrfs_free_space *info)
1114 BUG_ON(!info->bitmap && !info->bytes);
1115 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1116 &info->offset_index, (info->bitmap != NULL));
1120 ctl->free_space += info->bytes;
1121 ctl->free_extents++;
1125 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1127 struct btrfs_block_group_cache *block_group = ctl->private;
1131 u64 size = block_group->key.offset;
1132 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1133 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1135 BUG_ON(ctl->total_bitmaps > max_bitmaps);
1138 * The goal is to keep the total amount of memory used per 1gb of space
1139 * at or below 32k, so we need to adjust how much memory we allow to be
1140 * used by extent based free space tracking
1142 if (size < 1024 * 1024 * 1024)
1143 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1145 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1146 div64_u64(size, 1024 * 1024 * 1024);
1149 * we want to account for 1 more bitmap than what we have so we can make
1150 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1151 * we add more bitmaps.
1153 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
1155 if (bitmap_bytes >= max_bytes) {
1156 ctl->extents_thresh = 0;
1161 * we want the extent entry threshold to always be at most 1/2 the maxw
1162 * bytes we can have, or whatever is less than that.
1164 extent_bytes = max_bytes - bitmap_bytes;
1165 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1167 ctl->extents_thresh =
1168 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1171 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1172 struct btrfs_free_space *info, u64 offset,
1175 unsigned long start, count;
1177 start = offset_to_bit(info->offset, ctl->unit, offset);
1178 count = bytes_to_bits(bytes, ctl->unit);
1179 BUG_ON(start + count > BITS_PER_BITMAP);
1181 bitmap_clear(info->bitmap, start, count);
1183 info->bytes -= bytes;
1184 ctl->free_space -= bytes;
1187 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1188 struct btrfs_free_space *info, u64 offset,
1191 unsigned long start, count;
1193 start = offset_to_bit(info->offset, ctl->unit, offset);
1194 count = bytes_to_bits(bytes, ctl->unit);
1195 BUG_ON(start + count > BITS_PER_BITMAP);
1197 bitmap_set(info->bitmap, start, count);
1199 info->bytes += bytes;
1200 ctl->free_space += bytes;
1203 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1204 struct btrfs_free_space *bitmap_info, u64 *offset,
1207 unsigned long found_bits = 0;
1208 unsigned long bits, i;
1209 unsigned long next_zero;
1211 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1212 max_t(u64, *offset, bitmap_info->offset));
1213 bits = bytes_to_bits(*bytes, ctl->unit);
1215 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1216 i < BITS_PER_BITMAP;
1217 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1218 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1219 BITS_PER_BITMAP, i);
1220 if ((next_zero - i) >= bits) {
1221 found_bits = next_zero - i;
1228 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1229 *bytes = (u64)(found_bits) * ctl->unit;
1236 static struct btrfs_free_space *
1237 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
1239 struct btrfs_free_space *entry;
1240 struct rb_node *node;
1243 if (!ctl->free_space_offset.rb_node)
1246 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1250 for (node = &entry->offset_index; node; node = rb_next(node)) {
1251 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1252 if (entry->bytes < *bytes)
1255 if (entry->bitmap) {
1256 ret = search_bitmap(ctl, entry, offset, bytes);
1262 *offset = entry->offset;
1263 *bytes = entry->bytes;
1270 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1271 struct btrfs_free_space *info, u64 offset)
1273 info->offset = offset_to_bitmap(ctl, offset);
1275 link_free_space(ctl, info);
1276 ctl->total_bitmaps++;
1278 ctl->op->recalc_thresholds(ctl);
1281 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1282 struct btrfs_free_space *bitmap_info)
1284 unlink_free_space(ctl, bitmap_info);
1285 kfree(bitmap_info->bitmap);
1286 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1287 ctl->total_bitmaps--;
1288 ctl->op->recalc_thresholds(ctl);
1291 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1292 struct btrfs_free_space *bitmap_info,
1293 u64 *offset, u64 *bytes)
1296 u64 search_start, search_bytes;
1300 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1303 * XXX - this can go away after a few releases.
1305 * since the only user of btrfs_remove_free_space is the tree logging
1306 * stuff, and the only way to test that is under crash conditions, we
1307 * want to have this debug stuff here just in case somethings not
1308 * working. Search the bitmap for the space we are trying to use to
1309 * make sure its actually there. If its not there then we need to stop
1310 * because something has gone wrong.
1312 search_start = *offset;
1313 search_bytes = *bytes;
1314 search_bytes = min(search_bytes, end - search_start + 1);
1315 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
1316 BUG_ON(ret < 0 || search_start != *offset);
1318 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
1319 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
1320 *bytes -= end - *offset + 1;
1322 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
1323 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
1328 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1329 if (!bitmap_info->bytes)
1330 free_bitmap(ctl, bitmap_info);
1333 * no entry after this bitmap, but we still have bytes to
1334 * remove, so something has gone wrong.
1339 bitmap_info = rb_entry(next, struct btrfs_free_space,
1343 * if the next entry isn't a bitmap we need to return to let the
1344 * extent stuff do its work.
1346 if (!bitmap_info->bitmap)
1350 * Ok the next item is a bitmap, but it may not actually hold
1351 * the information for the rest of this free space stuff, so
1352 * look for it, and if we don't find it return so we can try
1353 * everything over again.
1355 search_start = *offset;
1356 search_bytes = *bytes;
1357 ret = search_bitmap(ctl, bitmap_info, &search_start,
1359 if (ret < 0 || search_start != *offset)
1363 } else if (!bitmap_info->bytes)
1364 free_bitmap(ctl, bitmap_info);
1369 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1370 struct btrfs_free_space *info, u64 offset,
1373 u64 bytes_to_set = 0;
1376 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1378 bytes_to_set = min(end - offset, bytes);
1380 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1382 return bytes_to_set;
1386 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1387 struct btrfs_free_space *info)
1389 struct btrfs_block_group_cache *block_group = ctl->private;
1392 * If we are below the extents threshold then we can add this as an
1393 * extent, and don't have to deal with the bitmap
1395 if (ctl->free_extents < ctl->extents_thresh) {
1397 * If this block group has some small extents we don't want to
1398 * use up all of our free slots in the cache with them, we want
1399 * to reserve them to larger extents, however if we have plent
1400 * of cache left then go ahead an dadd them, no sense in adding
1401 * the overhead of a bitmap if we don't have to.
1403 if (info->bytes <= block_group->sectorsize * 4) {
1404 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1412 * some block groups are so tiny they can't be enveloped by a bitmap, so
1413 * don't even bother to create a bitmap for this
1415 if (BITS_PER_BITMAP * block_group->sectorsize >
1416 block_group->key.offset)
1422 static struct btrfs_free_space_op free_space_op = {
1423 .recalc_thresholds = recalculate_thresholds,
1424 .use_bitmap = use_bitmap,
1427 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1428 struct btrfs_free_space *info)
1430 struct btrfs_free_space *bitmap_info;
1431 struct btrfs_block_group_cache *block_group = NULL;
1433 u64 bytes, offset, bytes_added;
1436 bytes = info->bytes;
1437 offset = info->offset;
1439 if (!ctl->op->use_bitmap(ctl, info))
1442 if (ctl->op == &free_space_op)
1443 block_group = ctl->private;
1446 * Since we link bitmaps right into the cluster we need to see if we
1447 * have a cluster here, and if so and it has our bitmap we need to add
1448 * the free space to that bitmap.
1450 if (block_group && !list_empty(&block_group->cluster_list)) {
1451 struct btrfs_free_cluster *cluster;
1452 struct rb_node *node;
1453 struct btrfs_free_space *entry;
1455 cluster = list_entry(block_group->cluster_list.next,
1456 struct btrfs_free_cluster,
1458 spin_lock(&cluster->lock);
1459 node = rb_first(&cluster->root);
1461 spin_unlock(&cluster->lock);
1462 goto no_cluster_bitmap;
1465 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1466 if (!entry->bitmap) {
1467 spin_unlock(&cluster->lock);
1468 goto no_cluster_bitmap;
1471 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1472 bytes_added = add_bytes_to_bitmap(ctl, entry,
1474 bytes -= bytes_added;
1475 offset += bytes_added;
1477 spin_unlock(&cluster->lock);
1485 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1492 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1493 bytes -= bytes_added;
1494 offset += bytes_added;
1504 if (info && info->bitmap) {
1505 add_new_bitmap(ctl, info, offset);
1510 spin_unlock(&ctl->tree_lock);
1512 /* no pre-allocated info, allocate a new one */
1514 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1517 spin_lock(&ctl->tree_lock);
1523 /* allocate the bitmap */
1524 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1525 spin_lock(&ctl->tree_lock);
1526 if (!info->bitmap) {
1536 kfree(info->bitmap);
1537 kmem_cache_free(btrfs_free_space_cachep, info);
1543 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
1544 struct btrfs_free_space *info, bool update_stat)
1546 struct btrfs_free_space *left_info;
1547 struct btrfs_free_space *right_info;
1548 bool merged = false;
1549 u64 offset = info->offset;
1550 u64 bytes = info->bytes;
1553 * first we want to see if there is free space adjacent to the range we
1554 * are adding, if there is remove that struct and add a new one to
1555 * cover the entire range
1557 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
1558 if (right_info && rb_prev(&right_info->offset_index))
1559 left_info = rb_entry(rb_prev(&right_info->offset_index),
1560 struct btrfs_free_space, offset_index);
1562 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
1564 if (right_info && !right_info->bitmap) {
1566 unlink_free_space(ctl, right_info);
1568 __unlink_free_space(ctl, right_info);
1569 info->bytes += right_info->bytes;
1570 kmem_cache_free(btrfs_free_space_cachep, right_info);
1574 if (left_info && !left_info->bitmap &&
1575 left_info->offset + left_info->bytes == offset) {
1577 unlink_free_space(ctl, left_info);
1579 __unlink_free_space(ctl, left_info);
1580 info->offset = left_info->offset;
1581 info->bytes += left_info->bytes;
1582 kmem_cache_free(btrfs_free_space_cachep, left_info);
1589 int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1590 u64 offset, u64 bytes)
1592 struct btrfs_free_space *info;
1595 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
1599 info->offset = offset;
1600 info->bytes = bytes;
1602 spin_lock(&ctl->tree_lock);
1604 if (try_merge_free_space(ctl, info, true))
1608 * There was no extent directly to the left or right of this new
1609 * extent then we know we're going to have to allocate a new extent, so
1610 * before we do that see if we need to drop this into a bitmap
1612 ret = insert_into_bitmap(ctl, info);
1620 ret = link_free_space(ctl, info);
1622 kmem_cache_free(btrfs_free_space_cachep, info);
1624 spin_unlock(&ctl->tree_lock);
1627 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
1628 BUG_ON(ret == -EEXIST);
1634 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1635 u64 offset, u64 bytes)
1637 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1638 struct btrfs_free_space *info;
1639 struct btrfs_free_space *next_info = NULL;
1642 spin_lock(&ctl->tree_lock);
1645 info = tree_search_offset(ctl, offset, 0, 0);
1648 * oops didn't find an extent that matched the space we wanted
1649 * to remove, look for a bitmap instead
1651 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1659 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1661 next_info = rb_entry(rb_next(&info->offset_index),
1662 struct btrfs_free_space,
1665 if (next_info->bitmap)
1666 end = next_info->offset +
1667 BITS_PER_BITMAP * ctl->unit - 1;
1669 end = next_info->offset + next_info->bytes;
1671 if (next_info->bytes < bytes ||
1672 next_info->offset > offset || offset > end) {
1673 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1674 " trying to use %llu\n",
1675 (unsigned long long)info->offset,
1676 (unsigned long long)info->bytes,
1677 (unsigned long long)bytes);
1686 if (info->bytes == bytes) {
1687 unlink_free_space(ctl, info);
1689 kfree(info->bitmap);
1690 ctl->total_bitmaps--;
1692 kmem_cache_free(btrfs_free_space_cachep, info);
1696 if (!info->bitmap && info->offset == offset) {
1697 unlink_free_space(ctl, info);
1698 info->offset += bytes;
1699 info->bytes -= bytes;
1700 link_free_space(ctl, info);
1704 if (!info->bitmap && info->offset <= offset &&
1705 info->offset + info->bytes >= offset + bytes) {
1706 u64 old_start = info->offset;
1708 * we're freeing space in the middle of the info,
1709 * this can happen during tree log replay
1711 * first unlink the old info and then
1712 * insert it again after the hole we're creating
1714 unlink_free_space(ctl, info);
1715 if (offset + bytes < info->offset + info->bytes) {
1716 u64 old_end = info->offset + info->bytes;
1718 info->offset = offset + bytes;
1719 info->bytes = old_end - info->offset;
1720 ret = link_free_space(ctl, info);
1725 /* the hole we're creating ends at the end
1726 * of the info struct, just free the info
1728 kmem_cache_free(btrfs_free_space_cachep, info);
1730 spin_unlock(&ctl->tree_lock);
1732 /* step two, insert a new info struct to cover
1733 * anything before the hole
1735 ret = btrfs_add_free_space(block_group, old_start,
1736 offset - old_start);
1741 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
1746 spin_unlock(&ctl->tree_lock);
1751 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1754 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1755 struct btrfs_free_space *info;
1759 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
1760 info = rb_entry(n, struct btrfs_free_space, offset_index);
1761 if (info->bytes >= bytes)
1763 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
1764 (unsigned long long)info->offset,
1765 (unsigned long long)info->bytes,
1766 (info->bitmap) ? "yes" : "no");
1768 printk(KERN_INFO "block group has cluster?: %s\n",
1769 list_empty(&block_group->cluster_list) ? "no" : "yes");
1770 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1774 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
1776 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1778 spin_lock_init(&ctl->tree_lock);
1779 ctl->unit = block_group->sectorsize;
1780 ctl->start = block_group->key.objectid;
1781 ctl->private = block_group;
1782 ctl->op = &free_space_op;
1785 * we only want to have 32k of ram per block group for keeping
1786 * track of free space, and if we pass 1/2 of that we want to
1787 * start converting things over to using bitmaps
1789 ctl->extents_thresh = ((1024 * 32) / 2) /
1790 sizeof(struct btrfs_free_space);
1794 * for a given cluster, put all of its extents back into the free
1795 * space cache. If the block group passed doesn't match the block group
1796 * pointed to by the cluster, someone else raced in and freed the
1797 * cluster already. In that case, we just return without changing anything
1800 __btrfs_return_cluster_to_free_space(
1801 struct btrfs_block_group_cache *block_group,
1802 struct btrfs_free_cluster *cluster)
1804 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1805 struct btrfs_free_space *entry;
1806 struct rb_node *node;
1808 spin_lock(&cluster->lock);
1809 if (cluster->block_group != block_group)
1812 cluster->block_group = NULL;
1813 cluster->window_start = 0;
1814 list_del_init(&cluster->block_group_list);
1816 node = rb_first(&cluster->root);
1820 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1821 node = rb_next(&entry->offset_index);
1822 rb_erase(&entry->offset_index, &cluster->root);
1824 bitmap = (entry->bitmap != NULL);
1826 try_merge_free_space(ctl, entry, false);
1827 tree_insert_offset(&ctl->free_space_offset,
1828 entry->offset, &entry->offset_index, bitmap);
1830 cluster->root = RB_ROOT;
1833 spin_unlock(&cluster->lock);
1834 btrfs_put_block_group(block_group);
1838 void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
1840 struct btrfs_free_space *info;
1841 struct rb_node *node;
1843 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1844 info = rb_entry(node, struct btrfs_free_space, offset_index);
1845 if (!info->bitmap) {
1846 unlink_free_space(ctl, info);
1847 kmem_cache_free(btrfs_free_space_cachep, info);
1849 free_bitmap(ctl, info);
1851 if (need_resched()) {
1852 spin_unlock(&ctl->tree_lock);
1854 spin_lock(&ctl->tree_lock);
1859 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1861 spin_lock(&ctl->tree_lock);
1862 __btrfs_remove_free_space_cache_locked(ctl);
1863 spin_unlock(&ctl->tree_lock);
1866 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1868 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1869 struct btrfs_free_cluster *cluster;
1870 struct list_head *head;
1872 spin_lock(&ctl->tree_lock);
1873 while ((head = block_group->cluster_list.next) !=
1874 &block_group->cluster_list) {
1875 cluster = list_entry(head, struct btrfs_free_cluster,
1878 WARN_ON(cluster->block_group != block_group);
1879 __btrfs_return_cluster_to_free_space(block_group, cluster);
1880 if (need_resched()) {
1881 spin_unlock(&ctl->tree_lock);
1883 spin_lock(&ctl->tree_lock);
1886 __btrfs_remove_free_space_cache_locked(ctl);
1887 spin_unlock(&ctl->tree_lock);
1891 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1892 u64 offset, u64 bytes, u64 empty_size)
1894 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1895 struct btrfs_free_space *entry = NULL;
1896 u64 bytes_search = bytes + empty_size;
1899 spin_lock(&ctl->tree_lock);
1900 entry = find_free_space(ctl, &offset, &bytes_search);
1905 if (entry->bitmap) {
1906 bitmap_clear_bits(ctl, entry, offset, bytes);
1908 free_bitmap(ctl, entry);
1910 unlink_free_space(ctl, entry);
1911 entry->offset += bytes;
1912 entry->bytes -= bytes;
1914 kmem_cache_free(btrfs_free_space_cachep, entry);
1916 link_free_space(ctl, entry);
1920 spin_unlock(&ctl->tree_lock);
1926 * given a cluster, put all of its extents back into the free space
1927 * cache. If a block group is passed, this function will only free
1928 * a cluster that belongs to the passed block group.
1930 * Otherwise, it'll get a reference on the block group pointed to by the
1931 * cluster and remove the cluster from it.
1933 int btrfs_return_cluster_to_free_space(
1934 struct btrfs_block_group_cache *block_group,
1935 struct btrfs_free_cluster *cluster)
1937 struct btrfs_free_space_ctl *ctl;
1940 /* first, get a safe pointer to the block group */
1941 spin_lock(&cluster->lock);
1943 block_group = cluster->block_group;
1945 spin_unlock(&cluster->lock);
1948 } else if (cluster->block_group != block_group) {
1949 /* someone else has already freed it don't redo their work */
1950 spin_unlock(&cluster->lock);
1953 atomic_inc(&block_group->count);
1954 spin_unlock(&cluster->lock);
1956 ctl = block_group->free_space_ctl;
1958 /* now return any extents the cluster had on it */
1959 spin_lock(&ctl->tree_lock);
1960 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1961 spin_unlock(&ctl->tree_lock);
1963 /* finally drop our ref */
1964 btrfs_put_block_group(block_group);
1968 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1969 struct btrfs_free_cluster *cluster,
1970 struct btrfs_free_space *entry,
1971 u64 bytes, u64 min_start)
1973 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1975 u64 search_start = cluster->window_start;
1976 u64 search_bytes = bytes;
1979 search_start = min_start;
1980 search_bytes = bytes;
1982 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
1987 bitmap_clear_bits(ctl, entry, ret, bytes);
1993 * given a cluster, try to allocate 'bytes' from it, returns 0
1994 * if it couldn't find anything suitably large, or a logical disk offset
1995 * if things worked out
1997 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1998 struct btrfs_free_cluster *cluster, u64 bytes,
2001 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2002 struct btrfs_free_space *entry = NULL;
2003 struct rb_node *node;
2006 spin_lock(&cluster->lock);
2007 if (bytes > cluster->max_size)
2010 if (cluster->block_group != block_group)
2013 node = rb_first(&cluster->root);
2017 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2019 if (entry->bytes < bytes ||
2020 (!entry->bitmap && entry->offset < min_start)) {
2021 node = rb_next(&entry->offset_index);
2024 entry = rb_entry(node, struct btrfs_free_space,
2029 if (entry->bitmap) {
2030 ret = btrfs_alloc_from_bitmap(block_group,
2031 cluster, entry, bytes,
2034 node = rb_next(&entry->offset_index);
2037 entry = rb_entry(node, struct btrfs_free_space,
2043 ret = entry->offset;
2045 entry->offset += bytes;
2046 entry->bytes -= bytes;
2049 if (entry->bytes == 0)
2050 rb_erase(&entry->offset_index, &cluster->root);
2054 spin_unlock(&cluster->lock);
2059 spin_lock(&ctl->tree_lock);
2061 ctl->free_space -= bytes;
2062 if (entry->bytes == 0) {
2063 ctl->free_extents--;
2064 if (entry->bitmap) {
2065 kfree(entry->bitmap);
2066 ctl->total_bitmaps--;
2067 ctl->op->recalc_thresholds(ctl);
2069 kmem_cache_free(btrfs_free_space_cachep, entry);
2072 spin_unlock(&ctl->tree_lock);
2077 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2078 struct btrfs_free_space *entry,
2079 struct btrfs_free_cluster *cluster,
2080 u64 offset, u64 bytes, u64 min_bytes)
2082 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2083 unsigned long next_zero;
2085 unsigned long search_bits;
2086 unsigned long total_bits;
2087 unsigned long found_bits;
2088 unsigned long start = 0;
2089 unsigned long total_found = 0;
2093 i = offset_to_bit(entry->offset, block_group->sectorsize,
2094 max_t(u64, offset, entry->offset));
2095 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2096 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
2100 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2101 i < BITS_PER_BITMAP;
2102 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2103 next_zero = find_next_zero_bit(entry->bitmap,
2104 BITS_PER_BITMAP, i);
2105 if (next_zero - i >= search_bits) {
2106 found_bits = next_zero - i;
2120 total_found += found_bits;
2122 if (cluster->max_size < found_bits * block_group->sectorsize)
2123 cluster->max_size = found_bits * block_group->sectorsize;
2125 if (total_found < total_bits) {
2126 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2127 if (i - start > total_bits * 2) {
2129 cluster->max_size = 0;
2135 cluster->window_start = start * block_group->sectorsize +
2137 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2138 ret = tree_insert_offset(&cluster->root, entry->offset,
2139 &entry->offset_index, 1);
2146 * This searches the block group for just extents to fill the cluster with.
2149 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2150 struct btrfs_free_cluster *cluster,
2151 struct list_head *bitmaps, u64 offset, u64 bytes,
2154 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2155 struct btrfs_free_space *first = NULL;
2156 struct btrfs_free_space *entry = NULL;
2157 struct btrfs_free_space *prev = NULL;
2158 struct btrfs_free_space *last;
2159 struct rb_node *node;
2163 u64 max_gap = 128 * 1024;
2165 entry = tree_search_offset(ctl, offset, 0, 1);
2170 * We don't want bitmaps, so just move along until we find a normal
2173 while (entry->bitmap) {
2174 if (list_empty(&entry->list))
2175 list_add_tail(&entry->list, bitmaps);
2176 node = rb_next(&entry->offset_index);
2179 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2182 window_start = entry->offset;
2183 window_free = entry->bytes;
2184 max_extent = entry->bytes;
2189 while (window_free <= min_bytes) {
2190 node = rb_next(&entry->offset_index);
2193 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2195 if (entry->bitmap) {
2196 if (list_empty(&entry->list))
2197 list_add_tail(&entry->list, bitmaps);
2202 * we haven't filled the empty size and the window is
2203 * very large. reset and try again
2205 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2206 entry->offset - window_start > (min_bytes * 2)) {
2208 window_start = entry->offset;
2209 window_free = entry->bytes;
2211 max_extent = entry->bytes;
2214 window_free += entry->bytes;
2215 if (entry->bytes > max_extent)
2216 max_extent = entry->bytes;
2221 cluster->window_start = first->offset;
2223 node = &first->offset_index;
2226 * now we've found our entries, pull them out of the free space
2227 * cache and put them into the cluster rbtree
2232 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2233 node = rb_next(&entry->offset_index);
2237 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2238 ret = tree_insert_offset(&cluster->root, entry->offset,
2239 &entry->offset_index, 0);
2241 } while (node && entry != last);
2243 cluster->max_size = max_extent;
2249 * This specifically looks for bitmaps that may work in the cluster, we assume
2250 * that we have already failed to find extents that will work.
2253 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2254 struct btrfs_free_cluster *cluster,
2255 struct list_head *bitmaps, u64 offset, u64 bytes,
2258 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2259 struct btrfs_free_space *entry;
2260 struct rb_node *node;
2263 if (ctl->total_bitmaps == 0)
2267 * First check our cached list of bitmaps and see if there is an entry
2268 * here that will work.
2270 list_for_each_entry(entry, bitmaps, list) {
2271 if (entry->bytes < min_bytes)
2273 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2280 * If we do have entries on our list and we are here then we didn't find
2281 * anything, so go ahead and get the next entry after the last entry in
2282 * this list and start the search from there.
2284 if (!list_empty(bitmaps)) {
2285 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2287 node = rb_next(&entry->offset_index);
2290 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2294 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
2299 node = &entry->offset_index;
2301 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2302 node = rb_next(&entry->offset_index);
2305 if (entry->bytes < min_bytes)
2307 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2309 } while (ret && node);
2315 * here we try to find a cluster of blocks in a block group. The goal
2316 * is to find at least bytes free and up to empty_size + bytes free.
2317 * We might not find them all in one contiguous area.
2319 * returns zero and sets up cluster if things worked out, otherwise
2320 * it returns -enospc
2322 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2323 struct btrfs_root *root,
2324 struct btrfs_block_group_cache *block_group,
2325 struct btrfs_free_cluster *cluster,
2326 u64 offset, u64 bytes, u64 empty_size)
2328 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2329 struct list_head bitmaps;
2330 struct btrfs_free_space *entry, *tmp;
2334 /* for metadata, allow allocates with more holes */
2335 if (btrfs_test_opt(root, SSD_SPREAD)) {
2336 min_bytes = bytes + empty_size;
2337 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
2339 * we want to do larger allocations when we are
2340 * flushing out the delayed refs, it helps prevent
2341 * making more work as we go along.
2343 if (trans->transaction->delayed_refs.flushing)
2344 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2346 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2348 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2350 spin_lock(&ctl->tree_lock);
2353 * If we know we don't have enough space to make a cluster don't even
2354 * bother doing all the work to try and find one.
2356 if (ctl->free_space < min_bytes) {
2357 spin_unlock(&ctl->tree_lock);
2361 spin_lock(&cluster->lock);
2363 /* someone already found a cluster, hooray */
2364 if (cluster->block_group) {
2369 INIT_LIST_HEAD(&bitmaps);
2370 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2373 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2374 offset, bytes, min_bytes);
2376 /* Clear our temporary list */
2377 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2378 list_del_init(&entry->list);
2381 atomic_inc(&block_group->count);
2382 list_add_tail(&cluster->block_group_list,
2383 &block_group->cluster_list);
2384 cluster->block_group = block_group;
2387 spin_unlock(&cluster->lock);
2388 spin_unlock(&ctl->tree_lock);
2394 * simple code to zero out a cluster
2396 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2398 spin_lock_init(&cluster->lock);
2399 spin_lock_init(&cluster->refill_lock);
2400 cluster->root = RB_ROOT;
2401 cluster->max_size = 0;
2402 INIT_LIST_HEAD(&cluster->block_group_list);
2403 cluster->block_group = NULL;
2406 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2407 u64 *trimmed, u64 start, u64 end, u64 minlen)
2409 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2410 struct btrfs_free_space *entry = NULL;
2411 struct btrfs_fs_info *fs_info = block_group->fs_info;
2413 u64 actually_trimmed;
2418 while (start < end) {
2419 spin_lock(&ctl->tree_lock);
2421 if (ctl->free_space < minlen) {
2422 spin_unlock(&ctl->tree_lock);
2426 entry = tree_search_offset(ctl, start, 0, 1);
2428 entry = tree_search_offset(ctl,
2429 offset_to_bitmap(ctl, start),
2432 if (!entry || entry->offset >= end) {
2433 spin_unlock(&ctl->tree_lock);
2437 if (entry->bitmap) {
2438 ret = search_bitmap(ctl, entry, &start, &bytes);
2441 spin_unlock(&ctl->tree_lock);
2444 bytes = min(bytes, end - start);
2445 bitmap_clear_bits(ctl, entry, start, bytes);
2446 if (entry->bytes == 0)
2447 free_bitmap(ctl, entry);
2449 start = entry->offset + BITS_PER_BITMAP *
2450 block_group->sectorsize;
2451 spin_unlock(&ctl->tree_lock);
2456 start = entry->offset;
2457 bytes = min(entry->bytes, end - start);
2458 unlink_free_space(ctl, entry);
2459 kmem_cache_free(btrfs_free_space_cachep, entry);
2462 spin_unlock(&ctl->tree_lock);
2464 if (bytes >= minlen) {
2466 update_ret = btrfs_update_reserved_bytes(block_group,
2469 ret = btrfs_error_discard_extent(fs_info->extent_root,
2474 btrfs_add_free_space(block_group, start, bytes);
2476 btrfs_update_reserved_bytes(block_group,
2481 *trimmed += actually_trimmed;
2486 if (fatal_signal_pending(current)) {
2498 * Find the left-most item in the cache tree, and then return the
2499 * smallest inode number in the item.
2501 * Note: the returned inode number may not be the smallest one in
2502 * the tree, if the left-most item is a bitmap.
2504 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2506 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2507 struct btrfs_free_space *entry = NULL;
2510 spin_lock(&ctl->tree_lock);
2512 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2515 entry = rb_entry(rb_first(&ctl->free_space_offset),
2516 struct btrfs_free_space, offset_index);
2518 if (!entry->bitmap) {
2519 ino = entry->offset;
2521 unlink_free_space(ctl, entry);
2525 kmem_cache_free(btrfs_free_space_cachep, entry);
2527 link_free_space(ctl, entry);
2533 ret = search_bitmap(ctl, entry, &offset, &count);
2537 bitmap_clear_bits(ctl, entry, offset, 1);
2538 if (entry->bytes == 0)
2539 free_bitmap(ctl, entry);
2542 spin_unlock(&ctl->tree_lock);
2547 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2548 struct btrfs_path *path)
2550 struct inode *inode = NULL;
2552 spin_lock(&root->cache_lock);
2553 if (root->cache_inode)
2554 inode = igrab(root->cache_inode);
2555 spin_unlock(&root->cache_lock);
2559 inode = __lookup_free_space_inode(root, path, 0);
2563 spin_lock(&root->cache_lock);
2564 if (!btrfs_fs_closing(root->fs_info))
2565 root->cache_inode = igrab(inode);
2566 spin_unlock(&root->cache_lock);
2571 int create_free_ino_inode(struct btrfs_root *root,
2572 struct btrfs_trans_handle *trans,
2573 struct btrfs_path *path)
2575 return __create_free_space_inode(root, trans, path,
2576 BTRFS_FREE_INO_OBJECTID, 0);
2579 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2581 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2582 struct btrfs_path *path;
2583 struct inode *inode;
2585 u64 root_gen = btrfs_root_generation(&root->root_item);
2587 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2591 * If we're unmounting then just return, since this does a search on the
2592 * normal root and not the commit root and we could deadlock.
2594 if (btrfs_fs_closing(fs_info))
2597 path = btrfs_alloc_path();
2601 inode = lookup_free_ino_inode(root, path);
2605 if (root_gen != BTRFS_I(inode)->generation)
2608 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2611 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2612 "root %llu\n", root->root_key.objectid);
2616 btrfs_free_path(path);
2620 int btrfs_write_out_ino_cache(struct btrfs_root *root,
2621 struct btrfs_trans_handle *trans,
2622 struct btrfs_path *path)
2624 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2625 struct inode *inode;
2628 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2631 inode = lookup_free_ino_inode(root, path);
2635 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
2637 printk(KERN_ERR "btrfs: failed to write free ino cache "
2638 "for root %llu\n", root->root_key.objectid);