• Filipe Manana's avatar
    Btrfs: fix data corruption when deduplicating between different files · e9773e97
    Filipe Manana authored
    commit de02b9f6 upstream.
    
    If we deduplicate extents between two different files we can end up
    corrupting data if the source range ends at the size of the source file,
    the source file's size is not aligned to the filesystem's block size
    and the destination range does not go past the size of the destination
    file size.
    
    Example:
    
      $ mkfs.btrfs -f /dev/sdb
      $ mount /dev/sdb /mnt
    
      $ xfs_io -f -c "pwrite -S 0x6b 0 2518890" /mnt/foo
      # The first byte with a value of 0xae starts at an offset (2518890)
      # which is not a multiple of the sector size.
      $ xfs_io -c "pwrite -S 0xae 2518890 102398" /mnt/foo
    
      # Confirm the file content is full of bytes with values 0x6b and 0xae.
      $ od -t x1 /mnt/foo
      0000000 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
      *
      11467540 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b ae ae ae ae ae ae
      11467560 ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae
      *
      11777540 ae ae ae ae ae ae ae ae
      11777550
    
      # Create a second file with a length not aligned to the sector size,
      # whose bytes all have the value 0x6b, so that its extent(s) can be
      # deduplicated with the first file.
      $ xfs_io -f -c "pwrite -S 0x6b 0 557771" /mnt/bar
    
      # Now deduplicate the entire second file into a range of the first file
      # that also has all bytes with the value 0x6b. The destination range's
      # end offset must not be aligned to the sector size and must be less
      # then the offset of the first byte with the value 0xae (byte at offset
      # 2518890).
      $ xfs_io -c "dedupe /mnt/bar 0 1957888 557771" /mnt/foo
    
      # The bytes in the range starting at offset 2515659 (end of the
      # deduplication range) and ending at offset 2519040 (start offset
      # rounded up to the block size) must all have the value 0xae (and not
      # replaced with 0x00 values). In other words, we should have exactly
      # the same data we had before we asked for deduplication.
      $ od -t x1 /mnt/foo
      0000000 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
      *
      11467540 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b ae ae ae ae ae ae
      11467560 ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae
      *
      11777540 ae ae ae ae ae ae ae ae
      11777550
    
      # Unmount the filesystem and mount it again. This guarantees any file
      # data in the page cache is dropped.
      $ umount /dev/sdb
      $ mount /dev/sdb /mnt
    
      $ od -t x1 /mnt/foo
      0000000 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
      *
      11461300 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 00 00 00 00 00
      11461320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
      *
      11470000 ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae ae
      *
      11777540 ae ae ae ae ae ae ae ae
      11777550
    
      # The bytes in range 2515659 to 2519040 have a value of 0x00 and not a
      # value of 0xae, data corruption happened due to the deduplication
      # operation.
    
    So fix this by rounding down, to the sector size, the length used for the
    deduplication when the following conditions are met:
    
      1) Source file's range ends at its i_size;
      2) Source file's i_size is not aligned to the sector size;
      3) Destination range does not cross the i_size of the destination file.
    
    Fixes: e1d227a4 ("btrfs: Handle unaligned length in extent_same")
    CC: stable@vger.kernel.org # 4.2+
    Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
    Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    e9773e97
Name
Last commit
Last update
..
tests Loading commit data...
Kconfig Loading commit data...
Makefile Loading commit data...
acl.c Loading commit data...
async-thread.c Loading commit data...
async-thread.h Loading commit data...
backref.c Loading commit data...
backref.h Loading commit data...
btrfs_inode.h Loading commit data...
check-integrity.c Loading commit data...
check-integrity.h Loading commit data...
compression.c Loading commit data...
compression.h Loading commit data...
ctree.c Loading commit data...
ctree.h Loading commit data...
dedupe.h Loading commit data...
delayed-inode.c Loading commit data...
delayed-inode.h Loading commit data...
delayed-ref.c Loading commit data...
delayed-ref.h Loading commit data...
dev-replace.c Loading commit data...
dev-replace.h Loading commit data...
dir-item.c Loading commit data...
disk-io.c Loading commit data...
disk-io.h Loading commit data...
export.c Loading commit data...
export.h Loading commit data...
extent-tree.c Loading commit data...
extent_io.c Loading commit data...
extent_io.h Loading commit data...
extent_map.c Loading commit data...
extent_map.h Loading commit data...
file-item.c Loading commit data...
file.c Loading commit data...
free-space-cache.c Loading commit data...
free-space-cache.h Loading commit data...
free-space-tree.c Loading commit data...
free-space-tree.h Loading commit data...
inode-item.c Loading commit data...
inode-map.c Loading commit data...
inode-map.h Loading commit data...
inode.c Loading commit data...
ioctl.c Loading commit data...
locking.c Loading commit data...
locking.h Loading commit data...
lzo.c Loading commit data...
math.h Loading commit data...
ordered-data.c Loading commit data...
ordered-data.h Loading commit data...
orphan.c Loading commit data...
print-tree.c Loading commit data...
print-tree.h Loading commit data...
props.c Loading commit data...
props.h Loading commit data...
qgroup.c Loading commit data...
qgroup.h Loading commit data...
raid56.c Loading commit data...
raid56.h Loading commit data...
rcu-string.h Loading commit data...
reada.c Loading commit data...
ref-verify.c Loading commit data...
ref-verify.h Loading commit data...
relocation.c Loading commit data...
root-tree.c Loading commit data...
scrub.c Loading commit data...
send.c Loading commit data...
send.h Loading commit data...
struct-funcs.c Loading commit data...
super.c Loading commit data...
sysfs.c Loading commit data...
sysfs.h Loading commit data...
transaction.c Loading commit data...
transaction.h Loading commit data...
tree-checker.c Loading commit data...
tree-checker.h Loading commit data...
tree-defrag.c Loading commit data...
tree-log.c Loading commit data...
tree-log.h Loading commit data...
ulist.c Loading commit data...
ulist.h Loading commit data...
uuid-tree.c Loading commit data...
volumes.c Loading commit data...
volumes.h Loading commit data...
xattr.c Loading commit data...
xattr.h Loading commit data...
zlib.c Loading commit data...
zstd.c Loading commit data...