@@ -3476,6 +3476,7 @@ enum btrfs_extent_allocation_policy {
34763476 */
34773477struct find_free_extent_ctl {
34783478 /* Basic allocation info */
3479+ u64 ram_bytes ;
34793480 u64 num_bytes ;
34803481 u64 empty_size ;
34813482 u64 flags ;
@@ -4130,73 +4131,70 @@ static int prepare_allocation(struct btrfs_fs_info *fs_info,
41304131 * |- If not found, re-iterate all block groups
41314132 */
41324133static noinline int find_free_extent (struct btrfs_root * root ,
4133- u64 ram_bytes , u64 num_bytes , u64 empty_size ,
4134- u64 hint_byte_orig , struct btrfs_key * ins ,
4135- u64 flags , int delalloc )
4134+ struct btrfs_key * ins ,
4135+ struct find_free_extent_ctl * ffe_ctl )
41364136{
41374137 struct btrfs_fs_info * fs_info = root -> fs_info ;
41384138 int ret = 0 ;
41394139 int cache_block_group_error = 0 ;
41404140 struct btrfs_block_group * block_group = NULL ;
4141- struct find_free_extent_ctl ffe_ctl = {0 };
41424141 struct btrfs_space_info * space_info ;
41434142 bool full_search = false;
4144- bool for_treelog = (root -> root_key .objectid == BTRFS_TREE_LOG_OBJECTID );
41454143
4146- WARN_ON (num_bytes < fs_info -> sectorsize );
4147-
4148- ffe_ctl .num_bytes = num_bytes ;
4149- ffe_ctl .empty_size = empty_size ;
4150- ffe_ctl .flags = flags ;
4151- ffe_ctl .search_start = 0 ;
4152- ffe_ctl .delalloc = delalloc ;
4153- ffe_ctl .index = btrfs_bg_flags_to_raid_index (flags );
4154- ffe_ctl .have_caching_bg = false;
4155- ffe_ctl .orig_have_caching_bg = false;
4156- ffe_ctl .found_offset = 0 ;
4157- ffe_ctl .hint_byte = hint_byte_orig ;
4158- ffe_ctl .for_treelog = for_treelog ;
4159- ffe_ctl .policy = BTRFS_EXTENT_ALLOC_CLUSTERED ;
4144+ WARN_ON (ffe_ctl -> num_bytes < fs_info -> sectorsize );
41604145
4146+ ffe_ctl -> search_start = 0 ;
4147+ /* For clustered allocation */
4148+ ffe_ctl -> empty_cluster = 0 ;
4149+ ffe_ctl -> last_ptr = NULL ;
4150+ ffe_ctl -> use_cluster = true;
4151+ ffe_ctl -> have_caching_bg = false;
4152+ ffe_ctl -> orig_have_caching_bg = false;
4153+ ffe_ctl -> index = btrfs_bg_flags_to_raid_index (ffe_ctl -> flags );
4154+ ffe_ctl -> loop = 0 ;
41614155 /* For clustered allocation */
4162- ffe_ctl .retry_clustered = false;
4163- ffe_ctl .retry_unclustered = false;
4164- ffe_ctl .last_ptr = NULL ;
4165- ffe_ctl .use_cluster = true;
4156+ ffe_ctl -> retry_clustered = false;
4157+ ffe_ctl -> retry_unclustered = false;
4158+ ffe_ctl -> cached = 0 ;
4159+ ffe_ctl -> max_extent_size = 0 ;
4160+ ffe_ctl -> total_free_space = 0 ;
4161+ ffe_ctl -> found_offset = 0 ;
4162+ ffe_ctl -> policy = BTRFS_EXTENT_ALLOC_CLUSTERED ;
41664163
41674164 if (btrfs_is_zoned (fs_info ))
4168- ffe_ctl . policy = BTRFS_EXTENT_ALLOC_ZONED ;
4165+ ffe_ctl -> policy = BTRFS_EXTENT_ALLOC_ZONED ;
41694166
41704167 ins -> type = BTRFS_EXTENT_ITEM_KEY ;
41714168 ins -> objectid = 0 ;
41724169 ins -> offset = 0 ;
41734170
4174- trace_find_free_extent (root , num_bytes , empty_size , flags );
4171+ trace_find_free_extent (root , ffe_ctl -> num_bytes , ffe_ctl -> empty_size ,
4172+ ffe_ctl -> flags );
41754173
4176- space_info = btrfs_find_space_info (fs_info , flags );
4174+ space_info = btrfs_find_space_info (fs_info , ffe_ctl -> flags );
41774175 if (!space_info ) {
4178- btrfs_err (fs_info , "No space info for %llu" , flags );
4176+ btrfs_err (fs_info , "No space info for %llu" , ffe_ctl -> flags );
41794177 return - ENOSPC ;
41804178 }
41814179
4182- ret = prepare_allocation (fs_info , & ffe_ctl , space_info , ins );
4180+ ret = prepare_allocation (fs_info , ffe_ctl , space_info , ins );
41834181 if (ret < 0 )
41844182 return ret ;
41854183
4186- ffe_ctl . search_start = max (ffe_ctl . search_start ,
4187- first_logical_byte (fs_info , 0 ));
4188- ffe_ctl . search_start = max (ffe_ctl . search_start , ffe_ctl . hint_byte );
4189- if (ffe_ctl . search_start == ffe_ctl . hint_byte ) {
4184+ ffe_ctl -> search_start = max (ffe_ctl -> search_start ,
4185+ first_logical_byte (fs_info , 0 ));
4186+ ffe_ctl -> search_start = max (ffe_ctl -> search_start , ffe_ctl -> hint_byte );
4187+ if (ffe_ctl -> search_start == ffe_ctl -> hint_byte ) {
41904188 block_group = btrfs_lookup_block_group (fs_info ,
4191- ffe_ctl . search_start );
4189+ ffe_ctl -> search_start );
41924190 /*
41934191 * we don't want to use the block group if it doesn't match our
41944192 * allocation bits, or if its not cached.
41954193 *
41964194 * However if we are re-searching with an ideal block group
41974195 * picked out then we don't care that the block group is cached.
41984196 */
4199- if (block_group && block_group_bits (block_group , flags ) &&
4197+ if (block_group && block_group_bits (block_group , ffe_ctl -> flags ) &&
42004198 block_group -> cached != BTRFS_CACHE_NO ) {
42014199 down_read (& space_info -> groups_sem );
42024200 if (list_empty (& block_group -> list ) ||
@@ -4210,41 +4208,42 @@ static noinline int find_free_extent(struct btrfs_root *root,
42104208 btrfs_put_block_group (block_group );
42114209 up_read (& space_info -> groups_sem );
42124210 } else {
4213- ffe_ctl .index = btrfs_bg_flags_to_raid_index (
4214- block_group -> flags );
4215- btrfs_lock_block_group (block_group , delalloc );
4211+ ffe_ctl -> index = btrfs_bg_flags_to_raid_index (
4212+ block_group -> flags );
4213+ btrfs_lock_block_group (block_group ,
4214+ ffe_ctl -> delalloc );
42164215 goto have_block_group ;
42174216 }
42184217 } else if (block_group ) {
42194218 btrfs_put_block_group (block_group );
42204219 }
42214220 }
42224221search :
4223- ffe_ctl . have_caching_bg = false;
4224- if (ffe_ctl . index == btrfs_bg_flags_to_raid_index (flags ) ||
4225- ffe_ctl . index == 0 )
4222+ ffe_ctl -> have_caching_bg = false;
4223+ if (ffe_ctl -> index == btrfs_bg_flags_to_raid_index (ffe_ctl -> flags ) ||
4224+ ffe_ctl -> index == 0 )
42264225 full_search = true;
42274226 down_read (& space_info -> groups_sem );
42284227 list_for_each_entry (block_group ,
4229- & space_info -> block_groups [ffe_ctl . index ], list ) {
4228+ & space_info -> block_groups [ffe_ctl -> index ], list ) {
42304229 struct btrfs_block_group * bg_ret ;
42314230
42324231 /* If the block group is read-only, we can skip it entirely. */
42334232 if (unlikely (block_group -> ro )) {
4234- if (for_treelog )
4233+ if (ffe_ctl -> for_treelog )
42354234 btrfs_clear_treelog_bg (block_group );
42364235 continue ;
42374236 }
42384237
4239- btrfs_grab_block_group (block_group , delalloc );
4240- ffe_ctl . search_start = block_group -> start ;
4238+ btrfs_grab_block_group (block_group , ffe_ctl -> delalloc );
4239+ ffe_ctl -> search_start = block_group -> start ;
42414240
42424241 /*
42434242 * this can happen if we end up cycling through all the
42444243 * raid types, but we want to make sure we only allocate
42454244 * for the proper type.
42464245 */
4247- if (!block_group_bits (block_group , flags )) {
4246+ if (!block_group_bits (block_group , ffe_ctl -> flags )) {
42484247 u64 extra = BTRFS_BLOCK_GROUP_DUP |
42494248 BTRFS_BLOCK_GROUP_RAID1_MASK |
42504249 BTRFS_BLOCK_GROUP_RAID56_MASK |
@@ -4255,22 +4254,22 @@ static noinline int find_free_extent(struct btrfs_root *root,
42554254 * doesn't provide them, bail. This does allow us to
42564255 * fill raid0 from raid1.
42574256 */
4258- if ((flags & extra ) && !(block_group -> flags & extra ))
4257+ if ((ffe_ctl -> flags & extra ) && !(block_group -> flags & extra ))
42594258 goto loop ;
42604259
42614260 /*
42624261 * This block group has different flags than we want.
42634262 * It's possible that we have MIXED_GROUP flag but no
42644263 * block group is mixed. Just skip such block group.
42654264 */
4266- btrfs_release_block_group (block_group , delalloc );
4265+ btrfs_release_block_group (block_group , ffe_ctl -> delalloc );
42674266 continue ;
42684267 }
42694268
42704269have_block_group :
4271- ffe_ctl . cached = btrfs_block_group_done (block_group );
4272- if (unlikely (!ffe_ctl . cached )) {
4273- ffe_ctl . have_caching_bg = true;
4270+ ffe_ctl -> cached = btrfs_block_group_done (block_group );
4271+ if (unlikely (!ffe_ctl -> cached )) {
4272+ ffe_ctl -> have_caching_bg = true;
42744273 ret = btrfs_cache_block_group (block_group , 0 );
42754274
42764275 /*
@@ -4293,10 +4292,11 @@ static noinline int find_free_extent(struct btrfs_root *root,
42934292 goto loop ;
42944293
42954294 bg_ret = NULL ;
4296- ret = do_allocation (block_group , & ffe_ctl , & bg_ret );
4295+ ret = do_allocation (block_group , ffe_ctl , & bg_ret );
42974296 if (ret == 0 ) {
42984297 if (bg_ret && bg_ret != block_group ) {
4299- btrfs_release_block_group (block_group , delalloc );
4298+ btrfs_release_block_group (block_group ,
4299+ ffe_ctl -> delalloc );
43004300 block_group = bg_ret ;
43014301 }
43024302 } else if (ret == - EAGAIN ) {
@@ -4306,46 +4306,49 @@ static noinline int find_free_extent(struct btrfs_root *root,
43064306 }
43074307
43084308 /* Checks */
4309- ffe_ctl . search_start = round_up (ffe_ctl . found_offset ,
4310- fs_info -> stripesize );
4309+ ffe_ctl -> search_start = round_up (ffe_ctl -> found_offset ,
4310+ fs_info -> stripesize );
43114311
43124312 /* move on to the next group */
4313- if (ffe_ctl . search_start + num_bytes >
4313+ if (ffe_ctl -> search_start + ffe_ctl -> num_bytes >
43144314 block_group -> start + block_group -> length ) {
43154315 btrfs_add_free_space_unused (block_group ,
4316- ffe_ctl .found_offset , num_bytes );
4316+ ffe_ctl -> found_offset ,
4317+ ffe_ctl -> num_bytes );
43174318 goto loop ;
43184319 }
43194320
4320- if (ffe_ctl . found_offset < ffe_ctl . search_start )
4321+ if (ffe_ctl -> found_offset < ffe_ctl -> search_start )
43214322 btrfs_add_free_space_unused (block_group ,
4322- ffe_ctl . found_offset ,
4323- ffe_ctl . search_start - ffe_ctl . found_offset );
4323+ ffe_ctl -> found_offset ,
4324+ ffe_ctl -> search_start - ffe_ctl -> found_offset );
43244325
4325- ret = btrfs_add_reserved_bytes (block_group , ram_bytes ,
4326- num_bytes , delalloc );
4326+ ret = btrfs_add_reserved_bytes (block_group , ffe_ctl -> ram_bytes ,
4327+ ffe_ctl -> num_bytes ,
4328+ ffe_ctl -> delalloc );
43274329 if (ret == - EAGAIN ) {
43284330 btrfs_add_free_space_unused (block_group ,
4329- ffe_ctl .found_offset , num_bytes );
4331+ ffe_ctl -> found_offset ,
4332+ ffe_ctl -> num_bytes );
43304333 goto loop ;
43314334 }
43324335 btrfs_inc_block_group_reservations (block_group );
43334336
43344337 /* we are all good, lets return */
4335- ins -> objectid = ffe_ctl . search_start ;
4336- ins -> offset = num_bytes ;
4338+ ins -> objectid = ffe_ctl -> search_start ;
4339+ ins -> offset = ffe_ctl -> num_bytes ;
43374340
4338- trace_btrfs_reserve_extent (block_group , ffe_ctl . search_start ,
4339- num_bytes );
4340- btrfs_release_block_group (block_group , delalloc );
4341+ trace_btrfs_reserve_extent (block_group , ffe_ctl -> search_start ,
4342+ ffe_ctl -> num_bytes );
4343+ btrfs_release_block_group (block_group , ffe_ctl -> delalloc );
43414344 break ;
43424345loop :
4343- release_block_group (block_group , & ffe_ctl , delalloc );
4346+ release_block_group (block_group , ffe_ctl , ffe_ctl -> delalloc );
43444347 cond_resched ();
43454348 }
43464349 up_read (& space_info -> groups_sem );
43474350
4348- ret = find_free_extent_update_loop (fs_info , ins , & ffe_ctl , full_search );
4351+ ret = find_free_extent_update_loop (fs_info , ins , ffe_ctl , full_search );
43494352 if (ret > 0 )
43504353 goto search ;
43514354
@@ -4354,12 +4357,12 @@ static noinline int find_free_extent(struct btrfs_root *root,
43544357 * Use ffe_ctl->total_free_space as fallback if we can't find
43554358 * any contiguous hole.
43564359 */
4357- if (!ffe_ctl . max_extent_size )
4358- ffe_ctl . max_extent_size = ffe_ctl . total_free_space ;
4360+ if (!ffe_ctl -> max_extent_size )
4361+ ffe_ctl -> max_extent_size = ffe_ctl -> total_free_space ;
43594362 spin_lock (& space_info -> lock );
4360- space_info -> max_extent_size = ffe_ctl . max_extent_size ;
4363+ space_info -> max_extent_size = ffe_ctl -> max_extent_size ;
43614364 spin_unlock (& space_info -> lock );
4362- ins -> offset = ffe_ctl . max_extent_size ;
4365+ ins -> offset = ffe_ctl -> max_extent_size ;
43634366 } else if (ret == - ENOSPC ) {
43644367 ret = cache_block_group_error ;
43654368 }
@@ -4417,6 +4420,7 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
44174420 struct btrfs_key * ins , int is_data , int delalloc )
44184421{
44194422 struct btrfs_fs_info * fs_info = root -> fs_info ;
4423+ struct find_free_extent_ctl ffe_ctl = {};
44204424 bool final_tried = num_bytes == min_alloc_size ;
44214425 u64 flags ;
44224426 int ret ;
@@ -4425,8 +4429,16 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
44254429 flags = get_alloc_profile_by_root (root , is_data );
44264430again :
44274431 WARN_ON (num_bytes < fs_info -> sectorsize );
4428- ret = find_free_extent (root , ram_bytes , num_bytes , empty_size ,
4429- hint_byte , ins , flags , delalloc );
4432+
4433+ ffe_ctl .ram_bytes = ram_bytes ;
4434+ ffe_ctl .num_bytes = num_bytes ;
4435+ ffe_ctl .empty_size = empty_size ;
4436+ ffe_ctl .flags = flags ;
4437+ ffe_ctl .delalloc = delalloc ;
4438+ ffe_ctl .hint_byte = hint_byte ;
4439+ ffe_ctl .for_treelog = for_treelog ;
4440+
4441+ ret = find_free_extent (root , ins , & ffe_ctl );
44304442 if (!ret && !is_data ) {
44314443 btrfs_dec_block_group_reservations (fs_info , ins -> objectid );
44324444 } else if (ret == - ENOSPC ) {
0 commit comments