Skip to content

Commit 606d1bf

Browse files
josefbacikkdave
authored andcommitted
btrfs: migrate the block group space accounting helpers
We can now easily migrate this code as well. Signed-off-by: Josef Bacik <josef@toxicpanda.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent ade4b51 commit 606d1bf

2 files changed

Lines changed: 175 additions & 173 deletions

File tree

‎fs/btrfs/block-group.c‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,3 +2519,178 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans)
25192519
btrfs_free_path(path);
25202520
return ret;
25212521
}
2522+
2523+
int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2524+
u64 bytenr, u64 num_bytes, int alloc)
2525+
{
2526+
struct btrfs_fs_info *info = trans->fs_info;
2527+
struct btrfs_block_group_cache *cache = NULL;
2528+
u64 total = num_bytes;
2529+
u64 old_val;
2530+
u64 byte_in_group;
2531+
int factor;
2532+
int ret = 0;
2533+
2534+
/* Block accounting for super block */
2535+
spin_lock(&info->delalloc_root_lock);
2536+
old_val = btrfs_super_bytes_used(info->super_copy);
2537+
if (alloc)
2538+
old_val += num_bytes;
2539+
else
2540+
old_val -= num_bytes;
2541+
btrfs_set_super_bytes_used(info->super_copy, old_val);
2542+
spin_unlock(&info->delalloc_root_lock);
2543+
2544+
while (total) {
2545+
cache = btrfs_lookup_block_group(info, bytenr);
2546+
if (!cache) {
2547+
ret = -ENOENT;
2548+
break;
2549+
}
2550+
factor = btrfs_bg_type_to_factor(cache->flags);
2551+
2552+
/*
2553+
* If this block group has free space cache written out, we
2554+
* need to make sure to load it if we are removing space. This
2555+
* is because we need the unpinning stage to actually add the
2556+
* space back to the block group, otherwise we will leak space.
2557+
*/
2558+
if (!alloc && cache->cached == BTRFS_CACHE_NO)
2559+
btrfs_cache_block_group(cache, 1);
2560+
2561+
byte_in_group = bytenr - cache->key.objectid;
2562+
WARN_ON(byte_in_group > cache->key.offset);
2563+
2564+
spin_lock(&cache->space_info->lock);
2565+
spin_lock(&cache->lock);
2566+
2567+
if (btrfs_test_opt(info, SPACE_CACHE) &&
2568+
cache->disk_cache_state < BTRFS_DC_CLEAR)
2569+
cache->disk_cache_state = BTRFS_DC_CLEAR;
2570+
2571+
old_val = btrfs_block_group_used(&cache->item);
2572+
num_bytes = min(total, cache->key.offset - byte_in_group);
2573+
if (alloc) {
2574+
old_val += num_bytes;
2575+
btrfs_set_block_group_used(&cache->item, old_val);
2576+
cache->reserved -= num_bytes;
2577+
cache->space_info->bytes_reserved -= num_bytes;
2578+
cache->space_info->bytes_used += num_bytes;
2579+
cache->space_info->disk_used += num_bytes * factor;
2580+
spin_unlock(&cache->lock);
2581+
spin_unlock(&cache->space_info->lock);
2582+
} else {
2583+
old_val -= num_bytes;
2584+
btrfs_set_block_group_used(&cache->item, old_val);
2585+
cache->pinned += num_bytes;
2586+
btrfs_space_info_update_bytes_pinned(info,
2587+
cache->space_info, num_bytes);
2588+
cache->space_info->bytes_used -= num_bytes;
2589+
cache->space_info->disk_used -= num_bytes * factor;
2590+
spin_unlock(&cache->lock);
2591+
spin_unlock(&cache->space_info->lock);
2592+
2593+
trace_btrfs_space_reservation(info, "pinned",
2594+
cache->space_info->flags,
2595+
num_bytes, 1);
2596+
percpu_counter_add_batch(
2597+
&cache->space_info->total_bytes_pinned,
2598+
num_bytes,
2599+
BTRFS_TOTAL_BYTES_PINNED_BATCH);
2600+
set_extent_dirty(info->pinned_extents,
2601+
bytenr, bytenr + num_bytes - 1,
2602+
GFP_NOFS | __GFP_NOFAIL);
2603+
}
2604+
2605+
spin_lock(&trans->transaction->dirty_bgs_lock);
2606+
if (list_empty(&cache->dirty_list)) {
2607+
list_add_tail(&cache->dirty_list,
2608+
&trans->transaction->dirty_bgs);
2609+
trans->delayed_ref_updates++;
2610+
btrfs_get_block_group(cache);
2611+
}
2612+
spin_unlock(&trans->transaction->dirty_bgs_lock);
2613+
2614+
/*
2615+
* No longer have used bytes in this block group, queue it for
2616+
* deletion. We do this after adding the block group to the
2617+
* dirty list to avoid races between cleaner kthread and space
2618+
* cache writeout.
2619+
*/
2620+
if (!alloc && old_val == 0)
2621+
btrfs_mark_bg_unused(cache);
2622+
2623+
btrfs_put_block_group(cache);
2624+
total -= num_bytes;
2625+
bytenr += num_bytes;
2626+
}
2627+
2628+
/* Modified block groups are accounted for in the delayed_refs_rsv. */
2629+
btrfs_update_delayed_refs_rsv(trans);
2630+
return ret;
2631+
}
2632+
2633+
/**
2634+
* btrfs_add_reserved_bytes - update the block_group and space info counters
2635+
* @cache: The cache we are manipulating
2636+
* @ram_bytes: The number of bytes of file content, and will be same to
2637+
* @num_bytes except for the compress path.
2638+
* @num_bytes: The number of bytes in question
2639+
* @delalloc: The blocks are allocated for the delalloc write
2640+
*
2641+
* This is called by the allocator when it reserves space. If this is a
2642+
* reservation and the block group has become read only we cannot make the
2643+
* reservation and return -EAGAIN, otherwise this function always succeeds.
2644+
*/
2645+
int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
2646+
u64 ram_bytes, u64 num_bytes, int delalloc)
2647+
{
2648+
struct btrfs_space_info *space_info = cache->space_info;
2649+
int ret = 0;
2650+
2651+
spin_lock(&space_info->lock);
2652+
spin_lock(&cache->lock);
2653+
if (cache->ro) {
2654+
ret = -EAGAIN;
2655+
} else {
2656+
cache->reserved += num_bytes;
2657+
space_info->bytes_reserved += num_bytes;
2658+
btrfs_space_info_update_bytes_may_use(cache->fs_info,
2659+
space_info, -ram_bytes);
2660+
if (delalloc)
2661+
cache->delalloc_bytes += num_bytes;
2662+
}
2663+
spin_unlock(&cache->lock);
2664+
spin_unlock(&space_info->lock);
2665+
return ret;
2666+
}
2667+
2668+
/**
2669+
* btrfs_free_reserved_bytes - update the block_group and space info counters
2670+
* @cache: The cache we are manipulating
2671+
* @num_bytes: The number of bytes in question
2672+
* @delalloc: The blocks are allocated for the delalloc write
2673+
*
2674+
* This is called by somebody who is freeing space that was never actually used
2675+
* on disk. For example if you reserve some space for a new leaf in transaction
2676+
* A and before transaction A commits you free that leaf, you call this with
2677+
* reserve set to 0 in order to clear the reservation.
2678+
*/
2679+
void btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
2680+
u64 num_bytes, int delalloc)
2681+
{
2682+
struct btrfs_space_info *space_info = cache->space_info;
2683+
2684+
spin_lock(&space_info->lock);
2685+
spin_lock(&cache->lock);
2686+
if (cache->ro)
2687+
space_info->bytes_readonly += num_bytes;
2688+
cache->reserved -= num_bytes;
2689+
space_info->bytes_reserved -= num_bytes;
2690+
space_info->max_extent_size = 0;
2691+
2692+
if (delalloc)
2693+
cache->delalloc_bytes -= num_bytes;
2694+
spin_unlock(&cache->lock);
2695+
spin_unlock(&space_info->lock);
2696+
}

‎fs/btrfs/extent-tree.c‎

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,115 +2898,6 @@ int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags,
28982898
return ret;
28992899
}
29002900

2901-
int btrfs_update_block_group(struct btrfs_trans_handle *trans,
2902-
u64 bytenr, u64 num_bytes, int alloc)
2903-
{
2904-
struct btrfs_fs_info *info = trans->fs_info;
2905-
struct btrfs_block_group_cache *cache = NULL;
2906-
u64 total = num_bytes;
2907-
u64 old_val;
2908-
u64 byte_in_group;
2909-
int factor;
2910-
int ret = 0;
2911-
2912-
/* block accounting for super block */
2913-
spin_lock(&info->delalloc_root_lock);
2914-
old_val = btrfs_super_bytes_used(info->super_copy);
2915-
if (alloc)
2916-
old_val += num_bytes;
2917-
else
2918-
old_val -= num_bytes;
2919-
btrfs_set_super_bytes_used(info->super_copy, old_val);
2920-
spin_unlock(&info->delalloc_root_lock);
2921-
2922-
while (total) {
2923-
cache = btrfs_lookup_block_group(info, bytenr);
2924-
if (!cache) {
2925-
ret = -ENOENT;
2926-
break;
2927-
}
2928-
factor = btrfs_bg_type_to_factor(cache->flags);
2929-
2930-
/*
2931-
* If this block group has free space cache written out, we
2932-
* need to make sure to load it if we are removing space. This
2933-
* is because we need the unpinning stage to actually add the
2934-
* space back to the block group, otherwise we will leak space.
2935-
*/
2936-
if (!alloc && cache->cached == BTRFS_CACHE_NO)
2937-
btrfs_cache_block_group(cache, 1);
2938-
2939-
byte_in_group = bytenr - cache->key.objectid;
2940-
WARN_ON(byte_in_group > cache->key.offset);
2941-
2942-
spin_lock(&cache->space_info->lock);
2943-
spin_lock(&cache->lock);
2944-
2945-
if (btrfs_test_opt(info, SPACE_CACHE) &&
2946-
cache->disk_cache_state < BTRFS_DC_CLEAR)
2947-
cache->disk_cache_state = BTRFS_DC_CLEAR;
2948-
2949-
old_val = btrfs_block_group_used(&cache->item);
2950-
num_bytes = min(total, cache->key.offset - byte_in_group);
2951-
if (alloc) {
2952-
old_val += num_bytes;
2953-
btrfs_set_block_group_used(&cache->item, old_val);
2954-
cache->reserved -= num_bytes;
2955-
cache->space_info->bytes_reserved -= num_bytes;
2956-
cache->space_info->bytes_used += num_bytes;
2957-
cache->space_info->disk_used += num_bytes * factor;
2958-
spin_unlock(&cache->lock);
2959-
spin_unlock(&cache->space_info->lock);
2960-
} else {
2961-
old_val -= num_bytes;
2962-
btrfs_set_block_group_used(&cache->item, old_val);
2963-
cache->pinned += num_bytes;
2964-
btrfs_space_info_update_bytes_pinned(info,
2965-
cache->space_info, num_bytes);
2966-
cache->space_info->bytes_used -= num_bytes;
2967-
cache->space_info->disk_used -= num_bytes * factor;
2968-
spin_unlock(&cache->lock);
2969-
spin_unlock(&cache->space_info->lock);
2970-
2971-
trace_btrfs_space_reservation(info, "pinned",
2972-
cache->space_info->flags,
2973-
num_bytes, 1);
2974-
percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2975-
num_bytes,
2976-
BTRFS_TOTAL_BYTES_PINNED_BATCH);
2977-
set_extent_dirty(info->pinned_extents,
2978-
bytenr, bytenr + num_bytes - 1,
2979-
GFP_NOFS | __GFP_NOFAIL);
2980-
}
2981-
2982-
spin_lock(&trans->transaction->dirty_bgs_lock);
2983-
if (list_empty(&cache->dirty_list)) {
2984-
list_add_tail(&cache->dirty_list,
2985-
&trans->transaction->dirty_bgs);
2986-
trans->delayed_ref_updates++;
2987-
btrfs_get_block_group(cache);
2988-
}
2989-
spin_unlock(&trans->transaction->dirty_bgs_lock);
2990-
2991-
/*
2992-
* No longer have used bytes in this block group, queue it for
2993-
* deletion. We do this after adding the block group to the
2994-
* dirty list to avoid races between cleaner kthread and space
2995-
* cache writeout.
2996-
*/
2997-
if (!alloc && old_val == 0)
2998-
btrfs_mark_bg_unused(cache);
2999-
3000-
btrfs_put_block_group(cache);
3001-
total -= num_bytes;
3002-
bytenr += num_bytes;
3003-
}
3004-
3005-
/* Modified block groups are accounted for in the delayed_refs_rsv. */
3006-
btrfs_update_delayed_refs_rsv(trans);
3007-
return ret;
3008-
}
3009-
30102901
static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
30112902
{
30122903
struct btrfs_block_group_cache *cache;
@@ -3187,70 +3078,6 @@ btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg)
31873078
atomic_inc(&bg->reservations);
31883079
}
31893080

3190-
/**
3191-
* btrfs_add_reserved_bytes - update the block_group and space info counters
3192-
* @cache: The cache we are manipulating
3193-
* @ram_bytes: The number of bytes of file content, and will be same to
3194-
* @num_bytes except for the compress path.
3195-
* @num_bytes: The number of bytes in question
3196-
* @delalloc: The blocks are allocated for the delalloc write
3197-
*
3198-
* This is called by the allocator when it reserves space. If this is a
3199-
* reservation and the block group has become read only we cannot make the
3200-
* reservation and return -EAGAIN, otherwise this function always succeeds.
3201-
*/
3202-
int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
3203-
u64 ram_bytes, u64 num_bytes, int delalloc)
3204-
{
3205-
struct btrfs_space_info *space_info = cache->space_info;
3206-
int ret = 0;
3207-
3208-
spin_lock(&space_info->lock);
3209-
spin_lock(&cache->lock);
3210-
if (cache->ro) {
3211-
ret = -EAGAIN;
3212-
} else {
3213-
cache->reserved += num_bytes;
3214-
space_info->bytes_reserved += num_bytes;
3215-
btrfs_space_info_update_bytes_may_use(cache->fs_info,
3216-
space_info, -ram_bytes);
3217-
if (delalloc)
3218-
cache->delalloc_bytes += num_bytes;
3219-
}
3220-
spin_unlock(&cache->lock);
3221-
spin_unlock(&space_info->lock);
3222-
return ret;
3223-
}
3224-
3225-
/**
3226-
* btrfs_free_reserved_bytes - update the block_group and space info counters
3227-
* @cache: The cache we are manipulating
3228-
* @num_bytes: The number of bytes in question
3229-
* @delalloc: The blocks are allocated for the delalloc write
3230-
*
3231-
* This is called by somebody who is freeing space that was never actually used
3232-
* on disk. For example if you reserve some space for a new leaf in transaction
3233-
* A and before transaction A commits you free that leaf, you call this with
3234-
* reserve set to 0 in order to clear the reservation.
3235-
*/
3236-
void btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
3237-
u64 num_bytes, int delalloc)
3238-
{
3239-
struct btrfs_space_info *space_info = cache->space_info;
3240-
3241-
spin_lock(&space_info->lock);
3242-
spin_lock(&cache->lock);
3243-
if (cache->ro)
3244-
space_info->bytes_readonly += num_bytes;
3245-
cache->reserved -= num_bytes;
3246-
space_info->bytes_reserved -= num_bytes;
3247-
space_info->max_extent_size = 0;
3248-
3249-
if (delalloc)
3250-
cache->delalloc_bytes -= num_bytes;
3251-
spin_unlock(&cache->lock);
3252-
spin_unlock(&space_info->lock);
3253-
}
32543081
void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
32553082
{
32563083
struct btrfs_caching_control *next;

0 commit comments

Comments
 (0)