• Qu Wenruo's avatar
    btrfs: tree-checker: add dev extent item checks · 008e2512
    Qu Wenruo authored
    [REPORT]
    There is a corruption report that btrfs refused to mount a fs that has
    overlapping dev extents:
    
      BTRFS error (device sdc): dev extent devid 4 physical offset 14263979671552 overlap with previous dev extent end 14263980982272
      BTRFS error (device sdc): failed to verify dev extents against chunks: -117
      BTRFS error (device sdc): open_ctree failed
    
    [CAUSE]
    The direct cause is very obvious, there is a bad dev extent item with
    incorrect length.
    
    With btrfs check reporting two overlapping extents, the second one shows
    some clue on the cause:
    
      ERROR: dev extent devid 4 offset 14263979671552 len 6488064 overlap with previous dev extent end 14263980982272
      ERROR: dev extent devid 13 offset 2257707008000 len 6488064 overlap with previous dev extent end 2257707270144
      ERROR: errors found in extent allocation tree or chunk allocation
    
    The second one looks like a bitflip happened during new chunk
    allocation:
    hex(2257707008000) = 0x20da9d30000
    hex(2257707270144) = 0x20da9d70000
    diff               = 0x00000040000
    
    So it looks like a bitflip happened during new dev extent allocation,
    resulting the second overlap.
    
    Currently we only do the dev-extent verification at mount time, but if the
    corruption is caused by memory bitflip, we really want to catch it before
    writing the corruption to the storage.
    
    Furthermore the dev extent items has the following key definition:
    
    	(<device id> DEV_EXTENT <physical offset>)
    
    Thus we can not just rely on the generic key order check to make sure
    there is no overlapping.
    
    [ENHANCEMENT]
    Introduce dedicated dev extent checks, including:
    
    - Fixed member checks
      * chunk_tree should always be BTRFS_CHUNK_TREE_OBJECTID (3)
      * chunk_objectid should always be
        BTRFS_FIRST_CHUNK_CHUNK_TREE_OBJECTID (256)
    
    - Alignment checks
      * chunk_offset should be aligned to sectorsize
      * length should be aligned to sectorsize
      * key.offset should be aligned to sectorsize
    
    - Overlap checks
      If the previous key is also a dev-extent item, with the same
      device id, make sure we do not overlap with the previous dev extent.
    
    Reported: Stefan N <stefannnau@gmail.com>
    Link: https://lore.kernel.org/linux-btrfs/CA+W5K0rSO3koYTo=nzxxTm1-Pdu1HYgVxEpgJ=aGc7d=E8mGEg@mail.gmail.com/
    CC: stable@vger.kernel.org # 5.10+
    Reviewed-by: default avatarAnand Jain <anand.jain@oracle.com>
    Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
    Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    008e2512
tree-checker.c 68.2 KB