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_fs_closing(root->fs_info)) {
102 block_group->inode = igrab(inode);
103 block_group->iref = 1;
105 spin_unlock(&block_group->lock);
110 int __create_free_space_inode(struct btrfs_root *root,
111 struct btrfs_trans_handle *trans,
112 struct btrfs_path *path, u64 ino, u64 offset)
114 struct btrfs_key key;
115 struct btrfs_disk_key disk_key;
116 struct btrfs_free_space_header *header;
117 struct btrfs_inode_item *inode_item;
118 struct extent_buffer *leaf;
121 ret = btrfs_insert_empty_inode(trans, root, path, ino);
125 leaf = path->nodes[0];
126 inode_item = btrfs_item_ptr(leaf, path->slots[0],
127 struct btrfs_inode_item);
128 btrfs_item_key(leaf, &disk_key, path->slots[0]);
129 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
130 sizeof(*inode_item));
131 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
132 btrfs_set_inode_size(leaf, inode_item, 0);
133 btrfs_set_inode_nbytes(leaf, inode_item, 0);
134 btrfs_set_inode_uid(leaf, inode_item, 0);
135 btrfs_set_inode_gid(leaf, inode_item, 0);
136 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
137 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
138 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
139 btrfs_set_inode_nlink(leaf, inode_item, 1);
140 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
141 btrfs_set_inode_block_group(leaf, inode_item, offset);
142 btrfs_mark_buffer_dirty(leaf);
143 btrfs_release_path(path);
145 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
149 ret = btrfs_insert_empty_item(trans, root, path, &key,
150 sizeof(struct btrfs_free_space_header));
152 btrfs_release_path(path);
155 leaf = path->nodes[0];
156 header = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_free_space_header);
158 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
159 btrfs_set_free_space_key(leaf, header, &disk_key);
160 btrfs_mark_buffer_dirty(leaf);
161 btrfs_release_path(path);
166 int create_free_space_inode(struct btrfs_root *root,
167 struct btrfs_trans_handle *trans,
168 struct btrfs_block_group_cache *block_group,
169 struct btrfs_path *path)
174 ret = btrfs_find_free_objectid(root, &ino);
178 return __create_free_space_inode(root, trans, path, ino,
179 block_group->key.objectid);
182 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
183 struct btrfs_trans_handle *trans,
184 struct btrfs_path *path,
190 trans->block_rsv = root->orphan_block_rsv;
191 ret = btrfs_block_rsv_check(trans, root,
192 root->orphan_block_rsv,
197 oldsize = i_size_read(inode);
198 btrfs_i_size_write(inode, 0);
199 truncate_pagecache(inode, oldsize, 0);
202 * We don't need an orphan item because truncating the free space cache
203 * will never be split across transactions.
205 ret = btrfs_truncate_inode_items(trans, root, inode,
206 0, BTRFS_EXTENT_DATA_KEY);
212 ret = btrfs_update_inode(trans, root, inode);
216 static int readahead_cache(struct inode *inode)
218 struct file_ra_state *ra;
219 unsigned long last_index;
221 ra = kzalloc(sizeof(*ra), GFP_NOFS);
225 file_ra_state_init(ra, inode->i_mapping);
226 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
228 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
235 int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
236 struct btrfs_free_space_ctl *ctl,
237 struct btrfs_path *path, u64 offset)
239 struct btrfs_free_space_header *header;
240 struct extent_buffer *leaf;
242 u32 *checksums = NULL, *crc;
243 char *disk_crcs = NULL;
244 struct btrfs_key key;
245 struct list_head bitmaps;
249 u32 cur_crc = ~(u32)0;
251 unsigned long first_page_offset;
255 INIT_LIST_HEAD(&bitmaps);
257 /* Nothing in the space cache, goodbye */
258 if (!i_size_read(inode))
261 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
269 btrfs_release_path(path);
276 leaf = path->nodes[0];
277 header = btrfs_item_ptr(leaf, path->slots[0],
278 struct btrfs_free_space_header);
279 num_entries = btrfs_free_space_entries(leaf, header);
280 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
281 generation = btrfs_free_space_generation(leaf, header);
282 btrfs_release_path(path);
284 if (BTRFS_I(inode)->generation != generation) {
285 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
286 " not match free space cache generation (%llu)\n",
287 (unsigned long long)BTRFS_I(inode)->generation,
288 (unsigned long long)generation);
295 /* Setup everything for doing checksumming */
296 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
297 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
300 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
301 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
305 ret = readahead_cache(inode);
310 struct btrfs_free_space_entry *entry;
311 struct btrfs_free_space *e;
313 unsigned long offset = 0;
314 unsigned long start_offset = 0;
317 if (!num_entries && !num_bitmaps)
321 start_offset = first_page_offset;
322 offset = start_offset;
325 page = grab_cache_page(inode->i_mapping, index);
329 if (!PageUptodate(page)) {
330 btrfs_readpage(NULL, page);
332 if (!PageUptodate(page)) {
334 page_cache_release(page);
335 printk(KERN_ERR "btrfs: error reading free "
345 memcpy(disk_crcs, addr, first_page_offset);
346 gen = addr + (sizeof(u32) * num_checksums);
347 if (*gen != BTRFS_I(inode)->generation) {
348 printk(KERN_ERR "btrfs: space cache generation"
349 " (%llu) does not match inode (%llu)\n",
350 (unsigned long long)*gen,
352 BTRFS_I(inode)->generation);
355 page_cache_release(page);
358 crc = (u32 *)disk_crcs;
360 entry = addr + start_offset;
362 /* First lets check our crc before we do anything fun */
364 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
365 PAGE_CACHE_SIZE - start_offset);
366 btrfs_csum_final(cur_crc, (char *)&cur_crc);
367 if (cur_crc != *crc) {
368 printk(KERN_ERR "btrfs: crc mismatch for page %lu\n",
372 page_cache_release(page);
382 e = kmem_cache_zalloc(btrfs_free_space_cachep,
387 page_cache_release(page);
391 e->offset = le64_to_cpu(entry->offset);
392 e->bytes = le64_to_cpu(entry->bytes);
395 kmem_cache_free(btrfs_free_space_cachep, e);
397 page_cache_release(page);
401 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
402 spin_lock(&ctl->tree_lock);
403 ret = link_free_space(ctl, e);
404 spin_unlock(&ctl->tree_lock);
406 printk(KERN_ERR "Duplicate entries in "
407 "free space cache, dumping\n");
410 page_cache_release(page);
414 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
418 btrfs_free_space_cachep, e);
420 page_cache_release(page);
423 spin_lock(&ctl->tree_lock);
424 ret2 = link_free_space(ctl, e);
425 ctl->total_bitmaps++;
426 ctl->op->recalc_thresholds(ctl);
427 spin_unlock(&ctl->tree_lock);
428 list_add_tail(&e->list, &bitmaps);
430 printk(KERN_ERR "Duplicate entries in "
431 "free space cache, dumping\n");
434 page_cache_release(page);
440 offset += sizeof(struct btrfs_free_space_entry);
441 if (offset + sizeof(struct btrfs_free_space_entry) >=
448 * We read an entry out of this page, we need to move on to the
457 * We add the bitmaps at the end of the entries in order that
458 * the bitmap entries are added to the cache.
460 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
461 list_del_init(&e->list);
462 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
467 page_cache_release(page);
477 __btrfs_remove_free_space_cache(ctl);
481 int load_free_space_cache(struct btrfs_fs_info *fs_info,
482 struct btrfs_block_group_cache *block_group)
484 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
485 struct btrfs_root *root = fs_info->tree_root;
487 struct btrfs_path *path;
490 u64 used = btrfs_block_group_used(&block_group->item);
493 * If we're unmounting then just return, since this does a search on the
494 * normal root and not the commit root and we could deadlock.
496 if (btrfs_fs_closing(fs_info))
500 * If this block group has been marked to be cleared for one reason or
501 * another then we can't trust the on disk cache, so just return.
503 spin_lock(&block_group->lock);
504 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
505 spin_unlock(&block_group->lock);
508 spin_unlock(&block_group->lock);
510 path = btrfs_alloc_path();
514 inode = lookup_free_space_inode(root, block_group, path);
516 btrfs_free_path(path);
520 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
521 path, block_group->key.objectid);
522 btrfs_free_path(path);
526 spin_lock(&ctl->tree_lock);
527 matched = (ctl->free_space == (block_group->key.offset - used -
528 block_group->bytes_super));
529 spin_unlock(&ctl->tree_lock);
532 __btrfs_remove_free_space_cache(ctl);
533 printk(KERN_ERR "block group %llu has an wrong amount of free "
534 "space\n", block_group->key.objectid);
539 /* This cache is bogus, make sure it gets cleared */
540 spin_lock(&block_group->lock);
541 block_group->disk_cache_state = BTRFS_DC_CLEAR;
542 spin_unlock(&block_group->lock);
545 printk(KERN_ERR "btrfs: failed to load free space cache "
546 "for block group %llu\n", block_group->key.objectid);
553 int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
554 struct btrfs_free_space_ctl *ctl,
555 struct btrfs_block_group_cache *block_group,
556 struct btrfs_trans_handle *trans,
557 struct btrfs_path *path, u64 offset)
559 struct btrfs_free_space_header *header;
560 struct extent_buffer *leaf;
561 struct rb_node *node;
562 struct list_head *pos, *n;
565 struct extent_state *cached_state = NULL;
566 struct btrfs_free_cluster *cluster = NULL;
567 struct extent_io_tree *unpin = NULL;
568 struct list_head bitmap_list;
569 struct btrfs_key key;
572 u32 *crc, *checksums;
573 unsigned long first_page_offset;
574 int index = 0, num_pages = 0;
578 bool next_page = false;
579 bool out_of_space = false;
581 INIT_LIST_HEAD(&bitmap_list);
583 node = rb_first(&ctl->free_space_offset);
587 if (!i_size_read(inode))
590 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
593 /* Since the first page has all of our checksums and our generation we
594 * need to calculate the offset into the page that we can start writing
597 first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
599 filemap_write_and_wait(inode->i_mapping);
600 btrfs_wait_ordered_range(inode, inode->i_size &
601 ~(root->sectorsize - 1), (u64)-1);
603 /* make sure we don't overflow that first page */
604 if (first_page_offset + sizeof(struct btrfs_free_space_entry) >= PAGE_CACHE_SIZE) {
605 /* this is really the same as running out of space, where we also return 0 */
606 printk(KERN_CRIT "Btrfs: free space cache was too big for the crc page\n");
611 /* We need a checksum per page. */
612 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
616 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
622 /* Get the cluster for this block_group if it exists */
623 if (block_group && !list_empty(&block_group->cluster_list))
624 cluster = list_entry(block_group->cluster_list.next,
625 struct btrfs_free_cluster,
629 * We shouldn't have switched the pinned extents yet so this is the
632 unpin = root->fs_info->pinned_extents;
635 * Lock all pages first so we can lock the extent safely.
637 * NOTE: Because we hold the ref the entire time we're going to write to
638 * the page find_get_page should never fail, so we don't do a check
639 * after find_get_page at this point. Just putting this here so people
640 * know and don't freak out.
642 while (index < num_pages) {
643 page = grab_cache_page(inode->i_mapping, index);
647 for (i = 0; i < num_pages; i++) {
648 unlock_page(pages[i]);
649 page_cache_release(pages[i]);
658 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
659 0, &cached_state, GFP_NOFS);
662 * When searching for pinned extents, we need to start at our start
666 start = block_group->key.objectid;
668 /* Write out the extent entries */
670 struct btrfs_free_space_entry *entry;
672 unsigned long offset = 0;
673 unsigned long start_offset = 0;
678 start_offset = first_page_offset;
679 offset = start_offset;
682 if (index >= num_pages) {
690 entry = addr + start_offset;
692 memset(addr, 0, PAGE_CACHE_SIZE);
693 while (node && !next_page) {
694 struct btrfs_free_space *e;
696 e = rb_entry(node, struct btrfs_free_space, offset_index);
699 entry->offset = cpu_to_le64(e->offset);
700 entry->bytes = cpu_to_le64(e->bytes);
702 entry->type = BTRFS_FREE_SPACE_BITMAP;
703 list_add_tail(&e->list, &bitmap_list);
706 entry->type = BTRFS_FREE_SPACE_EXTENT;
708 node = rb_next(node);
709 if (!node && cluster) {
710 node = rb_first(&cluster->root);
713 offset += sizeof(struct btrfs_free_space_entry);
714 if (offset + sizeof(struct btrfs_free_space_entry) >=
721 * We want to add any pinned extents to our free space cache
722 * so we don't leak the space
724 while (block_group && !next_page &&
725 (start < block_group->key.objectid +
726 block_group->key.offset)) {
727 ret = find_first_extent_bit(unpin, start, &start, &end,
734 /* This pinned extent is out of our range */
735 if (start >= block_group->key.objectid +
736 block_group->key.offset)
739 len = block_group->key.objectid +
740 block_group->key.offset - start;
741 len = min(len, end + 1 - start);
744 entry->offset = cpu_to_le64(start);
745 entry->bytes = cpu_to_le64(len);
746 entry->type = BTRFS_FREE_SPACE_EXTENT;
749 offset += sizeof(struct btrfs_free_space_entry);
750 if (offset + sizeof(struct btrfs_free_space_entry) >=
756 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
757 PAGE_CACHE_SIZE - start_offset);
760 btrfs_csum_final(*crc, (char *)crc);
763 bytes += PAGE_CACHE_SIZE;
766 } while (node || next_page);
768 /* Write out the bitmaps */
769 list_for_each_safe(pos, n, &bitmap_list) {
771 struct btrfs_free_space *entry =
772 list_entry(pos, struct btrfs_free_space, list);
774 if (index >= num_pages) {
781 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
783 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
785 btrfs_csum_final(*crc, (char *)crc);
787 bytes += PAGE_CACHE_SIZE;
789 list_del_init(&entry->list);
794 btrfs_drop_pages(pages, num_pages);
795 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
796 i_size_read(inode) - 1, &cached_state,
802 /* Zero out the rest of the pages just to make sure */
803 while (index < num_pages) {
808 memset(addr, 0, PAGE_CACHE_SIZE);
810 bytes += PAGE_CACHE_SIZE;
814 /* Write the checksums and trans id to the first page */
822 memcpy(addr, checksums, sizeof(u32) * num_pages);
823 gen = addr + (sizeof(u32) * num_pages);
824 *gen = trans->transid;
828 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
829 bytes, &cached_state);
830 btrfs_drop_pages(pages, num_pages);
831 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
832 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
839 BTRFS_I(inode)->generation = trans->transid;
841 filemap_write_and_wait(inode->i_mapping);
843 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
847 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
850 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
851 EXTENT_DIRTY | EXTENT_DELALLOC |
852 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
855 leaf = path->nodes[0];
857 struct btrfs_key found_key;
858 BUG_ON(!path->slots[0]);
860 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
861 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
862 found_key.offset != offset) {
864 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
865 EXTENT_DIRTY | EXTENT_DELALLOC |
866 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
868 btrfs_release_path(path);
872 header = btrfs_item_ptr(leaf, path->slots[0],
873 struct btrfs_free_space_header);
874 btrfs_set_free_space_entries(leaf, header, entries);
875 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
876 btrfs_set_free_space_generation(leaf, header, trans->transid);
877 btrfs_mark_buffer_dirty(leaf);
878 btrfs_release_path(path);
888 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
889 BTRFS_I(inode)->generation = 0;
891 btrfs_update_inode(trans, root, inode);
895 int btrfs_write_out_cache(struct btrfs_root *root,
896 struct btrfs_trans_handle *trans,
897 struct btrfs_block_group_cache *block_group,
898 struct btrfs_path *path)
900 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
904 root = root->fs_info->tree_root;
906 spin_lock(&block_group->lock);
907 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
908 spin_unlock(&block_group->lock);
911 spin_unlock(&block_group->lock);
913 inode = lookup_free_space_inode(root, block_group, path);
917 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
918 path, block_group->key.objectid);
920 spin_lock(&block_group->lock);
921 block_group->disk_cache_state = BTRFS_DC_ERROR;
922 spin_unlock(&block_group->lock);
925 printk(KERN_ERR "btrfs: failed to write free space cace "
926 "for block group %llu\n", block_group->key.objectid);
933 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
936 BUG_ON(offset < bitmap_start);
937 offset -= bitmap_start;
938 return (unsigned long)(div_u64(offset, unit));
941 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
943 return (unsigned long)(div_u64(bytes, unit));
946 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
950 u64 bytes_per_bitmap;
952 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
953 bitmap_start = offset - ctl->start;
954 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
955 bitmap_start *= bytes_per_bitmap;
956 bitmap_start += ctl->start;
961 static int tree_insert_offset(struct rb_root *root, u64 offset,
962 struct rb_node *node, int bitmap)
964 struct rb_node **p = &root->rb_node;
965 struct rb_node *parent = NULL;
966 struct btrfs_free_space *info;
970 info = rb_entry(parent, struct btrfs_free_space, offset_index);
972 if (offset < info->offset) {
974 } else if (offset > info->offset) {
978 * we could have a bitmap entry and an extent entry
979 * share the same offset. If this is the case, we want
980 * the extent entry to always be found first if we do a
981 * linear search through the tree, since we want to have
982 * the quickest allocation time, and allocating from an
983 * extent is faster than allocating from a bitmap. So
984 * if we're inserting a bitmap and we find an entry at
985 * this offset, we want to go right, or after this entry
986 * logically. If we are inserting an extent and we've
987 * found a bitmap, we want to go left, or before
1006 rb_link_node(node, parent, p);
1007 rb_insert_color(node, root);
1013 * searches the tree for the given offset.
1015 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1016 * want a section that has at least bytes size and comes at or after the given
1019 static struct btrfs_free_space *
1020 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1021 u64 offset, int bitmap_only, int fuzzy)
1023 struct rb_node *n = ctl->free_space_offset.rb_node;
1024 struct btrfs_free_space *entry, *prev = NULL;
1026 /* find entry that is closest to the 'offset' */
1033 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1036 if (offset < entry->offset)
1038 else if (offset > entry->offset)
1051 * bitmap entry and extent entry may share same offset,
1052 * in that case, bitmap entry comes after extent entry.
1057 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1058 if (entry->offset != offset)
1061 WARN_ON(!entry->bitmap);
1064 if (entry->bitmap) {
1066 * if previous extent entry covers the offset,
1067 * we should return it instead of the bitmap entry
1069 n = &entry->offset_index;
1074 prev = rb_entry(n, struct btrfs_free_space,
1076 if (!prev->bitmap) {
1077 if (prev->offset + prev->bytes > offset)
1089 /* find last entry before the 'offset' */
1091 if (entry->offset > offset) {
1092 n = rb_prev(&entry->offset_index);
1094 entry = rb_entry(n, struct btrfs_free_space,
1096 BUG_ON(entry->offset > offset);
1105 if (entry->bitmap) {
1106 n = &entry->offset_index;
1111 prev = rb_entry(n, struct btrfs_free_space,
1113 if (!prev->bitmap) {
1114 if (prev->offset + prev->bytes > offset)
1119 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1121 } else if (entry->offset + entry->bytes > offset)
1128 if (entry->bitmap) {
1129 if (entry->offset + BITS_PER_BITMAP *
1133 if (entry->offset + entry->bytes > offset)
1137 n = rb_next(&entry->offset_index);
1140 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1146 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1147 struct btrfs_free_space *info)
1149 rb_erase(&info->offset_index, &ctl->free_space_offset);
1150 ctl->free_extents--;
1153 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1154 struct btrfs_free_space *info)
1156 __unlink_free_space(ctl, info);
1157 ctl->free_space -= info->bytes;
1160 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1161 struct btrfs_free_space *info)
1165 BUG_ON(!info->bitmap && !info->bytes);
1166 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1167 &info->offset_index, (info->bitmap != NULL));
1171 ctl->free_space += info->bytes;
1172 ctl->free_extents++;
1176 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1178 struct btrfs_block_group_cache *block_group = ctl->private;
1182 u64 size = block_group->key.offset;
1183 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1184 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1186 BUG_ON(ctl->total_bitmaps > max_bitmaps);
1189 * The goal is to keep the total amount of memory used per 1gb of space
1190 * at or below 32k, so we need to adjust how much memory we allow to be
1191 * used by extent based free space tracking
1193 if (size < 1024 * 1024 * 1024)
1194 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1196 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1197 div64_u64(size, 1024 * 1024 * 1024);
1200 * we want to account for 1 more bitmap than what we have so we can make
1201 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1202 * we add more bitmaps.
1204 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
1206 if (bitmap_bytes >= max_bytes) {
1207 ctl->extents_thresh = 0;
1212 * we want the extent entry threshold to always be at most 1/2 the maxw
1213 * bytes we can have, or whatever is less than that.
1215 extent_bytes = max_bytes - bitmap_bytes;
1216 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1218 ctl->extents_thresh =
1219 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1222 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1223 struct btrfs_free_space *info, u64 offset,
1226 unsigned long start, count;
1228 start = offset_to_bit(info->offset, ctl->unit, offset);
1229 count = bytes_to_bits(bytes, ctl->unit);
1230 BUG_ON(start + count > BITS_PER_BITMAP);
1232 bitmap_clear(info->bitmap, start, count);
1234 info->bytes -= bytes;
1235 ctl->free_space -= bytes;
1238 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1239 struct btrfs_free_space *info, u64 offset,
1242 unsigned long start, count;
1244 start = offset_to_bit(info->offset, ctl->unit, offset);
1245 count = bytes_to_bits(bytes, ctl->unit);
1246 BUG_ON(start + count > BITS_PER_BITMAP);
1248 bitmap_set(info->bitmap, start, count);
1250 info->bytes += bytes;
1251 ctl->free_space += bytes;
1254 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1255 struct btrfs_free_space *bitmap_info, u64 *offset,
1258 unsigned long found_bits = 0;
1259 unsigned long bits, i;
1260 unsigned long next_zero;
1262 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1263 max_t(u64, *offset, bitmap_info->offset));
1264 bits = bytes_to_bits(*bytes, ctl->unit);
1266 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1267 i < BITS_PER_BITMAP;
1268 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1269 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1270 BITS_PER_BITMAP, i);
1271 if ((next_zero - i) >= bits) {
1272 found_bits = next_zero - i;
1279 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1280 *bytes = (u64)(found_bits) * ctl->unit;
1287 static struct btrfs_free_space *
1288 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
1290 struct btrfs_free_space *entry;
1291 struct rb_node *node;
1294 if (!ctl->free_space_offset.rb_node)
1297 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1301 for (node = &entry->offset_index; node; node = rb_next(node)) {
1302 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1303 if (entry->bytes < *bytes)
1306 if (entry->bitmap) {
1307 ret = search_bitmap(ctl, entry, offset, bytes);
1313 *offset = entry->offset;
1314 *bytes = entry->bytes;
1321 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1322 struct btrfs_free_space *info, u64 offset)
1324 info->offset = offset_to_bitmap(ctl, offset);
1326 link_free_space(ctl, info);
1327 ctl->total_bitmaps++;
1329 ctl->op->recalc_thresholds(ctl);
1332 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1333 struct btrfs_free_space *bitmap_info)
1335 unlink_free_space(ctl, bitmap_info);
1336 kfree(bitmap_info->bitmap);
1337 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1338 ctl->total_bitmaps--;
1339 ctl->op->recalc_thresholds(ctl);
1342 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1343 struct btrfs_free_space *bitmap_info,
1344 u64 *offset, u64 *bytes)
1347 u64 search_start, search_bytes;
1351 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1354 * XXX - this can go away after a few releases.
1356 * since the only user of btrfs_remove_free_space is the tree logging
1357 * stuff, and the only way to test that is under crash conditions, we
1358 * want to have this debug stuff here just in case somethings not
1359 * working. Search the bitmap for the space we are trying to use to
1360 * make sure its actually there. If its not there then we need to stop
1361 * because something has gone wrong.
1363 search_start = *offset;
1364 search_bytes = *bytes;
1365 search_bytes = min(search_bytes, end - search_start + 1);
1366 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
1367 BUG_ON(ret < 0 || search_start != *offset);
1369 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
1370 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
1371 *bytes -= end - *offset + 1;
1373 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
1374 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
1379 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1380 if (!bitmap_info->bytes)
1381 free_bitmap(ctl, bitmap_info);
1384 * no entry after this bitmap, but we still have bytes to
1385 * remove, so something has gone wrong.
1390 bitmap_info = rb_entry(next, struct btrfs_free_space,
1394 * if the next entry isn't a bitmap we need to return to let the
1395 * extent stuff do its work.
1397 if (!bitmap_info->bitmap)
1401 * Ok the next item is a bitmap, but it may not actually hold
1402 * the information for the rest of this free space stuff, so
1403 * look for it, and if we don't find it return so we can try
1404 * everything over again.
1406 search_start = *offset;
1407 search_bytes = *bytes;
1408 ret = search_bitmap(ctl, bitmap_info, &search_start,
1410 if (ret < 0 || search_start != *offset)
1414 } else if (!bitmap_info->bytes)
1415 free_bitmap(ctl, bitmap_info);
1420 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1421 struct btrfs_free_space *info)
1423 struct btrfs_block_group_cache *block_group = ctl->private;
1426 * If we are below the extents threshold then we can add this as an
1427 * extent, and don't have to deal with the bitmap
1429 if (ctl->free_extents < ctl->extents_thresh) {
1431 * If this block group has some small extents we don't want to
1432 * use up all of our free slots in the cache with them, we want
1433 * to reserve them to larger extents, however if we have plent
1434 * of cache left then go ahead an dadd them, no sense in adding
1435 * the overhead of a bitmap if we don't have to.
1437 if (info->bytes <= block_group->sectorsize * 4) {
1438 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1446 * some block groups are so tiny they can't be enveloped by a bitmap, so
1447 * don't even bother to create a bitmap for this
1449 if (BITS_PER_BITMAP * block_group->sectorsize >
1450 block_group->key.offset)
1456 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1457 struct btrfs_free_space *info)
1459 struct btrfs_free_space *bitmap_info;
1461 u64 bytes, offset, end;
1464 bytes = info->bytes;
1465 offset = info->offset;
1467 if (!ctl->op->use_bitmap(ctl, info))
1471 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1478 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1480 if (offset >= bitmap_info->offset && offset + bytes > end) {
1481 bitmap_set_bits(ctl, bitmap_info, offset, end - offset);
1482 bytes -= end - offset;
1485 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
1486 bitmap_set_bits(ctl, bitmap_info, offset, bytes);
1499 if (info && info->bitmap) {
1500 add_new_bitmap(ctl, info, offset);
1505 spin_unlock(&ctl->tree_lock);
1507 /* no pre-allocated info, allocate a new one */
1509 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1512 spin_lock(&ctl->tree_lock);
1518 /* allocate the bitmap */
1519 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1520 spin_lock(&ctl->tree_lock);
1521 if (!info->bitmap) {
1531 kfree(info->bitmap);
1532 kmem_cache_free(btrfs_free_space_cachep, info);
1538 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
1539 struct btrfs_free_space *info, bool update_stat)
1541 struct btrfs_free_space *left_info;
1542 struct btrfs_free_space *right_info;
1543 bool merged = false;
1544 u64 offset = info->offset;
1545 u64 bytes = info->bytes;
1548 * first we want to see if there is free space adjacent to the range we
1549 * are adding, if there is remove that struct and add a new one to
1550 * cover the entire range
1552 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
1553 if (right_info && rb_prev(&right_info->offset_index))
1554 left_info = rb_entry(rb_prev(&right_info->offset_index),
1555 struct btrfs_free_space, offset_index);
1557 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
1559 if (right_info && !right_info->bitmap) {
1561 unlink_free_space(ctl, right_info);
1563 __unlink_free_space(ctl, right_info);
1564 info->bytes += right_info->bytes;
1565 kmem_cache_free(btrfs_free_space_cachep, right_info);
1569 if (left_info && !left_info->bitmap &&
1570 left_info->offset + left_info->bytes == offset) {
1572 unlink_free_space(ctl, left_info);
1574 __unlink_free_space(ctl, left_info);
1575 info->offset = left_info->offset;
1576 info->bytes += left_info->bytes;
1577 kmem_cache_free(btrfs_free_space_cachep, left_info);
1584 int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1585 u64 offset, u64 bytes)
1587 struct btrfs_free_space *info;
1590 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
1594 info->offset = offset;
1595 info->bytes = bytes;
1597 spin_lock(&ctl->tree_lock);
1599 if (try_merge_free_space(ctl, info, true))
1603 * There was no extent directly to the left or right of this new
1604 * extent then we know we're going to have to allocate a new extent, so
1605 * before we do that see if we need to drop this into a bitmap
1607 ret = insert_into_bitmap(ctl, info);
1615 ret = link_free_space(ctl, info);
1617 kmem_cache_free(btrfs_free_space_cachep, info);
1619 spin_unlock(&ctl->tree_lock);
1622 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
1623 BUG_ON(ret == -EEXIST);
1629 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1630 u64 offset, u64 bytes)
1632 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1633 struct btrfs_free_space *info;
1634 struct btrfs_free_space *next_info = NULL;
1637 spin_lock(&ctl->tree_lock);
1640 info = tree_search_offset(ctl, offset, 0, 0);
1643 * oops didn't find an extent that matched the space we wanted
1644 * to remove, look for a bitmap instead
1646 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1654 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1656 next_info = rb_entry(rb_next(&info->offset_index),
1657 struct btrfs_free_space,
1660 if (next_info->bitmap)
1661 end = next_info->offset +
1662 BITS_PER_BITMAP * ctl->unit - 1;
1664 end = next_info->offset + next_info->bytes;
1666 if (next_info->bytes < bytes ||
1667 next_info->offset > offset || offset > end) {
1668 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1669 " trying to use %llu\n",
1670 (unsigned long long)info->offset,
1671 (unsigned long long)info->bytes,
1672 (unsigned long long)bytes);
1681 if (info->bytes == bytes) {
1682 unlink_free_space(ctl, info);
1684 kfree(info->bitmap);
1685 ctl->total_bitmaps--;
1687 kmem_cache_free(btrfs_free_space_cachep, info);
1691 if (!info->bitmap && info->offset == offset) {
1692 unlink_free_space(ctl, info);
1693 info->offset += bytes;
1694 info->bytes -= bytes;
1695 link_free_space(ctl, info);
1699 if (!info->bitmap && info->offset <= offset &&
1700 info->offset + info->bytes >= offset + bytes) {
1701 u64 old_start = info->offset;
1703 * we're freeing space in the middle of the info,
1704 * this can happen during tree log replay
1706 * first unlink the old info and then
1707 * insert it again after the hole we're creating
1709 unlink_free_space(ctl, info);
1710 if (offset + bytes < info->offset + info->bytes) {
1711 u64 old_end = info->offset + info->bytes;
1713 info->offset = offset + bytes;
1714 info->bytes = old_end - info->offset;
1715 ret = link_free_space(ctl, info);
1720 /* the hole we're creating ends at the end
1721 * of the info struct, just free the info
1723 kmem_cache_free(btrfs_free_space_cachep, info);
1725 spin_unlock(&ctl->tree_lock);
1727 /* step two, insert a new info struct to cover
1728 * anything before the hole
1730 ret = btrfs_add_free_space(block_group, old_start,
1731 offset - old_start);
1736 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
1741 spin_unlock(&ctl->tree_lock);
1746 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1749 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1750 struct btrfs_free_space *info;
1754 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
1755 info = rb_entry(n, struct btrfs_free_space, offset_index);
1756 if (info->bytes >= bytes)
1758 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
1759 (unsigned long long)info->offset,
1760 (unsigned long long)info->bytes,
1761 (info->bitmap) ? "yes" : "no");
1763 printk(KERN_INFO "block group has cluster?: %s\n",
1764 list_empty(&block_group->cluster_list) ? "no" : "yes");
1765 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1769 static struct btrfs_free_space_op free_space_op = {
1770 .recalc_thresholds = recalculate_thresholds,
1771 .use_bitmap = use_bitmap,
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 unlink_free_space(ctl, info);
1846 kfree(info->bitmap);
1847 kmem_cache_free(btrfs_free_space_cachep, info);
1848 if (need_resched()) {
1849 spin_unlock(&ctl->tree_lock);
1851 spin_lock(&ctl->tree_lock);
1856 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1858 spin_lock(&ctl->tree_lock);
1859 __btrfs_remove_free_space_cache_locked(ctl);
1860 spin_unlock(&ctl->tree_lock);
1863 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1865 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1866 struct btrfs_free_cluster *cluster;
1867 struct list_head *head;
1869 spin_lock(&ctl->tree_lock);
1870 while ((head = block_group->cluster_list.next) !=
1871 &block_group->cluster_list) {
1872 cluster = list_entry(head, struct btrfs_free_cluster,
1875 WARN_ON(cluster->block_group != block_group);
1876 __btrfs_return_cluster_to_free_space(block_group, cluster);
1877 if (need_resched()) {
1878 spin_unlock(&ctl->tree_lock);
1880 spin_lock(&ctl->tree_lock);
1883 __btrfs_remove_free_space_cache_locked(ctl);
1884 spin_unlock(&ctl->tree_lock);
1888 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1889 u64 offset, u64 bytes, u64 empty_size)
1891 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1892 struct btrfs_free_space *entry = NULL;
1893 u64 bytes_search = bytes + empty_size;
1896 spin_lock(&ctl->tree_lock);
1897 entry = find_free_space(ctl, &offset, &bytes_search);
1902 if (entry->bitmap) {
1903 bitmap_clear_bits(ctl, entry, offset, bytes);
1905 free_bitmap(ctl, entry);
1907 unlink_free_space(ctl, entry);
1908 entry->offset += bytes;
1909 entry->bytes -= bytes;
1911 kmem_cache_free(btrfs_free_space_cachep, entry);
1913 link_free_space(ctl, entry);
1917 spin_unlock(&ctl->tree_lock);
1923 * given a cluster, put all of its extents back into the free space
1924 * cache. If a block group is passed, this function will only free
1925 * a cluster that belongs to the passed block group.
1927 * Otherwise, it'll get a reference on the block group pointed to by the
1928 * cluster and remove the cluster from it.
1930 int btrfs_return_cluster_to_free_space(
1931 struct btrfs_block_group_cache *block_group,
1932 struct btrfs_free_cluster *cluster)
1934 struct btrfs_free_space_ctl *ctl;
1937 /* first, get a safe pointer to the block group */
1938 spin_lock(&cluster->lock);
1940 block_group = cluster->block_group;
1942 spin_unlock(&cluster->lock);
1945 } else if (cluster->block_group != block_group) {
1946 /* someone else has already freed it don't redo their work */
1947 spin_unlock(&cluster->lock);
1950 atomic_inc(&block_group->count);
1951 spin_unlock(&cluster->lock);
1953 ctl = block_group->free_space_ctl;
1955 /* now return any extents the cluster had on it */
1956 spin_lock(&ctl->tree_lock);
1957 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1958 spin_unlock(&ctl->tree_lock);
1960 /* finally drop our ref */
1961 btrfs_put_block_group(block_group);
1965 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1966 struct btrfs_free_cluster *cluster,
1967 struct btrfs_free_space *entry,
1968 u64 bytes, u64 min_start)
1970 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1972 u64 search_start = cluster->window_start;
1973 u64 search_bytes = bytes;
1976 search_start = min_start;
1977 search_bytes = bytes;
1979 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
1984 bitmap_clear_bits(ctl, entry, ret, bytes);
1990 * given a cluster, try to allocate 'bytes' from it, returns 0
1991 * if it couldn't find anything suitably large, or a logical disk offset
1992 * if things worked out
1994 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1995 struct btrfs_free_cluster *cluster, u64 bytes,
1998 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1999 struct btrfs_free_space *entry = NULL;
2000 struct rb_node *node;
2003 spin_lock(&cluster->lock);
2004 if (bytes > cluster->max_size)
2007 if (cluster->block_group != block_group)
2010 node = rb_first(&cluster->root);
2014 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2016 if (entry->bytes < bytes ||
2017 (!entry->bitmap && entry->offset < min_start)) {
2018 node = rb_next(&entry->offset_index);
2021 entry = rb_entry(node, struct btrfs_free_space,
2026 if (entry->bitmap) {
2027 ret = btrfs_alloc_from_bitmap(block_group,
2028 cluster, entry, bytes,
2031 node = rb_next(&entry->offset_index);
2034 entry = rb_entry(node, struct btrfs_free_space,
2040 ret = entry->offset;
2042 entry->offset += bytes;
2043 entry->bytes -= bytes;
2046 if (entry->bytes == 0)
2047 rb_erase(&entry->offset_index, &cluster->root);
2051 spin_unlock(&cluster->lock);
2056 spin_lock(&ctl->tree_lock);
2058 ctl->free_space -= bytes;
2059 if (entry->bytes == 0) {
2060 ctl->free_extents--;
2061 if (entry->bitmap) {
2062 kfree(entry->bitmap);
2063 ctl->total_bitmaps--;
2064 ctl->op->recalc_thresholds(ctl);
2066 kmem_cache_free(btrfs_free_space_cachep, entry);
2069 spin_unlock(&ctl->tree_lock);
2074 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2075 struct btrfs_free_space *entry,
2076 struct btrfs_free_cluster *cluster,
2077 u64 offset, u64 bytes, u64 min_bytes)
2079 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2080 unsigned long next_zero;
2082 unsigned long search_bits;
2083 unsigned long total_bits;
2084 unsigned long found_bits;
2085 unsigned long start = 0;
2086 unsigned long total_found = 0;
2090 i = offset_to_bit(entry->offset, block_group->sectorsize,
2091 max_t(u64, offset, entry->offset));
2092 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2093 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
2097 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2098 i < BITS_PER_BITMAP;
2099 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2100 next_zero = find_next_zero_bit(entry->bitmap,
2101 BITS_PER_BITMAP, i);
2102 if (next_zero - i >= search_bits) {
2103 found_bits = next_zero - i;
2117 total_found += found_bits;
2119 if (cluster->max_size < found_bits * block_group->sectorsize)
2120 cluster->max_size = found_bits * block_group->sectorsize;
2122 if (total_found < total_bits) {
2123 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2124 if (i - start > total_bits * 2) {
2126 cluster->max_size = 0;
2132 cluster->window_start = start * block_group->sectorsize +
2134 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2135 ret = tree_insert_offset(&cluster->root, entry->offset,
2136 &entry->offset_index, 1);
2143 * This searches the block group for just extents to fill the cluster with.
2145 static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2146 struct btrfs_free_cluster *cluster,
2147 struct list_head *bitmaps,
2148 u64 offset, u64 bytes, u64 min_bytes)
2150 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2151 struct btrfs_free_space *first = NULL;
2152 struct btrfs_free_space *entry = NULL;
2153 struct btrfs_free_space *prev = NULL;
2154 struct btrfs_free_space *last;
2155 struct rb_node *node;
2159 u64 max_gap = 128 * 1024;
2161 entry = tree_search_offset(ctl, offset, 0, 1);
2166 * We don't want bitmaps, so just move along until we find a normal
2169 while (entry->bitmap) {
2170 if (list_empty(&entry->list))
2171 list_add_tail(&entry->list, bitmaps);
2172 node = rb_next(&entry->offset_index);
2175 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2178 window_start = entry->offset;
2179 window_free = entry->bytes;
2180 max_extent = entry->bytes;
2185 while (window_free <= min_bytes) {
2186 node = rb_next(&entry->offset_index);
2189 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2191 if (entry->bitmap) {
2192 if (list_empty(&entry->list))
2193 list_add_tail(&entry->list, bitmaps);
2198 * we haven't filled the empty size and the window is
2199 * very large. reset and try again
2201 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2202 entry->offset - window_start > (min_bytes * 2)) {
2204 window_start = entry->offset;
2205 window_free = entry->bytes;
2207 max_extent = entry->bytes;
2210 window_free += entry->bytes;
2211 if (entry->bytes > max_extent)
2212 max_extent = entry->bytes;
2217 cluster->window_start = first->offset;
2219 node = &first->offset_index;
2222 * now we've found our entries, pull them out of the free space
2223 * cache and put them into the cluster rbtree
2228 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2229 node = rb_next(&entry->offset_index);
2233 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2234 ret = tree_insert_offset(&cluster->root, entry->offset,
2235 &entry->offset_index, 0);
2237 } while (node && entry != last);
2239 cluster->max_size = max_extent;
2245 * This specifically looks for bitmaps that may work in the cluster, we assume
2246 * that we have already failed to find extents that will work.
2248 static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2249 struct btrfs_free_cluster *cluster,
2250 struct list_head *bitmaps,
2251 u64 offset, u64 bytes, u64 min_bytes)
2253 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2254 struct btrfs_free_space *entry;
2255 struct rb_node *node;
2258 if (ctl->total_bitmaps == 0)
2262 * First check our cached list of bitmaps and see if there is an entry
2263 * here that will work.
2265 list_for_each_entry(entry, bitmaps, list) {
2266 if (entry->bytes < min_bytes)
2268 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2275 * If we do have entries on our list and we are here then we didn't find
2276 * anything, so go ahead and get the next entry after the last entry in
2277 * this list and start the search from there.
2279 if (!list_empty(bitmaps)) {
2280 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2282 node = rb_next(&entry->offset_index);
2285 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2289 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
2294 node = &entry->offset_index;
2296 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2297 node = rb_next(&entry->offset_index);
2300 if (entry->bytes < min_bytes)
2302 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2304 } while (ret && node);
2310 * here we try to find a cluster of blocks in a block group. The goal
2311 * is to find at least bytes free and up to empty_size + bytes free.
2312 * We might not find them all in one contiguous area.
2314 * returns zero and sets up cluster if things worked out, otherwise
2315 * it returns -enospc
2317 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2318 struct btrfs_root *root,
2319 struct btrfs_block_group_cache *block_group,
2320 struct btrfs_free_cluster *cluster,
2321 u64 offset, u64 bytes, u64 empty_size)
2323 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2324 struct list_head bitmaps;
2325 struct btrfs_free_space *entry, *tmp;
2329 /* for metadata, allow allocates with more holes */
2330 if (btrfs_test_opt(root, SSD_SPREAD)) {
2331 min_bytes = bytes + empty_size;
2332 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
2334 * we want to do larger allocations when we are
2335 * flushing out the delayed refs, it helps prevent
2336 * making more work as we go along.
2338 if (trans->transaction->delayed_refs.flushing)
2339 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2341 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2343 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2345 spin_lock(&ctl->tree_lock);
2348 * If we know we don't have enough space to make a cluster don't even
2349 * bother doing all the work to try and find one.
2351 if (ctl->free_space < min_bytes) {
2352 spin_unlock(&ctl->tree_lock);
2356 spin_lock(&cluster->lock);
2358 /* someone already found a cluster, hooray */
2359 if (cluster->block_group) {
2364 INIT_LIST_HEAD(&bitmaps);
2365 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2368 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2369 offset, bytes, min_bytes);
2371 /* Clear our temporary list */
2372 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2373 list_del_init(&entry->list);
2376 atomic_inc(&block_group->count);
2377 list_add_tail(&cluster->block_group_list,
2378 &block_group->cluster_list);
2379 cluster->block_group = block_group;
2382 spin_unlock(&cluster->lock);
2383 spin_unlock(&ctl->tree_lock);
2389 * simple code to zero out a cluster
2391 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2393 spin_lock_init(&cluster->lock);
2394 spin_lock_init(&cluster->refill_lock);
2395 cluster->root = RB_ROOT;
2396 cluster->max_size = 0;
2397 INIT_LIST_HEAD(&cluster->block_group_list);
2398 cluster->block_group = NULL;
2401 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2402 u64 *trimmed, u64 start, u64 end, u64 minlen)
2404 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2405 struct btrfs_free_space *entry = NULL;
2406 struct btrfs_fs_info *fs_info = block_group->fs_info;
2408 u64 actually_trimmed;
2413 while (start < end) {
2414 spin_lock(&ctl->tree_lock);
2416 if (ctl->free_space < minlen) {
2417 spin_unlock(&ctl->tree_lock);
2421 entry = tree_search_offset(ctl, start, 0, 1);
2423 entry = tree_search_offset(ctl,
2424 offset_to_bitmap(ctl, start),
2427 if (!entry || entry->offset >= end) {
2428 spin_unlock(&ctl->tree_lock);
2432 if (entry->bitmap) {
2433 ret = search_bitmap(ctl, entry, &start, &bytes);
2436 spin_unlock(&ctl->tree_lock);
2439 bytes = min(bytes, end - start);
2440 bitmap_clear_bits(ctl, entry, start, bytes);
2441 if (entry->bytes == 0)
2442 free_bitmap(ctl, entry);
2444 start = entry->offset + BITS_PER_BITMAP *
2445 block_group->sectorsize;
2446 spin_unlock(&ctl->tree_lock);
2451 start = entry->offset;
2452 bytes = min(entry->bytes, end - start);
2453 unlink_free_space(ctl, entry);
2454 kmem_cache_free(btrfs_free_space_cachep, entry);
2457 spin_unlock(&ctl->tree_lock);
2459 if (bytes >= minlen) {
2461 update_ret = btrfs_update_reserved_bytes(block_group,
2464 ret = btrfs_error_discard_extent(fs_info->extent_root,
2469 btrfs_add_free_space(block_group, start, bytes);
2471 btrfs_update_reserved_bytes(block_group,
2476 *trimmed += actually_trimmed;
2481 if (fatal_signal_pending(current)) {
2493 * Find the left-most item in the cache tree, and then return the
2494 * smallest inode number in the item.
2496 * Note: the returned inode number may not be the smallest one in
2497 * the tree, if the left-most item is a bitmap.
2499 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2501 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2502 struct btrfs_free_space *entry = NULL;
2505 spin_lock(&ctl->tree_lock);
2507 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2510 entry = rb_entry(rb_first(&ctl->free_space_offset),
2511 struct btrfs_free_space, offset_index);
2513 if (!entry->bitmap) {
2514 ino = entry->offset;
2516 unlink_free_space(ctl, entry);
2520 kmem_cache_free(btrfs_free_space_cachep, entry);
2522 link_free_space(ctl, entry);
2528 ret = search_bitmap(ctl, entry, &offset, &count);
2532 bitmap_clear_bits(ctl, entry, offset, 1);
2533 if (entry->bytes == 0)
2534 free_bitmap(ctl, entry);
2537 spin_unlock(&ctl->tree_lock);
2542 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2543 struct btrfs_path *path)
2545 struct inode *inode = NULL;
2547 spin_lock(&root->cache_lock);
2548 if (root->cache_inode)
2549 inode = igrab(root->cache_inode);
2550 spin_unlock(&root->cache_lock);
2554 inode = __lookup_free_space_inode(root, path, 0);
2558 spin_lock(&root->cache_lock);
2559 if (!btrfs_fs_closing(root->fs_info))
2560 root->cache_inode = igrab(inode);
2561 spin_unlock(&root->cache_lock);
2566 int create_free_ino_inode(struct btrfs_root *root,
2567 struct btrfs_trans_handle *trans,
2568 struct btrfs_path *path)
2570 return __create_free_space_inode(root, trans, path,
2571 BTRFS_FREE_INO_OBJECTID, 0);
2574 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2576 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2577 struct btrfs_path *path;
2578 struct inode *inode;
2580 u64 root_gen = btrfs_root_generation(&root->root_item);
2582 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2586 * If we're unmounting then just return, since this does a search on the
2587 * normal root and not the commit root and we could deadlock.
2589 if (btrfs_fs_closing(fs_info))
2592 path = btrfs_alloc_path();
2596 inode = lookup_free_ino_inode(root, path);
2600 if (root_gen != BTRFS_I(inode)->generation)
2603 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2606 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2607 "root %llu\n", root->root_key.objectid);
2611 btrfs_free_path(path);
2615 int btrfs_write_out_ino_cache(struct btrfs_root *root,
2616 struct btrfs_trans_handle *trans,
2617 struct btrfs_path *path)
2619 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2620 struct inode *inode;
2623 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2626 inode = lookup_free_ino_inode(root, path);
2630 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
2632 printk(KERN_ERR "btrfs: failed to write free ino cache "
2633 "for root %llu\n", root->root_key.objectid);