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>
12 #include "free-space-tree.h"
13 #include "transaction.h"
14 #include "block-group.h"
17 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
18 struct btrfs_block_group *block_group,
19 struct btrfs_path *path);
21 static struct btrfs_root *btrfs_free_space_root(
22 struct btrfs_block_group *block_group)
24 struct btrfs_key key = {
25 .objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
26 .type = BTRFS_ROOT_ITEM_KEY,
30 if (btrfs_fs_incompat(block_group->fs_info, EXTENT_TREE_V2))
31 key.offset = block_group->global_root_id;
32 return btrfs_global_root(block_group->fs_info, &key);
35 void set_free_space_tree_thresholds(struct btrfs_block_group *cache)
39 u64 num_bitmaps, total_bitmap_size;
41 if (WARN_ON(cache->length == 0))
42 btrfs_warn(cache->fs_info, "block group %llu length is zero",
46 * We convert to bitmaps when the disk space required for using extents
47 * exceeds that required for using bitmaps.
49 bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
50 num_bitmaps = div_u64(cache->length + bitmap_range - 1, bitmap_range);
51 bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
52 total_bitmap_size = num_bitmaps * bitmap_size;
53 cache->bitmap_high_thresh = div_u64(total_bitmap_size,
54 sizeof(struct btrfs_item));
57 * We allow for a small buffer between the high threshold and low
58 * threshold to avoid thrashing back and forth between the two formats.
60 if (cache->bitmap_high_thresh > 100)
61 cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
63 cache->bitmap_low_thresh = 0;
66 static int add_new_free_space_info(struct btrfs_trans_handle *trans,
67 struct btrfs_block_group *block_group,
68 struct btrfs_path *path)
70 struct btrfs_root *root = btrfs_free_space_root(block_group);
71 struct btrfs_free_space_info *info;
73 struct extent_buffer *leaf;
76 key.objectid = block_group->start;
77 key.type = BTRFS_FREE_SPACE_INFO_KEY;
78 key.offset = block_group->length;
80 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
84 leaf = path->nodes[0];
85 info = btrfs_item_ptr(leaf, path->slots[0],
86 struct btrfs_free_space_info);
87 btrfs_set_free_space_extent_count(leaf, info, 0);
88 btrfs_set_free_space_flags(leaf, info, 0);
89 btrfs_mark_buffer_dirty(leaf);
93 btrfs_release_path(path);
98 struct btrfs_free_space_info *search_free_space_info(
99 struct btrfs_trans_handle *trans,
100 struct btrfs_block_group *block_group,
101 struct btrfs_path *path, int cow)
103 struct btrfs_fs_info *fs_info = block_group->fs_info;
104 struct btrfs_root *root = btrfs_free_space_root(block_group);
105 struct btrfs_key key;
108 key.objectid = block_group->start;
109 key.type = BTRFS_FREE_SPACE_INFO_KEY;
110 key.offset = block_group->length;
112 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
116 btrfs_warn(fs_info, "missing free space info for %llu",
119 return ERR_PTR(-ENOENT);
122 return btrfs_item_ptr(path->nodes[0], path->slots[0],
123 struct btrfs_free_space_info);
127 * btrfs_search_slot() but we're looking for the greatest key less than the
130 static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
131 struct btrfs_root *root,
132 struct btrfs_key *key, struct btrfs_path *p,
133 int ins_len, int cow)
137 ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
146 if (p->slots[0] == 0) {
155 static inline u32 free_space_bitmap_size(const struct btrfs_fs_info *fs_info,
158 return DIV_ROUND_UP(size >> fs_info->sectorsize_bits, BITS_PER_BYTE);
161 static unsigned long *alloc_bitmap(u32 bitmap_size)
164 unsigned int nofs_flag;
165 u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
168 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
169 * into the filesystem as the free space bitmap can be modified in the
170 * critical section of a transaction commit.
172 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
173 * know that recursion is unsafe.
175 nofs_flag = memalloc_nofs_save();
176 ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
177 memalloc_nofs_restore(nofs_flag);
181 static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
183 u8 *p = ((u8 *)map) + BIT_BYTE(start);
184 const unsigned int size = start + len;
185 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
186 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
188 while (len - bits_to_set >= 0) {
191 bits_to_set = BITS_PER_BYTE;
196 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
202 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
203 struct btrfs_block_group *block_group,
204 struct btrfs_path *path)
206 struct btrfs_fs_info *fs_info = trans->fs_info;
207 struct btrfs_root *root = btrfs_free_space_root(block_group);
208 struct btrfs_free_space_info *info;
209 struct btrfs_key key, found_key;
210 struct extent_buffer *leaf;
211 unsigned long *bitmap;
215 u32 bitmap_size, flags, expected_extent_count;
216 u32 extent_count = 0;
220 bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
221 bitmap = alloc_bitmap(bitmap_size);
227 start = block_group->start;
228 end = block_group->start + block_group->length;
230 key.objectid = end - 1;
232 key.offset = (u64)-1;
235 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
239 leaf = path->nodes[0];
242 while (path->slots[0] > 0) {
243 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
245 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
246 ASSERT(found_key.objectid == block_group->start);
247 ASSERT(found_key.offset == block_group->length);
250 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
253 ASSERT(found_key.objectid >= start);
254 ASSERT(found_key.objectid < end);
255 ASSERT(found_key.objectid + found_key.offset <= end);
257 first = div_u64(found_key.objectid - start,
258 fs_info->sectorsize);
259 last = div_u64(found_key.objectid + found_key.offset - start,
260 fs_info->sectorsize);
261 le_bitmap_set(bitmap, first, last - first);
271 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
274 btrfs_release_path(path);
277 info = search_free_space_info(trans, block_group, path, 1);
282 leaf = path->nodes[0];
283 flags = btrfs_free_space_flags(leaf, info);
284 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
285 btrfs_set_free_space_flags(leaf, info, flags);
286 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
287 btrfs_mark_buffer_dirty(leaf);
288 btrfs_release_path(path);
290 if (extent_count != expected_extent_count) {
292 "incorrect extent count for %llu; counted %u, expected %u",
293 block_group->start, extent_count,
294 expected_extent_count);
300 bitmap_cursor = (char *)bitmap;
301 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
308 extent_size = min(end - i, bitmap_range);
309 data_size = free_space_bitmap_size(fs_info, extent_size);
312 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
313 key.offset = extent_size;
315 ret = btrfs_insert_empty_item(trans, root, path, &key,
320 leaf = path->nodes[0];
321 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
322 write_extent_buffer(leaf, bitmap_cursor, ptr,
324 btrfs_mark_buffer_dirty(leaf);
325 btrfs_release_path(path);
328 bitmap_cursor += data_size;
335 btrfs_abort_transaction(trans, ret);
340 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
341 struct btrfs_block_group *block_group,
342 struct btrfs_path *path)
344 struct btrfs_fs_info *fs_info = trans->fs_info;
345 struct btrfs_root *root = btrfs_free_space_root(block_group);
346 struct btrfs_free_space_info *info;
347 struct btrfs_key key, found_key;
348 struct extent_buffer *leaf;
349 unsigned long *bitmap;
351 u32 bitmap_size, flags, expected_extent_count;
352 unsigned long nrbits, start_bit, end_bit;
353 u32 extent_count = 0;
357 bitmap_size = free_space_bitmap_size(fs_info, block_group->length);
358 bitmap = alloc_bitmap(bitmap_size);
364 start = block_group->start;
365 end = block_group->start + block_group->length;
367 key.objectid = end - 1;
369 key.offset = (u64)-1;
372 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
376 leaf = path->nodes[0];
379 while (path->slots[0] > 0) {
380 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
382 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
383 ASSERT(found_key.objectid == block_group->start);
384 ASSERT(found_key.offset == block_group->length);
387 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
390 u32 bitmap_pos, data_size;
392 ASSERT(found_key.objectid >= start);
393 ASSERT(found_key.objectid < end);
394 ASSERT(found_key.objectid + found_key.offset <= end);
396 bitmap_pos = div_u64(found_key.objectid - start,
397 fs_info->sectorsize *
399 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
400 data_size = free_space_bitmap_size(fs_info,
403 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
404 read_extent_buffer(leaf, bitmap_cursor, ptr,
414 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
417 btrfs_release_path(path);
420 info = search_free_space_info(trans, block_group, path, 1);
425 leaf = path->nodes[0];
426 flags = btrfs_free_space_flags(leaf, info);
427 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
428 btrfs_set_free_space_flags(leaf, info, flags);
429 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
430 btrfs_mark_buffer_dirty(leaf);
431 btrfs_release_path(path);
433 nrbits = block_group->length >> block_group->fs_info->sectorsize_bits;
434 start_bit = find_next_bit_le(bitmap, nrbits, 0);
436 while (start_bit < nrbits) {
437 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
438 ASSERT(start_bit < end_bit);
440 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
441 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
442 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
444 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
447 btrfs_release_path(path);
451 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
454 if (extent_count != expected_extent_count) {
456 "incorrect extent count for %llu; counted %u, expected %u",
457 block_group->start, extent_count,
458 expected_extent_count);
468 btrfs_abort_transaction(trans, ret);
472 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
473 struct btrfs_block_group *block_group,
474 struct btrfs_path *path,
477 struct btrfs_free_space_info *info;
482 if (new_extents == 0)
485 info = search_free_space_info(trans, block_group, path, 1);
490 flags = btrfs_free_space_flags(path->nodes[0], info);
491 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
493 extent_count += new_extents;
494 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
495 btrfs_mark_buffer_dirty(path->nodes[0]);
496 btrfs_release_path(path);
498 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
499 extent_count > block_group->bitmap_high_thresh) {
500 ret = convert_free_space_to_bitmaps(trans, block_group, path);
501 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
502 extent_count < block_group->bitmap_low_thresh) {
503 ret = convert_free_space_to_extents(trans, block_group, path);
511 int free_space_test_bit(struct btrfs_block_group *block_group,
512 struct btrfs_path *path, u64 offset)
514 struct extent_buffer *leaf;
515 struct btrfs_key key;
516 u64 found_start, found_end;
517 unsigned long ptr, i;
519 leaf = path->nodes[0];
520 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
521 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
523 found_start = key.objectid;
524 found_end = key.objectid + key.offset;
525 ASSERT(offset >= found_start && offset < found_end);
527 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
528 i = div_u64(offset - found_start,
529 block_group->fs_info->sectorsize);
530 return !!extent_buffer_test_bit(leaf, ptr, i);
533 static void free_space_set_bits(struct btrfs_block_group *block_group,
534 struct btrfs_path *path, u64 *start, u64 *size,
537 struct btrfs_fs_info *fs_info = block_group->fs_info;
538 struct extent_buffer *leaf;
539 struct btrfs_key key;
540 u64 end = *start + *size;
541 u64 found_start, found_end;
542 unsigned long ptr, first, last;
544 leaf = path->nodes[0];
545 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
546 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
548 found_start = key.objectid;
549 found_end = key.objectid + key.offset;
550 ASSERT(*start >= found_start && *start < found_end);
551 ASSERT(end > found_start);
556 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
557 first = (*start - found_start) >> fs_info->sectorsize_bits;
558 last = (end - found_start) >> fs_info->sectorsize_bits;
560 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
562 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
563 btrfs_mark_buffer_dirty(leaf);
565 *size -= end - *start;
570 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
571 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
572 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
575 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
576 struct btrfs_root *root, struct btrfs_path *p)
578 struct btrfs_key key;
580 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
585 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
586 btrfs_release_path(p);
588 key.objectid += key.offset;
590 key.offset = (u64)-1;
592 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
596 * If remove is 1, then we are removing free space, thus clearing bits in the
597 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
600 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
601 struct btrfs_block_group *block_group,
602 struct btrfs_path *path,
603 u64 start, u64 size, int remove)
605 struct btrfs_root *root = btrfs_free_space_root(block_group);
606 struct btrfs_key key;
607 u64 end = start + size;
608 u64 cur_start, cur_size;
609 int prev_bit, next_bit;
614 * Read the bit for the block immediately before the extent of space if
615 * that block is within the block group.
617 if (start > block_group->start) {
618 u64 prev_block = start - block_group->fs_info->sectorsize;
620 key.objectid = prev_block;
622 key.offset = (u64)-1;
624 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
628 prev_bit = free_space_test_bit(block_group, path, prev_block);
630 /* The previous block may have been in the previous bitmap. */
631 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
632 if (start >= key.objectid + key.offset) {
633 ret = free_space_next_bitmap(trans, root, path);
638 key.objectid = start;
640 key.offset = (u64)-1;
642 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
650 * Iterate over all of the bitmaps overlapped by the extent of space,
651 * clearing/setting bits as required.
656 free_space_set_bits(block_group, path, &cur_start, &cur_size,
660 ret = free_space_next_bitmap(trans, root, path);
666 * Read the bit for the block immediately after the extent of space if
667 * that block is within the block group.
669 if (end < block_group->start + block_group->length) {
670 /* The next block may be in the next bitmap. */
671 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
672 if (end >= key.objectid + key.offset) {
673 ret = free_space_next_bitmap(trans, root, path);
678 next_bit = free_space_test_bit(block_group, path, end);
686 /* Leftover on the left. */
690 /* Leftover on the right. */
696 /* Merging with neighbor on the left. */
700 /* Merging with neighbor on the right. */
705 btrfs_release_path(path);
706 ret = update_free_space_extent_count(trans, block_group, path,
713 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
714 struct btrfs_block_group *block_group,
715 struct btrfs_path *path,
718 struct btrfs_root *root = btrfs_free_space_root(block_group);
719 struct btrfs_key key;
720 u64 found_start, found_end;
721 u64 end = start + size;
722 int new_extents = -1;
725 key.objectid = start;
727 key.offset = (u64)-1;
729 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
733 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
735 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
737 found_start = key.objectid;
738 found_end = key.objectid + key.offset;
739 ASSERT(start >= found_start && end <= found_end);
742 * Okay, now that we've found the free space extent which contains the
743 * free space that we are removing, there are four cases:
745 * 1. We're using the whole extent: delete the key we found and
746 * decrement the free space extent count.
747 * 2. We are using part of the extent starting at the beginning: delete
748 * the key we found and insert a new key representing the leftover at
749 * the end. There is no net change in the number of extents.
750 * 3. We are using part of the extent ending at the end: delete the key
751 * we found and insert a new key representing the leftover at the
752 * beginning. There is no net change in the number of extents.
753 * 4. We are using part of the extent in the middle: delete the key we
754 * found and insert two new keys representing the leftovers on each
755 * side. Where we used to have one extent, we now have two, so increment
756 * the extent count. We may need to convert the block group to bitmaps
760 /* Delete the existing key (cases 1-4). */
761 ret = btrfs_del_item(trans, root, path);
765 /* Add a key for leftovers at the beginning (cases 3 and 4). */
766 if (start > found_start) {
767 key.objectid = found_start;
768 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
769 key.offset = start - found_start;
771 btrfs_release_path(path);
772 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
778 /* Add a key for leftovers at the end (cases 2 and 4). */
779 if (end < found_end) {
781 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
782 key.offset = found_end - end;
784 btrfs_release_path(path);
785 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
791 btrfs_release_path(path);
792 ret = update_free_space_extent_count(trans, block_group, path,
800 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
801 struct btrfs_block_group *block_group,
802 struct btrfs_path *path, u64 start, u64 size)
804 struct btrfs_free_space_info *info;
808 if (block_group->needs_free_space) {
809 ret = __add_block_group_free_space(trans, block_group, path);
814 info = search_free_space_info(NULL, block_group, path, 0);
816 return PTR_ERR(info);
817 flags = btrfs_free_space_flags(path->nodes[0], info);
818 btrfs_release_path(path);
820 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
821 return modify_free_space_bitmap(trans, block_group, path,
824 return remove_free_space_extent(trans, block_group, path,
829 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
832 struct btrfs_block_group *block_group;
833 struct btrfs_path *path;
836 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
839 path = btrfs_alloc_path();
845 block_group = btrfs_lookup_block_group(trans->fs_info, start);
852 mutex_lock(&block_group->free_space_lock);
853 ret = __remove_from_free_space_tree(trans, block_group, path, start,
855 mutex_unlock(&block_group->free_space_lock);
857 btrfs_put_block_group(block_group);
859 btrfs_free_path(path);
861 btrfs_abort_transaction(trans, ret);
865 static int add_free_space_extent(struct btrfs_trans_handle *trans,
866 struct btrfs_block_group *block_group,
867 struct btrfs_path *path,
870 struct btrfs_root *root = btrfs_free_space_root(block_group);
871 struct btrfs_key key, new_key;
872 u64 found_start, found_end;
873 u64 end = start + size;
878 * We are adding a new extent of free space, but we need to merge
879 * extents. There are four cases here:
881 * 1. The new extent does not have any immediate neighbors to merge
882 * with: add the new key and increment the free space extent count. We
883 * may need to convert the block group to bitmaps as a result.
884 * 2. The new extent has an immediate neighbor before it: remove the
885 * previous key and insert a new key combining both of them. There is no
886 * net change in the number of extents.
887 * 3. The new extent has an immediate neighbor after it: remove the next
888 * key and insert a new key combining both of them. There is no net
889 * change in the number of extents.
890 * 4. The new extent has immediate neighbors on both sides: remove both
891 * of the keys and insert a new key combining all of them. Where we used
892 * to have two extents, we now have one, so decrement the extent count.
895 new_key.objectid = start;
896 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
897 new_key.offset = size;
899 /* Search for a neighbor on the left. */
900 if (start == block_group->start)
902 key.objectid = start - 1;
904 key.offset = (u64)-1;
906 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
910 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
912 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
913 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
914 btrfs_release_path(path);
918 found_start = key.objectid;
919 found_end = key.objectid + key.offset;
920 ASSERT(found_start >= block_group->start &&
921 found_end > block_group->start);
922 ASSERT(found_start < start && found_end <= start);
925 * Delete the neighbor on the left and absorb it into the new key (cases
928 if (found_end == start) {
929 ret = btrfs_del_item(trans, root, path);
932 new_key.objectid = found_start;
933 new_key.offset += key.offset;
936 btrfs_release_path(path);
939 /* Search for a neighbor on the right. */
940 if (end == block_group->start + block_group->length)
944 key.offset = (u64)-1;
946 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
950 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
952 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
953 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
954 btrfs_release_path(path);
958 found_start = key.objectid;
959 found_end = key.objectid + key.offset;
960 ASSERT(found_start >= block_group->start &&
961 found_end > block_group->start);
962 ASSERT((found_start < start && found_end <= start) ||
963 (found_start >= end && found_end > end));
966 * Delete the neighbor on the right and absorb it into the new key
969 if (found_start == end) {
970 ret = btrfs_del_item(trans, root, path);
973 new_key.offset += key.offset;
976 btrfs_release_path(path);
979 /* Insert the new key (cases 1-4). */
980 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
984 btrfs_release_path(path);
985 ret = update_free_space_extent_count(trans, block_group, path,
993 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
994 struct btrfs_block_group *block_group,
995 struct btrfs_path *path, u64 start, u64 size)
997 struct btrfs_free_space_info *info;
1001 if (block_group->needs_free_space) {
1002 ret = __add_block_group_free_space(trans, block_group, path);
1007 info = search_free_space_info(NULL, block_group, path, 0);
1009 return PTR_ERR(info);
1010 flags = btrfs_free_space_flags(path->nodes[0], info);
1011 btrfs_release_path(path);
1013 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
1014 return modify_free_space_bitmap(trans, block_group, path,
1017 return add_free_space_extent(trans, block_group, path, start,
1022 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1023 u64 start, u64 size)
1025 struct btrfs_block_group *block_group;
1026 struct btrfs_path *path;
1029 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1032 path = btrfs_alloc_path();
1038 block_group = btrfs_lookup_block_group(trans->fs_info, start);
1045 mutex_lock(&block_group->free_space_lock);
1046 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1047 mutex_unlock(&block_group->free_space_lock);
1049 btrfs_put_block_group(block_group);
1051 btrfs_free_path(path);
1053 btrfs_abort_transaction(trans, ret);
1058 * Populate the free space tree by walking the extent tree. Operations on the
1059 * extent tree that happen as a result of writes to the free space tree will go
1060 * through the normal add/remove hooks.
1062 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1063 struct btrfs_block_group *block_group)
1065 struct btrfs_root *extent_root;
1066 struct btrfs_path *path, *path2;
1067 struct btrfs_key key;
1071 path = btrfs_alloc_path();
1074 path->reada = READA_FORWARD;
1076 path2 = btrfs_alloc_path();
1078 btrfs_free_path(path);
1082 ret = add_new_free_space_info(trans, block_group, path2);
1086 mutex_lock(&block_group->free_space_lock);
1089 * Iterate through all of the extent and metadata items in this block
1090 * group, adding the free space between them and the free space at the
1091 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1092 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1095 key.objectid = block_group->start;
1096 key.type = BTRFS_EXTENT_ITEM_KEY;
1099 extent_root = btrfs_extent_root(trans->fs_info, key.objectid);
1100 ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1105 start = block_group->start;
1106 end = block_group->start + block_group->length;
1108 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1110 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1111 key.type == BTRFS_METADATA_ITEM_KEY) {
1112 if (key.objectid >= end)
1115 if (start < key.objectid) {
1116 ret = __add_to_free_space_tree(trans,
1124 start = key.objectid;
1125 if (key.type == BTRFS_METADATA_ITEM_KEY)
1126 start += trans->fs_info->nodesize;
1128 start += key.offset;
1129 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1130 if (key.objectid != block_group->start)
1134 ret = btrfs_next_item(extent_root, path);
1141 ret = __add_to_free_space_tree(trans, block_group, path2,
1142 start, end - start);
1149 mutex_unlock(&block_group->free_space_lock);
1151 btrfs_free_path(path2);
1152 btrfs_free_path(path);
1156 int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1158 struct btrfs_trans_handle *trans;
1159 struct btrfs_root *tree_root = fs_info->tree_root;
1160 struct btrfs_root *free_space_root;
1161 struct btrfs_block_group *block_group;
1162 struct rb_node *node;
1165 trans = btrfs_start_transaction(tree_root, 0);
1167 return PTR_ERR(trans);
1169 set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1170 set_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1171 free_space_root = btrfs_create_tree(trans,
1172 BTRFS_FREE_SPACE_TREE_OBJECTID);
1173 if (IS_ERR(free_space_root)) {
1174 ret = PTR_ERR(free_space_root);
1177 ret = btrfs_global_root_insert(free_space_root);
1179 btrfs_put_root(free_space_root);
1183 node = rb_first_cached(&fs_info->block_group_cache_tree);
1185 block_group = rb_entry(node, struct btrfs_block_group,
1187 ret = populate_free_space_tree(trans, block_group);
1190 node = rb_next(node);
1193 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1194 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1195 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1196 ret = btrfs_commit_transaction(trans);
1199 * Now that we've committed the transaction any reading of our commit
1200 * root will be safe, so we can cache from the free space tree now.
1202 clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1206 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1207 clear_bit(BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED, &fs_info->flags);
1208 btrfs_abort_transaction(trans, ret);
1209 btrfs_end_transaction(trans);
1213 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1214 struct btrfs_root *root)
1216 struct btrfs_path *path;
1217 struct btrfs_key key;
1221 path = btrfs_alloc_path();
1230 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1234 nr = btrfs_header_nritems(path->nodes[0]);
1239 ret = btrfs_del_items(trans, root, path, 0, nr);
1243 btrfs_release_path(path);
1248 btrfs_free_path(path);
1252 int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1254 struct btrfs_trans_handle *trans;
1255 struct btrfs_root *tree_root = fs_info->tree_root;
1256 struct btrfs_key key = {
1257 .objectid = BTRFS_FREE_SPACE_TREE_OBJECTID,
1258 .type = BTRFS_ROOT_ITEM_KEY,
1261 struct btrfs_root *free_space_root = btrfs_global_root(fs_info, &key);
1264 trans = btrfs_start_transaction(tree_root, 0);
1266 return PTR_ERR(trans);
1268 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1269 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1271 ret = clear_free_space_tree(trans, free_space_root);
1275 ret = btrfs_del_root(trans, &free_space_root->root_key);
1279 btrfs_global_root_delete(free_space_root);
1280 list_del(&free_space_root->dirty_list);
1282 btrfs_tree_lock(free_space_root->node);
1283 btrfs_clean_tree_block(free_space_root->node);
1284 btrfs_tree_unlock(free_space_root->node);
1285 btrfs_free_tree_block(trans, btrfs_root_id(free_space_root),
1286 free_space_root->node, 0, 1);
1288 btrfs_put_root(free_space_root);
1290 return btrfs_commit_transaction(trans);
1293 btrfs_abort_transaction(trans, ret);
1294 btrfs_end_transaction(trans);
1298 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1299 struct btrfs_block_group *block_group,
1300 struct btrfs_path *path)
1304 block_group->needs_free_space = 0;
1306 ret = add_new_free_space_info(trans, block_group, path);
1310 return __add_to_free_space_tree(trans, block_group, path,
1312 block_group->length);
1315 int add_block_group_free_space(struct btrfs_trans_handle *trans,
1316 struct btrfs_block_group *block_group)
1318 struct btrfs_fs_info *fs_info = trans->fs_info;
1319 struct btrfs_path *path = NULL;
1322 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1325 mutex_lock(&block_group->free_space_lock);
1326 if (!block_group->needs_free_space)
1329 path = btrfs_alloc_path();
1335 ret = __add_block_group_free_space(trans, block_group, path);
1338 btrfs_free_path(path);
1339 mutex_unlock(&block_group->free_space_lock);
1341 btrfs_abort_transaction(trans, ret);
1345 int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1346 struct btrfs_block_group *block_group)
1348 struct btrfs_root *root = btrfs_free_space_root(block_group);
1349 struct btrfs_path *path;
1350 struct btrfs_key key, found_key;
1351 struct extent_buffer *leaf;
1356 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1359 if (block_group->needs_free_space) {
1360 /* We never added this block group to the free space tree. */
1364 path = btrfs_alloc_path();
1370 start = block_group->start;
1371 end = block_group->start + block_group->length;
1373 key.objectid = end - 1;
1375 key.offset = (u64)-1;
1378 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1382 leaf = path->nodes[0];
1385 while (path->slots[0] > 0) {
1386 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1388 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1389 ASSERT(found_key.objectid == block_group->start);
1390 ASSERT(found_key.offset == block_group->length);
1395 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1396 found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1397 ASSERT(found_key.objectid >= start);
1398 ASSERT(found_key.objectid < end);
1399 ASSERT(found_key.objectid + found_key.offset <= end);
1407 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1410 btrfs_release_path(path);
1415 btrfs_free_path(path);
1417 btrfs_abort_transaction(trans, ret);
1421 static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1422 struct btrfs_path *path,
1423 u32 expected_extent_count)
1425 struct btrfs_block_group *block_group;
1426 struct btrfs_fs_info *fs_info;
1427 struct btrfs_root *root;
1428 struct btrfs_key key;
1429 int prev_bit = 0, bit;
1430 /* Initialize to silence GCC. */
1431 u64 extent_start = 0;
1433 u64 total_found = 0;
1434 u32 extent_count = 0;
1437 block_group = caching_ctl->block_group;
1438 fs_info = block_group->fs_info;
1439 root = btrfs_free_space_root(block_group);
1441 end = block_group->start + block_group->length;
1444 ret = btrfs_next_item(root, path);
1450 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1452 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1455 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1456 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
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);
1498 static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1499 struct btrfs_path *path,
1500 u32 expected_extent_count)
1502 struct btrfs_block_group *block_group;
1503 struct btrfs_fs_info *fs_info;
1504 struct btrfs_root *root;
1505 struct btrfs_key key;
1507 u64 total_found = 0;
1508 u32 extent_count = 0;
1511 block_group = caching_ctl->block_group;
1512 fs_info = block_group->fs_info;
1513 root = btrfs_free_space_root(block_group);
1515 end = block_group->start + block_group->length;
1518 ret = btrfs_next_item(root, path);
1524 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1526 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1529 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1530 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1532 total_found += add_new_free_space(block_group, key.objectid,
1533 key.objectid + key.offset);
1534 if (total_found > CACHING_CTL_WAKE_UP) {
1536 wake_up(&caching_ctl->wait);
1541 if (extent_count != expected_extent_count) {
1543 "incorrect extent count for %llu; counted %u, expected %u",
1544 block_group->start, extent_count,
1545 expected_extent_count);
1556 int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1558 struct btrfs_block_group *block_group;
1559 struct btrfs_free_space_info *info;
1560 struct btrfs_path *path;
1561 u32 extent_count, flags;
1564 block_group = caching_ctl->block_group;
1566 path = btrfs_alloc_path();
1571 * Just like caching_thread() doesn't want to deadlock on the extent
1572 * tree, we don't want to deadlock on the free space tree.
1574 path->skip_locking = 1;
1575 path->search_commit_root = 1;
1576 path->reada = READA_FORWARD;
1578 info = search_free_space_info(NULL, block_group, path, 0);
1580 ret = PTR_ERR(info);
1583 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1584 flags = btrfs_free_space_flags(path->nodes[0], info);
1587 * We left path pointing to the free space info item, so now
1588 * load_free_space_foo can just iterate through the free space tree from
1591 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1592 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1594 ret = load_free_space_extents(caching_ctl, path, extent_count);
1597 btrfs_free_path(path);