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"
13 #include "block-group.h"
15 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
16 struct btrfs_block_group *block_group,
17 struct btrfs_path *path);
19 static struct btrfs_root *btrfs_free_space_root(
20 struct btrfs_block_group *block_group)
22 struct btrfs_key key = {
23 .objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
24 .type = BTRFS_ROOT_ITEM_KEY,
28 if (btrfs_fs_incompat(block_group->fs_info, EXTENT_TREE_V2))
29 key.offset = block_group->global_root_id;
30 return btrfs_global_root(block_group->fs_info, &key);
33 void set_free_space_tree_thresholds(struct btrfs_block_group *cache)
37 u64 num_bitmaps, total_bitmap_size;
39 if (WARN_ON(cache->length == 0))
40 btrfs_warn(cache->fs_info, "block group %llu length is zero",
44 * We convert to bitmaps when the disk space required for using extents
45 * exceeds that required for using bitmaps.
47 bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
48 num_bitmaps = div_u64(cache->length + bitmap_range - 1, bitmap_range);
49 bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
50 total_bitmap_size = num_bitmaps * bitmap_size;
51 cache->bitmap_high_thresh = div_u64(total_bitmap_size,
52 sizeof(struct btrfs_item));
55 * We allow for a small buffer between the high threshold and low
56 * threshold to avoid thrashing back and forth between the two formats.
58 if (cache->bitmap_high_thresh > 100)
59 cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
61 cache->bitmap_low_thresh = 0;
64 static int add_new_free_space_info(struct btrfs_trans_handle *trans,
65 struct btrfs_block_group *block_group,
66 struct btrfs_path *path)
68 struct btrfs_root *root = btrfs_free_space_root(block_group);
69 struct btrfs_free_space_info *info;
71 struct extent_buffer *leaf;
74 key.objectid = block_group->start;
75 key.type = BTRFS_FREE_SPACE_INFO_KEY;
76 key.offset = block_group->length;
78 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
82 leaf = path->nodes[0];
83 info = btrfs_item_ptr(leaf, path->slots[0],
84 struct btrfs_free_space_info);
85 btrfs_set_free_space_extent_count(leaf, info, 0);
86 btrfs_set_free_space_flags(leaf, info, 0);
87 btrfs_mark_buffer_dirty(leaf);
91 btrfs_release_path(path);
96 struct btrfs_free_space_info *search_free_space_info(
97 struct btrfs_trans_handle *trans,
98 struct btrfs_block_group *block_group,
99 struct btrfs_path *path, int cow)
101 struct btrfs_fs_info *fs_info = block_group->fs_info;
102 struct btrfs_root *root = btrfs_free_space_root(block_group);
103 struct btrfs_key key;
106 key.objectid = block_group->start;
107 key.type = BTRFS_FREE_SPACE_INFO_KEY;
108 key.offset = block_group->length;
110 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
114 btrfs_warn(fs_info, "missing free space info for %llu",
117 return ERR_PTR(-ENOENT);
120 return btrfs_item_ptr(path->nodes[0], path->slots[0],
121 struct btrfs_free_space_info);
125 * btrfs_search_slot() but we're looking for the greatest key less than the
128 static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
129 struct btrfs_root *root,
130 struct btrfs_key *key, struct btrfs_path *p,
131 int ins_len, int cow)
135 ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
144 if (p->slots[0] == 0) {
153 static inline u32 free_space_bitmap_size(const struct btrfs_fs_info *fs_info,
156 return DIV_ROUND_UP(size >> fs_info->sectorsize_bits, BITS_PER_BYTE);
159 static unsigned long *alloc_bitmap(u32 bitmap_size)
162 unsigned int nofs_flag;
163 u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
166 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
167 * into the filesystem as the free space bitmap can be modified in the
168 * critical section of a transaction commit.
170 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
171 * know that recursion is unsafe.
173 nofs_flag = memalloc_nofs_save();
174 ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
175 memalloc_nofs_restore(nofs_flag);
179 static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
181 u8 *p = ((u8 *)map) + BIT_BYTE(start);
182 const unsigned int size = start + len;
183 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
184 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
186 while (len - bits_to_set >= 0) {
189 bits_to_set = BITS_PER_BYTE;
194 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
200 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
201 struct btrfs_block_group *block_group,
202 struct btrfs_path *path)
204 struct btrfs_fs_info *fs_info = trans->fs_info;
205 struct btrfs_root *root = btrfs_free_space_root(block_group);
206 struct btrfs_free_space_info *info;
207 struct btrfs_key key, found_key;
208 struct extent_buffer *leaf;
209 unsigned long *bitmap;
213 u32 bitmap_size, flags, expected_extent_count;
214 u32 extent_count = 0;
218 bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
219 bitmap = alloc_bitmap(bitmap_size);
225 start = block_group->start;
226 end = block_group->start + block_group->length;
228 key.objectid = end - 1;
230 key.offset = (u64)-1;
233 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
237 leaf = path->nodes[0];
240 while (path->slots[0] > 0) {
241 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
243 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
244 ASSERT(found_key.objectid == block_group->start);
245 ASSERT(found_key.offset == block_group->length);
248 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
251 ASSERT(found_key.objectid >= start);
252 ASSERT(found_key.objectid < end);
253 ASSERT(found_key.objectid + found_key.offset <= end);
255 first = div_u64(found_key.objectid - start,
256 fs_info->sectorsize);
257 last = div_u64(found_key.objectid + found_key.offset - start,
258 fs_info->sectorsize);
259 le_bitmap_set(bitmap, first, last - first);
269 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
272 btrfs_release_path(path);
275 info = search_free_space_info(trans, block_group, path, 1);
280 leaf = path->nodes[0];
281 flags = btrfs_free_space_flags(leaf, info);
282 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
283 btrfs_set_free_space_flags(leaf, info, flags);
284 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
285 btrfs_mark_buffer_dirty(leaf);
286 btrfs_release_path(path);
288 if (extent_count != expected_extent_count) {
290 "incorrect extent count for %llu; counted %u, expected %u",
291 block_group->start, extent_count,
292 expected_extent_count);
298 bitmap_cursor = (char *)bitmap;
299 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
306 extent_size = min(end - i, bitmap_range);
307 data_size = free_space_bitmap_size(fs_info, extent_size);
310 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
311 key.offset = extent_size;
313 ret = btrfs_insert_empty_item(trans, root, path, &key,
318 leaf = path->nodes[0];
319 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
320 write_extent_buffer(leaf, bitmap_cursor, ptr,
322 btrfs_mark_buffer_dirty(leaf);
323 btrfs_release_path(path);
326 bitmap_cursor += data_size;
333 btrfs_abort_transaction(trans, ret);
338 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
339 struct btrfs_block_group *block_group,
340 struct btrfs_path *path)
342 struct btrfs_fs_info *fs_info = trans->fs_info;
343 struct btrfs_root *root = btrfs_free_space_root(block_group);
344 struct btrfs_free_space_info *info;
345 struct btrfs_key key, found_key;
346 struct extent_buffer *leaf;
347 unsigned long *bitmap;
349 u32 bitmap_size, flags, expected_extent_count;
350 unsigned long nrbits, start_bit, end_bit;
351 u32 extent_count = 0;
355 bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
356 bitmap = alloc_bitmap(bitmap_size);
362 start = block_group->start;
363 end = block_group->start + block_group->length;
365 key.objectid = end - 1;
367 key.offset = (u64)-1;
370 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
374 leaf = path->nodes[0];
377 while (path->slots[0] > 0) {
378 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
380 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
381 ASSERT(found_key.objectid == block_group->start);
382 ASSERT(found_key.offset == block_group->length);
385 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
388 u32 bitmap_pos, data_size;
390 ASSERT(found_key.objectid >= start);
391 ASSERT(found_key.objectid < end);
392 ASSERT(found_key.objectid + found_key.offset <= end);
394 bitmap_pos = div_u64(found_key.objectid - start,
395 fs_info->sectorsize *
397 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
398 data_size = free_space_bitmap_size(fs_info,
401 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
402 read_extent_buffer(leaf, bitmap_cursor, ptr,
412 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
415 btrfs_release_path(path);
418 info = search_free_space_info(trans, block_group, path, 1);
423 leaf = path->nodes[0];
424 flags = btrfs_free_space_flags(leaf, info);
425 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
426 btrfs_set_free_space_flags(leaf, info, flags);
427 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
428 btrfs_mark_buffer_dirty(leaf);
429 btrfs_release_path(path);
431 nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;
432 start_bit = find_next_bit_le(bitmap, nrbits, 0);
434 while (start_bit < nrbits) {
435 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
436 ASSERT(start_bit < end_bit);
438 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
439 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
440 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
442 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
445 btrfs_release_path(path);
449 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
452 if (extent_count != expected_extent_count) {
454 "incorrect extent count for %llu; counted %u, expected %u",
455 block_group->start, extent_count,
456 expected_extent_count);
466 btrfs_abort_transaction(trans, ret);
470 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
471 struct btrfs_block_group *block_group,
472 struct btrfs_path *path,
475 struct btrfs_free_space_info *info;
480 if (new_extents == 0)
483 info = search_free_space_info(trans, block_group, path, 1);
488 flags = btrfs_free_space_flags(path->nodes[0], info);
489 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
491 extent_count += new_extents;
492 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
493 btrfs_mark_buffer_dirty(path->nodes[0]);
494 btrfs_release_path(path);
496 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
497 extent_count > block_group->bitmap_high_thresh) {
498 ret = convert_free_space_to_bitmaps(trans, block_group, path);
499 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
500 extent_count < block_group->bitmap_low_thresh) {
501 ret = convert_free_space_to_extents(trans, block_group, path);
509 int free_space_test_bit(struct btrfs_block_group *block_group,
510 struct btrfs_path *path, u64 offset)
512 struct extent_buffer *leaf;
513 struct btrfs_key key;
514 u64 found_start, found_end;
515 unsigned long ptr, i;
517 leaf = path->nodes[0];
518 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
519 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
521 found_start = key.objectid;
522 found_end = key.objectid + key.offset;
523 ASSERT(offset >= found_start && offset < found_end);
525 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
526 i = div_u64(offset - found_start,
527 block_group->fs_info->sectorsize);
528 return !!extent_buffer_test_bit(leaf, ptr, i);
531 static void free_space_set_bits(struct btrfs_block_group *block_group,
532 struct btrfs_path *path, u64 *start, u64 *size,
535 struct btrfs_fs_info *fs_info = block_group->fs_info;
536 struct extent_buffer *leaf;
537 struct btrfs_key key;
538 u64 end = *start + *size;
539 u64 found_start, found_end;
540 unsigned long ptr, first, last;
542 leaf = path->nodes[0];
543 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
544 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
546 found_start = key.objectid;
547 found_end = key.objectid + key.offset;
548 ASSERT(*start >= found_start && *start < found_end);
549 ASSERT(end > found_start);
554 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
555 first = (*start - found_start) >> fs_info->sectorsize_bits;
556 last = (end - found_start) >> fs_info->sectorsize_bits;
558 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
560 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
561 btrfs_mark_buffer_dirty(leaf);
563 *size -= end - *start;
568 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
569 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
570 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
573 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
574 struct btrfs_root *root, struct btrfs_path *p)
576 struct btrfs_key key;
578 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
583 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
584 btrfs_release_path(p);
586 key.objectid += key.offset;
588 key.offset = (u64)-1;
590 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
594 * If remove is 1, then we are removing free space, thus clearing bits in the
595 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
598 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
599 struct btrfs_block_group *block_group,
600 struct btrfs_path *path,
601 u64 start, u64 size, int remove)
603 struct btrfs_root *root = btrfs_free_space_root(block_group);
604 struct btrfs_key key;
605 u64 end = start + size;
606 u64 cur_start, cur_size;
607 int prev_bit, next_bit;
612 * Read the bit for the block immediately before the extent of space if
613 * that block is within the block group.
615 if (start > block_group->start) {
616 u64 prev_block = start - block_group->fs_info->sectorsize;
618 key.objectid = prev_block;
620 key.offset = (u64)-1;
622 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
626 prev_bit = free_space_test_bit(block_group, path, prev_block);
628 /* The previous block may have been in the previous bitmap. */
629 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
630 if (start >= key.objectid + key.offset) {
631 ret = free_space_next_bitmap(trans, root, path);
636 key.objectid = start;
638 key.offset = (u64)-1;
640 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
648 * Iterate over all of the bitmaps overlapped by the extent of space,
649 * clearing/setting bits as required.
654 free_space_set_bits(block_group, path, &cur_start, &cur_size,
658 ret = free_space_next_bitmap(trans, root, path);
664 * Read the bit for the block immediately after the extent of space if
665 * that block is within the block group.
667 if (end < block_group->start + block_group->length) {
668 /* The next block may be in the next bitmap. */
669 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
670 if (end >= key.objectid + key.offset) {
671 ret = free_space_next_bitmap(trans, root, path);
676 next_bit = free_space_test_bit(block_group, path, end);
684 /* Leftover on the left. */
688 /* Leftover on the right. */
694 /* Merging with neighbor on the left. */
698 /* Merging with neighbor on the right. */
703 btrfs_release_path(path);
704 ret = update_free_space_extent_count(trans, block_group, path,
711 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
712 struct btrfs_block_group *block_group,
713 struct btrfs_path *path,
716 struct btrfs_root *root = btrfs_free_space_root(block_group);
717 struct btrfs_key key;
718 u64 found_start, found_end;
719 u64 end = start + size;
720 int new_extents = -1;
723 key.objectid = start;
725 key.offset = (u64)-1;
727 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
731 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
733 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
735 found_start = key.objectid;
736 found_end = key.objectid + key.offset;
737 ASSERT(start >= found_start && end <= found_end);
740 * Okay, now that we've found the free space extent which contains the
741 * free space that we are removing, there are four cases:
743 * 1. We're using the whole extent: delete the key we found and
744 * decrement the free space extent count.
745 * 2. We are using part of the extent starting at the beginning: delete
746 * the key we found and insert a new key representing the leftover at
747 * the end. There is no net change in the number of extents.
748 * 3. We are using part of the extent ending at the end: delete the key
749 * we found and insert a new key representing the leftover at the
750 * beginning. There is no net change in the number of extents.
751 * 4. We are using part of the extent in the middle: delete the key we
752 * found and insert two new keys representing the leftovers on each
753 * side. Where we used to have one extent, we now have two, so increment
754 * the extent count. We may need to convert the block group to bitmaps
758 /* Delete the existing key (cases 1-4). */
759 ret = btrfs_del_item(trans, root, path);
763 /* Add a key for leftovers at the beginning (cases 3 and 4). */
764 if (start > found_start) {
765 key.objectid = found_start;
766 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
767 key.offset = start - found_start;
769 btrfs_release_path(path);
770 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
776 /* Add a key for leftovers at the end (cases 2 and 4). */
777 if (end < found_end) {
779 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
780 key.offset = found_end - end;
782 btrfs_release_path(path);
783 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
789 btrfs_release_path(path);
790 ret = update_free_space_extent_count(trans, block_group, path,
798 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
799 struct btrfs_block_group *block_group,
800 struct btrfs_path *path, u64 start, u64 size)
802 struct btrfs_free_space_info *info;
806 if (block_group->needs_free_space) {
807 ret = __add_block_group_free_space(trans, block_group, path);
812 info = search_free_space_info(NULL, block_group, path, 0);
814 return PTR_ERR(info);
815 flags = btrfs_free_space_flags(path->nodes[0], info);
816 btrfs_release_path(path);
818 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
819 return modify_free_space_bitmap(trans, block_group, path,
822 return remove_free_space_extent(trans, block_group, path,
827 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
830 struct btrfs_block_group *block_group;
831 struct btrfs_path *path;
834 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
837 path = btrfs_alloc_path();
843 block_group = btrfs_lookup_block_group(trans->fs_info, start);
850 mutex_lock(&block_group->free_space_lock);
851 ret = __remove_from_free_space_tree(trans, block_group, path, start,
853 mutex_unlock(&block_group->free_space_lock);
855 btrfs_put_block_group(block_group);
857 btrfs_free_path(path);
859 btrfs_abort_transaction(trans, ret);
863 static int add_free_space_extent(struct btrfs_trans_handle *trans,
864 struct btrfs_block_group *block_group,
865 struct btrfs_path *path,
868 struct btrfs_root *root = btrfs_free_space_root(block_group);
869 struct btrfs_key key, new_key;
870 u64 found_start, found_end;
871 u64 end = start + size;
876 * We are adding a new extent of free space, but we need to merge
877 * extents. There are four cases here:
879 * 1. The new extent does not have any immediate neighbors to merge
880 * with: add the new key and increment the free space extent count. We
881 * may need to convert the block group to bitmaps as a result.
882 * 2. The new extent has an immediate neighbor before it: remove the
883 * previous key and insert a new key combining both of them. There is no
884 * net change in the number of extents.
885 * 3. The new extent has an immediate neighbor after it: remove the next
886 * key and insert a new key combining both of them. There is no net
887 * change in the number of extents.
888 * 4. The new extent has immediate neighbors on both sides: remove both
889 * of the keys and insert a new key combining all of them. Where we used
890 * to have two extents, we now have one, so decrement the extent count.
893 new_key.objectid = start;
894 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
895 new_key.offset = size;
897 /* Search for a neighbor on the left. */
898 if (start == block_group->start)
900 key.objectid = start - 1;
902 key.offset = (u64)-1;
904 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
908 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
910 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
911 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
912 btrfs_release_path(path);
916 found_start = key.objectid;
917 found_end = key.objectid + key.offset;
918 ASSERT(found_start >= block_group->start &&
919 found_end > block_group->start);
920 ASSERT(found_start < start && found_end <= start);
923 * Delete the neighbor on the left and absorb it into the new key (cases
926 if (found_end == start) {
927 ret = btrfs_del_item(trans, root, path);
930 new_key.objectid = found_start;
931 new_key.offset += key.offset;
934 btrfs_release_path(path);
937 /* Search for a neighbor on the right. */
938 if (end == block_group->start + block_group->length)
942 key.offset = (u64)-1;
944 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
948 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
950 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
951 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
952 btrfs_release_path(path);
956 found_start = key.objectid;
957 found_end = key.objectid + key.offset;
958 ASSERT(found_start >= block_group->start &&
959 found_end > block_group->start);
960 ASSERT((found_start < start && found_end <= start) ||
961 (found_start >= end && found_end > end));
964 * Delete the neighbor on the right and absorb it into the new key
967 if (found_start == end) {
968 ret = btrfs_del_item(trans, root, path);
971 new_key.offset += key.offset;
974 btrfs_release_path(path);
977 /* Insert the new key (cases 1-4). */
978 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
982 btrfs_release_path(path);
983 ret = update_free_space_extent_count(trans, block_group, path,
991 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
992 struct btrfs_block_group *block_group,
993 struct btrfs_path *path, u64 start, u64 size)
995 struct btrfs_free_space_info *info;
999 if (block_group->needs_free_space) {
1000 ret = __add_block_group_free_space(trans, block_group, path);
1005 info = search_free_space_info(NULL, block_group, path, 0);
1007 return PTR_ERR(info);
1008 flags = btrfs_free_space_flags(path->nodes[0], info);
1009 btrfs_release_path(path);
1011 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
1012 return modify_free_space_bitmap(trans, block_group, path,
1015 return add_free_space_extent(trans, block_group, path, start,
1020 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1021 u64 start, u64 size)
1023 struct btrfs_block_group *block_group;
1024 struct btrfs_path *path;
1027 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1030 path = btrfs_alloc_path();
1036 block_group = btrfs_lookup_block_group(trans->fs_info, start);
1043 mutex_lock(&block_group->free_space_lock);
1044 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1045 mutex_unlock(&block_group->free_space_lock);
1047 btrfs_put_block_group(block_group);
1049 btrfs_free_path(path);
1051 btrfs_abort_transaction(trans, ret);
1056 * Populate the free space tree by walking the extent tree. Operations on the
1057 * extent tree that happen as a result of writes to the free space tree will go
1058 * through the normal add/remove hooks.
1060 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1061 struct btrfs_block_group *block_group)
1063 struct btrfs_root *extent_root;
1064 struct btrfs_path *path, *path2;
1065 struct btrfs_key key;
1069 path = btrfs_alloc_path();
1072 path->reada = READA_FORWARD;
1074 path2 = btrfs_alloc_path();
1076 btrfs_free_path(path);
1080 ret = add_new_free_space_info(trans, block_group, path2);
1084 mutex_lock(&block_group->free_space_lock);
1087 * Iterate through all of the extent and metadata items in this block
1088 * group, adding the free space between them and the free space at the
1089 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1090 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1093 key.objectid = block_group->start;
1094 key.type = BTRFS_EXTENT_ITEM_KEY;
1097 extent_root = btrfs_extent_root(trans->fs_info, key.objectid);
1098 ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1103 start = block_group->start;
1104 end = block_group->start + block_group->length;
1106 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1108 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1109 key.type == BTRFS_METADATA_ITEM_KEY) {
1110 if (key.objectid >= end)
1113 if (start < key.objectid) {
1114 ret = __add_to_free_space_tree(trans,
1122 start = key.objectid;
1123 if (key.type == BTRFS_METADATA_ITEM_KEY)
1124 start += trans->fs_info->nodesize;
1126 start += key.offset;
1127 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1128 if (key.objectid != block_group->start)
1132 ret = btrfs_next_item(extent_root, path);
1139 ret = __add_to_free_space_tree(trans, block_group, path2,
1140 start, end - start);
1147 mutex_unlock(&block_group->free_space_lock);
1149 btrfs_free_path(path2);
1150 btrfs_free_path(path);
1154 int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1156 struct btrfs_trans_handle *trans;
1157 struct btrfs_root *tree_root = fs_info->tree_root;
1158 struct btrfs_root *free_space_root;
1159 struct btrfs_block_group *block_group;
1160 struct rb_node *node;
1163 trans = btrfs_start_transaction(tree_root, 0);
1165 return PTR_ERR(trans);
1167 set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1168 set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1169 free_space_root = btrfs_create_tree(trans,
1170 BTRFS_FREE_SPACE_TREE_OBJECTID);
1171 if (IS_ERR(free_space_root)) {
1172 ret = PTR_ERR(free_space_root);
1175 ret = btrfs_global_root_insert(free_space_root);
1177 btrfs_put_root(free_space_root);
1181 node = rb_first_cached(&fs_info->block_group_cache_tree);
1183 block_group = rb_entry(node, struct btrfs_block_group,
1185 ret = populate_free_space_tree(trans, block_group);
1188 node = rb_next(node);
1191 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1192 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1193 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1194 ret = btrfs_commit_transaction(trans);
1197 * Now that we've committed the transaction any reading of our commit
1198 * root will be safe, so we can cache from the free space tree now.
1200 clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1204 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1205 clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1206 btrfs_abort_transaction(trans, ret);
1207 btrfs_end_transaction(trans);
1211 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1212 struct btrfs_root *root)
1214 struct btrfs_path *path;
1215 struct btrfs_key key;
1219 path = btrfs_alloc_path();
1228 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1232 nr = btrfs_header_nritems(path->nodes[0]);
1237 ret = btrfs_del_items(trans, root, path, 0, nr);
1241 btrfs_release_path(path);
1246 btrfs_free_path(path);
1250 int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1252 struct btrfs_trans_handle *trans;
1253 struct btrfs_root *tree_root = fs_info->tree_root;
1254 struct btrfs_key key = {
1255 .objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
1256 .type = BTRFS_ROOT_ITEM_KEY,
1259 struct btrfs_root *free_space_root = btrfs_global_root(fs_info, &key);
1262 trans = btrfs_start_transaction(tree_root, 0);
1264 return PTR_ERR(trans);
1266 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1267 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1269 ret = clear_free_space_tree(trans, free_space_root);
1273 ret = btrfs_del_root(trans, &free_space_root->root_key);
1277 btrfs_global_root_delete(free_space_root);
1278 list_del(&free_space_root->dirty_list);
1280 btrfs_tree_lock(free_space_root->node);
1281 btrfs_clean_tree_block(free_space_root->node);
1282 btrfs_tree_unlock(free_space_root->node);
1283 btrfs_free_tree_block(trans, btrfs_root_id(free_space_root),
1284 free_space_root->node, 0, 1);
1286 btrfs_put_root(free_space_root);
1288 return btrfs_commit_transaction(trans);
1291 btrfs_abort_transaction(trans, ret);
1292 btrfs_end_transaction(trans);
1296 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1297 struct btrfs_block_group *block_group,
1298 struct btrfs_path *path)
1302 block_group->needs_free_space = 0;
1304 ret = add_new_free_space_info(trans, block_group, path);
1308 return __add_to_free_space_tree(trans, block_group, path,
1310 block_group->length);
1313 int add_block_group_free_space(struct btrfs_trans_handle *trans,
1314 struct btrfs_block_group *block_group)
1316 struct btrfs_fs_info *fs_info = trans->fs_info;
1317 struct btrfs_path *path = NULL;
1320 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1323 mutex_lock(&block_group->free_space_lock);
1324 if (!block_group->needs_free_space)
1327 path = btrfs_alloc_path();
1333 ret = __add_block_group_free_space(trans, block_group, path);
1336 btrfs_free_path(path);
1337 mutex_unlock(&block_group->free_space_lock);
1339 btrfs_abort_transaction(trans, ret);
1343 int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1344 struct btrfs_block_group *block_group)
1346 struct btrfs_root *root = btrfs_free_space_root(block_group);
1347 struct btrfs_path *path;
1348 struct btrfs_key key, found_key;
1349 struct extent_buffer *leaf;
1354 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1357 if (block_group->needs_free_space) {
1358 /* We never added this block group to the free space tree. */
1362 path = btrfs_alloc_path();
1368 start = block_group->start;
1369 end = block_group->start + block_group->length;
1371 key.objectid = end - 1;
1373 key.offset = (u64)-1;
1376 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1380 leaf = path->nodes[0];
1383 while (path->slots[0] > 0) {
1384 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1386 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1387 ASSERT(found_key.objectid == block_group->start);
1388 ASSERT(found_key.offset == block_group->length);
1393 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1394 found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1395 ASSERT(found_key.objectid >= start);
1396 ASSERT(found_key.objectid < end);
1397 ASSERT(found_key.objectid + found_key.offset <= end);
1405 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1408 btrfs_release_path(path);
1413 btrfs_free_path(path);
1415 btrfs_abort_transaction(trans, ret);
1419 static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1420 struct btrfs_path *path,
1421 u32 expected_extent_count)
1423 struct btrfs_block_group *block_group;
1424 struct btrfs_fs_info *fs_info;
1425 struct btrfs_root *root;
1426 struct btrfs_key key;
1427 int prev_bit = 0, bit;
1428 /* Initialize to silence GCC. */
1429 u64 extent_start = 0;
1431 u64 total_found = 0;
1432 u32 extent_count = 0;
1435 block_group = caching_ctl->block_group;
1436 fs_info = block_group->fs_info;
1437 root = btrfs_free_space_root(block_group);
1439 end = block_group->start + block_group->length;
1442 ret = btrfs_next_item(root, path);
1448 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1450 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1453 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1454 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1456 caching_ctl->progress = key.objectid;
1458 offset = key.objectid;
1459 while (offset < key.objectid + key.offset) {
1460 bit = free_space_test_bit(block_group, path, offset);
1461 if (prev_bit == 0 && bit == 1) {
1462 extent_start = offset;
1463 } else if (prev_bit == 1 && bit == 0) {
1464 total_found += add_new_free_space(block_group,
1467 if (total_found > CACHING_CTL_WAKE_UP) {
1469 wake_up(&caching_ctl->wait);
1474 offset += fs_info->sectorsize;
1477 if (prev_bit == 1) {
1478 total_found += add_new_free_space(block_group, extent_start,
1483 if (extent_count != expected_extent_count) {
1485 "incorrect extent count for %llu; counted %u, expected %u",
1486 block_group->start, extent_count,
1487 expected_extent_count);
1493 caching_ctl->progress = (u64)-1;
1500 static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1501 struct btrfs_path *path,
1502 u32 expected_extent_count)
1504 struct btrfs_block_group *block_group;
1505 struct btrfs_fs_info *fs_info;
1506 struct btrfs_root *root;
1507 struct btrfs_key key;
1509 u64 total_found = 0;
1510 u32 extent_count = 0;
1513 block_group = caching_ctl->block_group;
1514 fs_info = block_group->fs_info;
1515 root = btrfs_free_space_root(block_group);
1517 end = block_group->start + block_group->length;
1520 ret = btrfs_next_item(root, path);
1526 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1528 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1531 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1532 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1534 caching_ctl->progress = key.objectid;
1536 total_found += add_new_free_space(block_group, key.objectid,
1537 key.objectid + key.offset);
1538 if (total_found > CACHING_CTL_WAKE_UP) {
1540 wake_up(&caching_ctl->wait);
1545 if (extent_count != expected_extent_count) {
1547 "incorrect extent count for %llu; counted %u, expected %u",
1548 block_group->start, extent_count,
1549 expected_extent_count);
1555 caching_ctl->progress = (u64)-1;
1562 int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1564 struct btrfs_block_group *block_group;
1565 struct btrfs_free_space_info *info;
1566 struct btrfs_path *path;
1567 u32 extent_count, flags;
1570 block_group = caching_ctl->block_group;
1572 path = btrfs_alloc_path();
1577 * Just like caching_thread() doesn't want to deadlock on the extent
1578 * tree, we don't want to deadlock on the free space tree.
1580 path->skip_locking = 1;
1581 path->search_commit_root = 1;
1582 path->reada = READA_FORWARD;
1584 info = search_free_space_info(NULL, block_group, path, 0);
1586 ret = PTR_ERR(info);
1589 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1590 flags = btrfs_free_space_flags(path->nodes[0], info);
1593 * We left path pointing to the free space info item, so now
1594 * load_free_space_foo can just iterate through the free space tree from
1597 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1598 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1600 ret = load_free_space_extents(caching_ctl, path, extent_count);
1603 btrfs_free_path(path);