Lines Matching refs:ctl

46 static int link_free_space(struct btrfs_free_space_ctl *ctl,
48 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
50 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
53 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
55 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
64 static void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) in __btrfs_remove_free_space_cache() argument
69 while ((node = rb_last(&ctl->free_space_offset)) != NULL) { in __btrfs_remove_free_space_cache()
72 unlink_free_space(ctl, info, true); in __btrfs_remove_free_space_cache()
75 free_bitmap(ctl, info); in __btrfs_remove_free_space_cache()
78 cond_resched_lock(&ctl->tree_lock); in __btrfs_remove_free_space_cache()
692 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) in recalculate_thresholds() argument
694 struct btrfs_block_group *block_group = ctl->block_group; in recalculate_thresholds()
699 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit; in recalculate_thresholds()
704 if (ctl->total_bitmaps > max_bitmaps) in recalculate_thresholds()
708 ctl->total_bitmaps, ctl->unit, max_bitmaps, in recalculate_thresholds()
710 ASSERT(ctl->total_bitmaps <= max_bitmaps); in recalculate_thresholds()
723 bitmap_bytes = ctl->total_bitmaps * ctl->unit; in recalculate_thresholds()
732 ctl->extents_thresh = in recalculate_thresholds()
737 struct btrfs_free_space_ctl *ctl, in __load_free_space_cache() argument
835 spin_lock(&ctl->tree_lock); in __load_free_space_cache()
836 ret = link_free_space(ctl, e); in __load_free_space_cache()
837 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
855 spin_lock(&ctl->tree_lock); in __load_free_space_cache()
856 ret = link_free_space(ctl, e); in __load_free_space_cache()
858 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
865 ctl->total_bitmaps++; in __load_free_space_cache()
866 recalculate_thresholds(ctl); in __load_free_space_cache()
867 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
895 spin_lock(&ctl->tree_lock); in __load_free_space_cache()
896 __btrfs_remove_free_space_cache(ctl); in __load_free_space_cache()
897 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
902 struct btrfs_free_space_ctl *ctl) in copy_free_space_cache() argument
908 while (!ret && (n = rb_first(&ctl->free_space_offset)) != NULL) { in copy_free_space_cache()
914 unlink_free_space(ctl, info, true); in copy_free_space_cache()
915 spin_unlock(&ctl->tree_lock); in copy_free_space_cache()
918 spin_lock(&ctl->tree_lock); in copy_free_space_cache()
921 u64 bytes = ctl->unit; in copy_free_space_cache()
923 ret = search_bitmap(ctl, info, &offset, &bytes, false); in copy_free_space_cache()
925 bitmap_clear_bits(ctl, info, offset, bytes, true); in copy_free_space_cache()
926 spin_unlock(&ctl->tree_lock); in copy_free_space_cache()
929 spin_lock(&ctl->tree_lock); in copy_free_space_cache()
931 free_bitmap(ctl, info); in copy_free_space_cache()
935 cond_resched_lock(&ctl->tree_lock); in copy_free_space_cache()
945 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in load_free_space_cache() local
1064 spin_lock(&ctl->tree_lock); in load_free_space_cache()
1066 spin_unlock(&ctl->tree_lock); in load_free_space_cache()
1073 struct btrfs_free_space_ctl *ctl, in write_cache_extent_entries() argument
1081 struct rb_node *node = rb_first(&ctl->free_space_offset); in write_cache_extent_entries()
1133 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) { in write_cache_extent_entries()
1380 struct btrfs_free_space_ctl *ctl, in __btrfs_write_out_cache() argument
1426 mutex_lock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1428 spin_lock(&ctl->tree_lock); in __btrfs_write_out_cache()
1429 ret = write_cache_extent_entries(io_ctl, ctl, in __btrfs_write_out_cache()
1453 spin_unlock(&ctl->tree_lock); in __btrfs_write_out_cache()
1454 mutex_unlock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1501 spin_unlock(&ctl->tree_lock); in __btrfs_write_out_cache()
1502 mutex_unlock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1529 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_write_out_cache() local
1544 ret = __btrfs_write_out_cache(inode, ctl, block_group, in btrfs_write_out_cache()
1579 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, in offset_to_bitmap() argument
1585 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; in offset_to_bitmap()
1586 bitmap_start = offset - ctl->start; in offset_to_bitmap()
1589 bitmap_start += ctl->start; in offset_to_bitmap()
1594 static int tree_insert_offset(struct btrfs_free_space_ctl *ctl, in tree_insert_offset() argument
1602 lockdep_assert_held(&ctl->tree_lock); in tree_insert_offset()
1608 root = &ctl->free_space_offset; in tree_insert_offset()
1711 tree_search_offset(struct btrfs_free_space_ctl *ctl, in tree_search_offset() argument
1714 struct rb_node *n = ctl->free_space_offset.rb_node; in tree_search_offset()
1717 lockdep_assert_held(&ctl->tree_lock); in tree_search_offset()
1799 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) in tree_search_offset()
1814 ctl->unit > offset) in tree_search_offset()
1824 static inline void unlink_free_space(struct btrfs_free_space_ctl *ctl, in unlink_free_space() argument
1828 lockdep_assert_held(&ctl->tree_lock); in unlink_free_space()
1830 rb_erase(&info->offset_index, &ctl->free_space_offset); in unlink_free_space()
1831 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); in unlink_free_space()
1832 ctl->free_extents--; in unlink_free_space()
1835 ctl->discardable_extents[BTRFS_STAT_CURR]--; in unlink_free_space()
1836 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes; in unlink_free_space()
1840 ctl->free_space -= info->bytes; in unlink_free_space()
1843 static int link_free_space(struct btrfs_free_space_ctl *ctl, in link_free_space() argument
1848 lockdep_assert_held(&ctl->tree_lock); in link_free_space()
1851 ret = tree_insert_offset(ctl, NULL, info); in link_free_space()
1855 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); in link_free_space()
1858 ctl->discardable_extents[BTRFS_STAT_CURR]++; in link_free_space()
1859 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; in link_free_space()
1862 ctl->free_space += info->bytes; in link_free_space()
1863 ctl->free_extents++; in link_free_space()
1867 static void relink_bitmap_entry(struct btrfs_free_space_ctl *ctl, in relink_bitmap_entry() argument
1879 lockdep_assert_held(&ctl->tree_lock); in relink_bitmap_entry()
1881 rb_erase_cached(&info->bytes_index, &ctl->free_space_bytes); in relink_bitmap_entry()
1882 rb_add_cached(&info->bytes_index, &ctl->free_space_bytes, entry_less); in relink_bitmap_entry()
1885 static inline void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, in bitmap_clear_bits() argument
1892 start = offset_to_bit(info->offset, ctl->unit, offset); in bitmap_clear_bits()
1893 count = bytes_to_bits(bytes, ctl->unit); in bitmap_clear_bits()
1900 if (info->max_extent_size > ctl->unit) in bitmap_clear_bits()
1903 relink_bitmap_entry(ctl, info); in bitmap_clear_bits()
1913 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; in bitmap_clear_bits()
1914 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; in bitmap_clear_bits()
1918 ctl->free_space -= bytes; in bitmap_clear_bits()
1921 static void btrfs_bitmap_set_bits(struct btrfs_free_space_ctl *ctl, in btrfs_bitmap_set_bits() argument
1928 start = offset_to_bit(info->offset, ctl->unit, offset); in btrfs_bitmap_set_bits()
1929 count = bytes_to_bits(bytes, ctl->unit); in btrfs_bitmap_set_bits()
1941 ctl->free_space += bytes; in btrfs_bitmap_set_bits()
1943 relink_bitmap_entry(ctl, info); in btrfs_bitmap_set_bits()
1953 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; in btrfs_bitmap_set_bits()
1954 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes; in btrfs_bitmap_set_bits()
1962 static int search_bitmap(struct btrfs_free_space_ctl *ctl, in search_bitmap() argument
1983 i = offset_to_bit(bitmap_info->offset, ctl->unit, in search_bitmap()
1985 bits = bytes_to_bits(*bytes, ctl->unit); in search_bitmap()
2005 *offset = (u64)(i * ctl->unit) + bitmap_info->offset; in search_bitmap()
2006 *bytes = (u64)(found_bits) * ctl->unit; in search_bitmap()
2010 *bytes = (u64)(max_bits) * ctl->unit; in search_bitmap()
2012 relink_bitmap_entry(ctl, bitmap_info); in search_bitmap()
2018 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes, in find_free_space() argument
2027 if (!ctl->free_space_offset.rb_node) in find_free_space()
2031 node = rb_first_cached(&ctl->free_space_bytes); in find_free_space()
2033 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), in find_free_space()
2068 tmp = entry->offset - ctl->start + align - 1; in find_free_space()
2070 tmp = tmp * align + ctl->start; in find_free_space()
2094 ret = search_bitmap(ctl, entry, &tmp, &size, true); in find_free_space()
2124 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, in add_new_bitmap() argument
2127 info->offset = offset_to_bitmap(ctl, offset); in add_new_bitmap()
2131 link_free_space(ctl, info); in add_new_bitmap()
2132 ctl->total_bitmaps++; in add_new_bitmap()
2133 recalculate_thresholds(ctl); in add_new_bitmap()
2136 static void free_bitmap(struct btrfs_free_space_ctl *ctl, in free_bitmap() argument
2146 ctl->discardable_extents[BTRFS_STAT_CURR] -= in free_bitmap()
2148 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes; in free_bitmap()
2151 unlink_free_space(ctl, bitmap_info, true); in free_bitmap()
2154 ctl->total_bitmaps--; in free_bitmap()
2155 recalculate_thresholds(ctl); in free_bitmap()
2158 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, in remove_from_bitmap() argument
2167 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; in remove_from_bitmap()
2176 search_bytes = ctl->unit; in remove_from_bitmap()
2178 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes, in remove_from_bitmap()
2189 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes, true); in remove_from_bitmap()
2196 free_bitmap(ctl, bitmap_info); in remove_from_bitmap()
2222 search_bytes = ctl->unit; in remove_from_bitmap()
2223 ret = search_bitmap(ctl, bitmap_info, &search_start, in remove_from_bitmap()
2230 free_bitmap(ctl, bitmap_info); in remove_from_bitmap()
2235 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, in add_bytes_to_bitmap() argument
2248 ctl->discardable_extents[BTRFS_STAT_CURR] += in add_bytes_to_bitmap()
2250 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; in add_bytes_to_bitmap()
2255 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); in add_bytes_to_bitmap()
2259 btrfs_bitmap_set_bits(ctl, info, offset, bytes_to_set); in add_bytes_to_bitmap()
2265 static bool use_bitmap(struct btrfs_free_space_ctl *ctl, in use_bitmap() argument
2268 struct btrfs_block_group *block_group = ctl->block_group; in use_bitmap()
2285 if (!forced && ctl->free_extents < ctl->extents_thresh) { in use_bitmap()
2294 if (ctl->free_extents * 3 <= ctl->extents_thresh) in use_bitmap()
2309 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length) in use_bitmap()
2319 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, in insert_into_bitmap() argument
2333 if (!ctl->op->use_bitmap(ctl, info)) in insert_into_bitmap()
2336 if (ctl->op == &free_space_op) in insert_into_bitmap()
2337 block_group = ctl->block_group; in insert_into_bitmap()
2365 if (entry->offset == offset_to_bitmap(ctl, offset)) { in insert_into_bitmap()
2366 bytes_added = add_bytes_to_bitmap(ctl, entry, offset, in insert_into_bitmap()
2379 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in insert_into_bitmap()
2386 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, in insert_into_bitmap()
2400 add_new_bitmap(ctl, info, offset); in insert_into_bitmap()
2405 spin_unlock(&ctl->tree_lock); in insert_into_bitmap()
2412 spin_lock(&ctl->tree_lock); in insert_into_bitmap()
2422 spin_lock(&ctl->tree_lock); in insert_into_bitmap()
2457 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, in try_merge_free_space() argument
2473 right_info = tree_search_offset(ctl, offset + bytes, 0, 0); in try_merge_free_space()
2480 left_info = tree_search_offset(ctl, offset - 1, 0, 0); in try_merge_free_space()
2485 unlink_free_space(ctl, right_info, update_stat); in try_merge_free_space()
2495 unlink_free_space(ctl, left_info, update_stat); in try_merge_free_space()
2505 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap_to_end() argument
2513 const u64 bitmap_offset = offset_to_bitmap(ctl, end); in steal_from_bitmap_to_end()
2516 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); in steal_from_bitmap_to_end()
2520 i = offset_to_bit(bitmap->offset, ctl->unit, end); in steal_from_bitmap_to_end()
2524 bytes = (j - i) * ctl->unit; in steal_from_bitmap_to_end()
2531 bitmap_clear_bits(ctl, bitmap, end, bytes, update_stat); in steal_from_bitmap_to_end()
2534 free_bitmap(ctl, bitmap); in steal_from_bitmap_to_end()
2539 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap_to_front() argument
2550 bitmap_offset = offset_to_bitmap(ctl, info->offset); in steal_from_bitmap_to_front()
2555 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1); in steal_from_bitmap_to_front()
2558 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); in steal_from_bitmap_to_front()
2562 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1; in steal_from_bitmap_to_front()
2574 bytes = (i + 1) * ctl->unit; in steal_from_bitmap_to_front()
2576 bytes = (i - prev_j) * ctl->unit; in steal_from_bitmap_to_front()
2585 bitmap_clear_bits(ctl, bitmap, info->offset, bytes, update_stat); in steal_from_bitmap_to_front()
2588 free_bitmap(ctl, bitmap); in steal_from_bitmap_to_front()
2604 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap() argument
2615 if (ctl->total_bitmaps > 0) { in steal_from_bitmap()
2619 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat); in steal_from_bitmap()
2620 if (ctl->total_bitmaps > 0) in steal_from_bitmap()
2621 stole_front = steal_from_bitmap_to_front(ctl, info, in steal_from_bitmap()
2625 try_merge_free_space(ctl, info, update_stat); in steal_from_bitmap()
2634 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in __btrfs_add_free_space() local
2651 spin_lock(&ctl->tree_lock); in __btrfs_add_free_space()
2653 if (try_merge_free_space(ctl, info, true)) in __btrfs_add_free_space()
2661 ret = insert_into_bitmap(ctl, info); in __btrfs_add_free_space()
2675 steal_from_bitmap(ctl, info, true); in __btrfs_add_free_space()
2679 ret = link_free_space(ctl, info); in __btrfs_add_free_space()
2684 spin_unlock(&ctl->tree_lock); in __btrfs_add_free_space()
2703 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in __btrfs_add_free_space_zoned() local
2729 spin_lock(&ctl->tree_lock); in __btrfs_add_free_space_zoned()
2730 ctl->free_space += to_free; in __btrfs_add_free_space_zoned()
2731 spin_unlock(&ctl->tree_lock); in __btrfs_add_free_space_zoned()
2809 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_remove_free_space() local
2834 spin_lock(&ctl->tree_lock); in btrfs_remove_free_space()
2841 info = tree_search_offset(ctl, offset, 0, 0); in btrfs_remove_free_space()
2847 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in btrfs_remove_free_space()
2862 unlink_free_space(ctl, info, true); in btrfs_remove_free_space()
2869 ret = link_free_space(ctl, info); in btrfs_remove_free_space()
2882 ret = link_free_space(ctl, info); in btrfs_remove_free_space()
2896 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space()
2907 ret = remove_from_bitmap(ctl, info, &offset, &bytes); in btrfs_remove_free_space()
2914 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space()
2923 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_dump_free_space() local
2940 spin_lock(&ctl->tree_lock); in btrfs_dump_free_space()
2941 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { in btrfs_dump_free_space()
2948 spin_unlock(&ctl->tree_lock); in btrfs_dump_free_space()
2957 struct btrfs_free_space_ctl *ctl) in btrfs_init_free_space_ctl() argument
2961 spin_lock_init(&ctl->tree_lock); in btrfs_init_free_space_ctl()
2962 ctl->unit = fs_info->sectorsize; in btrfs_init_free_space_ctl()
2963 ctl->start = block_group->start; in btrfs_init_free_space_ctl()
2964 ctl->block_group = block_group; in btrfs_init_free_space_ctl()
2965 ctl->op = &free_space_op; in btrfs_init_free_space_ctl()
2966 ctl->free_space_bytes = RB_ROOT_CACHED; in btrfs_init_free_space_ctl()
2967 INIT_LIST_HEAD(&ctl->trimming_ranges); in btrfs_init_free_space_ctl()
2968 mutex_init(&ctl->cache_writeout_mutex); in btrfs_init_free_space_ctl()
2975 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space); in btrfs_init_free_space_ctl()
2988 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in __btrfs_return_cluster_to_free_space() local
2991 lockdep_assert_held(&ctl->tree_lock); in __btrfs_return_cluster_to_free_space()
3015 ctl->discardable_extents[BTRFS_STAT_CURR]--; in __btrfs_return_cluster_to_free_space()
3016 ctl->discardable_bytes[BTRFS_STAT_CURR] -= in __btrfs_return_cluster_to_free_space()
3020 try_merge_free_space(ctl, entry, false); in __btrfs_return_cluster_to_free_space()
3021 steal_from_bitmap(ctl, entry, false); in __btrfs_return_cluster_to_free_space()
3025 ctl->discardable_extents[BTRFS_STAT_CURR]++; in __btrfs_return_cluster_to_free_space()
3026 ctl->discardable_bytes[BTRFS_STAT_CURR] += in __btrfs_return_cluster_to_free_space()
3030 tree_insert_offset(ctl, NULL, entry); in __btrfs_return_cluster_to_free_space()
3031 rb_add_cached(&entry->bytes_index, &ctl->free_space_bytes, in __btrfs_return_cluster_to_free_space()
3041 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_remove_free_space_cache() local
3045 spin_lock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
3054 cond_resched_lock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
3056 __btrfs_remove_free_space_cache(ctl); in btrfs_remove_free_space_cache()
3058 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
3067 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_is_free_space_trimmed() local
3072 spin_lock(&ctl->tree_lock); in btrfs_is_free_space_trimmed()
3073 node = rb_first(&ctl->free_space_offset); in btrfs_is_free_space_trimmed()
3086 spin_unlock(&ctl->tree_lock); in btrfs_is_free_space_trimmed()
3094 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_find_space_for_alloc() local
3107 spin_lock(&ctl->tree_lock); in btrfs_find_space_for_alloc()
3108 entry = find_free_space(ctl, &offset, &bytes_search, in btrfs_find_space_for_alloc()
3116 bitmap_clear_bits(ctl, entry, offset, bytes, true); in btrfs_find_space_for_alloc()
3122 free_bitmap(ctl, entry); in btrfs_find_space_for_alloc()
3124 unlink_free_space(ctl, entry, true); in btrfs_find_space_for_alloc()
3139 link_free_space(ctl, entry); in btrfs_find_space_for_alloc()
3143 spin_unlock(&ctl->tree_lock); in btrfs_find_space_for_alloc()
3163 struct btrfs_free_space_ctl *ctl; in btrfs_return_cluster_to_free_space() local
3181 ctl = block_group->free_space_ctl; in btrfs_return_cluster_to_free_space()
3184 spin_lock(&ctl->tree_lock); in btrfs_return_cluster_to_free_space()
3186 spin_unlock(&ctl->tree_lock); in btrfs_return_cluster_to_free_space()
3200 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_alloc_from_bitmap() local
3209 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true); in btrfs_alloc_from_bitmap()
3217 bitmap_clear_bits(ctl, entry, ret, bytes, false); in btrfs_alloc_from_bitmap()
3231 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_alloc_from_cluster() local
3296 spin_lock(&ctl->tree_lock); in btrfs_alloc_from_cluster()
3301 ctl->free_space -= bytes; in btrfs_alloc_from_cluster()
3303 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; in btrfs_alloc_from_cluster()
3308 ctl->free_extents--; in btrfs_alloc_from_cluster()
3312 ctl->total_bitmaps--; in btrfs_alloc_from_cluster()
3313 recalculate_thresholds(ctl); in btrfs_alloc_from_cluster()
3315 ctl->discardable_extents[BTRFS_STAT_CURR]--; in btrfs_alloc_from_cluster()
3321 spin_unlock(&ctl->tree_lock); in btrfs_alloc_from_cluster()
3332 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_bitmap_cluster() local
3343 lockdep_assert_held(&ctl->tree_lock); in btrfs_bitmap_cluster()
3345 i = offset_to_bit(entry->offset, ctl->unit, in btrfs_bitmap_cluster()
3347 want_bits = bytes_to_bits(bytes, ctl->unit); in btrfs_bitmap_cluster()
3348 min_bits = bytes_to_bits(min_bytes, ctl->unit); in btrfs_bitmap_cluster()
3374 entry->max_extent_size = (u64)max_bits * ctl->unit; in btrfs_bitmap_cluster()
3385 if (cluster->max_size < found_bits * ctl->unit) in btrfs_bitmap_cluster()
3386 cluster->max_size = found_bits * ctl->unit; in btrfs_bitmap_cluster()
3393 cluster->window_start = start * ctl->unit + entry->offset; in btrfs_bitmap_cluster()
3394 rb_erase(&entry->offset_index, &ctl->free_space_offset); in btrfs_bitmap_cluster()
3395 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); in btrfs_bitmap_cluster()
3406 ret = tree_insert_offset(ctl, cluster, entry); in btrfs_bitmap_cluster()
3410 total_found * ctl->unit, 1); in btrfs_bitmap_cluster()
3425 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in setup_cluster_no_bitmap() local
3434 lockdep_assert_held(&ctl->tree_lock); in setup_cluster_no_bitmap()
3436 entry = tree_search_offset(ctl, offset, 0, 1); in setup_cluster_no_bitmap()
3496 rb_erase(&entry->offset_index, &ctl->free_space_offset); in setup_cluster_no_bitmap()
3497 rb_erase_cached(&entry->bytes_index, &ctl->free_space_bytes); in setup_cluster_no_bitmap()
3498 ret = tree_insert_offset(ctl, cluster, entry); in setup_cluster_no_bitmap()
3518 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in setup_cluster_bitmap() local
3521 u64 bitmap_offset = offset_to_bitmap(ctl, offset); in setup_cluster_bitmap()
3523 if (ctl->total_bitmaps == 0) in setup_cluster_bitmap()
3534 entry = tree_search_offset(ctl, bitmap_offset, 1, 0); in setup_cluster_bitmap()
3568 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_find_space_cluster() local
3592 spin_lock(&ctl->tree_lock); in btrfs_find_space_cluster()
3598 if (ctl->free_space < bytes) { in btrfs_find_space_cluster()
3599 spin_unlock(&ctl->tree_lock); in btrfs_find_space_cluster()
3636 spin_unlock(&ctl->tree_lock); in btrfs_find_space_cluster()
3663 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in do_trimming() local
3687 mutex_lock(&ctl->cache_writeout_mutex); in do_trimming()
3697 mutex_unlock(&ctl->cache_writeout_mutex); in do_trimming()
3722 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in trim_no_bitmap() local
3735 mutex_lock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3736 spin_lock(&ctl->tree_lock); in trim_no_bitmap()
3738 if (ctl->free_space < minlen) in trim_no_bitmap()
3741 entry = tree_search_offset(ctl, start, 0, 1); in trim_no_bitmap()
3765 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3766 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3769 unlink_free_space(ctl, entry, true); in trim_no_bitmap()
3782 link_free_space(ctl, entry); in trim_no_bitmap()
3790 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3791 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3795 unlink_free_space(ctl, entry, true); in trim_no_bitmap()
3799 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3802 list_add_tail(&trim_entry.list, &ctl->trimming_ranges); in trim_no_bitmap()
3803 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3830 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3831 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3850 static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset) in reset_trimming_bitmap() argument
3854 spin_lock(&ctl->tree_lock); in reset_trimming_bitmap()
3855 entry = tree_search_offset(ctl, offset, 1, 0); in reset_trimming_bitmap()
3858 ctl->discardable_extents[BTRFS_STAT_CURR] += in reset_trimming_bitmap()
3860 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes; in reset_trimming_bitmap()
3865 spin_unlock(&ctl->tree_lock); in reset_trimming_bitmap()
3868 static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl, in end_trimming_bitmap() argument
3873 ctl->discardable_extents[BTRFS_STAT_CURR] -= in end_trimming_bitmap()
3875 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes; in end_trimming_bitmap()
3888 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in trim_bitmaps() local
3893 u64 offset = offset_to_bitmap(ctl, start); in trim_bitmaps()
3900 mutex_lock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3901 spin_lock(&ctl->tree_lock); in trim_bitmaps()
3903 if (ctl->free_space < minlen) { in trim_bitmaps()
3906 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3907 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3911 entry = tree_search_offset(ctl, offset, 1, 0); in trim_bitmaps()
3922 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3923 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3938 ret2 = search_bitmap(ctl, entry, &start, &bytes, false); in trim_bitmaps()
3945 end_trimming_bitmap(ctl, entry); in trim_bitmaps()
3948 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3949 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3959 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3960 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3966 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3967 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3982 bitmap_clear_bits(ctl, entry, start, bytes, true); in trim_bitmaps()
3984 free_bitmap(ctl, entry); in trim_bitmaps()
3986 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3989 list_add_tail(&trim_entry.list, &ctl->trimming_ranges); in trim_bitmaps()
3990 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3995 reset_trimming_bitmap(ctl, offset); in trim_bitmaps()
4002 offset += BITS_PER_BITMAP * ctl->unit; in trim_bitmaps()
4011 reset_trimming_bitmap(ctl, offset); in trim_bitmaps()
4029 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_trim_block_group() local
4050 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem); in btrfs_trim_block_group()
4053 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end)); in btrfs_trim_block_group()
4198 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; in test_add_free_space_entry() local
4213 spin_lock(&ctl->tree_lock); in test_add_free_space_entry()
4217 ret = link_free_space(ctl, info); in test_add_free_space_entry()
4218 spin_unlock(&ctl->tree_lock); in test_add_free_space_entry()
4232 spin_lock(&ctl->tree_lock); in test_add_free_space_entry()
4233 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in test_add_free_space_entry()
4238 add_new_bitmap(ctl, info, offset); in test_add_free_space_entry()
4243 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, in test_add_free_space_entry()
4248 spin_unlock(&ctl->tree_lock); in test_add_free_space_entry()
4268 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; in test_check_exists() local
4272 spin_lock(&ctl->tree_lock); in test_check_exists()
4273 info = tree_search_offset(ctl, offset, 0, 0); in test_check_exists()
4275 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in test_check_exists()
4288 bit_bytes = ctl->unit; in test_check_exists()
4289 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false); in test_check_exists()
4341 spin_unlock(&ctl->tree_lock); in test_check_exists()