@@ -1187,7 +1187,7 @@ int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1187
1187
* offset.
1188
1188
*/
1189
1189
static int calculate_alloc_pointer (struct btrfs_block_group * cache ,
1190
- u64 * offset_ret )
1190
+ u64 * offset_ret , bool new )
1191
1191
{
1192
1192
struct btrfs_fs_info * fs_info = cache -> fs_info ;
1193
1193
struct btrfs_root * root ;
@@ -1197,6 +1197,21 @@ static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1197
1197
int ret ;
1198
1198
u64 length ;
1199
1199
1200
+ /*
1201
+ * Avoid tree lookups for a new block group, there's no use for it.
1202
+ * It must always be 0.
1203
+ *
1204
+ * Also, we have a lock chain of extent buffer lock -> chunk mutex.
1205
+ * For new a block group, this function is called from
1206
+ * btrfs_make_block_group() which is already taking the chunk mutex.
1207
+ * Thus, we cannot call calculate_alloc_pointer() which takes extent
1208
+ * buffer locks to avoid deadlock.
1209
+ */
1210
+ if (new ) {
1211
+ * offset_ret = 0 ;
1212
+ return 0 ;
1213
+ }
1214
+
1200
1215
path = btrfs_alloc_path ();
1201
1216
if (!path )
1202
1217
return - ENOMEM ;
@@ -1332,6 +1347,13 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1332
1347
else
1333
1348
num_conventional ++ ;
1334
1349
1350
+ /*
1351
+ * Consider a zone as active if we can allow any number of
1352
+ * active zones.
1353
+ */
1354
+ if (!device -> zone_info -> max_active_zones )
1355
+ __set_bit (i , active );
1356
+
1335
1357
if (!is_sequential ) {
1336
1358
alloc_offsets [i ] = WP_CONVENTIONAL ;
1337
1359
continue ;
@@ -1398,45 +1420,23 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1398
1420
__set_bit (i , active );
1399
1421
break ;
1400
1422
}
1401
-
1402
- /*
1403
- * Consider a zone as active if we can allow any number of
1404
- * active zones.
1405
- */
1406
- if (!device -> zone_info -> max_active_zones )
1407
- __set_bit (i , active );
1408
1423
}
1409
1424
1410
1425
if (num_sequential > 0 )
1411
1426
cache -> seq_zone = true;
1412
1427
1413
1428
if (num_conventional > 0 ) {
1414
- /*
1415
- * Avoid calling calculate_alloc_pointer() for new BG. It
1416
- * is no use for new BG. It must be always 0.
1417
- *
1418
- * Also, we have a lock chain of extent buffer lock ->
1419
- * chunk mutex. For new BG, this function is called from
1420
- * btrfs_make_block_group() which is already taking the
1421
- * chunk mutex. Thus, we cannot call
1422
- * calculate_alloc_pointer() which takes extent buffer
1423
- * locks to avoid deadlock.
1424
- */
1425
-
1426
1429
/* Zone capacity is always zone size in emulation */
1427
1430
cache -> zone_capacity = cache -> length ;
1428
- if (new ) {
1429
- cache -> alloc_offset = 0 ;
1430
- goto out ;
1431
- }
1432
- ret = calculate_alloc_pointer (cache , & last_alloc );
1433
- if (ret || map -> num_stripes == num_conventional ) {
1434
- if (!ret )
1435
- cache -> alloc_offset = last_alloc ;
1436
- else
1437
- btrfs_err (fs_info ,
1431
+ ret = calculate_alloc_pointer (cache , & last_alloc , new );
1432
+ if (ret ) {
1433
+ btrfs_err (fs_info ,
1438
1434
"zoned: failed to determine allocation offset of bg %llu" ,
1439
- cache -> start );
1435
+ cache -> start );
1436
+ goto out ;
1437
+ } else if (map -> num_stripes == num_conventional ) {
1438
+ cache -> alloc_offset = last_alloc ;
1439
+ cache -> zone_is_active = 1 ;
1440
1440
goto out ;
1441
1441
}
1442
1442
}
@@ -1504,13 +1504,6 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1504
1504
goto out ;
1505
1505
}
1506
1506
1507
- if (cache -> zone_is_active ) {
1508
- btrfs_get_block_group (cache );
1509
- spin_lock (& fs_info -> zone_active_bgs_lock );
1510
- list_add_tail (& cache -> active_bg_list , & fs_info -> zone_active_bgs );
1511
- spin_unlock (& fs_info -> zone_active_bgs_lock );
1512
- }
1513
-
1514
1507
out :
1515
1508
if (cache -> alloc_offset > fs_info -> zone_size ) {
1516
1509
btrfs_err (fs_info ,
@@ -1535,10 +1528,16 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1535
1528
ret = - EIO ;
1536
1529
}
1537
1530
1538
- if (!ret )
1531
+ if (!ret ) {
1539
1532
cache -> meta_write_pointer = cache -> alloc_offset + cache -> start ;
1540
-
1541
- if (ret ) {
1533
+ if (cache -> zone_is_active ) {
1534
+ btrfs_get_block_group (cache );
1535
+ spin_lock (& fs_info -> zone_active_bgs_lock );
1536
+ list_add_tail (& cache -> active_bg_list ,
1537
+ & fs_info -> zone_active_bgs );
1538
+ spin_unlock (& fs_info -> zone_active_bgs_lock );
1539
+ }
1540
+ } else {
1542
1541
kfree (cache -> physical_map );
1543
1542
cache -> physical_map = NULL ;
1544
1543
}
0 commit comments