1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2015 Facebook. All rights reserved.
6 #include <linux/kernel.h>
7 #include <linux/sched/mm.h>
11 #include "free-space-tree.h"
12 #include "transaction.h"
14 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
15 struct btrfs_block_group_cache *block_group,
16 struct btrfs_path *path);
18 void set_free_space_tree_thresholds(struct btrfs_block_group_cache *cache)
22 u64 num_bitmaps, total_bitmap_size;
25 * We convert to bitmaps when the disk space required for using extents
26 * exceeds that required for using bitmaps.
28 bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
29 num_bitmaps = div_u64(cache->key.offset + bitmap_range - 1,
31 bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
32 total_bitmap_size = num_bitmaps * bitmap_size;
33 cache->bitmap_high_thresh = div_u64(total_bitmap_size,
34 sizeof(struct btrfs_item));
37 * We allow for a small buffer between the high threshold and low
38 * threshold to avoid thrashing back and forth between the two formats.
40 if (cache->bitmap_high_thresh > 100)
41 cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
43 cache->bitmap_low_thresh = 0;
46 static int add_new_free_space_info(struct btrfs_trans_handle *trans,
47 struct btrfs_block_group_cache *block_group,
48 struct btrfs_path *path)
50 struct btrfs_root *root = trans->fs_info->free_space_root;
51 struct btrfs_free_space_info *info;
53 struct extent_buffer *leaf;
56 key.objectid = block_group->key.objectid;
57 key.type = BTRFS_FREE_SPACE_INFO_KEY;
58 key.offset = block_group->key.offset;
60 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
64 leaf = path->nodes[0];
65 info = btrfs_item_ptr(leaf, path->slots[0],
66 struct btrfs_free_space_info);
67 btrfs_set_free_space_extent_count(leaf, info, 0);
68 btrfs_set_free_space_flags(leaf, info, 0);
69 btrfs_mark_buffer_dirty(leaf);
73 btrfs_release_path(path);
78 struct btrfs_free_space_info *search_free_space_info(
79 struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info,
80 struct btrfs_block_group_cache *block_group,
81 struct btrfs_path *path, int cow)
83 struct btrfs_root *root = fs_info->free_space_root;
87 key.objectid = block_group->key.objectid;
88 key.type = BTRFS_FREE_SPACE_INFO_KEY;
89 key.offset = block_group->key.offset;
91 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
95 btrfs_warn(fs_info, "missing free space info for %llu",
96 block_group->key.objectid);
98 return ERR_PTR(-ENOENT);
101 return btrfs_item_ptr(path->nodes[0], path->slots[0],
102 struct btrfs_free_space_info);
106 * btrfs_search_slot() but we're looking for the greatest key less than the
109 static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root,
111 struct btrfs_key *key, struct btrfs_path *p,
112 int ins_len, int cow)
116 ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
125 if (p->slots[0] == 0) {
134 static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
136 return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
139 static unsigned long *alloc_bitmap(u32 bitmap_size)
142 unsigned int nofs_flag;
143 u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
146 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
147 * into the filesystem as the free space bitmap can be modified in the
148 * critical section of a transaction commit.
150 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
151 * know that recursion is unsafe.
153 nofs_flag = memalloc_nofs_save();
154 ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
155 memalloc_nofs_restore(nofs_flag);
159 static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
161 u8 *p = ((u8 *)map) + BIT_BYTE(start);
162 const unsigned int size = start + len;
163 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
164 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
166 while (len - bits_to_set >= 0) {
169 bits_to_set = BITS_PER_BYTE;
174 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
180 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
181 struct btrfs_block_group_cache *block_group,
182 struct btrfs_path *path)
184 struct btrfs_fs_info *fs_info = trans->fs_info;
185 struct btrfs_root *root = fs_info->free_space_root;
186 struct btrfs_free_space_info *info;
187 struct btrfs_key key, found_key;
188 struct extent_buffer *leaf;
189 unsigned long *bitmap;
193 u32 bitmap_size, flags, expected_extent_count;
194 u32 extent_count = 0;
198 bitmap_size = free_space_bitmap_size(block_group->key.offset,
199 fs_info->sectorsize);
200 bitmap = alloc_bitmap(bitmap_size);
206 start = block_group->key.objectid;
207 end = block_group->key.objectid + block_group->key.offset;
209 key.objectid = end - 1;
211 key.offset = (u64)-1;
214 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
218 leaf = path->nodes[0];
221 while (path->slots[0] > 0) {
222 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
224 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
225 ASSERT(found_key.objectid == block_group->key.objectid);
226 ASSERT(found_key.offset == block_group->key.offset);
229 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
232 ASSERT(found_key.objectid >= start);
233 ASSERT(found_key.objectid < end);
234 ASSERT(found_key.objectid + found_key.offset <= end);
236 first = div_u64(found_key.objectid - start,
237 fs_info->sectorsize);
238 last = div_u64(found_key.objectid + found_key.offset - start,
239 fs_info->sectorsize);
240 le_bitmap_set(bitmap, first, last - first);
250 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
253 btrfs_release_path(path);
256 info = search_free_space_info(trans, fs_info, block_group, path, 1);
261 leaf = path->nodes[0];
262 flags = btrfs_free_space_flags(leaf, info);
263 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
264 btrfs_set_free_space_flags(leaf, info, flags);
265 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
266 btrfs_mark_buffer_dirty(leaf);
267 btrfs_release_path(path);
269 if (extent_count != expected_extent_count) {
271 "incorrect extent count for %llu; counted %u, expected %u",
272 block_group->key.objectid, extent_count,
273 expected_extent_count);
279 bitmap_cursor = (char *)bitmap;
280 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
287 extent_size = min(end - i, bitmap_range);
288 data_size = free_space_bitmap_size(extent_size,
289 fs_info->sectorsize);
292 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
293 key.offset = extent_size;
295 ret = btrfs_insert_empty_item(trans, root, path, &key,
300 leaf = path->nodes[0];
301 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
302 write_extent_buffer(leaf, bitmap_cursor, ptr,
304 btrfs_mark_buffer_dirty(leaf);
305 btrfs_release_path(path);
308 bitmap_cursor += data_size;
315 btrfs_abort_transaction(trans, ret);
320 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
321 struct btrfs_block_group_cache *block_group,
322 struct btrfs_path *path)
324 struct btrfs_fs_info *fs_info = trans->fs_info;
325 struct btrfs_root *root = fs_info->free_space_root;
326 struct btrfs_free_space_info *info;
327 struct btrfs_key key, found_key;
328 struct extent_buffer *leaf;
329 unsigned long *bitmap;
331 u32 bitmap_size, flags, expected_extent_count;
332 unsigned long nrbits, start_bit, end_bit;
333 u32 extent_count = 0;
337 bitmap_size = free_space_bitmap_size(block_group->key.offset,
338 fs_info->sectorsize);
339 bitmap = alloc_bitmap(bitmap_size);
345 start = block_group->key.objectid;
346 end = block_group->key.objectid + block_group->key.offset;
348 key.objectid = end - 1;
350 key.offset = (u64)-1;
353 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
357 leaf = path->nodes[0];
360 while (path->slots[0] > 0) {
361 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
363 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
364 ASSERT(found_key.objectid == block_group->key.objectid);
365 ASSERT(found_key.offset == block_group->key.offset);
368 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
371 u32 bitmap_pos, data_size;
373 ASSERT(found_key.objectid >= start);
374 ASSERT(found_key.objectid < end);
375 ASSERT(found_key.objectid + found_key.offset <= end);
377 bitmap_pos = div_u64(found_key.objectid - start,
378 fs_info->sectorsize *
380 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
381 data_size = free_space_bitmap_size(found_key.offset,
382 fs_info->sectorsize);
384 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
385 read_extent_buffer(leaf, bitmap_cursor, ptr,
395 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
398 btrfs_release_path(path);
401 info = search_free_space_info(trans, fs_info, block_group, path, 1);
406 leaf = path->nodes[0];
407 flags = btrfs_free_space_flags(leaf, info);
408 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
409 btrfs_set_free_space_flags(leaf, info, flags);
410 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
411 btrfs_mark_buffer_dirty(leaf);
412 btrfs_release_path(path);
414 nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
415 start_bit = find_next_bit_le(bitmap, nrbits, 0);
417 while (start_bit < nrbits) {
418 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
419 ASSERT(start_bit < end_bit);
421 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
422 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
423 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
425 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
428 btrfs_release_path(path);
432 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
435 if (extent_count != expected_extent_count) {
437 "incorrect extent count for %llu; counted %u, expected %u",
438 block_group->key.objectid, extent_count,
439 expected_extent_count);
449 btrfs_abort_transaction(trans, ret);
453 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
454 struct btrfs_block_group_cache *block_group,
455 struct btrfs_path *path,
458 struct btrfs_free_space_info *info;
463 if (new_extents == 0)
466 info = search_free_space_info(trans, trans->fs_info, block_group, path,
472 flags = btrfs_free_space_flags(path->nodes[0], info);
473 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
475 extent_count += new_extents;
476 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
477 btrfs_mark_buffer_dirty(path->nodes[0]);
478 btrfs_release_path(path);
480 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
481 extent_count > block_group->bitmap_high_thresh) {
482 ret = convert_free_space_to_bitmaps(trans, block_group, path);
483 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
484 extent_count < block_group->bitmap_low_thresh) {
485 ret = convert_free_space_to_extents(trans, block_group, path);
493 int free_space_test_bit(struct btrfs_block_group_cache *block_group,
494 struct btrfs_path *path, u64 offset)
496 struct extent_buffer *leaf;
497 struct btrfs_key key;
498 u64 found_start, found_end;
499 unsigned long ptr, i;
501 leaf = path->nodes[0];
502 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
503 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
505 found_start = key.objectid;
506 found_end = key.objectid + key.offset;
507 ASSERT(offset >= found_start && offset < found_end);
509 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
510 i = div_u64(offset - found_start,
511 block_group->fs_info->sectorsize);
512 return !!extent_buffer_test_bit(leaf, ptr, i);
515 static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
516 struct btrfs_path *path, u64 *start, u64 *size,
519 struct btrfs_fs_info *fs_info = block_group->fs_info;
520 struct extent_buffer *leaf;
521 struct btrfs_key key;
522 u64 end = *start + *size;
523 u64 found_start, found_end;
524 unsigned long ptr, first, last;
526 leaf = path->nodes[0];
527 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
528 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
530 found_start = key.objectid;
531 found_end = key.objectid + key.offset;
532 ASSERT(*start >= found_start && *start < found_end);
533 ASSERT(end > found_start);
538 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
539 first = div_u64(*start - found_start, fs_info->sectorsize);
540 last = div_u64(end - found_start, fs_info->sectorsize);
542 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
544 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
545 btrfs_mark_buffer_dirty(leaf);
547 *size -= end - *start;
552 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
553 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
554 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
557 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
558 struct btrfs_root *root, struct btrfs_path *p)
560 struct btrfs_key key;
562 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
567 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
568 btrfs_release_path(p);
570 key.objectid += key.offset;
572 key.offset = (u64)-1;
574 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
578 * If remove is 1, then we are removing free space, thus clearing bits in the
579 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
582 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
583 struct btrfs_block_group_cache *block_group,
584 struct btrfs_path *path,
585 u64 start, u64 size, int remove)
587 struct btrfs_root *root = block_group->fs_info->free_space_root;
588 struct btrfs_key key;
589 u64 end = start + size;
590 u64 cur_start, cur_size;
591 int prev_bit, next_bit;
596 * Read the bit for the block immediately before the extent of space if
597 * that block is within the block group.
599 if (start > block_group->key.objectid) {
600 u64 prev_block = start - block_group->fs_info->sectorsize;
602 key.objectid = prev_block;
604 key.offset = (u64)-1;
606 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
610 prev_bit = free_space_test_bit(block_group, path, prev_block);
612 /* The previous block may have been in the previous bitmap. */
613 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
614 if (start >= key.objectid + key.offset) {
615 ret = free_space_next_bitmap(trans, root, path);
620 key.objectid = start;
622 key.offset = (u64)-1;
624 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
632 * Iterate over all of the bitmaps overlapped by the extent of space,
633 * clearing/setting bits as required.
638 free_space_set_bits(block_group, path, &cur_start, &cur_size,
642 ret = free_space_next_bitmap(trans, root, path);
648 * Read the bit for the block immediately after the extent of space if
649 * that block is within the block group.
651 if (end < block_group->key.objectid + block_group->key.offset) {
652 /* The next block may be in the next bitmap. */
653 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
654 if (end >= key.objectid + key.offset) {
655 ret = free_space_next_bitmap(trans, root, path);
660 next_bit = free_space_test_bit(block_group, path, end);
668 /* Leftover on the left. */
672 /* Leftover on the right. */
678 /* Merging with neighbor on the left. */
682 /* Merging with neighbor on the right. */
687 btrfs_release_path(path);
688 ret = update_free_space_extent_count(trans, block_group, path,
695 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
696 struct btrfs_block_group_cache *block_group,
697 struct btrfs_path *path,
700 struct btrfs_root *root = trans->fs_info->free_space_root;
701 struct btrfs_key key;
702 u64 found_start, found_end;
703 u64 end = start + size;
704 int new_extents = -1;
707 key.objectid = start;
709 key.offset = (u64)-1;
711 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
715 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
717 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
719 found_start = key.objectid;
720 found_end = key.objectid + key.offset;
721 ASSERT(start >= found_start && end <= found_end);
724 * Okay, now that we've found the free space extent which contains the
725 * free space that we are removing, there are four cases:
727 * 1. We're using the whole extent: delete the key we found and
728 * decrement the free space extent count.
729 * 2. We are using part of the extent starting at the beginning: delete
730 * the key we found and insert a new key representing the leftover at
731 * the end. There is no net change in the number of extents.
732 * 3. We are using part of the extent ending at the end: delete the key
733 * we found and insert a new key representing the leftover at the
734 * beginning. There is no net change in the number of extents.
735 * 4. We are using part of the extent in the middle: delete the key we
736 * found and insert two new keys representing the leftovers on each
737 * side. Where we used to have one extent, we now have two, so increment
738 * the extent count. We may need to convert the block group to bitmaps
742 /* Delete the existing key (cases 1-4). */
743 ret = btrfs_del_item(trans, root, path);
747 /* Add a key for leftovers at the beginning (cases 3 and 4). */
748 if (start > found_start) {
749 key.objectid = found_start;
750 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
751 key.offset = start - found_start;
753 btrfs_release_path(path);
754 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
760 /* Add a key for leftovers at the end (cases 2 and 4). */
761 if (end < found_end) {
763 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
764 key.offset = found_end - end;
766 btrfs_release_path(path);
767 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
773 btrfs_release_path(path);
774 ret = update_free_space_extent_count(trans, block_group, path,
782 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
783 struct btrfs_block_group_cache *block_group,
784 struct btrfs_path *path, u64 start, u64 size)
786 struct btrfs_free_space_info *info;
790 if (block_group->needs_free_space) {
791 ret = __add_block_group_free_space(trans, block_group, path);
796 info = search_free_space_info(NULL, trans->fs_info, block_group, path,
799 return PTR_ERR(info);
800 flags = btrfs_free_space_flags(path->nodes[0], info);
801 btrfs_release_path(path);
803 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
804 return modify_free_space_bitmap(trans, block_group, path,
807 return remove_free_space_extent(trans, block_group, path,
812 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
815 struct btrfs_block_group_cache *block_group;
816 struct btrfs_path *path;
819 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
822 path = btrfs_alloc_path();
828 block_group = btrfs_lookup_block_group(trans->fs_info, start);
835 mutex_lock(&block_group->free_space_lock);
836 ret = __remove_from_free_space_tree(trans, block_group, path, start,
838 mutex_unlock(&block_group->free_space_lock);
840 btrfs_put_block_group(block_group);
842 btrfs_free_path(path);
844 btrfs_abort_transaction(trans, ret);
848 static int add_free_space_extent(struct btrfs_trans_handle *trans,
849 struct btrfs_block_group_cache *block_group,
850 struct btrfs_path *path,
853 struct btrfs_root *root = trans->fs_info->free_space_root;
854 struct btrfs_key key, new_key;
855 u64 found_start, found_end;
856 u64 end = start + size;
861 * We are adding a new extent of free space, but we need to merge
862 * extents. There are four cases here:
864 * 1. The new extent does not have any immediate neighbors to merge
865 * with: add the new key and increment the free space extent count. We
866 * may need to convert the block group to bitmaps as a result.
867 * 2. The new extent has an immediate neighbor before it: remove the
868 * previous key and insert a new key combining both of them. There is no
869 * net change in the number of extents.
870 * 3. The new extent has an immediate neighbor after it: remove the next
871 * key and insert a new key combining both of them. There is no net
872 * change in the number of extents.
873 * 4. The new extent has immediate neighbors on both sides: remove both
874 * of the keys and insert a new key combining all of them. Where we used
875 * to have two extents, we now have one, so decrement the extent count.
878 new_key.objectid = start;
879 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
880 new_key.offset = size;
882 /* Search for a neighbor on the left. */
883 if (start == block_group->key.objectid)
885 key.objectid = start - 1;
887 key.offset = (u64)-1;
889 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
893 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
895 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
896 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
897 btrfs_release_path(path);
901 found_start = key.objectid;
902 found_end = key.objectid + key.offset;
903 ASSERT(found_start >= block_group->key.objectid &&
904 found_end > block_group->key.objectid);
905 ASSERT(found_start < start && found_end <= start);
908 * Delete the neighbor on the left and absorb it into the new key (cases
911 if (found_end == start) {
912 ret = btrfs_del_item(trans, root, path);
915 new_key.objectid = found_start;
916 new_key.offset += key.offset;
919 btrfs_release_path(path);
922 /* Search for a neighbor on the right. */
923 if (end == block_group->key.objectid + block_group->key.offset)
927 key.offset = (u64)-1;
929 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
933 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
935 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
936 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
937 btrfs_release_path(path);
941 found_start = key.objectid;
942 found_end = key.objectid + key.offset;
943 ASSERT(found_start >= block_group->key.objectid &&
944 found_end > block_group->key.objectid);
945 ASSERT((found_start < start && found_end <= start) ||
946 (found_start >= end && found_end > end));
949 * Delete the neighbor on the right and absorb it into the new key
952 if (found_start == end) {
953 ret = btrfs_del_item(trans, root, path);
956 new_key.offset += key.offset;
959 btrfs_release_path(path);
962 /* Insert the new key (cases 1-4). */
963 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
967 btrfs_release_path(path);
968 ret = update_free_space_extent_count(trans, block_group, path,
976 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
977 struct btrfs_block_group_cache *block_group,
978 struct btrfs_path *path, u64 start, u64 size)
980 struct btrfs_fs_info *fs_info = trans->fs_info;
981 struct btrfs_free_space_info *info;
985 if (block_group->needs_free_space) {
986 ret = __add_block_group_free_space(trans, block_group, path);
991 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
993 return PTR_ERR(info);
994 flags = btrfs_free_space_flags(path->nodes[0], info);
995 btrfs_release_path(path);
997 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
998 return modify_free_space_bitmap(trans, block_group, path,
1001 return add_free_space_extent(trans, block_group, path, start,
1006 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1007 u64 start, u64 size)
1009 struct btrfs_block_group_cache *block_group;
1010 struct btrfs_path *path;
1013 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1016 path = btrfs_alloc_path();
1022 block_group = btrfs_lookup_block_group(trans->fs_info, start);
1029 mutex_lock(&block_group->free_space_lock);
1030 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1031 mutex_unlock(&block_group->free_space_lock);
1033 btrfs_put_block_group(block_group);
1035 btrfs_free_path(path);
1037 btrfs_abort_transaction(trans, ret);
1042 * Populate the free space tree by walking the extent tree. Operations on the
1043 * extent tree that happen as a result of writes to the free space tree will go
1044 * through the normal add/remove hooks.
1046 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1047 struct btrfs_block_group_cache *block_group)
1049 struct btrfs_root *extent_root = trans->fs_info->extent_root;
1050 struct btrfs_path *path, *path2;
1051 struct btrfs_key key;
1055 path = btrfs_alloc_path();
1058 path->reada = READA_FORWARD;
1060 path2 = btrfs_alloc_path();
1062 btrfs_free_path(path);
1066 ret = add_new_free_space_info(trans, block_group, path2);
1070 mutex_lock(&block_group->free_space_lock);
1073 * Iterate through all of the extent and metadata items in this block
1074 * group, adding the free space between them and the free space at the
1075 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1076 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1079 key.objectid = block_group->key.objectid;
1080 key.type = BTRFS_EXTENT_ITEM_KEY;
1083 ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1088 start = block_group->key.objectid;
1089 end = block_group->key.objectid + block_group->key.offset;
1091 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1093 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1094 key.type == BTRFS_METADATA_ITEM_KEY) {
1095 if (key.objectid >= end)
1098 if (start < key.objectid) {
1099 ret = __add_to_free_space_tree(trans,
1107 start = key.objectid;
1108 if (key.type == BTRFS_METADATA_ITEM_KEY)
1109 start += trans->fs_info->nodesize;
1111 start += key.offset;
1112 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1113 if (key.objectid != block_group->key.objectid)
1117 ret = btrfs_next_item(extent_root, path);
1124 ret = __add_to_free_space_tree(trans, block_group, path2,
1125 start, end - start);
1132 mutex_unlock(&block_group->free_space_lock);
1134 btrfs_free_path(path2);
1135 btrfs_free_path(path);
1139 int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1141 struct btrfs_trans_handle *trans;
1142 struct btrfs_root *tree_root = fs_info->tree_root;
1143 struct btrfs_root *free_space_root;
1144 struct btrfs_block_group_cache *block_group;
1145 struct rb_node *node;
1148 trans = btrfs_start_transaction(tree_root, 0);
1150 return PTR_ERR(trans);
1152 set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1153 free_space_root = btrfs_create_tree(trans, fs_info,
1154 BTRFS_FREE_SPACE_TREE_OBJECTID);
1155 if (IS_ERR(free_space_root)) {
1156 ret = PTR_ERR(free_space_root);
1159 fs_info->free_space_root = free_space_root;
1161 node = rb_first(&fs_info->block_group_cache_tree);
1163 block_group = rb_entry(node, struct btrfs_block_group_cache,
1165 ret = populate_free_space_tree(trans, block_group);
1168 node = rb_next(node);
1171 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1172 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1173 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1175 return btrfs_commit_transaction(trans);
1178 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1179 btrfs_abort_transaction(trans, ret);
1180 btrfs_end_transaction(trans);
1184 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root)
1187 struct btrfs_path *path;
1188 struct btrfs_key key;
1192 path = btrfs_alloc_path();
1196 path->leave_spinning = 1;
1203 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1207 nr = btrfs_header_nritems(path->nodes[0]);
1212 ret = btrfs_del_items(trans, root, path, 0, nr);
1216 btrfs_release_path(path);
1221 btrfs_free_path(path);
1225 int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1227 struct btrfs_trans_handle *trans;
1228 struct btrfs_root *tree_root = fs_info->tree_root;
1229 struct btrfs_root *free_space_root = fs_info->free_space_root;
1232 trans = btrfs_start_transaction(tree_root, 0);
1234 return PTR_ERR(trans);
1236 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1237 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1238 fs_info->free_space_root = NULL;
1240 ret = clear_free_space_tree(trans, free_space_root);
1244 ret = btrfs_del_root(trans, &free_space_root->root_key);
1248 list_del(&free_space_root->dirty_list);
1250 btrfs_tree_lock(free_space_root->node);
1251 clean_tree_block(fs_info, free_space_root->node);
1252 btrfs_tree_unlock(free_space_root->node);
1253 btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1256 free_extent_buffer(free_space_root->node);
1257 free_extent_buffer(free_space_root->commit_root);
1258 kfree(free_space_root);
1260 return btrfs_commit_transaction(trans);
1263 btrfs_abort_transaction(trans, ret);
1264 btrfs_end_transaction(trans);
1268 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1269 struct btrfs_block_group_cache *block_group,
1270 struct btrfs_path *path)
1274 block_group->needs_free_space = 0;
1276 ret = add_new_free_space_info(trans, block_group, path);
1280 return __add_to_free_space_tree(trans, block_group, path,
1281 block_group->key.objectid,
1282 block_group->key.offset);
1285 int add_block_group_free_space(struct btrfs_trans_handle *trans,
1286 struct btrfs_block_group_cache *block_group)
1288 struct btrfs_fs_info *fs_info = trans->fs_info;
1289 struct btrfs_path *path = NULL;
1292 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1295 mutex_lock(&block_group->free_space_lock);
1296 if (!block_group->needs_free_space)
1299 path = btrfs_alloc_path();
1305 ret = __add_block_group_free_space(trans, block_group, path);
1308 btrfs_free_path(path);
1309 mutex_unlock(&block_group->free_space_lock);
1311 btrfs_abort_transaction(trans, ret);
1315 int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1316 struct btrfs_block_group_cache *block_group)
1318 struct btrfs_root *root = trans->fs_info->free_space_root;
1319 struct btrfs_path *path;
1320 struct btrfs_key key, found_key;
1321 struct extent_buffer *leaf;
1326 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1329 if (block_group->needs_free_space) {
1330 /* We never added this block group to the free space tree. */
1334 path = btrfs_alloc_path();
1340 start = block_group->key.objectid;
1341 end = block_group->key.objectid + block_group->key.offset;
1343 key.objectid = end - 1;
1345 key.offset = (u64)-1;
1348 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1352 leaf = path->nodes[0];
1355 while (path->slots[0] > 0) {
1356 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1358 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1359 ASSERT(found_key.objectid == block_group->key.objectid);
1360 ASSERT(found_key.offset == block_group->key.offset);
1365 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1366 found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1367 ASSERT(found_key.objectid >= start);
1368 ASSERT(found_key.objectid < end);
1369 ASSERT(found_key.objectid + found_key.offset <= end);
1377 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1380 btrfs_release_path(path);
1385 btrfs_free_path(path);
1387 btrfs_abort_transaction(trans, ret);
1391 static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1392 struct btrfs_path *path,
1393 u32 expected_extent_count)
1395 struct btrfs_block_group_cache *block_group;
1396 struct btrfs_fs_info *fs_info;
1397 struct btrfs_root *root;
1398 struct btrfs_key key;
1399 int prev_bit = 0, bit;
1400 /* Initialize to silence GCC. */
1401 u64 extent_start = 0;
1403 u64 total_found = 0;
1404 u32 extent_count = 0;
1407 block_group = caching_ctl->block_group;
1408 fs_info = block_group->fs_info;
1409 root = fs_info->free_space_root;
1411 end = block_group->key.objectid + block_group->key.offset;
1414 ret = btrfs_next_item(root, path);
1420 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1422 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1425 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1426 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1428 caching_ctl->progress = key.objectid;
1430 offset = key.objectid;
1431 while (offset < key.objectid + key.offset) {
1432 bit = free_space_test_bit(block_group, path, offset);
1433 if (prev_bit == 0 && bit == 1) {
1434 extent_start = offset;
1435 } else if (prev_bit == 1 && bit == 0) {
1436 total_found += add_new_free_space(block_group,
1439 if (total_found > CACHING_CTL_WAKE_UP) {
1441 wake_up(&caching_ctl->wait);
1446 offset += fs_info->sectorsize;
1449 if (prev_bit == 1) {
1450 total_found += add_new_free_space(block_group, extent_start,
1455 if (extent_count != expected_extent_count) {
1457 "incorrect extent count for %llu; counted %u, expected %u",
1458 block_group->key.objectid, extent_count,
1459 expected_extent_count);
1465 caching_ctl->progress = (u64)-1;
1472 static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1473 struct btrfs_path *path,
1474 u32 expected_extent_count)
1476 struct btrfs_block_group_cache *block_group;
1477 struct btrfs_fs_info *fs_info;
1478 struct btrfs_root *root;
1479 struct btrfs_key key;
1481 u64 total_found = 0;
1482 u32 extent_count = 0;
1485 block_group = caching_ctl->block_group;
1486 fs_info = block_group->fs_info;
1487 root = fs_info->free_space_root;
1489 end = block_group->key.objectid + block_group->key.offset;
1492 ret = btrfs_next_item(root, path);
1498 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1500 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1503 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1504 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1506 caching_ctl->progress = key.objectid;
1508 total_found += add_new_free_space(block_group, key.objectid,
1509 key.objectid + key.offset);
1510 if (total_found > CACHING_CTL_WAKE_UP) {
1512 wake_up(&caching_ctl->wait);
1517 if (extent_count != expected_extent_count) {
1519 "incorrect extent count for %llu; counted %u, expected %u",
1520 block_group->key.objectid, extent_count,
1521 expected_extent_count);
1527 caching_ctl->progress = (u64)-1;
1534 int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1536 struct btrfs_block_group_cache *block_group;
1537 struct btrfs_fs_info *fs_info;
1538 struct btrfs_free_space_info *info;
1539 struct btrfs_path *path;
1540 u32 extent_count, flags;
1543 block_group = caching_ctl->block_group;
1544 fs_info = block_group->fs_info;
1546 path = btrfs_alloc_path();
1551 * Just like caching_thread() doesn't want to deadlock on the extent
1552 * tree, we don't want to deadlock on the free space tree.
1554 path->skip_locking = 1;
1555 path->search_commit_root = 1;
1556 path->reada = READA_FORWARD;
1558 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
1560 ret = PTR_ERR(info);
1563 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1564 flags = btrfs_free_space_flags(path->nodes[0], info);
1567 * We left path pointing to the free space info item, so now
1568 * load_free_space_foo can just iterate through the free space tree from
1571 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1572 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1574 ret = load_free_space_extents(caching_ctl, path, extent_count);
1577 btrfs_free_path(path);