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_cache *block_group,
17 struct btrfs_path *path);
19 void set_free_space_tree_thresholds(struct btrfs_block_group_cache *cache)
23 u64 num_bitmaps, total_bitmap_size;
26 * We convert to bitmaps when the disk space required for using extents
27 * exceeds that required for using bitmaps.
29 bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
30 num_bitmaps = div_u64(cache->key.offset + bitmap_range - 1,
32 bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
33 total_bitmap_size = num_bitmaps * bitmap_size;
34 cache->bitmap_high_thresh = div_u64(total_bitmap_size,
35 sizeof(struct btrfs_item));
38 * We allow for a small buffer between the high threshold and low
39 * threshold to avoid thrashing back and forth between the two formats.
41 if (cache->bitmap_high_thresh > 100)
42 cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
44 cache->bitmap_low_thresh = 0;
47 static int add_new_free_space_info(struct btrfs_trans_handle *trans,
48 struct btrfs_block_group_cache *block_group,
49 struct btrfs_path *path)
51 struct btrfs_root *root = trans->fs_info->free_space_root;
52 struct btrfs_free_space_info *info;
54 struct extent_buffer *leaf;
57 key.objectid = block_group->key.objectid;
58 key.type = BTRFS_FREE_SPACE_INFO_KEY;
59 key.offset = block_group->key.offset;
61 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
65 leaf = path->nodes[0];
66 info = btrfs_item_ptr(leaf, path->slots[0],
67 struct btrfs_free_space_info);
68 btrfs_set_free_space_extent_count(leaf, info, 0);
69 btrfs_set_free_space_flags(leaf, info, 0);
70 btrfs_mark_buffer_dirty(leaf);
74 btrfs_release_path(path);
79 struct btrfs_free_space_info *search_free_space_info(
80 struct btrfs_trans_handle *trans,
81 struct btrfs_block_group_cache *block_group,
82 struct btrfs_path *path, int cow)
84 struct btrfs_fs_info *fs_info = block_group->fs_info;
85 struct btrfs_root *root = fs_info->free_space_root;
89 key.objectid = block_group->key.objectid;
90 key.type = BTRFS_FREE_SPACE_INFO_KEY;
91 key.offset = block_group->key.offset;
93 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
97 btrfs_warn(fs_info, "missing free space info for %llu",
98 block_group->key.objectid);
100 return ERR_PTR(-ENOENT);
103 return btrfs_item_ptr(path->nodes[0], path->slots[0],
104 struct btrfs_free_space_info);
108 * btrfs_search_slot() but we're looking for the greatest key less than the
111 static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
112 struct btrfs_root *root,
113 struct btrfs_key *key, struct btrfs_path *p,
114 int ins_len, int cow)
118 ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
127 if (p->slots[0] == 0) {
136 static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
138 return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
141 static unsigned long *alloc_bitmap(u32 bitmap_size)
144 unsigned int nofs_flag;
145 u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
148 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
149 * into the filesystem as the free space bitmap can be modified in the
150 * critical section of a transaction commit.
152 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
153 * know that recursion is unsafe.
155 nofs_flag = memalloc_nofs_save();
156 ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
157 memalloc_nofs_restore(nofs_flag);
161 static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
163 u8 *p = ((u8 *)map) + BIT_BYTE(start);
164 const unsigned int size = start + len;
165 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
166 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
168 while (len - bits_to_set >= 0) {
171 bits_to_set = BITS_PER_BYTE;
176 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
182 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
183 struct btrfs_block_group_cache *block_group,
184 struct btrfs_path *path)
186 struct btrfs_fs_info *fs_info = trans->fs_info;
187 struct btrfs_root *root = fs_info->free_space_root;
188 struct btrfs_free_space_info *info;
189 struct btrfs_key key, found_key;
190 struct extent_buffer *leaf;
191 unsigned long *bitmap;
195 u32 bitmap_size, flags, expected_extent_count;
196 u32 extent_count = 0;
200 bitmap_size = free_space_bitmap_size(block_group->key.offset,
201 fs_info->sectorsize);
202 bitmap = alloc_bitmap(bitmap_size);
208 start = block_group->key.objectid;
209 end = block_group->key.objectid + block_group->key.offset;
211 key.objectid = end - 1;
213 key.offset = (u64)-1;
216 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
220 leaf = path->nodes[0];
223 while (path->slots[0] > 0) {
224 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
226 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
227 ASSERT(found_key.objectid == block_group->key.objectid);
228 ASSERT(found_key.offset == block_group->key.offset);
231 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
234 ASSERT(found_key.objectid >= start);
235 ASSERT(found_key.objectid < end);
236 ASSERT(found_key.objectid + found_key.offset <= end);
238 first = div_u64(found_key.objectid - start,
239 fs_info->sectorsize);
240 last = div_u64(found_key.objectid + found_key.offset - start,
241 fs_info->sectorsize);
242 le_bitmap_set(bitmap, first, last - first);
252 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
255 btrfs_release_path(path);
258 info = search_free_space_info(trans, block_group, path, 1);
263 leaf = path->nodes[0];
264 flags = btrfs_free_space_flags(leaf, info);
265 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
266 btrfs_set_free_space_flags(leaf, info, flags);
267 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
268 btrfs_mark_buffer_dirty(leaf);
269 btrfs_release_path(path);
271 if (extent_count != expected_extent_count) {
273 "incorrect extent count for %llu; counted %u, expected %u",
274 block_group->key.objectid, extent_count,
275 expected_extent_count);
281 bitmap_cursor = (char *)bitmap;
282 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
289 extent_size = min(end - i, bitmap_range);
290 data_size = free_space_bitmap_size(extent_size,
291 fs_info->sectorsize);
294 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
295 key.offset = extent_size;
297 ret = btrfs_insert_empty_item(trans, root, path, &key,
302 leaf = path->nodes[0];
303 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
304 write_extent_buffer(leaf, bitmap_cursor, ptr,
306 btrfs_mark_buffer_dirty(leaf);
307 btrfs_release_path(path);
310 bitmap_cursor += data_size;
317 btrfs_abort_transaction(trans, ret);
322 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
323 struct btrfs_block_group_cache *block_group,
324 struct btrfs_path *path)
326 struct btrfs_fs_info *fs_info = trans->fs_info;
327 struct btrfs_root *root = fs_info->free_space_root;
328 struct btrfs_free_space_info *info;
329 struct btrfs_key key, found_key;
330 struct extent_buffer *leaf;
331 unsigned long *bitmap;
333 u32 bitmap_size, flags, expected_extent_count;
334 unsigned long nrbits, start_bit, end_bit;
335 u32 extent_count = 0;
339 bitmap_size = free_space_bitmap_size(block_group->key.offset,
340 fs_info->sectorsize);
341 bitmap = alloc_bitmap(bitmap_size);
347 start = block_group->key.objectid;
348 end = block_group->key.objectid + block_group->key.offset;
350 key.objectid = end - 1;
352 key.offset = (u64)-1;
355 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
359 leaf = path->nodes[0];
362 while (path->slots[0] > 0) {
363 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
365 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
366 ASSERT(found_key.objectid == block_group->key.objectid);
367 ASSERT(found_key.offset == block_group->key.offset);
370 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
373 u32 bitmap_pos, data_size;
375 ASSERT(found_key.objectid >= start);
376 ASSERT(found_key.objectid < end);
377 ASSERT(found_key.objectid + found_key.offset <= end);
379 bitmap_pos = div_u64(found_key.objectid - start,
380 fs_info->sectorsize *
382 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
383 data_size = free_space_bitmap_size(found_key.offset,
384 fs_info->sectorsize);
386 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
387 read_extent_buffer(leaf, bitmap_cursor, ptr,
397 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
400 btrfs_release_path(path);
403 info = search_free_space_info(trans, block_group, path, 1);
408 leaf = path->nodes[0];
409 flags = btrfs_free_space_flags(leaf, info);
410 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
411 btrfs_set_free_space_flags(leaf, info, flags);
412 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
413 btrfs_mark_buffer_dirty(leaf);
414 btrfs_release_path(path);
416 nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
417 start_bit = find_next_bit_le(bitmap, nrbits, 0);
419 while (start_bit < nrbits) {
420 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
421 ASSERT(start_bit < end_bit);
423 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
424 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
425 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
427 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
430 btrfs_release_path(path);
434 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
437 if (extent_count != expected_extent_count) {
439 "incorrect extent count for %llu; counted %u, expected %u",
440 block_group->key.objectid, extent_count,
441 expected_extent_count);
451 btrfs_abort_transaction(trans, ret);
455 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
456 struct btrfs_block_group_cache *block_group,
457 struct btrfs_path *path,
460 struct btrfs_free_space_info *info;
465 if (new_extents == 0)
468 info = search_free_space_info(trans, block_group, path, 1);
473 flags = btrfs_free_space_flags(path->nodes[0], info);
474 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
476 extent_count += new_extents;
477 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
478 btrfs_mark_buffer_dirty(path->nodes[0]);
479 btrfs_release_path(path);
481 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
482 extent_count > block_group->bitmap_high_thresh) {
483 ret = convert_free_space_to_bitmaps(trans, block_group, path);
484 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
485 extent_count < block_group->bitmap_low_thresh) {
486 ret = convert_free_space_to_extents(trans, block_group, path);
494 int free_space_test_bit(struct btrfs_block_group_cache *block_group,
495 struct btrfs_path *path, u64 offset)
497 struct extent_buffer *leaf;
498 struct btrfs_key key;
499 u64 found_start, found_end;
500 unsigned long ptr, i;
502 leaf = path->nodes[0];
503 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
504 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
506 found_start = key.objectid;
507 found_end = key.objectid + key.offset;
508 ASSERT(offset >= found_start && offset < found_end);
510 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
511 i = div_u64(offset - found_start,
512 block_group->fs_info->sectorsize);
513 return !!extent_buffer_test_bit(leaf, ptr, i);
516 static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
517 struct btrfs_path *path, u64 *start, u64 *size,
520 struct btrfs_fs_info *fs_info = block_group->fs_info;
521 struct extent_buffer *leaf;
522 struct btrfs_key key;
523 u64 end = *start + *size;
524 u64 found_start, found_end;
525 unsigned long ptr, first, last;
527 leaf = path->nodes[0];
528 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
529 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
531 found_start = key.objectid;
532 found_end = key.objectid + key.offset;
533 ASSERT(*start >= found_start && *start < found_end);
534 ASSERT(end > found_start);
539 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
540 first = div_u64(*start - found_start, fs_info->sectorsize);
541 last = div_u64(end - found_start, fs_info->sectorsize);
543 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
545 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
546 btrfs_mark_buffer_dirty(leaf);
548 *size -= end - *start;
553 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
554 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
555 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
558 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
559 struct btrfs_root *root, struct btrfs_path *p)
561 struct btrfs_key key;
563 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
568 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
569 btrfs_release_path(p);
571 key.objectid += key.offset;
573 key.offset = (u64)-1;
575 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
579 * If remove is 1, then we are removing free space, thus clearing bits in the
580 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
583 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
584 struct btrfs_block_group_cache *block_group,
585 struct btrfs_path *path,
586 u64 start, u64 size, int remove)
588 struct btrfs_root *root = block_group->fs_info->free_space_root;
589 struct btrfs_key key;
590 u64 end = start + size;
591 u64 cur_start, cur_size;
592 int prev_bit, next_bit;
597 * Read the bit for the block immediately before the extent of space if
598 * that block is within the block group.
600 if (start > block_group->key.objectid) {
601 u64 prev_block = start - block_group->fs_info->sectorsize;
603 key.objectid = prev_block;
605 key.offset = (u64)-1;
607 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
611 prev_bit = free_space_test_bit(block_group, path, prev_block);
613 /* The previous block may have been in the previous bitmap. */
614 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
615 if (start >= key.objectid + key.offset) {
616 ret = free_space_next_bitmap(trans, root, path);
621 key.objectid = start;
623 key.offset = (u64)-1;
625 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
633 * Iterate over all of the bitmaps overlapped by the extent of space,
634 * clearing/setting bits as required.
639 free_space_set_bits(block_group, path, &cur_start, &cur_size,
643 ret = free_space_next_bitmap(trans, root, path);
649 * Read the bit for the block immediately after the extent of space if
650 * that block is within the block group.
652 if (end < block_group->key.objectid + block_group->key.offset) {
653 /* The next block may be in the next bitmap. */
654 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
655 if (end >= key.objectid + key.offset) {
656 ret = free_space_next_bitmap(trans, root, path);
661 next_bit = free_space_test_bit(block_group, path, end);
669 /* Leftover on the left. */
673 /* Leftover on the right. */
679 /* Merging with neighbor on the left. */
683 /* Merging with neighbor on the right. */
688 btrfs_release_path(path);
689 ret = update_free_space_extent_count(trans, block_group, path,
696 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
697 struct btrfs_block_group_cache *block_group,
698 struct btrfs_path *path,
701 struct btrfs_root *root = trans->fs_info->free_space_root;
702 struct btrfs_key key;
703 u64 found_start, found_end;
704 u64 end = start + size;
705 int new_extents = -1;
708 key.objectid = start;
710 key.offset = (u64)-1;
712 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
716 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
718 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
720 found_start = key.objectid;
721 found_end = key.objectid + key.offset;
722 ASSERT(start >= found_start && end <= found_end);
725 * Okay, now that we've found the free space extent which contains the
726 * free space that we are removing, there are four cases:
728 * 1. We're using the whole extent: delete the key we found and
729 * decrement the free space extent count.
730 * 2. We are using part of the extent starting at the beginning: delete
731 * the key we found and insert a new key representing the leftover at
732 * the end. There is no net change in the number of extents.
733 * 3. We are using part of the extent ending at the end: delete the key
734 * we found and insert a new key representing the leftover at the
735 * beginning. There is no net change in the number of extents.
736 * 4. We are using part of the extent in the middle: delete the key we
737 * found and insert two new keys representing the leftovers on each
738 * side. Where we used to have one extent, we now have two, so increment
739 * the extent count. We may need to convert the block group to bitmaps
743 /* Delete the existing key (cases 1-4). */
744 ret = btrfs_del_item(trans, root, path);
748 /* Add a key for leftovers at the beginning (cases 3 and 4). */
749 if (start > found_start) {
750 key.objectid = found_start;
751 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
752 key.offset = start - found_start;
754 btrfs_release_path(path);
755 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
761 /* Add a key for leftovers at the end (cases 2 and 4). */
762 if (end < found_end) {
764 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
765 key.offset = found_end - end;
767 btrfs_release_path(path);
768 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
774 btrfs_release_path(path);
775 ret = update_free_space_extent_count(trans, block_group, path,
783 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
784 struct btrfs_block_group_cache *block_group,
785 struct btrfs_path *path, u64 start, u64 size)
787 struct btrfs_free_space_info *info;
791 if (block_group->needs_free_space) {
792 ret = __add_block_group_free_space(trans, block_group, path);
797 info = search_free_space_info(NULL, block_group, path, 0);
799 return PTR_ERR(info);
800 flags = btrfs_free_space_flags(path->nodes[0], info);
801 btrfs_release_path(path);
803 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
804 return modify_free_space_bitmap(trans, block_group, path,
807 return remove_free_space_extent(trans, block_group, path,
812 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
815 struct btrfs_block_group_cache *block_group;
816 struct btrfs_path *path;
819 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
822 path = btrfs_alloc_path();
828 block_group = btrfs_lookup_block_group(trans->fs_info, start);
835 mutex_lock(&block_group->free_space_lock);
836 ret = __remove_from_free_space_tree(trans, block_group, path, start,
838 mutex_unlock(&block_group->free_space_lock);
840 btrfs_put_block_group(block_group);
842 btrfs_free_path(path);
844 btrfs_abort_transaction(trans, ret);
848 static int add_free_space_extent(struct btrfs_trans_handle *trans,
849 struct btrfs_block_group_cache *block_group,
850 struct btrfs_path *path,
853 struct btrfs_root *root = trans->fs_info->free_space_root;
854 struct btrfs_key key, new_key;
855 u64 found_start, found_end;
856 u64 end = start + size;
861 * We are adding a new extent of free space, but we need to merge
862 * extents. There are four cases here:
864 * 1. The new extent does not have any immediate neighbors to merge
865 * with: add the new key and increment the free space extent count. We
866 * may need to convert the block group to bitmaps as a result.
867 * 2. The new extent has an immediate neighbor before it: remove the
868 * previous key and insert a new key combining both of them. There is no
869 * net change in the number of extents.
870 * 3. The new extent has an immediate neighbor after it: remove the next
871 * key and insert a new key combining both of them. There is no net
872 * change in the number of extents.
873 * 4. The new extent has immediate neighbors on both sides: remove both
874 * of the keys and insert a new key combining all of them. Where we used
875 * to have two extents, we now have one, so decrement the extent count.
878 new_key.objectid = start;
879 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
880 new_key.offset = size;
882 /* Search for a neighbor on the left. */
883 if (start == block_group->key.objectid)
885 key.objectid = start - 1;
887 key.offset = (u64)-1;
889 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
893 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
895 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
896 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
897 btrfs_release_path(path);
901 found_start = key.objectid;
902 found_end = key.objectid + key.offset;
903 ASSERT(found_start >= block_group->key.objectid &&
904 found_end > block_group->key.objectid);
905 ASSERT(found_start < start && found_end <= start);
908 * Delete the neighbor on the left and absorb it into the new key (cases
911 if (found_end == start) {
912 ret = btrfs_del_item(trans, root, path);
915 new_key.objectid = found_start;
916 new_key.offset += key.offset;
919 btrfs_release_path(path);
922 /* Search for a neighbor on the right. */
923 if (end == block_group->key.objectid + block_group->key.offset)
927 key.offset = (u64)-1;
929 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
933 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
935 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
936 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
937 btrfs_release_path(path);
941 found_start = key.objectid;
942 found_end = key.objectid + key.offset;
943 ASSERT(found_start >= block_group->key.objectid &&
944 found_end > block_group->key.objectid);
945 ASSERT((found_start < start && found_end <= start) ||
946 (found_start >= end && found_end > end));
949 * Delete the neighbor on the right and absorb it into the new key
952 if (found_start == end) {
953 ret = btrfs_del_item(trans, root, path);
956 new_key.offset += key.offset;
959 btrfs_release_path(path);
962 /* Insert the new key (cases 1-4). */
963 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
967 btrfs_release_path(path);
968 ret = update_free_space_extent_count(trans, block_group, path,
976 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
977 struct btrfs_block_group_cache *block_group,
978 struct btrfs_path *path, u64 start, u64 size)
980 struct btrfs_free_space_info *info;
984 if (block_group->needs_free_space) {
985 ret = __add_block_group_free_space(trans, block_group, path);
990 info = search_free_space_info(NULL, block_group, path, 0);
992 return PTR_ERR(info);
993 flags = btrfs_free_space_flags(path->nodes[0], info);
994 btrfs_release_path(path);
996 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
997 return modify_free_space_bitmap(trans, block_group, path,
1000 return add_free_space_extent(trans, block_group, path, start,
1005 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1006 u64 start, u64 size)
1008 struct btrfs_block_group_cache *block_group;
1009 struct btrfs_path *path;
1012 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
1015 path = btrfs_alloc_path();
1021 block_group = btrfs_lookup_block_group(trans->fs_info, start);
1028 mutex_lock(&block_group->free_space_lock);
1029 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1030 mutex_unlock(&block_group->free_space_lock);
1032 btrfs_put_block_group(block_group);
1034 btrfs_free_path(path);
1036 btrfs_abort_transaction(trans, ret);
1041 * Populate the free space tree by walking the extent tree. Operations on the
1042 * extent tree that happen as a result of writes to the free space tree will go
1043 * through the normal add/remove hooks.
1045 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1046 struct btrfs_block_group_cache *block_group)
1048 struct btrfs_root *extent_root = trans->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 += trans->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,
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, 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, &free_space_root->root_key);
1247 list_del(&free_space_root->dirty_list);
1249 btrfs_tree_lock(free_space_root->node);
1250 btrfs_clean_tree_block(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_free_space_info *info;
1537 struct btrfs_path *path;
1538 u32 extent_count, flags;
1541 block_group = caching_ctl->block_group;
1543 path = btrfs_alloc_path();
1548 * Just like caching_thread() doesn't want to deadlock on the extent
1549 * tree, we don't want to deadlock on the free space tree.
1551 path->skip_locking = 1;
1552 path->search_commit_root = 1;
1553 path->reada = READA_FORWARD;
1555 info = search_free_space_info(NULL, block_group, path, 0);
1557 ret = PTR_ERR(info);
1560 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1561 flags = btrfs_free_space_flags(path->nodes[0], info);
1564 * We left path pointing to the free space info item, so now
1565 * load_free_space_foo can just iterate through the free space tree from
1568 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1569 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1571 ret = load_free_space_extents(caching_ctl, path, extent_count);
1574 btrfs_free_path(path);