generic/520: Remove sync in clean_dir
[xfstests-dev.git] / tests / generic / 187
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. 187
6 #
7 # Ensuring that copy on write in directio mode works when free space
8 # is heavily fragmented.
9 #   - Create two files
10 #   - Reflink the odd blocks of the first file into a third file.
11 #   - Reflink the even blocks of the second file into the third file.
12 #   - Try to fragment the free space by allocating a huge file and
13 #     punching out every other block.
14 #   - CoW across the halfway mark.
15 #   - Check that the files are now different where we say they're different.
16 #
17 seq=`basename $0`
18 seqres=$RESULT_DIR/$seq
19 echo "QA output created by $seq"
20
21 here=`pwd`
22 tmp=/tmp/$$
23 status=1    # failure is the default!
24 trap "_cleanup; exit \$status" 0 1 2 3 15
25
26 _cleanup()
27 {
28     cd /
29 #    rm -rf $tmp.* $testdir
30 }
31
32 # get standard environment, filters and checks
33 . ./common/rc
34 . ./common/filter
35 . ./common/reflink
36
37 # real QA test starts here
38 _supported_os Linux
39 _require_scratch_reflink
40 _require_cp_reflink
41 _require_xfs_io_command "falloc"
42 _require_xfs_io_command "fpunch"
43 test $FSTYP = "btrfs" && _notrun "Can't fragment free space on btrfs."
44 _require_odirect
45
46 rm -f $seqres.full
47
48 _fragment_freesp()
49 {
50         file=$1
51
52         # consume nearly all available space (leave ~1MB)
53         avail=`_get_available_space $SCRATCH_MNT`
54         echo "$avail bytes left"
55         filesize=$((avail - 1048576))
56         $XFS_IO_PROG -fc "truncate $filesize" $file
57
58         chunks=20
59         chunksizemb=$((filesize / chunks / 1048576))
60         seq 1 $chunks | while read f; do
61                 echo "$((f * chunksizemb)) file size $f / 20"
62                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
63         done
64
65         chunks=100
66         chunksizemb=$((filesize / chunks / 1048576))
67         seq 80 $chunks | while read f; do
68                 echo "$((f * chunksizemb)) file size $f / $chunks"
69                 $XFS_IO_PROG -fc "falloc -k $(( (f - 1) * chunksizemb))m ${chunksizemb}m" $file
70         done
71
72         filesizemb=$((filesize / 1048576))
73         $XFS_IO_PROG -fc "falloc -k 0 ${filesizemb}m" $file
74
75         # Try again anyway
76         avail=`_get_available_space $SCRATCH_MNT`
77         $XFS_IO_PROG -fc "pwrite -S 0x65 0 $avail" ${file}
78
79         # Punch out whatever we need
80         seq 1 $((nr * 4)) | while read f; do
81                 $XFS_IO_PROG -f -c "fpunch $((f * 2 * blksz)) $blksz" $file
82         done
83 }
84
85 echo "Format and mount"
86 _scratch_mkfs > $seqres.full 2>&1
87 _scratch_mount >> $seqres.full 2>&1
88
89 testdir=$SCRATCH_MNT/test-$seq
90 mkdir $testdir
91
92 echo "Create the original files"
93 blksz=65536
94 nr=1024
95 filesize=$((blksz * nr))
96 _pwrite_byte 0x61 0 $filesize $testdir/file1 >> $seqres.full
97 _pwrite_byte 0x62 0 $filesize $testdir/file2 >> $seqres.full
98 seq 0 2 $((nr-1)) | while read f; do
99         _reflink_range $testdir/file1 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
100         _pwrite_byte 0x61 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
101 done
102 seq 1 2 $((nr-1)) | while read f; do
103         _reflink_range $testdir/file2 $((blksz * f)) $testdir/file3 $((blksz * f)) $blksz >> $seqres.full
104         _pwrite_byte 0x62 $((blksz * f)) $blksz $testdir/file3.chk >> $seqres.full
105 done
106 _scratch_cycle_mount
107 _fragment_freesp $testdir/bigfile >> $seqres.full 2>&1
108 filesize=$((blksz * nr))
109 _scratch_cycle_mount
110
111 echo "Compare files"
112 md5sum $testdir/file1 | _filter_scratch
113 md5sum $testdir/file2 | _filter_scratch
114 md5sum $testdir/file3 | _filter_scratch
115 md5sum $testdir/file3.chk | _filter_scratch
116
117 echo "CoW with multiple extents?"
118 cowoff=$((filesize / 4))
119 cowsz=$((filesize / 2))
120 $XFS_IO_PROG -d -f -c "pwrite -S 0x63 -b $cowsz $cowoff $cowsz" $testdir/file3 >> $seqres.full
121 _pwrite_byte 0x63 $cowoff $cowsz $testdir/file3.chk >> $seqres.full
122 _scratch_cycle_mount
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 # success, all done
131 status=0
132 exit