2 * Copyright (C) 2008 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/math64.h>
24 #include "free-space-cache.h"
25 #include "transaction.h"
28 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
29 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
31 struct inode *lookup_free_space_inode(struct btrfs_root *root,
32 struct btrfs_block_group_cache
33 *block_group, struct btrfs_path *path)
36 struct btrfs_key location;
37 struct btrfs_disk_key disk_key;
38 struct btrfs_free_space_header *header;
39 struct extent_buffer *leaf;
40 struct inode *inode = NULL;
43 spin_lock(&block_group->lock);
44 if (block_group->inode)
45 inode = igrab(block_group->inode);
46 spin_unlock(&block_group->lock);
50 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
51 key.offset = block_group->key.objectid;
54 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
58 btrfs_release_path(root, path);
59 return ERR_PTR(-ENOENT);
62 leaf = path->nodes[0];
63 header = btrfs_item_ptr(leaf, path->slots[0],
64 struct btrfs_free_space_header);
65 btrfs_free_space_key(leaf, header, &disk_key);
66 btrfs_disk_key_to_cpu(&location, &disk_key);
67 btrfs_release_path(root, path);
69 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
71 return ERR_PTR(-ENOENT);
74 if (is_bad_inode(inode)) {
76 return ERR_PTR(-ENOENT);
79 spin_lock(&block_group->lock);
80 if (!root->fs_info->closing) {
81 block_group->inode = igrab(inode);
82 block_group->iref = 1;
84 spin_unlock(&block_group->lock);
89 int create_free_space_inode(struct btrfs_root *root,
90 struct btrfs_trans_handle *trans,
91 struct btrfs_block_group_cache *block_group,
92 struct btrfs_path *path)
95 struct btrfs_disk_key disk_key;
96 struct btrfs_free_space_header *header;
97 struct btrfs_inode_item *inode_item;
98 struct extent_buffer *leaf;
102 ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
106 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
110 leaf = path->nodes[0];
111 inode_item = btrfs_item_ptr(leaf, path->slots[0],
112 struct btrfs_inode_item);
113 btrfs_item_key(leaf, &disk_key, path->slots[0]);
114 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
115 sizeof(*inode_item));
116 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
117 btrfs_set_inode_size(leaf, inode_item, 0);
118 btrfs_set_inode_nbytes(leaf, inode_item, 0);
119 btrfs_set_inode_uid(leaf, inode_item, 0);
120 btrfs_set_inode_gid(leaf, inode_item, 0);
121 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
122 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
123 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
124 btrfs_set_inode_nlink(leaf, inode_item, 1);
125 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
126 btrfs_set_inode_block_group(leaf, inode_item,
127 block_group->key.objectid);
128 btrfs_mark_buffer_dirty(leaf);
129 btrfs_release_path(root, path);
131 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
132 key.offset = block_group->key.objectid;
135 ret = btrfs_insert_empty_item(trans, root, path, &key,
136 sizeof(struct btrfs_free_space_header));
138 btrfs_release_path(root, path);
141 leaf = path->nodes[0];
142 header = btrfs_item_ptr(leaf, path->slots[0],
143 struct btrfs_free_space_header);
144 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
145 btrfs_set_free_space_key(leaf, header, &disk_key);
146 btrfs_mark_buffer_dirty(leaf);
147 btrfs_release_path(root, path);
152 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
153 struct btrfs_trans_handle *trans,
154 struct btrfs_path *path,
160 trans->block_rsv = root->orphan_block_rsv;
161 ret = btrfs_block_rsv_check(trans, root,
162 root->orphan_block_rsv,
167 oldsize = i_size_read(inode);
168 btrfs_i_size_write(inode, 0);
169 truncate_pagecache(inode, oldsize, 0);
172 * We don't need an orphan item because truncating the free space cache
173 * will never be split across transactions.
175 ret = btrfs_truncate_inode_items(trans, root, inode,
176 0, BTRFS_EXTENT_DATA_KEY);
182 return btrfs_update_inode(trans, root, inode);
185 static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
188 BUG_ON(offset < bitmap_start);
189 offset -= bitmap_start;
190 return (unsigned long)(div64_u64(offset, sectorsize));
193 static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
195 return (unsigned long)(div64_u64(bytes, sectorsize));
198 static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
202 u64 bytes_per_bitmap;
204 bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
205 bitmap_start = offset - block_group->key.objectid;
206 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
207 bitmap_start *= bytes_per_bitmap;
208 bitmap_start += block_group->key.objectid;
213 static int tree_insert_offset(struct rb_root *root, u64 offset,
214 struct rb_node *node, int bitmap)
216 struct rb_node **p = &root->rb_node;
217 struct rb_node *parent = NULL;
218 struct btrfs_free_space *info;
222 info = rb_entry(parent, struct btrfs_free_space, offset_index);
224 if (offset < info->offset) {
226 } else if (offset > info->offset) {
230 * we could have a bitmap entry and an extent entry
231 * share the same offset. If this is the case, we want
232 * the extent entry to always be found first if we do a
233 * linear search through the tree, since we want to have
234 * the quickest allocation time, and allocating from an
235 * extent is faster than allocating from a bitmap. So
236 * if we're inserting a bitmap and we find an entry at
237 * this offset, we want to go right, or after this entry
238 * logically. If we are inserting an extent and we've
239 * found a bitmap, we want to go left, or before
243 WARN_ON(info->bitmap);
246 WARN_ON(!info->bitmap);
252 rb_link_node(node, parent, p);
253 rb_insert_color(node, root);
259 * searches the tree for the given offset.
261 * fuzzy - If this is set, then we are trying to make an allocation, and we just
262 * want a section that has at least bytes size and comes at or after the given
265 static struct btrfs_free_space *
266 tree_search_offset(struct btrfs_block_group_cache *block_group,
267 u64 offset, int bitmap_only, int fuzzy)
269 struct rb_node *n = block_group->free_space_offset.rb_node;
270 struct btrfs_free_space *entry, *prev = NULL;
272 /* find entry that is closest to the 'offset' */
279 entry = rb_entry(n, struct btrfs_free_space, offset_index);
282 if (offset < entry->offset)
284 else if (offset > entry->offset)
297 * bitmap entry and extent entry may share same offset,
298 * in that case, bitmap entry comes after extent entry.
303 entry = rb_entry(n, struct btrfs_free_space, offset_index);
304 if (entry->offset != offset)
307 WARN_ON(!entry->bitmap);
312 * if previous extent entry covers the offset,
313 * we should return it instead of the bitmap entry
315 n = &entry->offset_index;
320 prev = rb_entry(n, struct btrfs_free_space,
323 if (prev->offset + prev->bytes > offset)
335 /* find last entry before the 'offset' */
337 if (entry->offset > offset) {
338 n = rb_prev(&entry->offset_index);
340 entry = rb_entry(n, struct btrfs_free_space,
342 BUG_ON(entry->offset > offset);
352 n = &entry->offset_index;
357 prev = rb_entry(n, struct btrfs_free_space,
360 if (prev->offset + prev->bytes > offset)
365 if (entry->offset + BITS_PER_BITMAP *
366 block_group->sectorsize > offset)
368 } else if (entry->offset + entry->bytes > offset)
376 if (entry->offset + BITS_PER_BITMAP *
377 block_group->sectorsize > offset)
380 if (entry->offset + entry->bytes > offset)
384 n = rb_next(&entry->offset_index);
387 entry = rb_entry(n, struct btrfs_free_space, offset_index);
392 static void unlink_free_space(struct btrfs_block_group_cache *block_group,
393 struct btrfs_free_space *info)
395 rb_erase(&info->offset_index, &block_group->free_space_offset);
396 block_group->free_extents--;
397 block_group->free_space -= info->bytes;
400 static int link_free_space(struct btrfs_block_group_cache *block_group,
401 struct btrfs_free_space *info)
405 BUG_ON(!info->bitmap && !info->bytes);
406 ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
407 &info->offset_index, (info->bitmap != NULL));
411 block_group->free_space += info->bytes;
412 block_group->free_extents++;
416 static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
423 * The goal is to keep the total amount of memory used per 1gb of space
424 * at or below 32k, so we need to adjust how much memory we allow to be
425 * used by extent based free space tracking
427 max_bytes = MAX_CACHE_BYTES_PER_GIG *
428 (div64_u64(block_group->key.offset, 1024 * 1024 * 1024));
431 * we want to account for 1 more bitmap than what we have so we can make
432 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
433 * we add more bitmaps.
435 bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
437 if (bitmap_bytes >= max_bytes) {
438 block_group->extents_thresh = 0;
443 * we want the extent entry threshold to always be at most 1/2 the maxw
444 * bytes we can have, or whatever is less than that.
446 extent_bytes = max_bytes - bitmap_bytes;
447 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
449 block_group->extents_thresh =
450 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
453 static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
454 struct btrfs_free_space *info, u64 offset,
457 unsigned long start, end;
460 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
461 end = start + bytes_to_bits(bytes, block_group->sectorsize);
462 BUG_ON(end > BITS_PER_BITMAP);
464 for (i = start; i < end; i++)
465 clear_bit(i, info->bitmap);
467 info->bytes -= bytes;
468 block_group->free_space -= bytes;
471 static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
472 struct btrfs_free_space *info, u64 offset,
475 unsigned long start, end;
478 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
479 end = start + bytes_to_bits(bytes, block_group->sectorsize);
480 BUG_ON(end > BITS_PER_BITMAP);
482 for (i = start; i < end; i++)
483 set_bit(i, info->bitmap);
485 info->bytes += bytes;
486 block_group->free_space += bytes;
489 static int search_bitmap(struct btrfs_block_group_cache *block_group,
490 struct btrfs_free_space *bitmap_info, u64 *offset,
493 unsigned long found_bits = 0;
494 unsigned long bits, i;
495 unsigned long next_zero;
497 i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
498 max_t(u64, *offset, bitmap_info->offset));
499 bits = bytes_to_bits(*bytes, block_group->sectorsize);
501 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
503 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
504 next_zero = find_next_zero_bit(bitmap_info->bitmap,
506 if ((next_zero - i) >= bits) {
507 found_bits = next_zero - i;
514 *offset = (u64)(i * block_group->sectorsize) +
516 *bytes = (u64)(found_bits) * block_group->sectorsize;
523 static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
524 *block_group, u64 *offset,
525 u64 *bytes, int debug)
527 struct btrfs_free_space *entry;
528 struct rb_node *node;
531 if (!block_group->free_space_offset.rb_node)
534 entry = tree_search_offset(block_group,
535 offset_to_bitmap(block_group, *offset),
540 for (node = &entry->offset_index; node; node = rb_next(node)) {
541 entry = rb_entry(node, struct btrfs_free_space, offset_index);
542 if (entry->bytes < *bytes)
546 ret = search_bitmap(block_group, entry, offset, bytes);
552 *offset = entry->offset;
553 *bytes = entry->bytes;
560 static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
561 struct btrfs_free_space *info, u64 offset)
563 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
564 int max_bitmaps = (int)div64_u64(block_group->key.offset +
565 bytes_per_bg - 1, bytes_per_bg);
566 BUG_ON(block_group->total_bitmaps >= max_bitmaps);
568 info->offset = offset_to_bitmap(block_group, offset);
570 link_free_space(block_group, info);
571 block_group->total_bitmaps++;
573 recalculate_thresholds(block_group);
576 static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
577 struct btrfs_free_space *bitmap_info,
578 u64 *offset, u64 *bytes)
581 u64 search_start, search_bytes;
585 end = bitmap_info->offset +
586 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
589 * XXX - this can go away after a few releases.
591 * since the only user of btrfs_remove_free_space is the tree logging
592 * stuff, and the only way to test that is under crash conditions, we
593 * want to have this debug stuff here just in case somethings not
594 * working. Search the bitmap for the space we are trying to use to
595 * make sure its actually there. If its not there then we need to stop
596 * because something has gone wrong.
598 search_start = *offset;
599 search_bytes = *bytes;
600 ret = search_bitmap(block_group, bitmap_info, &search_start,
602 BUG_ON(ret < 0 || search_start != *offset);
604 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
605 bitmap_clear_bits(block_group, bitmap_info, *offset,
607 *bytes -= end - *offset + 1;
609 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
610 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
615 struct rb_node *next = rb_next(&bitmap_info->offset_index);
616 if (!bitmap_info->bytes) {
617 unlink_free_space(block_group, bitmap_info);
618 kfree(bitmap_info->bitmap);
620 block_group->total_bitmaps--;
621 recalculate_thresholds(block_group);
625 * no entry after this bitmap, but we still have bytes to
626 * remove, so something has gone wrong.
631 bitmap_info = rb_entry(next, struct btrfs_free_space,
635 * if the next entry isn't a bitmap we need to return to let the
636 * extent stuff do its work.
638 if (!bitmap_info->bitmap)
642 * Ok the next item is a bitmap, but it may not actually hold
643 * the information for the rest of this free space stuff, so
644 * look for it, and if we don't find it return so we can try
645 * everything over again.
647 search_start = *offset;
648 search_bytes = *bytes;
649 ret = search_bitmap(block_group, bitmap_info, &search_start,
651 if (ret < 0 || search_start != *offset)
655 } else if (!bitmap_info->bytes) {
656 unlink_free_space(block_group, bitmap_info);
657 kfree(bitmap_info->bitmap);
659 block_group->total_bitmaps--;
660 recalculate_thresholds(block_group);
666 static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
667 struct btrfs_free_space *info)
669 struct btrfs_free_space *bitmap_info;
671 u64 bytes, offset, end;
675 * If we are below the extents threshold then we can add this as an
676 * extent, and don't have to deal with the bitmap
678 if (block_group->free_extents < block_group->extents_thresh &&
679 info->bytes > block_group->sectorsize * 4)
683 * some block groups are so tiny they can't be enveloped by a bitmap, so
684 * don't even bother to create a bitmap for this
686 if (BITS_PER_BITMAP * block_group->sectorsize >
687 block_group->key.offset)
691 offset = info->offset;
694 bitmap_info = tree_search_offset(block_group,
695 offset_to_bitmap(block_group, offset),
702 end = bitmap_info->offset +
703 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
705 if (offset >= bitmap_info->offset && offset + bytes > end) {
706 bitmap_set_bits(block_group, bitmap_info, offset,
708 bytes -= end - offset;
711 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
712 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
725 if (info && info->bitmap) {
726 add_new_bitmap(block_group, info, offset);
731 spin_unlock(&block_group->tree_lock);
733 /* no pre-allocated info, allocate a new one */
735 info = kzalloc(sizeof(struct btrfs_free_space),
738 spin_lock(&block_group->tree_lock);
744 /* allocate the bitmap */
745 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
746 spin_lock(&block_group->tree_lock);
764 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
765 u64 offset, u64 bytes)
767 struct btrfs_free_space *right_info = NULL;
768 struct btrfs_free_space *left_info = NULL;
769 struct btrfs_free_space *info = NULL;
772 info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
776 info->offset = offset;
779 spin_lock(&block_group->tree_lock);
782 * first we want to see if there is free space adjacent to the range we
783 * are adding, if there is remove that struct and add a new one to
784 * cover the entire range
786 right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
787 if (right_info && rb_prev(&right_info->offset_index))
788 left_info = rb_entry(rb_prev(&right_info->offset_index),
789 struct btrfs_free_space, offset_index);
791 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
794 * If there was no extent directly to the left or right of this new
795 * extent then we know we're going to have to allocate a new extent, so
796 * before we do that see if we need to drop this into a bitmap
798 if ((!left_info || left_info->bitmap) &&
799 (!right_info || right_info->bitmap)) {
800 ret = insert_into_bitmap(block_group, info);
810 if (right_info && !right_info->bitmap) {
811 unlink_free_space(block_group, right_info);
812 info->bytes += right_info->bytes;
816 if (left_info && !left_info->bitmap &&
817 left_info->offset + left_info->bytes == offset) {
818 unlink_free_space(block_group, left_info);
819 info->offset = left_info->offset;
820 info->bytes += left_info->bytes;
824 ret = link_free_space(block_group, info);
828 spin_unlock(&block_group->tree_lock);
831 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
832 BUG_ON(ret == -EEXIST);
838 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
839 u64 offset, u64 bytes)
841 struct btrfs_free_space *info;
842 struct btrfs_free_space *next_info = NULL;
845 spin_lock(&block_group->tree_lock);
848 info = tree_search_offset(block_group, offset, 0, 0);
851 * oops didn't find an extent that matched the space we wanted
852 * to remove, look for a bitmap instead
854 info = tree_search_offset(block_group,
855 offset_to_bitmap(block_group, offset),
863 if (info->bytes < bytes && rb_next(&info->offset_index)) {
865 next_info = rb_entry(rb_next(&info->offset_index),
866 struct btrfs_free_space,
869 if (next_info->bitmap)
870 end = next_info->offset + BITS_PER_BITMAP *
871 block_group->sectorsize - 1;
873 end = next_info->offset + next_info->bytes;
875 if (next_info->bytes < bytes ||
876 next_info->offset > offset || offset > end) {
877 printk(KERN_CRIT "Found free space at %llu, size %llu,"
878 " trying to use %llu\n",
879 (unsigned long long)info->offset,
880 (unsigned long long)info->bytes,
881 (unsigned long long)bytes);
890 if (info->bytes == bytes) {
891 unlink_free_space(block_group, info);
894 block_group->total_bitmaps--;
900 if (!info->bitmap && info->offset == offset) {
901 unlink_free_space(block_group, info);
902 info->offset += bytes;
903 info->bytes -= bytes;
904 link_free_space(block_group, info);
908 if (!info->bitmap && info->offset <= offset &&
909 info->offset + info->bytes >= offset + bytes) {
910 u64 old_start = info->offset;
912 * we're freeing space in the middle of the info,
913 * this can happen during tree log replay
915 * first unlink the old info and then
916 * insert it again after the hole we're creating
918 unlink_free_space(block_group, info);
919 if (offset + bytes < info->offset + info->bytes) {
920 u64 old_end = info->offset + info->bytes;
922 info->offset = offset + bytes;
923 info->bytes = old_end - info->offset;
924 ret = link_free_space(block_group, info);
929 /* the hole we're creating ends at the end
930 * of the info struct, just free the info
934 spin_unlock(&block_group->tree_lock);
936 /* step two, insert a new info struct to cover
937 * anything before the hole
939 ret = btrfs_add_free_space(block_group, old_start,
945 ret = remove_from_bitmap(block_group, info, &offset, &bytes);
950 spin_unlock(&block_group->tree_lock);
955 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
958 struct btrfs_free_space *info;
962 for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
963 info = rb_entry(n, struct btrfs_free_space, offset_index);
964 if (info->bytes >= bytes)
966 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
967 (unsigned long long)info->offset,
968 (unsigned long long)info->bytes,
969 (info->bitmap) ? "yes" : "no");
971 printk(KERN_INFO "block group has cluster?: %s\n",
972 list_empty(&block_group->cluster_list) ? "no" : "yes");
973 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
977 u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
979 struct btrfs_free_space *info;
983 for (n = rb_first(&block_group->free_space_offset); n;
985 info = rb_entry(n, struct btrfs_free_space, offset_index);
993 * for a given cluster, put all of its extents back into the free
994 * space cache. If the block group passed doesn't match the block group
995 * pointed to by the cluster, someone else raced in and freed the
996 * cluster already. In that case, we just return without changing anything
999 __btrfs_return_cluster_to_free_space(
1000 struct btrfs_block_group_cache *block_group,
1001 struct btrfs_free_cluster *cluster)
1003 struct btrfs_free_space *entry;
1004 struct rb_node *node;
1007 spin_lock(&cluster->lock);
1008 if (cluster->block_group != block_group)
1011 bitmap = cluster->points_to_bitmap;
1012 cluster->block_group = NULL;
1013 cluster->window_start = 0;
1014 list_del_init(&cluster->block_group_list);
1015 cluster->points_to_bitmap = false;
1020 node = rb_first(&cluster->root);
1022 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1023 node = rb_next(&entry->offset_index);
1024 rb_erase(&entry->offset_index, &cluster->root);
1025 BUG_ON(entry->bitmap);
1026 tree_insert_offset(&block_group->free_space_offset,
1027 entry->offset, &entry->offset_index, 0);
1029 cluster->root = RB_ROOT;
1032 spin_unlock(&cluster->lock);
1033 btrfs_put_block_group(block_group);
1037 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1039 struct btrfs_free_space *info;
1040 struct rb_node *node;
1041 struct btrfs_free_cluster *cluster;
1042 struct list_head *head;
1044 spin_lock(&block_group->tree_lock);
1045 while ((head = block_group->cluster_list.next) !=
1046 &block_group->cluster_list) {
1047 cluster = list_entry(head, struct btrfs_free_cluster,
1050 WARN_ON(cluster->block_group != block_group);
1051 __btrfs_return_cluster_to_free_space(block_group, cluster);
1052 if (need_resched()) {
1053 spin_unlock(&block_group->tree_lock);
1055 spin_lock(&block_group->tree_lock);
1059 while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
1060 info = rb_entry(node, struct btrfs_free_space, offset_index);
1061 unlink_free_space(block_group, info);
1063 kfree(info->bitmap);
1065 if (need_resched()) {
1066 spin_unlock(&block_group->tree_lock);
1068 spin_lock(&block_group->tree_lock);
1072 spin_unlock(&block_group->tree_lock);
1075 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1076 u64 offset, u64 bytes, u64 empty_size)
1078 struct btrfs_free_space *entry = NULL;
1079 u64 bytes_search = bytes + empty_size;
1082 spin_lock(&block_group->tree_lock);
1083 entry = find_free_space(block_group, &offset, &bytes_search, 0);
1088 if (entry->bitmap) {
1089 bitmap_clear_bits(block_group, entry, offset, bytes);
1090 if (!entry->bytes) {
1091 unlink_free_space(block_group, entry);
1092 kfree(entry->bitmap);
1094 block_group->total_bitmaps--;
1095 recalculate_thresholds(block_group);
1098 unlink_free_space(block_group, entry);
1099 entry->offset += bytes;
1100 entry->bytes -= bytes;
1104 link_free_space(block_group, entry);
1108 spin_unlock(&block_group->tree_lock);
1114 * given a cluster, put all of its extents back into the free space
1115 * cache. If a block group is passed, this function will only free
1116 * a cluster that belongs to the passed block group.
1118 * Otherwise, it'll get a reference on the block group pointed to by the
1119 * cluster and remove the cluster from it.
1121 int btrfs_return_cluster_to_free_space(
1122 struct btrfs_block_group_cache *block_group,
1123 struct btrfs_free_cluster *cluster)
1127 /* first, get a safe pointer to the block group */
1128 spin_lock(&cluster->lock);
1130 block_group = cluster->block_group;
1132 spin_unlock(&cluster->lock);
1135 } else if (cluster->block_group != block_group) {
1136 /* someone else has already freed it don't redo their work */
1137 spin_unlock(&cluster->lock);
1140 atomic_inc(&block_group->count);
1141 spin_unlock(&cluster->lock);
1143 /* now return any extents the cluster had on it */
1144 spin_lock(&block_group->tree_lock);
1145 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1146 spin_unlock(&block_group->tree_lock);
1148 /* finally drop our ref */
1149 btrfs_put_block_group(block_group);
1153 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1154 struct btrfs_free_cluster *cluster,
1155 u64 bytes, u64 min_start)
1157 struct btrfs_free_space *entry;
1159 u64 search_start = cluster->window_start;
1160 u64 search_bytes = bytes;
1163 spin_lock(&block_group->tree_lock);
1164 spin_lock(&cluster->lock);
1166 if (!cluster->points_to_bitmap)
1169 if (cluster->block_group != block_group)
1173 * search_start is the beginning of the bitmap, but at some point it may
1174 * be a good idea to point to the actual start of the free area in the
1175 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1176 * to 1 to make sure we get the bitmap entry
1178 entry = tree_search_offset(block_group,
1179 offset_to_bitmap(block_group, search_start),
1181 if (!entry || !entry->bitmap)
1184 search_start = min_start;
1185 search_bytes = bytes;
1187 err = search_bitmap(block_group, entry, &search_start,
1193 bitmap_clear_bits(block_group, entry, ret, bytes);
1195 spin_unlock(&cluster->lock);
1196 spin_unlock(&block_group->tree_lock);
1202 * given a cluster, try to allocate 'bytes' from it, returns 0
1203 * if it couldn't find anything suitably large, or a logical disk offset
1204 * if things worked out
1206 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1207 struct btrfs_free_cluster *cluster, u64 bytes,
1210 struct btrfs_free_space *entry = NULL;
1211 struct rb_node *node;
1214 if (cluster->points_to_bitmap)
1215 return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
1218 spin_lock(&cluster->lock);
1219 if (bytes > cluster->max_size)
1222 if (cluster->block_group != block_group)
1225 node = rb_first(&cluster->root);
1229 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1232 if (entry->bytes < bytes || entry->offset < min_start) {
1233 struct rb_node *node;
1235 node = rb_next(&entry->offset_index);
1238 entry = rb_entry(node, struct btrfs_free_space,
1242 ret = entry->offset;
1244 entry->offset += bytes;
1245 entry->bytes -= bytes;
1247 if (entry->bytes == 0) {
1248 rb_erase(&entry->offset_index, &cluster->root);
1254 spin_unlock(&cluster->lock);
1259 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1260 struct btrfs_free_space *entry,
1261 struct btrfs_free_cluster *cluster,
1262 u64 offset, u64 bytes, u64 min_bytes)
1264 unsigned long next_zero;
1266 unsigned long search_bits;
1267 unsigned long total_bits;
1268 unsigned long found_bits;
1269 unsigned long start = 0;
1270 unsigned long total_found = 0;
1273 i = offset_to_bit(entry->offset, block_group->sectorsize,
1274 max_t(u64, offset, entry->offset));
1275 search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
1276 total_bits = bytes_to_bits(bytes, block_group->sectorsize);
1280 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
1281 i < BITS_PER_BITMAP;
1282 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
1283 next_zero = find_next_zero_bit(entry->bitmap,
1284 BITS_PER_BITMAP, i);
1285 if (next_zero - i >= search_bits) {
1286 found_bits = next_zero - i;
1300 total_found += found_bits;
1302 if (cluster->max_size < found_bits * block_group->sectorsize)
1303 cluster->max_size = found_bits * block_group->sectorsize;
1305 if (total_found < total_bits) {
1306 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
1307 if (i - start > total_bits * 2) {
1309 cluster->max_size = 0;
1315 cluster->window_start = start * block_group->sectorsize +
1317 cluster->points_to_bitmap = true;
1323 * here we try to find a cluster of blocks in a block group. The goal
1324 * is to find at least bytes free and up to empty_size + bytes free.
1325 * We might not find them all in one contiguous area.
1327 * returns zero and sets up cluster if things worked out, otherwise
1328 * it returns -enospc
1330 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root,
1332 struct btrfs_block_group_cache *block_group,
1333 struct btrfs_free_cluster *cluster,
1334 u64 offset, u64 bytes, u64 empty_size)
1336 struct btrfs_free_space *entry = NULL;
1337 struct rb_node *node;
1338 struct btrfs_free_space *next;
1339 struct btrfs_free_space *last = NULL;
1344 bool found_bitmap = false;
1347 /* for metadata, allow allocates with more holes */
1348 if (btrfs_test_opt(root, SSD_SPREAD)) {
1349 min_bytes = bytes + empty_size;
1350 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
1352 * we want to do larger allocations when we are
1353 * flushing out the delayed refs, it helps prevent
1354 * making more work as we go along.
1356 if (trans->transaction->delayed_refs.flushing)
1357 min_bytes = max(bytes, (bytes + empty_size) >> 1);
1359 min_bytes = max(bytes, (bytes + empty_size) >> 4);
1361 min_bytes = max(bytes, (bytes + empty_size) >> 2);
1363 spin_lock(&block_group->tree_lock);
1364 spin_lock(&cluster->lock);
1366 /* someone already found a cluster, hooray */
1367 if (cluster->block_group) {
1372 entry = tree_search_offset(block_group, offset, found_bitmap, 1);
1379 * If found_bitmap is true, we exhausted our search for extent entries,
1380 * and we just want to search all of the bitmaps that we can find, and
1381 * ignore any extent entries we find.
1383 while (entry->bitmap || found_bitmap ||
1384 (!entry->bitmap && entry->bytes < min_bytes)) {
1385 struct rb_node *node = rb_next(&entry->offset_index);
1387 if (entry->bitmap && entry->bytes > bytes + empty_size) {
1388 ret = btrfs_bitmap_cluster(block_group, entry, cluster,
1389 offset, bytes + empty_size,
1399 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1403 * We already searched all the extent entries from the passed in offset
1404 * to the end and didn't find enough space for the cluster, and we also
1405 * didn't find any bitmaps that met our criteria, just go ahead and exit
1412 cluster->points_to_bitmap = false;
1413 window_start = entry->offset;
1414 window_free = entry->bytes;
1416 max_extent = entry->bytes;
1419 /* out window is just right, lets fill it */
1420 if (window_free >= bytes + empty_size)
1423 node = rb_next(&last->offset_index);
1430 next = rb_entry(node, struct btrfs_free_space, offset_index);
1433 * we found a bitmap, so if this search doesn't result in a
1434 * cluster, we know to go and search again for the bitmaps and
1435 * start looking for space there
1439 offset = next->offset;
1440 found_bitmap = true;
1446 * we haven't filled the empty size and the window is
1447 * very large. reset and try again
1449 if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
1450 next->offset - window_start > (bytes + empty_size) * 2) {
1452 window_start = entry->offset;
1453 window_free = entry->bytes;
1455 max_extent = entry->bytes;
1458 window_free += next->bytes;
1459 if (entry->bytes > max_extent)
1460 max_extent = entry->bytes;
1464 cluster->window_start = entry->offset;
1467 * now we've found our entries, pull them out of the free space
1468 * cache and put them into the cluster rbtree
1470 * The cluster includes an rbtree, but only uses the offset index
1471 * of each free space cache entry.
1474 node = rb_next(&entry->offset_index);
1475 if (entry->bitmap && node) {
1476 entry = rb_entry(node, struct btrfs_free_space,
1479 } else if (entry->bitmap && !node) {
1483 rb_erase(&entry->offset_index, &block_group->free_space_offset);
1484 ret = tree_insert_offset(&cluster->root, entry->offset,
1485 &entry->offset_index, 0);
1488 if (!node || entry == last)
1491 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1494 cluster->max_size = max_extent;
1497 atomic_inc(&block_group->count);
1498 list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
1499 cluster->block_group = block_group;
1501 spin_unlock(&cluster->lock);
1502 spin_unlock(&block_group->tree_lock);
1508 * simple code to zero out a cluster
1510 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
1512 spin_lock_init(&cluster->lock);
1513 spin_lock_init(&cluster->refill_lock);
1514 cluster->root = RB_ROOT;
1515 cluster->max_size = 0;
1516 cluster->points_to_bitmap = false;
1517 INIT_LIST_HEAD(&cluster->block_group_list);
1518 cluster->block_group = NULL;