@@ -421,10 +421,19 @@ int btrfs_get_dev_zone_info(struct btrfs_device *device, bool populate_cache)
421
421
* since btrfs adds the pages one by one to a bio, and btrfs cannot
422
422
* increase the metadata reservation even if it increases the number of
423
423
* extents, it is safe to stick with the limit.
424
+ *
425
+ * With the zoned emulation, we can have non-zoned device on the zoned
426
+ * mode. In this case, we don't have a valid max zone append size. So,
427
+ * use max_segments * PAGE_SIZE as the pseudo max_zone_append_size.
424
428
*/
425
- zone_info -> max_zone_append_size =
426
- min_t (u64 , (u64 )bdev_max_zone_append_sectors (bdev ) << SECTOR_SHIFT ,
427
- (u64 )bdev_max_segments (bdev ) << PAGE_SHIFT );
429
+ if (bdev_is_zoned (bdev )) {
430
+ zone_info -> max_zone_append_size = min_t (u64 ,
431
+ (u64 )bdev_max_zone_append_sectors (bdev ) << SECTOR_SHIFT ,
432
+ (u64 )bdev_max_segments (bdev ) << PAGE_SHIFT );
433
+ } else {
434
+ zone_info -> max_zone_append_size =
435
+ (u64 )bdev_max_segments (bdev ) << PAGE_SHIFT ;
436
+ }
428
437
if (!IS_ALIGNED (nr_sectors , zone_sectors ))
429
438
zone_info -> nr_zones ++ ;
430
439
@@ -1178,7 +1187,7 @@ int btrfs_ensure_empty_zones(struct btrfs_device *device, u64 start, u64 size)
1178
1187
* offset.
1179
1188
*/
1180
1189
static int calculate_alloc_pointer (struct btrfs_block_group * cache ,
1181
- u64 * offset_ret )
1190
+ u64 * offset_ret , bool new )
1182
1191
{
1183
1192
struct btrfs_fs_info * fs_info = cache -> fs_info ;
1184
1193
struct btrfs_root * root ;
@@ -1188,6 +1197,21 @@ static int calculate_alloc_pointer(struct btrfs_block_group *cache,
1188
1197
int ret ;
1189
1198
u64 length ;
1190
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
+
1191
1215
path = btrfs_alloc_path ();
1192
1216
if (!path )
1193
1217
return - ENOMEM ;
@@ -1323,6 +1347,13 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1323
1347
else
1324
1348
num_conventional ++ ;
1325
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
+
1326
1357
if (!is_sequential ) {
1327
1358
alloc_offsets [i ] = WP_CONVENTIONAL ;
1328
1359
continue ;
@@ -1389,45 +1420,23 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1389
1420
__set_bit (i , active );
1390
1421
break ;
1391
1422
}
1392
-
1393
- /*
1394
- * Consider a zone as active if we can allow any number of
1395
- * active zones.
1396
- */
1397
- if (!device -> zone_info -> max_active_zones )
1398
- __set_bit (i , active );
1399
1423
}
1400
1424
1401
1425
if (num_sequential > 0 )
1402
1426
cache -> seq_zone = true;
1403
1427
1404
1428
if (num_conventional > 0 ) {
1405
- /*
1406
- * Avoid calling calculate_alloc_pointer() for new BG. It
1407
- * is no use for new BG. It must be always 0.
1408
- *
1409
- * Also, we have a lock chain of extent buffer lock ->
1410
- * chunk mutex. For new BG, this function is called from
1411
- * btrfs_make_block_group() which is already taking the
1412
- * chunk mutex. Thus, we cannot call
1413
- * calculate_alloc_pointer() which takes extent buffer
1414
- * locks to avoid deadlock.
1415
- */
1416
-
1417
1429
/* Zone capacity is always zone size in emulation */
1418
1430
cache -> zone_capacity = cache -> length ;
1419
- if (new ) {
1420
- cache -> alloc_offset = 0 ;
1421
- goto out ;
1422
- }
1423
- ret = calculate_alloc_pointer (cache , & last_alloc );
1424
- if (ret || map -> num_stripes == num_conventional ) {
1425
- if (!ret )
1426
- cache -> alloc_offset = last_alloc ;
1427
- else
1428
- btrfs_err (fs_info ,
1431
+ ret = calculate_alloc_pointer (cache , & last_alloc , new );
1432
+ if (ret ) {
1433
+ btrfs_err (fs_info ,
1429
1434
"zoned: failed to determine allocation offset of bg %llu" ,
1430
- 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 ;
1431
1440
goto out ;
1432
1441
}
1433
1442
}
@@ -1495,13 +1504,6 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1495
1504
goto out ;
1496
1505
}
1497
1506
1498
- if (cache -> zone_is_active ) {
1499
- btrfs_get_block_group (cache );
1500
- spin_lock (& fs_info -> zone_active_bgs_lock );
1501
- list_add_tail (& cache -> active_bg_list , & fs_info -> zone_active_bgs );
1502
- spin_unlock (& fs_info -> zone_active_bgs_lock );
1503
- }
1504
-
1505
1507
out :
1506
1508
if (cache -> alloc_offset > fs_info -> zone_size ) {
1507
1509
btrfs_err (fs_info ,
@@ -1526,10 +1528,16 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
1526
1528
ret = - EIO ;
1527
1529
}
1528
1530
1529
- if (!ret )
1531
+ if (!ret ) {
1530
1532
cache -> meta_write_pointer = cache -> alloc_offset + cache -> start ;
1531
-
1532
- 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 {
1533
1541
kfree (cache -> physical_map );
1534
1542
cache -> physical_map = NULL ;
1535
1543
}
@@ -2007,8 +2015,7 @@ static int do_zone_finish(struct btrfs_block_group *block_group, bool fully_writ
2007
2015
/* For active_bg_list */
2008
2016
btrfs_put_block_group (block_group );
2009
2017
2010
- clear_bit (BTRFS_FS_NEED_ZONE_FINISH , & fs_info -> flags );
2011
- wake_up_all (& fs_info -> zone_finish_wait );
2018
+ clear_and_wake_up_bit (BTRFS_FS_NEED_ZONE_FINISH , & fs_info -> flags );
2012
2019
2013
2020
return 0 ;
2014
2021
}
0 commit comments