Re: [PATCH] btrfs: add test for BTRFS_IOC_GET_CSUMS ioctl
Qu Wenruo <[email protected]>
| Newsgroups | org.kernel.vger.fstests,org.kernel.vger.linux-btrfs |
|---|---|
| Message-ID | <[email protected]> |
在 2026/3/20 23:22, Mark Harmstone 写道:
> Test the new BTRFS_IOC_GET_CSUMS ioctl which retrieves file checksums
> from the kernel's csum tree. The test covers:
I think it's a great idea to have a dedicated test case for the new ioctl.
Although that ioctl has some changes and a refresh will be appriciated.
>
> 1. Regular data extents returning HAS_CSUMS with checksum data
Maybe also test an inlined extent?
> 2. Holes returning SPARSE entries
> 3. Preallocated extents returning SPARSE entries
> 4. Buffer continuation (small buffer forcing multiple calls)
> 5. Checksum consistency (identical data produces identical csums)
> 6. Querying past file data returning SPARSE
I guess we may also want some corner cases related to the last block:
- The file size is not aligned
* Test get csum of the last block
Which should return the csum of the last full block.
* Test get csum beyond the last block
Which should return ZEROED.
- The file size is aligned, but the last extent is truncated
E.g. the file size is 4K, but the data extent is 16K (e.g. reflinked)
* Test get csum of the [4K, 8K)
Which should return ZEROED
> 7. Precomputed values
>
> Signed-off-by: Mark Harmstone <[email protected]>
[...]
> +
> +# Test 7: Verify checksum values against precomputed values for all algorithms.
> +# Uses explicit -s 4096 as the expected values are precomputed for 4096-byte
> +# sectors. Write three sectors: all 0x00, all 0xff, all 0xaa.
> +echo "=== Test 7: precomputed csums ==="
One idea to get rid of the pre-calculated csum is, to read out the data
block by block from the file (as long as the file size is aligned to
block size), then calculate the csum.
For xxhash/sha256/blake2, the checksum can be calculated in user space
and should match the one in csum tree.
It's only crc32c that we do not have a handy tool in user-space.
Thanks,
Qu
> +_scratch_unmount
> +for csum_algo in crc32c xxhash sha256 blake2; do
> + echo "--- $csum_algo ---"
> +
> + _scratch_mkfs -s 4096 --csum $csum_algo >> $seqres.full 2>&1
> + _scratch_mount
> +
> + case "$csum_algo" in
> + crc32c)
> + expect_00="98f94189"
> + expect_ff="25c1fe13"
> + expect_aa="4ed66b65"
> + ;;
> + xxhash)
> + expect_00="ac869b6f32d8bbdb"
> + expect_ff="10af2cb94282321f"
> + expect_aa="e35ac2d66625ceaa"
> + ;;
> + sha256)
> + expect_00="ad7facb2586fc6e966c004d7d1d16b024f5805ff7cb47c7a85dabd8b48892ca7"
> + expect_ff="f47a8ec3e9aff2318d896942282ad4fe37d6391c82914f54a5da8a37de1300c6"
> + expect_aa="c622005493c4cb75f3e08eda4cc0bfe172e2c5eeca661ec4908c5490fc3d6994"
> + ;;
> + blake2)
> + expect_00="686ede9288c391e7e05026e56f2f91bfd879987a040ea98445dabc76f55b8e5f"
> + expect_ff="1e23d2944a4523734ef9c8b536eed668fa8a99b272bfc10988864e1ef135197d"
> + expect_aa="8337334ce71745681a24e9aa409fdf85fe4081242c39815ccf266df6d33355a2"
> + ;;
> + esac
> +
> + $XFS_IO_PROG -f \
> + -c "pwrite -S 0x00 0 4096" \
> + -c "pwrite -S 0xff 4096 4096" \
> + -c "pwrite -S 0xaa 8192 4096" \
> + $SCRATCH_MNT/file7 >> $seqres.full
> + sync
> +
> + output=$($GET_CSUMS $SCRATCH_MNT/file7 0 12288)
> + echo "$output" >> $seqres.full
> +
> + got_csums=$(echo "$output" | grep "^HAS_CSUMS" | \
> + awk '{for(i=4;i<=NF;i++) printf $i " "; print ""}')
> + got_00=$(echo $got_csums | awk '{print $1}')
> + got_ff=$(echo $got_csums | awk '{print $2}')
> + got_aa=$(echo $got_csums | awk '{print $3}')
> +
> + pass=true
> + if [ "$got_00" != "$expect_00" ]; then
> + echo "FAIL: 0x00 sector csum $got_00 != $expect_00"
> + pass=false
> + fi
> + if [ "$got_ff" != "$expect_ff" ]; then
> + echo "FAIL: 0xff sector csum $got_ff != $expect_ff"
> + pass=false
> + fi
> + if [ "$got_aa" != "$expect_aa" ]; then
> + echo "FAIL: 0xaa sector csum $got_aa != $expect_aa"
> + pass=false
> + fi
> + $pass && echo "csums verified"
> +
> + _scratch_unmount
> +done
> +
> +echo "=== done ==="
> +status=0
> +exit
> diff --git a/tests/btrfs/343.out b/tests/btrfs/343.out
> new file mode 100644
> index 00000000..85582c24
> --- /dev/null
> +++ b/tests/btrfs/343.out
> @@ -0,0 +1,33 @@
> +QA output created by 343
> +=== Test 1: regular data ===
> +HAS_CSUMS
> +has csums
> +complete
> +=== Test 2: hole ===
> +HAS_CSUMS
> +SPARSE
> +HAS_CSUMS
> +complete
> +=== Test 3: prealloc ===
> +SPARSE
> +complete
> +=== Test 4: continuation ===
> +SPARSE
> +incomplete
> +HAS_CSUMS
> +complete
> +=== Test 5: csum consistency ===
> +csums match
> +=== Test 6: past data ===
> +SPARSE
> +complete
> +=== Test 7: precomputed csums ===
> +--- crc32c ---
> +csums verified
> +--- xxhash ---
> +csums verified
> +--- sha256 ---
> +csums verified
> +--- blake2 ---
> +csums verified
> +=== done ===