• Qu Wenruo's avatar
    btrfs: don't use btrfs_chunk::sub_stripes from disk · 76a66ba1
    Qu Wenruo authored
    [BUG]
    There are two reports (the earliest one from LKP, a more recent one from
    kernel bugzilla) that we can have some chunks with 0 as sub_stripes.
    
    This will cause divide-by-zero errors at btrfs_rmap_block, which is
    introduced by a recent kernel patch ac067734 ("btrfs: merge
    calculations for simple striped profiles in btrfs_rmap_block"):
    
    		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
    				 BTRFS_BLOCK_GROUP_RAID10)) {
    			stripe_nr = stripe_nr * map->num_stripes + i;
    			stripe_nr = div_u64(stripe_nr, map->sub_stripes); <<<
    		}
    
    [CAUSE]
    From the more recent report, it has been proven that we have some chunks
    with 0 as sub_stripes, mostly caused by older mkfs.
    
    It turns out that the mkfs.btrfs fix is only introduced in 6718ab4d33aa
    ("btrfs-progs: Initialize sub_stripes to 1 in btrfs_alloc_data_chunk")
    which is included in v5.4 btrfs-progs release.
    
    So there would be quite some old filesystems with such 0 sub_stripes.
    
    [FIX]
    Just don't trust the sub_stripes values from disk.
    
    We have a trusted btrfs_raid_array[] to fetch the correct sub_stripes
    numbers for each profile and that are fixed.
    
    By this, we can keep the compatibility with older filesystems while
    still avoid divide-by-zero bugs.
    Reported-by: default avatarkernel test robot <oliver.sang@intel.com>
    Reported-by: default avatarViktor Kuzmin <kvaster@gmail.com>
    Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216559
    Fixes: ac067734 ("btrfs: merge calculations for simple striped profiles in btrfs_rmap_block")
    CC: stable@vger.kernel.org # 6.0
    Reviewed-by: default avatarSu Yue <glass@fydeos.io>
    Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
    Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    76a66ba1
volumes.c 226 KB