reflink: basic tests of the reflink and dedupe ioctls
[xfstests-dev.git] / tests / generic / 137
1 #! /bin/bash
2 # FS QA Test No. 137
3 #
4 # Ensure that we can reflink and dedupe blocks within the same file...
5 #   - Create a file with three distinct blocks ABB
6 #   - Reflink block zero to the multiple-of-three blocks
7 #   - Reflink block one to the multiple-of-five blocks
8 #   - Dedupe block two to the multiple-of-seven blocks
9 #   - Check that we successfully avoid deduping with holes, unwritten
10 #     extents, and non-matches; but actually dedupe real matches.
11 #
12 #-----------------------------------------------------------------------
13 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
14 #
15 # This program is free software; you can redistribute it and/or
16 # modify it under the terms of the GNU General Public License as
17 # published by the Free Software Foundation.
18 #
19 # This program is distributed in the hope that it would be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write the Free Software Foundation,
26 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
27 #-----------------------------------------------------------------------
28
29 seq=`basename "$0"`
30 seqres="$RESULT_DIR/$seq"
31 echo "QA output created by $seq"
32
33 here=`pwd`
34 tmp=/tmp/$$
35 status=1    # failure is the default!
36 trap "_cleanup; exit \$status" 0 1 2 3 15
37
38 _cleanup()
39 {
40     cd /
41     rm -rf "$tmp".* "$TESTDIR"
42 }
43
44 # get standard environment, filters and checks
45 . ./common/rc
46 . ./common/filter
47 . ./common/reflink
48
49 # real QA test starts here
50 _supported_os Linux
51 _require_test_reflink
52 _require_test_dedupe
53 _require_xfs_io_command "falloc"
54
55 rm -f "$seqres.full"
56
57 TESTDIR="$TEST_DIR/test-$seq"
58 rm -rf "$TESTDIR"
59 mkdir "$TESTDIR"
60
61 echo "Create the original file blocks"
62 BLKSZ=65536
63 _pwrite_byte 0x61 0 $BLKSZ "$TESTDIR/file1" >> "$seqres.full"
64 _pwrite_byte 0x62 $BLKSZ $((BLKSZ * 2)) "$TESTDIR/file1" >> "$seqres.full"
65
66 NR_BLKS=1024
67
68 echo "fallocate half the file"
69 "$XFS_IO_PROG" -f -c "falloc $((NR_BLKS * BLKSZ / 2)) $((NR_BLKS * BLKSZ / 2))" "$TESTDIR/file1" >> "$seqres.full"
70
71 echo "Reflink block zero to the threes"
72 seq 1 $((NR_BLKS / 3)) | while read nr; do
73         _reflink_range "$TESTDIR/file1" 0 "$TESTDIR/file1" $((nr * 3 * BLKSZ)) \
74                         $BLKSZ >> "$seqres.full"
75 done
76
77 echo "Reflink block one to the fives"
78 seq 1 $((NR_BLKS / 5)) | while read nr; do
79         _reflink_range "$TESTDIR/file1" $BLKSZ "$TESTDIR/file1" \
80                         $((nr * 5 * BLKSZ)) $BLKSZ >> "$seqres.full"
81 done
82
83 echo "Dedupe block two to the sevens"
84 seq 1 $((NR_BLKS / 7)) | while read nr; do
85         _dedupe_range "$TESTDIR/file1" $((BLKSZ * 2)) "$TESTDIR/file1" \
86                         $((nr * 7 * BLKSZ)) $BLKSZ >> "$seqres.full"
87 done
88
89 _test_remount
90
91 echo "Check block mappings"
92 md5sum "$TESTDIR/file1" | _filter_test_dir
93
94 crcZ=$(_md5_range_checksum /dev/zero 0 $BLKSZ)
95 crc0=$(_md5_range_checksum "$TESTDIR/file1" 0 $BLKSZ)
96 crc1=$(_md5_range_checksum "$TESTDIR/file1" $BLKSZ $BLKSZ)
97 crc2=$(_md5_range_checksum "$TESTDIR/file1" $((BLKSZ * 2)) $BLKSZ)
98
99 check_block() {
100         lblk="$1"
101         rem7=$((lblk % 7))
102         rem5=$((lblk % 5))
103         rem3=$((lblk % 3))
104
105         crc=$(_md5_range_checksum "$TESTDIR/file1" $((lblk * BLKSZ)) $BLKSZ)
106
107         if [ $rem7 -eq 0 ]; then
108                 if [ $rem5 -eq 0 ]; then
109                         test $crc2 = $crc || echo "lblk $lblk doesn't match block 2"
110                 elif [ $rem3 -eq 0 ]; then
111                         test $crc0 = $crc || echo "lblk $lblk doesn't match block 0"
112                 elif [ $lblk -lt $((NR_BLKS / 2)) ]; then
113                         test $crcZ = $crc || echo "lblk $lblk isn't zeroed"
114                 fi
115         elif [ $rem5 -eq 0 ]; then
116                 test $crc1 = $crc || echo "lblk $lblk doesn't match block 1"
117         elif [ $rem3 -eq 0 ]; then
118                 test $crc0 = $crc || echo "lblk $lblk doesn't match block 0"
119         elif [ $lblk -lt $((NR_BLKS / 2)) ]; then
120                 test $crcZ = $crc || echo "lblk $lblk isn't zeroed"
121         fi
122 }
123
124 seq 3 $((NR_BLKS - 1)) | while read lblk; do
125         err="$(check_block $lblk)"
126         test -n "$err" && echo "$lblk: $err"
127 done
128
129 # success, all done
130 status=0
131 exit