generic: convert tests to SPDX license tags
[xfstests-dev.git] / tests / generic / 137
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
4 #
5 # FS QA Test No. 137
6 #
7 # Ensure that we can reflink and dedupe blocks within the same file...
8 #   - Create a file with three distinct blocks ABB
9 #   - Reflink block zero to the multiple-of-three blocks
10 #   - Reflink block one to the multiple-of-five blocks
11 #   - Dedupe block two to the multiple-of-seven blocks
12 #   - Check that we successfully avoid deduping with holes, unwritten
13 #     extents, and non-matches; but actually dedupe real matches.
14 #
15 seq=`basename $0`
16 seqres=$RESULT_DIR/$seq
17 echo "QA output created by $seq"
18
19 here=`pwd`
20 tmp=/tmp/$$
21 status=1    # failure is the default!
22 trap "_cleanup; exit \$status" 0 1 2 3 15
23
24 _cleanup()
25 {
26     cd /
27     rm -rf $tmp.* $testdir
28 }
29
30 # get standard environment, filters and checks
31 . ./common/rc
32 . ./common/filter
33 . ./common/reflink
34
35 # real QA test starts here
36 _supported_os Linux
37 _require_test_reflink
38 _require_test_dedupe
39 _require_xfs_io_command "falloc"
40
41 rm -f $seqres.full
42
43 testdir=$TEST_DIR/test-$seq
44 rm -rf $testdir
45 mkdir $testdir
46
47 echo "Create the original file blocks"
48 blksz=65536
49 _pwrite_byte 0x61 0 $blksz $testdir/file1 >> $seqres.full
50 _pwrite_byte 0x62 $blksz $((blksz * 2)) $testdir/file1 >> $seqres.full
51
52 nr_blks=1024
53
54 echo "fallocate half the file"
55 $XFS_IO_PROG -f -c "falloc $((nr_blks * blksz / 2)) $((nr_blks * blksz / 2))" $testdir/file1 >> $seqres.full
56
57 echo "Reflink block zero to the threes"
58 seq 1 $((nr_blks / 3)) | while read nr; do
59         _reflink_range $testdir/file1 0 $testdir/file1 $((nr * 3 * blksz)) \
60                         $blksz >> $seqres.full
61 done
62
63 echo "Reflink block one to the fives"
64 seq 1 $((nr_blks / 5)) | while read nr; do
65         _reflink_range $testdir/file1 $blksz $testdir/file1 \
66                         $((nr * 5 * blksz)) $blksz >> $seqres.full
67 done
68
69 echo "Dedupe block two to the sevens"
70 seq 1 $((nr_blks / 7)) | while read nr; do
71         _dedupe_range $testdir/file1 $((blksz * 2)) $testdir/file1 \
72                         $((nr * 7 * blksz)) $blksz >> $seqres.full 2>&1
73 done
74
75 _test_cycle_mount
76
77 echo "Check block mappings"
78 md5sum $testdir/file1 | _filter_test_dir
79
80 crcZ=$(_md5_range_checksum /dev/zero 0 $blksz)
81 crc0=$(_md5_range_checksum $testdir/file1 0 $blksz)
82 crc1=$(_md5_range_checksum $testdir/file1 $blksz $blksz)
83 crc2=$(_md5_range_checksum $testdir/file1 $((blksz * 2)) $blksz)
84
85 check_block() {
86         lblk=$1
87         rem7=$((lblk % 7))
88         rem5=$((lblk % 5))
89         rem3=$((lblk % 3))
90
91         crc=$(_md5_range_checksum $testdir/file1 $((lblk * blksz)) $blksz)
92
93         if [ $rem7 -eq 0 ]; then
94                 if [ $rem5 -eq 0 ]; then
95                         test $crc2 = $crc || echo "lblk $lblk doesn't match block 2"
96                 elif [ $rem3 -eq 0 ]; then
97                         test $crc0 = $crc || echo "lblk $lblk doesn't match block 0"
98                 elif [ $lblk -lt $((nr_blks / 2)) ]; then
99                         test $crcZ = $crc || echo "lblk $lblk isn't zeroed"
100                 fi
101         elif [ $rem5 -eq 0 ]; then
102                 test $crc1 = $crc || echo "lblk $lblk doesn't match block 1"
103         elif [ $rem3 -eq 0 ]; then
104                 test $crc0 = $crc || echo "lblk $lblk doesn't match block 0"
105         elif [ $lblk -lt $((nr_blks / 2)) ]; then
106                 test $crcZ = $crc || echo "lblk $lblk isn't zeroed"
107         fi
108 }
109
110 seq 3 $((nr_blks - 1)) | while read lblk; do
111         err="$(check_block $lblk)"
112         test -n "$err" && echo "$lblk: $err"
113 done
114
115 # success, all done
116 status=0
117 exit