Search code examples
linuxbtrfs

btrfs ioctl: get file checksums from userspace


I would like to obtain the BTRFS checksums related to the specific file, but unfortunately I have not found appropriate ioctl to perform this action. Is it possible to do? If so, how to do that? I need stored checksums to try to reduce CPU load in cases similar to rsync behaviour.


Solution

  • Just now pushed this messy code into my github repo. https://github.com/Lakshmipathi/btrfs-progs/tree/dump_csum Its not official code. I tested for files between size 100K to 50GB. They seem to match.

    Usage:

    ./btrfs-debug-tree -f /path/to/file /btrfs/partition
    

    will create csumdump file at destination.

    Example:

    sudo ./btrfs-debug-tree -f /btrfs/50gbfile1 /dev/sda4
    

    will create an output file-named '/btrfs/50gbfile1.csumdump' with csum of file blocks.

    Note: I was trying this out for educational/learning purpose,so it comes with all usual disclaimers. Planning to cleanup this code sometime this week.

    If you plan to use, I would recommend you to test with following cases:

    1) Create 20GB (or any file with size > 1KB) on /tmp/ 
    2) mount your btrfs partition on /btrfs and copy file /tmp/file /btrfs/f1
    3) Now dump the csum it will produce /btrfs/f1.csumdump
    4) cp /tmp/file /btrfs/f2 and dump f2's csum.
    5) Now compare f1.csumdump with f2.csumdump If they match, it seems to be 
    working. If they didn't match something went wrong. 
    

    Update after almost 5 years!

    New location: https://github.com/Lakshmipathi/dduper