generic: add _require_odirect to generic/113 and generic/214
[xfstests-dev.git] / tests / generic / 186
1 #! /bin/bash
2 # FS QA Test No. 186
3 #
4 # Ensuring that copy on write in buffered mode works when free space
5 # is heavily fragmented.
6 #   - Create two files
7 #   - Reflink the odd blocks of the first file into a third file.
8 #   - Reflink the even blocks of the second file into the third file.
9 #   - Try to fragment the free space by allocating a huge file and
10 #     punching out every other block.
11 #   - CoW across the halfway mark.
12 #   - Check that the files are now different where we say they're different.
13 #
14 #-----------------------------------------------------------------------
15 # Copyright (c) 2015, Oracle and/or its affiliates.  All Rights Reserved.
16 #
17 # This program is free software; you can redistribute it and/or
18 # modify it under the terms of the GNU General Public License as
19 # published by the Free Software Foundation.
20 #
21 # This program is distributed in the hope that it would be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write the Free Software Foundation,
28 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29 #-----------------------------------------------------------------------
30
31 seq=`basename $0`
32 seqres=$RESULT_DIR/$seq
33 echo "QA output created by $seq"
34
35 here=`pwd`
36 tmp=/tmp/$$
37 status=1    # failure is the default!
38 trap "_cleanup; exit \$status" 0 1 2 3 15
39
40 _cleanup()
41 {
42     cd /
43 #    rm -rf $tmp.* $testdir
44 }
45
46 # get standard environment, filters and checks
47 . ./common/rc
48 . ./common/filter
49 . ./common/reflink
50
51 # real QA test starts here
52 _supported_os Linux
53 _require_scratch_reflink
54 _require_cp_reflink
55 _require_xfs_io_command "falloc"
56 _require_xfs_io_command "fpunch"
57 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
58
59 rm -f $seqres.full
60
61 _fragment_freesp()
62 {
63         file=$1
64
65         # consume nearly all available space (leave ~1MB)
66         avail=`_get_available_space $SCRATCH_MNT`
67         echo "$avail bytes left"
68         filesize=$((avail - 1048576))
69         $XFS_IO_PROG -fc "truncate $filesize" $file
70
71         chunks=20
72         chunksizemb=$((filesize / chunks / 1048576))
73         seq 1 $chunks | while read f; do
74                 echo "$((f * chunksizemb)) file size $f / 20"
75                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
76         done
77
78         chunks=100
79         chunksizemb=$((filesize / chunks / 1048576))
80         seq 80 $chunks | while read f; do
81                 echo "$((f * chunksizemb)) file size $f / $chunks"
82                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
83         done
84
85         filesizemb=$((filesize / 1048576))
86         $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
87
88         # Try again anyway
89         avail=`_get_available_space $SCRATCH_MNT`
90         $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}.${i}
91
92         # Punch out whatever we need
93         seq 1 $((nr * 4)) | while read f; do
94                 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
95         done
96 }
97
98 echo "Format and mount"
99 _scratch_mkfs > $seqres.full 2>&1
100 _scratch_mount >> $seqres.full 2>&1
101
102 testdir=$SCRATCH_MNT/test-$seq
103 mkdir $testdir
104
105 echo "Create the original files"
106 blksz=65536
107 nr=1024
108 filesize=$((blksz * nr))
109 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
110 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
111 seq 0 2 $((nr-1)) | while read f; do
112         _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
113         _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
114 done
115 seq 1 2 $((nr-1)) | while read f; do
116         _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
117         _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
118 done
119 _scratch_remount
120 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
121 filesize=$((blksz * nr))
122 _scratch_remount
123
124 echo "Compare files"
125 md5sum $testdir/file1 | _filter_scratch
126 md5sum $testdir/file2 | _filter_scratch
127 md5sum $testdir/file3 | _filter_scratch
128 md5sum $testdir/file3.chk | _filter_scratch
129
130 echo "CoW with multiple extents?"
131 cowoff=$((filesize / 4))
132 cowsz=$((filesize / 2))
133 $XFS_IO_PROG -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
134 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
135 _scratch_remount
136
137 echo "Compare files"
138 md5sum $testdir/file1 | _filter_scratch
139 md5sum $testdir/file2 | _filter_scratch
140 md5sum $testdir/file3 | _filter_scratch
141 md5sum $testdir/file3.chk | _filter_scratch
142
143 # success, all done
144 status=0
145 exit