Lines Matching refs:ctl
36 static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl,
38 static int link_free_space(struct btrfs_free_space_ctl *ctl,
40 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
637 static void merge_space_tree(struct btrfs_free_space_ctl *ctl) in merge_space_tree() argument
643 spin_lock(&ctl->tree_lock); in merge_space_tree()
644 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { in merge_space_tree()
651 unlink_free_space(ctl, prev); in merge_space_tree()
652 unlink_free_space(ctl, e); in merge_space_tree()
655 link_free_space(ctl, prev); in merge_space_tree()
657 spin_unlock(&ctl->tree_lock); in merge_space_tree()
663 spin_unlock(&ctl->tree_lock); in merge_space_tree()
667 struct btrfs_free_space_ctl *ctl, in __load_free_space_cache() argument
775 spin_lock(&ctl->tree_lock); in __load_free_space_cache()
776 ret = link_free_space(ctl, e); in __load_free_space_cache()
777 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
795 spin_lock(&ctl->tree_lock); in __load_free_space_cache()
796 ret = link_free_space(ctl, e); in __load_free_space_cache()
798 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
804 ctl->total_bitmaps++; in __load_free_space_cache()
805 ctl->op->recalc_thresholds(ctl); in __load_free_space_cache()
806 spin_unlock(&ctl->tree_lock); in __load_free_space_cache()
824 e->bitmap_extents = count_bitmap_extents(ctl, e); in __load_free_space_cache()
826 ctl->discardable_extents[BTRFS_STAT_CURR] += in __load_free_space_cache()
828 ctl->discardable_bytes[BTRFS_STAT_CURR] += e->bytes; in __load_free_space_cache()
833 merge_space_tree(ctl); in __load_free_space_cache()
836 btrfs_discard_update_discardable(ctl->private, ctl); in __load_free_space_cache()
841 __btrfs_remove_free_space_cache(ctl); in __load_free_space_cache()
848 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in load_free_space_cache() local
906 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl, in load_free_space_cache()
912 spin_lock(&ctl->tree_lock); in load_free_space_cache()
913 matched = (ctl->free_space == (block_group->length - used - in load_free_space_cache()
915 spin_unlock(&ctl->tree_lock); in load_free_space_cache()
918 __btrfs_remove_free_space_cache(ctl); in load_free_space_cache()
943 struct btrfs_free_space_ctl *ctl, in write_cache_extent_entries() argument
951 struct rb_node *node = rb_first(&ctl->free_space_offset); in write_cache_extent_entries()
1003 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) { in write_cache_extent_entries()
1257 struct btrfs_free_space_ctl *ctl, in __btrfs_write_out_cache() argument
1302 mutex_lock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1304 spin_lock(&ctl->tree_lock); in __btrfs_write_out_cache()
1305 ret = write_cache_extent_entries(io_ctl, ctl, in __btrfs_write_out_cache()
1329 spin_unlock(&ctl->tree_lock); in __btrfs_write_out_cache()
1330 mutex_unlock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1372 spin_unlock(&ctl->tree_lock); in __btrfs_write_out_cache()
1373 mutex_unlock(&ctl->cache_writeout_mutex); in __btrfs_write_out_cache()
1400 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_write_out_cache() local
1415 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl, in btrfs_write_out_cache()
1450 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, in offset_to_bitmap() argument
1456 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; in offset_to_bitmap()
1457 bitmap_start = offset - ctl->start; in offset_to_bitmap()
1460 bitmap_start += ctl->start; in offset_to_bitmap()
1524 tree_search_offset(struct btrfs_free_space_ctl *ctl, in tree_search_offset() argument
1527 struct rb_node *n = ctl->free_space_offset.rb_node; in tree_search_offset()
1613 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) in tree_search_offset()
1624 ctl->unit > offset) in tree_search_offset()
1640 __unlink_free_space(struct btrfs_free_space_ctl *ctl, in __unlink_free_space() argument
1643 rb_erase(&info->offset_index, &ctl->free_space_offset); in __unlink_free_space()
1644 ctl->free_extents--; in __unlink_free_space()
1647 ctl->discardable_extents[BTRFS_STAT_CURR]--; in __unlink_free_space()
1648 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes; in __unlink_free_space()
1652 static void unlink_free_space(struct btrfs_free_space_ctl *ctl, in unlink_free_space() argument
1655 __unlink_free_space(ctl, info); in unlink_free_space()
1656 ctl->free_space -= info->bytes; in unlink_free_space()
1659 static int link_free_space(struct btrfs_free_space_ctl *ctl, in link_free_space() argument
1665 ret = tree_insert_offset(&ctl->free_space_offset, info->offset, in link_free_space()
1671 ctl->discardable_extents[BTRFS_STAT_CURR]++; in link_free_space()
1672 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; in link_free_space()
1675 ctl->free_space += info->bytes; in link_free_space()
1676 ctl->free_extents++; in link_free_space()
1680 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) in recalculate_thresholds() argument
1682 struct btrfs_block_group *block_group = ctl->private; in recalculate_thresholds()
1687 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit; in recalculate_thresholds()
1692 ASSERT(ctl->total_bitmaps <= max_bitmaps); in recalculate_thresholds()
1705 bitmap_bytes = ctl->total_bitmaps * ctl->unit; in recalculate_thresholds()
1714 ctl->extents_thresh = in recalculate_thresholds()
1718 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, in __bitmap_clear_bits() argument
1725 start = offset_to_bit(info->offset, ctl->unit, offset); in __bitmap_clear_bits()
1726 count = bytes_to_bits(bytes, ctl->unit); in __bitmap_clear_bits()
1733 if (info->max_extent_size > ctl->unit) in __bitmap_clear_bits()
1744 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; in __bitmap_clear_bits()
1745 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; in __bitmap_clear_bits()
1749 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, in bitmap_clear_bits() argument
1753 __bitmap_clear_bits(ctl, info, offset, bytes); in bitmap_clear_bits()
1754 ctl->free_space -= bytes; in bitmap_clear_bits()
1757 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, in bitmap_set_bits() argument
1764 start = offset_to_bit(info->offset, ctl->unit, offset); in bitmap_set_bits()
1765 count = bytes_to_bits(bytes, ctl->unit); in bitmap_set_bits()
1772 ctl->free_space += bytes; in bitmap_set_bits()
1782 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta; in bitmap_set_bits()
1783 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes; in bitmap_set_bits()
1791 static int search_bitmap(struct btrfs_free_space_ctl *ctl, in search_bitmap() argument
1812 i = offset_to_bit(bitmap_info->offset, ctl->unit, in search_bitmap()
1814 bits = bytes_to_bits(*bytes, ctl->unit); in search_bitmap()
1834 *offset = (u64)(i * ctl->unit) + bitmap_info->offset; in search_bitmap()
1835 *bytes = (u64)(found_bits) * ctl->unit; in search_bitmap()
1839 *bytes = (u64)(max_bits) * ctl->unit; in search_bitmap()
1853 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes, in find_free_space() argument
1862 if (!ctl->free_space_offset.rb_node) in find_free_space()
1865 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1); in find_free_space()
1881 tmp = entry->offset - ctl->start + align - 1; in find_free_space()
1883 tmp = tmp * align + ctl->start; in find_free_space()
1899 ret = search_bitmap(ctl, entry, &tmp, &size, true); in find_free_space()
1920 static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl, in count_bitmap_extents() argument
1923 struct btrfs_block_group *block_group = ctl->private; in count_bitmap_extents()
1933 bytes -= (rs - re) * ctl->unit; in count_bitmap_extents()
1943 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, in add_new_bitmap() argument
1946 info->offset = offset_to_bitmap(ctl, offset); in add_new_bitmap()
1950 link_free_space(ctl, info); in add_new_bitmap()
1951 ctl->total_bitmaps++; in add_new_bitmap()
1953 ctl->op->recalc_thresholds(ctl); in add_new_bitmap()
1956 static void free_bitmap(struct btrfs_free_space_ctl *ctl, in free_bitmap() argument
1966 ctl->discardable_extents[BTRFS_STAT_CURR] -= in free_bitmap()
1968 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes; in free_bitmap()
1971 unlink_free_space(ctl, bitmap_info); in free_bitmap()
1974 ctl->total_bitmaps--; in free_bitmap()
1975 ctl->op->recalc_thresholds(ctl); in free_bitmap()
1978 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, in remove_from_bitmap() argument
1987 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; in remove_from_bitmap()
1996 search_bytes = ctl->unit; in remove_from_bitmap()
1998 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes, in remove_from_bitmap()
2009 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes); in remove_from_bitmap()
2016 free_bitmap(ctl, bitmap_info); in remove_from_bitmap()
2042 search_bytes = ctl->unit; in remove_from_bitmap()
2043 ret = search_bitmap(ctl, bitmap_info, &search_start, in remove_from_bitmap()
2050 free_bitmap(ctl, bitmap_info); in remove_from_bitmap()
2055 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl, in add_bytes_to_bitmap() argument
2068 ctl->discardable_extents[BTRFS_STAT_CURR] += in add_bytes_to_bitmap()
2070 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes; in add_bytes_to_bitmap()
2075 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); in add_bytes_to_bitmap()
2079 bitmap_set_bits(ctl, info, offset, bytes_to_set); in add_bytes_to_bitmap()
2091 static bool use_bitmap(struct btrfs_free_space_ctl *ctl, in use_bitmap() argument
2094 struct btrfs_block_group *block_group = ctl->private; in use_bitmap()
2111 if (!forced && ctl->free_extents < ctl->extents_thresh) { in use_bitmap()
2120 if (ctl->free_extents * 3 <= ctl->extents_thresh) in use_bitmap()
2135 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length) in use_bitmap()
2146 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, in insert_into_bitmap() argument
2160 if (!ctl->op->use_bitmap(ctl, info)) in insert_into_bitmap()
2163 if (ctl->op == &free_space_op) in insert_into_bitmap()
2164 block_group = ctl->private; in insert_into_bitmap()
2192 if (entry->offset == offset_to_bitmap(ctl, offset)) { in insert_into_bitmap()
2193 bytes_added = add_bytes_to_bitmap(ctl, entry, offset, in insert_into_bitmap()
2206 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in insert_into_bitmap()
2213 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, in insert_into_bitmap()
2227 add_new_bitmap(ctl, info, offset); in insert_into_bitmap()
2232 spin_unlock(&ctl->tree_lock); in insert_into_bitmap()
2239 spin_lock(&ctl->tree_lock); in insert_into_bitmap()
2249 spin_lock(&ctl->tree_lock); in insert_into_bitmap()
2284 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, in try_merge_free_space() argument
2299 right_info = tree_search_offset(ctl, offset + bytes, 0, 0); in try_merge_free_space()
2304 left_info = tree_search_offset(ctl, offset - 1, 0, 0); in try_merge_free_space()
2310 unlink_free_space(ctl, right_info); in try_merge_free_space()
2312 __unlink_free_space(ctl, right_info); in try_merge_free_space()
2323 unlink_free_space(ctl, left_info); in try_merge_free_space()
2325 __unlink_free_space(ctl, left_info); in try_merge_free_space()
2335 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap_to_end() argument
2343 const u64 bitmap_offset = offset_to_bitmap(ctl, end); in steal_from_bitmap_to_end()
2346 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); in steal_from_bitmap_to_end()
2350 i = offset_to_bit(bitmap->offset, ctl->unit, end); in steal_from_bitmap_to_end()
2354 bytes = (j - i) * ctl->unit; in steal_from_bitmap_to_end()
2362 bitmap_clear_bits(ctl, bitmap, end, bytes); in steal_from_bitmap_to_end()
2364 __bitmap_clear_bits(ctl, bitmap, end, bytes); in steal_from_bitmap_to_end()
2367 free_bitmap(ctl, bitmap); in steal_from_bitmap_to_end()
2372 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap_to_front() argument
2383 bitmap_offset = offset_to_bitmap(ctl, info->offset); in steal_from_bitmap_to_front()
2388 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1); in steal_from_bitmap_to_front()
2391 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0); in steal_from_bitmap_to_front()
2395 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1; in steal_from_bitmap_to_front()
2407 bytes = (i + 1) * ctl->unit; in steal_from_bitmap_to_front()
2409 bytes = (i - prev_j) * ctl->unit; in steal_from_bitmap_to_front()
2419 bitmap_clear_bits(ctl, bitmap, info->offset, bytes); in steal_from_bitmap_to_front()
2421 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes); in steal_from_bitmap_to_front()
2424 free_bitmap(ctl, bitmap); in steal_from_bitmap_to_front()
2440 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl, in steal_from_bitmap() argument
2451 if (ctl->total_bitmaps > 0) { in steal_from_bitmap()
2455 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat); in steal_from_bitmap()
2456 if (ctl->total_bitmaps > 0) in steal_from_bitmap()
2457 stole_front = steal_from_bitmap_to_front(ctl, info, in steal_from_bitmap()
2461 try_merge_free_space(ctl, info, update_stat); in steal_from_bitmap()
2466 struct btrfs_free_space_ctl *ctl, in __btrfs_add_free_space() argument
2470 struct btrfs_block_group *block_group = ctl->private; in __btrfs_add_free_space()
2484 spin_lock(&ctl->tree_lock); in __btrfs_add_free_space()
2486 if (try_merge_free_space(ctl, info, true)) in __btrfs_add_free_space()
2494 ret = insert_into_bitmap(ctl, info); in __btrfs_add_free_space()
2508 steal_from_bitmap(ctl, info, true); in __btrfs_add_free_space()
2512 ret = link_free_space(ctl, info); in __btrfs_add_free_space()
2516 btrfs_discard_update_discardable(block_group, ctl); in __btrfs_add_free_space()
2517 spin_unlock(&ctl->tree_lock); in __btrfs_add_free_space()
2567 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_remove_free_space() local
2572 spin_lock(&ctl->tree_lock); in btrfs_remove_free_space()
2579 info = tree_search_offset(ctl, offset, 0, 0); in btrfs_remove_free_space()
2585 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in btrfs_remove_free_space()
2600 unlink_free_space(ctl, info); in btrfs_remove_free_space()
2607 ret = link_free_space(ctl, info); in btrfs_remove_free_space()
2620 ret = link_free_space(ctl, info); in btrfs_remove_free_space()
2634 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space()
2636 ret = __btrfs_add_free_space(block_group->fs_info, ctl, in btrfs_remove_free_space()
2645 ret = remove_from_bitmap(ctl, info, &offset, &bytes); in btrfs_remove_free_space()
2651 btrfs_discard_update_discardable(block_group, ctl); in btrfs_remove_free_space()
2652 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space()
2661 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_dump_free_space() local
2666 spin_lock(&ctl->tree_lock); in btrfs_dump_free_space()
2667 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { in btrfs_dump_free_space()
2675 spin_unlock(&ctl->tree_lock); in btrfs_dump_free_space()
2685 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_init_free_space_ctl() local
2687 spin_lock_init(&ctl->tree_lock); in btrfs_init_free_space_ctl()
2688 ctl->unit = fs_info->sectorsize; in btrfs_init_free_space_ctl()
2689 ctl->start = block_group->start; in btrfs_init_free_space_ctl()
2690 ctl->private = block_group; in btrfs_init_free_space_ctl()
2691 ctl->op = &free_space_op; in btrfs_init_free_space_ctl()
2692 INIT_LIST_HEAD(&ctl->trimming_ranges); in btrfs_init_free_space_ctl()
2693 mutex_init(&ctl->cache_writeout_mutex); in btrfs_init_free_space_ctl()
2700 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space); in btrfs_init_free_space_ctl()
2713 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in __btrfs_return_cluster_to_free_space() local
2740 ctl->discardable_extents[BTRFS_STAT_CURR]--; in __btrfs_return_cluster_to_free_space()
2741 ctl->discardable_bytes[BTRFS_STAT_CURR] -= in __btrfs_return_cluster_to_free_space()
2745 try_merge_free_space(ctl, entry, false); in __btrfs_return_cluster_to_free_space()
2746 steal_from_bitmap(ctl, entry, false); in __btrfs_return_cluster_to_free_space()
2750 ctl->discardable_extents[BTRFS_STAT_CURR]++; in __btrfs_return_cluster_to_free_space()
2751 ctl->discardable_bytes[BTRFS_STAT_CURR] += in __btrfs_return_cluster_to_free_space()
2755 tree_insert_offset(&ctl->free_space_offset, in __btrfs_return_cluster_to_free_space()
2764 struct btrfs_free_space_ctl *ctl) in __btrfs_remove_free_space_cache_locked() argument
2769 while ((node = rb_last(&ctl->free_space_offset)) != NULL) { in __btrfs_remove_free_space_cache_locked()
2772 unlink_free_space(ctl, info); in __btrfs_remove_free_space_cache_locked()
2775 free_bitmap(ctl, info); in __btrfs_remove_free_space_cache_locked()
2778 cond_resched_lock(&ctl->tree_lock); in __btrfs_remove_free_space_cache_locked()
2782 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl) in __btrfs_remove_free_space_cache() argument
2784 spin_lock(&ctl->tree_lock); in __btrfs_remove_free_space_cache()
2785 __btrfs_remove_free_space_cache_locked(ctl); in __btrfs_remove_free_space_cache()
2786 if (ctl->private) in __btrfs_remove_free_space_cache()
2787 btrfs_discard_update_discardable(ctl->private, ctl); in __btrfs_remove_free_space_cache()
2788 spin_unlock(&ctl->tree_lock); in __btrfs_remove_free_space_cache()
2793 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_remove_free_space_cache() local
2797 spin_lock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
2806 cond_resched_lock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
2808 __btrfs_remove_free_space_cache_locked(ctl); in btrfs_remove_free_space_cache()
2809 btrfs_discard_update_discardable(block_group, ctl); in btrfs_remove_free_space_cache()
2810 spin_unlock(&ctl->tree_lock); in btrfs_remove_free_space_cache()
2822 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_is_free_space_trimmed() local
2827 spin_lock(&ctl->tree_lock); in btrfs_is_free_space_trimmed()
2828 node = rb_first(&ctl->free_space_offset); in btrfs_is_free_space_trimmed()
2841 spin_unlock(&ctl->tree_lock); in btrfs_is_free_space_trimmed()
2849 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_find_space_for_alloc() local
2859 spin_lock(&ctl->tree_lock); in btrfs_find_space_for_alloc()
2860 entry = find_free_space(ctl, &offset, &bytes_search, in btrfs_find_space_for_alloc()
2867 bitmap_clear_bits(ctl, entry, offset, bytes); in btrfs_find_space_for_alloc()
2873 free_bitmap(ctl, entry); in btrfs_find_space_for_alloc()
2875 unlink_free_space(ctl, entry); in btrfs_find_space_for_alloc()
2890 link_free_space(ctl, entry); in btrfs_find_space_for_alloc()
2893 btrfs_discard_update_discardable(block_group, ctl); in btrfs_find_space_for_alloc()
2894 spin_unlock(&ctl->tree_lock); in btrfs_find_space_for_alloc()
2897 __btrfs_add_free_space(block_group->fs_info, ctl, in btrfs_find_space_for_alloc()
2915 struct btrfs_free_space_ctl *ctl; in btrfs_return_cluster_to_free_space() local
2933 ctl = block_group->free_space_ctl; in btrfs_return_cluster_to_free_space()
2936 spin_lock(&ctl->tree_lock); in btrfs_return_cluster_to_free_space()
2938 spin_unlock(&ctl->tree_lock); in btrfs_return_cluster_to_free_space()
2952 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_alloc_from_bitmap() local
2961 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true); in btrfs_alloc_from_bitmap()
2969 __bitmap_clear_bits(ctl, entry, ret, bytes); in btrfs_alloc_from_bitmap()
2983 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_alloc_from_cluster() local
3046 spin_lock(&ctl->tree_lock); in btrfs_alloc_from_cluster()
3051 ctl->free_space -= bytes; in btrfs_alloc_from_cluster()
3053 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes; in btrfs_alloc_from_cluster()
3058 ctl->free_extents--; in btrfs_alloc_from_cluster()
3062 ctl->total_bitmaps--; in btrfs_alloc_from_cluster()
3063 ctl->op->recalc_thresholds(ctl); in btrfs_alloc_from_cluster()
3065 ctl->discardable_extents[BTRFS_STAT_CURR]--; in btrfs_alloc_from_cluster()
3071 spin_unlock(&ctl->tree_lock); in btrfs_alloc_from_cluster()
3082 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_bitmap_cluster() local
3093 i = offset_to_bit(entry->offset, ctl->unit, in btrfs_bitmap_cluster()
3095 want_bits = bytes_to_bits(bytes, ctl->unit); in btrfs_bitmap_cluster()
3096 min_bits = bytes_to_bits(min_bytes, ctl->unit); in btrfs_bitmap_cluster()
3122 entry->max_extent_size = (u64)max_bits * ctl->unit; in btrfs_bitmap_cluster()
3133 if (cluster->max_size < found_bits * ctl->unit) in btrfs_bitmap_cluster()
3134 cluster->max_size = found_bits * ctl->unit; in btrfs_bitmap_cluster()
3141 cluster->window_start = start * ctl->unit + entry->offset; in btrfs_bitmap_cluster()
3142 rb_erase(&entry->offset_index, &ctl->free_space_offset); in btrfs_bitmap_cluster()
3148 total_found * ctl->unit, 1); in btrfs_bitmap_cluster()
3163 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in setup_cluster_no_bitmap() local
3172 entry = tree_search_offset(ctl, offset, 0, 1); in setup_cluster_no_bitmap()
3232 rb_erase(&entry->offset_index, &ctl->free_space_offset); in setup_cluster_no_bitmap()
3254 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in setup_cluster_bitmap() local
3257 u64 bitmap_offset = offset_to_bitmap(ctl, offset); in setup_cluster_bitmap()
3259 if (ctl->total_bitmaps == 0) in setup_cluster_bitmap()
3270 entry = tree_search_offset(ctl, bitmap_offset, 1, 0); in setup_cluster_bitmap()
3304 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_find_space_cluster() local
3327 spin_lock(&ctl->tree_lock); in btrfs_find_space_cluster()
3333 if (ctl->free_space < bytes) { in btrfs_find_space_cluster()
3334 spin_unlock(&ctl->tree_lock); in btrfs_find_space_cluster()
3371 spin_unlock(&ctl->tree_lock); in btrfs_find_space_cluster()
3398 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in do_trimming() local
3422 mutex_lock(&ctl->cache_writeout_mutex); in do_trimming()
3424 __btrfs_add_free_space(fs_info, ctl, reserved_start, in do_trimming()
3428 __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end, in do_trimming()
3430 __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state); in do_trimming()
3432 mutex_unlock(&ctl->cache_writeout_mutex); in do_trimming()
3457 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in trim_no_bitmap() local
3470 mutex_lock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3471 spin_lock(&ctl->tree_lock); in trim_no_bitmap()
3473 if (ctl->free_space < minlen) in trim_no_bitmap()
3476 entry = tree_search_offset(ctl, start, 0, 1); in trim_no_bitmap()
3500 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3501 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3504 unlink_free_space(ctl, entry); in trim_no_bitmap()
3517 link_free_space(ctl, entry); in trim_no_bitmap()
3525 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3526 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3530 unlink_free_space(ctl, entry); in trim_no_bitmap()
3534 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3537 list_add_tail(&trim_entry.list, &ctl->trimming_ranges); in trim_no_bitmap()
3538 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3565 spin_unlock(&ctl->tree_lock); in trim_no_bitmap()
3566 mutex_unlock(&ctl->cache_writeout_mutex); in trim_no_bitmap()
3585 static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset) in reset_trimming_bitmap() argument
3589 spin_lock(&ctl->tree_lock); in reset_trimming_bitmap()
3590 entry = tree_search_offset(ctl, offset, 1, 0); in reset_trimming_bitmap()
3593 ctl->discardable_extents[BTRFS_STAT_CURR] += in reset_trimming_bitmap()
3595 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes; in reset_trimming_bitmap()
3600 spin_unlock(&ctl->tree_lock); in reset_trimming_bitmap()
3603 static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl, in end_trimming_bitmap() argument
3608 ctl->discardable_extents[BTRFS_STAT_CURR] -= in end_trimming_bitmap()
3610 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes; in end_trimming_bitmap()
3623 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in trim_bitmaps() local
3628 u64 offset = offset_to_bitmap(ctl, start); in trim_bitmaps()
3635 mutex_lock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3636 spin_lock(&ctl->tree_lock); in trim_bitmaps()
3638 if (ctl->free_space < minlen) { in trim_bitmaps()
3641 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3642 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3646 entry = tree_search_offset(ctl, offset, 1, 0); in trim_bitmaps()
3657 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3658 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3673 ret2 = search_bitmap(ctl, entry, &start, &bytes, false); in trim_bitmaps()
3680 end_trimming_bitmap(ctl, entry); in trim_bitmaps()
3683 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3684 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3694 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3695 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3701 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3702 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3717 bitmap_clear_bits(ctl, entry, start, bytes); in trim_bitmaps()
3719 free_bitmap(ctl, entry); in trim_bitmaps()
3721 spin_unlock(&ctl->tree_lock); in trim_bitmaps()
3724 list_add_tail(&trim_entry.list, &ctl->trimming_ranges); in trim_bitmaps()
3725 mutex_unlock(&ctl->cache_writeout_mutex); in trim_bitmaps()
3730 reset_trimming_bitmap(ctl, offset); in trim_bitmaps()
3737 offset += BITS_PER_BITMAP * ctl->unit; in trim_bitmaps()
3746 reset_trimming_bitmap(ctl, offset); in trim_bitmaps()
3764 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; in btrfs_trim_block_group() local
3783 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem); in btrfs_trim_block_group()
3786 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end)); in btrfs_trim_block_group()
3847 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl; in btrfs_find_ino_for_alloc() local
3851 spin_lock(&ctl->tree_lock); in btrfs_find_ino_for_alloc()
3853 if (RB_EMPTY_ROOT(&ctl->free_space_offset)) in btrfs_find_ino_for_alloc()
3856 entry = rb_entry(rb_first(&ctl->free_space_offset), in btrfs_find_ino_for_alloc()
3862 unlink_free_space(ctl, entry); in btrfs_find_ino_for_alloc()
3868 link_free_space(ctl, entry); in btrfs_find_ino_for_alloc()
3874 ret = search_bitmap(ctl, entry, &offset, &count, true); in btrfs_find_ino_for_alloc()
3879 bitmap_clear_bits(ctl, entry, offset, 1); in btrfs_find_ino_for_alloc()
3881 free_bitmap(ctl, entry); in btrfs_find_ino_for_alloc()
3884 spin_unlock(&ctl->tree_lock); in btrfs_find_ino_for_alloc()
3923 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; in load_free_ino_cache() local
3950 ret = __load_free_space_cache(root, inode, ctl, path, 0); in load_free_ino_cache()
3969 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; in btrfs_write_out_ino_cache() local
3978 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans); in btrfs_write_out_ino_cache()
4012 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; in test_add_free_space_entry() local
4027 spin_lock(&ctl->tree_lock); in test_add_free_space_entry()
4031 ret = link_free_space(ctl, info); in test_add_free_space_entry()
4032 spin_unlock(&ctl->tree_lock); in test_add_free_space_entry()
4046 spin_lock(&ctl->tree_lock); in test_add_free_space_entry()
4047 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in test_add_free_space_entry()
4052 add_new_bitmap(ctl, info, offset); in test_add_free_space_entry()
4057 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes, in test_add_free_space_entry()
4062 spin_unlock(&ctl->tree_lock); in test_add_free_space_entry()
4082 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl; in test_check_exists() local
4086 spin_lock(&ctl->tree_lock); in test_check_exists()
4087 info = tree_search_offset(ctl, offset, 0, 0); in test_check_exists()
4089 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), in test_check_exists()
4102 bit_bytes = ctl->unit; in test_check_exists()
4103 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false); in test_check_exists()
4155 spin_unlock(&ctl->tree_lock); in test_check_exists()