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);
77 struct btrfs_free_space_info *
78 search_free_space_info(struct btrfs_trans_handle *trans,
79 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);
179 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
180 struct btrfs_block_group_cache *block_group,
181 struct btrfs_path *path)
183 struct btrfs_fs_info *fs_info = trans->fs_info;
184 struct btrfs_root *root = fs_info->free_space_root;
185 struct btrfs_free_space_info *info;
186 struct btrfs_key key, found_key;
187 struct extent_buffer *leaf;
188 unsigned long *bitmap;
192 u32 bitmap_size, flags, expected_extent_count;
193 u32 extent_count = 0;
197 bitmap_size = free_space_bitmap_size(block_group->key.offset,
198 fs_info->sectorsize);
199 bitmap = alloc_bitmap(bitmap_size);
205 start = block_group->key.objectid;
206 end = block_group->key.objectid + block_group->key.offset;
208 key.objectid = end - 1;
210 key.offset = (u64)-1;
213 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
217 leaf = path->nodes[0];
220 while (path->slots[0] > 0) {
221 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
223 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
224 ASSERT(found_key.objectid == block_group->key.objectid);
225 ASSERT(found_key.offset == block_group->key.offset);
228 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
231 ASSERT(found_key.objectid >= start);
232 ASSERT(found_key.objectid < end);
233 ASSERT(found_key.objectid + found_key.offset <= end);
235 first = div_u64(found_key.objectid - start,
236 fs_info->sectorsize);
237 last = div_u64(found_key.objectid + found_key.offset - start,
238 fs_info->sectorsize);
239 le_bitmap_set(bitmap, first, last - first);
249 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
252 btrfs_release_path(path);
255 info = search_free_space_info(trans, fs_info, block_group, path, 1);
260 leaf = path->nodes[0];
261 flags = btrfs_free_space_flags(leaf, info);
262 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
263 btrfs_set_free_space_flags(leaf, info, flags);
264 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
265 btrfs_mark_buffer_dirty(leaf);
266 btrfs_release_path(path);
268 if (extent_count != expected_extent_count) {
270 "incorrect extent count for %llu; counted %u, expected %u",
271 block_group->key.objectid, extent_count,
272 expected_extent_count);
278 bitmap_cursor = (char *)bitmap;
279 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
286 extent_size = min(end - i, bitmap_range);
287 data_size = free_space_bitmap_size(extent_size,
288 fs_info->sectorsize);
291 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
292 key.offset = extent_size;
294 ret = btrfs_insert_empty_item(trans, root, path, &key,
299 leaf = path->nodes[0];
300 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
301 write_extent_buffer(leaf, bitmap_cursor, ptr,
303 btrfs_mark_buffer_dirty(leaf);
304 btrfs_release_path(path);
307 bitmap_cursor += data_size;
314 btrfs_abort_transaction(trans, ret);
318 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
319 struct btrfs_block_group_cache *block_group,
320 struct btrfs_path *path)
322 struct btrfs_fs_info *fs_info = trans->fs_info;
323 struct btrfs_root *root = fs_info->free_space_root;
324 struct btrfs_free_space_info *info;
325 struct btrfs_key key, found_key;
326 struct extent_buffer *leaf;
327 unsigned long *bitmap;
329 u32 bitmap_size, flags, expected_extent_count;
330 unsigned long nrbits, start_bit, end_bit;
331 u32 extent_count = 0;
335 bitmap_size = free_space_bitmap_size(block_group->key.offset,
336 fs_info->sectorsize);
337 bitmap = alloc_bitmap(bitmap_size);
343 start = block_group->key.objectid;
344 end = block_group->key.objectid + block_group->key.offset;
346 key.objectid = end - 1;
348 key.offset = (u64)-1;
351 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
355 leaf = path->nodes[0];
358 while (path->slots[0] > 0) {
359 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
361 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
362 ASSERT(found_key.objectid == block_group->key.objectid);
363 ASSERT(found_key.offset == block_group->key.offset);
366 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
369 u32 bitmap_pos, data_size;
371 ASSERT(found_key.objectid >= start);
372 ASSERT(found_key.objectid < end);
373 ASSERT(found_key.objectid + found_key.offset <= end);
375 bitmap_pos = div_u64(found_key.objectid - start,
376 fs_info->sectorsize *
378 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
379 data_size = free_space_bitmap_size(found_key.offset,
380 fs_info->sectorsize);
382 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
383 read_extent_buffer(leaf, bitmap_cursor, ptr,
393 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
396 btrfs_release_path(path);
399 info = search_free_space_info(trans, fs_info, block_group, path, 1);
404 leaf = path->nodes[0];
405 flags = btrfs_free_space_flags(leaf, info);
406 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
407 btrfs_set_free_space_flags(leaf, info, flags);
408 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
409 btrfs_mark_buffer_dirty(leaf);
410 btrfs_release_path(path);
412 nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
413 start_bit = find_next_bit_le(bitmap, nrbits, 0);
415 while (start_bit < nrbits) {
416 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
417 ASSERT(start_bit < end_bit);
419 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
420 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
421 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
423 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
426 btrfs_release_path(path);
430 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
433 if (extent_count != expected_extent_count) {
435 "incorrect extent count for %llu; counted %u, expected %u",
436 block_group->key.objectid, extent_count,
437 expected_extent_count);
447 btrfs_abort_transaction(trans, ret);
451 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
452 struct btrfs_block_group_cache *block_group,
453 struct btrfs_path *path,
456 struct btrfs_free_space_info *info;
461 if (new_extents == 0)
464 info = search_free_space_info(trans, trans->fs_info, block_group, path,
470 flags = btrfs_free_space_flags(path->nodes[0], info);
471 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
473 extent_count += new_extents;
474 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
475 btrfs_mark_buffer_dirty(path->nodes[0]);
476 btrfs_release_path(path);
478 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
479 extent_count > block_group->bitmap_high_thresh) {
480 ret = convert_free_space_to_bitmaps(trans, block_group, path);
481 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
482 extent_count < block_group->bitmap_low_thresh) {
483 ret = convert_free_space_to_extents(trans, block_group, path);
490 int free_space_test_bit(struct btrfs_block_group_cache *block_group,
491 struct btrfs_path *path, u64 offset)
493 struct extent_buffer *leaf;
494 struct btrfs_key key;
495 u64 found_start, found_end;
496 unsigned long ptr, i;
498 leaf = path->nodes[0];
499 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
500 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
502 found_start = key.objectid;
503 found_end = key.objectid + key.offset;
504 ASSERT(offset >= found_start && offset < found_end);
506 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
507 i = div_u64(offset - found_start,
508 block_group->fs_info->sectorsize);
509 return !!extent_buffer_test_bit(leaf, ptr, i);
512 static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
513 struct btrfs_path *path, u64 *start, u64 *size,
516 struct btrfs_fs_info *fs_info = block_group->fs_info;
517 struct extent_buffer *leaf;
518 struct btrfs_key key;
519 u64 end = *start + *size;
520 u64 found_start, found_end;
521 unsigned long ptr, first, last;
523 leaf = path->nodes[0];
524 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
525 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
527 found_start = key.objectid;
528 found_end = key.objectid + key.offset;
529 ASSERT(*start >= found_start && *start < found_end);
530 ASSERT(end > found_start);
535 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
536 first = div_u64(*start - found_start, fs_info->sectorsize);
537 last = div_u64(end - found_start, fs_info->sectorsize);
539 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
541 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
542 btrfs_mark_buffer_dirty(leaf);
544 *size -= end - *start;
549 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
550 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
551 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
554 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root, struct btrfs_path *p)
557 struct btrfs_key key;
559 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
564 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
565 btrfs_release_path(p);
567 key.objectid += key.offset;
569 key.offset = (u64)-1;
571 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
575 * If remove is 1, then we are removing free space, thus clearing bits in the
576 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
579 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
580 struct btrfs_block_group_cache *block_group,
581 struct btrfs_path *path,
582 u64 start, u64 size, int remove)
584 struct btrfs_root *root = block_group->fs_info->free_space_root;
585 struct btrfs_key key;
586 u64 end = start + size;
587 u64 cur_start, cur_size;
588 int prev_bit, next_bit;
593 * Read the bit for the block immediately before the extent of space if
594 * that block is within the block group.
596 if (start > block_group->key.objectid) {
597 u64 prev_block = start - block_group->fs_info->sectorsize;
599 key.objectid = prev_block;
601 key.offset = (u64)-1;
603 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
607 prev_bit = free_space_test_bit(block_group, path, prev_block);
609 /* The previous block may have been in the previous bitmap. */
610 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
611 if (start >= key.objectid + key.offset) {
612 ret = free_space_next_bitmap(trans, root, path);
617 key.objectid = start;
619 key.offset = (u64)-1;
621 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
629 * Iterate over all of the bitmaps overlapped by the extent of space,
630 * clearing/setting bits as required.
635 free_space_set_bits(block_group, path, &cur_start, &cur_size,
639 ret = free_space_next_bitmap(trans, root, path);
645 * Read the bit for the block immediately after the extent of space if
646 * that block is within the block group.
648 if (end < block_group->key.objectid + block_group->key.offset) {
649 /* The next block may be in the next bitmap. */
650 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
651 if (end >= key.objectid + key.offset) {
652 ret = free_space_next_bitmap(trans, root, path);
657 next_bit = free_space_test_bit(block_group, path, end);
665 /* Leftover on the left. */
669 /* Leftover on the right. */
675 /* Merging with neighbor on the left. */
679 /* Merging with neighbor on the right. */
684 btrfs_release_path(path);
685 ret = update_free_space_extent_count(trans, block_group, path,
692 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
693 struct btrfs_fs_info *fs_info,
694 struct btrfs_block_group_cache *block_group,
695 struct btrfs_path *path,
698 struct btrfs_root *root = fs_info->free_space_root;
699 struct btrfs_key key;
700 u64 found_start, found_end;
701 u64 end = start + size;
702 int new_extents = -1;
705 key.objectid = start;
707 key.offset = (u64)-1;
709 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
713 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
715 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
717 found_start = key.objectid;
718 found_end = key.objectid + key.offset;
719 ASSERT(start >= found_start && end <= found_end);
722 * Okay, now that we've found the free space extent which contains the
723 * free space that we are removing, there are four cases:
725 * 1. We're using the whole extent: delete the key we found and
726 * decrement the free space extent count.
727 * 2. We are using part of the extent starting at the beginning: delete
728 * the key we found and insert a new key representing the leftover at
729 * the end. There is no net change in the number of extents.
730 * 3. We are using part of the extent ending at the end: delete the key
731 * we found and insert a new key representing the leftover at the
732 * beginning. There is no net change in the number of extents.
733 * 4. We are using part of the extent in the middle: delete the key we
734 * found and insert two new keys representing the leftovers on each
735 * side. Where we used to have one extent, we now have two, so increment
736 * the extent count. We may need to convert the block group to bitmaps
740 /* Delete the existing key (cases 1-4). */
741 ret = btrfs_del_item(trans, root, path);
745 /* Add a key for leftovers at the beginning (cases 3 and 4). */
746 if (start > found_start) {
747 key.objectid = found_start;
748 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
749 key.offset = start - found_start;
751 btrfs_release_path(path);
752 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
758 /* Add a key for leftovers at the end (cases 2 and 4). */
759 if (end < found_end) {
761 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
762 key.offset = found_end - end;
764 btrfs_release_path(path);
765 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
771 btrfs_release_path(path);
772 ret = update_free_space_extent_count(trans, block_group, path,
779 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
780 struct btrfs_fs_info *fs_info,
781 struct btrfs_block_group_cache *block_group,
782 struct btrfs_path *path, u64 start, u64 size)
784 struct btrfs_free_space_info *info;
788 if (block_group->needs_free_space) {
789 ret = __add_block_group_free_space(trans, block_group, path);
794 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
796 return PTR_ERR(info);
797 flags = btrfs_free_space_flags(path->nodes[0], info);
798 btrfs_release_path(path);
800 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
801 return modify_free_space_bitmap(trans, block_group, path,
804 return remove_free_space_extent(trans, fs_info, block_group,
809 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
810 struct btrfs_fs_info *fs_info,
813 struct btrfs_block_group_cache *block_group;
814 struct btrfs_path *path;
817 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
820 path = btrfs_alloc_path();
826 block_group = btrfs_lookup_block_group(fs_info, start);
833 mutex_lock(&block_group->free_space_lock);
834 ret = __remove_from_free_space_tree(trans, fs_info, block_group, path,
836 mutex_unlock(&block_group->free_space_lock);
838 btrfs_put_block_group(block_group);
840 btrfs_free_path(path);
842 btrfs_abort_transaction(trans, ret);
846 static int add_free_space_extent(struct btrfs_trans_handle *trans,
847 struct btrfs_block_group_cache *block_group,
848 struct btrfs_path *path,
851 struct btrfs_root *root = trans->fs_info->free_space_root;
852 struct btrfs_key key, new_key;
853 u64 found_start, found_end;
854 u64 end = start + size;
859 * We are adding a new extent of free space, but we need to merge
860 * extents. There are four cases here:
862 * 1. The new extent does not have any immediate neighbors to merge
863 * with: add the new key and increment the free space extent count. We
864 * may need to convert the block group to bitmaps as a result.
865 * 2. The new extent has an immediate neighbor before it: remove the
866 * previous key and insert a new key combining both of them. There is no
867 * net change in the number of extents.
868 * 3. The new extent has an immediate neighbor after it: remove the next
869 * key and insert a new key combining both of them. There is no net
870 * change in the number of extents.
871 * 4. The new extent has immediate neighbors on both sides: remove both
872 * of the keys and insert a new key combining all of them. Where we used
873 * to have two extents, we now have one, so decrement the extent count.
876 new_key.objectid = start;
877 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
878 new_key.offset = size;
880 /* Search for a neighbor on the left. */
881 if (start == block_group->key.objectid)
883 key.objectid = start - 1;
885 key.offset = (u64)-1;
887 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
891 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
893 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
894 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
895 btrfs_release_path(path);
899 found_start = key.objectid;
900 found_end = key.objectid + key.offset;
901 ASSERT(found_start >= block_group->key.objectid &&
902 found_end > block_group->key.objectid);
903 ASSERT(found_start < start && found_end <= start);
906 * Delete the neighbor on the left and absorb it into the new key (cases
909 if (found_end == start) {
910 ret = btrfs_del_item(trans, root, path);
913 new_key.objectid = found_start;
914 new_key.offset += key.offset;
917 btrfs_release_path(path);
920 /* Search for a neighbor on the right. */
921 if (end == block_group->key.objectid + block_group->key.offset)
925 key.offset = (u64)-1;
927 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
931 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
933 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
934 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
935 btrfs_release_path(path);
939 found_start = key.objectid;
940 found_end = key.objectid + key.offset;
941 ASSERT(found_start >= block_group->key.objectid &&
942 found_end > block_group->key.objectid);
943 ASSERT((found_start < start && found_end <= start) ||
944 (found_start >= end && found_end > end));
947 * Delete the neighbor on the right and absorb it into the new key
950 if (found_start == end) {
951 ret = btrfs_del_item(trans, root, path);
954 new_key.offset += key.offset;
957 btrfs_release_path(path);
960 /* Insert the new key (cases 1-4). */
961 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
965 btrfs_release_path(path);
966 ret = update_free_space_extent_count(trans, block_group, path,
973 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
974 struct btrfs_block_group_cache *block_group,
975 struct btrfs_path *path, u64 start, u64 size)
977 struct btrfs_fs_info *fs_info = trans->fs_info;
978 struct btrfs_free_space_info *info;
982 if (block_group->needs_free_space) {
983 ret = __add_block_group_free_space(trans, block_group, path);
988 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
990 return PTR_ERR(info);
991 flags = btrfs_free_space_flags(path->nodes[0], info);
992 btrfs_release_path(path);
994 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
995 return modify_free_space_bitmap(trans, block_group, path,
998 return add_free_space_extent(trans, block_group, path, start,
1003 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1004 struct btrfs_fs_info *fs_info,
1005 u64 start, u64 size)
1007 struct btrfs_block_group_cache *block_group;
1008 struct btrfs_path *path;
1011 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1014 path = btrfs_alloc_path();
1020 block_group = btrfs_lookup_block_group(fs_info, start);
1027 mutex_lock(&block_group->free_space_lock);
1028 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1029 mutex_unlock(&block_group->free_space_lock);
1031 btrfs_put_block_group(block_group);
1033 btrfs_free_path(path);
1035 btrfs_abort_transaction(trans, ret);
1040 * Populate the free space tree by walking the extent tree. Operations on the
1041 * extent tree that happen as a result of writes to the free space tree will go
1042 * through the normal add/remove hooks.
1044 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1045 struct btrfs_fs_info *fs_info,
1046 struct btrfs_block_group_cache *block_group)
1048 struct btrfs_root *extent_root = fs_info->extent_root;
1049 struct btrfs_path *path, *path2;
1050 struct btrfs_key key;
1054 path = btrfs_alloc_path();
1057 path->reada = READA_FORWARD;
1059 path2 = btrfs_alloc_path();
1061 btrfs_free_path(path);
1065 ret = add_new_free_space_info(trans, block_group, path2);
1069 mutex_lock(&block_group->free_space_lock);
1072 * Iterate through all of the extent and metadata items in this block
1073 * group, adding the free space between them and the free space at the
1074 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1075 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1078 key.objectid = block_group->key.objectid;
1079 key.type = BTRFS_EXTENT_ITEM_KEY;
1082 ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1087 start = block_group->key.objectid;
1088 end = block_group->key.objectid + block_group->key.offset;
1090 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1092 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1093 key.type == BTRFS_METADATA_ITEM_KEY) {
1094 if (key.objectid >= end)
1097 if (start < key.objectid) {
1098 ret = __add_to_free_space_tree(trans,
1106 start = key.objectid;
1107 if (key.type == BTRFS_METADATA_ITEM_KEY)
1108 start += fs_info->nodesize;
1110 start += key.offset;
1111 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1112 if (key.objectid != block_group->key.objectid)
1116 ret = btrfs_next_item(extent_root, path);
1123 ret = __add_to_free_space_tree(trans, block_group, path2,
1124 start, end - start);
1131 mutex_unlock(&block_group->free_space_lock);
1133 btrfs_free_path(path2);
1134 btrfs_free_path(path);
1138 int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1140 struct btrfs_trans_handle *trans;
1141 struct btrfs_root *tree_root = fs_info->tree_root;
1142 struct btrfs_root *free_space_root;
1143 struct btrfs_block_group_cache *block_group;
1144 struct rb_node *node;
1147 trans = btrfs_start_transaction(tree_root, 0);
1149 return PTR_ERR(trans);
1151 set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1152 free_space_root = btrfs_create_tree(trans, fs_info,
1153 BTRFS_FREE_SPACE_TREE_OBJECTID);
1154 if (IS_ERR(free_space_root)) {
1155 ret = PTR_ERR(free_space_root);
1158 fs_info->free_space_root = free_space_root;
1160 node = rb_first(&fs_info->block_group_cache_tree);
1162 block_group = rb_entry(node, struct btrfs_block_group_cache,
1164 ret = populate_free_space_tree(trans, fs_info, block_group);
1167 node = rb_next(node);
1170 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1171 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1172 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1174 return btrfs_commit_transaction(trans);
1177 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1178 btrfs_abort_transaction(trans, ret);
1179 btrfs_end_transaction(trans);
1183 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root)
1186 struct btrfs_path *path;
1187 struct btrfs_key key;
1191 path = btrfs_alloc_path();
1195 path->leave_spinning = 1;
1202 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1206 nr = btrfs_header_nritems(path->nodes[0]);
1211 ret = btrfs_del_items(trans, root, path, 0, nr);
1215 btrfs_release_path(path);
1220 btrfs_free_path(path);
1224 int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1226 struct btrfs_trans_handle *trans;
1227 struct btrfs_root *tree_root = fs_info->tree_root;
1228 struct btrfs_root *free_space_root = fs_info->free_space_root;
1231 trans = btrfs_start_transaction(tree_root, 0);
1233 return PTR_ERR(trans);
1235 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1236 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1237 fs_info->free_space_root = NULL;
1239 ret = clear_free_space_tree(trans, free_space_root);
1243 ret = btrfs_del_root(trans, fs_info, &free_space_root->root_key);
1247 list_del(&free_space_root->dirty_list);
1249 btrfs_tree_lock(free_space_root->node);
1250 clean_tree_block(fs_info, free_space_root->node);
1251 btrfs_tree_unlock(free_space_root->node);
1252 btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1255 free_extent_buffer(free_space_root->node);
1256 free_extent_buffer(free_space_root->commit_root);
1257 kfree(free_space_root);
1259 return btrfs_commit_transaction(trans);
1262 btrfs_abort_transaction(trans, ret);
1263 btrfs_end_transaction(trans);
1267 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1268 struct btrfs_block_group_cache *block_group,
1269 struct btrfs_path *path)
1273 block_group->needs_free_space = 0;
1275 ret = add_new_free_space_info(trans, block_group, path);
1279 return __add_to_free_space_tree(trans, block_group, path,
1280 block_group->key.objectid,
1281 block_group->key.offset);
1284 int add_block_group_free_space(struct btrfs_trans_handle *trans,
1285 struct btrfs_block_group_cache *block_group)
1287 struct btrfs_fs_info *fs_info = trans->fs_info;
1288 struct btrfs_path *path = NULL;
1291 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1294 mutex_lock(&block_group->free_space_lock);
1295 if (!block_group->needs_free_space)
1298 path = btrfs_alloc_path();
1304 ret = __add_block_group_free_space(trans, block_group, path);
1307 btrfs_free_path(path);
1308 mutex_unlock(&block_group->free_space_lock);
1310 btrfs_abort_transaction(trans, ret);
1314 int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1315 struct btrfs_block_group_cache *block_group)
1317 struct btrfs_root *root = trans->fs_info->free_space_root;
1318 struct btrfs_path *path;
1319 struct btrfs_key key, found_key;
1320 struct extent_buffer *leaf;
1325 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1328 if (block_group->needs_free_space) {
1329 /* We never added this block group to the free space tree. */
1333 path = btrfs_alloc_path();
1339 start = block_group->key.objectid;
1340 end = block_group->key.objectid + block_group->key.offset;
1342 key.objectid = end - 1;
1344 key.offset = (u64)-1;
1347 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1351 leaf = path->nodes[0];
1354 while (path->slots[0] > 0) {
1355 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1357 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1358 ASSERT(found_key.objectid == block_group->key.objectid);
1359 ASSERT(found_key.offset == block_group->key.offset);
1364 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1365 found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1366 ASSERT(found_key.objectid >= start);
1367 ASSERT(found_key.objectid < end);
1368 ASSERT(found_key.objectid + found_key.offset <= end);
1376 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1379 btrfs_release_path(path);
1384 btrfs_free_path(path);
1386 btrfs_abort_transaction(trans, ret);
1390 static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1391 struct btrfs_path *path,
1392 u32 expected_extent_count)
1394 struct btrfs_block_group_cache *block_group;
1395 struct btrfs_fs_info *fs_info;
1396 struct btrfs_root *root;
1397 struct btrfs_key key;
1398 int prev_bit = 0, bit;
1399 /* Initialize to silence GCC. */
1400 u64 extent_start = 0;
1402 u64 total_found = 0;
1403 u32 extent_count = 0;
1406 block_group = caching_ctl->block_group;
1407 fs_info = block_group->fs_info;
1408 root = fs_info->free_space_root;
1410 end = block_group->key.objectid + block_group->key.offset;
1413 ret = btrfs_next_item(root, path);
1419 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1421 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1424 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1425 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1427 caching_ctl->progress = key.objectid;
1429 offset = key.objectid;
1430 while (offset < key.objectid + key.offset) {
1431 bit = free_space_test_bit(block_group, path, offset);
1432 if (prev_bit == 0 && bit == 1) {
1433 extent_start = offset;
1434 } else if (prev_bit == 1 && bit == 0) {
1435 total_found += add_new_free_space(block_group,
1438 if (total_found > CACHING_CTL_WAKE_UP) {
1440 wake_up(&caching_ctl->wait);
1445 offset += fs_info->sectorsize;
1448 if (prev_bit == 1) {
1449 total_found += add_new_free_space(block_group, extent_start,
1454 if (extent_count != expected_extent_count) {
1456 "incorrect extent count for %llu; counted %u, expected %u",
1457 block_group->key.objectid, extent_count,
1458 expected_extent_count);
1464 caching_ctl->progress = (u64)-1;
1471 static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1472 struct btrfs_path *path,
1473 u32 expected_extent_count)
1475 struct btrfs_block_group_cache *block_group;
1476 struct btrfs_fs_info *fs_info;
1477 struct btrfs_root *root;
1478 struct btrfs_key key;
1480 u64 total_found = 0;
1481 u32 extent_count = 0;
1484 block_group = caching_ctl->block_group;
1485 fs_info = block_group->fs_info;
1486 root = fs_info->free_space_root;
1488 end = block_group->key.objectid + block_group->key.offset;
1491 ret = btrfs_next_item(root, path);
1497 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1499 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1502 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1503 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1505 caching_ctl->progress = key.objectid;
1507 total_found += add_new_free_space(block_group, key.objectid,
1508 key.objectid + key.offset);
1509 if (total_found > CACHING_CTL_WAKE_UP) {
1511 wake_up(&caching_ctl->wait);
1516 if (extent_count != expected_extent_count) {
1518 "incorrect extent count for %llu; counted %u, expected %u",
1519 block_group->key.objectid, extent_count,
1520 expected_extent_count);
1526 caching_ctl->progress = (u64)-1;
1533 int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1535 struct btrfs_block_group_cache *block_group;
1536 struct btrfs_fs_info *fs_info;
1537 struct btrfs_free_space_info *info;
1538 struct btrfs_path *path;
1539 u32 extent_count, flags;
1542 block_group = caching_ctl->block_group;
1543 fs_info = block_group->fs_info;
1545 path = btrfs_alloc_path();
1550 * Just like caching_thread() doesn't want to deadlock on the extent
1551 * tree, we don't want to deadlock on the free space tree.
1553 path->skip_locking = 1;
1554 path->search_commit_root = 1;
1555 path->reada = READA_FORWARD;
1557 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
1559 ret = PTR_ERR(info);
1562 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1563 flags = btrfs_free_space_flags(path->nodes[0], info);
1566 * We left path pointing to the free space info item, so now
1567 * load_free_space_foo can just iterate through the free space tree from
1570 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1571 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1573 ret = load_free_space_extents(caching_ctl, path, extent_count);
1576 btrfs_free_path(path);