@@ -1317,6 +1317,7 @@ static int btrfs_load_zone_info(struct btrfs_fs_info *fs_info, int zone_idx,
13171317 if (!btrfs_dev_is_sequential (device , info -> physical )) {
13181318 up_read (& dev_replace -> rwsem );
13191319 info -> alloc_offset = WP_CONVENTIONAL ;
1320+ info -> capacity = device -> zone_info -> zone_size ;
13201321 return 0 ;
13211322 }
13221323
@@ -1522,35 +1523,35 @@ static int btrfs_load_block_group_raid0(struct btrfs_block_group *bg,
15221523 u64 last_alloc )
15231524{
15241525 struct btrfs_fs_info * fs_info = bg -> fs_info ;
1526+ u64 stripe_nr = 0 , stripe_offset = 0 ;
1527+ u32 stripe_index = 0 ;
15251528
15261529 if ((map -> type & BTRFS_BLOCK_GROUP_DATA ) && !fs_info -> stripe_root ) {
15271530 btrfs_err (fs_info , "zoned: data %s needs raid-stripe-tree" ,
15281531 btrfs_bg_type_to_raid_name (map -> type ));
15291532 return - EINVAL ;
15301533 }
15311534
1535+ if (last_alloc ) {
1536+ u32 factor = map -> num_stripes ;
1537+
1538+ stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT ;
1539+ stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK ;
1540+ stripe_nr = div_u64_rem (stripe_nr , factor , & stripe_index );
1541+ }
1542+
15321543 for (int i = 0 ; i < map -> num_stripes ; i ++ ) {
15331544 if (zone_info [i ].alloc_offset == WP_MISSING_DEV )
15341545 continue ;
15351546
15361547 if (zone_info [i ].alloc_offset == WP_CONVENTIONAL ) {
1537- u64 stripe_nr , full_stripe_nr ;
1538- u64 stripe_offset ;
1539- int stripe_index ;
15401548
1541- stripe_nr = div64_u64 (last_alloc , map -> stripe_size );
1542- stripe_offset = stripe_nr * map -> stripe_size ;
1543- full_stripe_nr = div_u64 (stripe_nr , map -> num_stripes );
1544- div_u64_rem (stripe_nr , map -> num_stripes , & stripe_index );
1545-
1546- zone_info [i ].alloc_offset =
1547- full_stripe_nr * map -> stripe_size ;
1549+ zone_info [i ].alloc_offset = btrfs_stripe_nr_to_offset (stripe_nr );
15481550
15491551 if (stripe_index > i )
1550- zone_info [i ].alloc_offset += map -> stripe_size ;
1552+ zone_info [i ].alloc_offset += BTRFS_STRIPE_LEN ;
15511553 else if (stripe_index == i )
1552- zone_info [i ].alloc_offset +=
1553- (last_alloc - stripe_offset );
1554+ zone_info [i ].alloc_offset += stripe_offset ;
15541555 }
15551556
15561557 if (test_bit (0 , active ) != test_bit (i , active )) {
@@ -1574,13 +1575,23 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
15741575 u64 last_alloc )
15751576{
15761577 struct btrfs_fs_info * fs_info = bg -> fs_info ;
1578+ u64 stripe_nr = 0 , stripe_offset = 0 ;
1579+ u32 stripe_index = 0 ;
15771580
15781581 if ((map -> type & BTRFS_BLOCK_GROUP_DATA ) && !fs_info -> stripe_root ) {
15791582 btrfs_err (fs_info , "zoned: data %s needs raid-stripe-tree" ,
15801583 btrfs_bg_type_to_raid_name (map -> type ));
15811584 return - EINVAL ;
15821585 }
15831586
1587+ if (last_alloc ) {
1588+ u32 factor = map -> num_stripes / map -> sub_stripes ;
1589+
1590+ stripe_nr = last_alloc >> BTRFS_STRIPE_LEN_SHIFT ;
1591+ stripe_offset = last_alloc & BTRFS_STRIPE_LEN_MASK ;
1592+ stripe_nr = div_u64_rem (stripe_nr , factor , & stripe_index );
1593+ }
1594+
15841595 for (int i = 0 ; i < map -> num_stripes ; i ++ ) {
15851596 if (zone_info [i ].alloc_offset == WP_MISSING_DEV )
15861597 continue ;
@@ -1594,26 +1605,12 @@ static int btrfs_load_block_group_raid10(struct btrfs_block_group *bg,
15941605 }
15951606
15961607 if (zone_info [i ].alloc_offset == WP_CONVENTIONAL ) {
1597- u64 stripe_nr , full_stripe_nr ;
1598- u64 stripe_offset ;
1599- int stripe_index ;
1600-
1601- stripe_nr = div64_u64 (last_alloc , map -> stripe_size );
1602- stripe_offset = stripe_nr * map -> stripe_size ;
1603- full_stripe_nr = div_u64 (stripe_nr ,
1604- map -> num_stripes / map -> sub_stripes );
1605- div_u64_rem (stripe_nr ,
1606- (map -> num_stripes / map -> sub_stripes ),
1607- & stripe_index );
1608-
1609- zone_info [i ].alloc_offset =
1610- full_stripe_nr * map -> stripe_size ;
1608+ zone_info [i ].alloc_offset = btrfs_stripe_nr_to_offset (stripe_nr );
16111609
16121610 if (stripe_index > (i / map -> sub_stripes ))
1613- zone_info [i ].alloc_offset += map -> stripe_size ;
1611+ zone_info [i ].alloc_offset += BTRFS_STRIPE_LEN ;
16141612 else if (stripe_index == (i / map -> sub_stripes ))
1615- zone_info [i ].alloc_offset +=
1616- (last_alloc - stripe_offset );
1613+ zone_info [i ].alloc_offset += stripe_offset ;
16171614 }
16181615
16191616 if ((i % map -> sub_stripes ) == 0 ) {
@@ -1683,8 +1680,6 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
16831680 set_bit (BLOCK_GROUP_FLAG_SEQUENTIAL_ZONE , & cache -> runtime_flags );
16841681
16851682 if (num_conventional > 0 ) {
1686- /* Zone capacity is always zone size in emulation */
1687- cache -> zone_capacity = cache -> length ;
16881683 ret = calculate_alloc_pointer (cache , & last_alloc , new );
16891684 if (ret ) {
16901685 btrfs_err (fs_info ,
@@ -1693,6 +1688,7 @@ int btrfs_load_block_group_zone_info(struct btrfs_block_group *cache, bool new)
16931688 goto out ;
16941689 } else if (map -> num_stripes == num_conventional ) {
16951690 cache -> alloc_offset = last_alloc ;
1691+ cache -> zone_capacity = cache -> length ;
16961692 set_bit (BLOCK_GROUP_FLAG_ZONE_IS_ACTIVE , & cache -> runtime_flags );
16971693 goto out ;
16981694 }
0 commit comments